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