append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Lib / test / test_array.py
blobb650033292fc64574e1608db84a5725aaf1d582f
1 #! /usr/bin/env python
2 """Test the arraymodule.
3 Roger E. Masse
4 """
5 import array
6 from test.test_support import verbose, TESTFN, unlink, TestFailed,\
7 have_unicode, vereq
9 def main():
10 testtype('c', 'c')
11 if have_unicode:
12 testtype('u', unicode(r'\u263a', 'unicode-escape'))
13 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
14 testtype(type, 1)
15 if have_unicode:
16 testunicode()
17 testsubclassing()
18 unlink(TESTFN)
20 def testunicode():
21 try:
22 array.array('b', unicode('foo', 'ascii'))
23 except TypeError:
24 pass
25 else:
26 raise TestFailed("creating a non-unicode array from "
27 "a Unicode string should fail")
29 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'))
34 s = x.tounicode()
35 if s != unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape'):
36 raise TestFailed("fromunicode()/tounicode()")
38 s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape')
39 a = array.array('u', s)
40 if verbose:
41 print "repr of type 'u' array:", repr(a)
42 print " expected: array('u', %r)" % s
44 def testsubclassing():
45 class EditableString(array.array):
46 def __new__(cls, s, *args, **kwargs):
47 return array.array.__new__(cls, 'c', s)
49 def __init__(self, s, color='blue'):
50 array.array.__init__(self, 'c', s)
51 self.color = color
53 def strip(self):
54 self[:] = array.array('c', self.tostring().strip())
56 def __repr__(self):
57 return 'EditableString(%r)' % self.tostring()
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__"
71 class ExaggeratingArray(array.array):
72 __slots__ = ['offset']
74 def __new__(cls, typecode, data, offset):
75 return array.array.__new__(cls, typecode, data)
77 def __init__(self, typecode, data, offset):
78 self.offset = offset
80 def __getitem__(self, i):
81 return array.array.__getitem__(self, i) + self.offset
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"
94 def testoverflow(type, lowerLimit, upperLimit):
95 # 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
132 def testtype(type, example):
133 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()
143 if a.typecode == 'c':
144 f = open(TESTFN, "w")
145 f.write("The quick brown fox jumps over the lazy dog.\n")
146 f.close()
147 f = open(TESTFN, 'r')
148 a.fromfile(f, 10)
149 f.close()
150 if verbose:
151 print 'char array with 10 bytes of TESTFN appended: ', a
152 a.fromlist(['a', 'b', 'c'])
153 if verbose:
154 print 'char array with list appended: ', a
156 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()
163 # This block is just to verify that the operations don't blow up.
164 a.tolist()
165 a.tostring()
166 repr(a)
167 str(a)
169 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()`
175 # 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`
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`
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`
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`
202 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`
240 elif type == 'u':
241 a = array.array(type, unicode("abcde", 'ascii'))
242 a[:-1] = a
243 if a != array.array(type, unicode("abcdee", 'ascii')):
244 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
245 a = array.array(type, unicode("abcde", 'ascii'))
246 a[1:] = a
247 if a != array.array(type, unicode("aabcde", 'ascii')):
248 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
249 a = array.array(type, unicode("abcde", 'ascii'))
250 a[1:-1] = a
251 if a != array.array(type, unicode("aabcdee", 'ascii')):
252 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
253 if a.index(unicode("e", 'ascii')) != 5:
254 raise TestFailed, "array(%s) index-test" % `type`
255 if a.count(unicode("a", 'ascii')) != 2:
256 raise TestFailed, "array(%s) count-test" % `type`
257 a.remove(unicode("e", 'ascii'))
258 if a != array.array(type, unicode("aabcde", 'ascii')):
259 raise TestFailed, "array(%s) remove-test" % `type`
260 if a.pop(0) != unicode("a", 'ascii'):
261 raise TestFailed, "array(%s) pop-test" % `type`
262 if a.pop(1) != unicode("b", 'ascii'):
263 raise TestFailed, "array(%s) pop-test" % `type`
264 a.extend(array.array(type, unicode("xyz", 'ascii')))
265 if a != array.array(type, unicode("acdexyz", 'ascii')):
266 raise TestFailed, "array(%s) extend-test" % `type`
267 a.pop()
268 a.pop()
269 a.pop()
270 x = a.pop()
271 if x != unicode('e', 'ascii'):
272 raise TestFailed, "array(%s) pop-test" % `type`
273 if a != array.array(type, unicode("acd", 'ascii')):
274 raise TestFailed, "array(%s) pop-test" % `type`
275 a.reverse()
276 if a != array.array(type, unicode("dca", 'ascii')):
277 raise TestFailed, "array(%s) reverse-test" % `type`
278 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`
316 # 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 a = array.array(type, range(10))
340 del a[::1000]
341 vereq(a, array.array(type, [1,2,3,4,5,6,7,8,9]))
342 # assignment
343 a = array.array(type, range(10))
344 a[::2] = array.array(type, [-1]*5)
345 vereq(a, array.array(type, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
346 a = array.array(type, range(10))
347 a[::-4] = array.array(type, [10]*3)
348 vereq(a, array.array(type, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
349 a = array.array(type, range(4))
350 a[::-1] = a
351 vereq(a, array.array(type, [3, 2, 1, 0]))
352 a = array.array(type, range(10))
353 b = a[:]
354 c = a[:]
355 ins = array.array(type, range(2))
356 a[2:3] = ins
357 b[slice(2,3)] = ins
358 c[2:3:] = ins
360 # test that overflow exceptions are raised as expected for assignment
361 # to array of specific integral types
362 from math import pow
363 if type in ('b', 'h', 'i', 'l'):
364 # check signed and unsigned versions
365 a = array.array(type)
366 signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1))
367 signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L
368 unsignedLowerLimit = 0
369 unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L
370 testoverflow(type, signedLowerLimit, signedUpperLimit)
371 testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit)
375 main()