diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-06-11 10:44:52 +0000 |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-06-11 10:44:52 +0000 |
commit | f7493243b80a99f58b75d2081b6c25a117cd7e93 (patch) | |
tree | 90fbb9145f4509befc3331e1e4d2bb83cbfab632 /Lib/test/test_numeric_tower.py | |
parent | ae09ad4799527c431ee5e1370f70b8c5d8d19c79 (diff) | |
download | cpython-f7493243b80a99f58b75d2081b6c25a117cd7e93.tar.gz |
Issue #8188: Comparisons between Decimal objects and other numeric
objects (Fraction, float, complex, int) now all function as expected.
Diffstat (limited to 'Lib/test/test_numeric_tower.py')
-rw-r--r-- | Lib/test/test_numeric_tower.py | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/Lib/test/test_numeric_tower.py b/Lib/test/test_numeric_tower.py index eafdb0f81f..b0c953731e 100644 --- a/Lib/test/test_numeric_tower.py +++ b/Lib/test/test_numeric_tower.py @@ -143,9 +143,64 @@ class HashTest(unittest.TestCase): x = {'halibut', HalibutProxy()} self.assertEqual(len(x), 1) +class ComparisonTest(unittest.TestCase): + def test_mixed_comparisons(self): + + # ordered list of distinct test values of various types: + # int, float, Fraction, Decimal + test_values = [ + float('-inf'), + D('-1e999999999'), + -1e308, + F(-22, 7), + -3.14, + -2, + 0.0, + 1e-320, + True, + F('1.2'), + D('1.3'), + float('1.4'), + F(275807, 195025), + D('1.414213562373095048801688724'), + F(114243, 80782), + F(473596569, 84615), + 7e200, + D('infinity'), + ] + for i, first in enumerate(test_values): + for second in test_values[i+1:]: + self.assertLess(first, second) + self.assertLessEqual(first, second) + self.assertGreater(second, first) + self.assertGreaterEqual(second, first) + + def test_complex(self): + # comparisons with complex are special: equality and inequality + # comparisons should always succeed, but order comparisons should + # raise TypeError. + z = 1.0 + 0j + w = -3.14 + 2.7j + + for v in 1, 1.0, F(1), D(1), complex(1): + self.assertEqual(z, v) + self.assertEqual(v, z) + + for v in 2, 2.0, F(2), D(2), complex(2): + self.assertNotEqual(z, v) + self.assertNotEqual(v, z) + self.assertNotEqual(w, v) + self.assertNotEqual(v, w) + + for v in (1, 1.0, F(1), D(1), complex(1), + 2, 2.0, F(2), D(2), complex(2), w): + for op in operator.le, operator.lt, operator.ge, operator.gt: + self.assertRaises(TypeError, op, z, v) + self.assertRaises(TypeError, op, v, z) + def test_main(): - run_unittest(HashTest) + run_unittest(HashTest, ComparisonTest) if __name__ == '__main__': test_main() |