blob: 8100aec7ab84f99bac6aa68abfe1541305af8cb4 [file] [log] [blame]
Neil Schemenauerfd288c72001-01-02 16:30:31 +00001import sys
2
3from test_support import *
4
5class Empty:
6 def __repr__(self):
7 return '<Empty>'
8
9class Coerce:
10 def __init__(self, arg):
11 self.arg = arg
12
13 def __repr__(self):
14 return '<Coerce %s>' % self.arg
15
16 def __coerce__(self, other):
17 if isinstance(other, Coerce):
18 return self.arg, other.arg
19 else:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000020 return self.arg, other
Neil Schemenauerfd288c72001-01-02 16:30:31 +000021
22class Cmp:
23 def __init__(self,arg):
24 self.arg = arg
25
26 def __repr__(self):
27 return '<Cmp %s>' % self.arg
28
29 def __cmp__(self, other):
30 return cmp(self.arg, other)
31
Neil Schemenauerfd288c72001-01-02 16:30:31 +000032
Neil Schemenaueraa726ba2001-01-04 01:34:52 +000033candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2), Cmp(2.0)]
Neil Schemenauerfd288c72001-01-02 16:30:31 +000034
35def test():
36 for a in candidates:
37 for b in candidates:
Neil Schemenauerfd288c72001-01-02 16:30:31 +000038 try:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000039 x = a == b
Neil Schemenauerfd288c72001-01-02 16:30:31 +000040 except:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000041 print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0])
Neil Schemenauerfd288c72001-01-02 16:30:31 +000042 else:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000043 if x:
44 print "%s == %s" % (a, b)
45 else:
46 print "%s != %s" % (a, b)
Guido van Rossum28962cc2001-08-15 21:02:20 +000047 # Ensure default comparison compares id() of args
Tim Peters63a8d692001-08-16 16:56:16 +000048 L = []
Guido van Rossum28962cc2001-08-15 21:02:20 +000049 for i in range(10):
Guido van Rossum54e54c62001-09-04 19:14:14 +000050 L.insert(len(L)//2, Empty())
Guido van Rossum28962cc2001-08-15 21:02:20 +000051 for a in L:
52 for b in L:
53 if cmp(a, b) != cmp(id(a), id(b)):
54 print "ERROR:", cmp(a, b), cmp(id(a), id(b)), id(a), id(b)
Neil Schemenauerfd288c72001-01-02 16:30:31 +000055
56test()