blob: 8f82ab5297b7b72356cfc05da5f0cd55e8129108 [file] [log] [blame]
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support, list_tests
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003import pickle
Zachary Wareac28b792015-12-04 23:32:23 -06004import unittest
Walter Dörwald1dde95d2003-12-08 11:38:45 +00005
6class ListTest(list_tests.CommonTest):
7 type2test = list
8
Christian Heimes81ee3ef2008-05-04 22:42:01 +00009 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 Petersonc9c0f202009-06-30 23:06:06 +000014 self.assertTrue(l0_3 is not l0_3_bis)
Christian Heimes81ee3ef2008-05-04 22:42:01 +000015 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 Petersona786b022008-08-25 21:05:21 +000020 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 Heimes81ee3ef2008-05-04 22:42:01 +000037 # 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örwald1dde95d2003-12-08 11:38:45 +000042 def test_truth(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000043 super().test_truth()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000044 self.assertTrue(not [])
45 self.assertTrue([42])
Walter Dörwald1dde95d2003-12-08 11:38:45 +000046
47 def test_identity(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000048 self.assertTrue([] is not [])
Walter Dörwald1dde95d2003-12-08 11:38:45 +000049
50 def test_len(self):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000051 super().test_len()
Walter Dörwald1dde95d2003-12-08 11:38:45 +000052 self.assertEqual(len([]), 0)
53 self.assertEqual(len([0]), 1)
54 self.assertEqual(len([0, 1, 2]), 3)
55
Guido van Rossum8ce8a782007-11-01 19:42:39 +000056 def test_overflow(self):
57 lst = [4, 5, 6, 7]
Christian Heimesa37d4c62007-12-04 23:02:19 +000058 n = int((sys.maxsize*2+2) // len(lst))
Guido van Rossum8ce8a782007-11-01 19:42:39 +000059 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 Pitroueeb7eea2011-10-06 18:57:27 +020064 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ónsson31668b82012-04-03 10:49:41 +000074 def test_iterator_pickle(self):
Serhiy Storchakaaabafe72016-03-06 14:10:24 +020075 orig = self.type2test([4, 5, 6, 7])
76 data = [10, 11, 12, 13, 14, 15]
Serhiy Storchakabad12572014-12-15 14:03:42 +020077 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Serhiy Storchakaaabafe72016-03-06 14:10:24 +020078 # 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ónsson31668b82012-04-03 10:49:41 +000085
Serhiy Storchakaaabafe72016-03-06 14:10:24 +020086 # 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ónsson31668b82012-04-03 10:49:41 +0000109
110 def test_reversed_pickle(self):
Serhiy Storchakaaabafe72016-03-06 14:10:24 +0200111 orig = self.type2test([4, 5, 6, 7])
112 data = [10, 11, 12, 13, 14, 15]
Serhiy Storchakabad12572014-12-15 14:03:42 +0200113 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Serhiy Storchakaaabafe72016-03-06 14:10:24 +0200114 # 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ónsson31668b82012-04-03 10:49:41 +0000121
Serhiy Storchakaaabafe72016-03-06 14:10:24 +0200122 # 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 Pitroueeb7eea2011-10-06 18:57:27 +0200145
Martin v. Löwis4c1730d2012-08-01 10:32:11 +0200146 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 Hettingerbcab2b22004-09-29 08:03:17 +0000154if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500155 unittest.main()