OLD | NEW |
1 # Tests for rich comparisons | 1 # Tests for rich comparisons |
2 | 2 |
3 import unittest | 3 import unittest |
4 from test import support | 4 from test import support |
5 | 5 |
6 import operator | 6 import operator |
7 | 7 |
8 class Number: | 8 class Number: |
9 | 9 |
10 def __init__(self, x): | 10 def __init__(self, x): |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 for op in opmap[opname]: | 99 for op in opmap[opname]: |
100 self.assertRaises(error, op, *args) | 100 self.assertRaises(error, op, *args) |
101 | 101 |
102 def checkequal(self, opname, a, b, expres): | 102 def checkequal(self, opname, a, b, expres): |
103 for op in opmap[opname]: | 103 for op in opmap[opname]: |
104 realres = op(a, b) | 104 realres = op(a, b) |
105 # can't use assertEqual(realres, expres) here | 105 # can't use assertEqual(realres, expres) here |
106 self.assertEqual(len(realres), len(expres)) | 106 self.assertEqual(len(realres), len(expres)) |
107 for i in range(len(realres)): | 107 for i in range(len(realres)): |
108 # results are bool, so we can use "is" here | 108 # results are bool, so we can use "is" here |
109 self.assertTrue(realres[i] is expres[i]) | 109 self.assertIs(realres[i], expres[i]) |
110 | 110 |
111 def test_mixed(self): | 111 def test_mixed(self): |
112 # check that comparisons involving Vector objects | 112 # check that comparisons involving Vector objects |
113 # which return rich results (i.e. Vectors with itemwise | 113 # which return rich results (i.e. Vectors with itemwise |
114 # comparison results) work | 114 # comparison results) work |
115 a = Vector(range(2)) | 115 a = Vector(range(2)) |
116 b = Vector(range(3)) | 116 b = Vector(range(3)) |
117 # all comparisons should fail for different length | 117 # all comparisons should fail for different length |
118 for opname in opmap: | 118 for opname in opmap: |
119 self.checkfail(ValueError, opname, a, b) | 119 self.checkfail(ValueError, opname, a, b) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 self.assertEqual(realoutcome, testoutcome) | 156 self.assertEqual(realoutcome, testoutcome) |
157 | 157 |
158 def checkvalue(self, opname, a, b, expres): | 158 def checkvalue(self, opname, a, b, expres): |
159 for typea in (int, Number): | 159 for typea in (int, Number): |
160 for typeb in (int, Number): | 160 for typeb in (int, Number): |
161 ta = typea(a) | 161 ta = typea(a) |
162 tb = typeb(b) | 162 tb = typeb(b) |
163 for op in opmap[opname]: | 163 for op in opmap[opname]: |
164 realres = op(ta, tb) | 164 realres = op(ta, tb) |
165 realres = getattr(realres, "x", realres) | 165 realres = getattr(realres, "x", realres) |
166 self.assertTrue(realres is expres) | 166 self.assertIs(realres, expres) |
167 | 167 |
168 def test_values(self): | 168 def test_values(self): |
169 # check all operators and all comparison results | 169 # check all operators and all comparison results |
170 self.checkvalue("lt", 0, 0, False) | 170 self.checkvalue("lt", 0, 0, False) |
171 self.checkvalue("le", 0, 0, True ) | 171 self.checkvalue("le", 0, 0, True ) |
172 self.checkvalue("eq", 0, 0, True ) | 172 self.checkvalue("eq", 0, 0, True ) |
173 self.checkvalue("ne", 0, 0, False) | 173 self.checkvalue("ne", 0, 0, False) |
174 self.checkvalue("gt", 0, 0, False) | 174 self.checkvalue("gt", 0, 0, False) |
175 self.checkvalue("ge", 0, 0, True ) | 175 self.checkvalue("ge", 0, 0, True ) |
176 | 176 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 self.assertRaises(RuntimeError, operator.eq, a, b) | 231 self.assertRaises(RuntimeError, operator.eq, a, b) |
232 self.assertRaises(RuntimeError, operator.ne, a, b) | 232 self.assertRaises(RuntimeError, operator.ne, a, b) |
233 self.assertRaises(RuntimeError, operator.lt, a, b) | 233 self.assertRaises(RuntimeError, operator.lt, a, b) |
234 self.assertRaises(RuntimeError, operator.le, a, b) | 234 self.assertRaises(RuntimeError, operator.le, a, b) |
235 self.assertRaises(RuntimeError, operator.gt, a, b) | 235 self.assertRaises(RuntimeError, operator.gt, a, b) |
236 self.assertRaises(RuntimeError, operator.ge, a, b) | 236 self.assertRaises(RuntimeError, operator.ge, a, b) |
237 | 237 |
238 b.append(17) | 238 b.append(17) |
239 # Even recursive lists of different lengths are different, | 239 # Even recursive lists of different lengths are different, |
240 # but they cannot be ordered | 240 # but they cannot be ordered |
241 self.assertTrue(not (a == b)) | 241 self.assertFalse(a == b) |
242 self.assertTrue(a != b) | 242 self.assertTrue(a != b) |
243 self.assertRaises(RuntimeError, operator.lt, a, b) | 243 self.assertRaises(RuntimeError, operator.lt, a, b) |
244 self.assertRaises(RuntimeError, operator.le, a, b) | 244 self.assertRaises(RuntimeError, operator.le, a, b) |
245 self.assertRaises(RuntimeError, operator.gt, a, b) | 245 self.assertRaises(RuntimeError, operator.gt, a, b) |
246 self.assertRaises(RuntimeError, operator.ge, a, b) | 246 self.assertRaises(RuntimeError, operator.ge, a, b) |
247 a.append(17) | 247 a.append(17) |
248 self.assertRaises(RuntimeError, operator.eq, a, b) | 248 self.assertRaises(RuntimeError, operator.eq, a, b) |
249 self.assertRaises(RuntimeError, operator.ne, a, b) | 249 self.assertRaises(RuntimeError, operator.ne, a, b) |
250 a.insert(0, 11) | 250 a.insert(0, 11) |
251 b.insert(0, 12) | 251 b.insert(0, 12) |
252 self.assertTrue(not (a == b)) | 252 self.assertFalse(a == b) |
253 self.assertTrue(a != b) | 253 self.assertTrue(a != b) |
254 self.assertTrue(a < b) | 254 self.assertTrue(a < b) |
255 | 255 |
256 class DictTest(unittest.TestCase): | 256 class DictTest(unittest.TestCase): |
257 | 257 |
258 def test_dicts(self): | 258 def test_dicts(self): |
259 # Verify that __eq__ and __ne__ work for dicts even if the keys and | 259 # Verify that __eq__ and __ne__ work for dicts even if the keys and |
260 # values don't support anything other than __eq__ and __ne__ (and | 260 # values don't support anything other than __eq__ and __ne__ (and |
261 # __hash__). Complex numbers are a fine example of that. | 261 # __hash__). Complex numbers are a fine example of that. |
262 import random | 262 import random |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 | 324 |
325 for op in opmap["lt"]: | 325 for op in opmap["lt"]: |
326 self.assertIs(op(x, y), True) | 326 self.assertIs(op(x, y), True) |
327 | 327 |
328 | 328 |
329 def test_main(): | 329 def test_main(): |
330 support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest) | 330 support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest) |
331 | 331 |
332 if __name__ == "__main__": | 332 if __name__ == "__main__": |
333 test_main() | 333 test_main() |
OLD | NEW |