source: trunk/essentials/dev-lang/python/Lib/test/test_compare.py@ 3226

Last change on this file since 3226 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 1.5 KB
Line 
1import sys
2import unittest
3from test import test_support
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:
20 return self.arg, other
21
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
32class 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
36
37 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)
42 else:
43 self.assertNotEqual(a, b)
44
45 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()
Note: See TracBrowser for help on using the repository browser.