# Vérifications de Qui a mis le chat dans la boîte v0.3.1 (bibliothèque standard seulement)
import sys
from fractions import Fraction as F

out = []
def ok(label, cond):
    out.append(f"{label}   {'OK' if cond else 'ECHEC'}")
    assert cond, label

def racine(n):
    r = n % 9
    return 9 if r == 0 else r

# 0.1 L'écriture n = 9k + racine rend le nombre entier
ok("0.1 Écriture n = 9 × k + racine, k tours, racine de 1 à 9, n = 1..100000",
   all(9 * ((n - racine(n)) // 9) + racine(n) == n and 1 <= racine(n) <= 9 for n in range(1, 100001)))

# 0.2 Tables inverses
t2 = [racine(2 * i) for i in range(1, 10)]
t7 = [racine(7 * i) for i in range(1, 10)]
parcourent = [a for a in range(1, 10) if sorted(racine(a * i) for i in range(1, 10)) == list(range(1, 10))]
ok(f"0.2 Table de 2 : {t2} ; table de 7 : {t7} ; sommes sur huit colonnes = 9 ; tables qui parcourent : {parcourent}",
   t2 == [2, 4, 6, 8, 1, 3, 5, 7, 9] and t7 == [7, 5, 3, 1, 8, 6, 4, 2, 9]
   and all(t2[i] + t7[i] == 9 for i in range(8)) and parcourent == [1, 2, 4, 5, 7, 8])

# 0.3 Reçu de Stevin
ecart = (F(1, 3) - F(3333, 10000)) * 3 ** 100
ordre = len(str(int(ecart))) - 1
ok(f"0.3 Stevin : 1/3 écrit 0,3333, multiplié cent fois par 3 : écart de l'ordre de 10^{ordre}", ordre == 43)

# 0.4 Le rendez-vous du 5 et du 7, à chaque tour
rdv = all(racine((6*m - 1) + (6*m + 1)) in (3, 6, 9) and (6*m - 1) * (6*m + 1) + 1 == (6*m) ** 2
          for m in range(1, 1001))
ok(f"0.4 Rendez-vous : 5 + 7 = {5+7} (racine {racine(12)}), 5 × 7 = {5*7}, 35 + 1 = 36 = 6 × 6 ; "
   "pour m = 1..1000, somme 12m sur l'axe 3-6-9, produit juste avant le carré de 6m", 5 * 7 == 35 and 35 + 1 == 36 and racine(12) == 3 and rdv)

# 4.2 Le chat et la chambre
masse, conso, vol, fo2 = F(4), F(4), F(50), F(209, 1000)
o2_h = masse * conso * 60 / 1000
o2_box = vol * fo2
part = o2_h / o2_box * 100
ok(f"4.2 Oxygène bu en une heure : {float(o2_h):.2f} L ; oxygène de la chambre : {float(o2_box):.2f} L ; part {float(part):.1f} %",
   o2_h == F(96, 100) and o2_box == F(1045, 100) and 9 < part < 10)
demi = [1 - F(1, 2) ** h for h in (1, 2, 3)]
ok(f"4.2 Demi-vie d'une heure : chances de désintégration en 1, 2, 3 heures = {', '.join(str(d) for d in demi)}",
   demi == [F(1, 2), F(3, 4), F(7, 8)])
ok("4.2 Respirations en une heure au rythme de repos (25 par minute) : 1500", 25 * 60 == 1500)

# 6.2 Ce que l'ouverture rend contre ce que le chemin avait
issues_chemin = 3600 + 1
issues_ouverture = 2
ok(f"6.2 Compteur : {issues_chemin} issues datées (un clic à l'une des 3600 secondes, ou aucun) ; "
   f"ouverture : {issues_ouverture} issues lues", issues_chemin == 3601 and issues_ouverture == 2 and issues_chemin > issues_ouverture)
ok(f"6.2 Plancher : au pas de la milliseconde, {3600*1000 + 1} issues datées", 3600 * 1000 + 1 == 3600001)

# Typographie
src = open(sys.argv[1], encoding="utf-8").read() if len(sys.argv) > 1 else ""
ok("Typographie : aucun tiret cadratin", len(src) > 0 and "—" not in src)

print("\n".join(out))
