| 1 |
import pytest |
| 2 |
from hypothesis import example, settings, given, strategies as st |
| 3 |
|
| 4 |
import pickle |
| 5 |
import sys |
| 6 |
|
| 7 |
from .support import import_fresh_module |
| 8 |
|
| 9 |
C = import_fresh_module('decimal', fresh=['_decimal']) |
| 10 |
P = import_fresh_module('decimal', blocked=['_decimal']) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
if not C: |
| 15 |
C = P |
| 16 |
|
| 17 |
@pytest.fixture(params=[C, P], ids=['_decimal', '_pydecimal']) |
| 18 |
def module(request): |
| 19 |
yield request.param |
| 20 |
|
| 21 |
|
| 22 |
CondMap = { |
| 23 |
C.Clamped: P.Clamped, |
| 24 |
C.ConversionSyntax: P.ConversionSyntax, |
| 25 |
C.DivisionByZero: P.DivisionByZero, |
| 26 |
C.DivisionImpossible: P.InvalidOperation, |
| 27 |
C.DivisionUndefined: P.DivisionUndefined, |
| 28 |
C.Inexact: P.Inexact, |
| 29 |
C.InvalidContext: P.InvalidContext, |
| 30 |
C.InvalidOperation: P.InvalidOperation, |
| 31 |
C.Overflow: P.Overflow, |
| 32 |
C.Rounded: P.Rounded, |
| 33 |
C.Subnormal: P.Subnormal, |
| 34 |
C.Underflow: P.Underflow, |
| 35 |
C.FloatOperation: P.FloatOperation, |
| 36 |
} |
| 37 |
|
| 38 |
def check_same_flags(flags_C, flags_P): |
| 39 |
for signal in flags_C: |
| 40 |
assert flags_C[signal] == flags_P[CondMap[signal]] |
| 41 |
|
| 42 |
|
| 43 |
def test_C(): |
| 44 |
sys.modules["decimal"] = C |
| 45 |
import decimal |
| 46 |
d = decimal.Decimal('1') |
| 47 |
assert isinstance(d, C.Decimal) |
| 48 |
assert isinstance(d, decimal.Decimal) |
| 49 |
assert isinstance(d.as_tuple(), C.DecimalTuple) |
| 50 |
|
| 51 |
assert d == C.Decimal('1') |
| 52 |
|
| 53 |
def check_round_trip(val, proto): |
| 54 |
d = C.Decimal(val) |
| 55 |
p = pickle.dumps(d, proto) |
| 56 |
assert d == pickle.loads(p) |
| 57 |
|
| 58 |
def test_pickle(): |
| 59 |
v = '-3.123e81723' |
| 60 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 61 |
sys.modules["decimal"] = C |
| 62 |
check_round_trip('-3.141590000', proto) |
| 63 |
check_round_trip(v, proto) |
| 64 |
|
| 65 |
cd = C.Decimal(v) |
| 66 |
pd = P.Decimal(v) |
| 67 |
cdt = cd.as_tuple() |
| 68 |
pdt = pd.as_tuple() |
| 69 |
assert cdt.__module__ == pdt.__module__ |
| 70 |
|
| 71 |
p = pickle.dumps(cdt, proto) |
| 72 |
r = pickle.loads(p) |
| 73 |
assert isinstance(r, C.DecimalTuple) |
| 74 |
assert cdt == r |
| 75 |
|
| 76 |
sys.modules["decimal"] = C |
| 77 |
p = pickle.dumps(cd, proto) |
| 78 |
sys.modules["decimal"] = P |
| 79 |
r = pickle.loads(p) |
| 80 |
assert isinstance(r, P.Decimal) |
| 81 |
assert r == pd |
| 82 |
|
| 83 |
sys.modules["decimal"] = C |
| 84 |
p = pickle.dumps(cdt, proto) |
| 85 |
sys.modules["decimal"] = P |
| 86 |
r = pickle.loads(p) |
| 87 |
assert isinstance(r, P.DecimalTuple) |
| 88 |
assert r == pdt |
| 89 |
|
| 90 |
def test_compare_total(module): |
| 91 |
assert module.Decimal('12').compare_total(module.Decimal('12.0')) == 1 |
| 92 |
assert module.Decimal('4367').compare_total(module.Decimal('NaN')) == -1 |
| 93 |
|
| 94 |
def test_compare_total_mag(module): |
| 95 |
assert module.Decimal(1).compare_total_mag(-2) == -1 |
| 96 |
|
| 97 |
def convert_arg(module, arg): |
| 98 |
if isinstance(arg, module.Decimal): |
| 99 |
return arg |
| 100 |
elif type(arg).__name__ == 'Decimal': |
| 101 |
return module.Decimal(str(arg)) |
| 102 |
else: |
| 103 |
return arg |
| 104 |
|
| 105 |
def test_subclass_fromfloat_oddity_fixed(module): |
| 106 |
|
| 107 |
class A(module.Decimal): |
| 108 |
def __init__(self, a): |
| 109 |
self.a_type = type(a) |
| 110 |
a = A.from_float(42.5) |
| 111 |
assert a.a_type is module.Decimal |
| 112 |
|
| 113 |
def test_subclass_float_constructor(module): |
| 114 |
class A(module.Decimal): |
| 115 |
pass |
| 116 |
a = A(0.25) |
| 117 |
assert type(a) is A |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
from fractions import Fraction |
| 122 |
from decimal import Decimal |
| 123 |
|
| 124 |
@given(st.decimals(), st.decimals() | st.fractions()) |
| 125 |
def test_lt(d1, d2): |
| 126 |
with C.localcontext(C.ExtendedContext) as ctx_C: |
| 127 |
d1_C = convert_arg(C, d1) |
| 128 |
d2_C = convert_arg(C, d2) |
| 129 |
try: |
| 130 |
res_C = d1_C < d2_C |
| 131 |
except Exception as e: |
| 132 |
res_C = str(type(e)) |
| 133 |
with P.localcontext(P.ExtendedContext) as ctx_P: |
| 134 |
d1_P = convert_arg(P, d1) |
| 135 |
d2_P = convert_arg(P, d2) |
| 136 |
try: |
| 137 |
res_P = d1_P < d2_P |
| 138 |
except Exception as e: |
| 139 |
res_P = str(type(e)) |
| 140 |
assert res_C == res_P |
| 141 |
check_same_flags(ctx_C.flags, ctx_P.flags) |