blob: 6899926facd1d782a3899481571174a5f34f0335 [file] [log] [blame]
Neil Schemenauerfd288c72001-01-02 16:30:31 +00001import sys
2
Neil Schemenauerfd288c72001-01-02 16:30:31 +00003class Empty:
4 def __repr__(self):
5 return '<Empty>'
6
7class Coerce:
8 def __init__(self, arg):
9 self.arg = arg
10
11 def __repr__(self):
12 return '<Coerce %s>' % self.arg
13
14 def __coerce__(self, other):
15 if isinstance(other, Coerce):
16 return self.arg, other.arg
17 else:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000018 return self.arg, other
Neil Schemenauerfd288c72001-01-02 16:30:31 +000019
20class Cmp:
21 def __init__(self,arg):
22 self.arg = arg
23
24 def __repr__(self):
25 return '<Cmp %s>' % self.arg
26
27 def __cmp__(self, other):
28 return cmp(self.arg, other)
29
Neil Schemenauerfd288c72001-01-02 16:30:31 +000030
Neil Schemenaueraa726ba2001-01-04 01:34:52 +000031candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2), Cmp(2.0)]
Neil Schemenauerfd288c72001-01-02 16:30:31 +000032
33def test():
34 for a in candidates:
35 for b in candidates:
Neil Schemenauerfd288c72001-01-02 16:30:31 +000036 try:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000037 x = a == b
Neil Schemenauerfd288c72001-01-02 16:30:31 +000038 except:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000039 print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0])
Neil Schemenauerfd288c72001-01-02 16:30:31 +000040 else:
Neil Schemenauere7e694f2001-01-03 02:13:26 +000041 if x:
42 print "%s == %s" % (a, b)
43 else:
44 print "%s != %s" % (a, b)
Guido van Rossum28962cc2001-08-15 21:02:20 +000045 # Ensure default comparison compares id() of args
Tim Peters63a8d692001-08-16 16:56:16 +000046 L = []
Guido van Rossum28962cc2001-08-15 21:02:20 +000047 for i in range(10):
Guido van Rossum54e54c62001-09-04 19:14:14 +000048 L.insert(len(L)//2, Empty())
Guido van Rossum28962cc2001-08-15 21:02:20 +000049 for a in L:
50 for b in L:
51 if cmp(a, b) != cmp(id(a), id(b)):
52 print "ERROR:", cmp(a, b), cmp(id(a), id(b)), id(a), id(b)
Neil Schemenauerfd288c72001-01-02 16:30:31 +000053
54test()