Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_tuple.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 83cc09682081 |
| children | 0e498a9fde3c |
| rev | line source |
|---|---|
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
1 from test import test_support, seq_tests |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
2 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
3 class TupleTest(seq_tests.CommonTest): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
4 type2test = tuple |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
5 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
6 def test_constructors(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
7 super(TupleTest, self).test_len() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
8 # calling built-in types without argument must return empty |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
9 self.assertEqual(tuple(), ()) |
|
46649
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
10 t0_3 = (0, 1, 2, 3) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
11 t0_3_bis = tuple(t0_3) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
12 self.assert_(t0_3 is t0_3_bis) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
13 self.assertEqual(tuple([]), ()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
14 self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
15 self.assertEqual(tuple(''), ()) |
|
83cc09682081
Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
16 self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
17 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
18 def test_truth(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
19 super(TupleTest, self).test_truth() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
20 self.assert_(not ()) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
21 self.assert_((42, )) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
22 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
23 def test_len(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
24 super(TupleTest, self).test_len() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
25 self.assertEqual(len(()), 0) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
26 self.assertEqual(len((0,)), 1) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
27 self.assertEqual(len((0, 1, 2)), 3) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
28 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
29 def test_iadd(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
30 super(TupleTest, self).test_iadd() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
31 u = (0, 1) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
32 u2 = u |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
33 u += (2, 3) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
34 self.assert_(u is not u2) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
35 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
36 def test_imul(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
37 super(TupleTest, self).test_imul() |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
38 u = (0, 1) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
39 u2 = u |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
40 u *= 3 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
41 self.assert_(u is not u2) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
42 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
43 def test_tupleresizebug(self): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
44 # Check that a specific bug in _PyTuple_Resize() is squashed. |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
45 def f(): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
46 for i in range(1000): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
47 yield i |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
48 self.assertEqual(list(tuple(f())), range(1000)) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
49 |
|
32171
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
50 def test_hash(self): |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
51 # See SF bug 942952: Weakness in tuple hash |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
52 # The hash should: |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
53 # be non-commutative |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
54 # should spread-out closely spaced values |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
55 # should not exhibit cancellation in tuples like (x,(x,y)) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
56 # should be distinct from element hashes: hash(x)!=hash((x,)) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
57 # This test exercises those cases. |
|
32178
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
58 # For a pure random hash and N=50, the expected number of occupied |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
59 # buckets when tossing 252,600 balls into 2**32 buckets |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
60 # is 252,592.6, or about 7.4 expected collisions. The |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
61 # standard deviation is 2.73. On a box with 64-bit hash |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
62 # codes, no collisions are expected. Here we accept no |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
63 # more than 15 collisions. Any worse and the hash function |
|
041f89c98c35
test_hash(): The test here is different enough from the one in the bug
Tim Peters <tim.peters@gmail.com>
parents:
32171
diff
changeset
|
64 # is sorely suspect. |
|
32171
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
65 |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
66 N=50 |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
67 base = range(N) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
68 xp = [(i, j) for i in base for j in base] |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
69 inps = base + [(i, j) for i in base for j in xp] + \ |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
70 [(i, j) for i in xp for j in base] + xp + zip(base) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
71 collisions = len(inps) - len(set(map(hash, inps))) |
|
97b908a9971a
SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents:
31159
diff
changeset
|
72 self.assert_(collisions <= 15) |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
73 |
|
33629
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
74 def test_repr(self): |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
75 l0 = tuple() |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
76 l2 = (0, 1, 2) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
77 a0 = self.type2test(l0) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
78 a2 = self.type2test(l2) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
79 |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
80 self.assertEqual(str(a0), repr(l0)) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
81 self.assertEqual(str(a2), repr(l2)) |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
82 self.assertEqual(repr(a0), "()") |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
83 self.assertEqual(repr(a2), "(0, 1, 2)") |
|
c84dbe717d49
Improve test coverage.
Raymond Hettinger <python@rcn.com>
parents:
32178
diff
changeset
|
84 |
|
31159
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
85 def test_main(): |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
86 test_support.run_unittest(TupleTest) |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
87 |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
88 if __name__=="__main__": |
|
e1f1fb74451b
Move list and tuple tests from test_types.py to their own scripts:
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
89 test_main() |
