| 1 | n/a | import sys |
|---|
| 2 | n/a | from test import list_tests |
|---|
| 3 | n/a | import pickle |
|---|
| 4 | n/a | import unittest |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | class ListTest(list_tests.CommonTest): |
|---|
| 7 | n/a | type2test = list |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | def test_basic(self): |
|---|
| 10 | n/a | self.assertEqual(list([]), []) |
|---|
| 11 | n/a | l0_3 = [0, 1, 2, 3] |
|---|
| 12 | n/a | l0_3_bis = list(l0_3) |
|---|
| 13 | n/a | self.assertEqual(l0_3, l0_3_bis) |
|---|
| 14 | n/a | self.assertTrue(l0_3 is not l0_3_bis) |
|---|
| 15 | n/a | self.assertEqual(list(()), []) |
|---|
| 16 | n/a | self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3]) |
|---|
| 17 | n/a | self.assertEqual(list(''), []) |
|---|
| 18 | n/a | self.assertEqual(list('spam'), ['s', 'p', 'a', 'm']) |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | if sys.maxsize == 0x7fffffff: |
|---|
| 21 | n/a | # This test can currently only work on 32-bit machines. |
|---|
| 22 | n/a | # XXX If/when PySequence_Length() returns a ssize_t, it should be |
|---|
| 23 | n/a | # XXX re-enabled. |
|---|
| 24 | n/a | # Verify clearing of bug #556025. |
|---|
| 25 | n/a | # This assumes that the max data size (sys.maxint) == max |
|---|
| 26 | n/a | # address size this also assumes that the address size is at |
|---|
| 27 | n/a | # least 4 bytes with 8 byte addresses, the bug is not well |
|---|
| 28 | n/a | # tested |
|---|
| 29 | n/a | # |
|---|
| 30 | n/a | # Note: This test is expected to SEGV under Cygwin 1.3.12 or |
|---|
| 31 | n/a | # earlier due to a newlib bug. See the following mailing list |
|---|
| 32 | n/a | # thread for the details: |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | # http://sources.redhat.com/ml/newlib/2002/msg00369.html |
|---|
| 35 | n/a | self.assertRaises(MemoryError, list, range(sys.maxsize // 2)) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | # This code used to segfault in Py2.4a3 |
|---|
| 38 | n/a | x = [] |
|---|
| 39 | n/a | x.extend(-y for y in x) |
|---|
| 40 | n/a | self.assertEqual(x, []) |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | def test_truth(self): |
|---|
| 43 | n/a | super().test_truth() |
|---|
| 44 | n/a | self.assertTrue(not []) |
|---|
| 45 | n/a | self.assertTrue([42]) |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def test_identity(self): |
|---|
| 48 | n/a | self.assertTrue([] is not []) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def test_len(self): |
|---|
| 51 | n/a | super().test_len() |
|---|
| 52 | n/a | self.assertEqual(len([]), 0) |
|---|
| 53 | n/a | self.assertEqual(len([0]), 1) |
|---|
| 54 | n/a | self.assertEqual(len([0, 1, 2]), 3) |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | def test_overflow(self): |
|---|
| 57 | n/a | lst = [4, 5, 6, 7] |
|---|
| 58 | n/a | n = int((sys.maxsize*2+2) // len(lst)) |
|---|
| 59 | n/a | def mul(a, b): return a * b |
|---|
| 60 | n/a | def imul(a, b): a *= b |
|---|
| 61 | n/a | self.assertRaises((MemoryError, OverflowError), mul, lst, n) |
|---|
| 62 | n/a | self.assertRaises((MemoryError, OverflowError), imul, lst, n) |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def test_repr_large(self): |
|---|
| 65 | n/a | # Check the repr of large list objects |
|---|
| 66 | n/a | def check(n): |
|---|
| 67 | n/a | l = [0] * n |
|---|
| 68 | n/a | s = repr(l) |
|---|
| 69 | n/a | self.assertEqual(s, |
|---|
| 70 | n/a | '[' + ', '.join(['0'] * n) + ']') |
|---|
| 71 | n/a | check(10) # check our checking code |
|---|
| 72 | n/a | check(1000000) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | def test_iterator_pickle(self): |
|---|
| 75 | n/a | orig = self.type2test([4, 5, 6, 7]) |
|---|
| 76 | n/a | data = [10, 11, 12, 13, 14, 15] |
|---|
| 77 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
|---|
| 78 | n/a | # initial iterator |
|---|
| 79 | n/a | itorig = iter(orig) |
|---|
| 80 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 81 | n/a | it, a = pickle.loads(d) |
|---|
| 82 | n/a | a[:] = data |
|---|
| 83 | n/a | self.assertEqual(type(it), type(itorig)) |
|---|
| 84 | n/a | self.assertEqual(list(it), data) |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | # running iterator |
|---|
| 87 | n/a | next(itorig) |
|---|
| 88 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 89 | n/a | it, a = pickle.loads(d) |
|---|
| 90 | n/a | a[:] = data |
|---|
| 91 | n/a | self.assertEqual(type(it), type(itorig)) |
|---|
| 92 | n/a | self.assertEqual(list(it), data[1:]) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | # empty iterator |
|---|
| 95 | n/a | for i in range(1, len(orig)): |
|---|
| 96 | n/a | next(itorig) |
|---|
| 97 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 98 | n/a | it, a = pickle.loads(d) |
|---|
| 99 | n/a | a[:] = data |
|---|
| 100 | n/a | self.assertEqual(type(it), type(itorig)) |
|---|
| 101 | n/a | self.assertEqual(list(it), data[len(orig):]) |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | # exhausted iterator |
|---|
| 104 | n/a | self.assertRaises(StopIteration, next, itorig) |
|---|
| 105 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 106 | n/a | it, a = pickle.loads(d) |
|---|
| 107 | n/a | a[:] = data |
|---|
| 108 | n/a | self.assertEqual(list(it), []) |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def test_reversed_pickle(self): |
|---|
| 111 | n/a | orig = self.type2test([4, 5, 6, 7]) |
|---|
| 112 | n/a | data = [10, 11, 12, 13, 14, 15] |
|---|
| 113 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
|---|
| 114 | n/a | # initial iterator |
|---|
| 115 | n/a | itorig = reversed(orig) |
|---|
| 116 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 117 | n/a | it, a = pickle.loads(d) |
|---|
| 118 | n/a | a[:] = data |
|---|
| 119 | n/a | self.assertEqual(type(it), type(itorig)) |
|---|
| 120 | n/a | self.assertEqual(list(it), data[len(orig)-1::-1]) |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | # running iterator |
|---|
| 123 | n/a | next(itorig) |
|---|
| 124 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 125 | n/a | it, a = pickle.loads(d) |
|---|
| 126 | n/a | a[:] = data |
|---|
| 127 | n/a | self.assertEqual(type(it), type(itorig)) |
|---|
| 128 | n/a | self.assertEqual(list(it), data[len(orig)-2::-1]) |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | # empty iterator |
|---|
| 131 | n/a | for i in range(1, len(orig)): |
|---|
| 132 | n/a | next(itorig) |
|---|
| 133 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 134 | n/a | it, a = pickle.loads(d) |
|---|
| 135 | n/a | a[:] = data |
|---|
| 136 | n/a | self.assertEqual(type(it), type(itorig)) |
|---|
| 137 | n/a | self.assertEqual(list(it), []) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | # exhausted iterator |
|---|
| 140 | n/a | self.assertRaises(StopIteration, next, itorig) |
|---|
| 141 | n/a | d = pickle.dumps((itorig, orig), proto) |
|---|
| 142 | n/a | it, a = pickle.loads(d) |
|---|
| 143 | n/a | a[:] = data |
|---|
| 144 | n/a | self.assertEqual(list(it), []) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def test_no_comdat_folding(self): |
|---|
| 147 | n/a | # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding |
|---|
| 148 | n/a | # optimization causes failures in code that relies on distinct |
|---|
| 149 | n/a | # function addresses. |
|---|
| 150 | n/a | class L(list): pass |
|---|
| 151 | n/a | with self.assertRaises(TypeError): |
|---|
| 152 | n/a | (3,) + L([1,2]) |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | if __name__ == "__main__": |
|---|
| 155 | n/a | unittest.main() |
|---|