summaryrefslogtreecommitdiff
path: root/test/graph_tests-search.adb
blob: 9933c7dd76494fd33bc3e626af41597e2b75a396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345


package body Graph_Tests.Search is


    function Find_Check
        return Test_Result
    is
        function Node_Perm is new Is_Permutation (Node_ID, Positive, Graphs.Node_Array);
        function Edge_Perm is new Is_Permutation (Graphs.Edge_Type, Positive, Graphs.Edge_Array);
        Copy_Graph : Graphs.Graph := My_Complex_Graph;
    begin
        if not Node_Perm (Copy_Graph.Find (Node_Label (+"LOL")), No_Nodes) or
            not Edge_Perm (Copy_Graph.Find (Edge_Label (+"KEK")), No_Edges)
        then
            return Fail;
        end if;
        Copy_Graph.Append_Label (2, Node_Label (+"LOL"));
        Copy_Graph.Append_Label (5, Node_Label (+"LOL"));
        Copy_Graph.Append_Label ((6, 5, 6), Edge_Label (+"KEK"));
        Copy_Graph.Append_Label ((7, 5, 7), Edge_Label (+"KEK"));
        Copy_Graph.Append_Label (10, Node_Label (+"KEK"));
        Copy_Graph.Append_Label ((4, 2, 4), Edge_Label (+"LOL"));
        if not Node_Perm (Copy_Graph.Find (Node_Label (+"LOL")), (2, 5)) or
            not Edge_Perm (Copy_Graph.Find (Edge_Label (+"KEK")), ((6, 5, 6), (7, 5, 7)))
        then
            return Fail;
        end if;
        return Pass;
    end Find_Check;


    function Find_Subgraph_Check
        return Test_Result
    is
        function Node_Perm is new Is_Permutation (Node_ID, Positive, Graphs.Node_Array);
        function Edge_Perm is new Is_Permutation (Graphs.Edge_Type, Positive, Graphs.Edge_Array);
        Copy_Graph : Graphs.Graph := My_Complex_Graph;
        Position : Graphs.Cursor := Copy_Graph.To_Cursor (7);
    begin
        if not Node_Perm (Graphs.Find_In_Subgraph (Position, Node_Label (+"LOL")), No_Nodes) or
            not Edge_Perm (Graphs.Find_In_Subgraph (Position, Edge_Label (+"KEK")), No_Edges)
        then
            return Fail;
        end if;
        Copy_Graph.Append_Label (2, Node_Label (+"LOL"));
        Copy_Graph.Append_Label (5, Node_Label (+"LOL"));
        Copy_Graph.Append_Label ((6, 5, 6), Edge_Label (+"KEK"));
        Copy_Graph.Append_Label ((7, 5, 7), Edge_Label (+"KEK"));
        Copy_Graph.Append_Label (10, Node_Label (+"KEK"));
        Copy_Graph.Append_Label ((4, 2, 4), Edge_Label (+"LOL"));
        if not Node_Perm (Graphs.Find_In_Subgraph (Position, Node_Label (+"LOL")), (1 => 5)) or
            not Edge_Perm (Graphs.Find_In_Subgraph (Position, Edge_Label (+"KEK")),
                ((6, 5, 6), (7, 5, 7)))
        then
            return Fail;
        end if;
        return Pass;
    end Find_Subgraph_Check;


    function Contains_Check
        return Test_Result
    is
        Copy_Graph : Graphs.Graph := My_Nonempty_Graph;
    begin
        if Copy_Graph.Contains (Node_ID (20)) or
            not Copy_Graph.Contains (Node_ID (2)) or
            Copy_Graph.Contains (Edge_ID (20)) or
            not Copy_Graph.Contains (Edge_ID (7)) or
            Copy_Graph.Contains (Graphs.Edge_Type'(99, 99, 99)) or
            not Copy_Graph.Contains (Graphs.Edge_Type'(7, 2, 5)) or
            Copy_Graph.Contains (Graphs.Labeled_Node_Type'(2, Node_Label (+"ABC"))) or
            Copy_Graph.Contains (Graphs.Labeled_Edge_Type'((7, 2, 5), Edge_Label (+"DEF")))
        then
            return Fail;
        end if;
        Copy_Graph.Append_Label (2, Node_Label (+"ABC"));
        Copy_Graph.Append_Label ((7, 2, 5), Edge_Label (+"DEF"));
        if not Copy_Graph.Contains (Graphs.Labeled_Node_Type'(2, Node_Label (+"ABC"))) or
            not Copy_Graph.Contains (Graphs.Labeled_Edge_Type'((7, 2, 5), Edge_Label (+"DEF")))
        then
            return Fail;
        end if;
        return Pass;
    end Contains_Check;


    function Contains_Label_Check
        return Test_Result
    is
        Copy_Graph : Graphs.Graph := My_Nonempty_Graph;
    begin
        if Copy_Graph.Contains_Label (Node_Label (+"ABC")) or
            Copy_Graph.Contains_Label (Edge_Label (+"DEF"))
        then
            return Fail;
        end if;
        Copy_Graph.Append_Label (2, Node_Label (+"ABC"));
        Copy_Graph.Append_Label ((7, 2, 5), Edge_Label (+"DEF"));
        if not Copy_Graph.Contains_Label (Node_Label (+"ABC")) or
            not Copy_Graph.Contains_Label (Edge_Label (+"DEF"))
        then
            return Fail;
        end if;
        return Pass;
    end Contains_Label_Check;


    function Contains_Subgraph_Check
        return Test_Result
    is
        Copy_Graph : Graphs.Graph := My_Complex_Graph;
        Branch_1 : Graphs.Cursor := Copy_Graph.To_Cursor (5);
        Branch_2 : Graphs.Cursor := Copy_Graph.To_Cursor (2);
    begin
        if Graphs.Contains_In_Subgraph (Branch_1, Node_ID (2)) or
            not Graphs.Contains_In_Subgraph (Branch_1, Node_ID (10)) or
            Graphs.Contains_In_Subgraph (Branch_1, Graphs.Edge_Type'(2, 2, 3)) or
            not Graphs.Contains_In_Subgraph (Branch_1, Graphs.Edge_Type'(13, 7, 5)) or
            Graphs.Contains_In_Subgraph (Branch_2, Node_ID (99)) or
            Graphs.Contains_In_Subgraph (Branch_2, Graphs.Edge_Type'(1, 99, 99)) or
            Graphs.Contains_In_Subgraph
                (Branch_1, Graphs.Labeled_Node_Type'(Node_ID (5), Node_Label (+"ABC"))) or
            Graphs.Contains_In_Subgraph
                (Branch_1, Graphs.Labeled_Edge_Type'((13, 7, 5), Edge_Label (+"DEF")))
        then
            return Fail;
        end if;
        Copy_Graph.Append_Label (5, Node_Label (+"ABC"));
        Copy_Graph.Append_Label ((13, 7, 5), Edge_Label (+"DEF"));
        if Graphs.Contains_In_Subgraph
                (Branch_2, Graphs.Labeled_Node_Type'(Node_ID (5), Node_Label (+"ABC"))) or
            not Graphs.Contains_In_Subgraph
                (Branch_1, Graphs.Labeled_Node_Type'(Node_ID (5), Node_Label (+"ABC"))) or
            Graphs.Contains_In_Subgraph
                (Branch_2, Graphs.Labeled_Edge_Type'((13, 7, 5), Edge_Label (+"DEF"))) or
            not Graphs.Contains_In_Subgraph
                (Branch_1, Graphs.Labeled_Edge_Type'((13, 7, 5), Edge_Label (+"DEF")))
        then
            return Fail;
        end if;
        return Pass;
    end Contains_Subgraph_Check;


    function Contains_Label_Subgraph_Check
        return Test_Result
    is
        Copy_Graph : Graphs.Graph := My_Complex_Graph;
        Branch_1 : Graphs.Cursor := Copy_Graph.To_Cursor (5);
        Branch_2 : Graphs.Cursor := Copy_Graph.To_Cursor (2);
    begin
        if Graphs.Contains_Label_In_Subgraph (Branch_1, Node_Label (+"ABC")) or
            Graphs.Contains_Label_In_Subgraph (Branch_1, Edge_Label (+"DEF")) or
            Graphs.Contains_Label_In_Subgraph (Branch_2, Node_Label (+"ABC")) or
            Graphs.Contains_Label_In_Subgraph (Branch_2, Edge_Label (+"DEF"))
        then
            return Fail;
        end if;
        Copy_Graph.Append_Label (5, Node_Label (+"ABC"));
        Copy_Graph.Append_Label ((8, 6, 8), Edge_Label (+"DEF"));
        if not Graphs.Contains_Label_In_Subgraph (Branch_1, Node_Label (+"ABC")) or
            not Graphs.Contains_Label_In_Subgraph (Branch_1, Edge_Label (+"DEF")) or
            Graphs.Contains_Label_In_Subgraph (Branch_2, Node_Label (+"ABC")) or
            Graphs.Contains_Label_In_Subgraph (Branch_2, Edge_Label (+"DEF"))
        then
            return Fail;
        end if;
        return Pass;
    end Contains_Label_Subgraph_Check;


    function Contains_Path_Check
        return Test_Result
    is
        Node_Path_In : Graphs.Node_Path := (1, 2, 3);
        Node_Path_Out : Graphs.Node_Path := (87, 88, 89, 100);
        Edge_Path_In : Graphs.Edge_Path := ((5, 1, 5), (6, 5, 6), (8, 6, 8));
        Edge_Path_Out : Graphs.Edge_Path := ((1, 1, 4), (5, 4, 7));
    begin
        if not My_Complex_Graph.Contains_Path (Node_Path_In) or
            My_Complex_Graph.Contains_Path (Node_Path_Out) or
            not My_Complex_Graph.Contains_Path (Edge_Path_In) or
            My_Complex_Graph.Contains_Path (Edge_Path_Out)
        then
            return Fail;
        end if;
        return Pass;
    end Contains_Path_Check;


    function Contains_Path_Subgraph_Check
        return Test_Result
    is
        Node_Path_In : Graphs.Node_Path := (5, 7, 7, 5);
        Node_Path_Out : Graphs.Node_Path := (1, 2, 3);
        Edge_Path_In : Graphs.Edge_Path := ((6, 5, 6), (8, 6, 8));
        Edge_Path_Out : Graphs.Edge_Path := ((1, 1, 2), (2, 2, 3));
        Position : Graphs.Cursor := My_Complex_Graph.To_Cursor (5);
    begin
        if not Graphs.Contains_Path_In_Subgraph (Position, Node_Path_In) or
            Graphs.Contains_Path_In_Subgraph (Position, Node_Path_Out) or
            not Graphs.Contains_Path_In_Subgraph (Position, Edge_Path_In) or
            Graphs.Contains_Path_In_Subgraph (Position, Edge_Path_Out)
        then
            return Fail;
        end if;
        return Pass;
    end Contains_Path_Subgraph_Check;


    function Iterate_Check
        return Test_Result
    is
        Index : Positive := 1;
        Check_1 : Graphs.Node_Array := (2, 5, 9, 11);
        Check_2 : Graphs.Node_Array := (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    begin
        for C in My_Nonempty_Graph.Iterate loop
            if Index not in Check_1'Range or else
                Graphs.Element (C) /= Check_1 (Index)
            then
                return Fail;
            else
                Index := Index + 1;
            end if;
        end loop;
        Index := 1;
        for C in My_Complex_Graph.Iterate loop
            if Index not in Check_2'Range or else
                Graphs.Element (C) /= Check_2 (Index)
            then
                return Fail;
            else
                Index := Index + 1;
            end if;
        end loop;
        return Pass;
    end Iterate_Check;


    function Iterate_Subgraph_Check
        return Test_Result
    is
        Index : Positive := 1;
        Check : Graphs.Node_Array := (5, 6, 7, 8, 9, 10);
        Position_1 : Graphs.Cursor := My_Complex_Graph.To_Cursor (5);
        Position_2 : Graphs.Cursor := My_Complex_Graph.To_Cursor (7);
    begin
        for C in My_Complex_Graph.Iterate_Subgraph (Position_1) loop
            if Index not in Check'Range or else
                Graphs.Element (C) /= Check (Index)
            then
                return Fail;
            else
                Index := Index + 1;
            end if;
        end loop;
        Index := 1;
        for C in My_Complex_Graph.Iterate_Subgraph (Position_2) loop
            if Index not in Check'Range or else
                Graphs.Element (C) /= Check (Index)
            then
                return Fail;
            else
                Index := Index + 1;
            end if;
        end loop;
        return Pass;
    end Iterate_Subgraph_Check;


    function Iterate_By_Check
        return Test_Result
    is
        function Max
               (Nodes : in Graphs.Node_Array)
            return Node_ID
        is
            Current : Node_ID := 1;
        begin
            for N of Nodes loop
                if N > Current then
                    Current := N;
                end if;
            end loop;
            return Current;
        end Max;

        function My_Choices
               (Position : in Graphs.Cursor)
            return Graphs.Cursor
        is
            Pick_From : Graphs.Node_Array := Graphs.Children (Position);
        begin
            if Pick_From'Length = 0 then
                return Graphs.No_Element;
            end if;
            return Graphs.Cursor_To (Position, Max (Pick_From));
        end My_Choices;

        function My_Filter
               (Position : in Graphs.Cursor)
            return Boolean is
        begin
            return not Graphs.Has_Element (Position) or else
                Graphs.Element (Position) mod 2 = 0;
        end My_Filter;

        Index : Positive := 1;
        Check_1 : Graphs.Node_Array := (1, 5, 7, 10);
        Check_2 : Graphs.Node_Array := (1 => 10);

        Start : Graphs.Cursor := My_Complex_Graph.To_Cursor (1);
    begin
        for C in My_Complex_Graph.Iterate_By (Start, My_Choices'Unrestricted_Access) loop
            if Index not in Check_1'Range or else
                Graphs.Element (C) /= Check_1 (Index)
            then
                return Fail;
            else
                Index := Index + 1;
            end if;
        end loop;

        Index := 1;
        for C in My_Complex_Graph.Iterate_By
            (Start, My_Choices'Unrestricted_Access, My_Filter'Unrestricted_Access)
        loop
            if Index not in Check_2'Range or else
                Graphs.Element (C) /= Check_2 (Index)
            then
                return Fail;
            else
                Index := Index + 1;
            end if;
        end loop;
        return Pass;
    end Iterate_By_Check;


end Graph_Tests.Search;