Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support, seq_tests |
Zachary Ware | ac28b79 | 2015-12-04 23:32:23 -0600 | [diff] [blame] | 2 | import unittest |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 3 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 4 | import gc |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 5 | import pickle |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 6 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 7 | class TupleTest(seq_tests.CommonTest): |
| 8 | type2test = tuple |
| 9 | |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 10 | def test_getitem_error(self): |
| 11 | msg = "tuple indices must be integers or slices" |
| 12 | with self.assertRaisesRegex(TypeError, msg): |
| 13 | ()['a'] |
| 14 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 15 | def test_constructors(self): |
Éric Araujo | a63c240 | 2010-12-23 19:13:05 +0000 | [diff] [blame] | 16 | super().test_constructors() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 17 | # calling built-in types without argument must return empty |
| 18 | self.assertEqual(tuple(), ()) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 19 | t0_3 = (0, 1, 2, 3) |
| 20 | t0_3_bis = tuple(t0_3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 21 | self.assertTrue(t0_3 is t0_3_bis) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 22 | self.assertEqual(tuple([]), ()) |
| 23 | self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
| 24 | self.assertEqual(tuple(''), ()) |
| 25 | self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
Serhiy Storchaka | 58d23e6 | 2017-03-06 00:53:39 +0200 | [diff] [blame] | 26 | self.assertEqual(tuple(x for x in range(10) if x % 2), |
| 27 | (1, 3, 5, 7, 9)) |
| 28 | |
| 29 | def test_keyword_args(self): |
Serhiy Storchaka | 2e56424 | 2017-03-06 17:01:06 +0200 | [diff] [blame] | 30 | with self.assertRaisesRegex(TypeError, 'keyword argument'): |
| 31 | tuple(sequence=()) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 32 | |
| 33 | def test_truth(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 34 | super().test_truth() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertTrue(not ()) |
| 36 | self.assertTrue((42, )) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 37 | |
| 38 | def test_len(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 39 | super().test_len() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 40 | self.assertEqual(len(()), 0) |
| 41 | self.assertEqual(len((0,)), 1) |
| 42 | self.assertEqual(len((0, 1, 2)), 3) |
| 43 | |
| 44 | def test_iadd(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 45 | super().test_iadd() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 46 | u = (0, 1) |
| 47 | u2 = u |
| 48 | u += (2, 3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 49 | self.assertTrue(u is not u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 50 | |
| 51 | def test_imul(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 52 | super().test_imul() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 53 | u = (0, 1) |
| 54 | u2 = u |
| 55 | u *= 3 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 56 | self.assertTrue(u is not u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 57 | |
| 58 | def test_tupleresizebug(self): |
| 59 | # Check that a specific bug in _PyTuple_Resize() is squashed. |
| 60 | def f(): |
| 61 | for i in range(1000): |
| 62 | yield i |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 63 | self.assertEqual(list(tuple(f())), list(range(1000))) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 64 | |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 65 | # Various tests for hashing of tuples to check that we get few collisions. |
| 66 | # |
| 67 | # Earlier versions of the tuple hash algorithm had collisions |
| 68 | # reported at: |
| 69 | # - https://bugs.python.org/issue942952 |
| 70 | # - https://bugs.python.org/issue34751 |
| 71 | # |
| 72 | # Notes: |
| 73 | # - The hash of tuples is deterministic: if the test passes once on a given |
| 74 | # system, it will always pass. So the probabilities mentioned in the |
| 75 | # test_hash functions below should be interpreted assuming that the |
| 76 | # hashes are random. |
| 77 | # - Due to the structure in the testsuite inputs, collisions are not |
| 78 | # independent. For example, if hash((a,b)) == hash((c,d)), then also |
| 79 | # hash((a,b,x)) == hash((c,d,x)). But the quoted probabilities assume |
| 80 | # independence anyway. |
| 81 | # - We limit the hash to 32 bits in the tests to have a good test on |
| 82 | # 64-bit systems too. Furthermore, this is also a sanity check that the |
| 83 | # lower 32 bits of a 64-bit hash are sufficiently random too. |
| 84 | def test_hash1(self): |
| 85 | # Check for hash collisions between small integers in range(50) and |
| 86 | # certain tuples and nested tuples of such integers. |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 87 | N=50 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 88 | base = list(range(N)) |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 89 | xp = [(i, j) for i in base for j in base] |
| 90 | 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] | 91 | [(i, j) for i in xp for j in base] + xp + list(zip(base)) |
jdemeyer | aeb1be5 | 2018-10-28 02:06:38 +0200 | [diff] [blame] | 92 | self.assertEqual(len(inps), 252600) |
| 93 | hashes = set(hash(x) % 2**32 for x in inps) |
| 94 | collisions = len(inps) - len(hashes) |
| 95 | |
| 96 | # For a pure random 32-bit hash and N = 252,600 test items, the |
| 97 | # expected number of collisions equals |
| 98 | # |
| 99 | # 2**(-32) * N(N-1)/2 = 7.4 |
| 100 | # |
| 101 | # We allow up to 15 collisions, which suffices to make the test |
| 102 | # pass with 99.5% confidence. |
| 103 | self.assertLessEqual(collisions, 15) |
| 104 | |
| 105 | def test_hash2(self): |
| 106 | # Check for hash collisions between small integers (positive and |
| 107 | # negative), tuples and nested tuples of such integers. |
| 108 | |
| 109 | # All numbers in the interval [-n, ..., n] except -1 because |
| 110 | # hash(-1) == hash(-2). |
| 111 | n = 5 |
| 112 | A = [x for x in range(-n, n+1) if x != -1] |
| 113 | |
| 114 | B = A + [(a,) for a in A] |
| 115 | |
| 116 | L2 = [(a,b) for a in A for b in A] |
| 117 | L3 = L2 + [(a,b,c) for a in A for b in A for c in A] |
| 118 | L4 = L3 + [(a,b,c,d) for a in A for b in A for c in A for d in A] |
| 119 | |
| 120 | # T = list of testcases. These consist of all (possibly nested |
| 121 | # at most 2 levels deep) tuples containing at most 4 items from |
| 122 | # the set A. |
| 123 | T = A |
| 124 | T += [(a,) for a in B + L4] |
| 125 | T += [(a,b) for a in L3 for b in B] |
| 126 | T += [(a,b) for a in L2 for b in L2] |
| 127 | T += [(a,b) for a in B for b in L3] |
| 128 | T += [(a,b,c) for a in B for b in B for c in L2] |
| 129 | T += [(a,b,c) for a in B for b in L2 for c in B] |
| 130 | T += [(a,b,c) for a in L2 for b in B for c in B] |
| 131 | T += [(a,b,c,d) for a in B for b in B for c in B for d in B] |
| 132 | self.assertEqual(len(T), 345130) |
| 133 | hashes = set(hash(x) % 2**32 for x in T) |
| 134 | collisions = len(T) - len(hashes) |
| 135 | |
| 136 | # For a pure random 32-bit hash and N = 345,130 test items, the |
| 137 | # expected number of collisions equals |
| 138 | # |
| 139 | # 2**(-32) * N(N-1)/2 = 13.9 |
| 140 | # |
| 141 | # We allow up to 20 collisions, which suffices to make the test |
| 142 | # pass with 95.5% confidence. |
| 143 | self.assertLessEqual(collisions, 20) |
| 144 | |
| 145 | def test_hash3(self): |
| 146 | # Check for hash collisions between tuples containing 0.0 and 0.5. |
| 147 | # The hashes of 0.0 and 0.5 itself differ only in one high bit. |
| 148 | # So this implicitly tests propagation of high bits to low bits. |
| 149 | from itertools import product |
| 150 | T = list(product([0.0, 0.5], repeat=18)) |
| 151 | self.assertEqual(len(T), 262144) |
| 152 | hashes = set(hash(x) % 2**32 for x in T) |
| 153 | collisions = len(T) - len(hashes) |
| 154 | |
| 155 | # For a pure random 32-bit hash and N = 262,144 test items, the |
| 156 | # expected number of collisions equals |
| 157 | # |
| 158 | # 2**(-32) * N(N-1)/2 = 8.0 |
| 159 | # |
| 160 | # We allow up to 15 collisions, which suffices to make the test |
| 161 | # pass with 99.1% confidence. |
| 162 | self.assertLessEqual(collisions, 15) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 163 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 164 | def test_repr(self): |
| 165 | l0 = tuple() |
| 166 | l2 = (0, 1, 2) |
| 167 | a0 = self.type2test(l0) |
| 168 | a2 = self.type2test(l2) |
| 169 | |
| 170 | self.assertEqual(str(a0), repr(l0)) |
| 171 | self.assertEqual(str(a2), repr(l2)) |
| 172 | self.assertEqual(repr(a0), "()") |
| 173 | self.assertEqual(repr(a2), "(0, 1, 2)") |
| 174 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 175 | def _not_tracked(self, t): |
| 176 | # Nested tuples can take several collections to untrack |
| 177 | gc.collect() |
| 178 | gc.collect() |
| 179 | self.assertFalse(gc.is_tracked(t), t) |
| 180 | |
| 181 | def _tracked(self, t): |
| 182 | self.assertTrue(gc.is_tracked(t), t) |
| 183 | gc.collect() |
| 184 | gc.collect() |
| 185 | self.assertTrue(gc.is_tracked(t), t) |
| 186 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 187 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 188 | def test_track_literals(self): |
| 189 | # Test GC-optimization of tuple literals |
| 190 | x, y, z = 1.5, "a", [] |
| 191 | |
| 192 | self._not_tracked(()) |
| 193 | self._not_tracked((1,)) |
| 194 | self._not_tracked((1, 2)) |
| 195 | self._not_tracked((1, 2, "a")) |
| 196 | self._not_tracked((1, 2, (None, True, False, ()), int)) |
| 197 | self._not_tracked((object(),)) |
| 198 | self._not_tracked(((1, x), y, (2, 3))) |
| 199 | |
| 200 | # Tuples with mutable elements are always tracked, even if those |
| 201 | # elements are not tracked right now. |
| 202 | self._tracked(([],)) |
| 203 | self._tracked(([1],)) |
| 204 | self._tracked(({},)) |
| 205 | self._tracked((set(),)) |
| 206 | self._tracked((x, y, z)) |
| 207 | |
| 208 | def check_track_dynamic(self, tp, always_track): |
| 209 | x, y, z = 1.5, "a", [] |
| 210 | |
| 211 | check = self._tracked if always_track else self._not_tracked |
| 212 | check(tp()) |
| 213 | check(tp([])) |
| 214 | check(tp(set())) |
| 215 | check(tp([1, x, y])) |
| 216 | check(tp(obj for obj in [1, x, y])) |
| 217 | check(tp(set([1, x, y]))) |
| 218 | check(tp(tuple([obj]) for obj in [1, x, y])) |
| 219 | check(tuple(tp([obj]) for obj in [1, x, y])) |
| 220 | |
| 221 | self._tracked(tp([z])) |
| 222 | self._tracked(tp([[x, y]])) |
| 223 | self._tracked(tp([{x: y}])) |
| 224 | self._tracked(tp(obj for obj in [x, y, z])) |
| 225 | self._tracked(tp(tuple([obj]) for obj in [x, y, z])) |
| 226 | self._tracked(tuple(tp([obj]) for obj in [x, y, z])) |
| 227 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 228 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 229 | def test_track_dynamic(self): |
| 230 | # Test GC-optimization of dynamically constructed tuples. |
| 231 | self.check_track_dynamic(tuple, False) |
| 232 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 233 | @support.cpython_only |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 234 | def test_track_subtypes(self): |
| 235 | # Tuple subtypes must always be tracked |
| 236 | class MyTuple(tuple): |
| 237 | pass |
| 238 | self.check_track_dynamic(MyTuple, True) |
| 239 | |
Benjamin Peterson | 02d7806 | 2010-06-27 22:44:51 +0000 | [diff] [blame] | 240 | @support.cpython_only |
Antoine Pitrou | 6b7dfc9 | 2009-12-12 19:18:27 +0000 | [diff] [blame] | 241 | def test_bug7466(self): |
| 242 | # Trying to untrack an unfinished tuple could crash Python |
| 243 | self._not_tracked(tuple(gc.collect() for i in range(101))) |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | eeb7eea | 2011-10-06 18:57:27 +0200 | [diff] [blame] | 245 | def test_repr_large(self): |
| 246 | # Check the repr of large list objects |
| 247 | def check(n): |
| 248 | l = (0,) * n |
| 249 | s = repr(l) |
| 250 | self.assertEqual(s, |
| 251 | '(' + ', '.join(['0'] * n) + ')') |
| 252 | check(10) # check our checking code |
| 253 | check(1000000) |
| 254 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 255 | def test_iterator_pickle(self): |
| 256 | # Userlist iterators don't support pickling yet since |
| 257 | # they are based on generators. |
| 258 | data = self.type2test([4, 5, 6, 7]) |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 259 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 260 | itorg = iter(data) |
| 261 | d = pickle.dumps(itorg, proto) |
| 262 | it = pickle.loads(d) |
| 263 | self.assertEqual(type(itorg), type(it)) |
| 264 | self.assertEqual(self.type2test(it), self.type2test(data)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 265 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 266 | it = pickle.loads(d) |
| 267 | next(it) |
| 268 | d = pickle.dumps(it, proto) |
| 269 | self.assertEqual(self.type2test(it), self.type2test(data)[1:]) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 270 | |
| 271 | def test_reversed_pickle(self): |
| 272 | data = self.type2test([4, 5, 6, 7]) |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 273 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 274 | itorg = reversed(data) |
| 275 | d = pickle.dumps(itorg, proto) |
| 276 | it = pickle.loads(d) |
| 277 | self.assertEqual(type(itorg), type(it)) |
| 278 | self.assertEqual(self.type2test(it), self.type2test(reversed(data))) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 279 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 280 | it = pickle.loads(d) |
| 281 | next(it) |
| 282 | d = pickle.dumps(it, proto) |
| 283 | self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:]) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 284 | |
Martin v. Löwis | 4c1730d | 2012-08-01 10:32:11 +0200 | [diff] [blame] | 285 | def test_no_comdat_folding(self): |
| 286 | # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding |
| 287 | # optimization causes failures in code that relies on distinct |
| 288 | # function addresses. |
| 289 | class T(tuple): pass |
| 290 | with self.assertRaises(TypeError): |
| 291 | [3,] + T((1,2)) |
| 292 | |
Raymond Hettinger | e5bb551 | 2014-03-30 10:12:09 -0700 | [diff] [blame] | 293 | def test_lexicographic_ordering(self): |
| 294 | # Issue 21100 |
| 295 | a = self.type2test([1, 2]) |
| 296 | b = self.type2test([1, 2, 0]) |
| 297 | c = self.type2test([1, 3]) |
| 298 | self.assertLess(a, b) |
| 299 | self.assertLess(b, c) |
| 300 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 301 | if __name__ == "__main__": |
| 302 | unittest.main() |