summaryrefslogtreecommitdiff
path: root/Lib/test/test_functools.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-04-04 22:24:03 +0000
committerRaymond Hettinger <python@rcn.com>2010-04-04 22:24:03 +0000
commit06bc0b6d2ef4fb132bfe03b24ee1b448d786f965 (patch)
tree4840bf693602a17956e3fb476da850bf283a180c /Lib/test/test_functools.py
parentb1affc517f67037fcd9027cf92fda3424b80c1ea (diff)
downloadcpython-git-06bc0b6d2ef4fb132bfe03b24ee1b448d786f965.tar.gz
Add tests for functools.total_ordering.
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r--Lib/test/test_functools.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 44992b8c5b..1629f7ce4e 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -345,6 +345,75 @@ class TestCmpToKey(unittest.TestCase):
self.assertEqual(sorted(range(5), key=functools.cmp_to_key(mycmp)),
[4, 3, 2, 1, 0])
+class TestTotalOrdering(unittest.TestCase):
+
+ def test_total_ordering_lt(self):
+ @functools.total_ordering
+ class A:
+ def __init__(self, value):
+ self.value = value
+ def __lt__(self, other):
+ return self.value < other.value
+ self.assert_(A(1) < A(2))
+ self.assert_(A(2) > A(1))
+ self.assert_(A(1) <= A(2))
+ self.assert_(A(2) >= A(1))
+ self.assert_(A(2) <= A(2))
+ self.assert_(A(2) >= A(2))
+
+ def test_total_ordering_le(self):
+ @functools.total_ordering
+ class A:
+ def __init__(self, value):
+ self.value = value
+ def __le__(self, other):
+ return self.value <= other.value
+ self.assert_(A(1) < A(2))
+ self.assert_(A(2) > A(1))
+ self.assert_(A(1) <= A(2))
+ self.assert_(A(2) >= A(1))
+ self.assert_(A(2) <= A(2))
+ self.assert_(A(2) >= A(2))
+
+ def test_total_ordering_gt(self):
+ @functools.total_ordering
+ class A:
+ def __init__(self, value):
+ self.value = value
+ def __gt__(self, other):
+ return self.value > other.value
+ self.assert_(A(1) < A(2))
+ self.assert_(A(2) > A(1))
+ self.assert_(A(1) <= A(2))
+ self.assert_(A(2) >= A(1))
+ self.assert_(A(2) <= A(2))
+ self.assert_(A(2) >= A(2))
+
+ def test_total_ordering_ge(self):
+ @functools.total_ordering
+ class A:
+ def __init__(self, value):
+ self.value = value
+ def __ge__(self, other):
+ return self.value >= other.value
+ self.assert_(A(1) < A(2))
+ self.assert_(A(2) > A(1))
+ self.assert_(A(1) <= A(2))
+ self.assert_(A(2) >= A(1))
+ self.assert_(A(2) <= A(2))
+ self.assert_(A(2) >= A(2))
+
+ def test_total_ordering_no_overwrite(self):
+ # new methods should not overwrite existing
+ @functools.total_ordering
+ class A(int):
+ pass
+ self.assert_(A(1) < A(2))
+ self.assert_(A(2) > A(1))
+ self.assert_(A(1) <= A(2))
+ self.assert_(A(2) >= A(1))
+ self.assert_(A(2) <= A(2))
+ self.assert_(A(2) >= A(2))
def test_main(verbose=None):
test_classes = (