def test_len(): liste = [1, 2, 2, 4, 3, 5, 5, 7, 7, 8, 1, 2, 4, 5, 6, 7, 9] m = MultiSet(liste) assert len(m) == len(liste) m.ajoute(4, 2) m.ajoute('brique', 3) assert len(m) == len(liste) + 5
def test_iter(): liste = [1, 2, 2, 4, 3, 5, 5, 7, 7, 8, 1, 2, 4, 5, 6, 7, 9] elements = [1, 2, 3, 4, 5, 6, 7, 8, 9] m = MultiSet(liste) for nombre in m: assert nombre in liste m = MultiSet() for rien in m: assert False m.ajoute('cobaye', 123) m.ajoute('sujet', 92) cpt = 0 for x in m: cpt += 1 assert cpt == 2
def test_supprime(): param = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} M = MultiSet(param) for element in M: M.ajoute(element, 2 + random.randrange(8)) # On ajoute de 2 à 9 éléments, donc # chaque élément est présent de 3 à 10 fois M.supprime(1, 20) assert 1 not in M assert M.multiplicity(1) == 0 for element in param: avant = M.multiplicity(element) M.supprime(element, 3) apres = M.multiplicity(element) assert avant == 0 or apres < avant
def test_multiplicite(): m = MultiSet() for i, lettre in enumerate(('a', 'b', 'c', 'd')): m.ajoute(lettre, i) assert m.multiplicity(lettre) == i m.ajoute('o', 18) assert m.multiplicity('o') == 18 m.ajoute('o', 12) assert m.multiplicity('o') == 30 assert m.multiplicity('test') == 0 assert m.multiplicity({1: 2, 3: 4, 5: 6, 7: 8}) == 0 m.ajoute('p', -5478) assert m.multiplicity('p') == 0 m.supprime('o', 165465) assert m.multiplicity('o') == 0
def test_ajoute(): m = MultiSet() # Signature for pas_un_nombre_valide in (-1, 0, 'a', [1, 2, 3]): m.ajoute('cobaye', pas_un_nombre_valide) assert 'cobaye' not in m # Axiomes m.ajoute(4) assert m.multiplicity(4) == 1 m.ajoute('un mot', 145) assert m.multiplicity('un mot') >= 145 m2 = MultiSet() for i in range(20): for n in (1, 4, 2, 7, 1, 3, 8, 9, 4): element = random.randrange(4) m1 = m2.copy() m2.ajoute(element, n) assert m2.multiplicity(element) - m1.multiplicity(element) == n assert len(m1) < len(m2) assert m1 < m2
def test_contains(): m = MultiSet() assert 0 not in m m.ajoute('dix-sept') assert 17 not in m assert 'dix-sept' in m