]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | import unittest\r |
2 | from test import test_support\r | |
3 | \r | |
4 | class Empty:\r | |
5 | def __repr__(self):\r | |
6 | return '<Empty>'\r | |
7 | \r | |
8 | class Coerce:\r | |
9 | def __init__(self, arg):\r | |
10 | self.arg = arg\r | |
11 | \r | |
12 | def __repr__(self):\r | |
13 | return '<Coerce %s>' % self.arg\r | |
14 | \r | |
15 | def __coerce__(self, other):\r | |
16 | if isinstance(other, Coerce):\r | |
17 | return self.arg, other.arg\r | |
18 | else:\r | |
19 | return self.arg, other\r | |
20 | \r | |
21 | class Cmp:\r | |
22 | def __init__(self,arg):\r | |
23 | self.arg = arg\r | |
24 | \r | |
25 | def __repr__(self):\r | |
26 | return '<Cmp %s>' % self.arg\r | |
27 | \r | |
28 | def __cmp__(self, other):\r | |
29 | return cmp(self.arg, other)\r | |
30 | \r | |
31 | class ComparisonTest(unittest.TestCase):\r | |
32 | set1 = [2, 2.0, 2L, 2+0j, Coerce(2), Cmp(2.0)]\r | |
33 | set2 = [[1], (3,), None, Empty()]\r | |
34 | candidates = set1 + set2\r | |
35 | \r | |
36 | def test_comparisons(self):\r | |
37 | for a in self.candidates:\r | |
38 | for b in self.candidates:\r | |
39 | if ((a in self.set1) and (b in self.set1)) or a is b:\r | |
40 | self.assertEqual(a, b)\r | |
41 | else:\r | |
42 | self.assertNotEqual(a, b)\r | |
43 | \r | |
44 | def test_id_comparisons(self):\r | |
45 | # Ensure default comparison compares id() of args\r | |
46 | L = []\r | |
47 | for i in range(10):\r | |
48 | L.insert(len(L)//2, Empty())\r | |
49 | for a in L:\r | |
50 | for b in L:\r | |
51 | self.assertEqual(cmp(a, b), cmp(id(a), id(b)),\r | |
52 | 'a=%r, b=%r' % (a, b))\r | |
53 | \r | |
54 | def test_main():\r | |
55 | test_support.run_unittest(ComparisonTest)\r | |
56 | \r | |
57 | if __name__ == '__main__':\r | |
58 | test_main()\r |