blob: 9a298875a186775be58cdd3ef685dce3e44fc11c [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
Michael W. Hudson9c14bad2002-06-19 15:44:15 +00006from test_support import verbose, TESTFN, unlink, TestFailed,\
7 have_unicode, vereq
Roger E. Massefab8ab81996-12-20 22:36:52 +00008
9def main():
Roger E. Massefab8ab81996-12-20 22:36:52 +000010 testtype('c', 'c')
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000011 if have_unicode:
12 testtype('u', unicode(r'\u263a', 'unicode-escape'))
Roger E. Massefab8ab81996-12-20 22:36:52 +000013 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
Guido van Rossum41360a41998-03-26 19:42:58 +000014 testtype(type, 1)
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000015 if have_unicode:
16 testunicode()
Martin v. Löwis99866332002-03-01 10:27:01 +000017 testsubclassing()
Guido van Rossumc9f8f141997-04-09 20:51:54 +000018 unlink(TESTFN)
19
Martin v. Löwis99866332002-03-01 10:27:01 +000020def testunicode():
21 try:
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000022 array.array('b', unicode('foo', 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +000023 except TypeError:
24 pass
25 else:
26 raise TestFailed("creating a non-unicode array from "
27 "a Unicode string should fail")
28
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000029 x = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape'))
30 x.fromunicode(unicode(' ', 'ascii'))
31 x.fromunicode(unicode('', 'ascii'))
32 x.fromunicode(unicode('', 'ascii'))
33 x.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape'))
Martin v. Löwis99866332002-03-01 10:27:01 +000034 s = x.tounicode()
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000035 if s != unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape'):
Martin v. Löwis99866332002-03-01 10:27:01 +000036 raise TestFailed("fromunicode()/tounicode()")
37
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000038 s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape')
Martin v. Löwis99866332002-03-01 10:27:01 +000039 a = array.array('u', s)
40 if verbose:
41 print "repr of type 'u' array:", repr(a)
42 print " expected: array('u', %r)" % s
43
44def testsubclassing():
45 class EditableString(array.array):
46 def __new__(cls, s, *args, **kwargs):
47 return array.array.__new__(cls, 'c', s)
48
49 def __init__(self, s, color='blue'):
50 array.array.__init__(self, 'c', s)
51 self.color = color
52
53 def strip(self):
54 self[:] = array.array('c', self.tostring().strip())
55
56 def __repr__(self):
57 return 'EditableString(%r)' % self.tostring()
58
59 s = EditableString("\ttest\r\n")
60 s.strip()
61 if s.tostring() != 'test':
62 raise TestFailed, "subclassing array.array failed somewhere"
63 if s.color != 'blue':
64 raise TestFailed, "assigning attributes to instance of array subclass"
65 s.color = 'red'
66 if s.color != 'red':
67 raise TestFailed, "assigning attributes to instance of array subclass"
68 if s.__dict__.keys() != ['color']:
69 raise TestFailed, "array subclass __dict__"
70
71 class ExaggeratingArray(array.array):
72 __slots__ = ['offset']
73
74 def __new__(cls, typecode, data, offset):
75 return array.array.__new__(cls, typecode, data)
76
77 def __init__(self, typecode, data, offset):
78 self.offset = offset
79
80 def __getitem__(self, i):
81 return array.array.__getitem__(self, i) + self.offset
82
83 a = ExaggeratingArray('i', [3, 6, 7, 11], 4)
84 if a[0] != 7:
85 raise TestFailed, "array subclass overriding __getitem__"
86 try:
87 a.color = 'blue'
88 except AttributeError:
89 pass
90 else:
91 raise TestFailed, "array subclass __slots__ was ignored"
92
Roger E. Masse8db1b071996-12-09 20:09:16 +000093
Fred Drake78334472000-06-28 17:50:51 +000094def testoverflow(type, lowerLimit, upperLimit):
Fred Drake004d5e62000-10-23 17:22:08 +000095 # should not overflow assigning lower limit
96 if verbose:
97 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`)
98 try:
99 a = array.array(type, [lowerLimit])
100 except:
101 raise TestFailed, "array(%s) overflowed assigning %s" %\
102 (`type`, `lowerLimit`)
103 # should overflow assigning less than lower limit
104 if verbose:
105 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`)
106 try:
107 a = array.array(type, [lowerLimit-1])
108 raise TestFailed, "array(%s) did not overflow assigning %s" %\
109 (`type`, `lowerLimit-1`)
110 except OverflowError:
111 pass
112 # should not overflow assigning upper limit
113 if verbose:
114 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`)
115 try:
116 a = array.array(type, [upperLimit])
117 except:
118 raise TestFailed, "array(%s) overflowed assigning %s" %\
119 (`type`, `upperLimit`)
120 # should overflow assigning more than upper limit
121 if verbose:
122 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`)
123 try:
124 a = array.array(type, [upperLimit+1])
125 raise TestFailed, "array(%s) did not overflow assigning %s" %\
126 (`type`, `upperLimit+1`)
127 except OverflowError:
128 pass
Fred Drake78334472000-06-28 17:50:51 +0000129
130
131
Roger E. Masse8db1b071996-12-09 20:09:16 +0000132def testtype(type, example):
Fred Drake004d5e62000-10-23 17:22:08 +0000133 a = array.array(type)
134 a.append(example)
135 if verbose:
136 print 40*'*'
137 print 'array after append: ', a
138 a.typecode
139 a.itemsize
140 if a.typecode in ('i', 'b', 'h', 'l'):
141 a.byteswap()
Roger E. Masse8db1b071996-12-09 20:09:16 +0000142
Fred Drake004d5e62000-10-23 17:22:08 +0000143 if a.typecode == 'c':
144 f = open(TESTFN, "w")
145 f.write("The quick brown fox jumps over the lazy dog.\n")
Guido van Rossum41360a41998-03-26 19:42:58 +0000146 f.close()
Fred Drake004d5e62000-10-23 17:22:08 +0000147 f = open(TESTFN, 'r')
148 a.fromfile(f, 10)
149 f.close()
Guido van Rossum41360a41998-03-26 19:42:58 +0000150 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000151 print 'char array with 10 bytes of TESTFN appended: ', a
152 a.fromlist(['a', 'b', 'c'])
Guido van Rossum41360a41998-03-26 19:42:58 +0000153 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000154 print 'char array with list appended: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +0000155
Fred Drake004d5e62000-10-23 17:22:08 +0000156 a.insert(0, example)
157 if verbose:
158 print 'array of %s after inserting another:' % a.typecode, a
159 f = open(TESTFN, 'w')
160 a.tofile(f)
161 f.close()
Tim Peterscc78e472000-11-14 21:36:07 +0000162
163 # This block is just to verify that the operations don't blow up.
Fred Drake004d5e62000-10-23 17:22:08 +0000164 a.tolist()
165 a.tostring()
Tim Peterscc78e472000-11-14 21:36:07 +0000166 repr(a)
167 str(a)
168
Fred Drake004d5e62000-10-23 17:22:08 +0000169 if verbose:
170 print 'array of %s converted to a list: ' % a.typecode, a.tolist()
171 if verbose:
172 print 'array of %s converted to a string: ' \
173 % a.typecode, `a.tostring()`
Guido van Rossum7f1d3aa1998-07-16 15:31:43 +0000174
Martin v. Löwis99866332002-03-01 10:27:01 +0000175 # Try out inplace addition and multiplication
176 a = array.array(type, [example])
177 b = a
178 a += array.array(type, [example]*2)
179 if a is not b:
180 raise TestFailed, "array(%s) inplace addition" % `type`
181 if a != array.array(type, [example] * 3):
182 raise TestFailed, "array(%s) inplace addition" % `type`
183
184 a *= 5
185 if a is not b:
186 raise TestFailed, "array(%s) inplace multiplication" % `type`
187 if a != array.array(type, [example] * 15):
188 raise TestFailed, "array(%s) inplace multiplication" % `type`
189
190 a *= 0
191 if a is not b:
192 raise TestFailed, "array(%s) inplace multiplication by 0" % `type`
193 if a != array.array(type, []):
194 raise TestFailed, "array(%s) inplace multiplication by 0" % `type`
195
196 a *= 1000
197 if a is not b:
198 raise TestFailed, "empty array(%s) inplace multiplication" % `type`
199 if a != array.array(type, []):
200 raise TestFailed, "empty array(%s) inplace multiplication" % `type`
201
Fred Drake004d5e62000-10-23 17:22:08 +0000202 if type == 'c':
203 a = array.array(type, "abcde")
204 a[:-1] = a
205 if a != array.array(type, "abcdee"):
206 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
207 a = array.array(type, "abcde")
208 a[1:] = a
209 if a != array.array(type, "aabcde"):
210 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
211 a = array.array(type, "abcde")
212 a[1:-1] = a
213 if a != array.array(type, "aabcdee"):
214 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
215 if a.index("e") != 5:
216 raise TestFailed, "array(%s) index-test" % `type`
217 if a.count("a") != 2:
218 raise TestFailed, "array(%s) count-test" % `type`
219 a.remove("e")
220 if a != array.array(type, "aabcde"):
221 raise TestFailed, "array(%s) remove-test" % `type`
222 if a.pop(0) != "a":
223 raise TestFailed, "array(%s) pop-test" % `type`
224 if a.pop(1) != "b":
225 raise TestFailed, "array(%s) pop-test" % `type`
226 a.extend(array.array(type, "xyz"))
227 if a != array.array(type, "acdexyz"):
228 raise TestFailed, "array(%s) extend-test" % `type`
229 a.pop()
230 a.pop()
231 a.pop()
232 x = a.pop()
233 if x != 'e':
234 raise TestFailed, "array(%s) pop-test" % `type`
235 if a != array.array(type, "acd"):
236 raise TestFailed, "array(%s) pop-test" % `type`
237 a.reverse()
238 if a != array.array(type, "dca"):
239 raise TestFailed, "array(%s) reverse-test" % `type`
Martin v. Löwis99866332002-03-01 10:27:01 +0000240 elif type == 'u':
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000241 a = array.array(type, unicode("abcde", 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +0000242 a[:-1] = a
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000243 if a != array.array(type, unicode("abcdee", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000244 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000245 a = array.array(type, unicode("abcde", 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +0000246 a[1:] = a
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000247 if a != array.array(type, unicode("aabcde", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000248 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000249 a = array.array(type, unicode("abcde", 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +0000250 a[1:-1] = a
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000251 if a != array.array(type, unicode("aabcdee", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000252 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000253 if a.index(unicode("e", 'ascii')) != 5:
Martin v. Löwis99866332002-03-01 10:27:01 +0000254 raise TestFailed, "array(%s) index-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000255 if a.count(unicode("a", 'ascii')) != 2:
Martin v. Löwis99866332002-03-01 10:27:01 +0000256 raise TestFailed, "array(%s) count-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000257 a.remove(unicode("e", 'ascii'))
258 if a != array.array(type, unicode("aabcde", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000259 raise TestFailed, "array(%s) remove-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000260 if a.pop(0) != unicode("a", 'ascii'):
Martin v. Löwis99866332002-03-01 10:27:01 +0000261 raise TestFailed, "array(%s) pop-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000262 if a.pop(1) != unicode("b", 'ascii'):
Martin v. Löwis99866332002-03-01 10:27:01 +0000263 raise TestFailed, "array(%s) pop-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000264 a.extend(array.array(type, unicode("xyz", 'ascii')))
265 if a != array.array(type, unicode("acdexyz", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000266 raise TestFailed, "array(%s) extend-test" % `type`
267 a.pop()
268 a.pop()
269 a.pop()
270 x = a.pop()
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000271 if x != unicode('e', 'ascii'):
Martin v. Löwis99866332002-03-01 10:27:01 +0000272 raise TestFailed, "array(%s) pop-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000273 if a != array.array(type, unicode("acd", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000274 raise TestFailed, "array(%s) pop-test" % `type`
275 a.reverse()
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000276 if a != array.array(type, unicode("dca", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000277 raise TestFailed, "array(%s) reverse-test" % `type`
Fred Drake004d5e62000-10-23 17:22:08 +0000278 else:
279 a = array.array(type, [1, 2, 3, 4, 5])
280 a[:-1] = a
281 if a != array.array(type, [1, 2, 3, 4, 5, 5]):
282 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
283 a = array.array(type, [1, 2, 3, 4, 5])
284 a[1:] = a
285 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
286 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
287 a = array.array(type, [1, 2, 3, 4, 5])
288 a[1:-1] = a
289 if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
290 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
291 if a.index(5) != 5:
292 raise TestFailed, "array(%s) index-test" % `type`
293 if a.count(1) != 2:
294 raise TestFailed, "array(%s) count-test" % `type`
295 a.remove(5)
296 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
297 raise TestFailed, "array(%s) remove-test" % `type`
298 if a.pop(0) != 1:
299 raise TestFailed, "array(%s) pop-test" % `type`
300 if a.pop(1) != 2:
301 raise TestFailed, "array(%s) pop-test" % `type`
302 a.extend(array.array(type, [7, 8, 9]))
303 if a != array.array(type, [1, 3, 4, 5, 7, 8, 9]):
304 raise TestFailed, "array(%s) extend-test" % `type`
305 a.pop()
306 a.pop()
307 a.pop()
308 x = a.pop()
309 if x != 5:
310 raise TestFailed, "array(%s) pop-test" % `type`
311 if a != array.array(type, [1, 3, 4]):
312 raise TestFailed, "array(%s) pop-test" % `type`
313 a.reverse()
314 if a != array.array(type, [4, 3, 1]):
315 raise TestFailed, "array(%s) reverse-test" % `type`
Michael W. Hudson9c14bad2002-06-19 15:44:15 +0000316 # extended slicing
317 # subscription
318 a = array.array(type, [0,1,2,3,4])
319 vereq(a[::], a)
320 vereq(a[::2], array.array(type, [0,2,4]))
321 vereq(a[1::2], array.array(type, [1,3]))
322 vereq(a[::-1], array.array(type, [4,3,2,1,0]))
323 vereq(a[::-2], array.array(type, [4,2,0]))
324 vereq(a[3::-2], array.array(type, [3,1]))
325 vereq(a[-100:100:], a)
326 vereq(a[100:-100:-1], a[::-1])
327 vereq(a[-100L:100L:2L], array.array(type, [0,2,4]))
328 vereq(a[1000:2000:2], array.array(type, []))
329 vereq(a[-1000:-2000:-2], array.array(type, []))
330 # deletion
331 del a[::2]
332 vereq(a, array.array(type, [1,3]))
333 a = array.array(type, range(5))
334 del a[1::2]
335 vereq(a, array.array(type, [0,2,4]))
336 a = array.array(type, range(5))
337 del a[1::-2]
338 vereq(a, array.array(type, [0,2,3,4]))
339 # assignment
340 a = array.array(type, range(10))
341 a[::2] = array.array(type, [-1]*5)
342 vereq(a, array.array(type, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
343 a = array.array(type, range(10))
344 a[::-4] = array.array(type, [10]*3)
345 vereq(a, array.array(type, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
346 a = array.array(type, range(4))
347 a[::-1] = a
348 vereq(a, array.array(type, [3, 2, 1, 0]))
349 a = array.array(type, range(10))
350 b = a[:]
351 c = a[:]
352 ins = array.array(type, range(2))
353 a[2:3] = ins
354 b[slice(2,3)] = ins
355 c[2:3:] = ins
Fred Drake004d5e62000-10-23 17:22:08 +0000356
357 # test that overflow exceptions are raised as expected for assignment
358 # to array of specific integral types
359 from math import pow
360 if type in ('b', 'h', 'i', 'l'):
361 # check signed and unsigned versions
362 a = array.array(type)
363 signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1))
364 signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L
365 unsignedLowerLimit = 0
366 unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L
367 testoverflow(type, signedLowerLimit, signedUpperLimit)
368 testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit)
369
370
371
Roger E. Massefab8ab81996-12-20 22:36:52 +0000372main()