Kaydet (Commit) a435c53e authored tarafından Raymond Hettinger's avatar Raymond Hettinger

* balance the left/right search for getitem.

* use assertions instead of tests after internal calls that can't fail.
* expand test coverage
üst 2b2795ac
...@@ -6,9 +6,14 @@ import copy ...@@ -6,9 +6,14 @@ import copy
import cPickle as pickle import cPickle as pickle
from cStringIO import StringIO from cStringIO import StringIO
import random import random
import os
BIG = 100000 BIG = 100000
def fail():
raise SyntaxError
yield 1
class TestBasic(unittest.TestCase): class TestBasic(unittest.TestCase):
def test_basics(self): def test_basics(self):
...@@ -58,6 +63,10 @@ class TestBasic(unittest.TestCase): ...@@ -58,6 +63,10 @@ class TestBasic(unittest.TestCase):
self.assertRaises(TypeError, d.extendleft, 1) self.assertRaises(TypeError, d.extendleft, 1)
d.extendleft('bcd') d.extendleft('bcd')
self.assertEqual(list(d), list(reversed('abcd'))) self.assertEqual(list(d), list(reversed('abcd')))
d = deque()
d.extendleft(range(1000))
self.assertEqual(list(d), list(reversed(range(1000))))
self.assertRaises(SyntaxError, d.extendleft, fail())
def test_getitem(self): def test_getitem(self):
n = 200 n = 200
...@@ -151,6 +160,13 @@ class TestBasic(unittest.TestCase): ...@@ -151,6 +160,13 @@ class TestBasic(unittest.TestCase):
dr() dr()
self.assertEqual(tuple(d), tuple(e)) self.assertEqual(tuple(d), tuple(e))
self.assertRaises(TypeError, d.rotate, 'x') # Wrong arg type
self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args
d = deque()
d.rotate() # rotate an empty deque
self.assertEqual(d, deque())
def test_len(self): def test_len(self):
d = deque('ab') d = deque('ab')
self.assertEqual(len(d), 2) self.assertEqual(len(d), 2)
...@@ -178,6 +194,8 @@ class TestBasic(unittest.TestCase): ...@@ -178,6 +194,8 @@ class TestBasic(unittest.TestCase):
d.clear() d.clear()
self.assertEqual(len(d), 0) self.assertEqual(len(d), 0)
self.assertEqual(list(d), []) self.assertEqual(list(d), [])
d.clear() # clear an emtpy deque
self.assertEqual(list(d), [])
def test_repr(self): def test_repr(self):
d = deque(xrange(200)) d = deque(xrange(200))
...@@ -189,10 +207,19 @@ class TestBasic(unittest.TestCase): ...@@ -189,10 +207,19 @@ class TestBasic(unittest.TestCase):
def test_print(self): def test_print(self):
d = deque(xrange(200)) d = deque(xrange(200))
d.append(d) d.append(d)
f = StringIO() try:
print >> f, d, fo = open(test_support.TESTFN, "wb")
self.assertEqual(f.getvalue(), repr(d)) print >> fo, d,
f.close() fo.close()
fo = open(test_support.TESTFN, "rb")
self.assertEqual(fo.read(), repr(d))
finally:
fo.close()
os.remove(test_support.TESTFN)
def test_init(self):
self.assertRaises(TypeError, deque, 'abc', 2);
self.assertRaises(TypeError, deque, 1);
def test_hash(self): def test_hash(self):
self.assertRaises(TypeError, hash, deque('abc')) self.assertRaises(TypeError, hash, deque('abc'))
......
...@@ -245,7 +245,7 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) ...@@ -245,7 +245,7 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
deque->leftblock->data[deque->leftindex] = item; deque->leftblock->data[deque->leftindex] = item;
} }
Py_DECREF(it); Py_DECREF(it);
if (PyErr_Occurred()) if (PyErr_Occurred())
return NULL; return NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
...@@ -274,8 +274,7 @@ deque_rotate(dequeobject *deque, PyObject *args) ...@@ -274,8 +274,7 @@ deque_rotate(dequeobject *deque, PyObject *args)
for (i=0 ; i<n ; i++) { for (i=0 ; i<n ; i++) {
item = deque_pop(deque, NULL); item = deque_pop(deque, NULL);
if (item == NULL) assert (item != NULL);
return NULL;
rv = deque_appendleft(deque, item); rv = deque_appendleft(deque, item);
Py_DECREF(item); Py_DECREF(item);
if (rv == NULL) if (rv == NULL)
...@@ -284,8 +283,7 @@ deque_rotate(dequeobject *deque, PyObject *args) ...@@ -284,8 +283,7 @@ deque_rotate(dequeobject *deque, PyObject *args)
} }
for (i=0 ; i>n ; i--) { for (i=0 ; i>n ; i--) {
item = deque_popleft(deque, NULL); item = deque_popleft(deque, NULL);
if (item == NULL) assert (item != NULL);
return NULL;
rv = deque_append(deque, item); rv = deque_append(deque, item);
Py_DECREF(item); Py_DECREF(item);
if (rv == NULL) if (rv == NULL)
...@@ -311,8 +309,7 @@ deque_clear(dequeobject *deque) ...@@ -311,8 +309,7 @@ deque_clear(dequeobject *deque)
while (deque_len(deque)) { while (deque_len(deque)) {
item = deque_pop(deque, NULL); item = deque_pop(deque, NULL);
if (item == NULL) assert (item != NULL);
return -1;
Py_DECREF(item); Py_DECREF(item);
} }
assert(deque->leftblock == deque->rightblock && assert(deque->leftblock == deque->rightblock &&
...@@ -388,8 +385,7 @@ deque_del_item(dequeobject *deque, int i) ...@@ -388,8 +385,7 @@ deque_del_item(dequeobject *deque, int i)
Py_DECREF(item); Py_DECREF(item);
item = deque_popleft(deque, NULL); item = deque_popleft(deque, NULL);
if (item == NULL) assert (item != NULL);
goto fail;
Py_DECREF(item); Py_DECREF(item);
item = deque_rotate(deque, plus_i); item = deque_rotate(deque, plus_i);
...@@ -409,9 +405,9 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) ...@@ -409,9 +405,9 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v)
{ {
PyObject *old_value; PyObject *old_value;
block *b; block *b;
int n; int n, len=deque->len, halflen=(len+1)>>1, index=i;
if (i < 0 || i >= deque->len) { if (i < 0 || i >= len) {
PyErr_SetString(PyExc_IndexError, PyErr_SetString(PyExc_IndexError,
"deque index out of range"); "deque index out of range");
return -1; return -1;
...@@ -422,12 +418,12 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) ...@@ -422,12 +418,12 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v)
i += deque->leftindex; i += deque->leftindex;
n = i / BLOCKLEN; n = i / BLOCKLEN;
i %= BLOCKLEN; i %= BLOCKLEN;
if (i < (deque->len >> 1)) { if (index <= halflen) {
b = deque->leftblock; b = deque->leftblock;
while (n--) while (n--)
b = b->rightlink; b = b->rightlink;
} else { } else {
n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; n = (deque->leftindex + len - 1) / BLOCKLEN - n;
b = deque->rightblock; b = deque->rightblock;
while (n--) while (n--)
b = b->leftlink; b = b->leftlink;
...@@ -442,8 +438,10 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) ...@@ -442,8 +438,10 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v)
static PyObject * static PyObject *
deque_clearmethod(dequeobject *deque) deque_clearmethod(dequeobject *deque)
{ {
if (deque_clear(deque) == -1) int rv;
return NULL;
rv = deque_clear(deque);
assert (rv != -1);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment