annotate Lib/test/test_richcmp.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Tests for rich comparisons
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
3 import unittest
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
4 from test import test_support
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
5
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
6 import operator
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 class Number:
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def __init__(self, x):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 self.x = x
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 def __lt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 return self.x < other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 def __le__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 return self.x <= other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def __eq__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 return self.x == other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 def __ne__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 return self.x != other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def __gt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 return self.x > other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 def __ge__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 return self.x >= other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 def __cmp__(self, other):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
32 raise test_support.TestFailed, "Number.__cmp__() should not be called"
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def __repr__(self):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
35 return "Number(%r)" % (self.x, )
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 class Vector:
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def __init__(self, data):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.data = data
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def __len__(self):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return len(self.data)
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def __getitem__(self, i):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return self.data[i]
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def __setitem__(self, i, v):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 self.data[i] = v
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 44357
diff changeset
51 __hash__ = None # Vectors cannot be hashed
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 def __nonzero__(self):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 raise TypeError, "Vectors cannot be used in Boolean contexts"
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 def __cmp__(self, other):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
57 raise test_support.TestFailed, "Vector.__cmp__() should not be called"
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def __repr__(self):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
60 return "Vector(%r)" % (self.data, )
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 def __lt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return Vector([a < b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def __le__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 return Vector([a <= b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def __eq__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 return Vector([a == b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 def __ne__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return Vector([a != b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def __gt__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 return Vector([a > b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def __ge__(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return Vector([a >= b for a, b in zip(self.data, self.__cast(other))])
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 def __cast(self, other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if isinstance(other, Vector):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 other = other.data
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 if len(self.data) != len(other):
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 raise ValueError, "Cannot compare vectors of different length"
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return other
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
87 opmap = {
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
88 "lt": (lambda a,b: a< b, operator.lt, operator.__lt__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
89 "le": (lambda a,b: a<=b, operator.le, operator.__le__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
90 "eq": (lambda a,b: a==b, operator.eq, operator.__eq__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
91 "ne": (lambda a,b: a!=b, operator.ne, operator.__ne__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
92 "gt": (lambda a,b: a> b, operator.gt, operator.__gt__),
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
93 "ge": (lambda a,b: a>=b, operator.ge, operator.__ge__)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
94 }
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
95
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
96 class VectorTest(unittest.TestCase):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
97
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
98 def checkfail(self, error, opname, *args):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
99 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
100 self.assertRaises(error, op, *args)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
101
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
102 def checkequal(self, opname, a, b, expres):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
103 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
104 realres = op(a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
105 # can't use assertEqual(realres, expres) here
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
106 self.assertEqual(len(realres), len(expres))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
107 for i in xrange(len(realres)):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
108 # results are bool, so we can use "is" here
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
109 self.assertTrue(realres[i] is expres[i])
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
111 def test_mixed(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
112 # check that comparisons involving Vector objects
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
113 # which return rich results (i.e. Vectors with itemwise
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
114 # comparison results) work
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
115 a = Vector(range(2))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
116 b = Vector(range(3))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
117 # all comparisons should fail for different length
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
118 for opname in opmap:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
119 self.checkfail(ValueError, opname, a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
120
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
121 a = range(5)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
122 b = 5 * [2]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
123 # try mixed arguments (but not (a, b) as that won't return a bool vector)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
124 args = [(a, Vector(b)), (Vector(a), b), (Vector(a), Vector(b))]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
125 for (a, b) in args:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
126 self.checkequal("lt", a, b, [True, True, False, False, False])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
127 self.checkequal("le", a, b, [True, True, True, False, False])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
128 self.checkequal("eq", a, b, [False, False, True, False, False])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
129 self.checkequal("ne", a, b, [True, True, False, True, True ])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
130 self.checkequal("gt", a, b, [False, False, False, True, True ])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
131 self.checkequal("ge", a, b, [False, False, True, True, True ])
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
132
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
133 for ops in opmap.itervalues():
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
134 for op in ops:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
135 # calls __nonzero__, which should fail
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
136 self.assertRaises(TypeError, bool, op(a, b))
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
137
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
138 class NumberTest(unittest.TestCase):
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
140 def test_basic(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
141 # Check that comparisons involving Number objects
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
142 # give the same results give as comparing the
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
143 # corresponding ints
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
144 for a in xrange(3):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
145 for b in xrange(3):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
146 for typea in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
147 for typeb in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
148 if typea==typeb==int:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
149 continue # the combination int, int is useless
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
150 ta = typea(a)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
151 tb = typeb(b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
152 for ops in opmap.itervalues():
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
153 for op in ops:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
154 realoutcome = op(a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
155 testoutcome = op(ta, tb)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
156 self.assertEqual(realoutcome, testoutcome)
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
158 def checkvalue(self, opname, a, b, expres):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
159 for typea in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
160 for typeb in (int, Number):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
161 ta = typea(a)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
162 tb = typeb(b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
163 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
164 realres = op(ta, tb)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
165 realres = getattr(realres, "x", realres)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
166 self.assertTrue(realres is expres)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
167
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
168 def test_values(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
169 # check all operators and all comparison results
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
170 self.checkvalue("lt", 0, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
171 self.checkvalue("le", 0, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
172 self.checkvalue("eq", 0, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
173 self.checkvalue("ne", 0, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
174 self.checkvalue("gt", 0, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
175 self.checkvalue("ge", 0, 0, True )
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
177 self.checkvalue("lt", 0, 1, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
178 self.checkvalue("le", 0, 1, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
179 self.checkvalue("eq", 0, 1, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
180 self.checkvalue("ne", 0, 1, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
181 self.checkvalue("gt", 0, 1, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
182 self.checkvalue("ge", 0, 1, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
183
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
184 self.checkvalue("lt", 1, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
185 self.checkvalue("le", 1, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
186 self.checkvalue("eq", 1, 0, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
187 self.checkvalue("ne", 1, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
188 self.checkvalue("gt", 1, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
189 self.checkvalue("ge", 1, 0, True )
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
190
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
191 class MiscTest(unittest.TestCase):
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
193 def test_misbehavin(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
194 class Misb:
58770
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
195 def __lt__(self_, other): return 0
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
196 def __gt__(self_, other): return 0
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
197 def __eq__(self_, other): return 0
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
198 def __le__(self_, other): self.fail("This shouldn't happen")
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
199 def __ge__(self_, other): self.fail("This shouldn't happen")
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
200 def __ne__(self_, other): self.fail("This shouldn't happen")
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58198
diff changeset
201 def __cmp__(self_, other): raise RuntimeError, "expected"
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
202 a = Misb()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
203 b = Misb()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
204 self.assertEqual(a<b, 0)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
205 self.assertEqual(a==b, 0)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
206 self.assertEqual(a>b, 0)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
207 self.assertRaises(RuntimeError, cmp, a, b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
208
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
209 def test_not(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
210 # Check that exceptions in __nonzero__ are properly
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
211 # propagated by the not operator
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
212 import operator
37180
64ae32b07829 Exceptions should inherit from Exception now.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35420
diff changeset
213 class Exc(Exception):
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
214 pass
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
215 class Bad:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
216 def __nonzero__(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
217 raise Exc
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
218
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
219 def do(bad):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
220 not bad
16026
865266011dfa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16000
diff changeset
221
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
222 for func in (do, operator.not_):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
223 self.assertRaises(Exc, func, Bad())
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
224
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
225 def test_recursion(self):
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
226 # Check that comparison for recursive objects fails gracefully
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
227 from UserList import UserList
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
228 a = UserList()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
229 b = UserList()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
230 a.append(b)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
231 b.append(a)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
232 self.assertRaises(RuntimeError, operator.eq, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
233 self.assertRaises(RuntimeError, operator.ne, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
234 self.assertRaises(RuntimeError, operator.lt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
235 self.assertRaises(RuntimeError, operator.le, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
236 self.assertRaises(RuntimeError, operator.gt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
237 self.assertRaises(RuntimeError, operator.ge, a, b)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
238
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
239 b.append(17)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
240 # Even recursive lists of different lengths are different,
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
241 # but they cannot be ordered
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
242 self.assertTrue(not (a == b))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
243 self.assertTrue(a != b)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
244 self.assertRaises(RuntimeError, operator.lt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
245 self.assertRaises(RuntimeError, operator.le, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
246 self.assertRaises(RuntimeError, operator.gt, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
247 self.assertRaises(RuntimeError, operator.ge, a, b)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
248 a.append(17)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
249 self.assertRaises(RuntimeError, operator.eq, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
250 self.assertRaises(RuntimeError, operator.ne, a, b)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
251 a.insert(0, 11)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28917
diff changeset
252 b.insert(0, 12)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
253 self.assertTrue(not (a == b))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
254 self.assertTrue(a != b)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
255 self.assertTrue(a < b)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
256
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
257 class DictTest(unittest.TestCase):
15989
1f1eff081a2a Add test for misbehaving rich comparisons (always returning 0) --
Guido van Rossum <guido@python.org>
parents: 15988
diff changeset
258
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
259 def test_dicts(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
260 # Verify that __eq__ and __ne__ work for dicts even if the keys and
35420
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 30801
diff changeset
261 # values don't support anything other than __eq__ and __ne__ (and
c4e5ed7a2ba3 patch [ 1141428 ] more __contains__ tests
Georg Brandl <georg@python.org>
parents: 30801
diff changeset
262 # __hash__). Complex numbers are a fine example of that.
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
263 import random
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
264 imag1a = {}
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
265 for i in range(50):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
266 imag1a[random.randrange(100)*1j] = random.randrange(100)*1j
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
267 items = imag1a.items()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
268 random.shuffle(items)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
269 imag1b = {}
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
270 for k, v in items:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
271 imag1b[k] = v
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
272 imag2 = imag1b.copy()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
273 imag2[k] = v + 1.0
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
274 self.assertTrue(imag1a == imag1a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
275 self.assertTrue(imag1a == imag1b)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
276 self.assertTrue(imag2 == imag2)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 47961
diff changeset
277 self.assertTrue(imag1a != imag2)
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
278 for opname in ("lt", "le", "gt", "ge"):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
279 for op in opmap[opname]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
280 self.assertRaises(TypeError, op, imag1a, imag2)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
281
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
282 class ListTest(unittest.TestCase):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
283
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
284 def test_coverage(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
285 # exercise all comparisons for lists
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
286 x = [42]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
287 self.assertIs(x<x, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
288 self.assertIs(x<=x, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
289 self.assertIs(x==x, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
290 self.assertIs(x!=x, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
291 self.assertIs(x>x, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
292 self.assertIs(x>=x, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
293 y = [42, 42]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
294 self.assertIs(x<y, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
295 self.assertIs(x<=y, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
296 self.assertIs(x==y, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
297 self.assertIs(x!=y, True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
298 self.assertIs(x>y, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
299 self.assertIs(x>=y, False)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
300
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
301 def test_badentry(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
302 # make sure that exceptions for item comparison are properly
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
303 # propagated in list comparisons
37180
64ae32b07829 Exceptions should inherit from Exception now.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35420
diff changeset
304 class Exc(Exception):
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 16026
diff changeset
305 pass
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
306 class Bad:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
307 def __eq__(self, other):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
308 raise Exc
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
309
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
310 x = [Bad()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
311 y = [Bad()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
312
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
313 for op in opmap["eq"]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
314 self.assertRaises(Exc, op, x, y)
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 16026
diff changeset
315
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
316 def test_goodentry(self):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
317 # This test exercises the final call to PyObject_RichCompare()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
318 # in Objects/listobject.c::list_richcompare()
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
319 class Good:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
320 def __lt__(self, other):
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
321 return True
15988
ae29a89b77c3 Rich comparison tests
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
323 x = [Good()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
324 y = [Good()]
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
325
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
326 for op in opmap["lt"]:
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
327 self.assertIs(op(x, y), True)
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
328
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
329 def test_main():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58791
diff changeset
330 test_support.run_unittest(VectorTest, NumberTest, MiscTest, ListTest)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58965
diff changeset
331 with test_support.check_py3k_warnings(("dict inequality comparisons "
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58791
diff changeset
332 "not supported in 3.x",
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58791
diff changeset
333 DeprecationWarning)):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58791
diff changeset
334 test_support.run_unittest(DictTest)
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58791
diff changeset
335
28894
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
336
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
337 if __name__ == "__main__":
d1cde3daf9a9 Port test_richcmp.py to PyUnit. From SF patch #662807 which additional
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
338 test_main()