blob: 2fde614dd46bdcbea2b24f3bfb3317af123ab232 [file] [log] [blame]
Neil Schemenauerfd288c72001-01-02 16:30:31 +00001import sys
Neal Norwitz349c0ed2006-04-09 04:50:18 +00002import unittest
3from test import test_support
Neil Schemenauerfd288c72001-01-02 16:30:31 +00004
Neil Schemenauerfd288c72001-01-02 16:30:31 +00005class 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
Neal Norwitz349c0ed2006-04-09 04:50:18 +000032class ComparisonTest(unittest.TestCase):
33 set1 = [2, 2.0, 2L, 2+0j, Coerce(2), Cmp(2.0)]
34 set2 = [[1], (3,), None, Empty()]
35 candidates = set1 + set2
Neil Schemenauerfd288c72001-01-02 16:30:31 +000036
Neal Norwitz349c0ed2006-04-09 04:50:18 +000037 def test_comparisons(self):
38 for a in self.candidates:
39 for b in self.candidates:
40 if ((a in self.set1) and (b in self.set1)) or a is b:
41 self.assertEqual(a, b)
Neil Schemenauere7e694f2001-01-03 02:13:26 +000042 else:
Neal Norwitz349c0ed2006-04-09 04:50:18 +000043 self.assertNotEqual(a, b)
Neil Schemenauerfd288c72001-01-02 16:30:31 +000044
Neal Norwitz349c0ed2006-04-09 04:50:18 +000045 def test_id_comparisons(self):
46 # Ensure default comparison compares id() of args
47 L = []
48 for i in range(10):
49 L.insert(len(L)//2, Empty())
50 for a in L:
51 for b in L:
52 self.assertEqual(cmp(a, b), cmp(id(a), id(b)),
53 'a=%r, b=%r' % (a, b))
54
55def test_main():
56 test_support.run_unittest(ComparisonTest)
57
58if __name__ == '__main__':
59 test_main()