1 from test
import test_support
, seq_tests
5 class TupleTest(seq_tests
.CommonTest
):
8 def test_constructors(self
):
9 super(TupleTest
, self
).test_len()
10 # calling built-in types without argument must return empty
11 self
.assertEqual(tuple(), ())
13 t0_3_bis
= tuple(t0_3
)
14 self
.assertTrue(t0_3
is t0_3_bis
)
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'))
21 super(TupleTest
, self
).test_truth()
22 self
.assertTrue(not ())
23 self
.assertTrue((42, ))
26 super(TupleTest
, self
).test_len()
27 self
.assertEqual(len(()), 0)
28 self
.assertEqual(len((0,)), 1)
29 self
.assertEqual(len((0, 1, 2)), 3)
32 super(TupleTest
, self
).test_iadd()
36 self
.assertTrue(u
is not u2
)
39 super(TupleTest
, self
).test_imul()
43 self
.assertTrue(u
is not u2
)
45 def test_tupleresizebug(self
):
46 # Check that a specific bug in _PyTuple_Resize() is squashed.
50 self
.assertEqual(list(tuple(f())), range(1000))
53 # See SF bug 942952: Weakness in tuple hash
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.
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
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
] + \
72 [(i
, j
) for i
in xp
for j
in base
] + xp
+ zip(base
)
73 collisions
= len(inps
) - len(set(map(hash, inps
)))
74 self
.assertTrue(collisions
<= 15)
79 a0
= self
.type2test(l0
)
80 a2
= self
.type2test(l2
)
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)")
87 def _not_tracked(self
, t
):
88 # Nested tuples can take several collections to untrack
91 self
.assertFalse(gc
.is_tracked(t
), t
)
93 def _tracked(self
, t
):
94 self
.assertTrue(gc
.is_tracked(t
), t
)
97 self
.assertTrue(gc
.is_tracked(t
), t
)
99 def test_track_literals(self
):
100 # Test GC-optimization of tuple literals
101 x
, y
, z
= 1.5, "a", []
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)))
111 # Tuples with mutable elements are always tracked, even if those
112 # elements are not tracked right now.
114 self
._tracked
(([1],))
116 self
._tracked
((set(),))
117 self
._tracked
((x
, y
, z
))
119 def check_track_dynamic(self
, tp
, always_track
):
120 x
, y
, z
= 1.5, "a", []
122 check
= self
._tracked
if always_track
else self
._not
_tracked
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
]))
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
]))
139 def test_track_dynamic(self
):
140 # Test GC-optimization of dynamically constructed tuples.
141 self
.check_track_dynamic(tuple, False)
143 def test_track_subtypes(self
):
144 # Tuple subtypes must always be tracked
145 class MyTuple(tuple):
147 self
.check_track_dynamic(MyTuple
, True)
151 test_support
.run_unittest(TupleTest
)
153 if __name__
=="__main__":