Back to index

python3.2  3.2.2
Public Member Functions
test.test_decimal.ContextFlags Class Reference

List of all members.

Public Member Functions

def test_flags_irrelevant

Detailed Description

Definition at line 2308 of file test_decimal.py.


Member Function Documentation

Definition at line 2309 of file test_decimal.py.

02309 
02310     def test_flags_irrelevant(self):
02311         # check that the result (numeric result + flags raised) of an
02312         # arithmetic operation doesn't depend on the current flags
02313 
02314         context = Context(prec=9, Emin = -999999999, Emax = 999999999,
02315                     rounding=ROUND_HALF_EVEN, traps=[], flags=[])
02316 
02317         # operations that raise various flags, in the form (function, arglist)
02318         operations = [
02319             (context._apply, [Decimal("100E-1000000009")]),
02320             (context.sqrt, [Decimal(2)]),
02321             (context.add, [Decimal("1.23456789"), Decimal("9.87654321")]),
02322             (context.multiply, [Decimal("1.23456789"), Decimal("9.87654321")]),
02323             (context.subtract, [Decimal("1.23456789"), Decimal("9.87654321")]),
02324             ]
02325 
02326         # try various flags individually, then a whole lot at once
02327         flagsets = [[Inexact], [Rounded], [Underflow], [Clamped], [Subnormal],
02328                     [Inexact, Rounded, Underflow, Clamped, Subnormal]]
02329 
02330         for fn, args in operations:
02331             # find answer and flags raised using a clean context
02332             context.clear_flags()
02333             ans = fn(*args)
02334             flags = [k for k, v in context.flags.items() if v]
02335 
02336             for extra_flags in flagsets:
02337                 # set flags, before calling operation
02338                 context.clear_flags()
02339                 for flag in extra_flags:
02340                     context._raise_error(flag)
02341                 new_ans = fn(*args)
02342 
02343                 # flags that we expect to be set after the operation
02344                 expected_flags = list(flags)
02345                 for flag in extra_flags:
02346                     if flag not in expected_flags:
02347                         expected_flags.append(flag)
02348                 expected_flags.sort(key=id)
02349 
02350                 # flags we actually got
02351                 new_flags = [k for k,v in context.flags.items() if v]
02352                 new_flags.sort(key=id)
02353 
02354                 self.assertEqual(ans, new_ans,
02355                                  "operation produces different answers depending on flags set: " +
02356                                  "expected %s, got %s." % (ans, new_ans))
02357                 self.assertEqual(new_flags, expected_flags,
02358                                   "operation raises different flags depending on flags set: " +
02359                                   "expected %s, got %s" % (expected_flags, new_flags))

Here is the call graph for this function:


The documentation for this class was generated from the following file: