blob: 1d0da692097ba7a31d4f46615b4f17a759bcfc3b [file] [log] [blame]
Neil Schemenauerfd288c72001-01-02 16:30:31 +00001import sys
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Neil Schemenauerfd288c72001-01-02 16:30:31 +00009class Cmp:
10 def __init__(self,arg):
11 self.arg = arg
12
13 def __repr__(self):
14 return '<Cmp %s>' % self.arg
15
Guido van Rossum47b9ff62006-08-24 00:41:19 +000016 def __eq__(self, other):
17 return self.arg == other
Neil Schemenauerfd288c72001-01-02 16:30:31 +000018
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019class ComparisonTest(unittest.TestCase):
Guido van Rossume2a383d2007-01-15 16:59:06 +000020 set1 = [2, 2.0, 2, 2+0j, Cmp(2.0)]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021 set2 = [[1], (3,), None, Empty()]
22 candidates = set1 + set2
Neil Schemenauerfd288c72001-01-02 16:30:31 +000023
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024 def test_comparisons(self):
25 for a in self.candidates:
26 for b in self.candidates:
27 if ((a in self.set1) and (b in self.set1)) or a is b:
28 self.assertEqual(a, b)
Neil Schemenauere7e694f2001-01-03 02:13:26 +000029 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030 self.assertNotEqual(a, b)
Neil Schemenauerfd288c72001-01-02 16:30:31 +000031
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032 def test_id_comparisons(self):
33 # Ensure default comparison compares id() of args
34 L = []
35 for i in range(10):
36 L.insert(len(L)//2, Empty())
37 for a in L:
38 for b in L:
Guido van Rossum47b9ff62006-08-24 00:41:19 +000039 self.assertEqual(a == b, id(a) == id(b),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040 'a=%r, b=%r' % (a, b))
41
42def test_main():
43 test_support.run_unittest(ComparisonTest)
44
45if __name__ == '__main__':
46 test_main()