[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.61,1.62

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 18 Sep 2001 14:06:06 -0700


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

Modified Files:
	test_descr.py 
Log Message:
Add a similar test for rich comparisons.


Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.61
retrieving revision 1.62
diff -C2 -d -r1.61 -r1.62
*** test_descr.py	2001/09/18 20:38:53	1.61
--- test_descr.py	2001/09/18 21:06:04	1.62
***************
*** 1834,1838 ****
  def classic_comparisons():
      if verbose: print "Testing classic comparisons..."
!     for base in (int, object):
          if verbose: print "        (base = %s)" % base
          class C(base):
--- 1834,1840 ----
  def classic_comparisons():
      if verbose: print "Testing classic comparisons..."
!     class classic:
!         pass
!     for base in (classic, int, object):
          if verbose: print "        (base = %s)" % base
          class C(base):
***************
*** 1859,1862 ****
--- 1861,1927 ----
                  verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
  
+ def rich_comparisons():
+     if verbose:
+         print "Testing rich comparisons..."
+     class classic:
+         pass
+     for base in (classic, int, object, list):
+         if verbose: print "        (base = %s)" % base
+         class C(base):
+             def __init__(self, value):
+                 self.value = int(value)
+             def __cmp__(self, other):
+                 raise TestFailed, "shouldn't call __cmp__"
+             def __eq__(self, other):
+                 if isinstance(other, C):
+                     return self.value == other.value
+                 if isinstance(other, int) or isinstance(other, long):
+                     return self.value == other
+                 return NotImplemented
+             def __ne__(self, other):
+                 if isinstance(other, C):
+                     return self.value != other.value
+                 if isinstance(other, int) or isinstance(other, long):
+                     return self.value != other
+                 return NotImplemented
+             def __lt__(self, other):
+                 if isinstance(other, C):
+                     return self.value < other.value
+                 if isinstance(other, int) or isinstance(other, long):
+                     return self.value < other
+                 return NotImplemented
+             def __le__(self, other):
+                 if isinstance(other, C):
+                     return self.value <= other.value
+                 if isinstance(other, int) or isinstance(other, long):
+                     return self.value <= other
+                 return NotImplemented
+             def __gt__(self, other):
+                 if isinstance(other, C):
+                     return self.value > other.value
+                 if isinstance(other, int) or isinstance(other, long):
+                     return self.value > other
+                 return NotImplemented
+             def __ge__(self, other):
+                 if isinstance(other, C):
+                     return self.value >= other.value
+                 if isinstance(other, int) or isinstance(other, long):
+                     return self.value >= other
+                 return NotImplemented
+         c1 = C(1)
+         c2 = C(2)
+         c3 = C(3)
+         verify(c1 == 1)
+         c = {1: c1, 2: c2, 3: c3}
+         for x in 1, 2, 3:
+             for y in 1, 2, 3:
+                 for op in "<", "<=", "==", "!=", ">", ">=":
+                     verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
+                            "x=%d, y=%d" % (x, y))
+                     verify(eval("c[x] %s y" % op) == eval("x %s y" % op),
+                            "x=%d, y=%d" % (x, y))
+                     verify(eval("x %s c[y]" % op) == eval("x %s y" % op),
+                            "x=%d, y=%d" % (x, y))
+ 
  
  def all():
***************
*** 1898,1901 ****
--- 1963,1967 ----
      str_subclass_as_dict_key()
      classic_comparisons()
+     rich_comparisons()
  
  all()