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 | |
| 3 | class TupleTest(seq_tests.CommonTest): |
| 4 | type2test = tuple |
| 5 | |
| 6 | def test_constructors(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7 | super().test_len() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 8 | # calling built-in types without argument must return empty |
| 9 | self.assertEqual(tuple(), ()) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 10 | t0_3 = (0, 1, 2, 3) |
| 11 | t0_3_bis = tuple(t0_3) |
| 12 | self.assert_(t0_3 is t0_3_bis) |
| 13 | self.assertEqual(tuple([]), ()) |
| 14 | self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
| 15 | self.assertEqual(tuple(''), ()) |
| 16 | self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 17 | |
| 18 | def test_truth(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 19 | super().test_truth() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 20 | self.assert_(not ()) |
| 21 | self.assert_((42, )) |
| 22 | |
| 23 | def test_len(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 24 | super().test_len() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 25 | self.assertEqual(len(()), 0) |
| 26 | self.assertEqual(len((0,)), 1) |
| 27 | self.assertEqual(len((0, 1, 2)), 3) |
| 28 | |
| 29 | def test_iadd(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 30 | super().test_iadd() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 31 | u = (0, 1) |
| 32 | u2 = u |
| 33 | u += (2, 3) |
| 34 | self.assert_(u is not u2) |
| 35 | |
| 36 | def test_imul(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 37 | super().test_imul() |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 38 | u = (0, 1) |
| 39 | u2 = u |
| 40 | u *= 3 |
| 41 | self.assert_(u is not u2) |
| 42 | |
| 43 | def test_tupleresizebug(self): |
| 44 | # Check that a specific bug in _PyTuple_Resize() is squashed. |
| 45 | def f(): |
| 46 | for i in range(1000): |
| 47 | yield i |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 48 | self.assertEqual(list(tuple(f())), list(range(1000))) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 49 | |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 50 | def test_hash(self): |
| 51 | # See SF bug 942952: Weakness in tuple hash |
| 52 | # The hash should: |
| 53 | # be non-commutative |
| 54 | # should spread-out closely spaced values |
| 55 | # should not exhibit cancellation in tuples like (x,(x,y)) |
| 56 | # should be distinct from element hashes: hash(x)!=hash((x,)) |
| 57 | # This test exercises those cases. |
Tim Peters | 1f4bcf9 | 2004-06-01 18:58:04 +0000 | [diff] [blame] | 58 | # For a pure random hash and N=50, the expected number of occupied |
| 59 | # buckets when tossing 252,600 balls into 2**32 buckets |
| 60 | # is 252,592.6, or about 7.4 expected collisions. The |
| 61 | # standard deviation is 2.73. On a box with 64-bit hash |
| 62 | # codes, no collisions are expected. Here we accept no |
| 63 | # more than 15 collisions. Any worse and the hash function |
| 64 | # is sorely suspect. |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 65 | |
| 66 | N=50 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 67 | base = list(range(N)) |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 68 | xp = [(i, j) for i in base for j in base] |
| 69 | 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] | 70 | [(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] | 71 | collisions = len(inps) - len(set(map(hash, inps))) |
| 72 | self.assert_(collisions <= 15) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 73 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 74 | def test_repr(self): |
| 75 | l0 = tuple() |
| 76 | l2 = (0, 1, 2) |
| 77 | a0 = self.type2test(l0) |
| 78 | a2 = self.type2test(l2) |
| 79 | |
| 80 | self.assertEqual(str(a0), repr(l0)) |
| 81 | self.assertEqual(str(a2), repr(l2)) |
| 82 | self.assertEqual(repr(a0), "()") |
| 83 | self.assertEqual(repr(a2), "(0, 1, 2)") |
| 84 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 85 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 86 | support.run_unittest(TupleTest) |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 87 | |
| 88 | if __name__=="__main__": |
| 89 | test_main() |