mirror of https://github.com/python/cpython.git
Add more tests for compare and coercion in preparation for the coercion
overhaul. Closes SF patch #102878.
This commit is contained in:
parent
10e31cf82e
commit
fd288c7cd5
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,122 @@
|
|||
test_compare
|
||||
cmp(2, 2) = 0
|
||||
cmp(2, 2.2) = -1
|
||||
cmp(2, 2) = 0
|
||||
cmp(2, (2+4j)) = -1
|
||||
cmp(2, [1]) = -108
|
||||
cmp(2, (2,)) = -116
|
||||
cmp(2, None) = -78
|
||||
cmp(2, <Empty>) = -1
|
||||
cmp(2, <Coerce 3>) = -1
|
||||
cmp(2, <Cmp 4>) = -1
|
||||
cmp(2, <RCmp 5>) = -1
|
||||
cmp(2.2, 2) = 1
|
||||
cmp(2.2, 2.2) = 0
|
||||
cmp(2.2, 2) = 1
|
||||
cmp(2.2, (2+4j)) = 1
|
||||
cmp(2.2, [1]) = -108
|
||||
cmp(2.2, (2,)) = -116
|
||||
cmp(2.2, None) = -78
|
||||
cmp(2.2, <Empty>) = -1
|
||||
cmp(2.2, <Coerce 3>) = -1
|
||||
cmp(2.2, <Cmp 4>) = -1
|
||||
cmp(2.2, <RCmp 5>) = -1
|
||||
cmp(2, 2) = 0
|
||||
cmp(2, 2.2) = -1
|
||||
cmp(2, 2) = 0
|
||||
cmp(2, (2+4j)) = -1
|
||||
cmp(2, [1]) = -108
|
||||
cmp(2, (2,)) = -116
|
||||
cmp(2, None) = -78
|
||||
cmp(2, <Empty>) = -1
|
||||
cmp(2, <Coerce 3>) = -1
|
||||
cmp(2, <Cmp 4>) = -1
|
||||
cmp(2, <RCmp 5>) = -1
|
||||
cmp((2+4j), 2) = 1
|
||||
cmp((2+4j), 2.2) = -1
|
||||
cmp((2+4j), 2) = 1
|
||||
cmp((2+4j), (2+4j)) = 0
|
||||
cmp((2+4j), [1]) = -108
|
||||
cmp((2+4j), (2,)) = -116
|
||||
cmp((2+4j), None) = -78
|
||||
cmp((2+4j), <Empty>) = -1
|
||||
cmp((2+4j), <Coerce 3>) = -1
|
||||
cmp((2+4j), <Cmp 4>) = -1
|
||||
cmp((2+4j), <RCmp 5>) = -1
|
||||
cmp([1], 2) = 108
|
||||
cmp([1], 2.2) = 108
|
||||
cmp([1], 2) = 108
|
||||
cmp([1], (2+4j)) = 108
|
||||
cmp([1], [1]) = 0
|
||||
cmp([1], (2,)) = -8
|
||||
cmp([1], None) = 30
|
||||
cmp([1], <Empty>) = -1
|
||||
cmp([1], <Coerce 3>) = 1
|
||||
cmp([1], <Cmp 4>) = 1
|
||||
cmp([1], <RCmp 5>) = -1
|
||||
cmp((2,), 2) = 116
|
||||
cmp((2,), 2.2) = 116
|
||||
cmp((2,), 2) = 116
|
||||
cmp((2,), (2+4j)) = 116
|
||||
cmp((2,), [1]) = 8
|
||||
cmp((2,), (2,)) = 0
|
||||
cmp((2,), None) = 38
|
||||
cmp((2,), <Empty>) = -1
|
||||
cmp((2,), <Coerce 3>) = 1
|
||||
cmp((2,), <Cmp 4>) = 1
|
||||
cmp((2,), <RCmp 5>) = -1
|
||||
cmp(None, 2) = 78
|
||||
cmp(None, 2.2) = 78
|
||||
cmp(None, 2) = 78
|
||||
cmp(None, (2+4j)) = 78
|
||||
cmp(None, [1]) = -30
|
||||
cmp(None, (2,)) = -38
|
||||
cmp(None, None) = 0
|
||||
cmp(None, <Empty>) = -1
|
||||
cmp(None, <Coerce 3>) = 1
|
||||
cmp(None, <Cmp 4>) = 1
|
||||
cmp(None, <RCmp 5>) = -1
|
||||
cmp(<Empty>, 2) = 1
|
||||
cmp(<Empty>, 2.2) = 1
|
||||
cmp(<Empty>, 2) = 1
|
||||
cmp(<Empty>, (2+4j)) = 1
|
||||
cmp(<Empty>, [1]) = 1
|
||||
cmp(<Empty>, (2,)) = 1
|
||||
cmp(<Empty>, None) = 1
|
||||
cmp(<Empty>, <Empty>) = 0
|
||||
cmp(<Empty>, <Coerce 3>) = -1
|
||||
cmp(<Empty>, <Cmp 4>) = -1
|
||||
cmp(<Empty>, <RCmp 5>) = 1
|
||||
cmp(<Coerce 3>, 2) = 1
|
||||
cmp(<Coerce 3>, 2.2) = 1
|
||||
cmp(<Coerce 3>, 2) = 1
|
||||
cmp(<Coerce 3>, (2+4j)) = 1
|
||||
cmp(<Coerce 3>, [1]) = -1
|
||||
cmp(<Coerce 3>, (2,)) = -1
|
||||
cmp(<Coerce 3>, None) = -1
|
||||
cmp(<Coerce 3>, <Empty>) = 1
|
||||
cmp(<Coerce 3>, <Coerce 3>) = 0
|
||||
cmp(<Coerce 3>, <Cmp 4>) = -1
|
||||
cmp(<Coerce 3>, <RCmp 5>) = -1
|
||||
cmp(<Cmp 4>, 2) = 1
|
||||
cmp(<Cmp 4>, 2.2) = 1
|
||||
cmp(<Cmp 4>, 2) = 1
|
||||
cmp(<Cmp 4>, (2+4j)) = 1
|
||||
cmp(<Cmp 4>, [1]) = -1
|
||||
cmp(<Cmp 4>, (2,)) = -1
|
||||
cmp(<Cmp 4>, None) = -1
|
||||
cmp(<Cmp 4>, <Empty>) = -1
|
||||
cmp(<Cmp 4>, <Coerce 3>) = 1
|
||||
cmp(<Cmp 4>, <Cmp 4>) = 0
|
||||
cmp(<Cmp 4>, <RCmp 5>) = -1
|
||||
cmp(<RCmp 5>, 2) = 1
|
||||
cmp(<RCmp 5>, 2.2) = 1
|
||||
cmp(<RCmp 5>, 2) = 1
|
||||
cmp(<RCmp 5>, (2+4j)) = 1
|
||||
cmp(<RCmp 5>, [1]) = 1
|
||||
cmp(<RCmp 5>, (2,)) = 1
|
||||
cmp(<RCmp 5>, None) = 1
|
||||
cmp(<RCmp 5>, <Empty>) = 1
|
||||
cmp(<RCmp 5>, <Coerce 3>) = 1
|
||||
cmp(<RCmp 5>, <Cmp 4>) = 1
|
||||
cmp(<RCmp 5>, <RCmp 5>) = 0
|
|
@ -0,0 +1,113 @@
|
|||
import copy
|
||||
import sys
|
||||
|
||||
# Fake a number that implements numeric methods through __coerce__
|
||||
class CoerceNumber:
|
||||
def __init__(self, arg):
|
||||
self.arg = arg
|
||||
|
||||
def __repr__(self):
|
||||
return '<CoerceNumber %s>' % repr(self.arg)
|
||||
|
||||
def __coerce__(self, other):
|
||||
if isinstance(other, CoerceNumber):
|
||||
return self.arg, other.arg
|
||||
else:
|
||||
return (self.arg, other)
|
||||
|
||||
|
||||
# Fake a number that implements numeric ops through methods.
|
||||
class MethodNumber:
|
||||
|
||||
def __init__(self,arg):
|
||||
self.arg = arg
|
||||
|
||||
def __repr__(self):
|
||||
return '<MethodNumber %s>' % repr(self.arg)
|
||||
|
||||
def __add__(self,other):
|
||||
return self.arg + other
|
||||
|
||||
def __radd__(self,other):
|
||||
return other + self.arg
|
||||
|
||||
def __sub__(self,other):
|
||||
return self.arg - other
|
||||
|
||||
def __rsub__(self,other):
|
||||
return other - self.arg
|
||||
|
||||
def __mul__(self,other):
|
||||
return self.arg * other
|
||||
|
||||
def __rmul__(self,other):
|
||||
return other * self.arg
|
||||
|
||||
def __div__(self,other):
|
||||
return self.arg / other
|
||||
|
||||
def __rdiv__(self,other):
|
||||
return other / self.arg
|
||||
|
||||
def __pow__(self,other):
|
||||
return self.arg ** other
|
||||
|
||||
def __rpow__(self,other):
|
||||
return other ** self.arg
|
||||
|
||||
def __mod__(self,other):
|
||||
return self.arg % other
|
||||
|
||||
def __rmod__(self,other):
|
||||
return other % self.arg
|
||||
|
||||
def __cmp__(self, other):
|
||||
return cmp(self.arg, other)
|
||||
|
||||
|
||||
candidates = [ 2, 2.2, 2L, 2+4j, [1], (2,), None,
|
||||
MethodNumber(1), CoerceNumber(8)]
|
||||
|
||||
infix_binops = [ '+', '-', '*', '/', '**', '%' ]
|
||||
prefix_binops = [ 'divmod' ]
|
||||
|
||||
def do_infix_binops():
|
||||
for a in candidates:
|
||||
for b in candidates:
|
||||
for op in infix_binops:
|
||||
print '%s %s %s' % (a, op, b),
|
||||
try:
|
||||
x = eval('a %s b' % op)
|
||||
except:
|
||||
error = sys.exc_info()[:2]
|
||||
print '... %s' % error[0]
|
||||
else:
|
||||
print '=', x
|
||||
try:
|
||||
z = copy.copy(a)
|
||||
except copy.Error:
|
||||
z = a # assume it has no inplace ops
|
||||
print '%s %s= %s' % (a, op, b),
|
||||
try:
|
||||
exec('z %s= b' % op)
|
||||
except:
|
||||
error = sys.exc_info()[:2]
|
||||
print '... %s' % error[0]
|
||||
else:
|
||||
print '=>', z
|
||||
|
||||
def do_prefix_binops():
|
||||
for a in candidates:
|
||||
for b in candidates:
|
||||
for op in prefix_binops:
|
||||
print '%s(%s, %s)' % (op, a, b),
|
||||
try:
|
||||
x = eval('%s(a, b)' % op)
|
||||
except:
|
||||
error = sys.exc_info()[:2]
|
||||
print '... %s' % error[0]
|
||||
else:
|
||||
print '=', x
|
||||
|
||||
do_infix_binops()
|
||||
do_prefix_binops()
|
|
@ -0,0 +1,57 @@
|
|||
import sys
|
||||
|
||||
from test_support import *
|
||||
|
||||
class Empty:
|
||||
def __repr__(self):
|
||||
return '<Empty>'
|
||||
|
||||
class Coerce:
|
||||
def __init__(self, arg):
|
||||
self.arg = arg
|
||||
|
||||
def __repr__(self):
|
||||
return '<Coerce %s>' % self.arg
|
||||
|
||||
def __coerce__(self, other):
|
||||
if isinstance(other, Coerce):
|
||||
return self.arg, other.arg
|
||||
else:
|
||||
return (self.arg, other)
|
||||
|
||||
class Cmp:
|
||||
def __init__(self,arg):
|
||||
self.arg = arg
|
||||
|
||||
def __repr__(self):
|
||||
return '<Cmp %s>' % self.arg
|
||||
|
||||
def __cmp__(self, other):
|
||||
return cmp(self.arg, other)
|
||||
|
||||
class RCmp:
|
||||
def __init__(self,arg):
|
||||
self.arg = arg
|
||||
|
||||
def __repr__(self):
|
||||
return '<RCmp %s>' % self.arg
|
||||
|
||||
def __rcmp__(self, other):
|
||||
return cmp(other, self.arg)
|
||||
|
||||
|
||||
candidates = [2, 2.2, 2L, 2+4j, [1], (2,), None, Empty(), Coerce(3),
|
||||
Cmp(4), RCmp(5)]
|
||||
|
||||
def test():
|
||||
for a in candidates:
|
||||
for b in candidates:
|
||||
print "cmp(%s, %s)" % (a, b),
|
||||
try:
|
||||
x = cmp(a, b)
|
||||
except:
|
||||
print '... %s' % sys.exc_info(0)
|
||||
else:
|
||||
print '=', x
|
||||
|
||||
test()
|
Loading…
Reference in New Issue