Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_sgraph_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38#include <gtest/gtest.h>
39#include <tpl_sgraph.H>
40#include <string>
41#include <type_traits>
42#include <utility>
43#include <vector>
44
45using namespace Aleph;
46
47namespace
48{
49 // Type aliases for common graph types
53}
54
55// =============================================================================
56// Graph_Snode Tests
57// =============================================================================
58
59TEST(GraphSnode, DefaultConstruction)
60{
62 EXPECT_EQ(node.num_arcs, 0u);
63 EXPECT_TRUE(node.arc_list.is_empty());
64}
65
66TEST(GraphSnode, ConstructionWithInfo)
67{
68 Graph_Snode<int> node(42);
69 EXPECT_EQ(node.get_info(), 42);
70 EXPECT_EQ(node.num_arcs, 0u);
71 EXPECT_TRUE(node.arc_list.is_empty());
72}
73
74TEST(GraphSnode, CopyConstruction)
75{
76 Graph_Snode<int> node1(42);
77 Graph_Snode<int> node2(node1);
78 EXPECT_EQ(node2.get_info(), 42);
79 EXPECT_EQ(node2.num_arcs, 0u);
80}
81
82TEST(GraphSnode, MoveConstruction)
83{
84 Graph_Snode<std::string> node1(std::string("test"));
85 Graph_Snode<std::string> node2(std::move(node1));
86 EXPECT_EQ(node2.get_info(), "test");
87}
88
89TEST(GraphSnode, CopyAssignment)
90{
91 Graph_Snode<int> node1(42);
92 Graph_Snode<int> node2(100);
93 node2 = node1;
94 EXPECT_EQ(node2.get_info(), 42);
95}
96
97TEST(GraphSnode, SelfAssignment)
98{
99 Graph_Snode<int> node(42);
100 node = node;
101 EXPECT_EQ(node.get_info(), 42);
102}
103
104TEST(GraphSnode, ConstructionFromPointer)
105{
106 Graph_Snode<int> node1(42);
107 Graph_Snode<int> node2(&node1);
108 EXPECT_EQ(node2.get_info(), 42);
109}
110
111// =============================================================================
112// Graph_Sarc Tests
113// =============================================================================
114
115TEST(GraphSarc, DefaultConstruction)
116{
117 Graph_Sarc<int> arc;
118 EXPECT_EQ(arc.get_info(), 0); // Default int value
119}
120
121TEST(GraphSarc, ConstructionWithInfo)
122{
123 Graph_Sarc<int> arc(100);
124 EXPECT_EQ(arc.get_info(), 100);
125}
126
127TEST(GraphSarc, CopyConstruction)
128{
129 Graph_Sarc<std::string> arc1(std::string("edge"));
130 Graph_Sarc<std::string> arc2(arc1);
131 EXPECT_EQ(arc2.get_info(), "edge");
132}
133
134TEST(GraphSarc, CopyAssignment)
135{
136 Graph_Sarc<int> arc1(42);
137 Graph_Sarc<int> arc2(100);
138 arc2 = arc1;
139 EXPECT_EQ(arc2.get_info(), 42);
140}
141
142TEST(GraphSarc, SelfAssignment)
143{
144 Graph_Sarc<int> arc(42);
145 arc = arc;
146 EXPECT_EQ(arc.get_info(), 42);
147}
148
149TEST(GraphSarc, ConstructionWithEndpoints)
150{
151 int src_dummy = 1, tgt_dummy = 2;
152 Graph_Sarc<int> arc(&src_dummy, &tgt_dummy, 100);
153 EXPECT_EQ(arc.get_info(), 100);
154 EXPECT_EQ(arc.src_node, &src_dummy);
155 EXPECT_EQ(arc.tgt_node, &tgt_dummy);
156}
157
158// =============================================================================
159// List_SGraph Basic Operations
160// =============================================================================
161
162TEST(ListSGraph, DefaultConstruction)
163{
164 IntGraph g;
165 EXPECT_EQ(g.vsize(), 0u);
166 EXPECT_EQ(g.esize(), 0u);
167 EXPECT_FALSE(g.is_digraph());
168}
169
170TEST(ListSGraph, InsertNode)
171{
172 IntGraph g;
173 auto n1 = g.insert_node(1);
174 auto n2 = g.insert_node(2);
175 auto n3 = g.insert_node(3);
176
177 EXPECT_EQ(g.vsize(), 3u);
178 EXPECT_EQ(n1->get_info(), 1);
179 EXPECT_EQ(n2->get_info(), 2);
180 EXPECT_EQ(n3->get_info(), 3);
181}
182
183TEST(ListSGraph, InsertArc)
184{
185 IntGraph g;
186 auto n1 = g.insert_node(1);
187 auto n2 = g.insert_node(2);
188
189 auto arc = g.insert_arc(n1, n2, 10);
190
191 EXPECT_EQ(g.esize(), 1u);
192 EXPECT_EQ(arc->get_info(), 10);
193 EXPECT_EQ(g.get_src_node(arc), n1);
194 EXPECT_EQ(g.get_tgt_node(arc), n2);
195}
196
197TEST(ListSGraph, UndirectedArcSymmetry)
198{
199 IntGraph g;
200 auto n1 = g.insert_node(1);
201 auto n2 = g.insert_node(2);
202
203 g.insert_arc(n1, n2, 10);
204
205 // In undirected graph, arc appears in both nodes' adjacency lists
206 EXPECT_EQ(n1->num_arcs, 1u);
207 EXPECT_EQ(n2->num_arcs, 1u);
208}
209
210TEST(ListSGraph, RemoveArc)
211{
212 IntGraph g;
213 auto n1 = g.insert_node(1);
214 auto n2 = g.insert_node(2);
215 auto arc = g.insert_arc(n1, n2, 10);
216
217 EXPECT_EQ(g.esize(), 1u);
218 g.remove_arc(arc);
219 EXPECT_EQ(g.esize(), 0u);
220 EXPECT_EQ(n1->num_arcs, 0u);
221 EXPECT_EQ(n2->num_arcs, 0u);
222}
223
224TEST(ListSGraph, RemoveNode)
225{
226 IntGraph g;
227 auto n1 = g.insert_node(1);
228 auto n2 = g.insert_node(2);
229 auto n3 = g.insert_node(3);
230
231 g.insert_arc(n1, n2, 12);
232 g.insert_arc(n2, n3, 23);
233
234 EXPECT_EQ(g.vsize(), 3u);
235 EXPECT_EQ(g.esize(), 2u);
236
237 g.remove_node(n2);
238
239 EXPECT_EQ(g.vsize(), 2u);
240 EXPECT_EQ(g.esize(), 0u); // All arcs connected to n2 removed
241}
242
243TEST(ListSGraph, MultipleArcs)
244{
245 IntGraph g;
246 auto n1 = g.insert_node(1);
247 auto n2 = g.insert_node(2);
248 auto n3 = g.insert_node(3);
249 auto n4 = g.insert_node(4);
250
251 g.insert_arc(n1, n2, 12);
252 g.insert_arc(n1, n3, 13);
253 g.insert_arc(n1, n4, 14);
254 g.insert_arc(n2, n3, 23);
255 g.insert_arc(n3, n4, 34);
256
257 EXPECT_EQ(g.esize(), 5u);
258 EXPECT_EQ(n1->num_arcs, 3u); // n1 connected to n2, n3, n4
259}
260
261// =============================================================================
262// List_SDigraph Tests
263// =============================================================================
264
265TEST(ListSDigraph, IsDirected)
266{
267 IntDigraph g;
268 EXPECT_TRUE(g.is_digraph());
269}
270
271TEST(ListSDigraph, DirectedArcAsymmetry)
272{
273 IntDigraph g;
274 auto n1 = g.insert_node(1);
275 auto n2 = g.insert_node(2);
276
277 g.insert_arc(n1, n2, 10);
278
279 // In directed graph, arc only appears in source node's adjacency list
280 EXPECT_EQ(n1->num_arcs, 1u);
281 EXPECT_EQ(n2->num_arcs, 0u);
282}
283
284TEST(ListSDigraph, BidirectionalArcs)
285{
286 IntDigraph g;
287 auto n1 = g.insert_node(1);
288 auto n2 = g.insert_node(2);
289
290 g.insert_arc(n1, n2, 12);
291 g.insert_arc(n2, n1, 21);
292
293 EXPECT_EQ(g.esize(), 2u);
294 EXPECT_EQ(n1->num_arcs, 1u);
295 EXPECT_EQ(n2->num_arcs, 1u);
296}
297
298// =============================================================================
299// Iterator Tests
300// =============================================================================
301
302TEST(ListSGraph, NodeIterator)
303{
304 IntGraph g;
305 g.insert_node(1);
306 g.insert_node(2);
307 g.insert_node(3);
308
309 int count = 0;
310 int sum = 0;
311 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
312 {
313 sum += it.get_curr()->get_info();
314 ++count;
315 }
316
317 EXPECT_EQ(count, 3);
318 EXPECT_EQ(sum, 6); // 1 + 2 + 3
319}
320
321TEST(ListSGraph, ArcIterator)
322{
323 IntGraph g;
324 auto n1 = g.insert_node(1);
325 auto n2 = g.insert_node(2);
326 auto n3 = g.insert_node(3);
327
328 g.insert_arc(n1, n2, 10);
329 g.insert_arc(n2, n3, 20);
330
331 int count = 0;
332 int sum = 0;
333 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
334 {
335 sum += it.get_curr()->get_info();
336 ++count;
337 }
338
339 EXPECT_EQ(count, 2);
340 EXPECT_EQ(sum, 30); // 10 + 20
341}
342
343// The sets of nodes and arcs of the graph are ordered by the stored pointers.
344// The iterators hand those pointers out by value, so that the sets cannot be
345// written through them, in particular from a constant graph.
346TEST(ListSGraph, IteratorsHandOutPointersByValue)
347{
348 static_assert(std::is_same_v<decltype(std::declval<IntGraph::Node_Iterator &>().get_curr()),
349 IntGraph::Node *>);
350 static_assert(std::is_same_v<decltype(std::declval<IntGraph::Node_Iterator &>().get_curr_ne()),
351 IntGraph::Node *>);
352 static_assert(std::is_same_v<decltype(std::declval<IntGraph::Arc_Iterator &>().get_curr()),
353 IntGraph::Arc *>);
354 static_assert(std::is_same_v<decltype(std::declval<IntGraph::Arc_Iterator &>().get_curr_ne()),
355 IntGraph::Arc *>);
356
357 IntGraph g;
358 auto n1 = g.insert_node(1);
359 auto n2 = g.insert_node(2);
360 auto a = g.insert_arc(n1, n2, 10);
361 const IntGraph & cg = g;
362
363 int sum = 0;
364 for (IntGraph::Node_Iterator it(cg); it.has_curr(); it.next_ne())
365 {
366 EXPECT_TRUE(it.get_curr() == n1 or it.get_curr() == n2);
367 EXPECT_EQ(it.get_curr(), it.get_curr_ne());
368 EXPECT_EQ(it.get_curr(), it.get_current_node());
369 sum += it.get_curr()->get_info();
370 }
371 EXPECT_EQ(sum, 3); // 1 + 2
372
373 IntGraph::Arc_Iterator ait(cg);
374 ASSERT_TRUE(ait.has_curr());
375 EXPECT_EQ(ait.get_curr(), a);
376 EXPECT_EQ(ait.get_curr_ne(), a);
377 EXPECT_EQ(ait.get_current_arc(), a);
378}
379
381{
382 IntGraph g;
383 auto n1 = g.insert_node(1);
384 auto n2 = g.insert_node(2);
385 auto n3 = g.insert_node(3);
386 auto n4 = g.insert_node(4);
387
388 g.insert_arc(n1, n2, 12);
389 g.insert_arc(n1, n3, 13);
390 g.insert_arc(n1, n4, 14);
391
392 int count = 0;
393 int sum = 0;
394 for (IntGraph::Node_Arc_Iterator it(n1); it.has_curr(); it.next())
395 {
396 sum += it.get_curr()->get_info();
397 ++count;
398 }
399
400 EXPECT_EQ(count, 3);
401 EXPECT_EQ(sum, 39); // 12 + 13 + 14
402}
403
404TEST(ListSGraph, NodeArcIteratorGetTgtNode)
405{
406 IntGraph g;
407 auto n1 = g.insert_node(1);
408 auto n2 = g.insert_node(2);
409 auto n3 = g.insert_node(3);
410
411 g.insert_arc(n1, n2, 12);
412 g.insert_arc(n1, n3, 13);
413
414 std::vector<int> targets;
415 for (IntGraph::Node_Arc_Iterator it(n1); it.has_curr(); it.next())
416 {
417 targets.push_back(it.get_tgt_node()->get_info());
418 }
419
420 EXPECT_EQ(targets.size(), 2u);
421 // Order may vary, just check both are present
422 EXPECT_TRUE(std::find(targets.begin(), targets.end(), 2) != targets.end());
423 EXPECT_TRUE(std::find(targets.begin(), targets.end(), 3) != targets.end());
424}
425
426TEST(ListSGraph, ArcIteratorGetEndpoints)
427{
428 IntDigraph g;
429 auto n1 = g.insert_node(1);
430 auto n2 = g.insert_node(2);
431
432 g.insert_arc(n1, n2, 12);
433
434 IntDigraph::Arc_Iterator it(g);
435 EXPECT_TRUE(it.has_curr());
436 EXPECT_EQ(it.get_src_node()->get_info(), 1);
437 EXPECT_EQ(it.get_tgt_node()->get_info(), 2);
438}
439
440// =============================================================================
441// Copy and Move Semantics
442// =============================================================================
443
444TEST(ListSGraph, CopyConstruction)
445{
446 IntGraph g1;
447 auto n1 = g1.insert_node(1);
448 auto n2 = g1.insert_node(2);
449 g1.insert_arc(n1, n2, 10);
450
451 IntGraph g2(g1);
452
453 EXPECT_EQ(g2.vsize(), 2u);
454 EXPECT_EQ(g2.esize(), 1u);
455}
456
457TEST(ListSGraph, MoveConstruction)
458{
459 IntGraph g1;
460 g1.insert_node(1);
461 g1.insert_node(2);
462
463 IntGraph g2(std::move(g1));
464
465 EXPECT_EQ(g2.vsize(), 2u);
466 EXPECT_EQ(g1.vsize(), 0u); // Moved-from state
467}
468
469TEST(ListSGraph, CopyAssignment)
470{
471 IntGraph g1;
472 auto n1 = g1.insert_node(1);
473 auto n2 = g1.insert_node(2);
474 g1.insert_arc(n1, n2, 10);
475
476 IntGraph g2;
477 g2 = g1;
478
479 EXPECT_EQ(g2.vsize(), 2u);
480 EXPECT_EQ(g2.esize(), 1u);
481}
482
483TEST(ListSGraph, SelfAssignment)
484{
485 IntGraph g;
486 g.insert_node(1);
487 g.insert_node(2);
488
489 g = g;
490
491 EXPECT_EQ(g.vsize(), 2u);
492}
493
494TEST(ListSGraph, MoveAssignment)
495{
496 IntGraph g1;
497 g1.insert_node(1);
498 g1.insert_node(2);
499
500 IntGraph g2;
501 g2 = std::move(g1);
502
503 EXPECT_EQ(g2.vsize(), 2u);
504 EXPECT_EQ(g1.vsize(), 0u);
505}
506
507TEST(ListSDigraph, CopyConstruction)
508{
509 IntDigraph g1;
510 auto n1 = g1.insert_node(1);
511 auto n2 = g1.insert_node(2);
512 g1.insert_arc(n1, n2, 10);
513
514 IntDigraph g2(g1);
515
516 EXPECT_EQ(g2.vsize(), 2u);
517 EXPECT_EQ(g2.esize(), 1u);
518 EXPECT_TRUE(g2.is_digraph());
519}
520
521TEST(ListSDigraph, MoveConstruction)
522{
523 IntDigraph g1;
524 g1.insert_node(1);
525 g1.insert_node(2);
526
527 IntDigraph g2(std::move(g1));
528
529 EXPECT_EQ(g2.vsize(), 2u);
530 EXPECT_TRUE(g2.is_digraph());
531}
532
533TEST(ListSDigraph, CopyAssignment)
534{
535 IntDigraph g1;
536 auto n1 = g1.insert_node(1);
537 auto n2 = g1.insert_node(2);
538 g1.insert_arc(n1, n2, 10);
539
540 IntDigraph g2;
541 g2 = g1;
542
543 EXPECT_EQ(g2.vsize(), 2u);
544 EXPECT_EQ(g2.esize(), 1u);
545 EXPECT_TRUE(g2.is_digraph());
546}
547
548TEST(ListSDigraph, MoveAssignment)
549{
550 IntDigraph g1;
551 g1.insert_node(1);
552 g1.insert_node(2);
553
554 IntDigraph g2;
555 g2 = std::move(g1);
556
557 EXPECT_EQ(g2.vsize(), 2u);
558 EXPECT_TRUE(g2.is_digraph());
559}
560
561// =============================================================================
562// Stress Tests
563// =============================================================================
564
565TEST(ListSGraph, LargeGraph)
566{
567 IntGraph g;
568 constexpr int N = 100;
569
570 // Insert N nodes
571 std::vector<IntGraph::Node*> nodes;
572 for (int i = 0; i < N; ++i)
573 nodes.push_back(g.insert_node(i));
574
575 EXPECT_EQ(g.vsize(), static_cast<size_t>(N));
576
577 // Create a complete graph (N*(N-1)/2 edges)
578 for (int i = 0; i < N; ++i)
579 for (int j = i + 1; j < N; ++j)
580 g.insert_arc(nodes[i], nodes[j], i * N + j);
581
582 EXPECT_EQ(g.esize(), static_cast<size_t>(N * (N - 1) / 2));
583}
584
585TEST(ListSDigraph, LargeDigraph)
586{
587 IntDigraph g;
588 constexpr int N = 50;
589
590 std::vector<IntDigraph::Node*> nodes;
591 for (int i = 0; i < N; ++i)
592 nodes.push_back(g.insert_node(i));
593
594 // Create complete digraph (N*(N-1) edges)
595 for (int i = 0; i < N; ++i)
596 for (int j = 0; j < N; ++j)
597 if (i != j)
598 g.insert_arc(nodes[i], nodes[j], i * N + j);
599
600 EXPECT_EQ(g.esize(), static_cast<size_t>(N * (N - 1)));
601}
602
603// =============================================================================
604// Edge Cases
605// =============================================================================
606
607TEST(ListSGraph, SelfLoop)
608{
609 IntGraph g;
610 auto n1 = g.insert_node(1);
611
612 auto arc = g.insert_arc(n1, n1, 11);
613
614 EXPECT_EQ(g.esize(), 1u);
615 EXPECT_EQ(n1->num_arcs, 1u); // Self-loop counted once in undirected
616 EXPECT_EQ(g.get_src_node(arc), n1);
617 EXPECT_EQ(g.get_tgt_node(arc), n1);
618}
619
620TEST(ListSDigraph, SelfLoop)
621{
622 IntDigraph g;
623 auto n1 = g.insert_node(1);
624
625 auto arc = g.insert_arc(n1, n1, 11);
626
627 EXPECT_EQ(g.esize(), 1u);
628 EXPECT_EQ(n1->num_arcs, 1u);
629 EXPECT_EQ(g.get_src_node(arc), n1);
630 EXPECT_EQ(g.get_tgt_node(arc), n1);
631}
632
633TEST(ListSGraph, EmptyGraphIterators)
634{
635 IntGraph g;
636
637 int node_count = 0;
638 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
639 ++node_count;
640 EXPECT_EQ(node_count, 0);
641
642 int arc_count = 0;
643 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
644 ++arc_count;
645 EXPECT_EQ(arc_count, 0);
646}
647
648TEST(ListSGraph, GetFirstNodeArc)
649{
650 IntGraph g;
651 auto n1 = g.insert_node(1);
652 auto n2 = g.insert_node(2);
653 auto arc = g.insert_arc(n1, n2, 10);
654
655 EXPECT_NE(g.get_first_node(), nullptr);
656 EXPECT_NE(g.get_first_arc(), nullptr);
657 EXPECT_EQ(g.get_first_arc(n1), arc);
658}
659
660TEST(ListSGraph, GetFirstArcNodeThrowsWhenEmpty)
661{
662 IntGraph g;
663 auto n1 = g.insert_node(1);
664
665 EXPECT_THROW(g.get_first_arc(n1), std::range_error);
666}
667
668TEST(ListSGraph, RemoveAllNodes)
669{
670 IntGraph g;
671 auto n1 = g.insert_node(1);
672 auto n2 = g.insert_node(2);
673 g.insert_arc(n1, n2, 10);
674
675 g.remove_node(n1);
676 g.remove_node(n2);
677
678 EXPECT_EQ(g.vsize(), 0u);
679 EXPECT_EQ(g.esize(), 0u);
680}
681
682TEST(ListSGraph, SwapGraphs)
683{
684 IntGraph g1;
685 g1.insert_node(1);
686 g1.insert_node(2);
687
688 IntGraph g2;
689 g2.insert_node(10);
690
691 g1.swap(g2);
692
693 EXPECT_EQ(g1.vsize(), 1u);
694 EXPECT_EQ(g2.vsize(), 2u);
695}
696
697// =============================================================================
698// String Data Type Tests
699// =============================================================================
700
701TEST(ListSGraph, StringNodeInfo)
702{
703 StringGraph g;
704 auto n1 = g.insert_node(std::string("node1"));
705 auto n2 = g.insert_node(std::string("node2"));
706
707 g.insert_arc(n1, n2, std::string("edge"));
708
709 EXPECT_EQ(n1->get_info(), "node1");
710 EXPECT_EQ(n2->get_info(), "node2");
711}
712
713// =============================================================================
714// Iterator Method Tests
715// =============================================================================
716
717TEST(ListSGraph, NodeIteratorCurrentNode)
718{
719 IntGraph g;
720 g.insert_node(42);
721
722 IntGraph::Node_Iterator it(g);
723 EXPECT_TRUE(it.has_curr());
724 EXPECT_EQ(it.get_current_node()->get_info(), 42);
725 EXPECT_EQ(it.get_current_node_ne()->get_info(), 42);
726}
727
728TEST(ListSGraph, NodeArcIteratorCurrentArc)
729{
730 IntGraph g;
731 auto n1 = g.insert_node(1);
732 auto n2 = g.insert_node(2);
733 g.insert_arc(n1, n2, 100);
734
735 IntGraph::Node_Arc_Iterator it(n1);
736 EXPECT_TRUE(it.has_curr());
737 EXPECT_EQ(it.get_current_arc()->get_info(), 100);
738 EXPECT_EQ(it.get_current_arc_ne()->get_info(), 100);
739}
740
741TEST(ListSGraph, ArcIteratorCurrentArc)
742{
743 IntGraph g;
744 auto n1 = g.insert_node(1);
745 auto n2 = g.insert_node(2);
746 g.insert_arc(n1, n2, 100);
747
748 IntGraph::Arc_Iterator it(g);
749 EXPECT_TRUE(it.has_curr());
750 EXPECT_EQ(it.get_current_arc()->get_info(), 100);
751 EXPECT_EQ(it.get_current_arc_ne()->get_info(), 100);
752}
753
754// =============================================================================
755// Multiple Operations Test
756// =============================================================================
757
758TEST(ListSGraph, ComplexOperations)
759{
760 IntGraph g;
761
762 // Build a graph
763 auto n1 = g.insert_node(1);
764 auto n2 = g.insert_node(2);
765 auto n3 = g.insert_node(3);
766 auto n4 = g.insert_node(4);
767
768 auto a12 = g.insert_arc(n1, n2, 12);
769 g.insert_arc(n2, n3, 23);
770 g.insert_arc(n3, n4, 34);
771 g.insert_arc(n4, n1, 41);
772
773 EXPECT_EQ(g.vsize(), 4u);
774 EXPECT_EQ(g.esize(), 4u);
775
776 // Remove one arc
777 g.remove_arc(a12);
778 EXPECT_EQ(g.esize(), 3u);
779
780 // Remove one node (should remove 2 arcs: 41 and 34)
781 g.remove_node(n4);
782 EXPECT_EQ(g.vsize(), 3u);
783 EXPECT_EQ(g.esize(), 1u); // Only 23 remains
784
785 // Copy the graph
786 IntGraph g2(g);
787 EXPECT_EQ(g2.vsize(), 3u);
788 EXPECT_EQ(g2.esize(), 1u);
789}
790
791// =============================================================================
792// Digraph Template Wrapper Tests
793// =============================================================================
794
795// These tests validate the generic Digraph<BaseGraph> template wrapper
796// that was introduced to reduce code duplication across digraph implementations.
797
798TEST(DigraphWrapper, DigraphInheritsFromBaseGraph)
799{
800 // Verify that List_SDigraph is actually a Digraph<List_SGraph<...>>
801 IntDigraph dg;
802
803 // Should inherit all base graph functionality
804 auto n1 = dg.insert_node(1);
805 auto n2 = dg.insert_node(2);
806 auto arc = dg.insert_arc(n1, n2, 100);
807
808 EXPECT_EQ(dg.vsize(), 2u);
809 EXPECT_EQ(dg.esize(), 1u);
810 EXPECT_TRUE(dg.is_digraph());
811 EXPECT_EQ(dg.get_src_node(arc), n1);
812 EXPECT_EQ(dg.get_tgt_node(arc), n2);
813}
814
815TEST(DigraphWrapper, DigraphCopyPreservesDirectedFlag)
816{
817 IntDigraph dg1;
818 dg1.insert_node(1);
819 dg1.insert_node(2);
820
821 // Copy construction should preserve digraph flag
822 IntDigraph dg2(dg1);
823 EXPECT_TRUE(dg2.is_digraph());
824 EXPECT_EQ(dg2.vsize(), 2u);
825}
826
827TEST(DigraphWrapper, DigraphMovePreservesDirectedFlag)
828{
829 IntDigraph dg1;
830 dg1.insert_node(1);
831 dg1.insert_node(2);
832
833 // Move construction should preserve digraph flag
834 IntDigraph dg2(std::move(dg1));
835 EXPECT_TRUE(dg2.is_digraph());
836 EXPECT_EQ(dg2.vsize(), 2u);
837}
838
839TEST(DigraphWrapper, DigraphCopyAssignmentPreservesDirectedFlag)
840{
841 IntDigraph dg1;
842 dg1.insert_node(1);
843
844 IntDigraph dg2;
845 dg2.insert_node(10);
846 dg2.insert_node(20);
847
848 dg2 = dg1;
849 EXPECT_TRUE(dg2.is_digraph());
850 EXPECT_EQ(dg2.vsize(), 1u);
851}
852
853TEST(DigraphWrapper, DigraphMoveAssignmentPreservesDirectedFlag)
854{
855 IntDigraph dg1;
856 dg1.insert_node(1);
857
858 IntDigraph dg2;
859 dg2.insert_node(10);
860
861 dg2 = std::move(dg1);
862 EXPECT_TRUE(dg2.is_digraph());
863 EXPECT_EQ(dg2.vsize(), 1u);
864}
865
866TEST(DigraphWrapper, DigraphSelfAssignment)
867{
868 IntDigraph dg;
869 auto n1 = dg.insert_node(1);
870 auto n2 = dg.insert_node(2);
871 dg.insert_arc(n1, n2, 100);
872
873 dg = dg; // Self-assignment should be safe
874
875 EXPECT_TRUE(dg.is_digraph());
876 EXPECT_EQ(dg.vsize(), 2u);
877 EXPECT_EQ(dg.esize(), 1u);
878}
879
880TEST(DigraphWrapper, DirectedArcNotVisibleFromTarget)
881{
882 IntDigraph dg;
883 auto n1 = dg.insert_node(1);
884 auto n2 = dg.insert_node(2);
885 dg.insert_arc(n1, n2, 100); // n1 -> n2
886
887 // In a digraph, arc should only be visible from source node
888 EXPECT_EQ(n1->num_arcs, 1u); // Source has the arc
889 // Note: In digraph mode, target node doesn't have the arc in its adjacency list
890}
891
892// =============================================================================
893// C++20 Concepts Tests
894// =============================================================================
895
896// Compile-time verification that List_SGraph iterators satisfy concepts
898 "List_SGraph::Node_Iterator must satisfy BasicGraphIterator");
900 "List_SGraph::Arc_Iterator must satisfy BasicGraphIterator");
902 "List_SGraph::Node_Iterator must satisfy GraphNodeIterator");
904 "List_SGraph::Arc_Iterator must satisfy GraphArcIterator");
905
906TEST(ConceptsTest, SGraphIteratorsSatisfyConcepts)
907{
908 IntGraph g;
909 auto n1 = g.insert_node(1);
910 auto n2 = g.insert_node(2);
911 g.insert_arc(n1, n2, 10);
912
913 // Verify node iterator works per concept
914 auto nit = g.get_node_it();
915 EXPECT_TRUE(nit.has_curr());
916 EXPECT_NE(nit.get_curr(), nullptr);
917
918 // Verify arc iterator works per concept
919 auto ait = g.get_arc_it();
920 EXPECT_TRUE(ait.has_curr());
921 EXPECT_NE(ait.get_curr(), nullptr);
922}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Node * get_first_node() const
Return any node in the graph.
Definition tpl_graph.H:590
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
Definition tpl_graph.H:609
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
void swap(List_Graph &g) noexcept
Swap in constant time this with g
Definition tpl_graph.H:1007
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:673
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Definition tpl_graph.H:557
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
void * tgt_node
Please don't use.
Definition graph-dry.H:793
void * src_node
Definition graph-dry.H:792
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:713
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
constexpr size_t vsize() const noexcept
See get_num_nodes().
Definition graph-dry.H:984
size_t esize() const noexcept
Return the total of arcs of graph.
Definition graph-dry.H:1078
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
Concept for basic graph iterators.
Definition graph-dry.H:393
Concept for graph arc iterators.
Definition graph-dry.H:451
Concept for graph node iterators.
Definition graph-dry.H:432
Concept for node adjacency iterators.
Definition graph-dry.H:475
#define TEST(name)
#define N
Definition fib.C:294
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Node for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:93
DynList< void * > arc_list
Adjacency list of arcs incident to this node.
Definition tpl_sgraph.H:97
Simple graph implementation with adjacency lists.