blob: 15bc2956cf0c9da6c7db419fa0c9ab753b87b301 [file] [log] [blame]
Walter Dörwald1dde95d2003-12-08 11:38:45 +00001from test import test_support, seq_tests
2
3class TupleTest(seq_tests.CommonTest):
4 type2test = tuple
5
6 def test_constructors(self):
7 super(TupleTest, self).test_len()
8 # calling built-in types without argument must return empty
9 self.assertEqual(tuple(), ())
Benjamin Peterson979395b2008-05-03 21:35:18 +000010 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örwald1dde95d2003-12-08 11:38:45 +000017
18 def test_truth(self):
19 super(TupleTest, self).test_truth()
20 self.assert_(not ())
21 self.assert_((42, ))
22
23 def test_len(self):
24 super(TupleTest, self).test_len()
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):
30 super(TupleTest, self).test_iadd()
31 u = (0, 1)
32 u2 = u
33 u += (2, 3)
34 self.assert_(u is not u2)
35
36 def test_imul(self):
37 super(TupleTest, self).test_imul()
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
48 self.assertEqual(list(tuple(f())), range(1000))
49
Raymond Hettinger41bd0222004-06-01 06:36:24 +000050 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 Peters1f4bcf92004-06-01 18:58:04 +000058 # 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 Hettinger41bd0222004-06-01 06:36:24 +000065
66 N=50
67 base = range(N)
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] + \
70 [(i, j) for i in xp for j in base] + xp + zip(base)
71 collisions = len(inps) - len(set(map(hash, inps)))
72 self.assert_(collisions <= 15)
Walter Dörwald1dde95d2003-12-08 11:38:45 +000073
Raymond Hettinger5ea7e312004-09-30 07:47:20 +000074 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örwald1dde95d2003-12-08 11:38:45 +000085def test_main():
86 test_support.run_unittest(TupleTest)
87
88if __name__=="__main__":
89 test_main()