[pypy-svn] r31346 - in pypy/dist/pypy/objspace/std: . test
benyoung at codespeak.net
benyoung at codespeak.net
Wed Aug 16 14:30:51 CEST 2006
Author: benyoung
Date: Wed Aug 16 14:30:36 2006
New Revision: 31346
Modified:
pypy/dist/pypy/objspace/std/dictmultiobject.py
pypy/dist/pypy/objspace/std/test/test_dictmultiobject.py
Log:
Added some tests for the implementations + some dict fixes
Modified: pypy/dist/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictmultiobject.py (original)
+++ pypy/dist/pypy/objspace/std/dictmultiobject.py Wed Aug 16 14:30:36 2006
@@ -111,14 +111,17 @@
self.valid += 1
entry.w_value = w_value
return self
+
def delitem(self, w_key):
entry = self._lookup(w_key)
- if entry.w_value:
+ if entry.w_value is not None:
for i in range(self.entries.index(entry), self.valid):
self.entries[i] = self.entries[i+1]
self.entries[self.valid] = entry
entry.w_value = None
self.valid -= 1
+ if self.valid == 0:
+ return self.space.emptydictimpl
return self
else:
raise KeyError
@@ -236,7 +239,7 @@
def items(self):
space = self.space
- return [(space.wrap(key), w_value) for (key, w_value) in self.content.iterkeys()]
+ return [(space.wrap(key), w_value) for (key, w_value) in self.content.iteritems()]
def _is_sane_hash(self, w_lookup_type):
""" Handles the case of a non string key lookup.
Modified: pypy/dist/pypy/objspace/std/test/test_dictmultiobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_dictmultiobject.py (original)
+++ pypy/dist/pypy/objspace/std/test/test_dictmultiobject.py Wed Aug 16 14:30:36 2006
@@ -1,7 +1,7 @@
import autopath
from pypy.objspace.std.dictmultiobject import \
W_DictMultiObject, setitem__DictMulti_ANY_ANY, getitem__DictMulti_ANY, \
- EmptyDictImplementation
+ EmptyDictImplementation, RDictImplementation, StrDictImplementation, SmallDictImplementation
from pypy.conftest import gettestobjspace
from pypy.objspace.std.test import test_dictobject
@@ -13,9 +13,17 @@
def setup_class(cls):
cls.space = gettestobjspace(**{"objspace.std.withmultidict": True})
+class FakeSpace(test_dictobject.FakeSpace):
+ def str_w(self, string):
+ assert isinstance(string, str)
+ return string
+
+ def wrap(self, obj):
+ return obj
+
class TestDictImplementation:
def setup_method(self,method):
- self.space = test_dictobject.FakeSpace()
+ self.space = FakeSpace()
self.space.emptydictimpl = EmptyDictImplementation(self.space)
self.space.DictObjectCls = W_DictMultiObject
@@ -30,3 +38,65 @@
pydict[x] = i
for x in pydict:
assert pydict[x] == getitem__DictMulti_ANY(self.space, d, x)
+
+class TestRDictImplementation:
+ ImplementionClass = RDictImplementation
+
+ def setup_method(self,method):
+ self.space = FakeSpace()
+ self.space.emptydictimpl = EmptyDictImplementation(self.space)
+ self.space.DictObjectCls = W_DictMultiObject
+ self.string = self.space.str_w("fish")
+ self.string2 = self.space.str_w("fish2")
+ self.impl = self.get_impl()
+
+ def get_impl(self):
+ return self.ImplementionClass(self.space)
+
+ def test_setitem(self):
+ assert self.impl.setitem(self.string, 1000) is self.impl
+ assert self.impl.get(self.string) == 1000
+
+ def test_delitem(self):
+ self.impl.setitem(self.string, 1000)
+ self.impl.setitem(self.string2, 2000)
+ assert self.impl.delitem(self.string) is self.impl
+ assert self.impl.delitem(self.string2) is self.space.emptydictimpl
+
+ def test_keys(self):
+ self.impl.setitem(self.string, 1000)
+ self.impl.setitem(self.string2, 2000)
+ keys = self.impl.keys()
+ keys.sort()
+ assert keys == [self.string, self.string2]
+
+ def test_values(self):
+ self.impl.setitem(self.string, 1000)
+ self.impl.setitem(self.string2, 2000)
+ values = self.impl.values()
+ values.sort()
+ assert values == [1000, 2000]
+
+ def test_items(self):
+ self.impl.setitem(self.string, 1000)
+ self.impl.setitem(self.string2, 2000)
+ items = self.impl.items()
+ items.sort()
+ assert items == zip([self.string, self.string2], [1000, 2000])
+
+ def test_devolve(self):
+ impl = self.impl
+ for x in xrange(100):
+ impl = impl.setitem(self.space.str_w(str(x)), x)
+ impl = impl.setitem(x, x)
+ assert isinstance(impl, RDictImplementation)
+
+class TestStrDictImplementation(TestRDictImplementation):
+ ImplementionClass = StrDictImplementation
+
+class TestSmallDictImplementation(TestRDictImplementation):
+ ImplementionClass = SmallDictImplementation
+
+ def get_impl(self):
+ return self.ImplementionClass(self.space, self.string, self.string2)
+
More information about the Pypy-commit
mailing list