diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-05 18:56:31 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-05 18:56:31 (GMT) |
commit | c50846aaef3e38d466ac9a0a87f72f09238e2061 (patch) | |
tree | f6ae48bcfbabb5107c971c240f3b06a549084f98 /Lib/test/test_functools.py | |
parent | 5daab45158094e577b9791cda7d8a0f4e34f45cb (diff) | |
download | cpython-c50846aaef3e38d466ac9a0a87f72f09238e2061.zip cpython-c50846aaef3e38d466ac9a0a87f72f09238e2061.tar.gz cpython-c50846aaef3e38d466ac9a0a87f72f09238e2061.tar.bz2 |
Forward port total_ordering() and cmp_to_key().
Diffstat (limited to 'Lib/test/test_functools.py')
-rw-r--r-- | Lib/test/test_functools.py | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index ae47dae..5cc2a50 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -364,7 +364,89 @@ class TestReduce(unittest.TestCase): d = {"one": 1, "two": 2, "three": 3} self.assertEqual(self.func(add, d), "".join(d.keys())) - +class TestCmpToKey(unittest.TestCase): + def test_cmp_to_key(self): + def mycmp(x, y): + return y - x + self.assertEqual(sorted(range(5), key=functools.cmp_to_key(mycmp)), + [4, 3, 2, 1, 0]) + + def test_hash(self): + def mycmp(x, y): + return y - x + key = functools.cmp_to_key(mycmp) + k = key(10) + self.assertRaises(TypeError, hash(k)) + +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): + raise Exception() + 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): |