[Python-checkins] python/dist/src/Lib/test test_sets.py, 1.24, 1.24.8.1

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sun Aug 17 16:09:00 EDT 2003


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

Modified Files:
      Tag: release23-maint
	test_sets.py 
Log Message:
Backport improvements to set.py so that the interface will remain
consistent across versions. 

* Relaxed the argument restrictions for non-operator methods.  They now
  allow any iterable instead of requiring a set.  This makes the module
  a little easier to use and paves the way for an efficient C 
  implementation which can take better advantage of iterable arguments
  while screening out immutables.

* Added a PendingDeprecationWarning for Set.update() because it now 
  duplicates Set.union_update()

* Adapted the tests and docs to include the above changes.

* Added more test coverage including testing identities and checking
  to make sure non-restartable generators work as arguments.



Index: test_sets.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_sets.py,v
retrieving revision 1.24
retrieving revision 1.24.8.1
diff -C2 -d -r1.24 -r1.24.8.1
*** test_sets.py	1 May 2003 17:45:48 -0000	1.24
--- test_sets.py	17 Aug 2003 22:08:58 -0000	1.24.8.1
***************
*** 153,157 ****
  
      def test_instancesWithoutException(self):
!         """All of these iterables should load without exception."""
          Set([1,2,3])
          Set((1,2,3))
--- 153,157 ----
  
      def test_instancesWithoutException(self):
!         # All of these iterables should load without exception.
          Set([1,2,3])
          Set((1,2,3))
***************
*** 393,405 ****
  
      def test_update_empty_tuple(self):
!         self.set.update(())
          self.assertEqual(self.set, Set(self.values))
  
      def test_update_unit_tuple_overlap(self):
!         self.set.update(("a",))
          self.assertEqual(self.set, Set(self.values))
  
      def test_update_unit_tuple_non_overlap(self):
!         self.set.update(("a", "z"))
          self.assertEqual(self.set, Set(self.values + ["z"]))
  
--- 393,405 ----
  
      def test_update_empty_tuple(self):
!         self.set.union_update(())
          self.assertEqual(self.set, Set(self.values))
  
      def test_update_unit_tuple_overlap(self):
!         self.set.union_update(("a",))
          self.assertEqual(self.set, Set(self.values))
  
      def test_update_unit_tuple_non_overlap(self):
!         self.set.union_update(("a", "z"))
          self.assertEqual(self.set, Set(self.values + ["z"]))
  
***************
*** 504,508 ****
          self.assertRaises(TypeError, lambda: self.other >= self.set)
  
!     def test_union_update(self):
          try:
              self.set |= self.other
--- 504,508 ----
          self.assertRaises(TypeError, lambda: self.other >= self.set)
  
!     def test_union_update_operator(self):
          try:
              self.set |= self.other
***************
*** 512,520 ****
              self.fail("expected TypeError")
  
      def test_union(self):
          self.assertRaises(TypeError, lambda: self.set | self.other)
          self.assertRaises(TypeError, lambda: self.other | self.set)
  
!     def test_intersection_update(self):
          try:
              self.set &= self.other
--- 512,530 ----
              self.fail("expected TypeError")
  
+     def test_union_update(self):
+         if self.otherIsIterable:
+             self.set.union_update(self.other)
+         else:
+             self.assertRaises(TypeError, self.set.union_update, self.other)
+ 
      def test_union(self):
          self.assertRaises(TypeError, lambda: self.set | self.other)
          self.assertRaises(TypeError, lambda: self.other | self.set)
+         if self.otherIsIterable:
+             self.set.union(self.other)
+         else:
+             self.assertRaises(TypeError, self.set.union, self.other)
  
!     def test_intersection_update_operator(self):
          try:
              self.set &= self.other
***************
*** 524,532 ****
              self.fail("expected TypeError")
  
      def test_intersection(self):
          self.assertRaises(TypeError, lambda: self.set & self.other)
          self.assertRaises(TypeError, lambda: self.other & self.set)
  
!     def test_sym_difference_update(self):
          try:
              self.set ^= self.other
--- 534,554 ----
              self.fail("expected TypeError")
  
+     def test_intersection_update(self):
+         if self.otherIsIterable:
+             self.set.intersection_update(self.other)
+         else:
+             self.assertRaises(TypeError,
+                               self.set.intersection_update,
+                               self.other)
+ 
      def test_intersection(self):
          self.assertRaises(TypeError, lambda: self.set & self.other)
          self.assertRaises(TypeError, lambda: self.other & self.set)
+         if self.otherIsIterable:
+             self.set.intersection(self.other)
+         else:
+             self.assertRaises(TypeError, self.set.intersection, self.other)
  
!     def test_sym_difference_update_operator(self):
          try:
              self.set ^= self.other
***************
*** 536,544 ****
              self.fail("expected TypeError")
  
      def test_sym_difference(self):
          self.assertRaises(TypeError, lambda: self.set ^ self.other)
          self.assertRaises(TypeError, lambda: self.other ^ self.set)
  
!     def test_difference_update(self):
          try:
              self.set -= self.other
--- 558,578 ----
              self.fail("expected TypeError")
  
+     def test_sym_difference_update(self):
+         if self.otherIsIterable:
+             self.set.symmetric_difference_update(self.other)
+         else:
+             self.assertRaises(TypeError,
+                               self.set.symmetric_difference_update,
+                               self.other)
+ 
      def test_sym_difference(self):
          self.assertRaises(TypeError, lambda: self.set ^ self.other)
          self.assertRaises(TypeError, lambda: self.other ^ self.set)
+         if self.otherIsIterable:
+             self.set.symmetric_difference(self.other)
+         else:
+             self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
  
!     def test_difference_update_operator(self):
          try:
              self.set -= self.other
***************
*** 548,555 ****
              self.fail("expected TypeError")
  
      def test_difference(self):
          self.assertRaises(TypeError, lambda: self.set - self.other)
          self.assertRaises(TypeError, lambda: self.other - self.set)
! 
  #------------------------------------------------------------------------------
  
--- 582,600 ----
              self.fail("expected TypeError")
  
+     def test_difference_update(self):
+         if self.otherIsIterable:
+             self.set.difference_update(self.other)
+         else:
+             self.assertRaises(TypeError,
+                               self.set.difference_update,
+                               self.other)
+ 
      def test_difference(self):
          self.assertRaises(TypeError, lambda: self.set - self.other)
          self.assertRaises(TypeError, lambda: self.other - self.set)
!         if self.otherIsIterable:
!             self.set.difference(self.other)
!         else:
!             self.assertRaises(TypeError, self.set.difference, self.other)
  #------------------------------------------------------------------------------
  
***************
*** 558,561 ****
--- 603,607 ----
          self.set   = Set((1, 2, 3))
          self.other = 19
+         self.otherIsIterable = False
  
  #------------------------------------------------------------------------------
***************
*** 565,568 ****
--- 611,615 ----
          self.set   = Set((1, 2, 3))
          self.other = {1:2, 3:4}
+         self.otherIsIterable = True
  
  #------------------------------------------------------------------------------
***************
*** 572,575 ****
--- 619,650 ----
          self.set   = Set((1, 2, 3))
          self.other = operator.add
+         self.otherIsIterable = False
+ 
+ #------------------------------------------------------------------------------
+ 
+ class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
+     def setUp(self):
+         self.set   = Set((1, 2, 3))
+         self.other = (2, 4, 6)
+         self.otherIsIterable = True
+ 
+ #------------------------------------------------------------------------------
+ 
+ class TestOnlySetsString(TestOnlySetsInBinaryOps):
+     def setUp(self):
+         self.set   = Set((1, 2, 3))
+         self.other = 'abc'
+         self.otherIsIterable = True
+ 
+ #------------------------------------------------------------------------------
+ 
+ class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
+     def setUp(self):
+         def gen():
+             for i in xrange(0, 10, 2):
+                 yield i
+         self.set   = Set((1, 2, 3))
+         self.other = gen()
+         self.otherIsIterable = True
  
  #==============================================================================
***************
*** 626,629 ****
--- 701,747 ----
  #==============================================================================
  
+ class TestIdentities(unittest.TestCase):
+     def setUp(self):
+         self.a = Set('abracadabra')
+         self.b = Set('alacazam')
+ 
+     def test_binopsVsSubsets(self):
+         a, b = self.a, self.b
+         self.assert_(a - b < a)
+         self.assert_(b - a < b)
+         self.assert_(a & b < a)
+         self.assert_(a & b < b)
+         self.assert_(a | b > a)
+         self.assert_(a | b > b)
+         self.assert_(a ^ b < a | b)
+ 
+     def test_commutativity(self):
+         a, b = self.a, self.b
+         self.assertEqual(a&b, b&a)
+         self.assertEqual(a|b, b|a)
+         self.assertEqual(a^b, b^a)
+         if a != b:
+             self.assertNotEqual(a-b, b-a)
+ 
+     def test_summations(self):
+         # check that sums of parts equal the whole
+         a, b = self.a, self.b
+         self.assertEqual((a-b)|(a&b)|(b-a), a|b)
+         self.assertEqual((a&b)|(a^b), a|b)
+         self.assertEqual(a|(b-a), a|b)
+         self.assertEqual((a-b)|b, a|b)
+         self.assertEqual((a-b)|(a&b), a)
+         self.assertEqual((b-a)|(a&b), b)
+         self.assertEqual((a-b)|(b-a), a^b)
+ 
+     def test_exclusion(self):
+         # check that inverse operations show non-overlap
+         a, b, zero = self.a, self.b, Set()
+         self.assertEqual((a-b)&b, zero)
+         self.assertEqual((b-a)&a, zero)
+         self.assertEqual((a&b)&(a^b), zero)
+ 
+ #==============================================================================
+ 
  libreftest = """
  Example from the Library Reference:  Doc/lib/libsets.tex
***************
*** 635,642 ****
  >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
  >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
! >>> management = Set(['Jane', 'Jack', 'Susan', 'Zack'])
! >>> employees = engineers | programmers | management           # union
! >>> engineering_management = engineers & programmers           # intersection
! >>> fulltime_management = management - engineers - programmers # difference
  >>> engineers.add('Marvin')
  >>> print engineers
--- 753,760 ----
  >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
  >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
! >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
! >>> employees = engineers | programmers | managers           # union
! >>> engineering_management = engineers & managers            # intersection
! >>> fulltime_management = managers - engineers - programmers # difference
  >>> engineers.add('Marvin')
  >>> print engineers
***************
*** 644,651 ****
  >>> employees.issuperset(engineers)           # superset test
  False
! >>> employees.update(engineers)               # update from another set
  >>> employees.issuperset(engineers)
  True
! >>> for group in [engineers, programmers, management, employees]:
  ...     group.discard('Susan')                # unconditionally remove element
  ...     print group
--- 762,769 ----
  >>> employees.issuperset(engineers)           # superset test
  False
! >>> employees.union_update(engineers)         # update from another set
  >>> employees.issuperset(engineers)
  True
! >>> for group in [engineers, programmers, managers, employees]:
  ...     group.discard('Susan')                # unconditionally remove element
  ...     print group
***************
*** 681,689 ****
          TestOnlySetsDict,
          TestOnlySetsOperator,
          TestCopyingEmpty,
          TestCopyingSingleton,
          TestCopyingTriple,
          TestCopyingTuple,
!         TestCopyingNested
      )
      test_support.run_doctest(test_sets, verbose)
--- 799,811 ----
          TestOnlySetsDict,
          TestOnlySetsOperator,
+         TestOnlySetsTuple,
+         TestOnlySetsString,
+         TestOnlySetsGenerator,
          TestCopyingEmpty,
          TestCopyingSingleton,
          TestCopyingTriple,
          TestCopyingTuple,
!         TestCopyingNested,
!         TestIdentities,
      )
      test_support.run_doctest(test_sets, verbose)





More information about the Python-checkins mailing list