Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_graph_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
47#include <gtest/gtest.h>
48#include <vector>
49#include <algorithm>
50#include <memory>
51#include <stdexcept>
52#include <type_traits>
53#include <random>
54#include <string>
55#include <tpl_graph.H>
56#include <tpl_agraph.H>
57#include <tpl_sgraph.H>
58
59using namespace std;
60using namespace testing;
61using namespace Aleph;
62
63// =============================================================================
64// Type Definitions
65// =============================================================================
66
70
71// =============================================================================
72// Graph_Node Tests
73// =============================================================================
74
75TEST(GraphNodeTest, DefaultConstruction)
76{
77 Graph_Node<int> node;
78 EXPECT_EQ(node.get_info(), 0);
79 EXPECT_EQ(node.num_arcs, 0u);
80}
81
82TEST(GraphNodeTest, ConstructionWithInfo)
83{
84 Graph_Node<int> node(42);
85 EXPECT_EQ(node.get_info(), 42);
86}
87
88TEST(GraphNodeTest, CopyConstruction)
89{
90 Graph_Node<int> node1(42);
91 Graph_Node<int> node2(node1);
92 EXPECT_EQ(node2.get_info(), 42);
93}
94
95TEST(GraphNodeTest, MoveConstruction)
96{
97 Graph_Node<string> node1(string("test"));
98 Graph_Node<string> node2(std::move(node1));
99 EXPECT_EQ(node2.get_info(), "test");
100}
101
102TEST(GraphNodeTest, CopyAssignment)
103{
104 Graph_Node<int> node1(42);
105 Graph_Node<int> node2(100);
106 node2 = node1;
107 EXPECT_EQ(node2.get_info(), 42);
108}
109
110TEST(GraphNodeTest, SelfAssignment)
111{
112 Graph_Node<int> node(42);
113 node = node;
114 EXPECT_EQ(node.get_info(), 42);
115}
116
117TEST(GraphNodeTest, ConstructionFromPointer)
118{
119 Graph_Node<int> node1(42);
120 Graph_Node<int> node2(&node1);
121 EXPECT_EQ(node2.get_info(), 42);
122}
123
124// =============================================================================
125// Graph_Arc Tests
126// =============================================================================
127
128TEST(GraphArcTest, DefaultConstruction)
129{
131 EXPECT_EQ(arc.get_info(), 0.0);
132}
133
134TEST(GraphArcTest, ConstructionWithInfo)
135{
136 Graph_Arc<double> arc(3.14);
137 EXPECT_EQ(arc.get_info(), 3.14);
138}
139
140TEST(GraphArcTest, CopyConstruction)
141{
142 Graph_Arc<double> arc1(3.14);
143 Graph_Arc<double> arc2(arc1);
144 EXPECT_EQ(arc2.get_info(), 3.14);
145}
146
147TEST(GraphArcTest, CopyAssignment)
148{
149 Graph_Arc<double> arc1(3.14);
150 Graph_Arc<double> arc2(1.0);
151 arc2 = arc1;
152 EXPECT_EQ(arc2.get_info(), 3.14);
153}
154
155TEST(GraphArcTest, SelfAssignment)
156{
157 Graph_Arc<double> arc(3.14);
158 arc = arc;
159 EXPECT_EQ(arc.get_info(), 3.14);
160}
161
162// =============================================================================
163// Basic Graph Construction Tests
164// =============================================================================
165
166class GraphBasicTest : public Test
167{
168protected:
171};
172
173TEST_F(GraphBasicTest, DefaultConstructorCreatesEmptyGraph)
174{
175 EXPECT_EQ(g.get_num_nodes(), 0);
176 EXPECT_EQ(g.get_num_arcs(), 0);
177 EXPECT_FALSE(g.is_digraph());
178}
179
180TEST_F(GraphBasicTest, DefaultConstructorCreatesEmptyDigraph)
181{
182 EXPECT_EQ(dg.get_num_nodes(), 0);
183 EXPECT_EQ(dg.get_num_arcs(), 0);
184 EXPECT_TRUE(dg.is_digraph());
185}
186
187TEST_F(GraphBasicTest, InsertSingleNode)
188{
189 auto n = g.insert_node(10);
190 EXPECT_NE(n, nullptr);
191 EXPECT_EQ(n->get_info(), 10);
192 EXPECT_EQ(g.get_num_nodes(), 1);
193 EXPECT_EQ(g.get_num_arcs(), 0);
194}
195
196TEST_F(GraphBasicTest, InsertMultipleNodes)
197{
198 vector<Graph::Node*> nodes;
199 for (int i = 0; i < 100; ++i)
200 nodes.push_back(g.insert_node(i));
201
202 EXPECT_EQ(g.get_num_nodes(), 100);
203 for (int i = 0; i < 100; ++i)
204 EXPECT_EQ(nodes[i]->get_info(), i);
205}
206
207TEST_F(GraphBasicTest, InsertArcBetweenTwoNodes)
208{
209 auto n1 = g.insert_node(1);
210 auto n2 = g.insert_node(2);
211 auto a = g.insert_arc(n1, n2, 1.5);
212
213 EXPECT_NE(a, nullptr);
214 EXPECT_EQ(a->get_info(), 1.5);
215 EXPECT_EQ(g.get_num_arcs(), 1);
216 EXPECT_EQ(g.get_src_node(a), n1);
217 EXPECT_EQ(g.get_tgt_node(a), n2);
218}
219
220TEST_F(GraphBasicTest, InsertSelfLoop)
221{
222 auto n = g.insert_node(1);
223 auto a = g.insert_arc(n, n, 0.0);
224
225 EXPECT_NE(a, nullptr);
226 EXPECT_EQ(g.get_num_arcs(), 1);
227 EXPECT_EQ(g.get_src_node(a), n);
228 EXPECT_EQ(g.get_tgt_node(a), n);
229}
230
231// =============================================================================
232// Node Removal Tests
233// =============================================================================
234
236{
237protected:
239
240 void SetUp() override
241 {
242 for (int i = 0; i < 5; ++i)
243 g.insert_node(i);
244 }
245};
246
247TEST_F(GraphNodeRemovalTest, RemoveSingleNode)
248{
249 auto n = g.get_first_node();
250 g.remove_node(n);
251 EXPECT_EQ(g.get_num_nodes(), 4);
252}
253
254TEST_F(GraphNodeRemovalTest, RemoveNodeWithArcs)
255{
256 vector<Graph::Node*> nodes;
257 for (auto it = g.get_node_it(); it.has_curr(); it.next())
258 nodes.push_back(it.get_curr());
259
260 g.insert_arc(nodes[0], nodes[1], 1.0);
261 g.insert_arc(nodes[0], nodes[2], 2.0);
262 g.insert_arc(nodes[1], nodes[2], 3.0);
263
264 EXPECT_EQ(g.get_num_arcs(), 3);
265 g.remove_node(nodes[0]);
266 EXPECT_EQ(g.get_num_nodes(), 4);
267 EXPECT_EQ(g.get_num_arcs(), 1);
268}
269
271{
272 while (g.get_num_nodes() > 0)
273 g.remove_node(g.get_first_node());
274
275 EXPECT_EQ(g.get_num_nodes(), 0);
276 EXPECT_EQ(g.get_num_arcs(), 0);
277}
278
279// =============================================================================
280// Arc Removal Tests
281// =============================================================================
282
284{
285protected:
289
290 void SetUp() override
291 {
292 n1 = g.insert_node(1);
293 n2 = g.insert_node(2);
294 n3 = g.insert_node(3);
295 a1 = g.insert_arc(n1, n2, 1.0);
296 a2 = g.insert_arc(n2, n3, 2.0);
297 a3 = g.insert_arc(n1, n3, 3.0);
298 }
299};
300
302{
303 g.remove_arc(a1);
304 EXPECT_EQ(g.get_num_arcs(), 2);
305 EXPECT_EQ(g.get_num_nodes(), 3);
306}
307
309{
310 g.remove_arc(a1);
311 g.remove_arc(a2);
312 g.remove_arc(a3);
313 EXPECT_EQ(g.get_num_arcs(), 0);
314 EXPECT_EQ(g.get_num_nodes(), 3);
315}
316
317TEST_F(GraphArcRemovalTest, DisconnectAndReconnectArc)
318{
319 g.disconnect_arc(a1);
320 EXPECT_EQ(g.get_num_arcs(), 2);
321
322 g.connect_arc(a1);
323 EXPECT_EQ(g.get_num_arcs(), 3);
324 // Note: do not delete a1 here - the graph destructor will handle it
325}
326
327// =============================================================================
328// Iterator Tests
329// =============================================================================
330
332{
333protected:
335 vector<Graph::Node*> nodes;
336 vector<Graph::Arc*> arcs;
337
338 void SetUp() override
339 {
340 for (int i = 0; i < 5; ++i)
341 nodes.push_back(g.insert_node(i * 10));
342
343 arcs.push_back(g.insert_arc(nodes[0], nodes[1], 0.1));
344 arcs.push_back(g.insert_arc(nodes[1], nodes[2], 0.2));
345 arcs.push_back(g.insert_arc(nodes[2], nodes[3], 0.3));
346 arcs.push_back(g.insert_arc(nodes[3], nodes[4], 0.4));
347 arcs.push_back(g.insert_arc(nodes[4], nodes[0], 0.5));
348 }
349};
350
351TEST_F(GraphIteratorTest, NodeIteratorTraversesAllNodes)
352{
353 size_t count = 0;
354 for (auto it = g.get_node_it(); it.has_curr(); it.next())
355 ++count;
356
357 EXPECT_EQ(count, 5);
358}
359
360TEST_F(GraphIteratorTest, ArcIteratorTraversesAllArcs)
361{
362 size_t count = 0;
363 for (auto it = g.get_arc_it(); it.has_curr(); it.next())
364 ++count;
365
366 EXPECT_EQ(count, 5);
367}
368
369TEST_F(GraphIteratorTest, NodeArcIteratorTraversesAdjacentArcs)
370{
371 size_t count = 0;
372 for (auto it = g.get_arc_it(nodes[0]); it.has_curr(); it.next())
373 ++count;
374
375 EXPECT_EQ(count, 2);
376}
377
378TEST_F(GraphIteratorTest, IteratorResetFirst)
379{
380 auto it = g.get_node_it();
381 it.next();
382 it.next();
383 it.reset_first();
384 EXPECT_TRUE(it.has_curr());
385}
386
387TEST_F(GraphIteratorTest, IteratorResetLast)
388{
389 auto it = g.get_node_it();
390 it.reset_last();
391 EXPECT_TRUE(it.has_curr());
392}
393
394// =============================================================================
395// Filtered Iterator Tests
396// =============================================================================
397
399{
400 bool operator()(Graph::Node *n) const noexcept
401 {
402 return n->get_info() % 20 == 0;
403 }
404};
405
407{
408 bool operator()(Graph::Arc *a) const noexcept
409 {
410 return a->get_info() > 0.25;
411 }
412};
413
414TEST_F(GraphIteratorTest, FilteredNodeIterator)
415{
416 size_t count = 0;
417 for (Node_Iterator<Graph, EvenNodeFilter> it(g); it.has_curr(); it.next())
418 ++count;
419
420 EXPECT_EQ(count, 3);
421}
422
423TEST_F(GraphIteratorTest, FilteredArcIterator)
424{
425 size_t count = 0;
426 for (Arc_Iterator<Graph, HighWeightArcFilter> it(g); it.has_curr(); it.next())
427 ++count;
428
429 EXPECT_EQ(count, 3);
430}
431
432// =============================================================================
433// Search Tests
434// =============================================================================
435
436TEST_F(GraphIteratorTest, SearchArcBetweenNodes)
437{
438 auto arc = search_arc(g, nodes[0], nodes[1]);
439 EXPECT_NE(arc, nullptr);
440 EXPECT_EQ(arc->get_info(), 0.1);
441}
442
443TEST_F(GraphIteratorTest, SearchArcNotFound)
444{
445 auto arc = search_arc(g, nodes[0], nodes[2]);
446 EXPECT_EQ(arc, nullptr);
447}
448
449TEST_F(GraphIteratorTest, SearchDirectedArc)
450{
451 auto arc = search_directed_arc(g, nodes[0], nodes[1]);
452 EXPECT_NE(arc, nullptr);
453}
454
455// =============================================================================
456// Graph Move Tests (Copy tests removed due to linker dependencies)
457// =============================================================================
458
459class GraphMoveTest : public Test
460{
461protected:
463
464 void SetUp() override
465 {
466 auto n1 = g.insert_node(1);
467 auto n2 = g.insert_node(2);
468 auto n3 = g.insert_node(3);
469 g.insert_arc(n1, n2, 1.0);
470 g.insert_arc(n2, n3, 2.0);
471 }
472};
473
474TEST_F(GraphMoveTest, MoveConstructor)
475{
476 size_t orig_nodes = g.get_num_nodes();
477 size_t orig_arcs = g.get_num_arcs();
478
479 Graph moved(std::move(g));
480 EXPECT_EQ(moved.get_num_nodes(), orig_nodes);
481 EXPECT_EQ(moved.get_num_arcs(), orig_arcs);
482 EXPECT_EQ(g.get_num_nodes(), 0);
483 EXPECT_EQ(g.get_num_arcs(), 0);
484}
485
486TEST_F(GraphMoveTest, MoveAssignment)
487{
488 size_t orig_nodes = g.get_num_nodes();
489 size_t orig_arcs = g.get_num_arcs();
490
491 Graph moved;
492 moved = std::move(g);
493 EXPECT_EQ(moved.get_num_nodes(), orig_nodes);
494 EXPECT_EQ(moved.get_num_arcs(), orig_arcs);
495}
496
498{
499 Graph g2;
500 auto n = g2.insert_node(100);
501 g2.insert_arc(n, n, 0.5);
502
503 size_t g1_nodes = g.get_num_nodes();
504 size_t g2_nodes = g2.get_num_nodes();
505
506 g.swap(g2);
507
508 EXPECT_EQ(g.get_num_nodes(), g2_nodes);
509 EXPECT_EQ(g2.get_num_nodes(), g1_nodes);
510}
511
512TEST_F(GraphMoveTest, SelfAssignment)
513{
514 size_t orig_nodes = g.get_num_nodes();
515 size_t orig_arcs = g.get_num_arcs();
516
517 g = g; // Self-assignment should be safe
518
519 EXPECT_EQ(g.get_num_nodes(), orig_nodes);
520 EXPECT_EQ(g.get_num_arcs(), orig_arcs);
521}
522
523TEST_F(GraphMoveTest, CopyConstruction)
524{
525 Graph copy(g);
526
527 EXPECT_EQ(copy.get_num_nodes(), g.get_num_nodes());
528 EXPECT_EQ(copy.get_num_arcs(), g.get_num_arcs());
529
530 // Verify deep copy: modifying copy doesn't affect original
531 auto n = copy.insert_node(999);
532 copy.insert_arc(n, n, 99.0);
533 EXPECT_NE(copy.get_num_nodes(), g.get_num_nodes());
534 EXPECT_NE(copy.get_num_arcs(), g.get_num_arcs());
535}
536
537TEST_F(GraphMoveTest, CopyAssignment)
538{
539 Graph copy;
540 copy = g;
541
542 EXPECT_EQ(copy.get_num_nodes(), g.get_num_nodes());
543 EXPECT_EQ(copy.get_num_arcs(), g.get_num_arcs());
544}
545
546TEST_F(GraphMoveTest, CopyToNonEmptyGraph)
547{
548 // Create a non-empty target graph
549 Graph target;
550 for (int i = 0; i < 10; ++i)
551 target.insert_node(i * 100);
552
553 size_t orig_nodes = g.get_num_nodes();
554 size_t orig_arcs = g.get_num_arcs();
555
556 // Copy should replace contents, not append
557 target = g;
558
559 EXPECT_EQ(target.get_num_nodes(), orig_nodes);
560 EXPECT_EQ(target.get_num_arcs(), orig_arcs);
561}
562
563// =============================================================================
564// Path Tests
565// =============================================================================
566
567class PathTest : public Test
568{
569protected:
571 vector<Graph::Node*> nodes;
572 vector<Graph::Arc*> arcs;
573
574 void SetUp() override
575 {
576 for (int i = 0; i < 5; ++i)
577 nodes.push_back(g.insert_node(i));
578
579 arcs.push_back(g.insert_arc(nodes[0], nodes[1], 0.1));
580 arcs.push_back(g.insert_arc(nodes[1], nodes[2], 0.2));
581 arcs.push_back(g.insert_arc(nodes[2], nodes[3], 0.3));
582 arcs.push_back(g.insert_arc(nodes[3], nodes[4], 0.4));
583 }
584};
585
586TEST_F(PathTest, EmptyPathConstruction)
587{
588 Path<Graph> path(g);
589 EXPECT_TRUE(path.is_empty());
590 EXPECT_EQ(path.size(), 0);
591}
592
593TEST_F(PathTest, PathWithSingleNode)
594{
595 Path<Graph> path(g, nodes[0]);
596 EXPECT_FALSE(path.is_empty());
597 EXPECT_EQ(path.size(), 1);
598 EXPECT_EQ(path.get_first_node(), nodes[0]);
599 EXPECT_EQ(path.get_last_node(), nodes[0]);
600}
601
602TEST_F(PathTest, AppendArcToPath)
603{
604 Path<Graph> path(g, nodes[0]);
605 path.append(arcs[0]);
606 EXPECT_EQ(path.size(), 2);
607 EXPECT_EQ(path.get_first_node(), nodes[0]);
608 EXPECT_EQ(path.get_last_node(), nodes[1]);
609}
610
611TEST_F(PathTest, AppendNodeToPath)
612{
613 Path<Graph> path(g, nodes[0]);
614 path.append(nodes[1]);
615 EXPECT_EQ(path.size(), 2);
616 EXPECT_EQ(path.get_last_node(), nodes[1]);
617}
618
619TEST_F(PathTest, InsertArcToPath)
620{
621 Path<Graph> path(g, nodes[1]);
622 path.insert(arcs[0]);
623 EXPECT_EQ(path.size(), 2);
624 EXPECT_EQ(path.get_first_node(), nodes[0]);
625}
626
627TEST_F(PathTest, InsertNodeToPath)
628{
629 Path<Graph> path(g, nodes[1]);
630 path.insert(nodes[0]);
631 EXPECT_EQ(path.size(), 2);
632 EXPECT_EQ(path.get_first_node(), nodes[0]);
633}
634
635TEST_F(PathTest, BuildFullPath)
636{
637 Path<Graph> path(g, nodes[0]);
638 for (size_t i = 0; i < arcs.size(); ++i)
639 path.append(arcs[i]);
640
641 EXPECT_EQ(path.size(), 5);
642 EXPECT_EQ(path.get_first_node(), nodes[0]);
643 EXPECT_EQ(path.get_last_node(), nodes[4]);
644}
645
646TEST_F(PathTest, FirstAndLastArc)
647{
648 Path<Graph> path(g, nodes[0]);
649 EXPECT_THROW(path.get_last_arc(), std::domain_error);
650
651 for (size_t i = 0; i < arcs.size(); ++i)
652 path.append(arcs[i]);
653
654 const Path<Graph> & cpath = path;
655 EXPECT_EQ(cpath.get_first_arc(), arcs[0]);
656 EXPECT_EQ(cpath.get_last_arc(), arcs[3]);
657}
658
659TEST_F(PathTest, PathContainsNode)
660{
661 Path<Graph> path(g, nodes[0]);
662 path.append(arcs[0]);
663 path.append(arcs[1]);
664
665 EXPECT_TRUE(path.contains_node(nodes[0]));
666 EXPECT_TRUE(path.contains_node(nodes[1]));
667 EXPECT_TRUE(path.contains_node(nodes[2]));
668 EXPECT_FALSE(path.contains_node(nodes[3]));
669}
670
671TEST_F(PathTest, PathContainsArc)
672{
673 Path<Graph> path(g, nodes[0]);
674 path.append(arcs[0]);
675 path.append(arcs[1]);
676
677 EXPECT_TRUE(path.contains_arc(arcs[0]));
678 EXPECT_TRUE(path.contains_arc(arcs[1]));
679 EXPECT_FALSE(path.contains_arc(arcs[2]));
680}
681
682TEST_F(PathTest, PathIterator)
683{
684 Path<Graph> path(g, nodes[0]);
685 path.append(arcs[0]);
686 path.append(arcs[1]);
687
688 size_t node_count = 0;
689 for (typename Path<Graph>::Iterator it(path); it.has_current_node(); it.next())
690 ++node_count;
691
692 EXPECT_EQ(node_count, 3);
693}
694
695TEST_F(PathTest, PathNodesList)
696{
697 Path<Graph> path(g, nodes[0]);
698 path.append(arcs[0]);
699 path.append(arcs[1]);
700
701 auto node_list = path.nodes();
702 EXPECT_EQ(node_list.size(), 3);
703}
704
705TEST_F(PathTest, PathArcsList)
706{
707 Path<Graph> path(g, nodes[0]);
708 path.append(arcs[0]);
709 path.append(arcs[1]);
710
711 auto arc_list = path.arcs();
712 EXPECT_EQ(arc_list.size(), 2);
713}
714
715TEST_F(PathTest, PathCopyConstruction)
716{
717 Path<Graph> path(g, nodes[0]);
718 path.append(arcs[0]);
719 path.append(arcs[1]);
720
721 Path<Graph> copy(path);
722 EXPECT_EQ(copy.size(), path.size());
723}
724
725TEST_F(PathTest, PathMoveConstruction)
726{
727 Path<Graph> path(g, nodes[0]);
728 path.append(arcs[0]);
729 path.append(arcs[1]);
730
731 size_t orig_size = path.size();
732 Path<Graph> moved(std::move(path));
733 EXPECT_EQ(moved.size(), orig_size);
734}
735
736TEST_F(PathTest, RemoveLastNode)
737{
738 Path<Graph> path(g, nodes[0]);
739 path.append(arcs[0]);
740 path.append(arcs[1]);
741
742 auto removed = path.remove_last_node();
743 EXPECT_EQ(removed, nodes[2]);
744 EXPECT_EQ(path.size(), 2);
745}
746
747// Removing the only node threw underflow_error, after it was removed
748TEST_F(PathTest, RemoveTheOnlyNode)
749{
750 Path<Graph> path(g, nodes[0]);
751
752 EXPECT_EQ(path.remove_last_node(), nodes[0]);
753 EXPECT_TRUE(path.is_empty());
754 EXPECT_THROW(path.remove_last_node(), std::underflow_error);
755}
756
757TEST_F(PathTest, RemoveFirstNode)
758{
759 Path<Graph> path(g, nodes[0]);
760 path.append(arcs[0]);
761 path.append(arcs[1]);
762
763 auto removed = path.remove_first_node();
764 EXPECT_EQ(removed, nodes[0]);
765 EXPECT_EQ(path.size(), 2);
766}
767
769{
770 auto loop_arc = g.insert_arc(nodes[4], nodes[0], 0.5);
771
772 Path<Graph> path(g, nodes[0]);
773 for (auto a : arcs)
774 path.append(a);
775 path.append(loop_arc);
776
777 EXPECT_TRUE(path.is_cycle());
778}
779
780TEST_F(PathTest, IsNotCycle)
781{
782 Path<Graph> path(g, nodes[0]);
783 path.append(arcs[0]);
784 path.append(arcs[1]);
785
786 EXPECT_FALSE(path.is_cycle());
787}
788
789// =============================================================================
790// Directed Path Tests
791// =============================================================================
792
793class DirectedPathTest : public Test
794{
795protected:
797 vector<TestDigraph::Node*> nodes;
798 vector<TestDigraph::Arc*> arcs;
799
800 void SetUp() override
801 {
802 for (int i = 0; i < 5; ++i)
803 nodes.push_back(dg.insert_node(i));
804
805 arcs.push_back(dg.insert_arc(nodes[0], nodes[1], 0.1));
806 arcs.push_back(dg.insert_arc(nodes[1], nodes[2], 0.2));
807 arcs.push_back(dg.insert_arc(nodes[2], nodes[3], 0.3));
808 arcs.push_back(dg.insert_arc(nodes[3], nodes[4], 0.4));
809 }
810};
811
812TEST_F(DirectedPathTest, AppendDirectedNode)
813{
814 Path<TestDigraph> path(dg, nodes[0]);
815 path.append_directed(nodes[1]);
816 EXPECT_EQ(path.size(), 2);
817 EXPECT_EQ(path.get_last_node(), nodes[1]);
818}
819
820TEST_F(DirectedPathTest, AppendDirectedArc)
821{
822 Path<TestDigraph> path(dg, nodes[0]);
823 path.append_directed(arcs[0]);
824 EXPECT_EQ(path.size(), 2);
825 EXPECT_EQ(path.get_last_node(), nodes[1]);
826}
827
828TEST_F(DirectedPathTest, InsertDirectedNode)
829{
830 Path<TestDigraph> path(dg, nodes[1]);
831 path.insert_directed(nodes[0]);
832 EXPECT_EQ(path.size(), 2);
833 EXPECT_EQ(path.get_first_node(), nodes[0]);
834}
835
836TEST_F(DirectedPathTest, InsertDirectedArc)
837{
838 Path<TestDigraph> path(dg, nodes[1]);
839 path.insert_directed(arcs[0]);
840 EXPECT_EQ(path.size(), 2);
841 EXPECT_EQ(path.get_first_node(), nodes[0]);
842}
843
844// =============================================================================
845// Exception Tests
846// =============================================================================
847
849{
850protected:
852};
853
854TEST_F(GraphExceptionTest, GetFirstNodeOnEmptyGraphThrows)
855{
856 EXPECT_THROW(g.get_first_node(), std::range_error);
857}
858
859TEST_F(GraphExceptionTest, GetFirstArcOnEmptyGraphThrows)
860{
861 g.insert_node(1);
862 EXPECT_THROW(g.get_first_arc(), std::range_error);
863}
864
865TEST_F(GraphExceptionTest, PathAppendOnEmptyPathThrows)
866{
867 auto n1 = g.insert_node(1);
868 auto n2 = g.insert_node(2);
869 auto a = g.insert_arc(n1, n2, 1.0);
870
871 Path<Graph> path(g);
872 EXPECT_THROW(path.append(a), std::domain_error);
873}
874
875TEST_F(GraphExceptionTest, PathAppendInvalidArcThrows)
876{
877 auto n1 = g.insert_node(1);
878 auto n2 = g.insert_node(2);
879 auto n3 = g.insert_node(3);
880 auto a = g.insert_arc(n2, n3, 1.0);
881
882 Path<Graph> path(g, n1);
883 EXPECT_THROW(path.append(a), std::invalid_argument);
884}
885
886TEST_F(GraphExceptionTest, PathInsertOnEmptyPathThrows)
887{
888 auto n1 = g.insert_node(1);
889 auto n2 = g.insert_node(2);
890 auto a = g.insert_arc(n1, n2, 1.0);
891
892 Path<Graph> path(g);
893 EXPECT_THROW(path.insert(a), std::domain_error);
894}
895
896// =============================================================================
897// Digraph-specific Tests
898// =============================================================================
899
900class DigraphTest : public Test
901{
902protected:
904 vector<TestDigraph::Node*> nodes;
905 vector<TestDigraph::Arc*> arcs;
906
907 void SetUp() override
908 {
909 for (int i = 0; i < 4; ++i)
910 nodes.push_back(dg.insert_node(i));
911
912 arcs.push_back(dg.insert_arc(nodes[0], nodes[1], 1.0));
913 arcs.push_back(dg.insert_arc(nodes[1], nodes[2], 2.0));
914 arcs.push_back(dg.insert_arc(nodes[2], nodes[3], 3.0));
915 arcs.push_back(dg.insert_arc(nodes[0], nodes[2], 4.0));
916 }
917};
918
919TEST_F(DigraphTest, NodeDegrees)
920{
921 // Count outgoing arcs manually
922 size_t out_count_0 = 0;
923 for (auto it = dg.get_arc_it(nodes[0]); it.has_curr(); it.next())
924 if (dg.get_src_node(it.get_curr()) == nodes[0])
925 ++out_count_0;
926 EXPECT_EQ(out_count_0, 2);
927
928 // Node 3 has no outgoing arcs
929 size_t out_count_3 = 0;
930 for (auto it = dg.get_arc_it(nodes[3]); it.has_curr(); it.next())
931 if (dg.get_src_node(it.get_curr()) == nodes[3])
932 ++out_count_3;
933 EXPECT_EQ(out_count_3, 0);
934}
935
936TEST_F(DigraphTest, TraverseArcsFromNode)
937{
938 size_t count = 0;
939 for (auto it = dg.get_arc_it(nodes[0]); it.has_curr(); it.next())
940 ++count;
941
942 EXPECT_EQ(count, 2);
943}
944
945TEST_F(DigraphTest, ArcDirection)
946{
947 // Verify arc direction
948 EXPECT_EQ(dg.get_src_node(arcs[0]), nodes[0]);
949 EXPECT_EQ(dg.get_tgt_node(arcs[0]), nodes[1]);
950}
951
952// =============================================================================
953// Functional Operations Tests
954// =============================================================================
955
956class GraphFunctionalTest : public Test
957{
958protected:
960 vector<Graph::Node*> nodes;
961
962 void SetUp() override
963 {
964 for (int i = 1; i <= 5; ++i)
965 nodes.push_back(g.insert_node(i * 10));
966
967 g.insert_arc(nodes[0], nodes[1], 1.0);
968 g.insert_arc(nodes[1], nodes[2], 2.0);
969 g.insert_arc(nodes[2], nodes[3], 3.0);
970 g.insert_arc(nodes[3], nodes[4], 4.0);
971 }
972};
973
975{
976 int sum = 0;
977 for_each_node<Graph>(g, [&sum](Graph::Node *n) { sum += n->get_info(); });
978 EXPECT_EQ(sum, 150);
979}
980
982{
983 double sum = 0;
984 for_each_arc<Graph>(g, [&sum](Graph::Arc *a) { sum += a->get_info(); });
985 EXPECT_EQ(sum, 10.0);
986}
987
989{
990 bool all_positive = forall_node<Graph>(g, [](Graph::Node *n) {
991 return n->get_info() > 0;
992 });
993 EXPECT_TRUE(all_positive);
994}
995
997{
998 bool all_positive = forall_arc<Graph>(g, [](Graph::Arc *a) {
999 return a->get_info() > 0;
1000 });
1001 EXPECT_TRUE(all_positive);
1002}
1003
1005{
1006 int sum = foldl_nodes<Graph, int>(g, 0, [](const int & acc, Graph::Node *n) {
1007 return acc + n->get_info();
1008 });
1009 EXPECT_EQ(sum, 150);
1010}
1011
1013{
1014 double sum = foldl_arcs<Graph, double>(g, 0.0, [](const double & acc, Graph::Arc *a) {
1015 return acc + a->get_info();
1016 });
1017 EXPECT_EQ(sum, 10.0);
1018}
1019
1020// =============================================================================
1021// Find Path Tests
1022// =============================================================================
1023
1024TEST_F(GraphFunctionalTest, FindPathDepthFirst)
1025{
1026 auto path = find_path_depth_first(g, nodes[0], nodes[4]);
1027 EXPECT_FALSE(path.is_empty());
1028 EXPECT_EQ(path.get_first_node(), nodes[0]);
1029 EXPECT_EQ(path.get_last_node(), nodes[4]);
1030}
1031
1032TEST_F(GraphFunctionalTest, FindPathDepthFirstNotFound)
1033{
1034 Graph g2;
1035 auto n1 = g2.insert_node(1);
1036 auto n2 = g2.insert_node(2);
1037
1038 auto path = find_path_depth_first(g2, n1, n2);
1039 EXPECT_TRUE(path.is_empty());
1040}
1041
1042// =============================================================================
1043// Stress Tests
1044// =============================================================================
1045
1046class GraphStressTest : public Test
1047{
1048protected:
1050 static constexpr size_t NUM_NODES = 1000;
1051 static constexpr size_t NUM_ARCS = 5000;
1052};
1053
1054TEST_F(GraphStressTest, InsertManyNodes)
1055{
1056 for (size_t i = 0; i < NUM_NODES; ++i)
1057 g.insert_node(static_cast<int>(i));
1058
1059 EXPECT_EQ(g.get_num_nodes(), NUM_NODES);
1060}
1061
1062TEST_F(GraphStressTest, InsertManyArcs)
1063{
1064 vector<Graph::Node*> nodes;
1065 for (size_t i = 0; i < NUM_NODES; ++i)
1066 nodes.push_back(g.insert_node(static_cast<int>(i)));
1067
1068 std::mt19937 rng(42);
1069 std::uniform_int_distribution<size_t> dist(0, NUM_NODES - 1);
1070
1071 for (size_t i = 0; i < NUM_ARCS; ++i)
1072 {
1073 size_t src = dist(rng);
1074 size_t tgt = dist(rng);
1075 g.insert_arc(nodes[src], nodes[tgt], static_cast<double>(i));
1076 }
1077
1078 EXPECT_EQ(g.get_num_arcs(), NUM_ARCS);
1079}
1080
1081TEST_F(GraphStressTest, InsertAndRemoveManyNodes)
1082{
1083 vector<Graph::Node*> nodes;
1084 for (size_t i = 0; i < 100; ++i)
1085 nodes.push_back(g.insert_node(static_cast<int>(i)));
1086
1087 EXPECT_EQ(g.get_num_nodes(), 100);
1088
1089 // Remove half
1090 for (size_t i = 0; i < 50; ++i)
1091 g.remove_node(nodes[i]);
1092
1093 EXPECT_EQ(g.get_num_nodes(), 50);
1094}
1095
1096// =============================================================================
1097// Sorting Tests
1098// =============================================================================
1099
1101{
1102 g.sort_nodes([](Graph::Node *a, Graph::Node *b) {
1103 return a->get_info() > b->get_info();
1104 });
1105
1106 auto it = g.get_node_it();
1107 int prev = it.get_curr()->get_info();
1108 it.next();
1109
1110 while (it.has_curr())
1111 {
1112 EXPECT_GE(prev, it.get_curr()->get_info());
1113 prev = it.get_curr()->get_info();
1114 it.next();
1115 }
1116}
1117
1119{
1120 g.sort_arcs([](Graph::Arc *a, Graph::Arc *b) {
1121 return a->get_info() > b->get_info();
1122 });
1123
1124 auto it = g.get_arc_it();
1125 double prev = it.get_curr()->get_info();
1126 it.next();
1127
1128 while (it.has_curr())
1129 {
1130 EXPECT_GE(prev, it.get_curr()->get_info());
1131 prev = it.get_curr()->get_info();
1132 it.next();
1133 }
1134}
1135
1136namespace
1137{
1138 // A comparison of arcs that throws at its call number fail_at
1139 struct Failing_Arc_Cmp
1140 {
1141 int * calls;
1142 int fail_at;
1143
1144 bool operator()(Graph::Arc *a, Graph::Arc *b) const
1145 {
1146 if (++*calls == fail_at)
1147 throw std::runtime_error("comparison failed");
1148 return a->get_info() < b->get_info();
1149 }
1150 };
1151
1152 struct Nothrow_Arc_Cmp
1153 {
1154 bool operator()(Graph::Arc *a, Graph::Arc *b) const noexcept
1155 {
1156 return a->get_info() < b->get_info();
1157 }
1158 };
1159}
1160
1161// sort_nodes() and sort_arcs() were noexcept, so a comparison that threw
1162// ended the program; mergesort() of a list leaves its elements spread over
1163// lists of its own when the comparison throws, so the graph lost its arcs
1164TEST_F(GraphFunctionalTest, SortArcsKeepsEveryArcIfTheComparisonThrows)
1165{
1166 std::vector<Graph::Arc *> arcs;
1167 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
1168 arcs.push_back(it.get_curr());
1169 ASSERT_GE(arcs.size(), 4u);
1170 std::sort(arcs.begin(), arcs.end());
1171
1172 int calls = 0;
1173 g.sort_arcs(Failing_Arc_Cmp{&calls, 0});
1174 const int total = calls;
1175 for (int k = 1; k <= total; ++k)
1176 {
1177 calls = 0;
1178 EXPECT_THROW(g.sort_arcs(Failing_Arc_Cmp{&calls, k}), std::runtime_error);
1179 std::vector<Graph::Arc *> now;
1180 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
1181 now.push_back(it.get_curr());
1182 std::sort(now.begin(), now.end());
1183 EXPECT_EQ(now, arcs) << "call " << k;
1184 EXPECT_EQ(g.get_num_arcs(), arcs.size());
1185 }
1186
1187 g.sort_arcs(Nothrow_Arc_Cmp());
1188 double prev = -1;
1189 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
1190 {
1191 EXPECT_LE(prev, it.get_curr()->get_info());
1192 prev = it.get_curr()->get_info();
1193 }
1194
1195 Failing_Arc_Cmp failing{&calls, 0};
1196 Nothrow_Arc_Cmp nothrow;
1197 static_assert(not noexcept(g.sort_arcs(failing)));
1198 static_assert(noexcept(g.sort_arcs(nothrow)));
1199}
1200
1201// get_control_bits() of a node returned what reset() returns, void, so it
1202// did not compile, and it would have cleared the bits
1203TEST(GraphCommonTest, GetControlBitsOfANode)
1204{
1205 Graph g;
1206 auto p = g.insert_node(1);
1207 NODE_BITS(p).set_bit(Aleph::Depth_First, true);
1209 EXPECT_TRUE(bits.get_bit(Aleph::Depth_First));
1210 bits.set_bit(Aleph::Breadth_First, true);
1211 EXPECT_TRUE(IS_NODE_VISITED(p, Aleph::Breadth_First));
1212 EXPECT_TRUE(IS_NODE_VISITED(p, Aleph::Depth_First));
1213}
1214
1215namespace
1216{
1217 // A payload that its constructor takes by moving a unique_ptr
1218 struct Owned
1219 {
1220 std::unique_ptr<int> value;
1221 Owned() = default;
1222 explicit Owned(std::unique_ptr<int> v) noexcept : value(std::move(v)) {}
1223 Owned(Owned &&) noexcept = default;
1224 Owned & operator=(Owned &&) noexcept = default;
1225 Owned(const Owned & o) : value(o.value ? std::make_unique<int>(*o.value) : nullptr) {}
1226 Owned & operator=(const Owned & o)
1227 {
1228 value = o.value ? std::make_unique<int>(*o.value) : nullptr;
1229 return *this;
1230 }
1231 };
1232
1233 // A payload whose copy throws on demand
1234 struct Fragile
1235 {
1236 static inline bool fail = false;
1237 int value = 0;
1238 Fragile() = default;
1239 explicit Fragile(int v) noexcept : value(v) {}
1240 Fragile(const Fragile & f) : value(f.value)
1241 {
1242 if (fail)
1243 throw std::runtime_error("copy failed");
1244 }
1245 Fragile & operator=(const Fragile &) = default;
1246 };
1247}
1248
1249// emplace_node() and emplace_arc() gave their arguments as lvalues, so an
1250// argument that can only be moved, as a unique_ptr, did not compile
1251TEST(GraphCommonTest, EmplaceForwardsItsArguments)
1252{
1254 auto a = g.emplace_node(std::make_unique<int>(1));
1255 auto b = g.emplace_node(std::make_unique<int>(2));
1256 auto arc = g.emplace_arc(a, b, std::make_unique<int>(3));
1257 EXPECT_EQ(*a->get_info().value, 1);
1258 EXPECT_EQ(*b->get_info().value, 2);
1259 EXPECT_EQ(*arc->get_info().value, 3);
1260}
1261
1262// The constructors of nodes and arcs that copy their data were noexcept, so
1263// a copy that threw ended the program
1264TEST(GraphNodeTest, ACopyOfTheDataThatThrowsReachesTheCaller)
1265{
1266 static_assert(std::is_nothrow_constructible_v<Graph_Node<int>, const int &>);
1267 static_assert(std::is_nothrow_constructible_v<Graph_Arc<int>, const int &>);
1268 static_assert(not std::is_nothrow_constructible_v<Graph_Node<Fragile>,
1269 const Fragile &>);
1270 static_assert(not std::is_nothrow_constructible_v<Graph_Arc<Fragile>,
1271 const Fragile &>);
1272
1274 const Fragile one(1), two(2);
1275 auto a = g.insert_node(one);
1276 auto b = g.insert_node(two);
1277 Fragile::fail = true;
1278 EXPECT_THROW(g.insert_node(one), std::runtime_error);
1279 EXPECT_THROW(g.insert_arc(a, b, two), std::runtime_error);
1280 Fragile::fail = false;
1281 EXPECT_EQ(g.get_num_nodes(), 2u);
1282 EXPECT_EQ(g.get_num_arcs(), 0u);
1283}
1284
1285namespace
1286{
1287 struct Not_Three
1288 {
1289 bool operator()(Graph::Node * p) const noexcept { return p->get_info() != 3; }
1290 };
1291
1292 struct Not_One
1293 {
1294 bool operator()(Graph::Arc * a) const noexcept { return a->get_info() != 1; }
1295 };
1296
1297 template <class G>
1298 std::vector<double> arc_infos(const G & g)
1299 {
1300 std::vector<double> infos;
1301 for (typename G::Arc_Iterator it(g); it.has_curr(); it.next_ne())
1302 infos.push_back(it.get_curr()->get_info());
1303 std::sort(infos.begin(), infos.end());
1304 return infos;
1305 }
1306}
1307
1308// Copy_Graph copied every arc that the arc filter shows: an arc to a node
1309// that the node filter hides took a null image, and the copy aborted. It
1310// copies the arcs between the nodes that it shows.
1311TEST(CopyGraphTest, CopiesTheArcsBetweenTheNodesThatItShows)
1312{
1313 Graph g;
1314 auto a = g.insert_node(1);
1315 auto b = g.insert_node(2);
1316 auto c = g.insert_node(3);
1317 g.insert_arc(a, b, 1);
1318 g.insert_arc(b, c, 2);
1319 g.insert_arc(c, a, 3);
1320 g.insert_arc(a, a, 4);
1321
1322 Graph tgt;
1323 Copy_Graph<Graph, Not_Three>(Dft_Show_Arc<Graph>(), Not_Three())(tgt, g);
1324 EXPECT_EQ(tgt.get_num_nodes(), 2u);
1325 EXPECT_EQ(arc_infos(tgt), (std::vector<double>{1, 4}));
1326 EXPECT_EQ(mapped_node<Graph>(a)->get_info(), 1);
1327 EXPECT_EQ(mapped_node<Graph>(b)->get_info(), 2);
1328 EXPECT_EQ(mapped_node<Graph>(c), nullptr);
1329
1330 Copy_Graph<Graph, Not_Three, Not_One>(Not_One(), Not_Three())(tgt, g, false);
1331 EXPECT_EQ(tgt.get_num_nodes(), 2u);
1332 EXPECT_EQ(arc_infos(tgt), (std::vector<double>{4}));
1333
1334 // a digraph keeps the sense of the arcs
1335 TestDigraph dg;
1336 auto x = dg.insert_node(1);
1337 auto y = dg.insert_node(2);
1338 auto z = dg.insert_node(3);
1339 dg.insert_arc(y, x, 1);
1340 dg.insert_arc(x, z, 2);
1341 dg.insert_arc(z, y, 3);
1342 struct Digraph_Not_Three
1343 {
1344 bool operator()(TestDigraph::Node * p) const noexcept { return p->get_info() != 3; }
1345 };
1346 TestDigraph dtgt;
1348 Digraph_Not_Three())(dtgt, dg);
1349 ASSERT_EQ(dtgt.get_num_arcs(), 1u);
1350 auto arc = dtgt.get_first_arc();
1351 EXPECT_EQ(dtgt.get_src_node(arc)->get_info(), 2);
1352 EXPECT_EQ(dtgt.get_tgt_node(arc)->get_info(), 1);
1353}
1354
1355namespace
1356{
1357 template <class A, class B>
1358 constexpr bool can_swap = requires (A & a, B & b) { a.swap(b); };
1359}
1360
1361// A digraph took the swap of its base graph, which also takes an undirected
1362// graph: the digraph was left with the arcs and the flag of the other one
1363TEST(DigraphSwap, SwapsOnlyWithADigraph)
1364{
1365 static_assert(can_swap<TestDigraph, TestDigraph>);
1366 static_assert(not can_swap<TestDigraph, Graph>);
1367 static_assert(can_swap<Graph, Graph>);
1368
1369 TestDigraph a, b;
1370 auto p = a.insert_node(1);
1371 auto q = a.insert_node(2);
1372 a.insert_arc(p, q, 1.0);
1373 b.insert_node(3);
1374
1375 a.swap(b);
1376 EXPECT_TRUE(a.is_digraph());
1377 EXPECT_TRUE(b.is_digraph());
1378 EXPECT_EQ(a.get_num_nodes(), 1u);
1379 EXPECT_EQ(b.get_num_nodes(), 2u);
1380 EXPECT_EQ(b.get_num_arcs(), 1u);
1381 EXPECT_EQ(b.get_num_arcs(p), 1u);
1382 EXPECT_EQ(b.get_num_arcs(q), 0u); // a directed arc is only in its source
1383
1384 TestDigraph moved(std::move(b));
1385 EXPECT_TRUE(moved.is_digraph());
1386 EXPECT_EQ(moved.get_num_arcs(), 1u);
1387}
1388
1389// A graph of an undirected type that set_digraph() marks as directed, as
1390// Random_Digraph does with its graph, links its arcs as a digraph, so its
1391// flag goes with them: a move from such a graph aborted, on an assertion
1392// that both graphs of a swap had the same flag
1393template <class G>
1394struct DirectedFlag : public ::testing::Test {};
1395
1397 ::testing::Types<List_Graph<Graph_Node<int>, Graph_Arc<int>>,
1401
1402TYPED_TEST(DirectedFlag, GoesWithTheArcs)
1403{
1404 using G = TypeParam;
1405 G g;
1406 g.set_digraph(true);
1407 auto p = g.insert_node(1);
1408 auto q = g.insert_node(2);
1409 auto pq = g.insert_arc(p, q, 0);
1410 ASSERT_EQ(g.get_num_arcs(q), 0u); // a directed arc is only in its source
1411
1412 G moved(std::move(g));
1413 EXPECT_TRUE(moved.is_digraph());
1414 EXPECT_EQ(moved.get_num_arcs(), 1u);
1415 EXPECT_EQ(g.get_num_nodes(), 0u);
1416
1417 G other;
1418 other.insert_node(3);
1419 other = std::move(moved);
1420 EXPECT_TRUE(other.is_digraph());
1421 EXPECT_FALSE(moved.is_digraph()); // it has what other had
1422 EXPECT_EQ(moved.get_num_nodes(), 1u);
1423
1424 G undirected;
1425 auto r = undirected.insert_node(4);
1426 auto s = undirected.insert_node(5);
1427 auto rs = undirected.insert_arc(r, s, 0);
1428 undirected.swap(other);
1429 ASSERT_TRUE(undirected.is_digraph());
1430 ASSERT_FALSE(other.is_digraph());
1431 EXPECT_EQ(other.get_num_arcs(s), 1u); // an undirected arc is in both nodes
1432
1433 // each one removes its arc as its flag says
1434 undirected.remove_arc(pq);
1435 other.remove_arc(rs);
1436 EXPECT_EQ(undirected.get_num_arcs(), 0u);
1437 EXPECT_EQ(undirected.get_num_arcs(p), 0u);
1438 EXPECT_EQ(other.get_num_arcs(), 0u);
1439 EXPECT_EQ(other.get_num_arcs(r), 0u);
1440 EXPECT_EQ(other.get_num_arcs(s), 0u);
1441}
1442
1443// =============================================================================
1444// Clear Graph Tests
1445// =============================================================================
1446
1448{
1449 clear_graph(g);
1450 EXPECT_EQ(g.get_num_nodes(), 0);
1451 EXPECT_EQ(g.get_num_arcs(), 0);
1452}
1453
1454// =============================================================================
1455// C++20 Concepts Tests
1456// =============================================================================
1457
1458// Compile-time verification that iterators satisfy concepts
1460 "Node_Iterator must satisfy BasicGraphIterator");
1462 "Arc_Iterator must satisfy BasicGraphIterator");
1464 "Node_Iterator must satisfy GraphNodeIterator");
1466 "Arc_Iterator must satisfy GraphArcIterator");
1467
1468// Also verify for digraph
1470 "Digraph::Node_Iterator must satisfy BasicGraphIterator");
1472 "Digraph::Arc_Iterator must satisfy BasicGraphIterator");
1473
1474TEST(ConceptsTest, NodeIteratorSatisfiesConcept)
1475{
1476 Graph g;
1477 g.insert_node(1);
1478 g.insert_node(2);
1479
1480 auto it = g.get_node_it();
1481
1482 // Verify the interface works as expected by the concept
1483 EXPECT_TRUE(it.has_curr());
1484 EXPECT_NE(it.get_curr(), nullptr);
1485 it.next();
1486 EXPECT_TRUE(it.has_curr());
1487 it.next();
1488 EXPECT_FALSE(it.has_curr());
1489}
1490
1491TEST(ConceptsTest, ArcIteratorSatisfiesConcept)
1492{
1493 Graph g;
1494 auto n1 = g.insert_node(1);
1495 auto n2 = g.insert_node(2);
1496 g.insert_arc(n1, n2, 1.0);
1497
1498 auto it = g.get_arc_it();
1499
1500 EXPECT_TRUE(it.has_curr());
1501 EXPECT_NE(it.get_curr(), nullptr);
1502 it.next();
1503 EXPECT_FALSE(it.has_curr());
1504}
1505
1506TEST(ConceptsTest, NodeArcIteratorSatisfiesConcept)
1507{
1508 Graph g;
1509 auto n1 = g.insert_node(1);
1510 auto n2 = g.insert_node(2);
1511 g.insert_arc(n1, n2, 1.0);
1512
1513 auto it = g.get_arc_it(n1);
1514
1515 EXPECT_TRUE(it.has_curr());
1516 auto* arc = it.get_curr();
1517 EXPECT_NE(arc, nullptr);
1518
1519 // Node arc iterator should provide get_tgt_node
1520 auto* tgt = it.get_tgt_node();
1521 EXPECT_EQ(tgt, n2);
1522}
1523
1524// Test that concepts work with constrained template functions
1525template <BasicGraphIterator It>
1527{
1528 size_t count = 0;
1529 for (; it.has_curr(); it.next())
1530 ++count;
1531 return count;
1532}
1533
1534TEST(ConceptsTest, ConceptConstrainedFunction)
1535{
1536 Graph g;
1537 g.insert_node(1);
1538 g.insert_node(2);
1539 g.insert_node(3);
1540
1541 auto node_it = g.get_node_it();
1542 EXPECT_EQ(count_elements(node_it), 3u);
1543}
1544
1545// =============================================================================
1546// Main
1547// =============================================================================
1548
1549int main(int argc, char **argv)
1550{
1551 InitGoogleTest(&argc, argv);
1552 return RUN_ALL_TESTS();
1553}
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Bit fields for nodes and arcs used for marking visit state during processing.
Filtered copy of graphs.
Definition tpl_graph.H:4951
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
typename BaseGraph::Node Node
Definition graph-dry.H:4380
void swap(Digraph &g) noexcept
Swap in constant time this digraph with g.
Definition graph-dry.H:4422
void next()
Advances the iterator to the next filtered element.
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
bool has_curr() const noexcept
Definition htlist.H:969
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
Node Node
The graph type.
Definition tpl_graph.H:443
Arc Arc
The node class type.
Definition tpl_graph.H:444
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
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:4395
bool has_current_node() const noexcept
Return true if the iterator has a current node.
Definition tpl_graph.H:4512
Path on a graph.
Definition tpl_graph.H:3832
bool is_cycle() const
Return true if this is a cycle; throws if path is empty.
Definition tpl_graph.H:4351
void insert(Arc *arc)
Insert an arc as the first of a path.
Definition tpl_graph.H:4195
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
Definition tpl_graph.H:4576
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
bool contains_node(Node *node) const noexcept
Return true if node belongs to the path.
Definition tpl_graph.H:4547
void insert_directed(Node *p)
Insert a node at the beginning of a directed path.
Definition tpl_graph.H:4266
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
Arc * get_last_arc() const
Return the last arc of a path; throws overflow_error if the path is empty.
Definition tpl_graph.H:4339
bool contains_arc(Arc *arc) const noexcept
Return true if arc belongs to the path.
Definition tpl_graph.H:4556
Node * remove_first_node()
Remove the first node of a path.
Definition tpl_graph.H:4376
Arc * get_first_arc() const
Return the first arc of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4332
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:4052
void append_directed(Node *p)
Append a node to a directed path.
Definition tpl_graph.H:4124
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Definition tpl_graph.H:4565
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4323
Node * remove_last_node()
Remove the last node of path.
Definition tpl_graph.H:4363
TestDigraph dg
vector< TestDigraph::Node * > nodes
vector< TestDigraph::Arc * > arcs
void SetUp() override
void SetUp() override
vector< TestDigraph::Node * > nodes
vector< TestDigraph::Arc * > arcs
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
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
void SetUp() override
Arc * emplace_arc(Node *src, Node *tgt, Args &&... args)
Insert a new arc in the graph by constructing its associated data in-place with the given args.
Definition graph-dry.H:1534
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
void set_digraph(bool val)
Temporal indication for preventing to other algorithms that an graph must be treated as a directed gr...
Definition graph-dry.H:972
Node * emplace_node(Args &&... args)
Insert a new node in the graph by constructing it in-place with the given args.
Definition graph-dry.H:1452
Bit_Fields & get_control_bits(Node *node) const noexcept
Return a reference to control fields of node
Definition graph-dry.H:1081
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
vector< Graph::Node * > nodes
void SetUp() override
vector< Graph::Node * > nodes
void SetUp() override
vector< Graph::Arc * > arcs
void SetUp() override
void SetUp() override
static constexpr size_t NUM_ARCS
static constexpr size_t NUM_NODES
vector< Graph::Arc * > arcs
vector< Graph::Node * > nodes
void SetUp() override
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
#define TEST(name)
static mt19937 rng
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Arc filtered searching given two nodes.
Definition tpl_graph.H:3574
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
GT::Arc * search_directed_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept(nothrow_arc_filter_v< GT, SA >)
Searching of directed arc linking two nodes.
Definition tpl_graph.H:3603
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
#define NODE_BITS(p)
Get the control bits of a node.
Path< GT > find_path_depth_first(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Depth-first search of a path between two nodes.
Definition tpl_graph.H:4695
@ Depth_First
Definition aleph-graph.H:73
@ Breadth_First
Definition aleph-graph.H:74
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
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.
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:126
Iterator on all arcs of a graph.
Definition tpl_graph.H:941
bool operator()(Graph::Node *n) const noexcept
bool operator()(Graph::Arc *a) const noexcept
Dnode< int > Test
Definition testDnode.C:42
static int * k
Gsl_Compatible_Rng r
Array-based graph implementation.
Generic graph and digraph implementations.
::testing::Types< List_Graph< Graph_Node< int >, Graph_Arc< int > >, List_SGraph< Graph_Snode< int >, Graph_Sarc< int > >, Array_Graph< Graph_Anode< int >, Graph_Aarc< int > > > Undirected_Graph_Kinds
TYPED_TEST(DirectedFlag, GoesWithTheArcs)
TEST_F(GraphBasicTest, DefaultConstructorCreatesEmptyGraph)
size_t count_elements(It it)
TYPED_TEST_SUITE(DirectedFlag, Undirected_Graph_Kinds)
Simple graph implementation with adjacency lists.