Repository Public MIT Research preview
1"""Tests du laboratoire S02 : le corrigé réussit, le point de départ échoue proprement, les données concordent."""2from __future__ import annotations34import math5import subprocess6import sys7from pathlib import Path89SESSION = Path(__file__).resolve().parents[2]10sys.path.insert(0, str(SESSION / "demos" / "src"))111213def lancer(dossier: str, script: str = "verifier.py") -> subprocess.CompletedProcess:14 return subprocess.run([sys.executable, script], cwd=SESSION / "labs" / dossier, capture_output=True, text=True)151617def charger(dossier: str, fichier: str) -> dict:18 espace: dict = {"__name__": "charge_pour_test"}19 exec((SESSION / "labs" / dossier / fichier).read_text(encoding="utf-8"), espace)20 return espace212223def test_corrige_reussit():24 r = lancer("solution")25 assert r.returncode == 0, r.stdout + r.stderr26 assert "4/4 étapes obligatoires réussies" in r.stdout27 assert "[ RÉUSSI ] Défi — tronquer" in r.stdout282930def test_point_de_depart_echoue_sans_planter():31 r = lancer("starter")32 assert r.returncode == 133 assert r.stdout.count("[ À FAIRE ]") == 4 and "Traceback" not in r.stderr343536def test_point_de_depart_ne_contient_pas_le_corrige():37 depart = (SESSION / "labs" / "starter" / "simulateur.py").read_text(encoding="utf-8")38 assert depart.count("raise NotImplementedError") == 439 assert "def tronquer" not in depart and "** (1 / temperature) for" not in depart404142def test_memes_donnees_partout():43 from atlas_s02.corpus import CORPUS44 from atlas_s02.modele import compter_suites, distribution_suivant4546 apres_le = distribution_suivant(compter_suites(CORPUS, 1), ("le",))47 for dossier in ("starter", "solution"):48 espace = charger(dossier, "corpus.py")49 assert espace["CORPUS"] == CORPUS50 assert list(espace["APRES_LE"]) == list(apres_le)51 assert all(math.isclose(espace["APRES_LE"][m], p) for m, p in apres_le.items())52 assert (SESSION / "labs" / "starter" / "verifier.py").read_text() == (SESSION / "labs" / "solution" / "verifier.py").read_text()535455def test_corrige_concorde_avec_la_demonstration():56 from atlas_s02.corpus import CORPUS, NOMBRE_FUSIONS57 from atlas_s02.jetons import apprendre_fusions, decouper_mot5859 sys.path.insert(0, str(SESSION / "labs" / "solution"))60 try:61 import simulateur62 fusions = simulateur.apprendre_fusions(CORPUS, NOMBRE_FUSIONS)63 assert fusions == [paire for paire, _ in apprendre_fusions(CORPUS, NOMBRE_FUSIONS)]64 phrase = "le chat dort sur le tapis .".split() # valeurs de labs/rubric.md65 comptes = [sum(len(simulateur.decouper_mot(m, simulateur.apprendre_fusions(CORPUS, n))) for m in phrase)66 for n in (5, 20, 40)]67 assert comptes == [16, 9, 7]68 assert [len(simulateur.decouper_mot(m, fusions)) for m in ("chien", "chaton", "tapisserie")] == [1, 3, 8]69 for mot in ("chaton", "chaise", "tapisserie", "chien"):70 assert simulateur.decouper_mot(mot, fusions) == decouper_mot(mot, apprendre_fusions(CORPUS, NOMBRE_FUSIONS))71 finally:72 sys.path.pop(0)73 sys.modules.pop("simulateur", None)747576def test_valeurs_du_defi_dans_la_grille():77 r = lancer("solution", "simulateur.py")78 assert r.returncode == 0, r.stderr79 for ligne in ("T = 0.5 : 7 mots différents ; « chat » sort 95 fois",80 "T = 1 : 7 mots différents ; « chat » sort 55 fois",81 "T = 2 : 7 mots différents ; « chat » sort 36 fois",82 "T = 2, k = 3 : 3 mots différents ; « chat » sort 72 fois"):83 assert ligne in r.stdout, ligne848586def test_reponses_du_parcours_sans_code():87 """Réponses de référence du parcours A (labs/rubric.md), recalculées par le code."""88 from atlas_s02.cout import cout, delai, occupation89 from atlas_s02.echantillonnage import appliquer_temperature90 from atlas_s02.jetons import compter_paires, fusionner9192 mots = {tuple("le"): 4, tuple("la"): 2, tuple("chat"): 3, tuple("chaton"): 1, tuple("tache"): 1}93 fusions = []94 for _ in range(5):95 paire, frequence = min(compter_paires(mots).items(), key=lambda pf: (-pf[1], pf[0]))96 fusions.append((paire, frequence))97 mots = {fusionner(s, paire): n for s, n in mots.items()}98 assert fusions == [(("c", "h"), 5), (("a", "t"), 4), (("ch", "at"), 4), (("l", "e"), 4), (("l", "a"), 2)]99100 def decouper(mot):101 symboles = tuple(mot)102 for paire, _ in fusions:103 symboles = fusionner(symboles, paire)104 return list(symboles)105106 assert decouper("chatte") == ["chat", "t", "e"] and decouper("cache") == ["c", "a", "ch", "e"]107 d = {"souris": 0.8, "cuisine": 0.2}108 assert [round(p, 2) for p in appliquer_temperature(d, 0.5).values()] == [0.94, 0.06]109 assert [round(p, 2) for p in appliquer_temperature(d, 2.0).values()] == [0.67, 0.33]110 assert 50 * (80 + 600) == 34000 and 50 * 300 == 15000111 assert math.isclose(cout(34000, 0, 2.0, 8.0), 0.068) and math.isclose(cout(0, 15000, 2.0, 8.0), 0.12)112 assert math.isclose(cout(34000, 15000, 2.0, 8.0), 0.188)113 assert math.isclose(delai(300, 0.6, 40.0), 8.1)114 assert occupation(680, 300, 1000) == {"jetons": 980, "part": 0.98, "libres": 20, "deborde": False}115 assert occupation(80 + 800, 300, 1000)["libres"] == -180116