blob: 4a64bd63ea41aa576acfdf6f07e6b7b2757e3f4a [file] [log] [blame]
Roger E. Masse8db1b071996-12-09 20:09:16 +00001#! /usr/bin/env python
2"""Test the arraymodule.
Roger E. Massefab8ab81996-12-20 22:36:52 +00003 Roger E. Masse
Roger E. Masse8db1b071996-12-09 20:09:16 +00004"""
5import array
Guido van Rossum7f1d3aa1998-07-16 15:31:43 +00006from test_support import verbose, TESTFN, unlink, TestFailed
Roger E. Massefab8ab81996-12-20 22:36:52 +00007
8def main():
9
10 testtype('c', 'c')
11
12 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
Guido van Rossum41360a41998-03-26 19:42:58 +000013 testtype(type, 1)
Roger E. Massefab8ab81996-12-20 22:36:52 +000014
Guido van Rossumc9f8f141997-04-09 20:51:54 +000015 unlink(TESTFN)
16
Roger E. Masse8db1b071996-12-09 20:09:16 +000017
Fred Drake78334472000-06-28 17:50:51 +000018def testoverflow(type, lowerLimit, upperLimit):
Fred Drake004d5e62000-10-23 17:22:08 +000019 # should not overflow assigning lower limit
20 if verbose:
21 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`)
22 try:
23 a = array.array(type, [lowerLimit])
24 except:
25 raise TestFailed, "array(%s) overflowed assigning %s" %\
26 (`type`, `lowerLimit`)
27 # should overflow assigning less than lower limit
28 if verbose:
29 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`)
30 try:
31 a = array.array(type, [lowerLimit-1])
32 raise TestFailed, "array(%s) did not overflow assigning %s" %\
33 (`type`, `lowerLimit-1`)
34 except OverflowError:
35 pass
36 # should not overflow assigning upper limit
37 if verbose:
38 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`)
39 try:
40 a = array.array(type, [upperLimit])
41 except:
42 raise TestFailed, "array(%s) overflowed assigning %s" %\
43 (`type`, `upperLimit`)
44 # should overflow assigning more than upper limit
45 if verbose:
46 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`)
47 try:
48 a = array.array(type, [upperLimit+1])
49 raise TestFailed, "array(%s) did not overflow assigning %s" %\
50 (`type`, `upperLimit+1`)
51 except OverflowError:
52 pass
Fred Drake78334472000-06-28 17:50:51 +000053
54
55
Roger E. Masse8db1b071996-12-09 20:09:16 +000056def testtype(type, example):
57
Fred Drake004d5e62000-10-23 17:22:08 +000058 a = array.array(type)
59 a.append(example)
60 if verbose:
61 print 40*'*'
62 print 'array after append: ', a
63 a.typecode
64 a.itemsize
65 if a.typecode in ('i', 'b', 'h', 'l'):
66 a.byteswap()
Roger E. Masse8db1b071996-12-09 20:09:16 +000067
Fred Drake004d5e62000-10-23 17:22:08 +000068 if a.typecode == 'c':
69 f = open(TESTFN, "w")
70 f.write("The quick brown fox jumps over the lazy dog.\n")
Guido van Rossum41360a41998-03-26 19:42:58 +000071 f.close()
Fred Drake004d5e62000-10-23 17:22:08 +000072 f = open(TESTFN, 'r')
73 a.fromfile(f, 10)
74 f.close()
Guido van Rossum41360a41998-03-26 19:42:58 +000075 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000076 print 'char array with 10 bytes of TESTFN appended: ', a
77 a.fromlist(['a', 'b', 'c'])
Guido van Rossum41360a41998-03-26 19:42:58 +000078 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000079 print 'char array with list appended: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +000080
Fred Drake004d5e62000-10-23 17:22:08 +000081 a.insert(0, example)
82 if verbose:
83 print 'array of %s after inserting another:' % a.typecode, a
84 f = open(TESTFN, 'w')
85 a.tofile(f)
86 f.close()
87 a.tolist()
88 a.tostring()
89 if verbose:
90 print 'array of %s converted to a list: ' % a.typecode, a.tolist()
91 if verbose:
92 print 'array of %s converted to a string: ' \
93 % a.typecode, `a.tostring()`
Guido van Rossum7f1d3aa1998-07-16 15:31:43 +000094
Fred Drake004d5e62000-10-23 17:22:08 +000095 if type == 'c':
96 a = array.array(type, "abcde")
97 a[:-1] = a
98 if a != array.array(type, "abcdee"):
99 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
100 a = array.array(type, "abcde")
101 a[1:] = a
102 if a != array.array(type, "aabcde"):
103 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
104 a = array.array(type, "abcde")
105 a[1:-1] = a
106 if a != array.array(type, "aabcdee"):
107 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
108 if a.index("e") != 5:
109 raise TestFailed, "array(%s) index-test" % `type`
110 if a.count("a") != 2:
111 raise TestFailed, "array(%s) count-test" % `type`
112 a.remove("e")
113 if a != array.array(type, "aabcde"):
114 raise TestFailed, "array(%s) remove-test" % `type`
115 if a.pop(0) != "a":
116 raise TestFailed, "array(%s) pop-test" % `type`
117 if a.pop(1) != "b":
118 raise TestFailed, "array(%s) pop-test" % `type`
119 a.extend(array.array(type, "xyz"))
120 if a != array.array(type, "acdexyz"):
121 raise TestFailed, "array(%s) extend-test" % `type`
122 a.pop()
123 a.pop()
124 a.pop()
125 x = a.pop()
126 if x != 'e':
127 raise TestFailed, "array(%s) pop-test" % `type`
128 if a != array.array(type, "acd"):
129 raise TestFailed, "array(%s) pop-test" % `type`
130 a.reverse()
131 if a != array.array(type, "dca"):
132 raise TestFailed, "array(%s) reverse-test" % `type`
133 else:
134 a = array.array(type, [1, 2, 3, 4, 5])
135 a[:-1] = a
136 if a != array.array(type, [1, 2, 3, 4, 5, 5]):
137 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
138 a = array.array(type, [1, 2, 3, 4, 5])
139 a[1:] = a
140 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
141 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
142 a = array.array(type, [1, 2, 3, 4, 5])
143 a[1:-1] = a
144 if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
145 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
146 if a.index(5) != 5:
147 raise TestFailed, "array(%s) index-test" % `type`
148 if a.count(1) != 2:
149 raise TestFailed, "array(%s) count-test" % `type`
150 a.remove(5)
151 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
152 raise TestFailed, "array(%s) remove-test" % `type`
153 if a.pop(0) != 1:
154 raise TestFailed, "array(%s) pop-test" % `type`
155 if a.pop(1) != 2:
156 raise TestFailed, "array(%s) pop-test" % `type`
157 a.extend(array.array(type, [7, 8, 9]))
158 if a != array.array(type, [1, 3, 4, 5, 7, 8, 9]):
159 raise TestFailed, "array(%s) extend-test" % `type`
160 a.pop()
161 a.pop()
162 a.pop()
163 x = a.pop()
164 if x != 5:
165 raise TestFailed, "array(%s) pop-test" % `type`
166 if a != array.array(type, [1, 3, 4]):
167 raise TestFailed, "array(%s) pop-test" % `type`
168 a.reverse()
169 if a != array.array(type, [4, 3, 1]):
170 raise TestFailed, "array(%s) reverse-test" % `type`
171
172 # test that overflow exceptions are raised as expected for assignment
173 # to array of specific integral types
174 from math import pow
175 if type in ('b', 'h', 'i', 'l'):
176 # check signed and unsigned versions
177 a = array.array(type)
178 signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1))
179 signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L
180 unsignedLowerLimit = 0
181 unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L
182 testoverflow(type, signedLowerLimit, signedUpperLimit)
183 testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit)
184
185
186
Roger E. Massefab8ab81996-12-20 22:36:52 +0000187main()