(self)
| 1564 | self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs |
| 1565 | |
| 1566 | def test_nan_comparisons(self): |
| 1567 | # comparisons involving signaling nans signal InvalidOperation |
| 1568 | |
| 1569 | # order comparisons (<, <=, >, >=) involving only quiet nans |
| 1570 | # also signal InvalidOperation |
| 1571 | |
| 1572 | # equality comparisons (==, !=) involving only quiet nans |
| 1573 | # don't signal, but return False or True respectively. |
| 1574 | Decimal = self.decimal.Decimal |
| 1575 | InvalidOperation = self.decimal.InvalidOperation |
| 1576 | localcontext = self.decimal.localcontext |
| 1577 | |
| 1578 | n = Decimal('NaN') |
| 1579 | s = Decimal('sNaN') |
| 1580 | i = Decimal('Inf') |
| 1581 | f = Decimal('2') |
| 1582 | |
| 1583 | qnan_pairs = (n, n), (n, i), (i, n), (n, f), (f, n) |
| 1584 | snan_pairs = (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s) |
| 1585 | order_ops = operator.lt, operator.le, operator.gt, operator.ge |
| 1586 | equality_ops = operator.eq, operator.ne |
| 1587 | |
| 1588 | # results when InvalidOperation is not trapped |
| 1589 | with localcontext() as ctx: |
| 1590 | ctx.traps[InvalidOperation] = 0 |
| 1591 | |
| 1592 | for x, y in qnan_pairs + snan_pairs: |
| 1593 | for op in order_ops + equality_ops: |
| 1594 | got = op(x, y) |
| 1595 | expected = True if op is operator.ne else False |
| 1596 | self.assertIs(expected, got, |
| 1597 | "expected {0!r} for operator.{1}({2!r}, {3!r}); " |
| 1598 | "got {4!r}".format( |
| 1599 | expected, op.__name__, x, y, got)) |
| 1600 | |
| 1601 | # repeat the above, but this time trap the InvalidOperation |
| 1602 | with localcontext() as ctx: |
| 1603 | ctx.traps[InvalidOperation] = 1 |
| 1604 | |
| 1605 | for x, y in qnan_pairs: |
| 1606 | for op in equality_ops: |
| 1607 | got = op(x, y) |
| 1608 | expected = True if op is operator.ne else False |
| 1609 | self.assertIs(expected, got, |
| 1610 | "expected {0!r} for " |
| 1611 | "operator.{1}({2!r}, {3!r}); " |
| 1612 | "got {4!r}".format( |
| 1613 | expected, op.__name__, x, y, got)) |
| 1614 | |
| 1615 | for x, y in snan_pairs: |
| 1616 | for op in equality_ops: |
| 1617 | self.assertRaises(InvalidOperation, operator.eq, x, y) |
| 1618 | self.assertRaises(InvalidOperation, operator.ne, x, y) |
| 1619 | |
| 1620 | for x, y in qnan_pairs + snan_pairs: |
| 1621 | for op in order_ops: |
| 1622 | self.assertRaises(InvalidOperation, op, x, y) |
| 1623 |
nothing calls this directly
no test coverage detected