summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_compare.py
blob: e8698a9b59a4d25fea271d485c369f233526344a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2),
                Cmp(2.0), RCmp(2L)]

def test():
    for a in candidates:
        for b in candidates:
            try:
                x = a == b
            except:
                print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0])
            else:
                if x:
                    print "%s == %s" % (a, b)
                else:
                    print "%s != %s" % (a, b)

test()