summaryrefslogtreecommitdiff
path: root/Lib/test/test_decimal.py
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-04-02 08:53:22 +0000
committerMark Dickinson <dickinsm@gmail.com>2010-04-02 08:53:22 +0000
commit99d8096c174ccb025e8ff55e614ea3820f89e204 (patch)
treeb93c027821ca0e003ce090b97319f151246230ff /Lib/test/test_decimal.py
parent6eba77923565dc90d5b77b4b3f0e8f82f6109714 (diff)
downloadcpython-git-99d8096c174ccb025e8ff55e614ea3820f89e204.tar.gz
Issue #2531: Make float-to-decimal comparisons return correct results.
Float to decimal comparison operations now return a result based on the numeric values of the operands. Decimal.__hash__ has also been fixed so that Decimal and float values that compare equal have equal hash value.
Diffstat (limited to 'Lib/test/test_decimal.py')
-rw-r--r--Lib/test/test_decimal.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 35d74056c9..4071eff8db 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -1208,6 +1208,23 @@ class DecimalUsabilityTest(unittest.TestCase):
self.assertFalse(Decimal(1) < None)
self.assertTrue(Decimal(1) > None)
+ def test_decimal_float_comparison(self):
+ da = Decimal('0.25')
+ db = Decimal('3.0')
+ self.assert_(da < 3.0)
+ self.assert_(da <= 3.0)
+ self.assert_(db > 0.25)
+ self.assert_(db >= 0.25)
+ self.assert_(da != 1.5)
+ self.assert_(da == 0.25)
+ self.assert_(3.0 > da)
+ self.assert_(3.0 >= da)
+ self.assert_(0.25 < db)
+ self.assert_(0.25 <= db)
+ self.assert_(0.25 != db)
+ self.assert_(3.0 == db)
+ self.assert_(0.1 != Decimal('0.1'))
+
def test_copy_and_deepcopy_methods(self):
d = Decimal('43.24')
c = copy.copy(d)
@@ -1256,6 +1273,15 @@ class DecimalUsabilityTest(unittest.TestCase):
self.assertTrue(hash(Decimal('Inf')))
self.assertTrue(hash(Decimal('-Inf')))
+ # check that the hashes of a Decimal float match when they
+ # represent exactly the same values
+ test_strings = ['inf', '-Inf', '0.0', '-.0e1',
+ '34.0', '2.5', '112390.625', '-0.515625']
+ for s in test_strings:
+ f = float(s)
+ d = Decimal(s)
+ self.assertEqual(hash(f), hash(d))
+
# check that the value of the hash doesn't depend on the
# current context (issue #1757)
c = getcontext()