|
1 from test import test_support, seq_tests |
|
2 |
|
3 class 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(), ()) |
|
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')) |
|
17 |
|
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 |
|
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. |
|
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. |
|
65 |
|
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) |
|
73 |
|
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 |
|
85 def test_main(): |
|
86 test_support.run_unittest(TupleTest) |
|
87 |
|
88 if __name__=="__main__": |
|
89 test_main() |