From d07979218ea80c58e64148d9638ed3e6195ff6ed Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 20 May 2020 01:21:51 +1000 Subject: Initial unit tests --- src/directed_graphs.adb | 8 ++-- src/directed_graphs.ads | 17 ++++---- test/graph_tests-basic.adb | 96 ++++++++++++++++++++++++++++++++++++++++++++++ test/graph_tests-basic.ads | 25 ++++++++++++ test/graph_tests.ads | 30 +++++++++++++++ test/test_main.adb | 64 +++++++++++++++++++++++++++++++ tests.gpr | 30 +++++++++++++++ 7 files changed, 258 insertions(+), 12 deletions(-) create mode 100644 test/graph_tests-basic.adb create mode 100644 test/graph_tests-basic.ads create mode 100644 test/graph_tests.ads create mode 100644 test/test_main.adb create mode 100644 tests.gpr diff --git a/src/directed_graphs.adb b/src/directed_graphs.adb index 8b59051..4b9e823 100644 --- a/src/directed_graphs.adb +++ b/src/directed_graphs.adb @@ -974,10 +974,8 @@ package body Directed_Graphs is is use type Node_Maps.Cursor; begin - if Impl.Checks and then Position.Container = null then - raise Constraint_Error with "Graph does not exist"; - end if; - if Position.Place = Node_Maps.No_Element or else + if Position.Container = null or else + Position.Place = Node_Maps.No_Element or else not Position.Container.Contains (Node_Maps.Key (Position.Place)) then return No_Node; @@ -2568,7 +2566,7 @@ package body Directed_Graphs is end if; end loop; raise Constraint_Error with "No unused nodes available"; - return Extended_Node_ID_Type'First; + return Node_ID_Type'First; end Unused; function Unused diff --git a/src/directed_graphs.ads b/src/directed_graphs.ads index 925948d..d89b647 100644 --- a/src/directed_graphs.ads +++ b/src/directed_graphs.ads @@ -795,8 +795,6 @@ private type Graph_Access is access all Graph; - Empty_Graph : constant Graph := (Ada.Finalization.Controlled with others => <>); - @@ -816,11 +814,6 @@ private Position : out Cursor); for Cursor'Read use Read; - No_Element : constant Cursor := - (Container => null, - Place => Node_Maps.No_Element, - Sub_Index => Node_Vectors.Extended_Index'First); - @@ -897,6 +890,16 @@ private + Empty_Graph : constant Graph := (Ada.Finalization.Controlled with others => <>); + + No_Element : constant Cursor := + (Container => null, + Place => Node_Maps.No_Element, + Sub_Index => Node_Vectors.Extended_Index'First); + + + + type Iterator is new Ada.Finalization.Limited_Controlled and Graph_Iterator_Interfaces.Reversible_Iterator with record diff --git a/test/graph_tests-basic.adb b/test/graph_tests-basic.adb new file mode 100644 index 0000000..b0292a8 --- /dev/null +++ b/test/graph_tests-basic.adb @@ -0,0 +1,96 @@ + + +-- Remove these when all tests are written +with Ada.Text_IO, Ada.Exceptions; +use Ada.Text_IO, Ada.Exceptions; + + +package body Graph_Tests.Basic is + + + No_Nodes : Graphs.Node_Array (1 .. 0); + No_Edges : Graphs.Edge_Array (1 .. 0); + + Dup_Nodes : Graphs.Node_Array := (1, 2, 2, 3, 5, 9, 9, 9); + + Some_Nodes : Graphs.Node_Array := (1, 2, 5, 9); + Some_Edges : Graphs.Edge_Array := ((1, 1, 2), (2, 5, 9), (5, 9, 1)); + + My_Empty_Graph : Graphs.Graph := Graphs.To_Graph (No_Nodes, No_Edges); + My_Nonempty_Graph : Graphs.Graph := Graphs.To_Graph (Some_Nodes, Some_Edges); + + + function To_Graph_Check + return Test_Result + is + Illegal_Graph : Graphs.Graph; + begin + begin + Illegal_Graph := Graphs.To_Graph (No_Nodes, Some_Edges); + return Fail; + exception + when Constraint_Error => null; + end; + begin + Illegal_Graph := Graphs.To_Graph (Dup_Nodes, No_Edges); + return Fail; + exception + when Constraint_Error => null; + end; + return Pass; + end To_Graph_Check; + + + function Is_Empty_Check + return Test_Result is + begin + if My_Empty_Graph.Is_Empty and not My_Nonempty_Graph.Is_Empty then + return Pass; + else + return Fail; + end if; + end Is_Empty_Check; + + + function Clear_Check + return Test_Result + is + My_Cleared_Graph : Graphs.Graph := My_Nonempty_Graph; + begin + My_Cleared_Graph.Clear; + if My_Cleared_Graph.Is_Empty then + return Pass; + else + return Fail; + end if; + end Clear_Check; + + + function Cursor_Element_Check + return Test_Result + is + use type Graphs.Cursor; + My_Cursor : Graphs.Cursor; + begin + if Graphs.Has_Element (Graphs.No_Element) or + Graphs.Element (Graphs.No_Element) /= Graphs.Extended_Node_ID_Type'First + then + return Fail; + end if; + My_Cursor := My_Nonempty_Graph.To_Cursor (2); + if not Graphs.Has_Element (My_Cursor) or + Graphs.Element (My_Cursor) /= 2 + then + return Fail; + end if; + My_Cursor := My_Empty_Graph.To_Cursor (1); + if My_Cursor /= Graphs.No_Element then + return Fail; + end if; + return Pass; + end Cursor_Element_Check; + + +end Graph_Tests.Basic; + + diff --git a/test/graph_tests-basic.ads b/test/graph_tests-basic.ads new file mode 100644 index 0000000..80fb05d --- /dev/null +++ b/test/graph_tests-basic.ads @@ -0,0 +1,25 @@ + + +with Unit_Tests; +use Unit_Tests; + + +package Graph_Tests.Basic is + + + function To_Graph_Check return Test_Result; + function Is_Empty_Check return Test_Result; + function Clear_Check return Test_Result; + function Cursor_Element_Check return Test_Result; + + + Tests : Test_Array := + ((+"To_Graph", To_Graph_Check'Access), + (+"Is_Empty", Is_Empty_Check'Access), + (+"Clear", Clear_Check'Access), + (+"To_Cursor, Has_Element, Element", Cursor_Element_Check'Access)); + + +end Graph_Tests.Basic; + + diff --git a/test/graph_tests.ads b/test/graph_tests.ads new file mode 100644 index 0000000..18ea321 --- /dev/null +++ b/test/graph_tests.ads @@ -0,0 +1,30 @@ + + +with + + Ada.Strings.Unbounded, + Directed_Graphs; + + +package Graph_Tests is + + + package SU renames Ada.Strings.Unbounded; + + + type Node_ID is new Positive; + type Edge_ID is new Positive; + type Node_Label is new SU.Unbounded_String; + type Edge_Label is new SU.Unbounded_String; + + + package Graphs is new Directed_Graphs + (Node_ID_Type => Node_ID, + Edge_ID_Type => Edge_ID, + Node_Label_Type => Node_Label, + Edge_Label_Type => Edge_Label); + + +end Graph_Tests; + + diff --git a/test/test_main.adb b/test/test_main.adb new file mode 100644 index 0000000..e116720 --- /dev/null +++ b/test/test_main.adb @@ -0,0 +1,64 @@ + + +with + + Ada.Text_IO, + Ada.Command_Line, + Ada.Characters.Latin_1, + Unit_Tests, + Graph_Tests.Basic; + + +use + + Ada.Text_IO, + Unit_Tests; + + +procedure Test_Main is + + + package Latin renames Ada.Characters.Latin_1; + + + Help_String : String := + "Runs unit tests on the Ada Directed Graph library." & Latin.LF & + "Usage: graphtest [switches]" & Latin.LF & + Latin.LF & + "Valid switches:" & Latin.LF & + "--help" & Latin.HT & Latin.HT & "Shows this information" & Latin.LF & + "--verbose" & Latin.HT & "Enables extra verbosity" & Latin.LF & + Latin.LF & + "All other command line input will be ignored."; + + + How_Verbose : Verbosity := Weak; + + +begin + + + for N in 1 .. Ada.Command_Line.Argument_Count loop + if Ada.Command_Line.Argument (N) = "--help" then + Put_Line (Help_String); + return; + end if; + end loop; + + + for N in 1 .. Ada.Command_Line.Argument_Count loop + if Ada.Command_Line.Argument (N) = "--verbose" then + How_Verbose := Strong; + exit; + end if; + end loop; + + + Put_Line ("Running basic construction and inspection tests..."); + Run_Tests (Graph_Tests.Basic.Tests, How_Verbose); + -- New_Line; + + +end Test_Main; + + diff --git a/tests.gpr b/tests.gpr new file mode 100644 index 0000000..715b9c2 --- /dev/null +++ b/tests.gpr @@ -0,0 +1,30 @@ + + +with "directed_graph", "basic_unit_test"; + + +project Tests is + + + for Languages use ("Ada"); + + + for Source_Dirs use ("test/**"); + for Object_Dir use "obj"; + for Exec_Dir use "bin"; + for Main use ("test_main.adb"); + + + package Builder is + for Executable("test_main.adb") use "graphtest"; + end Builder; + + + package Compiler is + for Default_Switches("Ada") use ("-gnaty4aAbcefhiklM100nprt"); + end Compiler; + + +end Tests; + + -- cgit