+++ /dev/null
-# Tests for rich comparisons\r
-\r
-import unittest\r
-from test import test_support\r
-\r
-import operator\r
-\r
-class Number:\r
-\r
- def __init__(self, x):\r
- self.x = x\r
-\r
- def __lt__(self, other):\r
- return self.x < other\r
-\r
- def __le__(self, other):\r
- return self.x <= other\r
-\r
- def __eq__(self, other):\r
- return self.x == other\r
-\r
- def __ne__(self, other):\r
- return self.x != other\r
-\r
- def __gt__(self, other):\r
- return self.x > other\r
-\r
- def __ge__(self, other):\r
- return self.x >= other\r
-\r
- def __cmp__(self, other):\r
- raise test_support.TestFailed, "Number.__cmp__() should not be called"\r
-\r
- def __repr__(self):\r
- return "Number(%r)" % (self.x, )\r
-\r
-class Vector:\r
-\r
- def __init__(self, data):\r
- self.data = data\r
-\r
- def __len__(self):\r
- return len(self.data)\r
-\r
- def __getitem__(self, i):\r
- return self.data[i]\r
-\r
- def __setitem__(self, i, v):\r
- self.data[i] = v\r
-\r
- __hash__ = None # Vectors cannot be hashed\r
-\r
- def __nonzero__(self):\r
- raise TypeError, "Vectors cannot be used in Boolean contexts"\r
-\r
- def __cmp__(self, other):\r
- raise test_support.TestFailed, "Vector.__cmp__() should not be called"\r
-\r
- def __repr__(self):\r
- return "Vector(%r)" % (self.data, )\r
-\r
- def __lt__(self, other):\r
- return Vector([a < b for a, b in zip(self.data, self.__cast(other))])\r
-\r
- def __le__(self, other):\r
- return Vector([a <= b for a, b in zip(self.data, self.__cast(other))])\r
-\r
- def __eq__(self, other):\r
- return Vector([a == b for a, b in zip(self.data, self.__cast(other))])\r
-\r
- def __ne__(self, other):\r
- return Vector([a != b for a, b in zip(self.data, self.__cast(other))])\r
-\r
- def __gt__(self, other):\r
- return Vector([a > b for a, b in zip(self.data, self.__cast(other))])\r
-\r
- def __ge__(self, other):\r
- return Vector([a >= b for a, b in zip(self.data, self.__cast(other))])\r
-\r
- def __cast(self, other):\r
- if isinstance(other, Vector):\r
- other = other.data\r
- if len(self.data) != len(other):\r
- raise ValueError, "Cannot compare vectors of different length"\r
- return other\r
-\r
-opmap = {\r
- "lt": (lambda a,b: a< b, operator.lt, operator.__lt__),\r
- "le": (lambda a,b: a<=b, operator.le, operator.__le__),\r
- "eq": (lambda a,b: a==b, operator.eq, operator.__eq__),\r
- "ne": (lambda a,b: a!=b, operator.ne, operator.__ne__),\r
- "gt": (lambda a,b: a> b, operator.gt, operator.__gt__),\r
- "ge": (lambda a,b: a>=b, operator.ge, operator.__ge__)\r
-}\r
-\r
-class VectorTest(unittest.TestCase):\r
-\r
- def checkfail(self, error, opname, *args):\r
- for op in opmap[opname]:\r
- self.assertRaises(error, op, *args)\r
-\r
- def checkequal(self, opname, a, b, expres):\r
- for op in opmap[opname]:\r
- realres = op(a, b)\r
- # can't use assertEqual(realres, expres) here\r
- self.assertEqual(len(realres), len(expres))\r
- for i in xrange(len(realres)):\r
- # results are bool, so we can use "is" here\r
- self.assertTrue(realres[i] is expres[i])\r
-\r
- def test_mixed(self):\r
- # check that comparisons involving Vector objects\r
- # which return rich results (i.e. Vectors with itemwise\r
- # comparison results) work\r
- a = Vector(range(2))\r
- b = Vector(range(3))\r
- # all comparisons should fail for different length\r
- for opname in opmap:\r
- self.checkfail(ValueError, opname, a, b)\r
-\r
- a = range(5)\r
- b = 5 * [2]\r
- # try mixed arguments (but not (a, b) as that won't return a bool vector)\r
- args = [(a, Vector(b)), (Vector(a), b), (Vector(a), Vector(b))]\r
- for (a, b) in args:\r
- self.checkequal("lt", a, b, [True, True, False, False, False])\r
- self.checkequal("le", a, b, [True, True, True, False, False])\r
- self.checkequal("eq", a, b, [False, False, True, False, False])\r
- self.checkequal("ne", a, b, [True, True, False, True, True ])\r
- self.checkequal("gt", a, b, [False, False, False, True, True ])\r
- self.checkequal("ge", a, b, [False, False, True, True, True ])\r
-\r
- for ops in opmap.itervalues():\r
- for op in ops:\r
- # calls __nonzero__, which should fail\r
- self.assertRaises(TypeError, bool, op(a, b))\r
-\r
-class NumberTest(unittest.TestCase):\r
-\r
- def test_basic(self):\r
- # Check that comparisons involving Number objects\r
- # give the same results give as comparing the\r
- # corresponding ints\r
- for a in xrange(3):\r
- for b in xrange(3):\r
- for typea in (int, Number):\r
- for typeb in (int, Number):\r
- if typea==typeb==int:\r
- continue # the combination int, int is useless\r
- ta = typea(a)\r
- tb = typeb(b)\r
- for ops in opmap.itervalues():\r
- for op in ops:\r
- realoutcome = op(a, b)\r
- testoutcome = op(ta, tb)\r
- self.assertEqual(realoutcome, testoutcome)\r
-\r
- def checkvalue(self, opname, a, b, expres):\r
- for typea in (int, Number):\r
- for typeb in (int, Number):\r
- ta = typea(a)\r
- tb = typeb(b)\r
- for op in opmap[opname]:\r
- realres = op(ta, tb)\r
- realres = getattr(realres, "x", realres)\r
- self.assertTrue(realres is expres)\r
-\r
- def test_values(self):\r
- # check all operators and all comparison results\r
- self.checkvalue("lt", 0, 0, False)\r
- self.checkvalue("le", 0, 0, True )\r
- self.checkvalue("eq", 0, 0, True )\r
- self.checkvalue("ne", 0, 0, False)\r
- self.checkvalue("gt", 0, 0, False)\r
- self.checkvalue("ge", 0, 0, True )\r
-\r
- self.checkvalue("lt", 0, 1, True )\r
- self.checkvalue("le", 0, 1, True )\r
- self.checkvalue("eq", 0, 1, False)\r
- self.checkvalue("ne", 0, 1, True )\r
- self.checkvalue("gt", 0, 1, False)\r
- self.checkvalue("ge", 0, 1, False)\r
-\r
- self.checkvalue("lt", 1, 0, False)\r
- self.checkvalue("le", 1, 0, False)\r
- self.checkvalue("eq", 1, 0, False)\r
- self.checkvalue("ne", 1, 0, True )\r
- self.checkvalue("gt", 1, 0, True )\r
- self.checkvalue("ge", 1, 0, True )\r
-\r
-class MiscTest(unittest.TestCase):\r
-\r
- def test_misbehavin(self):\r
- class Misb:\r
- def __lt__(self_, other): return 0\r
- def __gt__(self_, other): return 0\r
- def __eq__(self_, other): return 0\r
- def __le__(self_, other): self.fail("This shouldn't happen")\r
- def __ge__(self_, other): self.fail("This shouldn't happen")\r
- def __ne__(self_, other): self.fail("This shouldn't happen")\r
- def __cmp__(self_, other): raise RuntimeError, "expected"\r
- a = Misb()\r
- b = Misb()\r
- self.assertEqual(a<b, 0)\r
- self.assertEqual(a==b, 0)\r
- self.assertEqual(a>b, 0)\r
- self.assertRaises(RuntimeError, cmp, a, b)\r
-\r
- def test_not(self):\r
- # Check that exceptions in __nonzero__ are properly\r
- # propagated by the not operator\r
- import operator\r
- class Exc(Exception):\r
- pass\r
- class Bad:\r
- def __nonzero__(self):\r
- raise Exc\r
-\r
- def do(bad):\r
- not bad\r
-\r
- for func in (do, operator.not_):\r
- self.assertRaises(Exc, func, Bad())\r
-\r
- def test_recursion(self):\r
- # Check that comparison for recursive objects fails gracefully\r
- from UserList import UserList\r
- a = UserList()\r
- b = UserList()\r
- a.append(b)\r
- b.append(a)\r
- self.assertRaises(RuntimeError, operator.eq, a, b)\r
- self.assertRaises(RuntimeError, operator.ne, a, b)\r
- self.assertRaises(RuntimeError, operator.lt, a, b)\r
- self.assertRaises(RuntimeError, operator.le, a, b)\r
- self.assertRaises(RuntimeError, operator.gt, a, b)\r
- self.assertRaises(RuntimeError, operator.ge, a, b)\r
-\r
- b.append(17)\r
- # Even recursive lists of different lengths are different,\r
- # but they cannot be ordered\r
- self.assertTrue(not (a == b))\r
- self.assertTrue(a != b)\r
- self.assertRaises(RuntimeError, operator.lt, a, b)\r
- self.assertRaises(RuntimeError, operator.le, a, b)\r
- self.assertRaises(RuntimeError, operator.gt, a, b)\r
- self.assertRaises(RuntimeError, operator.ge, a, b)\r
- a.append(17)\r
- self.assertRaises(RuntimeError, operator.eq, a, b)\r
- self.assertRaises(RuntimeError, operator.ne, a, b)\r
- a.insert(0, 11)\r
- b.insert(0, 12)\r
- self.assertTrue(not (a == b))\r
- self.assertTrue(a != b)\r
- self.assertTrue(a < b)\r
-\r
-class DictTest(unittest.TestCase):\r
-\r
- def test_dicts(self):\r
- # Verify that __eq__ and __ne__ work for dicts even if the keys and\r
- # values don't support anything other than __eq__ and __ne__ (and\r
- # __hash__). Complex numbers are a fine example of that.\r
- import random\r
- imag1a = {}\r
- for i in range(50):\r
- imag1a[random.randrange(100)*1j] = random.randrange(100)*1j\r
- items = imag1a.items()\r
- random.shuffle(items)\r
- imag1b = {}\r
- for k, v in items:\r
- imag1b[k] = v\r
- imag2 = imag1b.copy()\r
- imag2[k] = v + 1.0\r
- self.assertTrue(imag1a == imag1a)\r
- self.assertTrue(imag1a == imag1b)\r
- self.assertTrue(imag2 == imag2)\r
- self.assertTrue(imag1a != imag2)\r
- for opname in ("lt", "le", "gt", "ge"):\r
- for op in opmap[opname]:\r
- self.assertRaises(TypeError, op, imag1a, imag2)\r
-\r
-class ListTest(unittest.TestCase):\r
-\r
- def test_coverage(self):\r
- # exercise all comparisons for lists\r
- x = [42]\r
- self.assertIs(x<x, False)\r
- self.assertIs(x<=x, True)\r
- self.assertIs(x==x, True)\r
- self.assertIs(x!=x, False)\r
- self.assertIs(x>x, False)\r
- self.assertIs(x>=x, True)\r
- y = [42, 42]\r
- self.assertIs(x<y, True)\r
- self.assertIs(x<=y, True)\r
- self.assertIs(x==y, False)\r
- self.assertIs(x!=y, True)\r
- self.assertIs(x>y, False)\r
- self.assertIs(x>=y, False)\r
-\r
- def test_badentry(self):\r
- # make sure that exceptions for item comparison are properly\r
- # propagated in list comparisons\r
- class Exc(Exception):\r
- pass\r
- class Bad:\r
- def __eq__(self, other):\r
- raise Exc\r
-\r
- x = [Bad()]\r
- y = [Bad()]\r
-\r
- for op in opmap["eq"]:\r
- self.assertRaises(Exc, op, x, y)\r
-\r
- def test_goodentry(self):\r
- # This test exercises the final call to PyObject_RichCompare()\r
- # in Objects/listobject.c::list_richcompare()\r
- class Good:\r
- def __lt__(self, other):\r
- return True\r
-\r
- x = [Good()]\r
- y = [Good()]\r
-\r
- for op in opmap["lt"]:\r
- self.assertIs(op(x, y), True)\r
-\r
-def test_main():\r
- test_support.run_unittest(VectorTest, NumberTest, MiscTest, ListTest)\r
- with test_support.check_py3k_warnings(("dict inequality comparisons "\r
- "not supported in 3.x",\r
- DeprecationWarning)):\r
- test_support.run_unittest(DictTest)\r
-\r
-\r
-if __name__ == "__main__":\r
- test_main()\r