class Pile1: """ Pile implémentée sur la base d'une liste chainée simple""" def __init__(self): self.__liste = Liste() def __len__(self): return len(self.__liste) def empiler(self, valeur): """ à faire """ self.__liste._inserer_apres(valeur) def depiler(self): """ à faire """ if len(self.__liste) == 0: raise IndexError("Pile vide!") return self.__liste._supprimer_apres() def __str__(self): return str(self.__liste)
def test_supprimer_tete(): l = Liste() l._inserer_apres(5) l._inserer_apres(2, l.queue) l._inserer_apres(3, l.tete) v = l.supprimer_tete() assert v == 5 and len(l) == 2 and l.tete.valeur == 3 and l.queue.valeur == 2 assert str(l) == "3 → 2"
def test_crochet(): l = Liste() l._inserer_apres(5) l._inserer_apres(2, l.queue) l._inserer_apres(3, l.tete) assert l[0] == 5 and l[1] == 3 and l[2] == 2 with pytest.raises(IndexError): assert l[-1] assert l[20] assert l["truc"]
def test_supprimer_apres(): l = Liste() l._inserer_apres(5) l._inserer_apres(2, l.queue) l._inserer_apres(3, l.tete) v = l._supprimer_apres() assert v == 5 and l.tete.valeur == 3 and l.queue.valeur == 2 assert len(l) == 2 and str(l) == "3 → 2" v = l._supprimer_apres(l.tete) assert v == 2 and l.tete is l.queue and l.tete.valeur == 3 assert len(l) == 1 and str(l) == "3" v = l._supprimer_apres() assert v == 3 and l.tete is None and l.queue is None assert len(l) == 0 and str(l) == "None"
def test_inserer_apres(): l = Liste() l._inserer_apres(5) assert len(l) == 1 assert l.tete.valeur == 5 and l.queue.valeur == 5 assert str(l) == "5" l._inserer_apres(2, l.queue) assert l.tete.valeur == 5 and l.queue.valeur == 2 assert len(l) == 2 assert str(l) == "5 → 2" l._inserer_apres(3, l.tete) assert len(l) == 3 assert l.tete.valeur == 5 and l.queue.valeur == 2 assert str(l) == "5 → 3 → 2"
def test_in(): l = Liste() l._inserer_apres(5) l._inserer_apres(2, l.queue) l._inserer_apres(3, l.tete) assert 2 in l and 5 in l and 3 in l and 10 not in l