1 | n/a | # Tests for rich comparisons |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | from test import support |
---|
5 | n/a | |
---|
6 | n/a | import operator |
---|
7 | n/a | |
---|
8 | n/a | class Number: |
---|
9 | n/a | |
---|
10 | n/a | def __init__(self, x): |
---|
11 | n/a | self.x = x |
---|
12 | n/a | |
---|
13 | n/a | def __lt__(self, other): |
---|
14 | n/a | return self.x < other |
---|
15 | n/a | |
---|
16 | n/a | def __le__(self, other): |
---|
17 | n/a | return self.x <= other |
---|
18 | n/a | |
---|
19 | n/a | def __eq__(self, other): |
---|
20 | n/a | return self.x == other |
---|
21 | n/a | |
---|
22 | n/a | def __ne__(self, other): |
---|
23 | n/a | return self.x != other |
---|
24 | n/a | |
---|
25 | n/a | def __gt__(self, other): |
---|
26 | n/a | return self.x > other |
---|
27 | n/a | |
---|
28 | n/a | def __ge__(self, other): |
---|
29 | n/a | return self.x >= other |
---|
30 | n/a | |
---|
31 | n/a | def __cmp__(self, other): |
---|
32 | n/a | raise support.TestFailed("Number.__cmp__() should not be called") |
---|
33 | n/a | |
---|
34 | n/a | def __repr__(self): |
---|
35 | n/a | return "Number(%r)" % (self.x, ) |
---|
36 | n/a | |
---|
37 | n/a | class Vector: |
---|
38 | n/a | |
---|
39 | n/a | def __init__(self, data): |
---|
40 | n/a | self.data = data |
---|
41 | n/a | |
---|
42 | n/a | def __len__(self): |
---|
43 | n/a | return len(self.data) |
---|
44 | n/a | |
---|
45 | n/a | def __getitem__(self, i): |
---|
46 | n/a | return self.data[i] |
---|
47 | n/a | |
---|
48 | n/a | def __setitem__(self, i, v): |
---|
49 | n/a | self.data[i] = v |
---|
50 | n/a | |
---|
51 | n/a | __hash__ = None # Vectors cannot be hashed |
---|
52 | n/a | |
---|
53 | n/a | def __bool__(self): |
---|
54 | n/a | raise TypeError("Vectors cannot be used in Boolean contexts") |
---|
55 | n/a | |
---|
56 | n/a | def __cmp__(self, other): |
---|
57 | n/a | raise support.TestFailed("Vector.__cmp__() should not be called") |
---|
58 | n/a | |
---|
59 | n/a | def __repr__(self): |
---|
60 | n/a | return "Vector(%r)" % (self.data, ) |
---|
61 | n/a | |
---|
62 | n/a | def __lt__(self, other): |
---|
63 | n/a | return Vector([a < b for a, b in zip(self.data, self.__cast(other))]) |
---|
64 | n/a | |
---|
65 | n/a | def __le__(self, other): |
---|
66 | n/a | return Vector([a <= b for a, b in zip(self.data, self.__cast(other))]) |
---|
67 | n/a | |
---|
68 | n/a | def __eq__(self, other): |
---|
69 | n/a | return Vector([a == b for a, b in zip(self.data, self.__cast(other))]) |
---|
70 | n/a | |
---|
71 | n/a | def __ne__(self, other): |
---|
72 | n/a | return Vector([a != b for a, b in zip(self.data, self.__cast(other))]) |
---|
73 | n/a | |
---|
74 | n/a | def __gt__(self, other): |
---|
75 | n/a | return Vector([a > b for a, b in zip(self.data, self.__cast(other))]) |
---|
76 | n/a | |
---|
77 | n/a | def __ge__(self, other): |
---|
78 | n/a | return Vector([a >= b for a, b in zip(self.data, self.__cast(other))]) |
---|
79 | n/a | |
---|
80 | n/a | def __cast(self, other): |
---|
81 | n/a | if isinstance(other, Vector): |
---|
82 | n/a | other = other.data |
---|
83 | n/a | if len(self.data) != len(other): |
---|
84 | n/a | raise ValueError("Cannot compare vectors of different length") |
---|
85 | n/a | return other |
---|
86 | n/a | |
---|
87 | n/a | opmap = { |
---|
88 | n/a | "lt": (lambda a,b: a< b, operator.lt, operator.__lt__), |
---|
89 | n/a | "le": (lambda a,b: a<=b, operator.le, operator.__le__), |
---|
90 | n/a | "eq": (lambda a,b: a==b, operator.eq, operator.__eq__), |
---|
91 | n/a | "ne": (lambda a,b: a!=b, operator.ne, operator.__ne__), |
---|
92 | n/a | "gt": (lambda a,b: a> b, operator.gt, operator.__gt__), |
---|
93 | n/a | "ge": (lambda a,b: a>=b, operator.ge, operator.__ge__) |
---|
94 | n/a | } |
---|
95 | n/a | |
---|
96 | n/a | class VectorTest(unittest.TestCase): |
---|
97 | n/a | |
---|
98 | n/a | def checkfail(self, error, opname, *args): |
---|
99 | n/a | for op in opmap[opname]: |
---|
100 | n/a | self.assertRaises(error, op, *args) |
---|
101 | n/a | |
---|
102 | n/a | def checkequal(self, opname, a, b, expres): |
---|
103 | n/a | for op in opmap[opname]: |
---|
104 | n/a | realres = op(a, b) |
---|
105 | n/a | # can't use assertEqual(realres, expres) here |
---|
106 | n/a | self.assertEqual(len(realres), len(expres)) |
---|
107 | n/a | for i in range(len(realres)): |
---|
108 | n/a | # results are bool, so we can use "is" here |
---|
109 | n/a | self.assertTrue(realres[i] is expres[i]) |
---|
110 | n/a | |
---|
111 | n/a | def test_mixed(self): |
---|
112 | n/a | # check that comparisons involving Vector objects |
---|
113 | n/a | # which return rich results (i.e. Vectors with itemwise |
---|
114 | n/a | # comparison results) work |
---|
115 | n/a | a = Vector(range(2)) |
---|
116 | n/a | b = Vector(range(3)) |
---|
117 | n/a | # all comparisons should fail for different length |
---|
118 | n/a | for opname in opmap: |
---|
119 | n/a | self.checkfail(ValueError, opname, a, b) |
---|
120 | n/a | |
---|
121 | n/a | a = list(range(5)) |
---|
122 | n/a | b = 5 * [2] |
---|
123 | n/a | # try mixed arguments (but not (a, b) as that won't return a bool vector) |
---|
124 | n/a | args = [(a, Vector(b)), (Vector(a), b), (Vector(a), Vector(b))] |
---|
125 | n/a | for (a, b) in args: |
---|
126 | n/a | self.checkequal("lt", a, b, [True, True, False, False, False]) |
---|
127 | n/a | self.checkequal("le", a, b, [True, True, True, False, False]) |
---|
128 | n/a | self.checkequal("eq", a, b, [False, False, True, False, False]) |
---|
129 | n/a | self.checkequal("ne", a, b, [True, True, False, True, True ]) |
---|
130 | n/a | self.checkequal("gt", a, b, [False, False, False, True, True ]) |
---|
131 | n/a | self.checkequal("ge", a, b, [False, False, True, True, True ]) |
---|
132 | n/a | |
---|
133 | n/a | for ops in opmap.values(): |
---|
134 | n/a | for op in ops: |
---|
135 | n/a | # calls __bool__, which should fail |
---|
136 | n/a | self.assertRaises(TypeError, bool, op(a, b)) |
---|
137 | n/a | |
---|
138 | n/a | class NumberTest(unittest.TestCase): |
---|
139 | n/a | |
---|
140 | n/a | def test_basic(self): |
---|
141 | n/a | # Check that comparisons involving Number objects |
---|
142 | n/a | # give the same results give as comparing the |
---|
143 | n/a | # corresponding ints |
---|
144 | n/a | for a in range(3): |
---|
145 | n/a | for b in range(3): |
---|
146 | n/a | for typea in (int, Number): |
---|
147 | n/a | for typeb in (int, Number): |
---|
148 | n/a | if typea==typeb==int: |
---|
149 | n/a | continue # the combination int, int is useless |
---|
150 | n/a | ta = typea(a) |
---|
151 | n/a | tb = typeb(b) |
---|
152 | n/a | for ops in opmap.values(): |
---|
153 | n/a | for op in ops: |
---|
154 | n/a | realoutcome = op(a, b) |
---|
155 | n/a | testoutcome = op(ta, tb) |
---|
156 | n/a | self.assertEqual(realoutcome, testoutcome) |
---|
157 | n/a | |
---|
158 | n/a | def checkvalue(self, opname, a, b, expres): |
---|
159 | n/a | for typea in (int, Number): |
---|
160 | n/a | for typeb in (int, Number): |
---|
161 | n/a | ta = typea(a) |
---|
162 | n/a | tb = typeb(b) |
---|
163 | n/a | for op in opmap[opname]: |
---|
164 | n/a | realres = op(ta, tb) |
---|
165 | n/a | realres = getattr(realres, "x", realres) |
---|
166 | n/a | self.assertTrue(realres is expres) |
---|
167 | n/a | |
---|
168 | n/a | def test_values(self): |
---|
169 | n/a | # check all operators and all comparison results |
---|
170 | n/a | self.checkvalue("lt", 0, 0, False) |
---|
171 | n/a | self.checkvalue("le", 0, 0, True ) |
---|
172 | n/a | self.checkvalue("eq", 0, 0, True ) |
---|
173 | n/a | self.checkvalue("ne", 0, 0, False) |
---|
174 | n/a | self.checkvalue("gt", 0, 0, False) |
---|
175 | n/a | self.checkvalue("ge", 0, 0, True ) |
---|
176 | n/a | |
---|
177 | n/a | self.checkvalue("lt", 0, 1, True ) |
---|
178 | n/a | self.checkvalue("le", 0, 1, True ) |
---|
179 | n/a | self.checkvalue("eq", 0, 1, False) |
---|
180 | n/a | self.checkvalue("ne", 0, 1, True ) |
---|
181 | n/a | self.checkvalue("gt", 0, 1, False) |
---|
182 | n/a | self.checkvalue("ge", 0, 1, False) |
---|
183 | n/a | |
---|
184 | n/a | self.checkvalue("lt", 1, 0, False) |
---|
185 | n/a | self.checkvalue("le", 1, 0, False) |
---|
186 | n/a | self.checkvalue("eq", 1, 0, False) |
---|
187 | n/a | self.checkvalue("ne", 1, 0, True ) |
---|
188 | n/a | self.checkvalue("gt", 1, 0, True ) |
---|
189 | n/a | self.checkvalue("ge", 1, 0, True ) |
---|
190 | n/a | |
---|
191 | n/a | class MiscTest(unittest.TestCase): |
---|
192 | n/a | |
---|
193 | n/a | def test_misbehavin(self): |
---|
194 | n/a | class Misb: |
---|
195 | n/a | def __lt__(self_, other): return 0 |
---|
196 | n/a | def __gt__(self_, other): return 0 |
---|
197 | n/a | def __eq__(self_, other): return 0 |
---|
198 | n/a | def __le__(self_, other): self.fail("This shouldn't happen") |
---|
199 | n/a | def __ge__(self_, other): self.fail("This shouldn't happen") |
---|
200 | n/a | def __ne__(self_, other): self.fail("This shouldn't happen") |
---|
201 | n/a | a = Misb() |
---|
202 | n/a | b = Misb() |
---|
203 | n/a | self.assertEqual(a<b, 0) |
---|
204 | n/a | self.assertEqual(a==b, 0) |
---|
205 | n/a | self.assertEqual(a>b, 0) |
---|
206 | n/a | |
---|
207 | n/a | def test_not(self): |
---|
208 | n/a | # Check that exceptions in __bool__ are properly |
---|
209 | n/a | # propagated by the not operator |
---|
210 | n/a | import operator |
---|
211 | n/a | class Exc(Exception): |
---|
212 | n/a | pass |
---|
213 | n/a | class Bad: |
---|
214 | n/a | def __bool__(self): |
---|
215 | n/a | raise Exc |
---|
216 | n/a | |
---|
217 | n/a | def do(bad): |
---|
218 | n/a | not bad |
---|
219 | n/a | |
---|
220 | n/a | for func in (do, operator.not_): |
---|
221 | n/a | self.assertRaises(Exc, func, Bad()) |
---|
222 | n/a | |
---|
223 | n/a | @support.no_tracing |
---|
224 | n/a | def test_recursion(self): |
---|
225 | n/a | # Check that comparison for recursive objects fails gracefully |
---|
226 | n/a | from collections import UserList |
---|
227 | n/a | a = UserList() |
---|
228 | n/a | b = UserList() |
---|
229 | n/a | a.append(b) |
---|
230 | n/a | b.append(a) |
---|
231 | n/a | self.assertRaises(RecursionError, operator.eq, a, b) |
---|
232 | n/a | self.assertRaises(RecursionError, operator.ne, a, b) |
---|
233 | n/a | self.assertRaises(RecursionError, operator.lt, a, b) |
---|
234 | n/a | self.assertRaises(RecursionError, operator.le, a, b) |
---|
235 | n/a | self.assertRaises(RecursionError, operator.gt, a, b) |
---|
236 | n/a | self.assertRaises(RecursionError, operator.ge, a, b) |
---|
237 | n/a | |
---|
238 | n/a | b.append(17) |
---|
239 | n/a | # Even recursive lists of different lengths are different, |
---|
240 | n/a | # but they cannot be ordered |
---|
241 | n/a | self.assertTrue(not (a == b)) |
---|
242 | n/a | self.assertTrue(a != b) |
---|
243 | n/a | self.assertRaises(RecursionError, operator.lt, a, b) |
---|
244 | n/a | self.assertRaises(RecursionError, operator.le, a, b) |
---|
245 | n/a | self.assertRaises(RecursionError, operator.gt, a, b) |
---|
246 | n/a | self.assertRaises(RecursionError, operator.ge, a, b) |
---|
247 | n/a | a.append(17) |
---|
248 | n/a | self.assertRaises(RecursionError, operator.eq, a, b) |
---|
249 | n/a | self.assertRaises(RecursionError, operator.ne, a, b) |
---|
250 | n/a | a.insert(0, 11) |
---|
251 | n/a | b.insert(0, 12) |
---|
252 | n/a | self.assertTrue(not (a == b)) |
---|
253 | n/a | self.assertTrue(a != b) |
---|
254 | n/a | self.assertTrue(a < b) |
---|
255 | n/a | |
---|
256 | n/a | def test_exception_message(self): |
---|
257 | n/a | class Spam: |
---|
258 | n/a | pass |
---|
259 | n/a | |
---|
260 | n/a | tests = [ |
---|
261 | n/a | (lambda: 42 < None, r"'<' .* of 'int' and 'NoneType'"), |
---|
262 | n/a | (lambda: None < 42, r"'<' .* of 'NoneType' and 'int'"), |
---|
263 | n/a | (lambda: 42 > None, r"'>' .* of 'int' and 'NoneType'"), |
---|
264 | n/a | (lambda: "foo" < None, r"'<' .* of 'str' and 'NoneType'"), |
---|
265 | n/a | (lambda: "foo" >= 666, r"'>=' .* of 'str' and 'int'"), |
---|
266 | n/a | (lambda: 42 <= None, r"'<=' .* of 'int' and 'NoneType'"), |
---|
267 | n/a | (lambda: 42 >= None, r"'>=' .* of 'int' and 'NoneType'"), |
---|
268 | n/a | (lambda: 42 < [], r"'<' .* of 'int' and 'list'"), |
---|
269 | n/a | (lambda: () > [], r"'>' .* of 'tuple' and 'list'"), |
---|
270 | n/a | (lambda: None >= None, r"'>=' .* of 'NoneType' and 'NoneType'"), |
---|
271 | n/a | (lambda: Spam() < 42, r"'<' .* of 'Spam' and 'int'"), |
---|
272 | n/a | (lambda: 42 < Spam(), r"'<' .* of 'int' and 'Spam'"), |
---|
273 | n/a | (lambda: Spam() <= Spam(), r"'<=' .* of 'Spam' and 'Spam'"), |
---|
274 | n/a | ] |
---|
275 | n/a | for i, test in enumerate(tests): |
---|
276 | n/a | with self.subTest(test=i): |
---|
277 | n/a | with self.assertRaisesRegex(TypeError, test[1]): |
---|
278 | n/a | test[0]() |
---|
279 | n/a | |
---|
280 | n/a | |
---|
281 | n/a | class DictTest(unittest.TestCase): |
---|
282 | n/a | |
---|
283 | n/a | def test_dicts(self): |
---|
284 | n/a | # Verify that __eq__ and __ne__ work for dicts even if the keys and |
---|
285 | n/a | # values don't support anything other than __eq__ and __ne__ (and |
---|
286 | n/a | # __hash__). Complex numbers are a fine example of that. |
---|
287 | n/a | import random |
---|
288 | n/a | imag1a = {} |
---|
289 | n/a | for i in range(50): |
---|
290 | n/a | imag1a[random.randrange(100)*1j] = random.randrange(100)*1j |
---|
291 | n/a | items = list(imag1a.items()) |
---|
292 | n/a | random.shuffle(items) |
---|
293 | n/a | imag1b = {} |
---|
294 | n/a | for k, v in items: |
---|
295 | n/a | imag1b[k] = v |
---|
296 | n/a | imag2 = imag1b.copy() |
---|
297 | n/a | imag2[k] = v + 1.0 |
---|
298 | n/a | self.assertEqual(imag1a, imag1a) |
---|
299 | n/a | self.assertEqual(imag1a, imag1b) |
---|
300 | n/a | self.assertEqual(imag2, imag2) |
---|
301 | n/a | self.assertTrue(imag1a != imag2) |
---|
302 | n/a | for opname in ("lt", "le", "gt", "ge"): |
---|
303 | n/a | for op in opmap[opname]: |
---|
304 | n/a | self.assertRaises(TypeError, op, imag1a, imag2) |
---|
305 | n/a | |
---|
306 | n/a | class ListTest(unittest.TestCase): |
---|
307 | n/a | |
---|
308 | n/a | def test_coverage(self): |
---|
309 | n/a | # exercise all comparisons for lists |
---|
310 | n/a | x = [42] |
---|
311 | n/a | self.assertIs(x<x, False) |
---|
312 | n/a | self.assertIs(x<=x, True) |
---|
313 | n/a | self.assertIs(x==x, True) |
---|
314 | n/a | self.assertIs(x!=x, False) |
---|
315 | n/a | self.assertIs(x>x, False) |
---|
316 | n/a | self.assertIs(x>=x, True) |
---|
317 | n/a | y = [42, 42] |
---|
318 | n/a | self.assertIs(x<y, True) |
---|
319 | n/a | self.assertIs(x<=y, True) |
---|
320 | n/a | self.assertIs(x==y, False) |
---|
321 | n/a | self.assertIs(x!=y, True) |
---|
322 | n/a | self.assertIs(x>y, False) |
---|
323 | n/a | self.assertIs(x>=y, False) |
---|
324 | n/a | |
---|
325 | n/a | def test_badentry(self): |
---|
326 | n/a | # make sure that exceptions for item comparison are properly |
---|
327 | n/a | # propagated in list comparisons |
---|
328 | n/a | class Exc(Exception): |
---|
329 | n/a | pass |
---|
330 | n/a | class Bad: |
---|
331 | n/a | def __eq__(self, other): |
---|
332 | n/a | raise Exc |
---|
333 | n/a | |
---|
334 | n/a | x = [Bad()] |
---|
335 | n/a | y = [Bad()] |
---|
336 | n/a | |
---|
337 | n/a | for op in opmap["eq"]: |
---|
338 | n/a | self.assertRaises(Exc, op, x, y) |
---|
339 | n/a | |
---|
340 | n/a | def test_goodentry(self): |
---|
341 | n/a | # This test exercises the final call to PyObject_RichCompare() |
---|
342 | n/a | # in Objects/listobject.c::list_richcompare() |
---|
343 | n/a | class Good: |
---|
344 | n/a | def __lt__(self, other): |
---|
345 | n/a | return True |
---|
346 | n/a | |
---|
347 | n/a | x = [Good()] |
---|
348 | n/a | y = [Good()] |
---|
349 | n/a | |
---|
350 | n/a | for op in opmap["lt"]: |
---|
351 | n/a | self.assertIs(op(x, y), True) |
---|
352 | n/a | |
---|
353 | n/a | |
---|
354 | n/a | if __name__ == "__main__": |
---|
355 | n/a | unittest.main() |
---|