from decimal import Decimal
from fractions import Fraction
from random import Random

assert Fraction(1, 3) + Fraction(2, 3) == 1
assert Decimal("0.10") + Decimal("0.20") == Decimal("0.30")
sample = Random(42).sample(["A", "B", "C", "D"], k=2)
assert len(sample) == len(set(sample)) == 2


import math

assert math.pow(5, 2) == 25.0 and 5 ** 2 == 25
assert math.sqrt(25) == 5.0 and math.fabs(-3) == 3.0
assert math.isclose(math.degrees(math.pi), 180.0)
assert math.isclose(math.sin(math.pi), 0.0, abs_tol=1e-12)
assert (math.floor(-2.3), math.ceil(-2.3), math.trunc(-2.3)) == (-3, -2, -2)

assert Fraction(-5, 30) == Fraction(-1, 6)
assert Fraction(0.5) == Fraction.from_float(0.5) == Fraction(1, 2)
assert Fraction(0.1) != Fraction(1, 10)
assert Fraction("0.1") * Fraction(1, 4) == Fraction(1, 40)
try:
    Fraction(1, 0)
except ZeroDivisionError:
    pass
else:
    raise AssertionError("Dénominateur nul accepté")

rng = Random(17)
assert 0.0 <= rng.random() < 1.0
assert rng.randrange(5, 10, 2) in (5, 7, 9)
assert 1 <= rng.randint(1, 6) <= 6
lots = ["A", "B", "C"]
assert rng.choice(lots) in lots
melanges = lots.copy()
assert rng.shuffle(melanges) is None
assert sorted(melanges) == lots and lots == ["A", "B", "C"]


assert math.exp(0) == 1.0 and abs(-3) == 3
assert math.isclose(math.radians(180), math.pi)
assert math.isclose(math.cos(0), 1.0)
assert math.isclose(math.degrees(math.asin(1)), 90.0)
assert (math.ceil(2.3), math.floor(5.8), math.trunc(9.5)) == (3, 5, 9)
assert Fraction() == 0 and Fraction("1/4") == Fraction(1, 4)
assert float(Fraction(1, 4)) == 0.25
assert sum([Fraction(1, 10)] * 3) == Fraction(3, 10)
assert 0.1 + 0.1 + 0.1 != 0.3
try:
    math.sqrt(-1)
except ValueError:
    pass
else:
    raise AssertionError("Une racine réelle négative doit être refusée")


assert Fraction(1, 10) + 5 == Fraction(51, 10)
assert Fraction(1, 2) / Fraction(1, 4) == 2
assert Fraction(1, 4) / Fraction(1, 2) == Fraction(1, 2)
population = ["lot-A", "lot-B", "lot-C", "lot-D"]
assert Random(42).sample(population, 3) == Random(42).sample(population, 3)
assert Random(3).sample(["A", "A"], 2) == ["A", "A"]
assert Random(3).choices(["A"], k=3) == ["A", "A", "A"]
try:
    Random(3).choice([])
except IndexError:
    pass
else:
    raise AssertionError("Un choix dans une séquence vide doit échouer")

assert Fraction(1, 10) * Fraction(1, 4) == Fraction(1, 40)
assert Fraction(1, 10) + 5 == Fraction(51, 10)
assert Fraction(1, 2) / Fraction(1, 4) == 2
assert Fraction(1, 4) / Fraction(1, 2) == Fraction(1, 2)
rng_a = Random(42)
rng_b = Random(42)
assert [rng_a.randrange(50) for _ in range(5)] == [rng_b.randrange(50) for _ in range(5)]
try:
    rng_a.choice([])
except IndexError:
    pass
else:
    raise AssertionError("On ne peut pas choisir dans une séquence vide")

montant_texte = Decimal("0.1")
montant_depuis_float = Decimal(0.1)
assert montant_texte == Decimal("0.10")
assert montant_depuis_float != montant_texte
assert montant_depuis_float == Decimal.from_float(0.1)

compteur_exact = 2 ** 53 + 1
compteur_flottant = math.pow(2, 53) + 1.0
assert compteur_exact == 9007199254740993
assert compteur_flottant == float(2 ** 53)
assert int(compteur_flottant) != compteur_exact

population_lots = ["A", "A", "B"]
tirage_local = Random(42)
assert tirage_local.sample(population_lots, 0) == []
assert sorted(tirage_local.sample(population_lots, 3)) == ["A", "A", "B"]
try:
    tirage_local.sample(population_lots, 4)
except ValueError:
    pass
else:
    raise AssertionError("Un échantillon trop grand doit être refusé")

from decimal import ROUND_HALF_UP, ROUND_HALF_EVEN

prix = Decimal("1.245")
centime = Decimal("0.01")
assert prix.quantize(centime, rounding=ROUND_HALF_UP) == Decimal("1.25")
assert prix.quantize(centime, rounding=ROUND_HALF_EVEN) == Decimal("1.24")
prix_lignes = [Decimal("0.005"), Decimal("0.005")]
arrondi_par_ligne = sum(p.quantize(centime, rounding=ROUND_HALF_UP) for p in prix_lignes)
arrondi_total = sum(prix_lignes).quantize(centime, rounding=ROUND_HALF_UP)
assert arrondi_par_ligne == Decimal("0.02")
assert arrondi_total == Decimal("0.01")

import math
mesures = [1e16, 1.0, -1e16]
assert (1e16 + 1.0) - 1e16 == 0.0
assert math.fsum(mesures) == 1.0

quantite_decimal = Decimal("0.10")
assert Fraction(quantite_decimal) == Fraction(1, 10)
assert Fraction(Decimal(0.1)) != Fraction(1, 10)
assert Fraction(Decimal(0.1)) == Fraction(0.1)

from fractions import Fraction

part_lot = Fraction(1, 10)
avec_entier = part_lot + 5
avec_flottant = part_lot + 0.2
avec_fraction = part_lot + Fraction("0.2")
assert avec_entier == Fraction(51, 10)
assert isinstance(avec_entier, Fraction)
assert isinstance(avec_flottant, float)
assert avec_flottant == 0.30000000000000004
assert avec_fraction == Fraction(3, 10)

from decimal import Decimal, ROUND_HALF_EVEN, ROUND_HALF_UP, localcontext

with localcontext() as contexte:
    contexte.rounding = ROUND_HALF_UP
    assert round(Decimal("1.25"), 1) == Decimal("1.3")
    assert round(Decimal("2.5")) == 2
with localcontext() as contexte:
    contexte.rounding = ROUND_HALF_EVEN
    assert round(Decimal("1.25"), 1) == Decimal("1.2")

generateur_lots = Random(42)
for borne_invalide in (5.0, Fraction(5, 1)):
    try:
        generateur_lots.randrange(borne_invalide)
    except TypeError:
        pass
    else:
        raise AssertionError("Une borne non entière doit être refusée")
for bornes in ((5, 10, 0), (5, 5, 1)):
    try:
        generateur_lots.randrange(*bornes)
    except ValueError:
        pass
    else:
        raise AssertionError("Un pas nul ou une plage vide doit être refusé")
assert generateur_lots.randrange(5, 10, 2) in (5, 7, 9)

lignes_acceptees, lignes_lues = 24, 60
proportion = Fraction(lignes_acceptees, lignes_lues)
copie_proportion = Fraction(proportion)
assert copie_proportion == Fraction(2, 5)
assert (copie_proportion.numerator, copie_proportion.denominator) == (2, 5)
assert float(copie_proportion) == 0.4
try:
    Fraction(0, 0)
except ZeroDivisionError:
    pass
else:
    raise AssertionError("Un taux avec dénominateur nul doit être signalé")
