Commit 721adf99 authored by Walter Dörwald's avatar Walter Dörwald

Port test_richcmp.py to PyUnit. From SF patch #662807 which additional

tests and comments.
parent 4a7751cc
test_richcmp
operator: <
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
Number(0) | False | True | True |
Number(1) | False | False | True |
Number(2) | False | False | False |
----------+-----------+-----------+-----------+-
operator: <=
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
Number(0) | True | True | True |
Number(1) | False | True | True |
Number(2) | False | False | True |
----------+-----------+-----------+-----------+-
operator: ==
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
Number(0) | True | False | False |
Number(1) | False | True | False |
Number(2) | False | False | True |
----------+-----------+-----------+-----------+-
operator: !=
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
Number(0) | False | True | True |
Number(1) | True | False | True |
Number(2) | True | True | False |
----------+-----------+-----------+-----------+-
operator: >
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
Number(0) | False | False | False |
Number(1) | True | False | False |
Number(2) | True | True | False |
----------+-----------+-----------+-----------+-
operator: >=
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
Number(0) | True | False | False |
Number(1) | True | True | False |
Number(2) | True | True | True |
----------+-----------+-----------+-----------+-
**************************************************
operator: <
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
0 | False | True | True |
1 | False | False | True |
2 | False | False | False |
----------+-----------+-----------+-----------+-
operator: <=
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
0 | True | True | True |
1 | False | True | True |
2 | False | False | True |
----------+-----------+-----------+-----------+-
operator: ==
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
0 | True | False | False |
1 | False | True | False |
2 | False | False | True |
----------+-----------+-----------+-----------+-
operator: !=
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
0 | False | True | True |
1 | True | False | True |
2 | True | True | False |
----------+-----------+-----------+-----------+-
operator: >
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
0 | False | False | False |
1 | True | False | False |
2 | True | True | False |
----------+-----------+-----------+-----------+-
operator: >=
| Number(0) | Number(1) | Number(2) |
----------+-----------+-----------+-----------+-
0 | True | False | False |
1 | True | True | False |
2 | True | True | True |
----------+-----------+-----------+-----------+-
**************************************************
operator: <
| 0 | 1 | 2 |
----------+-----------+-----------+-----------+-
Number(0) | False | True | True |
Number(1) | False | False | True |
Number(2) | False | False | False |
----------+-----------+-----------+-----------+-
operator: <=
| 0 | 1 | 2 |
----------+-----------+-----------+-----------+-
Number(0) | True | True | True |
Number(1) | False | True | True |
Number(2) | False | False | True |
----------+-----------+-----------+-----------+-
operator: ==
| 0 | 1 | 2 |
----------+-----------+-----------+-----------+-
Number(0) | True | False | False |
Number(1) | False | True | False |
Number(2) | False | False | True |
----------+-----------+-----------+-----------+-
operator: !=
| 0 | 1 | 2 |
----------+-----------+-----------+-----------+-
Number(0) | False | True | True |
Number(1) | True | False | True |
Number(2) | True | True | False |
----------+-----------+-----------+-----------+-
operator: >
| 0 | 1 | 2 |
----------+-----------+-----------+-----------+-
Number(0) | False | False | False |
Number(1) | True | False | False |
Number(2) | True | True | False |
----------+-----------+-----------+-----------+-
operator: >=
| 0 | 1 | 2 |
----------+-----------+-----------+-----------+-
Number(0) | True | False | False |
Number(1) | True | True | False |
Number(2) | True | True | True |
----------+-----------+-----------+-----------+-
**************************************************
Vector([0, 1, 2, 3, 4]) < Vector([2, 2, 2, 2, 2]) -> Vector([True, True, False, False, False])
Vector([0, 1, 2, 3, 4]) < [2, 2, 2, 2, 2] -> Vector([True, True, False, False, False])
[0, 1, 2, 3, 4] < Vector([2, 2, 2, 2, 2]) -> Vector([True, True, False, False, False])
Vector([0, 1, 2, 3, 4]) <= Vector([2, 2, 2, 2, 2]) -> Vector([True, True, True, False, False])
Vector([0, 1, 2, 3, 4]) <= [2, 2, 2, 2, 2] -> Vector([True, True, True, False, False])
[0, 1, 2, 3, 4] <= Vector([2, 2, 2, 2, 2]) -> Vector([True, True, True, False, False])
Vector([0, 1, 2, 3, 4]) == Vector([2, 2, 2, 2, 2]) -> Vector([False, False, True, False, False])
Vector([0, 1, 2, 3, 4]) == [2, 2, 2, 2, 2] -> Vector([False, False, True, False, False])
[0, 1, 2, 3, 4] == Vector([2, 2, 2, 2, 2]) -> Vector([False, False, True, False, False])
Vector([0, 1, 2, 3, 4]) != Vector([2, 2, 2, 2, 2]) -> Vector([True, True, False, True, True])
Vector([0, 1, 2, 3, 4]) != [2, 2, 2, 2, 2] -> Vector([True, True, False, True, True])
[0, 1, 2, 3, 4] != Vector([2, 2, 2, 2, 2]) -> Vector([True, True, False, True, True])
Vector([0, 1, 2, 3, 4]) > Vector([2, 2, 2, 2, 2]) -> Vector([False, False, False, True, True])
Vector([0, 1, 2, 3, 4]) > [2, 2, 2, 2, 2] -> Vector([False, False, False, True, True])
[0, 1, 2, 3, 4] > Vector([2, 2, 2, 2, 2]) -> Vector([False, False, False, True, True])
Vector([0, 1, 2, 3, 4]) >= Vector([2, 2, 2, 2, 2]) -> Vector([False, False, True, True, True])
Vector([0, 1, 2, 3, 4]) >= [2, 2, 2, 2, 2] -> Vector([False, False, True, True, True])
[0, 1, 2, 3, 4] >= Vector([2, 2, 2, 2, 2]) -> Vector([False, False, True, True, True])
# Tests for rich comparisons # Tests for rich comparisons
from test.test_support import TestFailed, verify, verbose import unittest
from test import test_support
import operator
class Number: class Number:
...@@ -26,10 +29,10 @@ class Number: ...@@ -26,10 +29,10 @@ class Number:
return self.x >= other return self.x >= other
def __cmp__(self, other): def __cmp__(self, other):
raise TestFailed, "Number.__cmp__() should not be called" raise test_support.TestFailed, "Number.__cmp__() should not be called"
def __repr__(self): def __repr__(self):
return "Number(%s)" % repr(self.x) return "Number(%r)" % (self.x, )
class Vector: class Vector:
...@@ -52,10 +55,10 @@ class Vector: ...@@ -52,10 +55,10 @@ class Vector:
raise TypeError, "Vectors cannot be used in Boolean contexts" raise TypeError, "Vectors cannot be used in Boolean contexts"
def __cmp__(self, other): def __cmp__(self, other):
raise TestFailed, "Vector.__cmp__() should not be called" raise test_support.TestFailed, "Vector.__cmp__() should not be called"
def __repr__(self): def __repr__(self):
return "Vector(%s)" % repr(self.data) return "Vector(%r)" % (self.data, )
def __lt__(self, other): def __lt__(self, other):
return Vector([a < b for a, b in zip(self.data, self.__cast(other))]) return Vector([a < b for a, b in zip(self.data, self.__cast(other))])
...@@ -82,92 +85,113 @@ class Vector: ...@@ -82,92 +85,113 @@ class Vector:
raise ValueError, "Cannot compare vectors of different length" raise ValueError, "Cannot compare vectors of different length"
return other return other
operators = "<", "<=", "==", "!=", ">", ">=" opmap = {
opmap = {} "lt": (lambda a,b: a< b, operator.lt, operator.__lt__),
for op in operators: "le": (lambda a,b: a<=b, operator.le, operator.__le__),
opmap[op] = eval("lambda a, b: a %s b" % op) "eq": (lambda a,b: a==b, operator.eq, operator.__eq__),
"ne": (lambda a,b: a!=b, operator.ne, operator.__ne__),
def testvector(): "gt": (lambda a,b: a> b, operator.gt, operator.__gt__),
"ge": (lambda a,b: a>=b, operator.ge, operator.__ge__)
}
class VectorTest(unittest.TestCase):
def checkfail(self, error, opname, *args):
for op in opmap[opname]:
self.assertRaises(error, op, *args)
def checkequal(self, opname, a, b, expres):
for op in opmap[opname]:
realres = op(a, b)
# can't use assertEqual(realres, expres) here
self.assertEqual(len(realres), len(expres))
for i in xrange(len(realres)):
# results are bool, so we can use "is" here
self.assert_(realres[i] is expres[i])
def test_mixed(self):
# check that comparisons involving Vector objects
# which return rich results (i.e. Vectors with itemwise
# comparison results) work
a = Vector(range(2)) a = Vector(range(2))
b = Vector(range(3)) b = Vector(range(3))
for op in operators: # all comparisons should fail for different length
try: for opname in opmap:
opmap[op](a, b) self.checkfail(ValueError, opname, a, b)
except ValueError:
pass a = range(5)
else: b = 5 * [2]
raise TestFailed, "a %s b for different length should fail" % op # try mixed arguments (but not (a, b) as that won't return a bool vector)
a = Vector(range(5)) args = [(a, Vector(b)), (Vector(a), b), (Vector(a), Vector(b))]
b = Vector(5 * [2]) for (a, b) in args:
for op in operators: self.checkequal("lt", a, b, [True, True, False, False, False])
print "%23s %-2s %-23s -> %s" % (a, op, b, opmap[op](a, b)) self.checkequal("le", a, b, [True, True, True, False, False])
print "%23s %-2s %-23s -> %s" % (a, op, b.data, opmap[op](a, b.data)) self.checkequal("eq", a, b, [False, False, True, False, False])
print "%23s %-2s %-23s -> %s" % (a.data, op, b, opmap[op](a.data, b)) self.checkequal("ne", a, b, [True, True, False, True, True ])
try: self.checkequal("gt", a, b, [False, False, False, True, True ])
if opmap[op](a, b): self.checkequal("ge", a, b, [False, False, True, True, True ])
raise TestFailed, "a %s b shouldn't be true" % op
else: for ops in opmap.itervalues():
raise TestFailed, "a %s b shouldn't be false" % op for op in ops:
except TypeError: # calls __nonzero__, which should fail
pass self.assertRaises(TypeError, bool, op(a, b))
def testop(a, b, op): class NumberTest(unittest.TestCase):
try:
ax = a.x def test_basic(self):
except AttributeError: # Check that comparisons involving Number objects
ax = a # give the same results give as comparing the
try: # corresponding ints
bx = b.x for a in xrange(3):
except AttributeError: for b in xrange(3):
bx = b for typea in (int, Number):
opfunc = opmap[op] for typeb in (int, Number):
realoutcome = opfunc(ax, bx) if typea==typeb==int:
testoutcome = opfunc(a, b) continue # the combination int, int is useless
if realoutcome != testoutcome: ta = typea(a)
print "Error for", a, op, b, ": expected", realoutcome, tb = typeb(b)
print "but got", testoutcome for ops in opmap.itervalues():
## else: for op in ops:
## print a, op, b, "-->", testoutcome # and "true" or "false" realoutcome = op(a, b)
testoutcome = op(ta, tb)
def testit(a, b): self.assertEqual(realoutcome, testoutcome)
testop(a, b, "<")
testop(a, b, "<=") def checkvalue(self, opname, a, b, expres):
testop(a, b, "==") for typea in (int, Number):
testop(a, b, "!=") for typeb in (int, Number):
testop(a, b, ">") ta = typea(a)
testop(a, b, ">=") tb = typeb(b)
for op in opmap[opname]:
def basic(): realres = op(ta, tb)
for a in range(3): realres = getattr(realres, "x", realres)
for b in range(3): self.assert_(realres is expres)
testit(Number(a), Number(b))
testit(a, Number(b)) def test_values(self):
testit(Number(a), b) # check all operators and all comparison results
self.checkvalue("lt", 0, 0, False)
def tabulate(c1=Number, c2=Number): self.checkvalue("le", 0, 0, True )
for op in operators: self.checkvalue("eq", 0, 0, True )
opfunc = opmap[op] self.checkvalue("ne", 0, 0, False)
print self.checkvalue("gt", 0, 0, False)
print "operator:", op self.checkvalue("ge", 0, 0, True )
print
print "%9s" % "", self.checkvalue("lt", 0, 1, True )
for b in range(3): self.checkvalue("le", 0, 1, True )
b = c2(b) self.checkvalue("eq", 0, 1, False)
print "| %9s" % b, self.checkvalue("ne", 0, 1, True )
print "|" self.checkvalue("gt", 0, 1, False)
print '----------+-' * 4 self.checkvalue("ge", 0, 1, False)
for a in range(3):
a = c1(a) self.checkvalue("lt", 1, 0, False)
print "%9s" % a, self.checkvalue("le", 1, 0, False)
for b in range(3): self.checkvalue("eq", 1, 0, False)
b = c2(b) self.checkvalue("ne", 1, 0, True )
print "| %9s" % opfunc(a, b), self.checkvalue("gt", 1, 0, True )
print "|" self.checkvalue("ge", 1, 0, True )
print '----------+-' * 4
print class MiscTest(unittest.TestCase):
print '*' * 50
def test_misbehavin(self):
def misbehavin():
class Misb: class Misb:
def __lt__(self, other): return 0 def __lt__(self, other): return 0
def __gt__(self, other): return 0 def __gt__(self, other): return 0
...@@ -178,50 +202,83 @@ def misbehavin(): ...@@ -178,50 +202,83 @@ def misbehavin():
def __cmp__(self, other): raise RuntimeError, "expected" def __cmp__(self, other): raise RuntimeError, "expected"
a = Misb() a = Misb()
b = Misb() b = Misb()
verify((a<b) == 0) self.assertEqual(a<b, 0)
verify((a==b) == 0) self.assertEqual(a==b, 0)
verify((a>b) == 0) self.assertEqual(a>b, 0)
try: self.assertRaises(RuntimeError, cmp, a, b)
print cmp(a, b)
except RuntimeError: def test_not(self):
# Check that exceptions in __nonzero__ are properly
# propagated by the not operator
import operator
class Exc:
pass pass
else: class Bad:
raise TestFailed, "cmp(Misb(), Misb()) didn't raise RuntimeError" def __nonzero__(self):
raise Exc
def do(bad):
not bad
def recursion(): for func in (do, operator.not_):
self.assertRaises(Exc, func, Bad())
def test_recursion(self):
# Check comparison for recursive objects
from UserList import UserList from UserList import UserList
a = UserList(); a.append(a) a = UserList(); a.append(a)
b = UserList(); b.append(b) b = UserList(); b.append(b)
def check(s, a=a, b=b):
if verbose: self.assert_(a == b)
print "check", s self.assert_(not a != b)
try:
if not eval(s):
raise TestFailed, s + " was false but expected to be true"
except RuntimeError, msg:
raise TestFailed, str(msg)
if verbose:
print "recursion tests: a=%s, b=%s" % (a, b)
check('a==b')
check('not a!=b')
a.append(1) a.append(1)
if verbose: self.assert_(a == a[0])
print "recursion tests: a=%s, b=%s" % (a, b) self.assert_(not a != a[0])
check('a!=b') self.assert_(a != b)
check('not a==b') self.assert_(not a == b)
b.append(0) b.append(0)
if verbose: self.assert_(a != b)
print "recursion tests: a=%s, b=%s" % (a, b) self.assert_(not a == b)
check('a!=b')
check('not a==b')
a[1] = -1 a[1] = -1
if verbose: self.assert_(a != b)
print "recursion tests: a=%s, b=%s" % (a, b) self.assert_(not a == b)
check('a!=b')
check('not a==b') a = UserList()
if verbose: print "recursion tests ok" b = UserList()
a.append(b)
b.append(a)
self.assert_(a == b)
self.assert_(not a != b)
b.append(17)
self.assert_(a != b)
self.assert_(not a == b)
a.append(17)
self.assert_(a == b)
self.assert_(not a != b)
def test_recursion2(self):
# This test exercises the circular structure handling code
# in PyObject_RichCompare()
class Weird(object):
def __eq__(self, other):
return self != other
def __ne__(self, other):
return self == other
def __lt__(self, other):
return self > other
def __gt__(self, other):
return self < other
def dicts(): self.assert_(Weird() == Weird())
self.assert_(not (Weird() != Weird()))
for op in opmap["lt"]:
self.assertRaises(ValueError, op, Weird(), Weird())
class DictTest(unittest.TestCase):
def test_dicts(self):
# Verify that __eq__ and __ne__ work for dicts even if the keys and # Verify that __eq__ and __ne__ work for dicts even if the keys and
# values don't support anything other than __eq__ and __ne__. Complex # values don't support anything other than __eq__ and __ne__. Complex
# numbers are a fine example of that. # numbers are a fine example of that.
...@@ -236,26 +293,66 @@ def dicts(): ...@@ -236,26 +293,66 @@ def dicts():
imag1b[k] = v imag1b[k] = v
imag2 = imag1b.copy() imag2 = imag1b.copy()
imag2[k] = v + 1.0 imag2[k] = v + 1.0
verify(imag1a == imag1a, "imag1a == imag1a should have worked") self.assert_(imag1a == imag1a)
verify(imag1a == imag1b, "imag1a == imag1b should have worked") self.assert_(imag1a == imag1b)
verify(imag2 == imag2, "imag2 == imag2 should have worked") self.assert_(imag2 == imag2)
verify(imag1a != imag2, "imag1a != imag2 should have worked") self.assert_(imag1a != imag2)
for op in "<", "<=", ">", ">=": for opname in ("lt", "le", "gt", "ge"):
try: for op in opmap[opname]:
eval("imag1a %s imag2" % op) self.assertRaises(TypeError, op, imag1a, imag2)
except TypeError:
class ListTest(unittest.TestCase):
def assertIs(self, a, b):
self.assert_(a is b)
def test_coverage(self):
# exercise all comparisons for lists
x = [42]
self.assertIs(x<x, False)
self.assertIs(x<=x, True)
self.assertIs(x==x, True)
self.assertIs(x!=x, False)
self.assertIs(x>x, False)
self.assertIs(x>=x, True)
y = [42, 42]
self.assertIs(x<y, True)
self.assertIs(x<=y, True)
self.assertIs(x==y, False)
self.assertIs(x!=y, True)
self.assertIs(x>y, False)
self.assertIs(x>=y, False)
def test_badentry(self):
# make sure that exceptions for item comparison are properly
# propagated in list comparisons
class Exc:
pass pass
else: class Bad:
raise TestFailed("expected TypeError from imag1a %s imag2" % op) def __eq__(self, other):
raise Exc
def main():
basic() x = [Bad()]
tabulate() y = [Bad()]
tabulate(c1=int)
tabulate(c2=int) for op in opmap["eq"]:
testvector() self.assertRaises(Exc, op, x, y)
misbehavin()
recursion() def test_goodentry(self):
dicts() # This test exercises the final call to PyObject_RichCompare()
# in Objects/listobject.c::list_richcompare()
main() class Good:
def __lt__(self, other):
return True
x = [Good()]
y = [Good()]
for op in opmap["lt"]:
self.assertIs(op(x, y), True)
def test_main():
test_support.run_classtests(VectorTest, NumberTest, MiscTest, DictTest, ListTest)
if __name__ == "__main__":
test_main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment