[Python-checkins] CVS: python/dist/src/Lib/test test_weakref.py,1.7,1.8

Fred L. Drake fdrake@users.sourceforge.net
Tue, 01 May 2001 22:44:24 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv28623

Modified Files:
	test_weakref.py 
Log Message:

Added tests for Weak*Dictionary iterator support.

Refactored some object initialization to be more reusable.


Index: test_weakref.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_weakref.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** test_weakref.py	2001/04/16 17:37:27	1.7
--- test_weakref.py	2001/05/02 05:44:22	1.8
***************
*** 230,239 ****
  
      def test_weak_values(self):
!         dict = weakref.WeakValueDictionary()
!         objects = map(Object, range(self.COUNT))
          for o in objects:
-             dict[o.arg] = o
- 
-         for o in objects:
              self.assert_(weakref.getweakrefcount(o) == 1,
                           "wrong number of weak references to %r!" % o)
--- 230,238 ----
  
      def test_weak_values(self):
!         #
!         #  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)
***************
*** 256,265 ****
  
      def test_weak_keys(self):
!         dict = weakref.WeakKeyDictionary()
!         objects = map(Object, range(self.COUNT))
          for o in objects:
-             dict[o] = o.arg
- 
-         for o in objects:
              self.assert_(weakref.getweakrefcount(o) == 1,
                           "wrong number of weak references to %r!" % o)
--- 255,264 ----
  
      def test_weak_keys(self):
!         #
!         #  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)
***************
*** 281,285 ****
--- 280,329 ----
                       "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)