cours_ada/semestre4/TP4_arbres_binaires_genericite/tester_abr_entiers.adb
2021-08-22 13:24:45 +02:00

168 lines
6.2 KiB
Ada

-- Date : Mars 2015
-- Auteur : MJ. Huguet
--
-- Objet : test Arbre Binaire Générique (avec des entiers)
--------------------------------------------------------------------------------
with Afficher_Test;
with Ada.Text_io; use Ada.Text_Io;
with Arbre_Bin_Recherche_Cle_G;
procedure Tester_Abr_entiers is
-----------------------------------------------------------------------------
-- Instanciation Arbre Binaire Générique sur des entiers
procedure Liberer_Entier(N : in out Integer) is
begin
null;
end Liberer_Entier;
function Cle(N : Integer) return Integer is
begin
return N;
end Cle;
package Arbre_Entiers is new Arbre_Bin_Recherche_Cle_G(Integer, Integer, Cle, "<", "=", Liberer_Entier, Integer'Image);
use Arbre_Entiers;
-----------------------------------------------------------------------------
begin
-----------------------------------------------------------------------------
-- test dans le cas Arbre Vide (Test 1)
declare
A : Arbre;
begin
Put_Line("Test 1 : apres Declaration d'un ABR");
Afficher_Test("Est_Vide(A) ?", "TRUE", Boolean'Image(Est_Vide(A)));
Afficher_Test("Taille(A) ?", " 0", Integer'Image(Taille(A)));
Afficher_Test("To_String(A) ?", "(0|)", Arbre_To_String(A));
Afficher_Test("Appartient(2, A) ?", "FALSE", Boolean'Image(Appartient(2, A)));
end;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Test d'insertion et appartient (Tests 2; 3; 4; 5; 6; 7)
declare
A : Arbre;
begin
New_Line;
Put_Line("Test 2 : Insertion de 6 entiers distincts");
Inserer(7, A);
Inserer(5, A);
Inserer(3, A);
Inserer(9, A);
Inserer(4, A);
Inserer(8, A);
Afficher_Test("Est_Vide(A) ?", "FALSE", Boolean'Image(Est_Vide(A)));
Afficher_Test("Taille(A) ?", " 6", Integer'Image(Taille(A)));
Afficher_Test("To_String(A) ?","( 6| 3 4 5 7 8 9)", Arbre_To_String(A));
New_Line;
Put_Line("Test 3 : Appartient d'un element existant (debut/fin/milieu)");
Afficher_Test("Appartient(3, A) ?", "TRUE", Boolean'Image(Appartient(3, A)));
Afficher_Test("Appartient(9, A) ?", "TRUE", Boolean'Image(Appartient(9, A)));
Afficher_Test("Appartient(5, A) ?", "TRUE", Boolean'Image(Appartient(5, A)));
New_Line;
Put_Line("Test 4 : Appartient d'un element non existant (debut/fin/milieu)");
Afficher_Test("Appartient(1, A) ?", "FALSE", Boolean'Image(Appartient(1, A)));
Afficher_Test("Appartient(6, A) ?", "FALSE", Boolean'Image(Appartient(6, A)));
Afficher_Test("Appartient(12, A) ?", "FALSE", Boolean'Image(Appartient(12, A)));
New_Line;
Put_Line("Test 5 : Insertion d'un doublon (au debut)");
begin
Inserer(3, A);
Put_Line(" bizarre ... si on passe ici c'est que l'exception ne s'est pas levee ... ");
exception
when Element_Deja_Present =>
Put_Line(" exception Element_Deja_Present levee !!");
end;
New_Line;
Put_Line("Test 6 : Insertion d'un doublon (en fin)");
begin
Inserer(9, A);
Put_Line(" bizarre ... si on passe ici c'est que l'exception ne s'est pas levee ... ");
exception
when Element_Deja_Present =>
Put_Line(" exception Element_Deja_Present levee !!");
end;
New_Line;
Put_Line("Test 7 : Insertion d'un doublon (au milieu)");
begin
Inserer(5, A);
Put_Line(" bizarre ... si on passe ici c'est que l'exception ne s'est pas levee ... ");
exception
when Element_Deja_Present =>
Put_Line(" exception Element_Deja_Present levee !!");
end;
end;
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Tests sur la suppression (Test 8; 9; 10; 11)
-- On considère que l'insertion est correcte
declare
A : Arbre;
begin
Inserer(7, A);
Inserer(5, A);
Inserer(3, A);
Inserer(9, A);
Inserer(4, A);
Inserer(8, A);
New_Line;
Put_Line("Test 8 : Suppression d'un element (en fin)");
Put_Line("Arbre avant suppression : " & Arbre_To_String(A));
begin
Supprimer(9, A);
Afficher_Test("Taille(A) ?", " 5", Integer'Image(Taille(A)));
Afficher_Test("To_String(A) ?","( 5| 3 4 5 7 8)", Arbre_To_String(A));
exception
when Element_Non_Present =>
Put_Line(" bizarre !!! exception Element_Non_Present levee !!");
end;
New_Line;
Put_Line("Test 9 : Suppression d'un element (en milieu)");
--Put_Line("Arbre avant suppression : " & Arbre_To_String(A));
begin
Supprimer(5, A);
Afficher_Test("Taille(A) ?", " 4", Integer'Image(Taille(A)));
Afficher_Test("To_String(A) ?","( 4| 3 4 7 8)", Arbre_To_String(A));
exception
when Element_Non_Present =>
Put_Line(" bizarre !!! exception Element_Non_Present levee !!");
end;
New_Line;
Put_Line("Test 10 : Suppression d'un element (en debut)");
--Put_Line("Arbre avant suppression : " & Arbre_To_String(A));
begin
Supprimer(3, A);
Afficher_Test("Taille(A) ?", " 3", Integer'Image(Taille(A)));
Afficher_Test("To_String(A) ?","( 3| 4 7 8)", Arbre_To_String(A));
exception
when Element_Non_Present =>
Put_Line(" bizarre !!! exception Element_Non_Present levee !!");
end;
New_Line;
Put_Line("Test 11 : Suppression d'un element non present");
--Put_Line("Arbre avant suppression : " & Arbre_To_String(A));
begin
Supprimer(6, A);
Afficher_Test("Taille(A) ?", " 3", Integer'Image(Taille(A)));
Afficher_Test("To_String(A) ?","( 3| 4 7 8)", Arbre_To_String(A));
Put_Line(" bizarre ... si on passe ici c'est que l'exception ne s'est pas levee ... ");
exception
when Element_Non_Present =>
Put_Line(" Exception Element_Non_Present levee !!");
end;
end;
end Tester_Abr_entiers;