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