Skip to content

Commit 383fc62

Browse files
committed
Test the // and divmod() operators too
1 parent 6af16be commit 383fc62

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

‎Lib/test/test_decimal.py‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,21 +3300,23 @@ def test_divide_int_overflow(self):
33003300
Inexact = self.decimal.Inexact
33013301
Rounded = self.decimal.Rounded
33023302

3303-
# The quotient 300 fits in prec digits but not in Emax.
3304-
c = Context(prec=3, Emin=-1, Emax=1)
3305-
a, b = Decimal(75), Decimal('0.25')
3306-
for op in (c.divide_int, lambda x, y: c.divmod(x, y)[0]):
3307-
c.traps[Overflow] = True
3308-
self.assertRaises(Overflow, op, a, b)
3309-
3310-
c.traps[Overflow] = False
3303+
# The quotient 300 fits in prec digits but not in Emax. The
3304+
# operators read the active context, so make it the one under test.
3305+
with self.decimal.localcontext(Context(prec=3, Emin=-1, Emax=1)) as c:
3306+
a, b = Decimal(75), Decimal('0.25')
3307+
for op in (c.divide_int, lambda x, y: c.divmod(x, y)[0],
3308+
operator.floordiv, lambda x, y: divmod(x, y)[0]):
3309+
c.traps[Overflow] = True
3310+
self.assertRaises(Overflow, op, a, b)
3311+
3312+
c.traps[Overflow] = False
3313+
c.clear_flags()
3314+
self.assertEqual(op(a, b), Decimal('Infinity'))
3315+
for flag in (Overflow, Inexact, Rounded):
3316+
self.assertTrue(c.flags[flag])
33113317
c.clear_flags()
3312-
self.assertEqual(op(a, b), Decimal('Infinity'))
3313-
for flag in (Overflow, Inexact, Rounded):
3314-
self.assertTrue(c.flags[flag])
3315-
c.clear_flags()
3316-
self.assertEqual(c.remainder(a, b), Decimal('0.00'))
3317-
self.assertFalse(any(c.flags.values()))
3318+
self.assertEqual(c.remainder(a, b), Decimal('0.00'))
3319+
self.assertFalse(any(c.flags.values()))
33183320

33193321
def test_exp(self):
33203322
Decimal = self.decimal.Decimal

0 commit comments

Comments
 (0)