diff options
author | Fred Drake <fdrake@acm.org> | 2001-05-02 05:44:22 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-05-02 05:44:22 (GMT) |
commit | 0e540c391f5d2af051ec7f1ef3edb4d1bad3fa91 (patch) | |
tree | 8b03cc334c729efa077a4f4ca2ac9a4743fe2dd6 /Lib/test | |
parent | 101209d44ce063cd04fd6793d94991f8e6688428 (diff) | |
download | cpython-0e540c391f5d2af051ec7f1ef3edb4d1bad3fa91.zip cpython-0e540c391f5d2af051ec7f1ef3edb4d1bad3fa91.tar.gz cpython-0e540c391f5d2af051ec7f1ef3edb4d1bad3fa91.tar.bz2 |
Added tests for Weak*Dictionary iterator support.
Refactored some object initialization to be more reusable.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_weakref.py | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 470c5ca..643948f 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -229,11 +229,10 @@ class MappingTestCase(TestBase): COUNT = 10 def test_weak_values(self): - dict = weakref.WeakValueDictionary() - objects = map(Object, range(self.COUNT)) - for o in objects: - dict[o.arg] = o - + # + # This exercises d.copy(), d.items(), d[], del d[], len(d). + # + dict, objects = self.make_weak_valued_dict() for o in objects: self.assert_(weakref.getweakrefcount(o) == 1, "wrong number of weak references to %r!" % o) @@ -255,11 +254,11 @@ class MappingTestCase(TestBase): "deleting the values did not clear the dictionary") def test_weak_keys(self): - dict = weakref.WeakKeyDictionary() - objects = map(Object, range(self.COUNT)) - for o in objects: - dict[o] = o.arg - + # + # This exercises d.copy(), d.items(), d[] = v, d[], del d[], + # len(d). + # + dict, objects = self.make_weak_keyed_dict() for o in objects: self.assert_(weakref.getweakrefcount(o) == 1, "wrong number of weak references to %r!" % o) @@ -280,7 +279,52 @@ class MappingTestCase(TestBase): self.assert_(len(dict) == 0, "deleting the keys did not clear the dictionary") + def test_weak_keyed_iters(self): + dict, objects = self.make_weak_keyed_dict() + self.check_iters(dict) + + def test_weak_valued_iters(self): + dict, objects = self.make_weak_valued_dict() + self.check_iters(dict) + + def check_iters(self, dict): + # item iterator: + items = dict.items() + for item in dict.iteritems(): + items.remove(item) + self.assert_(len(items) == 0, "iterator did not touch all items") + + # key iterator: + keys = dict.keys() + for k in dict: + keys.remove(k) + self.assert_(len(keys) == 0, "iterator did not touch all keys") + + # value iterator: + values = dict.values() + for v in dict.itervalues(): + values.remove(v) + self.assert_(len(values) == 0, "iterator did not touch all values") + + def make_weak_keyed_dict(self): + dict = weakref.WeakKeyDictionary() + objects = map(Object, range(self.COUNT)) + for o in objects: + dict[o] = o.arg + return dict, objects + + def make_weak_valued_dict(self): + dict = weakref.WeakValueDictionary() + objects = map(Object, range(self.COUNT)) + for o in objects: + dict[o.arg] = o + return dict, objects + def check_update(self, klass, dict): + # + # This exercises d.update(), len(d), d.keys(), d.has_key(), + # d.get(), d[]. + # weakdict = klass() weakdict.update(dict) self.assert_(len(weakdict) == len(dict)) |