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