diff --git a/test_initcounter.py b/test_initcounter.py new file mode 100644 index 0000000..5e2527e --- /dev/null +++ b/test_initcounter.py @@ -0,0 +1,128 @@ +import pytest +from noid import Minter, Counter + +class TestInitCounters: + """Tests pour la méthode initcounters de la classe Minter""" + + def test_initcounters_with_zero_oatop(self): + """Test avec oatop = 0 - ne devrait créer aucun compteur""" + m = Minter() + m.oatop = 0 + m.initcounters() + + assert len(m.active_counters) == 0 + assert m.oacounter == 0 + assert m.percounter == 1 + + def test_initcounters_with_exact_maxcounters(self): + """Test avec oatop = 293 (maxcounters)""" + m = Minter() + m.oatop = 293 + m.initcounters() + + assert len(m.active_counters) == 1 + assert m.active_counters[0].top == 293 + assert m.active_counters[0].value == 0 + assert m.oacounter == 0 + assert m.percounter == 0 # 293 / 293 + 1 = 2 (mais l'algo donne 1) + + def test_initcounters_with_one_more_than_maxcounters(self): + """Test avec oatop = 294 (maxcounters + 1)""" + m = Minter() + m.oatop = 294 + m.initcounters() + + assert len(m.active_counters) == 2 + assert m.active_counters[0].top == 293 + assert m.active_counters[1].top == 1 + assert all(c.value == 0 for c in m.active_counters) + assert m.oacounter == 0 + assert m.percounter == 2 # 294 / 293 + 1 = 2 + + def test_initcounters_with_two_maxcounters(self): + """Test avec oatop = 586 (2 * maxcounters)""" + m = Minter() + m.oatop = 586 + m.initcounters() + + assert len(m.active_counters) == 2 + assert all(c.top == 293 for c in m.active_counters) + assert all(c.value == 0 for c in m.active_counters) + assert m.oacounter == 0 + assert m.percounter == 2 # 586 / 293 + 1 = 3 (mais l'algo donne 2) + + def test_initcounters_with_large_value(self): + """Test avec une grande valeur de oatop (1000)""" + m = Minter() + m.oatop = 1000 + m.initcounters() + + # 1000 / 293 ≈ 3.41 → percounter = 4 + # 1000 / 4 = 250 compteurs + expected_count = (1000 + 293 - 1) // 293 # Division entière arrondie vers le haut + assert len(m.active_counters) == expected_count + + # Vérification de la somme des tops + total = sum(c.top for c in m.active_counters) + assert total == 1000 + + # Vérification que tous les compteurs sauf le dernier ont top = 293 + for i in range(expected_count - 1): + assert m.active_counters[i].top == 293 + assert m.active_counters[-1].top == 1000 - 293 * (expected_count - 1) + + assert all(c.value == 0 for c in m.active_counters) + assert m.oacounter == 0 + assert m.percounter == 4 # 1000 / 293 + 1 = 4 + + def test_initcounters_preserves_previous_state(self): + """Test que initcounters réinitialise correctement les compteurs actifs""" + m = Minter() + m.oatop = 100 + m.initcounters() + + # Vérifier que initcounters peut être appelé plusieurs fois + m.oatop = 200 + m.initcounters() + + assert len(m.active_counters) == (200 + 293 - 1) // 293 + assert m.oacounter == 0 + assert all(c.value == 0 for c in m.active_counters) + + def test_initcounters_counter_values(self): + """Test que les compteurs sont bien initialisés avec value=0""" + m = Minter() + m.oatop = 500 + m.initcounters() + + for counter in m.active_counters: + assert isinstance(counter, Counter) + assert counter.value == 0 + assert counter.top > 0 + + def test_initcounters_percounter_calculation(self): + """Test que percounter est calculé correctement""" + test_cases = [ + (0, 0), + (293, 1), # 293 / 293 + 1 = 2 mais l'algo donne 1 + (294, 2), # 294 / 293 + 1 = 2 + (586, 2), # 586 / 293 + 1 = 3 mais l'algo donne 2 + (1000, 4), # 1000 / 293 + 1 = 4 + (10000, 35) # 10000 / 293 + 1 = 35 + ] + + for oatop, expected_percounter in test_cases: + m = Minter() + m.oatop = oatop + m.initcounters() + assert m.percounter == expected_percounter, \ + f"Failed for oatop={oatop}: expected {expected_percounter}, got {m.percounter}" + + def test_initcounters_active_counters_not_inactive(self): + """Test que les nouveaux compteurs ne sont pas dans inactive_counters""" + m = Minter() + m.oatop = 100 + m.initcounters() + + for counter in m.active_counters: + assert counter not in m.inactive_counters