2 """Test the arraymodule.
6 from test
.test_support
import verbose
, TESTFN
, unlink
, TestFailed
,\
12 testtype('u', unicode(r
'\u263a', 'unicode-escape'))
13 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
22 array
.array('b', unicode('foo', 'ascii'))
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'))
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)
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)
54 self[:] = array.array('c
', self.tostring().strip())
57 return 'EditableString(%r)' % self.tostring()
59 s = EditableString("\ttest\r\n")
61 if s.tostring() != 'test
':
62 raise TestFailed, "subclassing array.array failed somewhere"
64 raise TestFailed, "assigning attributes to instance of array subclass"
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):
80 def __getitem__(self, i):
81 return array.array.__getitem__(self, i) + self.offset
83 a = ExaggeratingArray('i
', [3, 6, 7, 11], 4)
85 raise TestFailed, "array subclass overriding __getitem__"
88 except AttributeError:
91 raise TestFailed, "array subclass __slots__ was ignored"
94 def testoverflow(type, lowerLimit, upperLimit):
95 # should not overflow assigning lower limit
97 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`)
99 a = array.array(type, [lowerLimit])
101 raise TestFailed, "array(%s) overflowed assigning %s" %\
102 (`type`, `lowerLimit`)
103 # should overflow assigning less than lower limit
105 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`)
107 a = array.array(type, [lowerLimit-1])
108 raise TestFailed, "array(%s) did not overflow assigning %s" %\
109 (`type`, `lowerLimit-1`)
110 except OverflowError:
112 # should not overflow assigning upper limit
114 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`)
116 a = array.array(type, [upperLimit])
118 raise TestFailed, "array(%s) overflowed assigning %s" %\
119 (`type`, `upperLimit`)
120 # should overflow assigning more than upper limit
122 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`)
124 a = array.array(type, [upperLimit+1])
125 raise TestFailed, "array(%s) did not overflow assigning %s" %\
126 (`type`, `upperLimit+1`)
127 except OverflowError:
132 def testtype(type, example):
133 a = array.array(type)
137 print 'array after append
: ', a
140 if a.typecode in ('i
', 'b
', 'h
', 'l
'):
143 if a.typecode == 'c
':
144 f = open(TESTFN, "w")
145 f.write("The quick brown fox jumps over the lazy dog.\n")
147 f = open(TESTFN, 'r
')
151 print 'char array with
10 bytes of TESTFN appended
: ', a
152 a.fromlist(['a
', 'b
', 'c
'])
154 print 'char array with
list appended
: ', a
158 print 'array of
%s after inserting another
:' % a.typecode, a
159 f = open(TESTFN, 'w
')
163 # This block is just to verify that the operations don't blow up
.
170 print 'array of %s converted to a list: ' % a
.typecode
, a
.tolist()
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
])
178 a
+= array
.array(type, [example
]*2)
180 raise TestFailed
, "array(%s) inplace addition" % `
type`
181 if a
!= array
.array(type, [example
] * 3):
182 raise TestFailed
, "array(%s) inplace addition" % `
type`
186 raise TestFailed
, "array(%s) inplace multiplication" % `
type`
187 if a
!= array
.array(type, [example
] * 15):
188 raise TestFailed
, "array(%s) inplace multiplication" % `
type`
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`
198 raise TestFailed
, "empty array(%s) inplace multiplication" % `
type`
199 if a
!= array
.array(type, []):
200 raise TestFailed
, "empty array(%s) inplace multiplication" % `
type`
203 a
= array
.array(type, "abcde")
205 if a
!= array
.array(type, "abcdee"):
206 raise TestFailed
, "array(%s) self-slice-assign (head)" % `
type`
207 a
= array
.array(type, "abcde")
209 if a
!= array
.array(type, "aabcde"):
210 raise TestFailed
, "array(%s) self-slice-assign (tail)" % `
type`
211 a
= array
.array(type, "abcde")
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`
220 if a
!= array
.array(type, "aabcde"):
221 raise TestFailed
, "array(%s) remove-test" % `
type`
223 raise TestFailed
, "array(%s) pop-test" % `
type`
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`
234 raise TestFailed
, "array(%s) pop-test" % `
type`
235 if a
!= array
.array(type, "acd"):
236 raise TestFailed
, "array(%s) pop-test" % `
type`
238 if a
!= array
.array(type, "dca"):
239 raise TestFailed
, "array(%s) reverse-test" % `
type`
241 a
= array
.array(type, unicode("abcde", 'ascii'))
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'))
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'))
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`
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`
276 if a
!= array
.array(type, unicode("dca", 'ascii')):
277 raise TestFailed
, "array(%s) reverse-test" % `
type`
279 a
= array
.array(type, [1, 2, 3, 4, 5])
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])
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])
289 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5, 5]):
290 raise TestFailed
, "array(%s) self-slice-assign (cntr)" % `
type`
292 raise TestFailed
, "array(%s) index-test" % `
type`
294 raise TestFailed
, "array(%s) count-test" % `
type`
296 if a
!= array
.array(type, [1, 1, 2, 3, 4, 5]):
297 raise TestFailed
, "array(%s) remove-test" % `
type`
299 raise TestFailed
, "array(%s) pop-test" % `
type`
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`
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`
314 if a
!= array
.array(type, [4, 3, 1]):
315 raise TestFailed
, "array(%s) reverse-test" % `
type`
318 a
= array
.array(type, [0,1,2,3,4])
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, []))
332 vereq(a
, array
.array(type, [1,3]))
333 a
= array
.array(type, range(5))
335 vereq(a
, array
.array(type, [0,2,4]))
336 a
= array
.array(type, range(5))
338 vereq(a
, array
.array(type, [0,2,3,4]))
339 a
= array
.array(type, range(10))
341 vereq(a
, array
.array(type, [1,2,3,4,5,6,7,8,9]))
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))
351 vereq(a
, array
.array(type, [3, 2, 1, 0]))
352 a
= array
.array(type, range(10))
355 ins
= array
.array(type, range(2))
360 # test that overflow exceptions are raised as expected for assignment
361 # to array of specific integral types
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
)