1 | n/a | """Test the arraymodule. |
---|
2 | n/a | Roger E. Masse |
---|
3 | n/a | """ |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | from test import support |
---|
7 | n/a | import weakref |
---|
8 | n/a | import pickle |
---|
9 | n/a | import operator |
---|
10 | n/a | import struct |
---|
11 | n/a | import sys |
---|
12 | n/a | import warnings |
---|
13 | n/a | |
---|
14 | n/a | import array |
---|
15 | n/a | from array import _array_reconstructor as array_reconstructor |
---|
16 | n/a | |
---|
17 | n/a | try: |
---|
18 | n/a | # Try to determine availability of long long independently |
---|
19 | n/a | # of the array module under test |
---|
20 | n/a | struct.calcsize('@q') |
---|
21 | n/a | have_long_long = True |
---|
22 | n/a | except struct.error: |
---|
23 | n/a | have_long_long = False |
---|
24 | n/a | |
---|
25 | n/a | sizeof_wchar = array.array('u').itemsize |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | class ArraySubclass(array.array): |
---|
29 | n/a | pass |
---|
30 | n/a | |
---|
31 | n/a | class ArraySubclassWithKwargs(array.array): |
---|
32 | n/a | def __init__(self, typecode, newarg=None): |
---|
33 | n/a | array.array.__init__(self) |
---|
34 | n/a | |
---|
35 | n/a | typecodes = "ubBhHiIlLfd" |
---|
36 | n/a | if have_long_long: |
---|
37 | n/a | typecodes += 'qQ' |
---|
38 | n/a | |
---|
39 | n/a | class MiscTest(unittest.TestCase): |
---|
40 | n/a | |
---|
41 | n/a | def test_bad_constructor(self): |
---|
42 | n/a | self.assertRaises(TypeError, array.array) |
---|
43 | n/a | self.assertRaises(TypeError, array.array, spam=42) |
---|
44 | n/a | self.assertRaises(TypeError, array.array, 'xx') |
---|
45 | n/a | self.assertRaises(ValueError, array.array, 'x') |
---|
46 | n/a | |
---|
47 | n/a | def test_empty(self): |
---|
48 | n/a | # Exercise code for handling zero-length arrays |
---|
49 | n/a | a = array.array('B') |
---|
50 | n/a | a[:] = a |
---|
51 | n/a | self.assertEqual(len(a), 0) |
---|
52 | n/a | self.assertEqual(len(a + a), 0) |
---|
53 | n/a | self.assertEqual(len(a * 3), 0) |
---|
54 | n/a | a += a |
---|
55 | n/a | self.assertEqual(len(a), 0) |
---|
56 | n/a | |
---|
57 | n/a | |
---|
58 | n/a | # Machine format codes. |
---|
59 | n/a | # |
---|
60 | n/a | # Search for "enum machine_format_code" in Modules/arraymodule.c to get the |
---|
61 | n/a | # authoritative values. |
---|
62 | n/a | UNKNOWN_FORMAT = -1 |
---|
63 | n/a | UNSIGNED_INT8 = 0 |
---|
64 | n/a | SIGNED_INT8 = 1 |
---|
65 | n/a | UNSIGNED_INT16_LE = 2 |
---|
66 | n/a | UNSIGNED_INT16_BE = 3 |
---|
67 | n/a | SIGNED_INT16_LE = 4 |
---|
68 | n/a | SIGNED_INT16_BE = 5 |
---|
69 | n/a | UNSIGNED_INT32_LE = 6 |
---|
70 | n/a | UNSIGNED_INT32_BE = 7 |
---|
71 | n/a | SIGNED_INT32_LE = 8 |
---|
72 | n/a | SIGNED_INT32_BE = 9 |
---|
73 | n/a | UNSIGNED_INT64_LE = 10 |
---|
74 | n/a | UNSIGNED_INT64_BE = 11 |
---|
75 | n/a | SIGNED_INT64_LE = 12 |
---|
76 | n/a | SIGNED_INT64_BE = 13 |
---|
77 | n/a | IEEE_754_FLOAT_LE = 14 |
---|
78 | n/a | IEEE_754_FLOAT_BE = 15 |
---|
79 | n/a | IEEE_754_DOUBLE_LE = 16 |
---|
80 | n/a | IEEE_754_DOUBLE_BE = 17 |
---|
81 | n/a | UTF16_LE = 18 |
---|
82 | n/a | UTF16_BE = 19 |
---|
83 | n/a | UTF32_LE = 20 |
---|
84 | n/a | UTF32_BE = 21 |
---|
85 | n/a | |
---|
86 | n/a | class ArrayReconstructorTest(unittest.TestCase): |
---|
87 | n/a | |
---|
88 | n/a | def test_error(self): |
---|
89 | n/a | self.assertRaises(TypeError, array_reconstructor, |
---|
90 | n/a | "", "b", 0, b"") |
---|
91 | n/a | self.assertRaises(TypeError, array_reconstructor, |
---|
92 | n/a | str, "b", 0, b"") |
---|
93 | n/a | self.assertRaises(TypeError, array_reconstructor, |
---|
94 | n/a | array.array, "b", '', b"") |
---|
95 | n/a | self.assertRaises(TypeError, array_reconstructor, |
---|
96 | n/a | array.array, "b", 0, "") |
---|
97 | n/a | self.assertRaises(ValueError, array_reconstructor, |
---|
98 | n/a | array.array, "?", 0, b"") |
---|
99 | n/a | self.assertRaises(ValueError, array_reconstructor, |
---|
100 | n/a | array.array, "b", UNKNOWN_FORMAT, b"") |
---|
101 | n/a | self.assertRaises(ValueError, array_reconstructor, |
---|
102 | n/a | array.array, "b", 22, b"") |
---|
103 | n/a | self.assertRaises(ValueError, array_reconstructor, |
---|
104 | n/a | array.array, "d", 16, b"a") |
---|
105 | n/a | |
---|
106 | n/a | def test_numbers(self): |
---|
107 | n/a | testcases = ( |
---|
108 | n/a | (['B', 'H', 'I', 'L'], UNSIGNED_INT8, '=BBBB', |
---|
109 | n/a | [0x80, 0x7f, 0, 0xff]), |
---|
110 | n/a | (['b', 'h', 'i', 'l'], SIGNED_INT8, '=bbb', |
---|
111 | n/a | [-0x80, 0x7f, 0]), |
---|
112 | n/a | (['H', 'I', 'L'], UNSIGNED_INT16_LE, '<HHHH', |
---|
113 | n/a | [0x8000, 0x7fff, 0, 0xffff]), |
---|
114 | n/a | (['H', 'I', 'L'], UNSIGNED_INT16_BE, '>HHHH', |
---|
115 | n/a | [0x8000, 0x7fff, 0, 0xffff]), |
---|
116 | n/a | (['h', 'i', 'l'], SIGNED_INT16_LE, '<hhh', |
---|
117 | n/a | [-0x8000, 0x7fff, 0]), |
---|
118 | n/a | (['h', 'i', 'l'], SIGNED_INT16_BE, '>hhh', |
---|
119 | n/a | [-0x8000, 0x7fff, 0]), |
---|
120 | n/a | (['I', 'L'], UNSIGNED_INT32_LE, '<IIII', |
---|
121 | n/a | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
---|
122 | n/a | (['I', 'L'], UNSIGNED_INT32_BE, '>IIII', |
---|
123 | n/a | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
---|
124 | n/a | (['i', 'l'], SIGNED_INT32_LE, '<iii', |
---|
125 | n/a | [-1<<31, (1<<31)-1, 0]), |
---|
126 | n/a | (['i', 'l'], SIGNED_INT32_BE, '>iii', |
---|
127 | n/a | [-1<<31, (1<<31)-1, 0]), |
---|
128 | n/a | (['L'], UNSIGNED_INT64_LE, '<QQQQ', |
---|
129 | n/a | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
---|
130 | n/a | (['L'], UNSIGNED_INT64_BE, '>QQQQ', |
---|
131 | n/a | [1<<31, (1<<31)-1, 0, (1<<32)-1]), |
---|
132 | n/a | (['l'], SIGNED_INT64_LE, '<qqq', |
---|
133 | n/a | [-1<<31, (1<<31)-1, 0]), |
---|
134 | n/a | (['l'], SIGNED_INT64_BE, '>qqq', |
---|
135 | n/a | [-1<<31, (1<<31)-1, 0]), |
---|
136 | n/a | # The following tests for INT64 will raise an OverflowError |
---|
137 | n/a | # when run on a 32-bit machine. The tests are simply skipped |
---|
138 | n/a | # in that case. |
---|
139 | n/a | (['L'], UNSIGNED_INT64_LE, '<QQQQ', |
---|
140 | n/a | [1<<63, (1<<63)-1, 0, (1<<64)-1]), |
---|
141 | n/a | (['L'], UNSIGNED_INT64_BE, '>QQQQ', |
---|
142 | n/a | [1<<63, (1<<63)-1, 0, (1<<64)-1]), |
---|
143 | n/a | (['l'], SIGNED_INT64_LE, '<qqq', |
---|
144 | n/a | [-1<<63, (1<<63)-1, 0]), |
---|
145 | n/a | (['l'], SIGNED_INT64_BE, '>qqq', |
---|
146 | n/a | [-1<<63, (1<<63)-1, 0]), |
---|
147 | n/a | (['f'], IEEE_754_FLOAT_LE, '<ffff', |
---|
148 | n/a | [16711938.0, float('inf'), float('-inf'), -0.0]), |
---|
149 | n/a | (['f'], IEEE_754_FLOAT_BE, '>ffff', |
---|
150 | n/a | [16711938.0, float('inf'), float('-inf'), -0.0]), |
---|
151 | n/a | (['d'], IEEE_754_DOUBLE_LE, '<dddd', |
---|
152 | n/a | [9006104071832581.0, float('inf'), float('-inf'), -0.0]), |
---|
153 | n/a | (['d'], IEEE_754_DOUBLE_BE, '>dddd', |
---|
154 | n/a | [9006104071832581.0, float('inf'), float('-inf'), -0.0]) |
---|
155 | n/a | ) |
---|
156 | n/a | for testcase in testcases: |
---|
157 | n/a | valid_typecodes, mformat_code, struct_fmt, values = testcase |
---|
158 | n/a | arraystr = struct.pack(struct_fmt, *values) |
---|
159 | n/a | for typecode in valid_typecodes: |
---|
160 | n/a | try: |
---|
161 | n/a | a = array.array(typecode, values) |
---|
162 | n/a | except OverflowError: |
---|
163 | n/a | continue # Skip this test case. |
---|
164 | n/a | b = array_reconstructor( |
---|
165 | n/a | array.array, typecode, mformat_code, arraystr) |
---|
166 | n/a | self.assertEqual(a, b, |
---|
167 | n/a | msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) |
---|
168 | n/a | |
---|
169 | n/a | def test_unicode(self): |
---|
170 | n/a | teststr = "Bonne Journ\xe9e \U0002030a\U00020347" |
---|
171 | n/a | testcases = ( |
---|
172 | n/a | (UTF16_LE, "UTF-16-LE"), |
---|
173 | n/a | (UTF16_BE, "UTF-16-BE"), |
---|
174 | n/a | (UTF32_LE, "UTF-32-LE"), |
---|
175 | n/a | (UTF32_BE, "UTF-32-BE") |
---|
176 | n/a | ) |
---|
177 | n/a | for testcase in testcases: |
---|
178 | n/a | mformat_code, encoding = testcase |
---|
179 | n/a | a = array.array('u', teststr) |
---|
180 | n/a | b = array_reconstructor( |
---|
181 | n/a | array.array, 'u', mformat_code, teststr.encode(encoding)) |
---|
182 | n/a | self.assertEqual(a, b, |
---|
183 | n/a | msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) |
---|
184 | n/a | |
---|
185 | n/a | |
---|
186 | n/a | class BaseTest: |
---|
187 | n/a | # Required class attributes (provided by subclasses |
---|
188 | n/a | # typecode: the typecode to test |
---|
189 | n/a | # example: an initializer usable in the constructor for this type |
---|
190 | n/a | # smallerexample: the same length as example, but smaller |
---|
191 | n/a | # biggerexample: the same length as example, but bigger |
---|
192 | n/a | # outside: An entry that is not in example |
---|
193 | n/a | # minitemsize: the minimum guaranteed itemsize |
---|
194 | n/a | |
---|
195 | n/a | def assertEntryEqual(self, entry1, entry2): |
---|
196 | n/a | self.assertEqual(entry1, entry2) |
---|
197 | n/a | |
---|
198 | n/a | def badtypecode(self): |
---|
199 | n/a | # Return a typecode that is different from our own |
---|
200 | n/a | return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] |
---|
201 | n/a | |
---|
202 | n/a | def test_constructor(self): |
---|
203 | n/a | a = array.array(self.typecode) |
---|
204 | n/a | self.assertEqual(a.typecode, self.typecode) |
---|
205 | n/a | self.assertGreaterEqual(a.itemsize, self.minitemsize) |
---|
206 | n/a | self.assertRaises(TypeError, array.array, self.typecode, None) |
---|
207 | n/a | |
---|
208 | n/a | def test_len(self): |
---|
209 | n/a | a = array.array(self.typecode) |
---|
210 | n/a | a.append(self.example[0]) |
---|
211 | n/a | self.assertEqual(len(a), 1) |
---|
212 | n/a | |
---|
213 | n/a | a = array.array(self.typecode, self.example) |
---|
214 | n/a | self.assertEqual(len(a), len(self.example)) |
---|
215 | n/a | |
---|
216 | n/a | def test_buffer_info(self): |
---|
217 | n/a | a = array.array(self.typecode, self.example) |
---|
218 | n/a | self.assertRaises(TypeError, a.buffer_info, 42) |
---|
219 | n/a | bi = a.buffer_info() |
---|
220 | n/a | self.assertIsInstance(bi, tuple) |
---|
221 | n/a | self.assertEqual(len(bi), 2) |
---|
222 | n/a | self.assertIsInstance(bi[0], int) |
---|
223 | n/a | self.assertIsInstance(bi[1], int) |
---|
224 | n/a | self.assertEqual(bi[1], len(a)) |
---|
225 | n/a | |
---|
226 | n/a | def test_byteswap(self): |
---|
227 | n/a | if self.typecode == 'u': |
---|
228 | n/a | example = '\U00100100' |
---|
229 | n/a | else: |
---|
230 | n/a | example = self.example |
---|
231 | n/a | a = array.array(self.typecode, example) |
---|
232 | n/a | self.assertRaises(TypeError, a.byteswap, 42) |
---|
233 | n/a | if a.itemsize in (1, 2, 4, 8): |
---|
234 | n/a | b = array.array(self.typecode, example) |
---|
235 | n/a | b.byteswap() |
---|
236 | n/a | if a.itemsize==1: |
---|
237 | n/a | self.assertEqual(a, b) |
---|
238 | n/a | else: |
---|
239 | n/a | self.assertNotEqual(a, b) |
---|
240 | n/a | b.byteswap() |
---|
241 | n/a | self.assertEqual(a, b) |
---|
242 | n/a | |
---|
243 | n/a | def test_copy(self): |
---|
244 | n/a | import copy |
---|
245 | n/a | a = array.array(self.typecode, self.example) |
---|
246 | n/a | b = copy.copy(a) |
---|
247 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
248 | n/a | self.assertEqual(a, b) |
---|
249 | n/a | |
---|
250 | n/a | def test_deepcopy(self): |
---|
251 | n/a | import copy |
---|
252 | n/a | a = array.array(self.typecode, self.example) |
---|
253 | n/a | b = copy.deepcopy(a) |
---|
254 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
255 | n/a | self.assertEqual(a, b) |
---|
256 | n/a | |
---|
257 | n/a | def test_reduce_ex(self): |
---|
258 | n/a | a = array.array(self.typecode, self.example) |
---|
259 | n/a | for protocol in range(3): |
---|
260 | n/a | self.assertIs(a.__reduce_ex__(protocol)[0], array.array) |
---|
261 | n/a | for protocol in range(3, pickle.HIGHEST_PROTOCOL): |
---|
262 | n/a | self.assertIs(a.__reduce_ex__(protocol)[0], array_reconstructor) |
---|
263 | n/a | |
---|
264 | n/a | def test_pickle(self): |
---|
265 | n/a | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
266 | n/a | a = array.array(self.typecode, self.example) |
---|
267 | n/a | b = pickle.loads(pickle.dumps(a, protocol)) |
---|
268 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
269 | n/a | self.assertEqual(a, b) |
---|
270 | n/a | |
---|
271 | n/a | a = ArraySubclass(self.typecode, self.example) |
---|
272 | n/a | a.x = 10 |
---|
273 | n/a | b = pickle.loads(pickle.dumps(a, protocol)) |
---|
274 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
275 | n/a | self.assertEqual(a, b) |
---|
276 | n/a | self.assertEqual(a.x, b.x) |
---|
277 | n/a | self.assertEqual(type(a), type(b)) |
---|
278 | n/a | |
---|
279 | n/a | def test_pickle_for_empty_array(self): |
---|
280 | n/a | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
281 | n/a | a = array.array(self.typecode) |
---|
282 | n/a | b = pickle.loads(pickle.dumps(a, protocol)) |
---|
283 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
284 | n/a | self.assertEqual(a, b) |
---|
285 | n/a | |
---|
286 | n/a | a = ArraySubclass(self.typecode) |
---|
287 | n/a | a.x = 10 |
---|
288 | n/a | b = pickle.loads(pickle.dumps(a, protocol)) |
---|
289 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
290 | n/a | self.assertEqual(a, b) |
---|
291 | n/a | self.assertEqual(a.x, b.x) |
---|
292 | n/a | self.assertEqual(type(a), type(b)) |
---|
293 | n/a | |
---|
294 | n/a | def test_iterator_pickle(self): |
---|
295 | n/a | orig = array.array(self.typecode, self.example) |
---|
296 | n/a | data = list(orig) |
---|
297 | n/a | data2 = data[::-1] |
---|
298 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
299 | n/a | # initial iterator |
---|
300 | n/a | itorig = iter(orig) |
---|
301 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
302 | n/a | it, a = pickle.loads(d) |
---|
303 | n/a | a.fromlist(data2) |
---|
304 | n/a | self.assertEqual(type(it), type(itorig)) |
---|
305 | n/a | self.assertEqual(list(it), data + data2) |
---|
306 | n/a | |
---|
307 | n/a | # running iterator |
---|
308 | n/a | next(itorig) |
---|
309 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
310 | n/a | it, a = pickle.loads(d) |
---|
311 | n/a | a.fromlist(data2) |
---|
312 | n/a | self.assertEqual(type(it), type(itorig)) |
---|
313 | n/a | self.assertEqual(list(it), data[1:] + data2) |
---|
314 | n/a | |
---|
315 | n/a | # empty iterator |
---|
316 | n/a | for i in range(1, len(data)): |
---|
317 | n/a | next(itorig) |
---|
318 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
319 | n/a | it, a = pickle.loads(d) |
---|
320 | n/a | a.fromlist(data2) |
---|
321 | n/a | self.assertEqual(type(it), type(itorig)) |
---|
322 | n/a | self.assertEqual(list(it), data2) |
---|
323 | n/a | |
---|
324 | n/a | # exhausted iterator |
---|
325 | n/a | self.assertRaises(StopIteration, next, itorig) |
---|
326 | n/a | d = pickle.dumps((itorig, orig), proto) |
---|
327 | n/a | it, a = pickle.loads(d) |
---|
328 | n/a | a.fromlist(data2) |
---|
329 | n/a | self.assertEqual(list(it), []) |
---|
330 | n/a | |
---|
331 | n/a | def test_exhausted_iterator(self): |
---|
332 | n/a | a = array.array(self.typecode, self.example) |
---|
333 | n/a | self.assertEqual(list(a), list(self.example)) |
---|
334 | n/a | exhit = iter(a) |
---|
335 | n/a | empit = iter(a) |
---|
336 | n/a | for x in exhit: # exhaust the iterator |
---|
337 | n/a | next(empit) # not exhausted |
---|
338 | n/a | a.append(self.outside) |
---|
339 | n/a | self.assertEqual(list(exhit), []) |
---|
340 | n/a | self.assertEqual(list(empit), [self.outside]) |
---|
341 | n/a | self.assertEqual(list(a), list(self.example) + [self.outside]) |
---|
342 | n/a | |
---|
343 | n/a | def test_insert(self): |
---|
344 | n/a | a = array.array(self.typecode, self.example) |
---|
345 | n/a | a.insert(0, self.example[0]) |
---|
346 | n/a | self.assertEqual(len(a), 1+len(self.example)) |
---|
347 | n/a | self.assertEqual(a[0], a[1]) |
---|
348 | n/a | self.assertRaises(TypeError, a.insert) |
---|
349 | n/a | self.assertRaises(TypeError, a.insert, None) |
---|
350 | n/a | self.assertRaises(TypeError, a.insert, 0, None) |
---|
351 | n/a | |
---|
352 | n/a | a = array.array(self.typecode, self.example) |
---|
353 | n/a | a.insert(-1, self.example[0]) |
---|
354 | n/a | self.assertEqual( |
---|
355 | n/a | a, |
---|
356 | n/a | array.array( |
---|
357 | n/a | self.typecode, |
---|
358 | n/a | self.example[:-1] + self.example[:1] + self.example[-1:] |
---|
359 | n/a | ) |
---|
360 | n/a | ) |
---|
361 | n/a | |
---|
362 | n/a | a = array.array(self.typecode, self.example) |
---|
363 | n/a | a.insert(-1000, self.example[0]) |
---|
364 | n/a | self.assertEqual( |
---|
365 | n/a | a, |
---|
366 | n/a | array.array(self.typecode, self.example[:1] + self.example) |
---|
367 | n/a | ) |
---|
368 | n/a | |
---|
369 | n/a | a = array.array(self.typecode, self.example) |
---|
370 | n/a | a.insert(1000, self.example[0]) |
---|
371 | n/a | self.assertEqual( |
---|
372 | n/a | a, |
---|
373 | n/a | array.array(self.typecode, self.example + self.example[:1]) |
---|
374 | n/a | ) |
---|
375 | n/a | |
---|
376 | n/a | def test_tofromfile(self): |
---|
377 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
378 | n/a | self.assertRaises(TypeError, a.tofile) |
---|
379 | n/a | support.unlink(support.TESTFN) |
---|
380 | n/a | f = open(support.TESTFN, 'wb') |
---|
381 | n/a | try: |
---|
382 | n/a | a.tofile(f) |
---|
383 | n/a | f.close() |
---|
384 | n/a | b = array.array(self.typecode) |
---|
385 | n/a | f = open(support.TESTFN, 'rb') |
---|
386 | n/a | self.assertRaises(TypeError, b.fromfile) |
---|
387 | n/a | b.fromfile(f, len(self.example)) |
---|
388 | n/a | self.assertEqual(b, array.array(self.typecode, self.example)) |
---|
389 | n/a | self.assertNotEqual(a, b) |
---|
390 | n/a | self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1) |
---|
391 | n/a | self.assertEqual(a, b) |
---|
392 | n/a | f.close() |
---|
393 | n/a | finally: |
---|
394 | n/a | if not f.closed: |
---|
395 | n/a | f.close() |
---|
396 | n/a | support.unlink(support.TESTFN) |
---|
397 | n/a | |
---|
398 | n/a | def test_fromfile_ioerror(self): |
---|
399 | n/a | # Issue #5395: Check if fromfile raises a proper OSError |
---|
400 | n/a | # instead of EOFError. |
---|
401 | n/a | a = array.array(self.typecode) |
---|
402 | n/a | f = open(support.TESTFN, 'wb') |
---|
403 | n/a | try: |
---|
404 | n/a | self.assertRaises(OSError, a.fromfile, f, len(self.example)) |
---|
405 | n/a | finally: |
---|
406 | n/a | f.close() |
---|
407 | n/a | support.unlink(support.TESTFN) |
---|
408 | n/a | |
---|
409 | n/a | def test_filewrite(self): |
---|
410 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
411 | n/a | f = open(support.TESTFN, 'wb') |
---|
412 | n/a | try: |
---|
413 | n/a | f.write(a) |
---|
414 | n/a | f.close() |
---|
415 | n/a | b = array.array(self.typecode) |
---|
416 | n/a | f = open(support.TESTFN, 'rb') |
---|
417 | n/a | b.fromfile(f, len(self.example)) |
---|
418 | n/a | self.assertEqual(b, array.array(self.typecode, self.example)) |
---|
419 | n/a | self.assertNotEqual(a, b) |
---|
420 | n/a | b.fromfile(f, len(self.example)) |
---|
421 | n/a | self.assertEqual(a, b) |
---|
422 | n/a | f.close() |
---|
423 | n/a | finally: |
---|
424 | n/a | if not f.closed: |
---|
425 | n/a | f.close() |
---|
426 | n/a | support.unlink(support.TESTFN) |
---|
427 | n/a | |
---|
428 | n/a | def test_tofromlist(self): |
---|
429 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
430 | n/a | b = array.array(self.typecode) |
---|
431 | n/a | self.assertRaises(TypeError, a.tolist, 42) |
---|
432 | n/a | self.assertRaises(TypeError, b.fromlist) |
---|
433 | n/a | self.assertRaises(TypeError, b.fromlist, 42) |
---|
434 | n/a | self.assertRaises(TypeError, b.fromlist, [None]) |
---|
435 | n/a | b.fromlist(a.tolist()) |
---|
436 | n/a | self.assertEqual(a, b) |
---|
437 | n/a | |
---|
438 | n/a | def test_tofromstring(self): |
---|
439 | n/a | # Warnings not raised when arguments are incorrect as Argument Clinic |
---|
440 | n/a | # handles that before the warning can be raised. |
---|
441 | n/a | nb_warnings = 2 |
---|
442 | n/a | with warnings.catch_warnings(record=True) as r: |
---|
443 | n/a | warnings.filterwarnings("always", |
---|
444 | n/a | message=r"(to|from)string\(\) is deprecated", |
---|
445 | n/a | category=DeprecationWarning) |
---|
446 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
447 | n/a | b = array.array(self.typecode) |
---|
448 | n/a | self.assertRaises(TypeError, a.tostring, 42) |
---|
449 | n/a | self.assertRaises(TypeError, b.fromstring) |
---|
450 | n/a | self.assertRaises(TypeError, b.fromstring, 42) |
---|
451 | n/a | b.fromstring(a.tostring()) |
---|
452 | n/a | self.assertEqual(a, b) |
---|
453 | n/a | if a.itemsize>1: |
---|
454 | n/a | self.assertRaises(ValueError, b.fromstring, "x") |
---|
455 | n/a | nb_warnings += 1 |
---|
456 | n/a | self.assertEqual(len(r), nb_warnings) |
---|
457 | n/a | |
---|
458 | n/a | def test_tofrombytes(self): |
---|
459 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
460 | n/a | b = array.array(self.typecode) |
---|
461 | n/a | self.assertRaises(TypeError, a.tobytes, 42) |
---|
462 | n/a | self.assertRaises(TypeError, b.frombytes) |
---|
463 | n/a | self.assertRaises(TypeError, b.frombytes, 42) |
---|
464 | n/a | b.frombytes(a.tobytes()) |
---|
465 | n/a | c = array.array(self.typecode, bytearray(a.tobytes())) |
---|
466 | n/a | self.assertEqual(a, b) |
---|
467 | n/a | self.assertEqual(a, c) |
---|
468 | n/a | if a.itemsize>1: |
---|
469 | n/a | self.assertRaises(ValueError, b.frombytes, b"x") |
---|
470 | n/a | |
---|
471 | n/a | def test_fromarray(self): |
---|
472 | n/a | a = array.array(self.typecode, self.example) |
---|
473 | n/a | b = array.array(self.typecode, a) |
---|
474 | n/a | self.assertEqual(a, b) |
---|
475 | n/a | |
---|
476 | n/a | def test_repr(self): |
---|
477 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
478 | n/a | self.assertEqual(a, eval(repr(a), {"array": array.array})) |
---|
479 | n/a | |
---|
480 | n/a | a = array.array(self.typecode) |
---|
481 | n/a | self.assertEqual(repr(a), "array('%s')" % self.typecode) |
---|
482 | n/a | |
---|
483 | n/a | def test_str(self): |
---|
484 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
485 | n/a | str(a) |
---|
486 | n/a | |
---|
487 | n/a | def test_cmp(self): |
---|
488 | n/a | a = array.array(self.typecode, self.example) |
---|
489 | n/a | self.assertIs(a == 42, False) |
---|
490 | n/a | self.assertIs(a != 42, True) |
---|
491 | n/a | |
---|
492 | n/a | self.assertIs(a == a, True) |
---|
493 | n/a | self.assertIs(a != a, False) |
---|
494 | n/a | self.assertIs(a < a, False) |
---|
495 | n/a | self.assertIs(a <= a, True) |
---|
496 | n/a | self.assertIs(a > a, False) |
---|
497 | n/a | self.assertIs(a >= a, True) |
---|
498 | n/a | |
---|
499 | n/a | al = array.array(self.typecode, self.smallerexample) |
---|
500 | n/a | ab = array.array(self.typecode, self.biggerexample) |
---|
501 | n/a | |
---|
502 | n/a | self.assertIs(a == 2*a, False) |
---|
503 | n/a | self.assertIs(a != 2*a, True) |
---|
504 | n/a | self.assertIs(a < 2*a, True) |
---|
505 | n/a | self.assertIs(a <= 2*a, True) |
---|
506 | n/a | self.assertIs(a > 2*a, False) |
---|
507 | n/a | self.assertIs(a >= 2*a, False) |
---|
508 | n/a | |
---|
509 | n/a | self.assertIs(a == al, False) |
---|
510 | n/a | self.assertIs(a != al, True) |
---|
511 | n/a | self.assertIs(a < al, False) |
---|
512 | n/a | self.assertIs(a <= al, False) |
---|
513 | n/a | self.assertIs(a > al, True) |
---|
514 | n/a | self.assertIs(a >= al, True) |
---|
515 | n/a | |
---|
516 | n/a | self.assertIs(a == ab, False) |
---|
517 | n/a | self.assertIs(a != ab, True) |
---|
518 | n/a | self.assertIs(a < ab, True) |
---|
519 | n/a | self.assertIs(a <= ab, True) |
---|
520 | n/a | self.assertIs(a > ab, False) |
---|
521 | n/a | self.assertIs(a >= ab, False) |
---|
522 | n/a | |
---|
523 | n/a | def test_add(self): |
---|
524 | n/a | a = array.array(self.typecode, self.example) \ |
---|
525 | n/a | + array.array(self.typecode, self.example[::-1]) |
---|
526 | n/a | self.assertEqual( |
---|
527 | n/a | a, |
---|
528 | n/a | array.array(self.typecode, self.example + self.example[::-1]) |
---|
529 | n/a | ) |
---|
530 | n/a | |
---|
531 | n/a | b = array.array(self.badtypecode()) |
---|
532 | n/a | self.assertRaises(TypeError, a.__add__, b) |
---|
533 | n/a | |
---|
534 | n/a | self.assertRaises(TypeError, a.__add__, "bad") |
---|
535 | n/a | |
---|
536 | n/a | def test_iadd(self): |
---|
537 | n/a | a = array.array(self.typecode, self.example[::-1]) |
---|
538 | n/a | b = a |
---|
539 | n/a | a += array.array(self.typecode, 2*self.example) |
---|
540 | n/a | self.assertIs(a, b) |
---|
541 | n/a | self.assertEqual( |
---|
542 | n/a | a, |
---|
543 | n/a | array.array(self.typecode, self.example[::-1]+2*self.example) |
---|
544 | n/a | ) |
---|
545 | n/a | a = array.array(self.typecode, self.example) |
---|
546 | n/a | a += a |
---|
547 | n/a | self.assertEqual( |
---|
548 | n/a | a, |
---|
549 | n/a | array.array(self.typecode, self.example + self.example) |
---|
550 | n/a | ) |
---|
551 | n/a | |
---|
552 | n/a | b = array.array(self.badtypecode()) |
---|
553 | n/a | self.assertRaises(TypeError, a.__add__, b) |
---|
554 | n/a | |
---|
555 | n/a | self.assertRaises(TypeError, a.__iadd__, "bad") |
---|
556 | n/a | |
---|
557 | n/a | def test_mul(self): |
---|
558 | n/a | a = 5*array.array(self.typecode, self.example) |
---|
559 | n/a | self.assertEqual( |
---|
560 | n/a | a, |
---|
561 | n/a | array.array(self.typecode, 5*self.example) |
---|
562 | n/a | ) |
---|
563 | n/a | |
---|
564 | n/a | a = array.array(self.typecode, self.example)*5 |
---|
565 | n/a | self.assertEqual( |
---|
566 | n/a | a, |
---|
567 | n/a | array.array(self.typecode, self.example*5) |
---|
568 | n/a | ) |
---|
569 | n/a | |
---|
570 | n/a | a = 0*array.array(self.typecode, self.example) |
---|
571 | n/a | self.assertEqual( |
---|
572 | n/a | a, |
---|
573 | n/a | array.array(self.typecode) |
---|
574 | n/a | ) |
---|
575 | n/a | |
---|
576 | n/a | a = (-1)*array.array(self.typecode, self.example) |
---|
577 | n/a | self.assertEqual( |
---|
578 | n/a | a, |
---|
579 | n/a | array.array(self.typecode) |
---|
580 | n/a | ) |
---|
581 | n/a | |
---|
582 | n/a | a = 5 * array.array(self.typecode, self.example[:1]) |
---|
583 | n/a | self.assertEqual( |
---|
584 | n/a | a, |
---|
585 | n/a | array.array(self.typecode, [a[0]] * 5) |
---|
586 | n/a | ) |
---|
587 | n/a | |
---|
588 | n/a | self.assertRaises(TypeError, a.__mul__, "bad") |
---|
589 | n/a | |
---|
590 | n/a | def test_imul(self): |
---|
591 | n/a | a = array.array(self.typecode, self.example) |
---|
592 | n/a | b = a |
---|
593 | n/a | |
---|
594 | n/a | a *= 5 |
---|
595 | n/a | self.assertIs(a, b) |
---|
596 | n/a | self.assertEqual( |
---|
597 | n/a | a, |
---|
598 | n/a | array.array(self.typecode, 5*self.example) |
---|
599 | n/a | ) |
---|
600 | n/a | |
---|
601 | n/a | a *= 0 |
---|
602 | n/a | self.assertIs(a, b) |
---|
603 | n/a | self.assertEqual(a, array.array(self.typecode)) |
---|
604 | n/a | |
---|
605 | n/a | a *= 1000 |
---|
606 | n/a | self.assertIs(a, b) |
---|
607 | n/a | self.assertEqual(a, array.array(self.typecode)) |
---|
608 | n/a | |
---|
609 | n/a | a *= -1 |
---|
610 | n/a | self.assertIs(a, b) |
---|
611 | n/a | self.assertEqual(a, array.array(self.typecode)) |
---|
612 | n/a | |
---|
613 | n/a | a = array.array(self.typecode, self.example) |
---|
614 | n/a | a *= -1 |
---|
615 | n/a | self.assertEqual(a, array.array(self.typecode)) |
---|
616 | n/a | |
---|
617 | n/a | self.assertRaises(TypeError, a.__imul__, "bad") |
---|
618 | n/a | |
---|
619 | n/a | def test_getitem(self): |
---|
620 | n/a | a = array.array(self.typecode, self.example) |
---|
621 | n/a | self.assertEntryEqual(a[0], self.example[0]) |
---|
622 | n/a | self.assertEntryEqual(a[0], self.example[0]) |
---|
623 | n/a | self.assertEntryEqual(a[-1], self.example[-1]) |
---|
624 | n/a | self.assertEntryEqual(a[-1], self.example[-1]) |
---|
625 | n/a | self.assertEntryEqual(a[len(self.example)-1], self.example[-1]) |
---|
626 | n/a | self.assertEntryEqual(a[-len(self.example)], self.example[0]) |
---|
627 | n/a | self.assertRaises(TypeError, a.__getitem__) |
---|
628 | n/a | self.assertRaises(IndexError, a.__getitem__, len(self.example)) |
---|
629 | n/a | self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1) |
---|
630 | n/a | |
---|
631 | n/a | def test_setitem(self): |
---|
632 | n/a | a = array.array(self.typecode, self.example) |
---|
633 | n/a | a[0] = a[-1] |
---|
634 | n/a | self.assertEntryEqual(a[0], a[-1]) |
---|
635 | n/a | |
---|
636 | n/a | a = array.array(self.typecode, self.example) |
---|
637 | n/a | a[0] = a[-1] |
---|
638 | n/a | self.assertEntryEqual(a[0], a[-1]) |
---|
639 | n/a | |
---|
640 | n/a | a = array.array(self.typecode, self.example) |
---|
641 | n/a | a[-1] = a[0] |
---|
642 | n/a | self.assertEntryEqual(a[0], a[-1]) |
---|
643 | n/a | |
---|
644 | n/a | a = array.array(self.typecode, self.example) |
---|
645 | n/a | a[-1] = a[0] |
---|
646 | n/a | self.assertEntryEqual(a[0], a[-1]) |
---|
647 | n/a | |
---|
648 | n/a | a = array.array(self.typecode, self.example) |
---|
649 | n/a | a[len(self.example)-1] = a[0] |
---|
650 | n/a | self.assertEntryEqual(a[0], a[-1]) |
---|
651 | n/a | |
---|
652 | n/a | a = array.array(self.typecode, self.example) |
---|
653 | n/a | a[-len(self.example)] = a[-1] |
---|
654 | n/a | self.assertEntryEqual(a[0], a[-1]) |
---|
655 | n/a | |
---|
656 | n/a | self.assertRaises(TypeError, a.__setitem__) |
---|
657 | n/a | self.assertRaises(TypeError, a.__setitem__, None) |
---|
658 | n/a | self.assertRaises(TypeError, a.__setitem__, 0, None) |
---|
659 | n/a | self.assertRaises( |
---|
660 | n/a | IndexError, |
---|
661 | n/a | a.__setitem__, |
---|
662 | n/a | len(self.example), self.example[0] |
---|
663 | n/a | ) |
---|
664 | n/a | self.assertRaises( |
---|
665 | n/a | IndexError, |
---|
666 | n/a | a.__setitem__, |
---|
667 | n/a | -len(self.example)-1, self.example[0] |
---|
668 | n/a | ) |
---|
669 | n/a | |
---|
670 | n/a | def test_delitem(self): |
---|
671 | n/a | a = array.array(self.typecode, self.example) |
---|
672 | n/a | del a[0] |
---|
673 | n/a | self.assertEqual( |
---|
674 | n/a | a, |
---|
675 | n/a | array.array(self.typecode, self.example[1:]) |
---|
676 | n/a | ) |
---|
677 | n/a | |
---|
678 | n/a | a = array.array(self.typecode, self.example) |
---|
679 | n/a | del a[-1] |
---|
680 | n/a | self.assertEqual( |
---|
681 | n/a | a, |
---|
682 | n/a | array.array(self.typecode, self.example[:-1]) |
---|
683 | n/a | ) |
---|
684 | n/a | |
---|
685 | n/a | a = array.array(self.typecode, self.example) |
---|
686 | n/a | del a[len(self.example)-1] |
---|
687 | n/a | self.assertEqual( |
---|
688 | n/a | a, |
---|
689 | n/a | array.array(self.typecode, self.example[:-1]) |
---|
690 | n/a | ) |
---|
691 | n/a | |
---|
692 | n/a | a = array.array(self.typecode, self.example) |
---|
693 | n/a | del a[-len(self.example)] |
---|
694 | n/a | self.assertEqual( |
---|
695 | n/a | a, |
---|
696 | n/a | array.array(self.typecode, self.example[1:]) |
---|
697 | n/a | ) |
---|
698 | n/a | |
---|
699 | n/a | self.assertRaises(TypeError, a.__delitem__) |
---|
700 | n/a | self.assertRaises(TypeError, a.__delitem__, None) |
---|
701 | n/a | self.assertRaises(IndexError, a.__delitem__, len(self.example)) |
---|
702 | n/a | self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1) |
---|
703 | n/a | |
---|
704 | n/a | def test_getslice(self): |
---|
705 | n/a | a = array.array(self.typecode, self.example) |
---|
706 | n/a | self.assertEqual(a[:], a) |
---|
707 | n/a | |
---|
708 | n/a | self.assertEqual( |
---|
709 | n/a | a[1:], |
---|
710 | n/a | array.array(self.typecode, self.example[1:]) |
---|
711 | n/a | ) |
---|
712 | n/a | |
---|
713 | n/a | self.assertEqual( |
---|
714 | n/a | a[:1], |
---|
715 | n/a | array.array(self.typecode, self.example[:1]) |
---|
716 | n/a | ) |
---|
717 | n/a | |
---|
718 | n/a | self.assertEqual( |
---|
719 | n/a | a[:-1], |
---|
720 | n/a | array.array(self.typecode, self.example[:-1]) |
---|
721 | n/a | ) |
---|
722 | n/a | |
---|
723 | n/a | self.assertEqual( |
---|
724 | n/a | a[-1:], |
---|
725 | n/a | array.array(self.typecode, self.example[-1:]) |
---|
726 | n/a | ) |
---|
727 | n/a | |
---|
728 | n/a | self.assertEqual( |
---|
729 | n/a | a[-1:-1], |
---|
730 | n/a | array.array(self.typecode) |
---|
731 | n/a | ) |
---|
732 | n/a | |
---|
733 | n/a | self.assertEqual( |
---|
734 | n/a | a[2:1], |
---|
735 | n/a | array.array(self.typecode) |
---|
736 | n/a | ) |
---|
737 | n/a | |
---|
738 | n/a | self.assertEqual( |
---|
739 | n/a | a[1000:], |
---|
740 | n/a | array.array(self.typecode) |
---|
741 | n/a | ) |
---|
742 | n/a | self.assertEqual(a[-1000:], a) |
---|
743 | n/a | self.assertEqual(a[:1000], a) |
---|
744 | n/a | self.assertEqual( |
---|
745 | n/a | a[:-1000], |
---|
746 | n/a | array.array(self.typecode) |
---|
747 | n/a | ) |
---|
748 | n/a | self.assertEqual(a[-1000:1000], a) |
---|
749 | n/a | self.assertEqual( |
---|
750 | n/a | a[2000:1000], |
---|
751 | n/a | array.array(self.typecode) |
---|
752 | n/a | ) |
---|
753 | n/a | |
---|
754 | n/a | def test_extended_getslice(self): |
---|
755 | n/a | # Test extended slicing by comparing with list slicing |
---|
756 | n/a | # (Assumes list conversion works correctly, too) |
---|
757 | n/a | a = array.array(self.typecode, self.example) |
---|
758 | n/a | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
---|
759 | n/a | for start in indices: |
---|
760 | n/a | for stop in indices: |
---|
761 | n/a | # Everything except the initial 0 (invalid step) |
---|
762 | n/a | for step in indices[1:]: |
---|
763 | n/a | self.assertEqual(list(a[start:stop:step]), |
---|
764 | n/a | list(a)[start:stop:step]) |
---|
765 | n/a | |
---|
766 | n/a | def test_setslice(self): |
---|
767 | n/a | a = array.array(self.typecode, self.example) |
---|
768 | n/a | a[:1] = a |
---|
769 | n/a | self.assertEqual( |
---|
770 | n/a | a, |
---|
771 | n/a | array.array(self.typecode, self.example + self.example[1:]) |
---|
772 | n/a | ) |
---|
773 | n/a | |
---|
774 | n/a | a = array.array(self.typecode, self.example) |
---|
775 | n/a | a[:-1] = a |
---|
776 | n/a | self.assertEqual( |
---|
777 | n/a | a, |
---|
778 | n/a | array.array(self.typecode, self.example + self.example[-1:]) |
---|
779 | n/a | ) |
---|
780 | n/a | |
---|
781 | n/a | a = array.array(self.typecode, self.example) |
---|
782 | n/a | a[-1:] = a |
---|
783 | n/a | self.assertEqual( |
---|
784 | n/a | a, |
---|
785 | n/a | array.array(self.typecode, self.example[:-1] + self.example) |
---|
786 | n/a | ) |
---|
787 | n/a | |
---|
788 | n/a | a = array.array(self.typecode, self.example) |
---|
789 | n/a | a[1:] = a |
---|
790 | n/a | self.assertEqual( |
---|
791 | n/a | a, |
---|
792 | n/a | array.array(self.typecode, self.example[:1] + self.example) |
---|
793 | n/a | ) |
---|
794 | n/a | |
---|
795 | n/a | a = array.array(self.typecode, self.example) |
---|
796 | n/a | a[1:-1] = a |
---|
797 | n/a | self.assertEqual( |
---|
798 | n/a | a, |
---|
799 | n/a | array.array( |
---|
800 | n/a | self.typecode, |
---|
801 | n/a | self.example[:1] + self.example + self.example[-1:] |
---|
802 | n/a | ) |
---|
803 | n/a | ) |
---|
804 | n/a | |
---|
805 | n/a | a = array.array(self.typecode, self.example) |
---|
806 | n/a | a[1000:] = a |
---|
807 | n/a | self.assertEqual( |
---|
808 | n/a | a, |
---|
809 | n/a | array.array(self.typecode, 2*self.example) |
---|
810 | n/a | ) |
---|
811 | n/a | |
---|
812 | n/a | a = array.array(self.typecode, self.example) |
---|
813 | n/a | a[-1000:] = a |
---|
814 | n/a | self.assertEqual( |
---|
815 | n/a | a, |
---|
816 | n/a | array.array(self.typecode, self.example) |
---|
817 | n/a | ) |
---|
818 | n/a | |
---|
819 | n/a | a = array.array(self.typecode, self.example) |
---|
820 | n/a | a[:1000] = a |
---|
821 | n/a | self.assertEqual( |
---|
822 | n/a | a, |
---|
823 | n/a | array.array(self.typecode, self.example) |
---|
824 | n/a | ) |
---|
825 | n/a | |
---|
826 | n/a | a = array.array(self.typecode, self.example) |
---|
827 | n/a | a[:-1000] = a |
---|
828 | n/a | self.assertEqual( |
---|
829 | n/a | a, |
---|
830 | n/a | array.array(self.typecode, 2*self.example) |
---|
831 | n/a | ) |
---|
832 | n/a | |
---|
833 | n/a | a = array.array(self.typecode, self.example) |
---|
834 | n/a | a[1:0] = a |
---|
835 | n/a | self.assertEqual( |
---|
836 | n/a | a, |
---|
837 | n/a | array.array(self.typecode, self.example[:1] + self.example + self.example[1:]) |
---|
838 | n/a | ) |
---|
839 | n/a | |
---|
840 | n/a | a = array.array(self.typecode, self.example) |
---|
841 | n/a | a[2000:1000] = a |
---|
842 | n/a | self.assertEqual( |
---|
843 | n/a | a, |
---|
844 | n/a | array.array(self.typecode, 2*self.example) |
---|
845 | n/a | ) |
---|
846 | n/a | |
---|
847 | n/a | a = array.array(self.typecode, self.example) |
---|
848 | n/a | self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None) |
---|
849 | n/a | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) |
---|
850 | n/a | |
---|
851 | n/a | b = array.array(self.badtypecode()) |
---|
852 | n/a | self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b) |
---|
853 | n/a | self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) |
---|
854 | n/a | |
---|
855 | n/a | def test_extended_set_del_slice(self): |
---|
856 | n/a | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
---|
857 | n/a | for start in indices: |
---|
858 | n/a | for stop in indices: |
---|
859 | n/a | # Everything except the initial 0 (invalid step) |
---|
860 | n/a | for step in indices[1:]: |
---|
861 | n/a | a = array.array(self.typecode, self.example) |
---|
862 | n/a | L = list(a) |
---|
863 | n/a | # Make sure we have a slice of exactly the right length, |
---|
864 | n/a | # but with (hopefully) different data. |
---|
865 | n/a | data = L[start:stop:step] |
---|
866 | n/a | data.reverse() |
---|
867 | n/a | L[start:stop:step] = data |
---|
868 | n/a | a[start:stop:step] = array.array(self.typecode, data) |
---|
869 | n/a | self.assertEqual(a, array.array(self.typecode, L)) |
---|
870 | n/a | |
---|
871 | n/a | del L[start:stop:step] |
---|
872 | n/a | del a[start:stop:step] |
---|
873 | n/a | self.assertEqual(a, array.array(self.typecode, L)) |
---|
874 | n/a | |
---|
875 | n/a | def test_index(self): |
---|
876 | n/a | example = 2*self.example |
---|
877 | n/a | a = array.array(self.typecode, example) |
---|
878 | n/a | self.assertRaises(TypeError, a.index) |
---|
879 | n/a | for x in example: |
---|
880 | n/a | self.assertEqual(a.index(x), example.index(x)) |
---|
881 | n/a | self.assertRaises(ValueError, a.index, None) |
---|
882 | n/a | self.assertRaises(ValueError, a.index, self.outside) |
---|
883 | n/a | |
---|
884 | n/a | def test_count(self): |
---|
885 | n/a | example = 2*self.example |
---|
886 | n/a | a = array.array(self.typecode, example) |
---|
887 | n/a | self.assertRaises(TypeError, a.count) |
---|
888 | n/a | for x in example: |
---|
889 | n/a | self.assertEqual(a.count(x), example.count(x)) |
---|
890 | n/a | self.assertEqual(a.count(self.outside), 0) |
---|
891 | n/a | self.assertEqual(a.count(None), 0) |
---|
892 | n/a | |
---|
893 | n/a | def test_remove(self): |
---|
894 | n/a | for x in self.example: |
---|
895 | n/a | example = 2*self.example |
---|
896 | n/a | a = array.array(self.typecode, example) |
---|
897 | n/a | pos = example.index(x) |
---|
898 | n/a | example2 = example[:pos] + example[pos+1:] |
---|
899 | n/a | a.remove(x) |
---|
900 | n/a | self.assertEqual(a, array.array(self.typecode, example2)) |
---|
901 | n/a | |
---|
902 | n/a | a = array.array(self.typecode, self.example) |
---|
903 | n/a | self.assertRaises(ValueError, a.remove, self.outside) |
---|
904 | n/a | |
---|
905 | n/a | self.assertRaises(ValueError, a.remove, None) |
---|
906 | n/a | |
---|
907 | n/a | def test_pop(self): |
---|
908 | n/a | a = array.array(self.typecode) |
---|
909 | n/a | self.assertRaises(IndexError, a.pop) |
---|
910 | n/a | |
---|
911 | n/a | a = array.array(self.typecode, 2*self.example) |
---|
912 | n/a | self.assertRaises(TypeError, a.pop, 42, 42) |
---|
913 | n/a | self.assertRaises(TypeError, a.pop, None) |
---|
914 | n/a | self.assertRaises(IndexError, a.pop, len(a)) |
---|
915 | n/a | self.assertRaises(IndexError, a.pop, -len(a)-1) |
---|
916 | n/a | |
---|
917 | n/a | self.assertEntryEqual(a.pop(0), self.example[0]) |
---|
918 | n/a | self.assertEqual( |
---|
919 | n/a | a, |
---|
920 | n/a | array.array(self.typecode, self.example[1:]+self.example) |
---|
921 | n/a | ) |
---|
922 | n/a | self.assertEntryEqual(a.pop(1), self.example[2]) |
---|
923 | n/a | self.assertEqual( |
---|
924 | n/a | a, |
---|
925 | n/a | array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example) |
---|
926 | n/a | ) |
---|
927 | n/a | self.assertEntryEqual(a.pop(0), self.example[1]) |
---|
928 | n/a | self.assertEntryEqual(a.pop(), self.example[-1]) |
---|
929 | n/a | self.assertEqual( |
---|
930 | n/a | a, |
---|
931 | n/a | array.array(self.typecode, self.example[3:]+self.example[:-1]) |
---|
932 | n/a | ) |
---|
933 | n/a | |
---|
934 | n/a | def test_reverse(self): |
---|
935 | n/a | a = array.array(self.typecode, self.example) |
---|
936 | n/a | self.assertRaises(TypeError, a.reverse, 42) |
---|
937 | n/a | a.reverse() |
---|
938 | n/a | self.assertEqual( |
---|
939 | n/a | a, |
---|
940 | n/a | array.array(self.typecode, self.example[::-1]) |
---|
941 | n/a | ) |
---|
942 | n/a | |
---|
943 | n/a | def test_extend(self): |
---|
944 | n/a | a = array.array(self.typecode, self.example) |
---|
945 | n/a | self.assertRaises(TypeError, a.extend) |
---|
946 | n/a | a.extend(array.array(self.typecode, self.example[::-1])) |
---|
947 | n/a | self.assertEqual( |
---|
948 | n/a | a, |
---|
949 | n/a | array.array(self.typecode, self.example+self.example[::-1]) |
---|
950 | n/a | ) |
---|
951 | n/a | |
---|
952 | n/a | a = array.array(self.typecode, self.example) |
---|
953 | n/a | a.extend(a) |
---|
954 | n/a | self.assertEqual( |
---|
955 | n/a | a, |
---|
956 | n/a | array.array(self.typecode, self.example+self.example) |
---|
957 | n/a | ) |
---|
958 | n/a | |
---|
959 | n/a | b = array.array(self.badtypecode()) |
---|
960 | n/a | self.assertRaises(TypeError, a.extend, b) |
---|
961 | n/a | |
---|
962 | n/a | a = array.array(self.typecode, self.example) |
---|
963 | n/a | a.extend(self.example[::-1]) |
---|
964 | n/a | self.assertEqual( |
---|
965 | n/a | a, |
---|
966 | n/a | array.array(self.typecode, self.example+self.example[::-1]) |
---|
967 | n/a | ) |
---|
968 | n/a | |
---|
969 | n/a | def test_constructor_with_iterable_argument(self): |
---|
970 | n/a | a = array.array(self.typecode, iter(self.example)) |
---|
971 | n/a | b = array.array(self.typecode, self.example) |
---|
972 | n/a | self.assertEqual(a, b) |
---|
973 | n/a | |
---|
974 | n/a | # non-iterable argument |
---|
975 | n/a | self.assertRaises(TypeError, array.array, self.typecode, 10) |
---|
976 | n/a | |
---|
977 | n/a | # pass through errors raised in __iter__ |
---|
978 | n/a | class A: |
---|
979 | n/a | def __iter__(self): |
---|
980 | n/a | raise UnicodeError |
---|
981 | n/a | self.assertRaises(UnicodeError, array.array, self.typecode, A()) |
---|
982 | n/a | |
---|
983 | n/a | # pass through errors raised in next() |
---|
984 | n/a | def B(): |
---|
985 | n/a | raise UnicodeError |
---|
986 | n/a | yield None |
---|
987 | n/a | self.assertRaises(UnicodeError, array.array, self.typecode, B()) |
---|
988 | n/a | |
---|
989 | n/a | def test_coveritertraverse(self): |
---|
990 | n/a | try: |
---|
991 | n/a | import gc |
---|
992 | n/a | except ImportError: |
---|
993 | n/a | self.skipTest('gc module not available') |
---|
994 | n/a | a = array.array(self.typecode) |
---|
995 | n/a | l = [iter(a)] |
---|
996 | n/a | l.append(l) |
---|
997 | n/a | gc.collect() |
---|
998 | n/a | |
---|
999 | n/a | def test_buffer(self): |
---|
1000 | n/a | a = array.array(self.typecode, self.example) |
---|
1001 | n/a | m = memoryview(a) |
---|
1002 | n/a | expected = m.tobytes() |
---|
1003 | n/a | self.assertEqual(a.tobytes(), expected) |
---|
1004 | n/a | self.assertEqual(a.tobytes()[0], expected[0]) |
---|
1005 | n/a | # Resizing is forbidden when there are buffer exports. |
---|
1006 | n/a | # For issue 4509, we also check after each error that |
---|
1007 | n/a | # the array was not modified. |
---|
1008 | n/a | self.assertRaises(BufferError, a.append, a[0]) |
---|
1009 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1010 | n/a | self.assertRaises(BufferError, a.extend, a[0:1]) |
---|
1011 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1012 | n/a | self.assertRaises(BufferError, a.remove, a[0]) |
---|
1013 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1014 | n/a | self.assertRaises(BufferError, a.pop, 0) |
---|
1015 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1016 | n/a | self.assertRaises(BufferError, a.fromlist, a.tolist()) |
---|
1017 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1018 | n/a | self.assertRaises(BufferError, a.frombytes, a.tobytes()) |
---|
1019 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1020 | n/a | if self.typecode == 'u': |
---|
1021 | n/a | self.assertRaises(BufferError, a.fromunicode, a.tounicode()) |
---|
1022 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1023 | n/a | self.assertRaises(BufferError, operator.imul, a, 2) |
---|
1024 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1025 | n/a | self.assertRaises(BufferError, operator.imul, a, 0) |
---|
1026 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1027 | n/a | self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a) |
---|
1028 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1029 | n/a | self.assertRaises(BufferError, operator.delitem, a, 0) |
---|
1030 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1031 | n/a | self.assertRaises(BufferError, operator.delitem, a, slice(0, 1)) |
---|
1032 | n/a | self.assertEqual(m.tobytes(), expected) |
---|
1033 | n/a | |
---|
1034 | n/a | def test_weakref(self): |
---|
1035 | n/a | s = array.array(self.typecode, self.example) |
---|
1036 | n/a | p = weakref.proxy(s) |
---|
1037 | n/a | self.assertEqual(p.tobytes(), s.tobytes()) |
---|
1038 | n/a | s = None |
---|
1039 | n/a | self.assertRaises(ReferenceError, len, p) |
---|
1040 | n/a | |
---|
1041 | n/a | @unittest.skipUnless(hasattr(sys, 'getrefcount'), |
---|
1042 | n/a | 'test needs sys.getrefcount()') |
---|
1043 | n/a | def test_bug_782369(self): |
---|
1044 | n/a | for i in range(10): |
---|
1045 | n/a | b = array.array('B', range(64)) |
---|
1046 | n/a | rc = sys.getrefcount(10) |
---|
1047 | n/a | for i in range(10): |
---|
1048 | n/a | b = array.array('B', range(64)) |
---|
1049 | n/a | self.assertEqual(rc, sys.getrefcount(10)) |
---|
1050 | n/a | |
---|
1051 | n/a | def test_subclass_with_kwargs(self): |
---|
1052 | n/a | # SF bug #1486663 -- this used to erroneously raise a TypeError |
---|
1053 | n/a | ArraySubclassWithKwargs('b', newarg=1) |
---|
1054 | n/a | |
---|
1055 | n/a | def test_create_from_bytes(self): |
---|
1056 | n/a | # XXX This test probably needs to be moved in a subclass or |
---|
1057 | n/a | # generalized to use self.typecode. |
---|
1058 | n/a | a = array.array('H', b"1234") |
---|
1059 | n/a | self.assertEqual(len(a) * a.itemsize, 4) |
---|
1060 | n/a | |
---|
1061 | n/a | @support.cpython_only |
---|
1062 | n/a | def test_sizeof_with_buffer(self): |
---|
1063 | n/a | a = array.array(self.typecode, self.example) |
---|
1064 | n/a | basesize = support.calcvobjsize('Pn2Pi') |
---|
1065 | n/a | buffer_size = a.buffer_info()[1] * a.itemsize |
---|
1066 | n/a | support.check_sizeof(self, a, basesize + buffer_size) |
---|
1067 | n/a | |
---|
1068 | n/a | @support.cpython_only |
---|
1069 | n/a | def test_sizeof_without_buffer(self): |
---|
1070 | n/a | a = array.array(self.typecode) |
---|
1071 | n/a | basesize = support.calcvobjsize('Pn2Pi') |
---|
1072 | n/a | support.check_sizeof(self, a, basesize) |
---|
1073 | n/a | |
---|
1074 | n/a | def test_initialize_with_unicode(self): |
---|
1075 | n/a | if self.typecode != 'u': |
---|
1076 | n/a | with self.assertRaises(TypeError) as cm: |
---|
1077 | n/a | a = array.array(self.typecode, 'foo') |
---|
1078 | n/a | self.assertIn("cannot use a str", str(cm.exception)) |
---|
1079 | n/a | with self.assertRaises(TypeError) as cm: |
---|
1080 | n/a | a = array.array(self.typecode, array.array('u', 'foo')) |
---|
1081 | n/a | self.assertIn("cannot use a unicode array", str(cm.exception)) |
---|
1082 | n/a | else: |
---|
1083 | n/a | a = array.array(self.typecode, "foo") |
---|
1084 | n/a | a = array.array(self.typecode, array.array('u', 'foo')) |
---|
1085 | n/a | |
---|
1086 | n/a | @support.cpython_only |
---|
1087 | n/a | def test_obsolete_write_lock(self): |
---|
1088 | n/a | from _testcapi import getbuffer_with_null_view |
---|
1089 | n/a | a = array.array('B', b"") |
---|
1090 | n/a | self.assertRaises(BufferError, getbuffer_with_null_view, a) |
---|
1091 | n/a | |
---|
1092 | n/a | def test_free_after_iterating(self): |
---|
1093 | n/a | support.check_free_after_iterating(self, iter, array.array, |
---|
1094 | n/a | (self.typecode,)) |
---|
1095 | n/a | support.check_free_after_iterating(self, reversed, array.array, |
---|
1096 | n/a | (self.typecode,)) |
---|
1097 | n/a | |
---|
1098 | n/a | class StringTest(BaseTest): |
---|
1099 | n/a | |
---|
1100 | n/a | def test_setitem(self): |
---|
1101 | n/a | super().test_setitem() |
---|
1102 | n/a | a = array.array(self.typecode, self.example) |
---|
1103 | n/a | self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) |
---|
1104 | n/a | |
---|
1105 | n/a | class UnicodeTest(StringTest, unittest.TestCase): |
---|
1106 | n/a | typecode = 'u' |
---|
1107 | n/a | example = '\x01\u263a\x00\ufeff' |
---|
1108 | n/a | smallerexample = '\x01\u263a\x00\ufefe' |
---|
1109 | n/a | biggerexample = '\x01\u263a\x01\ufeff' |
---|
1110 | n/a | outside = str('\x33') |
---|
1111 | n/a | minitemsize = 2 |
---|
1112 | n/a | |
---|
1113 | n/a | def test_unicode(self): |
---|
1114 | n/a | self.assertRaises(TypeError, array.array, 'b', 'foo') |
---|
1115 | n/a | |
---|
1116 | n/a | a = array.array('u', '\xa0\xc2\u1234') |
---|
1117 | n/a | a.fromunicode(' ') |
---|
1118 | n/a | a.fromunicode('') |
---|
1119 | n/a | a.fromunicode('') |
---|
1120 | n/a | a.fromunicode('\x11abc\xff\u1234') |
---|
1121 | n/a | s = a.tounicode() |
---|
1122 | n/a | self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') |
---|
1123 | n/a | self.assertEqual(a.itemsize, sizeof_wchar) |
---|
1124 | n/a | |
---|
1125 | n/a | s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
---|
1126 | n/a | a = array.array('u', s) |
---|
1127 | n/a | self.assertEqual( |
---|
1128 | n/a | repr(a), |
---|
1129 | n/a | "array('u', '\\x00=\"\\'a\\\\b\\x80\xff\\x00\\x01\u1234')") |
---|
1130 | n/a | |
---|
1131 | n/a | self.assertRaises(TypeError, a.fromunicode) |
---|
1132 | n/a | |
---|
1133 | n/a | def test_issue17223(self): |
---|
1134 | n/a | # this used to crash |
---|
1135 | n/a | if sizeof_wchar == 4: |
---|
1136 | n/a | # U+FFFFFFFF is an invalid code point in Unicode 6.0 |
---|
1137 | n/a | invalid_str = b'\xff\xff\xff\xff' |
---|
1138 | n/a | else: |
---|
1139 | n/a | # PyUnicode_FromUnicode() cannot fail with 16-bit wchar_t |
---|
1140 | n/a | self.skipTest("specific to 32-bit wchar_t") |
---|
1141 | n/a | a = array.array('u', invalid_str) |
---|
1142 | n/a | self.assertRaises(ValueError, a.tounicode) |
---|
1143 | n/a | self.assertRaises(ValueError, str, a) |
---|
1144 | n/a | |
---|
1145 | n/a | class NumberTest(BaseTest): |
---|
1146 | n/a | |
---|
1147 | n/a | def test_extslice(self): |
---|
1148 | n/a | a = array.array(self.typecode, range(5)) |
---|
1149 | n/a | self.assertEqual(a[::], a) |
---|
1150 | n/a | self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) |
---|
1151 | n/a | self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) |
---|
1152 | n/a | self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0])) |
---|
1153 | n/a | self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) |
---|
1154 | n/a | self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) |
---|
1155 | n/a | self.assertEqual(a[-100:100:], a) |
---|
1156 | n/a | self.assertEqual(a[100:-100:-1], a[::-1]) |
---|
1157 | n/a | self.assertEqual(a[-100:100:2], array.array(self.typecode, [0,2,4])) |
---|
1158 | n/a | self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) |
---|
1159 | n/a | self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) |
---|
1160 | n/a | |
---|
1161 | n/a | def test_delslice(self): |
---|
1162 | n/a | a = array.array(self.typecode, range(5)) |
---|
1163 | n/a | del a[::2] |
---|
1164 | n/a | self.assertEqual(a, array.array(self.typecode, [1,3])) |
---|
1165 | n/a | a = array.array(self.typecode, range(5)) |
---|
1166 | n/a | del a[1::2] |
---|
1167 | n/a | self.assertEqual(a, array.array(self.typecode, [0,2,4])) |
---|
1168 | n/a | a = array.array(self.typecode, range(5)) |
---|
1169 | n/a | del a[1::-2] |
---|
1170 | n/a | self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) |
---|
1171 | n/a | a = array.array(self.typecode, range(10)) |
---|
1172 | n/a | del a[::1000] |
---|
1173 | n/a | self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) |
---|
1174 | n/a | # test issue7788 |
---|
1175 | n/a | a = array.array(self.typecode, range(10)) |
---|
1176 | n/a | del a[9::1<<333] |
---|
1177 | n/a | |
---|
1178 | n/a | def test_assignment(self): |
---|
1179 | n/a | a = array.array(self.typecode, range(10)) |
---|
1180 | n/a | a[::2] = array.array(self.typecode, [42]*5) |
---|
1181 | n/a | self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9])) |
---|
1182 | n/a | a = array.array(self.typecode, range(10)) |
---|
1183 | n/a | a[::-4] = array.array(self.typecode, [10]*3) |
---|
1184 | n/a | self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
---|
1185 | n/a | a = array.array(self.typecode, range(4)) |
---|
1186 | n/a | a[::-1] = a |
---|
1187 | n/a | self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) |
---|
1188 | n/a | a = array.array(self.typecode, range(10)) |
---|
1189 | n/a | b = a[:] |
---|
1190 | n/a | c = a[:] |
---|
1191 | n/a | ins = array.array(self.typecode, range(2)) |
---|
1192 | n/a | a[2:3] = ins |
---|
1193 | n/a | b[slice(2,3)] = ins |
---|
1194 | n/a | c[2:3:] = ins |
---|
1195 | n/a | |
---|
1196 | n/a | def test_iterationcontains(self): |
---|
1197 | n/a | a = array.array(self.typecode, range(10)) |
---|
1198 | n/a | self.assertEqual(list(a), list(range(10))) |
---|
1199 | n/a | b = array.array(self.typecode, [20]) |
---|
1200 | n/a | self.assertEqual(a[-1] in a, True) |
---|
1201 | n/a | self.assertEqual(b[0] not in a, True) |
---|
1202 | n/a | |
---|
1203 | n/a | def check_overflow(self, lower, upper): |
---|
1204 | n/a | # method to be used by subclasses |
---|
1205 | n/a | |
---|
1206 | n/a | # should not overflow assigning lower limit |
---|
1207 | n/a | a = array.array(self.typecode, [lower]) |
---|
1208 | n/a | a[0] = lower |
---|
1209 | n/a | # should overflow assigning less than lower limit |
---|
1210 | n/a | self.assertRaises(OverflowError, array.array, self.typecode, [lower-1]) |
---|
1211 | n/a | self.assertRaises(OverflowError, a.__setitem__, 0, lower-1) |
---|
1212 | n/a | # should not overflow assigning upper limit |
---|
1213 | n/a | a = array.array(self.typecode, [upper]) |
---|
1214 | n/a | a[0] = upper |
---|
1215 | n/a | # should overflow assigning more than upper limit |
---|
1216 | n/a | self.assertRaises(OverflowError, array.array, self.typecode, [upper+1]) |
---|
1217 | n/a | self.assertRaises(OverflowError, a.__setitem__, 0, upper+1) |
---|
1218 | n/a | |
---|
1219 | n/a | def test_subclassing(self): |
---|
1220 | n/a | typecode = self.typecode |
---|
1221 | n/a | class ExaggeratingArray(array.array): |
---|
1222 | n/a | __slots__ = ['offset'] |
---|
1223 | n/a | |
---|
1224 | n/a | def __new__(cls, typecode, data, offset): |
---|
1225 | n/a | return array.array.__new__(cls, typecode, data) |
---|
1226 | n/a | |
---|
1227 | n/a | def __init__(self, typecode, data, offset): |
---|
1228 | n/a | self.offset = offset |
---|
1229 | n/a | |
---|
1230 | n/a | def __getitem__(self, i): |
---|
1231 | n/a | return array.array.__getitem__(self, i) + self.offset |
---|
1232 | n/a | |
---|
1233 | n/a | a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4) |
---|
1234 | n/a | self.assertEntryEqual(a[0], 7) |
---|
1235 | n/a | |
---|
1236 | n/a | self.assertRaises(AttributeError, setattr, a, "color", "blue") |
---|
1237 | n/a | |
---|
1238 | n/a | def test_frombytearray(self): |
---|
1239 | n/a | a = array.array('b', range(10)) |
---|
1240 | n/a | b = array.array(self.typecode, a) |
---|
1241 | n/a | self.assertEqual(a, b) |
---|
1242 | n/a | |
---|
1243 | n/a | class SignedNumberTest(NumberTest): |
---|
1244 | n/a | example = [-1, 0, 1, 42, 0x7f] |
---|
1245 | n/a | smallerexample = [-1, 0, 1, 42, 0x7e] |
---|
1246 | n/a | biggerexample = [-1, 0, 1, 43, 0x7f] |
---|
1247 | n/a | outside = 23 |
---|
1248 | n/a | |
---|
1249 | n/a | def test_overflow(self): |
---|
1250 | n/a | a = array.array(self.typecode) |
---|
1251 | n/a | lower = -1 * int(pow(2, a.itemsize * 8 - 1)) |
---|
1252 | n/a | upper = int(pow(2, a.itemsize * 8 - 1)) - 1 |
---|
1253 | n/a | self.check_overflow(lower, upper) |
---|
1254 | n/a | |
---|
1255 | n/a | class UnsignedNumberTest(NumberTest): |
---|
1256 | n/a | example = [0, 1, 17, 23, 42, 0xff] |
---|
1257 | n/a | smallerexample = [0, 1, 17, 23, 42, 0xfe] |
---|
1258 | n/a | biggerexample = [0, 1, 17, 23, 43, 0xff] |
---|
1259 | n/a | outside = 0xaa |
---|
1260 | n/a | |
---|
1261 | n/a | def test_overflow(self): |
---|
1262 | n/a | a = array.array(self.typecode) |
---|
1263 | n/a | lower = 0 |
---|
1264 | n/a | upper = int(pow(2, a.itemsize * 8)) - 1 |
---|
1265 | n/a | self.check_overflow(lower, upper) |
---|
1266 | n/a | |
---|
1267 | n/a | def test_bytes_extend(self): |
---|
1268 | n/a | s = bytes(self.example) |
---|
1269 | n/a | |
---|
1270 | n/a | a = array.array(self.typecode, self.example) |
---|
1271 | n/a | a.extend(s) |
---|
1272 | n/a | self.assertEqual( |
---|
1273 | n/a | a, |
---|
1274 | n/a | array.array(self.typecode, self.example+self.example) |
---|
1275 | n/a | ) |
---|
1276 | n/a | |
---|
1277 | n/a | a = array.array(self.typecode, self.example) |
---|
1278 | n/a | a.extend(bytearray(reversed(s))) |
---|
1279 | n/a | self.assertEqual( |
---|
1280 | n/a | a, |
---|
1281 | n/a | array.array(self.typecode, self.example+self.example[::-1]) |
---|
1282 | n/a | ) |
---|
1283 | n/a | |
---|
1284 | n/a | |
---|
1285 | n/a | class ByteTest(SignedNumberTest, unittest.TestCase): |
---|
1286 | n/a | typecode = 'b' |
---|
1287 | n/a | minitemsize = 1 |
---|
1288 | n/a | |
---|
1289 | n/a | class UnsignedByteTest(UnsignedNumberTest, unittest.TestCase): |
---|
1290 | n/a | typecode = 'B' |
---|
1291 | n/a | minitemsize = 1 |
---|
1292 | n/a | |
---|
1293 | n/a | class ShortTest(SignedNumberTest, unittest.TestCase): |
---|
1294 | n/a | typecode = 'h' |
---|
1295 | n/a | minitemsize = 2 |
---|
1296 | n/a | |
---|
1297 | n/a | class UnsignedShortTest(UnsignedNumberTest, unittest.TestCase): |
---|
1298 | n/a | typecode = 'H' |
---|
1299 | n/a | minitemsize = 2 |
---|
1300 | n/a | |
---|
1301 | n/a | class IntTest(SignedNumberTest, unittest.TestCase): |
---|
1302 | n/a | typecode = 'i' |
---|
1303 | n/a | minitemsize = 2 |
---|
1304 | n/a | |
---|
1305 | n/a | class UnsignedIntTest(UnsignedNumberTest, unittest.TestCase): |
---|
1306 | n/a | typecode = 'I' |
---|
1307 | n/a | minitemsize = 2 |
---|
1308 | n/a | |
---|
1309 | n/a | class LongTest(SignedNumberTest, unittest.TestCase): |
---|
1310 | n/a | typecode = 'l' |
---|
1311 | n/a | minitemsize = 4 |
---|
1312 | n/a | |
---|
1313 | n/a | class UnsignedLongTest(UnsignedNumberTest, unittest.TestCase): |
---|
1314 | n/a | typecode = 'L' |
---|
1315 | n/a | minitemsize = 4 |
---|
1316 | n/a | |
---|
1317 | n/a | @unittest.skipIf(not have_long_long, 'need long long support') |
---|
1318 | n/a | class LongLongTest(SignedNumberTest, unittest.TestCase): |
---|
1319 | n/a | typecode = 'q' |
---|
1320 | n/a | minitemsize = 8 |
---|
1321 | n/a | |
---|
1322 | n/a | @unittest.skipIf(not have_long_long, 'need long long support') |
---|
1323 | n/a | class UnsignedLongLongTest(UnsignedNumberTest, unittest.TestCase): |
---|
1324 | n/a | typecode = 'Q' |
---|
1325 | n/a | minitemsize = 8 |
---|
1326 | n/a | |
---|
1327 | n/a | class FPTest(NumberTest): |
---|
1328 | n/a | example = [-42.0, 0, 42, 1e5, -1e10] |
---|
1329 | n/a | smallerexample = [-42.0, 0, 42, 1e5, -2e10] |
---|
1330 | n/a | biggerexample = [-42.0, 0, 42, 1e5, 1e10] |
---|
1331 | n/a | outside = 23 |
---|
1332 | n/a | |
---|
1333 | n/a | def assertEntryEqual(self, entry1, entry2): |
---|
1334 | n/a | self.assertAlmostEqual(entry1, entry2) |
---|
1335 | n/a | |
---|
1336 | n/a | def test_byteswap(self): |
---|
1337 | n/a | a = array.array(self.typecode, self.example) |
---|
1338 | n/a | self.assertRaises(TypeError, a.byteswap, 42) |
---|
1339 | n/a | if a.itemsize in (1, 2, 4, 8): |
---|
1340 | n/a | b = array.array(self.typecode, self.example) |
---|
1341 | n/a | b.byteswap() |
---|
1342 | n/a | if a.itemsize==1: |
---|
1343 | n/a | self.assertEqual(a, b) |
---|
1344 | n/a | else: |
---|
1345 | n/a | # On alphas treating the byte swapped bit patters as |
---|
1346 | n/a | # floats/doubles results in floating point exceptions |
---|
1347 | n/a | # => compare the 8bit string values instead |
---|
1348 | n/a | self.assertNotEqual(a.tobytes(), b.tobytes()) |
---|
1349 | n/a | b.byteswap() |
---|
1350 | n/a | self.assertEqual(a, b) |
---|
1351 | n/a | |
---|
1352 | n/a | class FloatTest(FPTest, unittest.TestCase): |
---|
1353 | n/a | typecode = 'f' |
---|
1354 | n/a | minitemsize = 4 |
---|
1355 | n/a | |
---|
1356 | n/a | class DoubleTest(FPTest, unittest.TestCase): |
---|
1357 | n/a | typecode = 'd' |
---|
1358 | n/a | minitemsize = 8 |
---|
1359 | n/a | |
---|
1360 | n/a | def test_alloc_overflow(self): |
---|
1361 | n/a | from sys import maxsize |
---|
1362 | n/a | a = array.array('d', [-1]*65536) |
---|
1363 | n/a | try: |
---|
1364 | n/a | a *= maxsize//65536 + 1 |
---|
1365 | n/a | except MemoryError: |
---|
1366 | n/a | pass |
---|
1367 | n/a | else: |
---|
1368 | n/a | self.fail("Array of size > maxsize created - MemoryError expected") |
---|
1369 | n/a | b = array.array('d', [ 2.71828183, 3.14159265, -1]) |
---|
1370 | n/a | try: |
---|
1371 | n/a | b * (maxsize//3 + 1) |
---|
1372 | n/a | except MemoryError: |
---|
1373 | n/a | pass |
---|
1374 | n/a | else: |
---|
1375 | n/a | self.fail("Array of size > maxsize created - MemoryError expected") |
---|
1376 | n/a | |
---|
1377 | n/a | |
---|
1378 | n/a | if __name__ == "__main__": |
---|
1379 | n/a | unittest.main() |
---|