183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehfrom collections import deque 283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport unittest 383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehfrom test import test_support, seq_tests 483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport gc 583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport weakref 683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport copy 783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport cPickle as pickle 883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport random 983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport struct 1083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 1183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehBIG = 100000 1283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 1383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdef fail(): 1483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh raise SyntaxError 1583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh yield 1 1683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 1783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass BadCmp: 1883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __eq__(self, other): 1983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh raise RuntimeError 2083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 2183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass MutateCmp: 2283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __init__(self, deque, result): 2383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.deque = deque 2483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.result = result 2583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __eq__(self, other): 2683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.deque.clear() 2783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return self.result 2883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 2983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass TestBasic(unittest.TestCase): 3083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 3183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_basics(self): 3283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(-5125, -5000)) 3383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.__init__(xrange(200)) 3483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(200, 400): 3583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(i) 3683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in reversed(xrange(-200, 0)): 3783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.appendleft(i) 3883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(-200, 400)) 3983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 600) 4083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 4183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh left = [d.popleft() for i in xrange(250)] 4283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(left, range(-200, 50)) 4383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(50, 400)) 4483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 4583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh right = [d.pop() for i in xrange(250)] 4683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh right.reverse() 4783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(right, range(150, 400)) 4883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(50, 150)) 4983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 5083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_maxlen(self): 5183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ValueError, deque, 'abc', -1) 5283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ValueError, deque, 'abc', -2) 5383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh it = iter(range(10)) 5483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(it, maxlen=3) 5583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(it), []) 5683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)') 5783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(7, 10)) 5883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d, deque(range(10), 3)) 5983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(10) 6083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(8, 11)) 6183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.appendleft(7) 6283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(7, 10)) 6383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extend([10, 11]) 6483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(9, 12)) 6583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extendleft([8, 7]) 6683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(7, 10)) 6783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(200), maxlen=10) 6883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(d) 6983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.unlink(test_support.TESTFN) 7083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo = open(test_support.TESTFN, "wb") 7183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh try: 7283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh print >> fo, d, 7383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo.close() 7483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo = open(test_support.TESTFN, "rb") 7583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(fo.read(), repr(d)) 7683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh finally: 7783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo.close() 7883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.unlink(test_support.TESTFN) 7983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 8083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(range(10), maxlen=None) 8183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(repr(d), 'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])') 8283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo = open(test_support.TESTFN, "wb") 8383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh try: 8483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh print >> fo, d, 8583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo.close() 8683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo = open(test_support.TESTFN, "rb") 8783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(fo.read(), repr(d)) 8883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh finally: 8983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo.close() 9083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.unlink(test_support.TESTFN) 9183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 9283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_maxlen_zero(self): 9383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh it = iter(range(100)) 9483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh deque(it, maxlen=0) 9583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(it), []) 9683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 9783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh it = iter(range(100)) 9883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(maxlen=0) 9983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extend(it) 10083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(it), []) 10183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 10283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh it = iter(range(100)) 10383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(maxlen=0) 10483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extendleft(it) 10583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(it), []) 10683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 10783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_maxlen_attribute(self): 10883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(deque().maxlen, None) 10983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(deque('abc').maxlen, None) 11083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(deque('abc', maxlen=4).maxlen, 4) 11183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(deque('abc', maxlen=2).maxlen, 2) 11283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(deque('abc', maxlen=0).maxlen, 0) 11383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh with self.assertRaises(AttributeError): 11483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('abc') 11583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.maxlen = 10 11683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 11783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_count(self): 11883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for s in ('', 'abracadabra', 'simsalabim'*500+'abc'): 11983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh s = list(s) 12083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(s) 12183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for letter in 'abcdefghijklmnopqrstuvwxyz': 12283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(s.count(letter), d.count(letter), (s, d, letter)) 12383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.count) # too few args 12483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.count, 1, 2) # too many args 12583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh class BadCompare: 12683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __eq__(self, other): 12783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh raise ArithmeticError 12883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque([1, 2, BadCompare(), 3]) 12983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ArithmeticError, d.count, 2) 13083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque([1, 2, 3]) 13183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ArithmeticError, d.count, BadCompare()) 13283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh class MutatingCompare: 13383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __eq__(self, other): 13483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.d.pop() 13583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return True 13683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh m = MutatingCompare() 13783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque([1, 2, 3, m, 4, 5]) 13883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh m.d = d 13983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(RuntimeError, d.count, 3) 14083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 14183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # test issue11004 14283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # block advance failed after rotation aligned elements on right side of block 14383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque([None]*16) 14483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in range(len(d)): 14583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(-1) 14683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(1) 14783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d.count(1), 0) 14883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d.count(None), 16) 14983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 15083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_comparisons(self): 15183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('xabc'); d.popleft() 15283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for e in [d, deque('abc'), deque('ab'), deque(), list(d)]: 15383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e)) 15483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e))) 15583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 15683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba')) 15783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for x in args: 15883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for y in args: 15983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x == y, list(x) == list(y), (x,y)) 16083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x != y, list(x) != list(y), (x,y)) 16183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x < y, list(x) < list(y), (x,y)) 16283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x <= y, list(x) <= list(y), (x,y)) 16383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x > y, list(x) > list(y), (x,y)) 16483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x >= y, list(x) >= list(y), (x,y)) 16583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(cmp(x,y), cmp(list(x),list(y)), (x,y)) 16683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 16783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_extend(self): 16883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('a') 16983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.extend, 1) 17083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extend('bcd') 17183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list('abcd')) 17283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extend(d) 17383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list('abcdabcd')) 17483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 17583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_iadd(self): 17683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('a') 17783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d += 'bcd' 17883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list('abcd')) 17983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d += d 18083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list('abcdabcd')) 18183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 18283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_extendleft(self): 18383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('a') 18483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.extendleft, 1) 18583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extendleft('bcd') 18683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(reversed('abcd'))) 18783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extendleft(d) 18883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list('abcddcba')) 18983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 19083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extendleft(range(1000)) 19183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(reversed(range(1000)))) 19283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(SyntaxError, d.extendleft, fail()) 19383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 19483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_getitem(self): 19583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh n = 200 19683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(n)) 19783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh l = range(n) 19883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(n): 19983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.popleft() 20083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh l.pop(0) 20183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if random.random() < 0.5: 20283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(i) 20383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh l.append(i) 20483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for j in xrange(1-len(l), len(l)): 20583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh assert d[j] == l[j] 20683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 20783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('superman') 20883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d[0], 's') 20983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d[-1], 'n') 21083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 21183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.__getitem__, 0) 21283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.__getitem__, -1) 21383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 21483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_setitem(self): 21583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh n = 200 21683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(n)) 21783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(n): 21883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d[i] = 10 * i 21983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), [10*i for i in xrange(n)]) 22083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh l = list(d) 22183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(1-n, 0, -1): 22283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d[i] = 7*i 22383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh l[i] = 7*i 22483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), l) 22583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 22683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_delitem(self): 22783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh n = 500 # O(n**2) test, don't make this too big 22883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(n)) 22983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.__delitem__, -n-1) 23083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.__delitem__, n) 23183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(n): 23283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), n-i) 23383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh j = random.randrange(-len(d), len(d)) 23483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh val = d[j] 23583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertIn(val, d) 23683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh del d[j] 23783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotIn(val, d) 23883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 23983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 24083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_reverse(self): 24183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh n = 500 # O(n**2) test, don't make this too big 24283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh data = [random.random() for i in range(n)] 24383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in range(n): 24483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(data[:i]) 24583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh r = d.reverse() 24683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(reversed(data[:i]))) 24783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertIs(r, None) 24883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.reverse() 24983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), data[:i]) 25083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.reverse, 1) # Arity is zero 25183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 25283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_rotate(self): 25383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh s = tuple('abcde') 25483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh n = len(s) 25583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 25683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(s) 25783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(1) # verify rot(1) 25883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(''.join(d), 'eabcd') 25983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 26083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(s) 26183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(-1) # verify rot(-1) 26283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(''.join(d), 'bcdea') 26383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate() # check default to 1 26483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(d), s) 26583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 26683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(n*3): 26783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(s) 26883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = deque(d) 26983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(i) # check vs. rot(1) n times 27083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for j in xrange(i): 27183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e.rotate(1) 27283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(d), tuple(e)) 27383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(-i) # check that it works in reverse 27483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(d), s) 27583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e.rotate(n-i) # check that it wraps forward 27683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(e), s) 27783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 27883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(n*3): 27983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(s) 28083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = deque(d) 28183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(-i) 28283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for j in xrange(i): 28383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e.rotate(-1) # check vs. rot(-1) n times 28483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(d), tuple(e)) 28583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate(i) # check that it works in reverse 28683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(d), s) 28783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e.rotate(i-n) # check that it wraps backaround 28883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(e), s) 28983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 29083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(s) 29183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = deque(s) 29283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e.rotate(BIG+17) # verify on long series of rotates 29383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh dr = d.rotate 29483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG+17): 29583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh dr() 29683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(tuple(d), tuple(e)) 29783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 29883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.rotate, 'x') # Wrong arg type 29983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args 30083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 30183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 30283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.rotate() # rotate an empty deque 30383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d, deque()) 30483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 30583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_len(self): 30683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('ab') 30783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 2) 30883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.popleft() 30983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 1) 31083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.pop() 31183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 31283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.pop) 31383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 31483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append('c') 31583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 1) 31683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.appendleft('d') 31783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 2) 31883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.clear() 31983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 32083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 32183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_underflow(self): 32283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 32383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.pop) 32483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.popleft) 32583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 32683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_clear(self): 32783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(100)) 32883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 100) 32983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.clear() 33083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 33183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), []) 33283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.clear() # clear an emtpy deque 33383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), []) 33483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 33583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_remove(self): 33683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('abcdefghcij') 33783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.remove('c') 33883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d, deque('abdefghcij')) 33983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.remove('c') 34083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d, deque('abdefghij')) 34183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ValueError, d.remove, 'c') 34283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d, deque('abdefghij')) 34383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 34483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # Handle comparison errors 34583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(['a', 'b', BadCmp(), 'c']) 34683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = deque(d) 34783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(RuntimeError, d.remove, 'c') 34883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for x, y in zip(d, e): 34983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # verify that original order and values are retained. 35083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertTrue(x is y) 35183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 35283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # Handle evil mutator 35383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for match in (True, False): 35483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(['ab']) 35583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.extend([MutateCmp(d, match), 'c']) 35683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(IndexError, d.remove, 'c') 35783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(d, deque()) 35883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 35983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_repr(self): 36083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(200)) 36183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = eval(repr(d)) 36283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 36383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(d) 36483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertIn('...', repr(d)) 36583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 36683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_print(self): 36783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(200)) 36883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(d) 36983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.unlink(test_support.TESTFN) 37083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo = open(test_support.TESTFN, "wb") 37183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh try: 37283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh print >> fo, d, 37383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo.close() 37483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo = open(test_support.TESTFN, "rb") 37583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(fo.read(), repr(d)) 37683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh finally: 37783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh fo.close() 37883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.unlink(test_support.TESTFN) 37983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 38083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_init(self): 38183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, deque, 'abc', 2, 3); 38283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, deque, 1); 38383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 38483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_hash(self): 38583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, hash, deque('abc')) 38683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 38783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_long_steadystate_queue_popleft(self): 38883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for size in (0, 1, 2, 100, 1000): 38983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(size)) 39083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append, pop = d.append, d.popleft 39183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(size, BIG): 39283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append(i) 39383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh x = pop() 39483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if x != i - size: 39583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x, i-size) 39683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(BIG-size, BIG)) 39783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 39883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_long_steadystate_queue_popright(self): 39983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for size in (0, 1, 2, 100, 1000): 40083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(reversed(xrange(size))) 40183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append, pop = d.appendleft, d.pop 40283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(size, BIG): 40383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append(i) 40483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh x = pop() 40583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if x != i - size: 40683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x, i-size) 40783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(reversed(list(d))), range(BIG-size, BIG)) 40883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 40983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_big_queue_popleft(self): 41083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh pass 41183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 41283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append, pop = d.append, d.popleft 41383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG): 41483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append(i) 41583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG): 41683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh x = pop() 41783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if x != i: 41883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x, i) 41983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 42083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_big_queue_popright(self): 42183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 42283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append, pop = d.appendleft, d.pop 42383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG): 42483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append(i) 42583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG): 42683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh x = pop() 42783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if x != i: 42883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x, i) 42983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 43083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_big_stack_right(self): 43183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 43283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append, pop = d.append, d.pop 43383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG): 43483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append(i) 43583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in reversed(xrange(BIG)): 43683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh x = pop() 43783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if x != i: 43883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x, i) 43983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 44083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 44183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_big_stack_left(self): 44283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 44383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append, pop = d.appendleft, d.popleft 44483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(BIG): 44583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh append(i) 44683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in reversed(xrange(BIG)): 44783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh x = pop() 44883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if x != i: 44983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(x, i) 45083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 45183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 45283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_roundtrip_iter_init(self): 45383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(200)) 45483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = deque(d) 45583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(id(d), id(e)) 45683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 45783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 45883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_pickle(self): 45983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque(xrange(200)) 46083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in range(pickle.HIGHEST_PROTOCOL + 1): 46183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh s = pickle.dumps(d, i) 46283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = pickle.loads(s) 46383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(id(d), id(e)) 46483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 46583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 46683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## def test_pickle_recursive(self): 46783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## d = deque('abc') 46883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## d.append(d) 46983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## for i in range(pickle.HIGHEST_PROTOCOL + 1): 47083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## e = pickle.loads(pickle.dumps(d, i)) 47183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertNotEqual(id(d), id(e)) 47283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertEqual(id(e), id(e[-1])) 47383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 47483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_deepcopy(self): 47583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh mut = [10] 47683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque([mut]) 47783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = copy.deepcopy(d) 47883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 47983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh mut[0] = 11 48083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(id(d), id(e)) 48183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(list(d), list(e)) 48283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 48383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_copy(self): 48483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh mut = [10] 48583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque([mut]) 48683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = copy.copy(d) 48783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 48883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh mut[0] = 11 48983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(id(d), id(e)) 49083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 49183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 49283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_reversed(self): 49383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for s in ('abcd', xrange(2000)): 49483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(reversed(deque(s))), list(reversed(s))) 49583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 49683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_gc_doesnt_blowup(self): 49783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh import gc 49883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # This used to assert-fail in deque_traverse() under a debug 49983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # build, or run wild with a NULL pointer in a release build. 50083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 50183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(100): 50283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(1) 50383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh gc.collect() 50483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 50583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_container_iterator(self): 50683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # Bug #3680: tp_traverse was not implemented for deque iterator objects 50783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh class C(object): 50883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh pass 50983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in range(2): 51083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh obj = C() 51183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh ref = weakref.ref(obj) 51283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if i == 0: 51383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh container = deque([obj, 1]) 51483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh else: 51583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh container = reversed(deque([obj, 1])) 51683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh obj.x = iter(container) 51783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh del obj, container 51883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh gc.collect() 51983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertTrue(ref() is None, "Cycle was not collected") 52083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 52183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check_sizeof = test_support.check_sizeof 52283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 52383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh @test_support.cpython_only 52483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_sizeof(self): 52583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh BLOCKLEN = 62 52683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh basesize = test_support.calcobjsize('2P4PlP') 52783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh blocksize = struct.calcsize('2P%dP' % BLOCKLEN) 52883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(object.__sizeof__(deque()), basesize) 52983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check = self.check_sizeof 53083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check(deque(), basesize + blocksize) 53183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check(deque('a'), basesize + blocksize) 53283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check(deque('a' * (BLOCKLEN // 2)), basesize + blocksize) 53383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check(deque('a' * (BLOCKLEN // 2 + 1)), basesize + 2 * blocksize) 53483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize) 53583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 53683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass TestVariousIteratorArgs(unittest.TestCase): 53783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 53883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_constructor(self): 53983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): 54083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for g in (seq_tests.Sequence, seq_tests.IterFunc, 54183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh seq_tests.IterGen, seq_tests.IterFuncStop, 54283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh seq_tests.itermulti, seq_tests.iterfunc): 54383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(deque(g(s))), list(g(s))) 54483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, deque, seq_tests.IterNextOnly(s)) 54583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(TypeError, deque, seq_tests.IterNoNext(s)) 54683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ZeroDivisionError, deque, seq_tests.IterGenExc(s)) 54783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 54883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_iter_with_altered_data(self): 54983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('abcdefg') 55083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh it = iter(d) 55183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.pop() 55283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(RuntimeError, it.next) 55383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 55483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_runtime_error_on_empty_deque(self): 55583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque() 55683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh it = iter(d) 55783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(10) 55883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(RuntimeError, it.next) 55983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 56083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass Deque(deque): 56183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh pass 56283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 56383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass DequeWithBadIter(deque): 56483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __iter__(self): 56583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh raise TypeError 56683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 56783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass TestSubclass(unittest.TestCase): 56883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 56983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_basics(self): 57083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = Deque(xrange(25)) 57183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.__init__(xrange(200)) 57283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(200, 400): 57383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.append(i) 57483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in reversed(xrange(-200, 0)): 57583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.appendleft(i) 57683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(-200, 400)) 57783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 600) 57883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 57983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh left = [d.popleft() for i in xrange(250)] 58083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(left, range(-200, 50)) 58183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(50, 400)) 58283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 58383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh right = [d.pop() for i in xrange(250)] 58483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh right.reverse() 58583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(right, range(150, 400)) 58683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), range(50, 150)) 58783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 58883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.clear() 58983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(len(d), 0) 59083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 59183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_copy_pickle(self): 59283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 59383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = Deque('abc') 59483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 59583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = d.__copy__() 59683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(type(d), type(e)) 59783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 59883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 59983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = Deque(d) 60083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(type(d), type(e)) 60183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 60283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 60383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh s = pickle.dumps(d) 60483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = pickle.loads(s) 60583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(id(d), id(e)) 60683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(type(d), type(e)) 60783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 60883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 60983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = Deque('abcde', maxlen=4) 61083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 61183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = d.__copy__() 61283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(type(d), type(e)) 61383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 61483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 61583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = Deque(d) 61683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(type(d), type(e)) 61783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 61883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 61983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh s = pickle.dumps(d) 62083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh e = pickle.loads(s) 62183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertNotEqual(id(d), id(e)) 62283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(type(d), type(e)) 62383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(list(d), list(e)) 62483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 62583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## def test_pickle(self): 62683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## d = Deque('abc') 62783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## d.append(d) 62883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## 62983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## e = pickle.loads(pickle.dumps(d)) 63083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertNotEqual(id(d), id(e)) 63183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertEqual(type(d), type(e)) 63283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## dd = d.pop() 63383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## ee = e.pop() 63483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertEqual(id(e), id(ee)) 63583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertEqual(d, e) 63683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## 63783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## d.x = d 63883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## e = pickle.loads(pickle.dumps(d)) 63983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertEqual(id(e), id(e.x)) 64083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## 64183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## d = DequeWithBadIter('abc') 64283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh## self.assertRaises(TypeError, pickle.dumps, d) 64383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 64483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_weakref(self): 64583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = deque('gallahad') 64683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh p = weakref.proxy(d) 64783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertEqual(str(p), str(d)) 64883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d = None 64983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh self.assertRaises(ReferenceError, str, p) 65083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 65183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_strange_subclass(self): 65283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh class X(deque): 65383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __iter__(self): 65483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh return iter([]) 65583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d1 = X([1,2,3]) 65683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d2 = X([4,5,6]) 65783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d1 == d2 # not clear if this is supposed to be True or False, 65883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # but it used to give a SystemError 65983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 66083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 66183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass SubclassWithKwargs(deque): 66283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def __init__(self, newarg=1): 66383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh deque.__init__(self) 66483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 66583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass TestSubclassWithKwargs(unittest.TestCase): 66683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh def test_subclass_with_kwargs(self): 66783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # SF bug #1486663 -- this used to erroneously raise a TypeError 66883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh SubclassWithKwargs(newarg=1) 66983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 67083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh#============================================================================== 67183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 67283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehlibreftest = """ 67383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehExample from the Library Reference: Doc/lib/libcollections.tex 67483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 67583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> from collections import deque 67683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d = deque('ghi') # make a new deque with three items 67783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> for elem in d: # iterate over the deque's elements 67883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... print elem.upper() 67983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehG 68083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehH 68183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehI 68283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.append('j') # add a new entry to the right side 68383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.appendleft('f') # add a new entry to the left side 68483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d # show the representation of the deque 68583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['f', 'g', 'h', 'i', 'j']) 68683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.pop() # return and remove the rightmost item 68783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh'j' 68883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.popleft() # return and remove the leftmost item 68983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh'f' 69083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> list(d) # list the contents of the deque 69183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh['g', 'h', 'i'] 69283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d[0] # peek at leftmost item 69383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh'g' 69483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d[-1] # peek at rightmost item 69583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh'i' 69683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> list(reversed(d)) # list the contents of a deque in reverse 69783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh['i', 'h', 'g'] 69883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> 'h' in d # search the deque 69983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehTrue 70083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.extend('jkl') # add multiple elements at once 70183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d 70283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['g', 'h', 'i', 'j', 'k', 'l']) 70383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.rotate(1) # right rotation 70483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d 70583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['l', 'g', 'h', 'i', 'j', 'k']) 70683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.rotate(-1) # left rotation 70783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d 70883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['g', 'h', 'i', 'j', 'k', 'l']) 70983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> deque(reversed(d)) # make a new deque in reverse order 71083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['l', 'k', 'j', 'i', 'h', 'g']) 71183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.clear() # empty the deque 71283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.pop() # cannot pop from an empty deque 71383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehTraceback (most recent call last): 71483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh File "<pyshell#6>", line 1, in -toplevel- 71583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh d.pop() 71683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew HsiehIndexError: pop from an empty deque 71783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 71883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d.extendleft('abc') # extendleft() reverses the input order 71983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d 72083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['c', 'b', 'a']) 72183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 72283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 72383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 72483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> def delete_nth(d, n): 72583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... d.rotate(-n) 72683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... d.popleft() 72783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... d.rotate(n) 72883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... 72983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d = deque('abcdef') 73083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> delete_nth(d, 2) # remove the entry at d[2] 73183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> d 73283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdeque(['a', 'b', 'd', 'e', 'f']) 73383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 73483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 73583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 73683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> def roundrobin(*iterables): 73783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... pending = deque(iter(i) for i in iterables) 73883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... while pending: 73983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... task = pending.popleft() 74083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... try: 74183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... yield task.next() 74283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... except StopIteration: 74383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... continue 74483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... pending.append(task) 74583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... 74683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 74783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> for value in roundrobin('abc', 'd', 'efgh'): 74883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... print value 74983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... 75083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieha 75183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehd 75283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehe 75383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehb 75483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehf 75583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehc 75683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehg 75783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehh 75883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 75983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 76083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> def maketree(iterable): 76183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... d = deque(iterable) 76283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... while len(d) > 1: 76383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... pair = [d.popleft(), d.popleft()] 76483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... d.append(pair) 76583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... return list(d) 76683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh... 76783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh>>> print maketree('abcdefgh') 76883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] 76983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 77083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh""" 77183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 77283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 77383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh#============================================================================== 77483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 77583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh__test__ = {'libreftest' : libreftest} 77683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 77783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdef test_main(verbose=None): 77883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh import sys 77983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_classes = ( 78083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh TestBasic, 78183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh TestVariousIteratorArgs, 78283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh TestSubclass, 78383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh TestSubclassWithKwargs, 78483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh ) 78583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 78683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.run_unittest(*test_classes) 78783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 78883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # verify reference counting 78983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh if verbose and hasattr(sys, "gettotalrefcount"): 79083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh import gc 79183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh counts = [None] * 5 79283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh for i in xrange(len(counts)): 79383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.run_unittest(*test_classes) 79483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh gc.collect() 79583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh counts[i] = sys.gettotalrefcount() 79683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh print counts 79783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 79883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh # doctests 79983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh from test import test_deque 80083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_support.run_doctest(test_deque, verbose) 80183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh 80283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehif __name__ == "__main__": 80383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh test_main(verbose=True) 804