Back to index

python3.2  3.2.2
Public Member Functions
test.test_deque.TestBasic Class Reference

List of all members.

Public Member Functions

def test_basics
def test_maxlen
def test_maxlen_zero
def test_maxlen_attribute
def test_count
def test_comparisons
def test_extend
def test_iadd
def test_extendleft
def test_getitem
def test_setitem
def test_delitem
def test_reverse
def test_rotate
def test_len
def test_underflow
def test_clear
def test_remove
def test_repr
def test_print
def test_init
def test_hash
def test_long_steadystate_queue_popleft
def test_long_steadystate_queue_popright
def test_big_queue_popleft
def test_big_queue_popright
def test_big_stack_right
def test_big_stack_left
def test_roundtrip_iter_init
def test_pickle

Detailed Description

Definition at line 29 of file test_deque.py.


Member Function Documentation

Definition at line 31 of file test_deque.py.

00031 
00032     def test_basics(self):
00033         d = deque(range(-5125, -5000))
00034         d.__init__(range(200))
00035         for i in range(200, 400):
00036             d.append(i)
00037         for i in reversed(range(-200, 0)):
00038             d.appendleft(i)
00039         self.assertEqual(list(d), list(range(-200, 400)))
00040         self.assertEqual(len(d), 600)
00041 
00042         left = [d.popleft() for i in range(250)]
00043         self.assertEqual(left, list(range(-200, 50)))
00044         self.assertEqual(list(d), list(range(50, 400)))
00045 
00046         right = [d.pop() for i in range(250)]
00047         right.reverse()
00048         self.assertEqual(right, list(range(150, 400)))
00049         self.assertEqual(list(d), list(range(50, 150)))

Here is the call graph for this function:

Definition at line 409 of file test_deque.py.

00409 
00410     def test_big_queue_popleft(self):
00411         pass
00412         d = deque()
00413         append, pop = d.append, d.popleft
00414         for i in range(BIG):
00415             append(i)
00416         for i in range(BIG):
00417             x = pop()
00418             if x != i:
00419                 self.assertEqual(x, i)

Here is the call graph for this function:

Definition at line 420 of file test_deque.py.

00420 
00421     def test_big_queue_popright(self):
00422         d = deque()
00423         append, pop = d.appendleft, d.pop
00424         for i in range(BIG):
00425             append(i)
00426         for i in range(BIG):
00427             x = pop()
00428             if x != i:
00429                 self.assertEqual(x, i)

Here is the call graph for this function:

Definition at line 441 of file test_deque.py.

00441 
00442     def test_big_stack_left(self):
00443         d = deque()
00444         append, pop = d.appendleft, d.popleft
00445         for i in range(BIG):
00446             append(i)
00447         for i in reversed(range(BIG)):
00448             x = pop()
00449             if x != i:
00450                 self.assertEqual(x, i)
00451         self.assertEqual(len(d), 0)

Here is the call graph for this function:

Definition at line 430 of file test_deque.py.

00430 
00431     def test_big_stack_right(self):
00432         d = deque()
00433         append, pop = d.append, d.pop
00434         for i in range(BIG):
00435             append(i)
00436         for i in reversed(range(BIG)):
00437             x = pop()
00438             if x != i:
00439                 self.assertEqual(x, i)
00440         self.assertEqual(len(d), 0)

Here is the call graph for this function:

Definition at line 325 of file test_deque.py.

00325 
00326     def test_clear(self):
00327         d = deque(range(100))
00328         self.assertEqual(len(d), 100)
00329         d.clear()
00330         self.assertEqual(len(d), 0)
00331         self.assertEqual(list(d), [])
00332         d.clear()               # clear an emtpy deque
00333         self.assertEqual(list(d), [])

Here is the call graph for this function:

Definition at line 150 of file test_deque.py.

00150 
00151     def test_comparisons(self):
00152         d = deque('xabc'); d.popleft()
00153         for e in [d, deque('abc'), deque('ab'), deque(), list(d)]:
00154             self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e))
00155             self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e)))
00156 
00157         args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba'))
00158         for x in args:
00159             for y in args:
00160                 self.assertEqual(x == y, list(x) == list(y), (x,y))
00161                 self.assertEqual(x != y, list(x) != list(y), (x,y))
00162                 self.assertEqual(x <  y, list(x) <  list(y), (x,y))
00163                 self.assertEqual(x <= y, list(x) <= list(y), (x,y))
00164                 self.assertEqual(x >  y, list(x) >  list(y), (x,y))
00165                 self.assertEqual(x >= y, list(x) >= list(y), (x,y))

Here is the call graph for this function:

Definition at line 117 of file test_deque.py.

00117 
00118     def test_count(self):
00119         for s in ('', 'abracadabra', 'simsalabim'*500+'abc'):
00120             s = list(s)
00121             d = deque(s)
00122             for letter in 'abcdefghijklmnopqrstuvwxyz':
00123                 self.assertEqual(s.count(letter), d.count(letter), (s, d, letter))
00124         self.assertRaises(TypeError, d.count)       # too few args
00125         self.assertRaises(TypeError, d.count, 1, 2) # too many args
00126         class BadCompare:
00127             def __eq__(self, other):
00128                 raise ArithmeticError
00129         d = deque([1, 2, BadCompare(), 3])
00130         self.assertRaises(ArithmeticError, d.count, 2)
00131         d = deque([1, 2, 3])
00132         self.assertRaises(ArithmeticError, d.count, BadCompare())
00133         class MutatingCompare:
00134             def __eq__(self, other):
00135                 self.d.pop()
00136                 return True
00137         m = MutatingCompare()
00138         d = deque([1, 2, 3, m, 4, 5])
00139         m.d = d
00140         self.assertRaises(RuntimeError, d.count, 3)
00141 
00142         # test issue11004
00143         # block advance failed after rotation aligned elements on right side of block
00144         d = deque([None]*16)
00145         for i in range(len(d)):
00146             d.rotate(-1)
00147         d.rotate(1)
00148         self.assertEqual(d.count(1), 0)
00149         self.assertEqual(d.count(None), 16)

Here is the call graph for this function:

Definition at line 225 of file test_deque.py.

00225 
00226     def test_delitem(self):
00227         n = 500         # O(n**2) test, don't make this too big
00228         d = deque(range(n))
00229         self.assertRaises(IndexError, d.__delitem__, -n-1)
00230         self.assertRaises(IndexError, d.__delitem__, n)
00231         for i in range(n):
00232             self.assertEqual(len(d), n-i)
00233             j = random.randrange(-len(d), len(d))
00234             val = d[j]
00235             self.assertIn(val, d)
00236             del d[j]
00237             self.assertNotIn(val, d)
00238         self.assertEqual(len(d), 0)

Here is the call graph for this function:

Definition at line 166 of file test_deque.py.

00166 
00167     def test_extend(self):
00168         d = deque('a')
00169         self.assertRaises(TypeError, d.extend, 1)
00170         d.extend('bcd')
00171         self.assertEqual(list(d), list('abcd'))
00172         d.extend(d)
00173         self.assertEqual(list(d), list('abcdabcd'))

Here is the call graph for this function:

Definition at line 181 of file test_deque.py.

00181 
00182     def test_extendleft(self):
00183         d = deque('a')
00184         self.assertRaises(TypeError, d.extendleft, 1)
00185         d.extendleft('bcd')
00186         self.assertEqual(list(d), list(reversed('abcd')))
00187         d.extendleft(d)
00188         self.assertEqual(list(d), list('abcddcba'))
00189         d = deque()
00190         d.extendleft(range(1000))
00191         self.assertEqual(list(d), list(reversed(range(1000))))
00192         self.assertRaises(SyntaxError, d.extendleft, fail())

Here is the call graph for this function:

Definition at line 193 of file test_deque.py.

00193 
00194     def test_getitem(self):
00195         n = 200
00196         d = deque(range(n))
00197         l = list(range(n))
00198         for i in range(n):
00199             d.popleft()
00200             l.pop(0)
00201             if random.random() < 0.5:
00202                 d.append(i)
00203                 l.append(i)
00204             for j in range(1-len(l), len(l)):
00205                 assert d[j] == l[j]
00206 
00207         d = deque('superman')
00208         self.assertEqual(d[0], 's')
00209         self.assertEqual(d[-1], 'n')
00210         d = deque()
00211         self.assertRaises(IndexError, d.__getitem__, 0)
00212         self.assertRaises(IndexError, d.__getitem__, -1)

Here is the call graph for this function:

Definition at line 383 of file test_deque.py.

00383 
00384     def test_hash(self):
00385         self.assertRaises(TypeError, hash, deque('abc'))

Here is the call graph for this function:

Definition at line 174 of file test_deque.py.

00174 
00175     def test_iadd(self):
00176         d = deque('a')
00177         d += 'bcd'
00178         self.assertEqual(list(d), list('abcd'))
00179         d += d
00180         self.assertEqual(list(d), list('abcdabcd'))

Here is the call graph for this function:

Definition at line 379 of file test_deque.py.

00379 
00380     def test_init(self):
00381         self.assertRaises(TypeError, deque, 'abc', 2, 3);
00382         self.assertRaises(TypeError, deque, 1);

Here is the call graph for this function:

Definition at line 304 of file test_deque.py.

00304 
00305     def test_len(self):
00306         d = deque('ab')
00307         self.assertEqual(len(d), 2)
00308         d.popleft()
00309         self.assertEqual(len(d), 1)
00310         d.pop()
00311         self.assertEqual(len(d), 0)
00312         self.assertRaises(IndexError, d.pop)
00313         self.assertEqual(len(d), 0)
00314         d.append('c')
00315         self.assertEqual(len(d), 1)
00316         d.appendleft('d')
00317         self.assertEqual(len(d), 2)
00318         d.clear()
00319         self.assertEqual(len(d), 0)

Here is the call graph for this function:

Definition at line 386 of file test_deque.py.

00386 
00387     def test_long_steadystate_queue_popleft(self):
00388         for size in (0, 1, 2, 100, 1000):
00389             d = deque(range(size))
00390             append, pop = d.append, d.popleft
00391             for i in range(size, BIG):
00392                 append(i)
00393                 x = pop()
00394                 if x != i - size:
00395                     self.assertEqual(x, i-size)
00396             self.assertEqual(list(d), list(range(BIG-size, BIG)))

Here is the call graph for this function:

Definition at line 397 of file test_deque.py.

00397 
00398     def test_long_steadystate_queue_popright(self):
00399         for size in (0, 1, 2, 100, 1000):
00400             d = deque(reversed(range(size)))
00401             append, pop = d.appendleft, d.pop
00402             for i in range(size, BIG):
00403                 append(i)
00404                 x = pop()
00405                 if x != i - size:
00406                     self.assertEqual(x, i-size)
00407             self.assertEqual(list(reversed(list(d))),
00408                              list(range(BIG-size, BIG)))

Here is the call graph for this function:

Definition at line 50 of file test_deque.py.

00050 
00051     def test_maxlen(self):
00052         self.assertRaises(ValueError, deque, 'abc', -1)
00053         self.assertRaises(ValueError, deque, 'abc', -2)
00054         it = iter(range(10))
00055         d = deque(it, maxlen=3)
00056         self.assertEqual(list(it), [])
00057         self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)')
00058         self.assertEqual(list(d), [7, 8, 9])
00059         self.assertEqual(d, deque(range(10), 3))
00060         d.append(10)
00061         self.assertEqual(list(d), [8, 9, 10])
00062         d.appendleft(7)
00063         self.assertEqual(list(d), [7, 8, 9])
00064         d.extend([10, 11])
00065         self.assertEqual(list(d), [9, 10, 11])
00066         d.extendleft([8, 7])
00067         self.assertEqual(list(d), [7, 8, 9])
00068         d = deque(range(200), maxlen=10)
00069         d.append(d)
00070         support.unlink(support.TESTFN)
00071         fo = open(support.TESTFN, "w")
00072         try:
00073             fo.write(str(d))
00074             fo.close()
00075             fo = open(support.TESTFN, "r")
00076             self.assertEqual(fo.read(), repr(d))
00077         finally:
00078             fo.close()
00079             support.unlink(support.TESTFN)
00080 
00081         d = deque(range(10), maxlen=None)
00082         self.assertEqual(repr(d), 'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])')
00083         fo = open(support.TESTFN, "w")
00084         try:
00085             fo.write(str(d))
00086             fo.close()
00087             fo = open(support.TESTFN, "r")
00088             self.assertEqual(fo.read(), repr(d))
00089         finally:
00090             fo.close()
00091             support.unlink(support.TESTFN)

Here is the call graph for this function:

Definition at line 107 of file test_deque.py.

00107 
00108     def test_maxlen_attribute(self):
00109         self.assertEqual(deque().maxlen, None)
00110         self.assertEqual(deque('abc').maxlen, None)
00111         self.assertEqual(deque('abc', maxlen=4).maxlen, 4)
00112         self.assertEqual(deque('abc', maxlen=2).maxlen, 2)
00113         self.assertEqual(deque('abc', maxlen=0).maxlen, 0)
00114         with self.assertRaises(AttributeError):
00115             d = deque('abc')
00116             d.maxlen = 10

Here is the call graph for this function:

Definition at line 92 of file test_deque.py.

00092 
00093     def test_maxlen_zero(self):
00094         it = iter(range(100))
00095         deque(it, maxlen=0)
00096         self.assertEqual(list(it), [])
00097 
00098         it = iter(range(100))
00099         d = deque(maxlen=0)
00100         d.extend(it)
00101         self.assertEqual(list(it), [])
00102 
00103         it = iter(range(100))
00104         d = deque(maxlen=0)
00105         d.extendleft(it)
00106         self.assertEqual(list(it), [])

Here is the call graph for this function:

Definition at line 458 of file test_deque.py.

00458 
00459     def test_pickle(self):
00460         d = deque(range(200))
00461         for i in range(pickle.HIGHEST_PROTOCOL + 1):
00462             s = pickle.dumps(d, i)
00463             e = pickle.loads(s)
00464             self.assertNotEqual(id(d), id(e))
00465             self.assertEqual(list(d), list(e))

Here is the call graph for this function:

Definition at line 365 of file test_deque.py.

00365 
00366     def test_print(self):
00367         d = deque(range(200))
00368         d.append(d)
00369         try:
00370             support.unlink(support.TESTFN)
00371             fo = open(support.TESTFN, "w")
00372             print(d, file=fo, end='')
00373             fo.close()
00374             fo = open(support.TESTFN, "r")
00375             self.assertEqual(fo.read(), repr(d))
00376         finally:
00377             fo.close()
00378             support.unlink(support.TESTFN)

Here is the call graph for this function:

Definition at line 334 of file test_deque.py.

00334 
00335     def test_remove(self):
00336         d = deque('abcdefghcij')
00337         d.remove('c')
00338         self.assertEqual(d, deque('abdefghcij'))
00339         d.remove('c')
00340         self.assertEqual(d, deque('abdefghij'))
00341         self.assertRaises(ValueError, d.remove, 'c')
00342         self.assertEqual(d, deque('abdefghij'))
00343 
00344         # Handle comparison errors
00345         d = deque(['a', 'b', BadCmp(), 'c'])
00346         e = deque(d)
00347         self.assertRaises(RuntimeError, d.remove, 'c')
00348         for x, y in zip(d, e):
00349             # verify that original order and values are retained.
00350             self.assertTrue(x is y)
00351 
00352         # Handle evil mutator
00353         for match in (True, False):
00354             d = deque(['ab'])
00355             d.extend([MutateCmp(d, match), 'c'])
00356             self.assertRaises(IndexError, d.remove, 'c')
00357             self.assertEqual(d, deque())

Here is the call graph for this function:

Definition at line 358 of file test_deque.py.

00358 
00359     def test_repr(self):
00360         d = deque(range(200))
00361         e = eval(repr(d))
00362         self.assertEqual(list(d), list(e))
00363         d.append(d)
00364         self.assertIn('...', repr(d))

Here is the call graph for this function:

Definition at line 239 of file test_deque.py.

00239 
00240     def test_reverse(self):
00241         n = 500         # O(n**2) test, don't make this too big
00242         data = [random.random() for i in range(n)]
00243         for i in range(n):
00244             d = deque(data[:i])
00245             r = d.reverse()
00246             self.assertEqual(list(d), list(reversed(data[:i])))
00247             self.assertIs(r, None)
00248             d.reverse()
00249             self.assertEqual(list(d), data[:i])
00250         self.assertRaises(TypeError, d.reverse, 1)          # Arity is zero

Here is the call graph for this function:

Definition at line 251 of file test_deque.py.

00251 
00252     def test_rotate(self):
00253         s = tuple('abcde')
00254         n = len(s)
00255 
00256         d = deque(s)
00257         d.rotate(1)             # verify rot(1)
00258         self.assertEqual(''.join(d), 'eabcd')
00259 
00260         d = deque(s)
00261         d.rotate(-1)            # verify rot(-1)
00262         self.assertEqual(''.join(d), 'bcdea')
00263         d.rotate()              # check default to 1
00264         self.assertEqual(tuple(d), s)
00265 
00266         for i in range(n*3):
00267             d = deque(s)
00268             e = deque(d)
00269             d.rotate(i)         # check vs. rot(1) n times
00270             for j in range(i):
00271                 e.rotate(1)
00272             self.assertEqual(tuple(d), tuple(e))
00273             d.rotate(-i)        # check that it works in reverse
00274             self.assertEqual(tuple(d), s)
00275             e.rotate(n-i)       # check that it wraps forward
00276             self.assertEqual(tuple(e), s)
00277 
00278         for i in range(n*3):
00279             d = deque(s)
00280             e = deque(d)
00281             d.rotate(-i)
00282             for j in range(i):
00283                 e.rotate(-1)    # check vs. rot(-1) n times
00284             self.assertEqual(tuple(d), tuple(e))
00285             d.rotate(i)         # check that it works in reverse
00286             self.assertEqual(tuple(d), s)
00287             e.rotate(i-n)       # check that it wraps backaround
00288             self.assertEqual(tuple(e), s)
00289 
00290         d = deque(s)
00291         e = deque(s)
00292         e.rotate(BIG+17)        # verify on long series of rotates
00293         dr = d.rotate
00294         for i in range(BIG+17):
00295             dr()
00296         self.assertEqual(tuple(d), tuple(e))
00297 
00298         self.assertRaises(TypeError, d.rotate, 'x')   # Wrong arg type
00299         self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args
00300 
00301         d = deque()
00302         d.rotate()              # rotate an empty deque
00303         self.assertEqual(d, deque())

Here is the call graph for this function:

Definition at line 452 of file test_deque.py.

00452 
00453     def test_roundtrip_iter_init(self):
00454         d = deque(range(200))
00455         e = deque(d)
00456         self.assertNotEqual(id(d), id(e))
00457         self.assertEqual(list(d), list(e))

Here is the call graph for this function:

Definition at line 213 of file test_deque.py.

00213 
00214     def test_setitem(self):
00215         n = 200
00216         d = deque(range(n))
00217         for i in range(n):
00218             d[i] = 10 * i
00219         self.assertEqual(list(d), [10*i for i in range(n)])
00220         l = list(d)
00221         for i in range(1-n, 0, -1):
00222             d[i] = 7*i
00223             l[i] = 7*i
00224         self.assertEqual(list(d), l)

Here is the call graph for this function:

Definition at line 320 of file test_deque.py.

00320 
00321     def test_underflow(self):
00322         d = deque()
00323         self.assertRaises(IndexError, d.pop)
00324         self.assertRaises(IndexError, d.popleft)

Here is the call graph for this function:


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