blob: 681a4d0d73f19a3df2ce5a4d3ae609cb608f5d61 [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. Hudson8bf46e42002-05-15 13:04:53 +00006from test_support import verbose, TESTFN, unlink, TestFailed, have_unicode
Roger E. Massefab8ab81996-12-20 22:36:52 +00007
8def main():
Roger E. Massefab8ab81996-12-20 22:36:52 +00009 testtype('c', 'c')
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000010 if have_unicode:
11 testtype('u', unicode(r'\u263a', 'unicode-escape'))
Roger E. Massefab8ab81996-12-20 22:36:52 +000012 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
Guido van Rossum41360a41998-03-26 19:42:58 +000013 testtype(type, 1)
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000014 if have_unicode:
15 testunicode()
Martin v. Löwis99866332002-03-01 10:27:01 +000016 testsubclassing()
Guido van Rossumc9f8f141997-04-09 20:51:54 +000017 unlink(TESTFN)
18
Martin v. Löwis99866332002-03-01 10:27:01 +000019def testunicode():
20 try:
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000021 array.array('b', unicode('foo', 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +000022 except TypeError:
23 pass
24 else:
25 raise TestFailed("creating a non-unicode array from "
26 "a Unicode string should fail")
27
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000028 x = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape'))
29 x.fromunicode(unicode(' ', 'ascii'))
30 x.fromunicode(unicode('', 'ascii'))
31 x.fromunicode(unicode('', 'ascii'))
32 x.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape'))
Martin v. Löwis99866332002-03-01 10:27:01 +000033 s = x.tounicode()
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000034 if s != unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape'):
Martin v. Löwis99866332002-03-01 10:27:01 +000035 raise TestFailed("fromunicode()/tounicode()")
36
Michael W. Hudson8bf46e42002-05-15 13:04:53 +000037 s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape')
Martin v. Löwis99866332002-03-01 10:27:01 +000038 a = array.array('u', s)
39 if verbose:
40 print "repr of type 'u' array:", repr(a)
41 print " expected: array('u', %r)" % s
42
43def testsubclassing():
44 class EditableString(array.array):
45 def __new__(cls, s, *args, **kwargs):
46 return array.array.__new__(cls, 'c', s)
47
48 def __init__(self, s, color='blue'):
49 array.array.__init__(self, 'c', s)
50 self.color = color
51
52 def strip(self):
53 self[:] = array.array('c', self.tostring().strip())
54
55 def __repr__(self):
56 return 'EditableString(%r)' % self.tostring()
57
58 s = EditableString("\ttest\r\n")
59 s.strip()
60 if s.tostring() != 'test':
61 raise TestFailed, "subclassing array.array failed somewhere"
62 if s.color != 'blue':
63 raise TestFailed, "assigning attributes to instance of array subclass"
64 s.color = 'red'
65 if s.color != 'red':
66 raise TestFailed, "assigning attributes to instance of array subclass"
67 if s.__dict__.keys() != ['color']:
68 raise TestFailed, "array subclass __dict__"
69
70 class ExaggeratingArray(array.array):
71 __slots__ = ['offset']
72
73 def __new__(cls, typecode, data, offset):
74 return array.array.__new__(cls, typecode, data)
75
76 def __init__(self, typecode, data, offset):
77 self.offset = offset
78
79 def __getitem__(self, i):
80 return array.array.__getitem__(self, i) + self.offset
81
82 a = ExaggeratingArray('i', [3, 6, 7, 11], 4)
83 if a[0] != 7:
84 raise TestFailed, "array subclass overriding __getitem__"
85 try:
86 a.color = 'blue'
87 except AttributeError:
88 pass
89 else:
90 raise TestFailed, "array subclass __slots__ was ignored"
91
Roger E. Masse8db1b071996-12-09 20:09:16 +000092
Fred Drake78334472000-06-28 17:50:51 +000093def testoverflow(type, lowerLimit, upperLimit):
Fred Drake004d5e62000-10-23 17:22:08 +000094 # should not overflow assigning lower limit
95 if verbose:
96 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`)
97 try:
98 a = array.array(type, [lowerLimit])
99 except:
100 raise TestFailed, "array(%s) overflowed assigning %s" %\
101 (`type`, `lowerLimit`)
102 # should overflow assigning less than lower limit
103 if verbose:
104 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`)
105 try:
106 a = array.array(type, [lowerLimit-1])
107 raise TestFailed, "array(%s) did not overflow assigning %s" %\
108 (`type`, `lowerLimit-1`)
109 except OverflowError:
110 pass
111 # should not overflow assigning upper limit
112 if verbose:
113 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`)
114 try:
115 a = array.array(type, [upperLimit])
116 except:
117 raise TestFailed, "array(%s) overflowed assigning %s" %\
118 (`type`, `upperLimit`)
119 # should overflow assigning more than upper limit
120 if verbose:
121 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`)
122 try:
123 a = array.array(type, [upperLimit+1])
124 raise TestFailed, "array(%s) did not overflow assigning %s" %\
125 (`type`, `upperLimit+1`)
126 except OverflowError:
127 pass
Fred Drake78334472000-06-28 17:50:51 +0000128
129
130
Roger E. Masse8db1b071996-12-09 20:09:16 +0000131def testtype(type, example):
Fred Drake004d5e62000-10-23 17:22:08 +0000132 a = array.array(type)
133 a.append(example)
134 if verbose:
135 print 40*'*'
136 print 'array after append: ', a
137 a.typecode
138 a.itemsize
139 if a.typecode in ('i', 'b', 'h', 'l'):
140 a.byteswap()
Roger E. Masse8db1b071996-12-09 20:09:16 +0000141
Fred Drake004d5e62000-10-23 17:22:08 +0000142 if a.typecode == 'c':
143 f = open(TESTFN, "w")
144 f.write("The quick brown fox jumps over the lazy dog.\n")
Guido van Rossum41360a41998-03-26 19:42:58 +0000145 f.close()
Fred Drake004d5e62000-10-23 17:22:08 +0000146 f = open(TESTFN, 'r')
147 a.fromfile(f, 10)
148 f.close()
Guido van Rossum41360a41998-03-26 19:42:58 +0000149 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000150 print 'char array with 10 bytes of TESTFN appended: ', a
151 a.fromlist(['a', 'b', 'c'])
Guido van Rossum41360a41998-03-26 19:42:58 +0000152 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000153 print 'char array with list appended: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +0000154
Fred Drake004d5e62000-10-23 17:22:08 +0000155 a.insert(0, example)
156 if verbose:
157 print 'array of %s after inserting another:' % a.typecode, a
158 f = open(TESTFN, 'w')
159 a.tofile(f)
160 f.close()
Tim Peterscc78e472000-11-14 21:36:07 +0000161
162 # This block is just to verify that the operations don't blow up.
Fred Drake004d5e62000-10-23 17:22:08 +0000163 a.tolist()
164 a.tostring()
Tim Peterscc78e472000-11-14 21:36:07 +0000165 repr(a)
166 str(a)
167
Fred Drake004d5e62000-10-23 17:22:08 +0000168 if verbose:
169 print 'array of %s converted to a list: ' % a.typecode, a.tolist()
170 if verbose:
171 print 'array of %s converted to a string: ' \
172 % a.typecode, `a.tostring()`
Guido van Rossum7f1d3aa1998-07-16 15:31:43 +0000173
Martin v. Löwis99866332002-03-01 10:27:01 +0000174 # Try out inplace addition and multiplication
175 a = array.array(type, [example])
176 b = a
177 a += array.array(type, [example]*2)
178 if a is not b:
179 raise TestFailed, "array(%s) inplace addition" % `type`
180 if a != array.array(type, [example] * 3):
181 raise TestFailed, "array(%s) inplace addition" % `type`
182
183 a *= 5
184 if a is not b:
185 raise TestFailed, "array(%s) inplace multiplication" % `type`
186 if a != array.array(type, [example] * 15):
187 raise TestFailed, "array(%s) inplace multiplication" % `type`
188
189 a *= 0
190 if a is not b:
191 raise TestFailed, "array(%s) inplace multiplication by 0" % `type`
192 if a != array.array(type, []):
193 raise TestFailed, "array(%s) inplace multiplication by 0" % `type`
194
195 a *= 1000
196 if a is not b:
197 raise TestFailed, "empty array(%s) inplace multiplication" % `type`
198 if a != array.array(type, []):
199 raise TestFailed, "empty array(%s) inplace multiplication" % `type`
200
Fred Drake004d5e62000-10-23 17:22:08 +0000201 if type == 'c':
202 a = array.array(type, "abcde")
203 a[:-1] = a
204 if a != array.array(type, "abcdee"):
205 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
206 a = array.array(type, "abcde")
207 a[1:] = a
208 if a != array.array(type, "aabcde"):
209 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
210 a = array.array(type, "abcde")
211 a[1:-1] = a
212 if a != array.array(type, "aabcdee"):
213 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
214 if a.index("e") != 5:
215 raise TestFailed, "array(%s) index-test" % `type`
216 if a.count("a") != 2:
217 raise TestFailed, "array(%s) count-test" % `type`
218 a.remove("e")
219 if a != array.array(type, "aabcde"):
220 raise TestFailed, "array(%s) remove-test" % `type`
221 if a.pop(0) != "a":
222 raise TestFailed, "array(%s) pop-test" % `type`
223 if a.pop(1) != "b":
224 raise TestFailed, "array(%s) pop-test" % `type`
225 a.extend(array.array(type, "xyz"))
226 if a != array.array(type, "acdexyz"):
227 raise TestFailed, "array(%s) extend-test" % `type`
228 a.pop()
229 a.pop()
230 a.pop()
231 x = a.pop()
232 if x != 'e':
233 raise TestFailed, "array(%s) pop-test" % `type`
234 if a != array.array(type, "acd"):
235 raise TestFailed, "array(%s) pop-test" % `type`
236 a.reverse()
237 if a != array.array(type, "dca"):
238 raise TestFailed, "array(%s) reverse-test" % `type`
Martin v. Löwis99866332002-03-01 10:27:01 +0000239 elif type == 'u':
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000240 a = array.array(type, unicode("abcde", 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +0000241 a[:-1] = a
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000242 if a != array.array(type, unicode("abcdee", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000243 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000244 a = array.array(type, unicode("abcde", 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +0000245 a[1:] = a
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000246 if a != array.array(type, unicode("aabcde", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000247 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000248 a = array.array(type, unicode("abcde", 'ascii'))
Martin v. Löwis99866332002-03-01 10:27:01 +0000249 a[1:-1] = a
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000250 if a != array.array(type, unicode("aabcdee", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000251 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000252 if a.index(unicode("e", 'ascii')) != 5:
Martin v. Löwis99866332002-03-01 10:27:01 +0000253 raise TestFailed, "array(%s) index-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000254 if a.count(unicode("a", 'ascii')) != 2:
Martin v. Löwis99866332002-03-01 10:27:01 +0000255 raise TestFailed, "array(%s) count-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000256 a.remove(unicode("e", 'ascii'))
257 if a != array.array(type, unicode("aabcde", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000258 raise TestFailed, "array(%s) remove-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000259 if a.pop(0) != unicode("a", 'ascii'):
Martin v. Löwis99866332002-03-01 10:27:01 +0000260 raise TestFailed, "array(%s) pop-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000261 if a.pop(1) != unicode("b", 'ascii'):
Martin v. Löwis99866332002-03-01 10:27:01 +0000262 raise TestFailed, "array(%s) pop-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000263 a.extend(array.array(type, unicode("xyz", 'ascii')))
264 if a != array.array(type, unicode("acdexyz", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000265 raise TestFailed, "array(%s) extend-test" % `type`
266 a.pop()
267 a.pop()
268 a.pop()
269 x = a.pop()
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000270 if x != unicode('e', 'ascii'):
Martin v. Löwis99866332002-03-01 10:27:01 +0000271 raise TestFailed, "array(%s) pop-test" % `type`
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000272 if a != array.array(type, unicode("acd", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000273 raise TestFailed, "array(%s) pop-test" % `type`
274 a.reverse()
Michael W. Hudson8bf46e42002-05-15 13:04:53 +0000275 if a != array.array(type, unicode("dca", 'ascii')):
Martin v. Löwis99866332002-03-01 10:27:01 +0000276 raise TestFailed, "array(%s) reverse-test" % `type`
Fred Drake004d5e62000-10-23 17:22:08 +0000277 else:
278 a = array.array(type, [1, 2, 3, 4, 5])
279 a[:-1] = a
280 if a != array.array(type, [1, 2, 3, 4, 5, 5]):
281 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
282 a = array.array(type, [1, 2, 3, 4, 5])
283 a[1:] = a
284 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
285 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
286 a = array.array(type, [1, 2, 3, 4, 5])
287 a[1:-1] = a
288 if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
289 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
290 if a.index(5) != 5:
291 raise TestFailed, "array(%s) index-test" % `type`
292 if a.count(1) != 2:
293 raise TestFailed, "array(%s) count-test" % `type`
294 a.remove(5)
295 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
296 raise TestFailed, "array(%s) remove-test" % `type`
297 if a.pop(0) != 1:
298 raise TestFailed, "array(%s) pop-test" % `type`
299 if a.pop(1) != 2:
300 raise TestFailed, "array(%s) pop-test" % `type`
301 a.extend(array.array(type, [7, 8, 9]))
302 if a != array.array(type, [1, 3, 4, 5, 7, 8, 9]):
303 raise TestFailed, "array(%s) extend-test" % `type`
304 a.pop()
305 a.pop()
306 a.pop()
307 x = a.pop()
308 if x != 5:
309 raise TestFailed, "array(%s) pop-test" % `type`
310 if a != array.array(type, [1, 3, 4]):
311 raise TestFailed, "array(%s) pop-test" % `type`
312 a.reverse()
313 if a != array.array(type, [4, 3, 1]):
314 raise TestFailed, "array(%s) reverse-test" % `type`
315
316 # test that overflow exceptions are raised as expected for assignment
317 # to array of specific integral types
318 from math import pow
319 if type in ('b', 'h', 'i', 'l'):
320 # check signed and unsigned versions
321 a = array.array(type)
322 signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1))
323 signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L
324 unsignedLowerLimit = 0
325 unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L
326 testoverflow(type, signedLowerLimit, signedUpperLimit)
327 testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit)
328
329
330
Roger E. Massefab8ab81996-12-20 22:36:52 +0000331main()