summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorFacundo Batista <facundobatista@gmail.com>2007-10-19 19:25:57 (GMT)
committerFacundo Batista <facundobatista@gmail.com>2007-10-19 19:25:57 (GMT)
commit9b5e23148badbb0d11fffd05cf9d432f35631a4a (patch)
tree653ef26a251493caa5088ce7165adc97bb156294 /Lib/test
parent91ac42243cd010691b082a0b2fdd414e6d47456c (diff)
downloadcpython-9b5e23148badbb0d11fffd05cf9d432f35631a4a.zip
cpython-9b5e23148badbb0d11fffd05cf9d432f35631a4a.tar.gz
cpython-9b5e23148badbb0d11fffd05cf9d432f35631a4a.tar.bz2
The constructor from tuple was way too permissive: it allowed bad
coefficient numbers, floats in the sign, and other details that generated directly the wrong number in the best case, or triggered misfunctionality in the alorithms. Test cases added for these issues. Thanks Mark Dickinson.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_decimal.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index e4b0ae5..27d1aa6 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -452,13 +452,18 @@ class DecimalExplicitConstructionTest(unittest.TestCase):
#bad sign
self.assertRaises(ValueError, Decimal, (8, (4, 3, 4, 9, 1), 2) )
+ self.assertRaises(ValueError, Decimal, (0., (4, 3, 4, 9, 1), 2) )
+ self.assertRaises(ValueError, Decimal, (Decimal(1), (4, 3, 4, 9, 1), 2))
#bad exp
self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 'wrong!') )
+ self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), 0.) )
+ self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 9, 1), '1') )
#bad coefficients
self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, None, 1), 2) )
self.assertRaises(ValueError, Decimal, (1, (4, -3, 4, 9, 1), 2) )
+ self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) )
def test_explicit_from_Decimal(self):
@@ -1060,6 +1065,28 @@ class DecimalUsabilityTest(unittest.TestCase):
d = Decimal("Infinity")
self.assertEqual(d.as_tuple(), (0, (0,), 'F') )
+ #leading zeros in coefficient should be stripped
+ d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), -2) )
+ self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), -2) )
+ d = Decimal( (1, (0, 0, 0), 37) )
+ self.assertEqual(d.as_tuple(), (1, (0,), 37))
+ d = Decimal( (1, (), 37) )
+ self.assertEqual(d.as_tuple(), (1, (0,), 37))
+
+ #leading zeros in NaN diagnostic info should be stripped
+ d = Decimal( (0, (0, 0, 4, 0, 5, 3, 4), 'n') )
+ self.assertEqual(d.as_tuple(), (0, (4, 0, 5, 3, 4), 'n') )
+ d = Decimal( (1, (0, 0, 0), 'N') )
+ self.assertEqual(d.as_tuple(), (1, (), 'N') )
+ d = Decimal( (1, (), 'n') )
+ self.assertEqual(d.as_tuple(), (1, (), 'n') )
+
+ #coefficient in infinity should be ignored
+ d = Decimal( (0, (4, 5, 3, 4), 'F') )
+ self.assertEqual(d.as_tuple(), (0, (0,), 'F'))
+ d = Decimal( (1, (0, 2, 7, 1), 'F') )
+ self.assertEqual(d.as_tuple(), (1, (0,), 'F'))
+
def test_immutability_operations(self):
# Do operations and check that it didn't change change internal objects.