blob: d3e115f70f880ba1fe32bd67a1ba8fa63d180831 [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
Fredrik Lundhf7850422001-01-17 21:51:36 +00006from test_support import verbose, TESTFN, unlink, TestFailed
Roger E. Massefab8ab81996-12-20 22:36:52 +00007
8def main():
Roger E. Massefab8ab81996-12-20 22:36:52 +00009 testtype('c', 'c')
Martin v. Löwis99866332002-03-01 10:27:01 +000010 testtype('u', u'\u263a')
Roger E. Massefab8ab81996-12-20 22:36:52 +000011 for type in (['b', 'h', 'i', 'l', 'f', 'd']):
Guido van Rossum41360a41998-03-26 19:42:58 +000012 testtype(type, 1)
Martin v. Löwis99866332002-03-01 10:27:01 +000013 testunicode()
14 testsubclassing()
Guido van Rossumc9f8f141997-04-09 20:51:54 +000015 unlink(TESTFN)
16
Martin v. Löwis99866332002-03-01 10:27:01 +000017def testunicode():
18 try:
19 array.array('b', u'foo')
20 except TypeError:
21 pass
22 else:
23 raise TestFailed("creating a non-unicode array from "
24 "a Unicode string should fail")
25
26 x = array.array('u', u'\xa0\xc2\u1234')
27 x.fromunicode(u' ')
28 x.fromunicode(u'')
29 x.fromunicode(u'')
30 x.fromunicode(u'\x11abc\xff\u1234')
31 s = x.tounicode()
32 if s != u'\xa0\xc2\u1234 \x11abc\xff\u1234':
33 raise TestFailed("fromunicode()/tounicode()")
34
35 s = u'\x00="\'a\\b\x80\xff\u0000\u0001\u1234'
36 a = array.array('u', s)
37 if verbose:
38 print "repr of type 'u' array:", repr(a)
39 print " expected: array('u', %r)" % s
40
41def testsubclassing():
42 class EditableString(array.array):
43 def __new__(cls, s, *args, **kwargs):
44 return array.array.__new__(cls, 'c', s)
45
46 def __init__(self, s, color='blue'):
47 array.array.__init__(self, 'c', s)
48 self.color = color
49
50 def strip(self):
51 self[:] = array.array('c', self.tostring().strip())
52
53 def __repr__(self):
54 return 'EditableString(%r)' % self.tostring()
55
56 s = EditableString("\ttest\r\n")
57 s.strip()
58 if s.tostring() != 'test':
59 raise TestFailed, "subclassing array.array failed somewhere"
60 if s.color != 'blue':
61 raise TestFailed, "assigning attributes to instance of array subclass"
62 s.color = 'red'
63 if s.color != 'red':
64 raise TestFailed, "assigning attributes to instance of array subclass"
65 if s.__dict__.keys() != ['color']:
66 raise TestFailed, "array subclass __dict__"
67
68 class ExaggeratingArray(array.array):
69 __slots__ = ['offset']
70
71 def __new__(cls, typecode, data, offset):
72 return array.array.__new__(cls, typecode, data)
73
74 def __init__(self, typecode, data, offset):
75 self.offset = offset
76
77 def __getitem__(self, i):
78 return array.array.__getitem__(self, i) + self.offset
79
80 a = ExaggeratingArray('i', [3, 6, 7, 11], 4)
81 if a[0] != 7:
82 raise TestFailed, "array subclass overriding __getitem__"
83 try:
84 a.color = 'blue'
85 except AttributeError:
86 pass
87 else:
88 raise TestFailed, "array subclass __slots__ was ignored"
89
Roger E. Masse8db1b071996-12-09 20:09:16 +000090
Fred Drake78334472000-06-28 17:50:51 +000091def testoverflow(type, lowerLimit, upperLimit):
Fred Drake004d5e62000-10-23 17:22:08 +000092 # should not overflow assigning lower limit
93 if verbose:
94 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit`)
95 try:
96 a = array.array(type, [lowerLimit])
97 except:
98 raise TestFailed, "array(%s) overflowed assigning %s" %\
99 (`type`, `lowerLimit`)
100 # should overflow assigning less than lower limit
101 if verbose:
102 print "overflow test: array(%s, [%s])" % (`type`, `lowerLimit-1`)
103 try:
104 a = array.array(type, [lowerLimit-1])
105 raise TestFailed, "array(%s) did not overflow assigning %s" %\
106 (`type`, `lowerLimit-1`)
107 except OverflowError:
108 pass
109 # should not overflow assigning upper limit
110 if verbose:
111 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit`)
112 try:
113 a = array.array(type, [upperLimit])
114 except:
115 raise TestFailed, "array(%s) overflowed assigning %s" %\
116 (`type`, `upperLimit`)
117 # should overflow assigning more than upper limit
118 if verbose:
119 print "overflow test: array(%s, [%s])" % (`type`, `upperLimit+1`)
120 try:
121 a = array.array(type, [upperLimit+1])
122 raise TestFailed, "array(%s) did not overflow assigning %s" %\
123 (`type`, `upperLimit+1`)
124 except OverflowError:
125 pass
Fred Drake78334472000-06-28 17:50:51 +0000126
127
128
Roger E. Masse8db1b071996-12-09 20:09:16 +0000129def testtype(type, example):
Fred Drake004d5e62000-10-23 17:22:08 +0000130 a = array.array(type)
131 a.append(example)
132 if verbose:
133 print 40*'*'
134 print 'array after append: ', a
135 a.typecode
136 a.itemsize
137 if a.typecode in ('i', 'b', 'h', 'l'):
138 a.byteswap()
Roger E. Masse8db1b071996-12-09 20:09:16 +0000139
Fred Drake004d5e62000-10-23 17:22:08 +0000140 if a.typecode == 'c':
141 f = open(TESTFN, "w")
142 f.write("The quick brown fox jumps over the lazy dog.\n")
Guido van Rossum41360a41998-03-26 19:42:58 +0000143 f.close()
Fred Drake004d5e62000-10-23 17:22:08 +0000144 f = open(TESTFN, 'r')
145 a.fromfile(f, 10)
146 f.close()
Guido van Rossum41360a41998-03-26 19:42:58 +0000147 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000148 print 'char array with 10 bytes of TESTFN appended: ', a
149 a.fromlist(['a', 'b', 'c'])
Guido van Rossum41360a41998-03-26 19:42:58 +0000150 if verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000151 print 'char array with list appended: ', a
Roger E. Masse8db1b071996-12-09 20:09:16 +0000152
Fred Drake004d5e62000-10-23 17:22:08 +0000153 a.insert(0, example)
154 if verbose:
155 print 'array of %s after inserting another:' % a.typecode, a
156 f = open(TESTFN, 'w')
157 a.tofile(f)
158 f.close()
Tim Peterscc78e472000-11-14 21:36:07 +0000159
160 # This block is just to verify that the operations don't blow up.
Fred Drake004d5e62000-10-23 17:22:08 +0000161 a.tolist()
162 a.tostring()
Tim Peterscc78e472000-11-14 21:36:07 +0000163 repr(a)
164 str(a)
165
Fred Drake004d5e62000-10-23 17:22:08 +0000166 if verbose:
167 print 'array of %s converted to a list: ' % a.typecode, a.tolist()
168 if verbose:
169 print 'array of %s converted to a string: ' \
170 % a.typecode, `a.tostring()`
Guido van Rossum7f1d3aa1998-07-16 15:31:43 +0000171
Martin v. Löwis99866332002-03-01 10:27:01 +0000172 # Try out inplace addition and multiplication
173 a = array.array(type, [example])
174 b = a
175 a += array.array(type, [example]*2)
176 if a is not b:
177 raise TestFailed, "array(%s) inplace addition" % `type`
178 if a != array.array(type, [example] * 3):
179 raise TestFailed, "array(%s) inplace addition" % `type`
180
181 a *= 5
182 if a is not b:
183 raise TestFailed, "array(%s) inplace multiplication" % `type`
184 if a != array.array(type, [example] * 15):
185 raise TestFailed, "array(%s) inplace multiplication" % `type`
186
187 a *= 0
188 if a is not b:
189 raise TestFailed, "array(%s) inplace multiplication by 0" % `type`
190 if a != array.array(type, []):
191 raise TestFailed, "array(%s) inplace multiplication by 0" % `type`
192
193 a *= 1000
194 if a is not b:
195 raise TestFailed, "empty array(%s) inplace multiplication" % `type`
196 if a != array.array(type, []):
197 raise TestFailed, "empty array(%s) inplace multiplication" % `type`
198
Fred Drake004d5e62000-10-23 17:22:08 +0000199 if type == 'c':
200 a = array.array(type, "abcde")
201 a[:-1] = a
202 if a != array.array(type, "abcdee"):
203 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
204 a = array.array(type, "abcde")
205 a[1:] = a
206 if a != array.array(type, "aabcde"):
207 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
208 a = array.array(type, "abcde")
209 a[1:-1] = a
210 if a != array.array(type, "aabcdee"):
211 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
212 if a.index("e") != 5:
213 raise TestFailed, "array(%s) index-test" % `type`
214 if a.count("a") != 2:
215 raise TestFailed, "array(%s) count-test" % `type`
216 a.remove("e")
217 if a != array.array(type, "aabcde"):
218 raise TestFailed, "array(%s) remove-test" % `type`
219 if a.pop(0) != "a":
220 raise TestFailed, "array(%s) pop-test" % `type`
221 if a.pop(1) != "b":
222 raise TestFailed, "array(%s) pop-test" % `type`
223 a.extend(array.array(type, "xyz"))
224 if a != array.array(type, "acdexyz"):
225 raise TestFailed, "array(%s) extend-test" % `type`
226 a.pop()
227 a.pop()
228 a.pop()
229 x = a.pop()
230 if x != 'e':
231 raise TestFailed, "array(%s) pop-test" % `type`
232 if a != array.array(type, "acd"):
233 raise TestFailed, "array(%s) pop-test" % `type`
234 a.reverse()
235 if a != array.array(type, "dca"):
236 raise TestFailed, "array(%s) reverse-test" % `type`
Martin v. Löwis99866332002-03-01 10:27:01 +0000237 elif type == 'u':
238 a = array.array(type, u"abcde")
239 a[:-1] = a
240 if a != array.array(type, u"abcdee"):
241 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
242 a = array.array(type, u"abcde")
243 a[1:] = a
244 if a != array.array(type, u"aabcde"):
245 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
246 a = array.array(type, u"abcde")
247 a[1:-1] = a
248 if a != array.array(type, u"aabcdee"):
249 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
250 if a.index(u"e") != 5:
251 raise TestFailed, "array(%s) index-test" % `type`
252 if a.count(u"a") != 2:
253 raise TestFailed, "array(%s) count-test" % `type`
254 a.remove(u"e")
255 if a != array.array(type, u"aabcde"):
256 raise TestFailed, "array(%s) remove-test" % `type`
257 if a.pop(0) != u"a":
258 raise TestFailed, "array(%s) pop-test" % `type`
259 if a.pop(1) != u"b":
260 raise TestFailed, "array(%s) pop-test" % `type`
261 a.extend(array.array(type, u"xyz"))
262 if a != array.array(type, u"acdexyz"):
263 raise TestFailed, "array(%s) extend-test" % `type`
264 a.pop()
265 a.pop()
266 a.pop()
267 x = a.pop()
268 if x != u'e':
269 raise TestFailed, "array(%s) pop-test" % `type`
270 if a != array.array(type, u"acd"):
271 raise TestFailed, "array(%s) pop-test" % `type`
272 a.reverse()
273 if a != array.array(type, u"dca"):
274 raise TestFailed, "array(%s) reverse-test" % `type`
Fred Drake004d5e62000-10-23 17:22:08 +0000275 else:
276 a = array.array(type, [1, 2, 3, 4, 5])
277 a[:-1] = a
278 if a != array.array(type, [1, 2, 3, 4, 5, 5]):
279 raise TestFailed, "array(%s) self-slice-assign (head)" % `type`
280 a = array.array(type, [1, 2, 3, 4, 5])
281 a[1:] = a
282 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
283 raise TestFailed, "array(%s) self-slice-assign (tail)" % `type`
284 a = array.array(type, [1, 2, 3, 4, 5])
285 a[1:-1] = a
286 if a != array.array(type, [1, 1, 2, 3, 4, 5, 5]):
287 raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type`
288 if a.index(5) != 5:
289 raise TestFailed, "array(%s) index-test" % `type`
290 if a.count(1) != 2:
291 raise TestFailed, "array(%s) count-test" % `type`
292 a.remove(5)
293 if a != array.array(type, [1, 1, 2, 3, 4, 5]):
294 raise TestFailed, "array(%s) remove-test" % `type`
295 if a.pop(0) != 1:
296 raise TestFailed, "array(%s) pop-test" % `type`
297 if a.pop(1) != 2:
298 raise TestFailed, "array(%s) pop-test" % `type`
299 a.extend(array.array(type, [7, 8, 9]))
300 if a != array.array(type, [1, 3, 4, 5, 7, 8, 9]):
301 raise TestFailed, "array(%s) extend-test" % `type`
302 a.pop()
303 a.pop()
304 a.pop()
305 x = a.pop()
306 if x != 5:
307 raise TestFailed, "array(%s) pop-test" % `type`
308 if a != array.array(type, [1, 3, 4]):
309 raise TestFailed, "array(%s) pop-test" % `type`
310 a.reverse()
311 if a != array.array(type, [4, 3, 1]):
312 raise TestFailed, "array(%s) reverse-test" % `type`
313
314 # test that overflow exceptions are raised as expected for assignment
315 # to array of specific integral types
316 from math import pow
317 if type in ('b', 'h', 'i', 'l'):
318 # check signed and unsigned versions
319 a = array.array(type)
320 signedLowerLimit = -1 * long(pow(2, a.itemsize * 8 - 1))
321 signedUpperLimit = long(pow(2, a.itemsize * 8 - 1)) - 1L
322 unsignedLowerLimit = 0
323 unsignedUpperLimit = long(pow(2, a.itemsize * 8)) - 1L
324 testoverflow(type, signedLowerLimit, signedUpperLimit)
325 testoverflow(type.upper(), unsignedLowerLimit, unsignedUpperLimit)
326
327
328
Roger E. Massefab8ab81996-12-20 22:36:52 +0000329main()