Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes | Private Member Functions
test.test_tuple.TupleTest Class Reference
Inheritance diagram for test.test_tuple.TupleTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_tuple.TupleTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_constructors
def test_truth
def test_len
def test_iadd
def test_imul
def test_tupleresizebug
def test_hash
def test_repr
def test_track_literals
def check_track_dynamic
def test_track_dynamic
def test_track_subtypes
def test_bug7466
def test_getitem
def test_getslice
def test_contains
def test_contains_fake
def test_contains_order
def test_minmax
def test_addmul
def test_getitemoverwriteiter
def test_repeat
def test_bigrepeat
def test_subscript
def test_count
def test_index

Static Public Attributes

 type2test = tuple

Private Member Functions

def _not_tracked
def _tracked

Detailed Description

Definition at line 5 of file test_tuple.py.


Member Function Documentation

def test.test_tuple.TupleTest._not_tracked (   self,
  t 
) [private]

Definition at line 87 of file test_tuple.py.

00087 
00088     def _not_tracked(self, t):
00089         # Nested tuples can take several collections to untrack
00090         gc.collect()
00091         gc.collect()
00092         self.assertFalse(gc.is_tracked(t), t)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_tuple.TupleTest._tracked (   self,
  t 
) [private]

Definition at line 93 of file test_tuple.py.

00093 
00094     def _tracked(self, t):
00095         self.assertTrue(gc.is_tracked(t), t)
00096         gc.collect()
00097         gc.collect()
00098         self.assertTrue(gc.is_tracked(t), t)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_tuple.TupleTest.check_track_dynamic (   self,
  tp,
  always_track 
)

Definition at line 120 of file test_tuple.py.

00120 
00121     def check_track_dynamic(self, tp, always_track):
00122         x, y, z = 1.5, "a", []
00123 
00124         check = self._tracked if always_track else self._not_tracked
00125         check(tp())
00126         check(tp([]))
00127         check(tp(set()))
00128         check(tp([1, x, y]))
00129         check(tp(obj for obj in [1, x, y]))
00130         check(tp(set([1, x, y])))
00131         check(tp(tuple([obj]) for obj in [1, x, y]))
00132         check(tuple(tp([obj]) for obj in [1, x, y]))
00133 
00134         self._tracked(tp([z]))
00135         self._tracked(tp([[x, y]]))
00136         self._tracked(tp([{x: y}]))
00137         self._tracked(tp(obj for obj in [x, y, z]))
00138         self._tracked(tp(tuple([obj]) for obj in [x, y, z]))
00139         self._tracked(tuple(tp([obj]) for obj in [x, y, z]))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.seq_tests.CommonTest.test_addmul (   self) [inherited]

Definition at line 245 of file seq_tests.py.

00245 
00246     def test_addmul(self):
00247         u1 = self.type2test([0])
00248         u2 = self.type2test([0, 1])
00249         self.assertEqual(u1, u1 + self.type2test())
00250         self.assertEqual(u1, self.type2test() + u1)
00251         self.assertEqual(u1 + self.type2test([1]), u2)
00252         self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
00253         self.assertEqual(self.type2test(), u2*0)
00254         self.assertEqual(self.type2test(), 0*u2)
00255         self.assertEqual(self.type2test(), u2*0)
00256         self.assertEqual(self.type2test(), 0*u2)
00257         self.assertEqual(u2, u2*1)
00258         self.assertEqual(u2, 1*u2)
00259         self.assertEqual(u2, u2*1)
00260         self.assertEqual(u2, 1*u2)
00261         self.assertEqual(u2+u2, u2*2)
00262         self.assertEqual(u2+u2, 2*u2)
00263         self.assertEqual(u2+u2, u2*2)
00264         self.assertEqual(u2+u2, 2*u2)
00265         self.assertEqual(u2+u2+u2, u2*3)
00266         self.assertEqual(u2+u2+u2, 3*u2)
00267 
00268         class subclass(self.type2test):
00269             pass
00270         u3 = subclass([0, 1])
00271         self.assertEqual(u3, u3*1)
00272         self.assertIsNot(u3, u3*1)

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_bigrepeat (   self) [inherited]

Definition at line 306 of file seq_tests.py.

00306 
00307     def test_bigrepeat(self):
00308         import sys
00309         if sys.maxsize <= 2147483647:
00310             x = self.type2test([0])
00311             x *= 2**16
00312             self.assertRaises(MemoryError, x.__mul__, 2**16)
00313             if hasattr(x, '__imul__'):
00314                 self.assertRaises(MemoryError, x.__imul__, 2**16)

Here is the call graph for this function:

Definition at line 153 of file test_tuple.py.

00153 
00154     def test_bug7466(self):
00155         # Trying to untrack an unfinished tuple could crash Python
00156         self._not_tracked(tuple(gc.collect() for i in range(101)))

Here is the call graph for this function:

Reimplemented from test.seq_tests.CommonTest.

Definition at line 8 of file test_tuple.py.

00008 
00009     def test_constructors(self):
00010         super().test_constructors()
00011         # calling built-in types without argument must return empty
00012         self.assertEqual(tuple(), ())
00013         t0_3 = (0, 1, 2, 3)
00014         t0_3_bis = tuple(t0_3)
00015         self.assertTrue(t0_3 is t0_3_bis)
00016         self.assertEqual(tuple([]), ())
00017         self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
00018         self.assertEqual(tuple(''), ())
00019         self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_contains (   self) [inherited]

Definition at line 199 of file seq_tests.py.

00199 
00200     def test_contains(self):
00201         u = self.type2test([0, 1, 2])
00202         for i in u:
00203             self.assertIn(i, u)
00204         for i in min(u)-1, max(u)+1:
00205             self.assertNotIn(i, u)
00206 
00207         self.assertRaises(TypeError, u.__contains__)

Here is the call graph for this function:

Definition at line 208 of file seq_tests.py.

00208 
00209     def test_contains_fake(self):
00210         class AllEq:
00211             # Sequences must use rich comparison against each item
00212             # (unless "is" is true, or an earlier item answered)
00213             # So instances of AllEq must be found in all non-empty sequences.
00214             def __eq__(self, other):
00215                 return True
00216             __hash__ = None # Can't meet hash invariant requirements
00217         self.assertNotIn(AllEq(), self.type2test([]))
00218         self.assertIn(AllEq(), self.type2test([1]))

Here is the call graph for this function:

Definition at line 219 of file seq_tests.py.

00219 
00220     def test_contains_order(self):
00221         # Sequences must test in-order.  If a rich comparison has side
00222         # effects, these will be visible to tests against later members.
00223         # In this test, the "side effect" is a short-circuiting raise.
00224         class DoNotTestEq(Exception):
00225             pass
00226         class StopCompares:
00227             def __eq__(self, other):
00228                 raise DoNotTestEq
00229 
00230         checkfirst = self.type2test([1, StopCompares()])
00231         self.assertIn(1, checkfirst)
00232         checklast = self.type2test([StopCompares(), 1])
00233         self.assertRaises(DoNotTestEq, checklast.__contains__, 1)

def test.seq_tests.CommonTest.test_count (   self) [inherited]

Reimplemented in test.list_tests.CommonTest.

Definition at line 331 of file seq_tests.py.

00331 
00332     def test_count(self):
00333         a = self.type2test([0, 1, 2])*3
00334         self.assertEqual(a.count(0), 3)
00335         self.assertEqual(a.count(1), 3)
00336         self.assertEqual(a.count(3), 0)
00337 
00338         self.assertRaises(TypeError, a.count)
00339 
00340         class BadExc(Exception):
00341             pass
00342 
00343         class BadCmp:
00344             def __eq__(self, other):
00345                 if other == 2:
00346                     raise BadExc()
00347                 return False
00348 
00349         self.assertRaises(BadExc, a.count, BadCmp())

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_getitem (   self) [inherited]

Definition at line 137 of file seq_tests.py.

00137 
00138     def test_getitem(self):
00139         u = self.type2test([0, 1, 2, 3, 4])
00140         for i in range(len(u)):
00141             self.assertEqual(u[i], i)
00142             self.assertEqual(u[int(i)], i)
00143         for i in range(-len(u), -1):
00144             self.assertEqual(u[i], len(u)+i)
00145             self.assertEqual(u[int(i)], len(u)+i)
00146         self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
00147         self.assertRaises(IndexError, u.__getitem__, len(u))
00148         self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
00149 
00150         u = self.type2test()
00151         self.assertRaises(IndexError, u.__getitem__, 0)
00152         self.assertRaises(IndexError, u.__getitem__, -1)
00153 
00154         self.assertRaises(TypeError, u.__getitem__)
00155 
00156         a = self.type2test([10, 11])
00157         self.assertEqual(a[0], 10)
00158         self.assertEqual(a[1], 11)
00159         self.assertEqual(a[-2], 10)
00160         self.assertEqual(a[-1], 11)
00161         self.assertRaises(IndexError, a.__getitem__, -3)
00162         self.assertRaises(IndexError, a.__getitem__, 3)

Here is the call graph for this function:

Reimplemented in test.test_userlist.UserListTest.

Definition at line 291 of file seq_tests.py.

00291 
00292     def test_getitemoverwriteiter(self):
00293         # Verify that __getitem__ overrides are not recognized by __iter__
00294         class T(self.type2test):
00295             def __getitem__(self, key):
00296                 return str(key) + '!!!'
00297         self.assertEqual(next(iter(T((1,2)))), 1)

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_getslice (   self) [inherited]

Reimplemented in test.test_userlist.UserListTest.

Definition at line 163 of file seq_tests.py.

00163 
00164     def test_getslice(self):
00165         l = [0, 1, 2, 3, 4]
00166         u = self.type2test(l)
00167 
00168         self.assertEqual(u[0:0], self.type2test())
00169         self.assertEqual(u[1:2], self.type2test([1]))
00170         self.assertEqual(u[-2:-1], self.type2test([3]))
00171         self.assertEqual(u[-1000:1000], u)
00172         self.assertEqual(u[1000:-1000], self.type2test([]))
00173         self.assertEqual(u[:], u)
00174         self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
00175         self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
00176 
00177         # Extended slices
00178         self.assertEqual(u[::], u)
00179         self.assertEqual(u[::2], self.type2test([0, 2, 4]))
00180         self.assertEqual(u[1::2], self.type2test([1, 3]))
00181         self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
00182         self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
00183         self.assertEqual(u[3::-2], self.type2test([3, 1]))
00184         self.assertEqual(u[3:3:-2], self.type2test([]))
00185         self.assertEqual(u[3:2:-2], self.type2test([3]))
00186         self.assertEqual(u[3:1:-2], self.type2test([3]))
00187         self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
00188         self.assertEqual(u[::-100], self.type2test([4]))
00189         self.assertEqual(u[100:-100:], self.type2test([]))
00190         self.assertEqual(u[-100:100:], u)
00191         self.assertEqual(u[100:-100:-1], u[::-1])
00192         self.assertEqual(u[-100:100:-1], self.type2test([]))
00193         self.assertEqual(u[-100:100:2], self.type2test([0, 2, 4]))
00194 
00195         # Test extreme cases with long ints
00196         a = self.type2test([0,1,2,3,4])
00197         self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2]))
00198         self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4]))

Here is the call graph for this function:

Definition at line 52 of file test_tuple.py.

00052 
00053     def test_hash(self):
00054         # See SF bug 942952:  Weakness in tuple hash
00055         # The hash should:
00056         #      be non-commutative
00057         #      should spread-out closely spaced values
00058         #      should not exhibit cancellation in tuples like (x,(x,y))
00059         #      should be distinct from element hashes:  hash(x)!=hash((x,))
00060         # This test exercises those cases.
00061         # For a pure random hash and N=50, the expected number of occupied
00062         #      buckets when tossing 252,600 balls into 2**32 buckets
00063         #      is 252,592.6, or about 7.4 expected collisions.  The
00064         #      standard deviation is 2.73.  On a box with 64-bit hash
00065         #      codes, no collisions are expected.  Here we accept no
00066         #      more than 15 collisions.  Any worse and the hash function
00067         #      is sorely suspect.
00068 
00069         N=50
00070         base = list(range(N))
00071         xp = [(i, j) for i in base for j in base]
00072         inps = base + [(i, j) for i in base for j in xp] + \
00073                      [(i, j) for i in xp for j in base] + xp + list(zip(base))
00074         collisions = len(inps) - len(set(map(hash, inps)))
00075         self.assertTrue(collisions <= 15)

Here is the call graph for this function:

Reimplemented from test.seq_tests.CommonTest.

Definition at line 31 of file test_tuple.py.

00031 
00032     def test_iadd(self):
00033         super().test_iadd()
00034         u = (0, 1)
00035         u2 = u
00036         u += (2, 3)
00037         self.assertTrue(u is not u2)

Here is the call graph for this function:

Reimplemented from test.seq_tests.CommonTest.

Definition at line 38 of file test_tuple.py.

00038 
00039     def test_imul(self):
00040         super().test_imul()
00041         u = (0, 1)
00042         u2 = u
00043         u *= 3
00044         self.assertTrue(u is not u2)

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_index (   self) [inherited]

Reimplemented in test.list_tests.CommonTest.

Definition at line 350 of file seq_tests.py.

00350 
00351     def test_index(self):
00352         u = self.type2test([0, 1])
00353         self.assertEqual(u.index(0), 0)
00354         self.assertEqual(u.index(1), 1)
00355         self.assertRaises(ValueError, u.index, 2)
00356 
00357         u = self.type2test([-2, -1, 0, 0, 1, 2])
00358         self.assertEqual(u.count(0), 2)
00359         self.assertEqual(u.index(0), 2)
00360         self.assertEqual(u.index(0, 2), 2)
00361         self.assertEqual(u.index(-2, -10), 0)
00362         self.assertEqual(u.index(0, 3), 3)
00363         self.assertEqual(u.index(0, 3, 4), 3)
00364         self.assertRaises(ValueError, u.index, 2, 0, -10)
00365 
00366         self.assertRaises(TypeError, u.index)
00367 
00368         class BadExc(Exception):
00369             pass
00370 
00371         class BadCmp:
00372             def __eq__(self, other):
00373                 if other == 2:
00374                     raise BadExc()
00375                 return False
00376 
00377         a = self.type2test([0, 1, 2, 3])
00378         self.assertRaises(BadExc, a.index, BadCmp())
00379 
00380         a = self.type2test([-2, -1, 0, 0, 1, 2])
00381         self.assertEqual(a.index(0), 2)
00382         self.assertEqual(a.index(0, 2), 2)
00383         self.assertEqual(a.index(0, -4), 2)
00384         self.assertEqual(a.index(-2, -10), 0)
00385         self.assertEqual(a.index(0, 3), 3)
00386         self.assertEqual(a.index(0, -3), 3)
00387         self.assertEqual(a.index(0, 3, 4), 3)
00388         self.assertEqual(a.index(0, -3, -2), 3)
00389         self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2)
00390         self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize)
00391         self.assertRaises(ValueError, a.index, 2, 0, -10)

Here is the call graph for this function:

Reimplemented from test.seq_tests.CommonTest.

Definition at line 25 of file test_tuple.py.

00025 
00026     def test_len(self):
00027         super().test_len()
00028         self.assertEqual(len(()), 0)
00029         self.assertEqual(len((0,)), 1)
00030         self.assertEqual(len((0, 1, 2)), 3)

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_minmax (   self) [inherited]

Definition at line 240 of file seq_tests.py.

00240 
00241     def test_minmax(self):
00242         u = self.type2test([0, 1, 2])
00243         self.assertEqual(min(u), 0)
00244         self.assertEqual(max(u), 2)

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_repeat (   self) [inherited]

Definition at line 298 of file seq_tests.py.

00298 
00299     def test_repeat(self):
00300         for m in range(4):
00301             s = tuple(range(m))
00302             for n in range(-3, 5):
00303                 self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
00304             self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
00305             self.assertEqual(id(s), id(s*1))

Here is the call graph for this function:

Definition at line 76 of file test_tuple.py.

00076 
00077     def test_repr(self):
00078         l0 = tuple()
00079         l2 = (0, 1, 2)
00080         a0 = self.type2test(l0)
00081         a2 = self.type2test(l2)
00082 
00083         self.assertEqual(str(a0), repr(l0))
00084         self.assertEqual(str(a2), repr(l2))
00085         self.assertEqual(repr(a0), "()")
00086         self.assertEqual(repr(a2), "(0, 1, 2)")

Here is the call graph for this function:

def test.seq_tests.CommonTest.test_subscript (   self) [inherited]

Definition at line 315 of file seq_tests.py.

00315 
00316     def test_subscript(self):
00317         a = self.type2test([10, 11])
00318         self.assertEqual(a.__getitem__(0), 10)
00319         self.assertEqual(a.__getitem__(1), 11)
00320         self.assertEqual(a.__getitem__(-2), 10)
00321         self.assertEqual(a.__getitem__(-1), 11)
00322         self.assertRaises(IndexError, a.__getitem__, -3)
00323         self.assertRaises(IndexError, a.__getitem__, 3)
00324         self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
00325         self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
00326         self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
00327         self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
00328         self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
00329         self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
00330         self.assertRaises(TypeError, a.__getitem__, 'x')

Here is the call graph for this function:

Definition at line 141 of file test_tuple.py.

00141 
00142     def test_track_dynamic(self):
00143         # Test GC-optimization of dynamically constructed tuples.
00144         self.check_track_dynamic(tuple, False)

Here is the call graph for this function:

Definition at line 100 of file test_tuple.py.

00100 
00101     def test_track_literals(self):
00102         # Test GC-optimization of tuple literals
00103         x, y, z = 1.5, "a", []
00104 
00105         self._not_tracked(())
00106         self._not_tracked((1,))
00107         self._not_tracked((1, 2))
00108         self._not_tracked((1, 2, "a"))
00109         self._not_tracked((1, 2, (None, True, False, ()), int))
00110         self._not_tracked((object(),))
00111         self._not_tracked(((1, x), y, (2, 3)))
00112 
00113         # Tuples with mutable elements are always tracked, even if those
00114         # elements are not tracked right now.
00115         self._tracked(([],))
00116         self._tracked(([1],))
00117         self._tracked(({},))
00118         self._tracked((set(),))
00119         self._tracked((x, y, z))

Here is the call graph for this function:

Definition at line 146 of file test_tuple.py.

00146 
00147     def test_track_subtypes(self):
00148         # Tuple subtypes must always be tracked
00149         class MyTuple(tuple):
00150             pass
00151         self.check_track_dynamic(MyTuple, True)

Reimplemented from test.seq_tests.CommonTest.

Definition at line 20 of file test_tuple.py.

00020 
00021     def test_truth(self):
00022         super().test_truth()
00023         self.assertTrue(not ())
00024         self.assertTrue((42, ))

Here is the call graph for this function:

Definition at line 45 of file test_tuple.py.

00045 
00046     def test_tupleresizebug(self):
00047         # Check that a specific bug in _PyTuple_Resize() is squashed.
00048         def f():
00049             for i in range(1000):
00050                 yield i
00051         self.assertEqual(list(tuple(f())), list(range(1000)))

Here is the call graph for this function:


Member Data Documentation

Reimplemented from test.seq_tests.CommonTest.

Definition at line 6 of file test_tuple.py.


The documentation for this class was generated from the following file: