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 |
| 4 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 5 | class TupleTest(seq_tests.CommonTest): |
| 6 | type2test = tuple |
| 7 | |
| 8 | def test_constructors(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9 | super().test_len() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 10 | # calling built-in types without argument must return empty |
| 11 | self.assertEqual(tuple(), ()) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 12 | t0_3 = (0, 1, 2, 3) |
| 13 | t0_3_bis = tuple(t0_3) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 14 | self.assertTrue(t0_3 is t0_3_bis) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 15 | self.assertEqual(tuple([]), ()) |
| 16 | self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
| 17 | self.assertEqual(tuple(''), ()) |
| 18 | self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 19 | |
| 20 | def test_truth(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 21 | super().test_truth() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 22 | self.assertTrue(not ()) |
| 23 | self.assertTrue((42, )) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 24 | |
| 25 | def test_len(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 26 | super().test_len() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 27 | self.assertEqual(len(()), 0) |
| 28 | self.assertEqual(len((0,)), 1) |
| 29 | self.assertEqual(len((0, 1, 2)), 3) |
| 30 | |
| 31 | def test_iadd(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 32 | super().test_iadd() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 33 | u = (0, 1) |
| 34 | u2 = u |
| 35 | u += (2, 3) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 36 | self.assertTrue(u is not u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 37 | |
| 38 | def test_imul(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 39 | super().test_imul() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 40 | u = (0, 1) |
| 41 | u2 = u |
| 42 | u *= 3 |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 43 | self.assertTrue(u is not u2) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 44 | |
| 45 | def test_tupleresizebug(self): |
| 46 | # Check that a specific bug in _PyTuple_Resize() is squashed. |
| 47 | def f(): |
| 48 | for i in range(1000): |
| 49 | yield i |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 50 | self.assertEqual(list(tuple(f())), list(range(1000))) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 51 | |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 52 | def test_hash(self): |
| 53 | # See SF bug 942952: Weakness in tuple hash |
| 54 | # The hash should: |
| 55 | # be non-commutative |
| 56 | # should spread-out closely spaced values |
| 57 | # should not exhibit cancellation in tuples like (x,(x,y)) |
| 58 | # should be distinct from element hashes: hash(x)!=hash((x,)) |
| 59 | # This test exercises those cases. |
Tim Peters | 1f4bcf9 | 2004-06-01 18:58:04 +0000 | [diff] [blame] | 60 | # For a pure random hash and N=50, the expected number of occupied |
| 61 | # buckets when tossing 252,600 balls into 2**32 buckets |
| 62 | # is 252,592.6, or about 7.4 expected collisions. The |
| 63 | # standard deviation is 2.73. On a box with 64-bit hash |
| 64 | # codes, no collisions are expected. Here we accept no |
| 65 | # more than 15 collisions. Any worse and the hash function |
| 66 | # is sorely suspect. |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 67 | |
| 68 | N=50 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 69 | base = list(range(N)) |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 70 | xp = [(i, j) for i in base for j in base] |
| 71 | 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] | 72 | [(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] | 73 | collisions = len(inps) - len(set(map(hash, inps))) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 74 | self.assertTrue(collisions <= 15) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 75 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 76 | def test_repr(self): |
| 77 | l0 = tuple() |
| 78 | l2 = (0, 1, 2) |
| 79 | a0 = self.type2test(l0) |
| 80 | a2 = self.type2test(l2) |
| 81 | |
| 82 | self.assertEqual(str(a0), repr(l0)) |
| 83 | self.assertEqual(str(a2), repr(l2)) |
| 84 | self.assertEqual(repr(a0), "()") |
| 85 | self.assertEqual(repr(a2), "(0, 1, 2)") |
| 86 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 87 | def _not_tracked(self, t): |
| 88 | # Nested tuples can take several collections to untrack |
| 89 | gc.collect() |
| 90 | gc.collect() |
| 91 | self.assertFalse(gc.is_tracked(t), t) |
| 92 | |
| 93 | def _tracked(self, t): |
| 94 | self.assertTrue(gc.is_tracked(t), t) |
| 95 | gc.collect() |
| 96 | gc.collect() |
| 97 | self.assertTrue(gc.is_tracked(t), t) |
| 98 | |
| 99 | def test_track_literals(self): |
| 100 | # Test GC-optimization of tuple literals |
| 101 | x, y, z = 1.5, "a", [] |
| 102 | |
| 103 | self._not_tracked(()) |
| 104 | self._not_tracked((1,)) |
| 105 | self._not_tracked((1, 2)) |
| 106 | self._not_tracked((1, 2, "a")) |
| 107 | self._not_tracked((1, 2, (None, True, False, ()), int)) |
| 108 | self._not_tracked((object(),)) |
| 109 | self._not_tracked(((1, x), y, (2, 3))) |
| 110 | |
| 111 | # Tuples with mutable elements are always tracked, even if those |
| 112 | # elements are not tracked right now. |
| 113 | self._tracked(([],)) |
| 114 | self._tracked(([1],)) |
| 115 | self._tracked(({},)) |
| 116 | self._tracked((set(),)) |
| 117 | self._tracked((x, y, z)) |
| 118 | |
| 119 | def check_track_dynamic(self, tp, always_track): |
| 120 | x, y, z = 1.5, "a", [] |
| 121 | |
| 122 | check = self._tracked if always_track else self._not_tracked |
| 123 | check(tp()) |
| 124 | check(tp([])) |
| 125 | check(tp(set())) |
| 126 | check(tp([1, x, y])) |
| 127 | check(tp(obj for obj in [1, x, y])) |
| 128 | check(tp(set([1, x, y]))) |
| 129 | check(tp(tuple([obj]) for obj in [1, x, y])) |
| 130 | check(tuple(tp([obj]) for obj in [1, x, y])) |
| 131 | |
| 132 | self._tracked(tp([z])) |
| 133 | self._tracked(tp([[x, y]])) |
| 134 | self._tracked(tp([{x: y}])) |
| 135 | self._tracked(tp(obj for obj in [x, y, z])) |
| 136 | self._tracked(tp(tuple([obj]) for obj in [x, y, z])) |
| 137 | self._tracked(tuple(tp([obj]) for obj in [x, y, z])) |
| 138 | |
| 139 | def test_track_dynamic(self): |
| 140 | # Test GC-optimization of dynamically constructed tuples. |
| 141 | self.check_track_dynamic(tuple, False) |
| 142 | |
| 143 | def test_track_subtypes(self): |
| 144 | # Tuple subtypes must always be tracked |
| 145 | class MyTuple(tuple): |
| 146 | pass |
| 147 | self.check_track_dynamic(MyTuple, True) |
| 148 | |
Antoine Pitrou | 2795e6c | 2009-12-12 19:26:06 +0000 | [diff] [blame] | 149 | def test_bug7466(self): |
| 150 | # Trying to untrack an unfinished tuple could crash Python |
| 151 | self._not_tracked(tuple(gc.collect() for i in range(101))) |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 152 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 153 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 154 | support.run_unittest(TupleTest) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 155 | |
| 156 | if __name__=="__main__": |
| 157 | test_main() |