def test_adjust_round(impl, rnd, value, prec): dec = impl.Decimal(value) adj = dec.adjusted(prec, rounding=rnd) res_prec = max(prec, 0) assert adj.precision == res_prec quant = StdLibDecimal("1e%i" % -prec) # compute equivalent StdLibDecimal eq_dec = StdLibDecimal(value).quantize(quant, rnd.name) assert adj.as_fraction() == Fraction(eq_dec)
def test_cmp(impl, op, x, y): x1 = impl.Decimal(x) x2 = StdLibDecimal(x) y1 = impl.Decimal(y) y2 = StdLibDecimal(y) assert op(x1, y1) == op(x2, y2) assert op(y1, x1) == op(y2, x2) assert op(-x1, y1) == op(-x2, y2) assert op(-y1, x1) == op(-y2, x2) assert op(x1, -y1) == op(x2, -y2) assert op(y1, -x1) == op(y2, -x2)
def test_quantize_round(impl, rnd, value, quant): dec = impl.Decimal(value) adj = dec.quantize(quant, rounding=rnd) # compute equivalent StdLibDecimal if isinstance(quant, Fraction): q = StdLibDecimal(quant.numerator) / StdLibDecimal(quant.denominator) else: q = StdLibDecimal(quant) eq_dec = (StdLibDecimal(value) / q).quantize(1, rnd.name) * q if isinstance(adj, Fraction): assert adj == Fraction(eq_dec) else: assert adj.as_fraction() == Fraction(eq_dec)
dec = impl.Decimal('3.12') with pytest.raises(TypeError): dec.adjusted(precision=prec) @pytest.mark.parametrize("prec", (MAX_DEC_PRECISION + 1, -MAX_DEC_PRECISION - 1), ids=("max+1", "-max-1")) def test_adjust_limits_exceeded(impl, prec): dec = impl.Decimal("4.83") with pytest.raises(ValueError): dec.adjusted(prec) @pytest.mark.parametrize("quant", (Fraction(1, 7), StdLibDecimal("-0.3"), 0.4, 3, 1), ids=("1/7", "StdLibDecimal -0.3", "0.4", "3", "1")) @pytest.mark.parametrize("value", ("17.849", ".".join( ("1" * 2259, "4" * 33)), "0.0025", "12345678901234567e12"), ids=("compact", "large", "fraction", "int")) def test_quantize_dflt_round(impl, value, quant): dec = impl.Decimal(value) adj = dec.quantize(quant) # compute equivalent Fraction quot = Fraction(quant) equiv = round(Fraction(value) / quot) * quot assert adj == equiv @pytest.mark.parametrize("quant", (Fraction(1, 40), StdLibDecimal("-0.3"), 0.4, 3, 1),
def test_ord_ops_decimal_nan(impl, op): dec = impl.Decimal() with pytest.raises(InvalidOperation): op(dec, StdLibDecimal('Nan')) with pytest.raises(InvalidOperation): op(StdLibDecimal('Nan'), dec)
@pytest.mark.parametrize("other", ["1/5", operator.ne], ids=("other='1/5'", "other=operator.ne")) @pytest.mark.parametrize("op", [op for op in ORDERING_OPS], ids=[op.__name__ for op in ORDERING_OPS]) def test_ord_ops_non_number(impl, op, other): dec = impl.Decimal('3.12') with pytest.raises(TypeError): op(dec, other) with pytest.raises(TypeError): op(other, dec) @pytest.mark.parametrize("other", [float('Inf'), StdLibDecimal('Inf')], ids=("other='inf' (float)", "other='inf' (Decimal)")) def test_inf(impl, other): dec = impl.Decimal() chk_gt(dec, other) @pytest.mark.parametrize("other", [float('Nan'), StdLibDecimal('Nan')], ids=("other='nan' (float)", "other='nan' (Decimal)")) def test_eq_ops_nan(impl, other): dec = impl.Decimal() assert not (dec == other) assert not (other == dec) assert dec != other assert other != dec
@pytest.mark.parametrize(("value", "prec", "ratio"), ((compact_str, compact_prec, compact_ratio), (small_str, small_prec, small_ratio), (large_str, large_prec, large_ratio)), ids=("compact", "small", "large")) def test_decimal_from_decimal_no_adj(impl, value, prec, ratio): prec += 17 dec = impl.Decimal(value) dec = impl.Decimal(dec, prec) assert isinstance(dec, impl.Decimal) assert dec.precision == prec assert dec.as_fraction() == ratio @pytest.mark.parametrize(("value", "prec", "ratio"), ((StdLibDecimal("123.4567"), 4, Fraction(1234567, 10000)), (5, 0, Fraction(5, 1))), ids=("StdLibDecimal", "int")) def test_decimal_from_decimal_cls_meth(impl, value, prec, ratio): dec = impl.Decimal.from_decimal(value) assert isinstance(dec, impl.Decimal) assert dec.precision == prec assert dec.as_fraction() == ratio @pytest.mark.parametrize("value", (Fraction(12346, 100), FloatWrapper(328.5), 5.31), ids=("Fraction", "FloatWrapper", "float")) def test_decimal_from_decimal_cls_meth_wrong_type(impl, value): with pytest.raises(TypeError):