blob: 1e0f1beaf8f6b0c05cc174d261ffed680df25306 [file] [log] [blame]
Guido van Rossum5de1f8d1996-12-10 16:02:14 +00001#! /usr/bin/env python
2"""Test the arraymodule.
Guido van Rossum228b8e81997-04-02 06:13:34 +00003 Roger E. Masse
Guido van Rossum5de1f8d1996-12-10 16:02:14 +00004"""
5import array
Guido van Rossume03c0501998-08-12 02:38:11 +00006from test_support import verbose, TESTFN, unlink, TestFailed
Guido van Rossum228b8e81997-04-02 06:13:34 +00007
8def main():
9
10 testtype('c', 'c')
11
12 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
Guido van Rossum548703a1998-03-26 22:14:20 +000013 testtype(type, 1)
Guido van Rossum228b8e81997-04-02 06:13:34 +000014
Guido van Rossumde554ec1997-05-08 23:14:57 +000015 unlink(TESTFN)
16
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000017
18def testtype(type, example):
19
Guido van Rossum548703a1998-03-26 22:14:20 +000020 a = array.array(type)
21 a.append(example)
22 if verbose:
23 print 40*'*'
24 print 'array after append: ', a
25 a.typecode
26 a.itemsize
27 if a.typecode in ('i', 'b', 'h', 'l'):
28 a.byteswap()
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000029
Guido van Rossum548703a1998-03-26 22:14:20 +000030 if a.typecode == 'c':
31 f = open(TESTFN, "w")
32 f.write("The quick brown fox jumps over the lazy dog.\n")
33 f.close()
34 f = open(TESTFN, 'r')
35 a.fromfile(f, 10)
36 f.close()
37 if verbose:
38 print 'char array with 10 bytes of TESTFN appended: ', a
39 a.fromlist(['a', 'b', 'c'])
40 if verbose:
41 print 'char array with list appended: ', a
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000042
Guido van Rossum548703a1998-03-26 22:14:20 +000043 a.insert(0, example)
44 if verbose:
45 print 'array of %s after inserting another:' % a.typecode, a
46 f = open(TESTFN, 'w')
47 a.tofile(f)
48 f.close()
49 a.tolist()
50 a.tostring()
51 if verbose:
52 print 'array of %s converted to a list: ' % a.typecode, a.tolist()
53 if verbose:
54 print 'array of %s converted to a string: ' \
55 % a.typecode, `a.tostring()`
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000056
Guido van Rossume03c0501998-08-12 02:38:11 +000057 if type == 'c':
58 a = array.array(type, "abcde")
59 a[:-1] = a
60 if a != array.array(type, "abcdee"):
61 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
62 a = array.array(type, "abcde")
63 a[1:] = a
64 if a != array.array(type, "aabcde"):
65 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
66 a = array.array(type, "abcde")
67 a[1:-1] = a
68 if a != array.array(type, "aabcdee"):
69 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
70 else:
71 a = array.array(type, [1, 2, 3, 4, 5])
72 a[:-1] = a
73 if a != array.array(type, [1, 2, 3, 4, 5, 5]):
74 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
75 a = array.array(type, [1, 2, 3, 4, 5])
76 a[1:] = a
77 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
78 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
79 a = array.array(type, [1, 2, 3, 4, 5])
80 a[1:-1] = a
81 if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
82 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
83
Guido van Rossumde554ec1997-05-08 23:14:57 +000084
Guido van Rossum228b8e81997-04-02 06:13:34 +000085main()
Guido van Rossum548703a1998-03-26 22:14:20 +000086