ยปCore Development>Code coverage>Lib/test/test_array.py

Python code coverage for Lib/test/test_array.py

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