Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support, seq_tests |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 2 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 3 | import gc |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4 | import pickle |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 5 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 6 | class TupleTest(seq_tests.CommonTest): |
| 7 | type2test = tuple |
| 8 | |
| 9 | def test_constructors(self): |
Éric Araujo | a63c240 | 2010-12-23 19:13:05 +0000 | [diff] [blame] | 10 | super().test_constructors() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 11 | # calling built-in types without argument must return empty |
| 12 | self.assertEqual(tuple(), ()) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 13 | t0_3 = (0, 1, 2, 3) |
| 14 | t0_3_bis = tuple(t0_3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 15 | self.assertTrue(t0_3 is t0_3_bis) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 16 | self.assertEqual(tuple([]), ()) |
| 17 | self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
| 18 | self.assertEqual(tuple(''), ()) |
| 19 | self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 20 | |
| 21 | def test_truth(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 22 | super().test_truth() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 23 | self.assertTrue(not ()) |
| 24 | self.assertTrue((42, )) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 25 | |
| 26 | def test_len(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 27 | super().test_len() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 28 | self.assertEqual(len(()), 0) |
| 29 | self.assertEqual(len((0,)), 1) |
| 30 | self.assertEqual(len((0, 1, 2)), 3) |
| 31 | |
| 32 | def test_iadd(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 33 | super().test_iadd() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 34 | u = (0, 1) |
| 35 | u2 = u |
| 36 | u += (2, 3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 37 | self.assertTrue(u is not u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 38 | |
| 39 | def test_imul(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 40 | super().test_imul() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 41 | u = (0, 1) |
| 42 | u2 = u |
| 43 | u *= 3 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 44 | self.assertTrue(u is not u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 45 | |
| 46 | def test_tupleresizebug(self): |
| 47 | # Check that a specific bug in _PyTuple_Resize() is squashed. |
| 48 | def f(): |
| 49 | for i in range(1000): |
| 50 | yield i |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 51 | self.assertEqual(list(tuple(f())), list(range(1000))) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 52 | |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 53 | def test_hash(self): |
| 54 | # See SF bug 942952: Weakness in tuple hash |
| 55 | # The hash should: |
| 56 | # be non-commutative |
| 57 | # should spread-out closely spaced values |
| 58 | # should not exhibit cancellation in tuples like (x,(x,y)) |
| 59 | # should be distinct from element hashes: hash(x)!=hash((x,)) |
| 60 | # This test exercises those cases. |
Tim Peters | 1f4bcf9 | 2004-06-01 18:58:04 +0000 | [diff] [blame] | 61 | # For a pure random hash and N=50, the expected number of occupied |
| 62 | # buckets when tossing 252,600 balls into 2**32 buckets |
| 63 | # is 252,592.6, or about 7.4 expected collisions. The |
| 64 | # standard deviation is 2.73. On a box with 64-bit hash |
| 65 | # codes, no collisions are expected. Here we accept no |
| 66 | # more than 15 collisions. Any worse and the hash function |
| 67 | # is sorely suspect. |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 68 | |
| 69 | N=50 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 70 | base = list(range(N)) |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 71 | xp = [(i, j) for i in base for j in base] |
| 72 | inps = base + [(i, j) for i in base for j in xp] + \ |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 73 | [(i, j) for i in xp for j in base] + xp + list(zip(base)) |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 74 | collisions = len(inps) - len(set(map(hash, inps))) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 75 | self.assertTrue(collisions <= 15) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 76 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 77 | def test_repr(self): |
| 78 | l0 = tuple() |
| 79 | l2 = (0, 1, 2) |
| 80 | a0 = self.type2test(l0) |
| 81 | a2 = self.type2test(l2) |
| 82 | |
| 83 | self.assertEqual(str(a0), repr(l0)) |
| 84 | self.assertEqual(str(a2), repr(l2)) |
| 85 | self.assertEqual(repr(a0), "()") |
| 86 | self.assertEqual(repr(a2), "(0, 1, 2)") |
| 87 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 88 | def _not_tracked(self, t): |
| 89 | # Nested tuples can take several collections to untrack |
| 90 | gc.collect() |
| 91 | gc.collect() |
| 92 | self.assertFalse(gc.is_tracked(t), t) |
| 93 | |
| 94 | def _tracked(self, t): |
| 95 | self.assertTrue(gc.is_tracked(t), t) |
| 96 | gc.collect() |
| 97 | gc.collect() |
| 98 | self.assertTrue(gc.is_tracked(t), t) |
| 99 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 100 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 101 | def test_track_literals(self): |
| 102 | # Test GC-optimization of tuple literals |
| 103 | x, y, z = 1.5, "a", [] |
| 104 | |
| 105 | self._not_tracked(()) |
| 106 | self._not_tracked((1,)) |
| 107 | self._not_tracked((1, 2)) |
| 108 | self._not_tracked((1, 2, "a")) |
| 109 | self._not_tracked((1, 2, (None, True, False, ()), int)) |
| 110 | self._not_tracked((object(),)) |
| 111 | self._not_tracked(((1, x), y, (2, 3))) |
| 112 | |
| 113 | # Tuples with mutable elements are always tracked, even if those |
| 114 | # elements are not tracked right now. |
| 115 | self._tracked(([],)) |
| 116 | self._tracked(([1],)) |
| 117 | self._tracked(({},)) |
| 118 | self._tracked((set(),)) |
| 119 | self._tracked((x, y, z)) |
| 120 | |
| 121 | def check_track_dynamic(self, tp, always_track): |
| 122 | x, y, z = 1.5, "a", [] |
| 123 | |
| 124 | check = self._tracked if always_track else self._not_tracked |
| 125 | check(tp()) |
| 126 | check(tp([])) |
| 127 | check(tp(set())) |
| 128 | check(tp([1, x, y])) |
| 129 | check(tp(obj for obj in [1, x, y])) |
| 130 | check(tp(set([1, x, y]))) |
| 131 | check(tp(tuple([obj]) for obj in [1, x, y])) |
| 132 | check(tuple(tp([obj]) for obj in [1, x, y])) |
| 133 | |
| 134 | self._tracked(tp([z])) |
| 135 | self._tracked(tp([[x, y]])) |
| 136 | self._tracked(tp([{x: y}])) |
| 137 | self._tracked(tp(obj for obj in [x, y, z])) |
| 138 | self._tracked(tp(tuple([obj]) for obj in [x, y, z])) |
| 139 | self._tracked(tuple(tp([obj]) for obj in [x, y, z])) |
| 140 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 141 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 142 | def test_track_dynamic(self): |
| 143 | # Test GC-optimization of dynamically constructed tuples. |
| 144 | self.check_track_dynamic(tuple, False) |
| 145 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 146 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 147 | def test_track_subtypes(self): |
| 148 | # Tuple subtypes must always be tracked |
| 149 | class MyTuple(tuple): |
| 150 | pass |
| 151 | self.check_track_dynamic(MyTuple, True) |
| 152 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 153 | @support.cpython_only |
Antoine Pitrou | 6b7dfc9 | 2009-12-12 19:18:27 +0000 | [diff] [blame] | 154 | def test_bug7466(self): |
| 155 | # Trying to untrack an unfinished tuple could crash Python |
| 156 | self._not_tracked(tuple(gc.collect() for i in range(101))) |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 158 | def test_repr_large(self): |
| 159 | # Check the repr of large list objects |
| 160 | def check(n): |
| 161 | l = (0,) * n |
| 162 | s = repr(l) |
| 163 | self.assertEqual(s, |
| 164 | '(' + ', '.join(['0'] * n) + ')') |
| 165 | check(10) # check our checking code |
| 166 | check(1000000) |
| 167 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 168 | def test_iterator_pickle(self): |
| 169 | # Userlist iterators don't support pickling yet since |
| 170 | # they are based on generators. |
| 171 | data = self.type2test([4, 5, 6, 7]) |
| 172 | itorg = iter(data) |
| 173 | d = pickle.dumps(itorg) |
| 174 | it = pickle.loads(d) |
| 175 | self.assertEqual(type(itorg), type(it)) |
| 176 | self.assertEqual(self.type2test(it), self.type2test(data)) |
| 177 | |
| 178 | it = pickle.loads(d) |
| 179 | next(it) |
| 180 | d = pickle.dumps(it) |
| 181 | self.assertEqual(self.type2test(it), self.type2test(data)[1:]) |
| 182 | |
| 183 | def test_reversed_pickle(self): |
| 184 | data = self.type2test([4, 5, 6, 7]) |
| 185 | itorg = reversed(data) |
| 186 | d = pickle.dumps(itorg) |
| 187 | it = pickle.loads(d) |
| 188 | self.assertEqual(type(itorg), type(it)) |
| 189 | self.assertEqual(self.type2test(it), self.type2test(reversed(data))) |
| 190 | |
| 191 | it = pickle.loads(d) |
| 192 | next(it) |
| 193 | d = pickle.dumps(it) |
| 194 | self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:]) |
| 195 | |
Martin v. Löwis | 4c1730d | 2012-08-01 10:32:11 +0200 | [diff] [blame] | 196 | def test_no_comdat_folding(self): |
| 197 | # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding |
| 198 | # optimization causes failures in code that relies on distinct |
| 199 | # function addresses. |
| 200 | class T(tuple): pass |
| 201 | with self.assertRaises(TypeError): |
| 202 | [3,] + T((1,2)) |
| 203 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 204 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 205 | support.run_unittest(TupleTest) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 206 | |
| 207 | if __name__=="__main__": |
| 208 | test_main() |