[Python-checkins] r56925 - python/branches/decimal-branch/Lib/test/test_decimal.py

facundo.batista python-checkins at python.org
Sat Aug 11 16:56:16 CEST 2007


Author: facundo.batista
Date: Sat Aug 11 16:56:15 2007
New Revision: 56925

Modified:
   python/branches/decimal-branch/Lib/test/test_decimal.py
Log:

More tests regarding the context flags. Thanks Mark Dickinson.


Modified: python/branches/decimal-branch/Lib/test/test_decimal.py
==============================================================================
--- python/branches/decimal-branch/Lib/test/test_decimal.py	(original)
+++ python/branches/decimal-branch/Lib/test/test_decimal.py	Sat Aug 11 16:56:15 2007
@@ -1100,6 +1100,58 @@
         self.assert_(new_ctx is not set_ctx, 'did not copy the context')
         self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
 
+class ContextFlags(unittest.TestCase):
+    def test_flags_irrelevant(self):
+        # check that the result (numeric result + flags raised) of an
+        # arithmetic operation doesn't depend on the current flags
+
+        context = Context(prec=9, Emin = -999999999, Emax = 999999999,
+                    rounding=ROUND_HALF_EVEN, traps=[], flags=[])
+
+        # operations that raise various flags, in the form (function, arglist)
+        operations = [
+            (context._apply, [Decimal("100E-1000000009")]),
+            (context.sqrt, [Decimal(2)]),
+            (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
+            (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
+            (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
+            ]
+
+        # try various flags individually, then a whole lot at once
+        flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal],
+                    [Inexact, Rounded, Underflow, Clamped, Subnormal]]
+
+        for fn, args in operations:
+            # find answer and flags raised using a clean context
+            context.clear_flags()
+            ans = fn(*args)
+            flags = [k for k, v in context.flags.items() if v]
+
+            for extra_flags in flagsets:
+                # set flags, before calling operation
+                context.clear_flags()
+                for flag in extra_flags:
+                    context._raise_error(flag)
+                new_ans = fn(*args)
+
+                # flags that we expect to be set after the operation
+                expected_flags = list(flags)
+                for flag in extra_flags:
+                    if flag not in expected_flags:
+                        expected_flags.append(flag)
+                expected_flags.sort()
+
+                # flags we actually got
+                new_flags = [k for k,v in context.flags.items() if v]
+                new_flags.sort()
+
+                self.assertEqual(ans, new_ans,
+                                 "operation produces different answers depending on flags set: " +
+                                 "expected %s, got %s." % (ans, new_ans))
+                self.assertEqual(new_flags, expected_flags,
+                                  "operation raises different flags depending on flags set: " +
+                                  "expected %s, got %s" % (expected_flags, new_flags))
+
 def test_main(arith=False, verbose=None, todo_tests=None, debug=None):
     """ Execute the tests.
 
@@ -1123,6 +1175,7 @@
             ContextAPItests,
             DecimalTest,
             WithStatementTest,
+            ContextFlags
         ]
     else:
         test_classes = [DecimalTest]


More information about the Python-checkins mailing list