Back to index

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

List of all members.

Public Member Functions

def test_constructors
def test_truth
def test_getitem
def test_getslice
def test_contains
def test_contains_fake
def test_contains_order
def test_len
def test_minmax
def test_addmul
def test_iadd
def test_imul
def test_getitemoverwriteiter
def test_repeat
def test_bigrepeat
def test_subscript
def test_count
def test_index

Static Public Attributes

 type2test = None

Private Attributes

 __data

Detailed Description

Definition at line 87 of file seq_tests.py.


Member Function Documentation

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:

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:

Reimplemented in test.test_tuple.TupleTest.

Definition at line 91 of file seq_tests.py.

00091 
00092     def test_constructors(self):
00093         l0 = []
00094         l1 = [0]
00095         l2 = [0, 1]
00096 
00097         u = self.type2test()
00098         u0 = self.type2test(l0)
00099         u1 = self.type2test(l1)
00100         u2 = self.type2test(l2)
00101 
00102         uu = self.type2test(u)
00103         uu0 = self.type2test(u0)
00104         uu1 = self.type2test(u1)
00105         uu2 = self.type2test(u2)
00106 
00107         v = self.type2test(tuple(u))
00108         class OtherSeq:
00109             def __init__(self, initseq):
00110                 self.__data = initseq
00111             def __len__(self):
00112                 return len(self.__data)
00113             def __getitem__(self, i):
00114                 return self.__data[i]
00115         s = OtherSeq(u0)
00116         v0 = self.type2test(s)
00117         self.assertEqual(len(v0), len(s))
00118 
00119         s = "this is also a sequence"
00120         vv = self.type2test(s)
00121         self.assertEqual(len(vv), len(s))
00122 
00123         # Create from various iteratables
00124         for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
00125             for g in (Sequence, IterFunc, IterGen,
00126                       itermulti, iterfunc):
00127                 self.assertEqual(self.type2test(g(s)), self.type2test(s))
00128             self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
00129             self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
00130             self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
00131             self.assertRaises(TypeError, self.type2test, IterNoNext(s))
00132             self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))

Here is the call graph for this function:

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)

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:

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:

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:

Reimplemented in test.list_tests.CommonTest, test.test_tuple.TupleTest, and test.test_userlist.UserListTest.

Definition at line 273 of file seq_tests.py.

00273 
00274     def test_iadd(self):
00275         u = self.type2test([0, 1])
00276         u += self.type2test()
00277         self.assertEqual(u, self.type2test([0, 1]))
00278         u += self.type2test([2, 3])
00279         self.assertEqual(u, self.type2test([0, 1, 2, 3]))
00280         u += self.type2test([4, 5])
00281         self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
00282 
00283         u = self.type2test("spam")
00284         u += self.type2test("eggs")
00285         self.assertEqual(u, self.type2test("spameggs"))

Here is the call graph for this function:

Reimplemented in test.list_tests.CommonTest, and test.test_tuple.TupleTest.

Definition at line 286 of file seq_tests.py.

00286 
00287     def test_imul(self):
00288         u = self.type2test([0, 1])
00289         u *= 3
00290         self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))

Here is the call graph for this function:

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 in test.test_list.ListTest, and test.test_tuple.TupleTest.

Definition at line 234 of file seq_tests.py.

00234 
00235     def test_len(self):
00236         self.assertEqual(len(self.type2test()), 0)
00237         self.assertEqual(len(self.type2test([])), 0)
00238         self.assertEqual(len(self.type2test([0])), 1)
00239         self.assertEqual(len(self.type2test([0, 1, 2])), 3)

Here is the call graph for this function:

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:

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 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:

Reimplemented in test.test_list.ListTest, and test.test_tuple.TupleTest.

Definition at line 133 of file seq_tests.py.

00133 
00134     def test_truth(self):
00135         self.assertFalse(self.type2test())
00136         self.assertTrue(self.type2test([42]))

Here is the call graph for this function:


Member Data Documentation

Definition at line 109 of file seq_tests.py.


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