[Python-checkins] r87637 - python/branches/py3k-cdecimal/Lib/test/decimal_tests.py
stefan.krah
python-checkins at python.org
Sun Jan 2 18:23:11 CET 2011
Author: stefan.krah
Date: Sun Jan 2 18:23:11 2011
New Revision: 87637
Log:
Add bignum and quantize() api tests.
Modified:
python/branches/py3k-cdecimal/Lib/test/decimal_tests.py
Modified: python/branches/py3k-cdecimal/Lib/test/decimal_tests.py
==============================================================================
--- python/branches/py3k-cdecimal/Lib/test/decimal_tests.py (original)
+++ python/branches/py3k-cdecimal/Lib/test/decimal_tests.py Sun Jan 2 18:23:11 2011
@@ -2128,6 +2128,38 @@
self.assertEqual(repr(context.create_decimal_from_float(10)),
"Decimal('10')")
+ def test_quantize(self):
+ c = Context(Emax=99999, Emin=-99999)
+ self.assertEqual(
+ Decimal('7.335').quantize(Decimal('.01')),
+ Decimal('7.34')
+ )
+ self.assertEqual(
+ Decimal('7.335').quantize(Decimal('.01'), rounding=ROUND_DOWN),
+ Decimal('7.33')
+ )
+ self.assertRaises(
+ InvalidOperation,
+ Decimal("10e99999").quantize, Decimal('1e100000'), context=c
+ )
+ if HAVE_CDECIMAL:
+ self.assertRaises(
+ TypeError,
+ Decimal("1.23456789").quantize, Decimal('1e-100000'), []
+ )
+ self.assertRaises(
+ TypeError,
+ Decimal("1.23456789").quantize, Decimal('1e-100000'), c
+ )
+ self.assertRaises(
+ ValueError,
+ Decimal("1.23456789").quantize, Decimal('1e-100000'), 10
+ )
+ self.assertRaises(
+ TypeError,
+ Decimal("1.23456789").quantize, Decimal('1e-100000'), ROUND_UP, 1000
+ )
+
class ContextAPItests(unittest.TestCase):
def test_pickle(self):
@@ -2892,10 +2924,10 @@
for attr in ['capitals', 'clamp', '_allcr']:
self.assertRaises(ValueError, setattr, c, attr, -1)
self.assertRaises(ValueError, setattr, c, attr, 2)
+ self.assertRaises(TypeError, setattr, c, attr, [1,2,3])
if HAVE_CONFIG_64:
self.assertRaises(ValueError, setattr, c, attr, 2**32)
self.assertRaises(ValueError, setattr, c, attr, 2**32+1)
- self.assertRaises(TypeError, setattr, c, attr, [1,2,3])
# Specific: _flags, _traps
for attr in ['_flags', '_traps']:
@@ -3051,14 +3083,17 @@
self.assertEqual(x.to_integral(), 10)
self.assertRaises(TypeError, x.to_integral, '10')
self.assertRaises(TypeError, x.to_integral, 10, 'x')
+ self.assertRaises(ValueError, x.to_integral, 10)
self.assertEqual(x.to_integral_value(), 10)
self.assertRaises(TypeError, x.to_integral_value, '10')
self.assertRaises(TypeError, x.to_integral_value, 10, 'x')
+ self.assertRaises(ValueError, x.to_integral_value, 10)
self.assertEqual(x.to_integral_exact(), 10)
self.assertRaises(TypeError, x.to_integral_exact, '10')
self.assertRaises(TypeError, x.to_integral_exact, 10, 'x')
+ self.assertRaises(ValueError, x.to_integral_exact, 10)
with localcontext() as c:
x = Decimal("99999999999999999999999999.9").to_integral_value(ROUND_UP)
@@ -3294,6 +3329,18 @@
self.assertTrue(c._traps&DecIEEEInvalidOperation)
assertIsExclusivelySet(InvalidOperation, c.traps)
+ def test_bignum(self):
+ b1 = 10**35
+ b2 = 10**36
+ with localcontext() as c:
+ c.prec = 1000000
+ for i in range(5):
+ a = random.randrange(b1, b2)
+ b = random.randrange(1000, 1200)
+ x = a ** b
+ y = Decimal(a) ** Decimal(b)
+ self.assertEqual(x, y)
+
def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
""" Execute the tests.
More information about the Python-checkins
mailing list