Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_agraph_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_agraph.H>
40#include <random>
41#include <string>
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_Anode Tests
57// =============================================================================
58
59TEST(GraphAnode, DefaultConstruction)
60{
62 EXPECT_EQ(node.num_arcs, 0u);
63 EXPECT_EQ(node.arcs_dim, 0u);
64 EXPECT_EQ(node.arc_array, nullptr);
65}
66
67TEST(GraphAnode, ConstructionWithInfo)
68{
69 Graph_Anode<int> node(42);
70 EXPECT_EQ(node.get_info(), 42);
71 EXPECT_EQ(node.num_arcs, 0u);
72 EXPECT_GT(node.arcs_dim, 0u); // Should have default capacity
73}
74
75TEST(GraphAnode, CopyConstruction)
76{
77 Graph_Anode<int> node1(42);
78 Graph_Anode<int> node2(node1);
79 EXPECT_EQ(node2.get_info(), 42);
80 EXPECT_EQ(node2.num_arcs, 0u);
81}
82
83TEST(GraphAnode, MoveConstruction)
84{
85 Graph_Anode<std::string> node1(std::string("test"));
86 Graph_Anode<std::string> node2(std::move(node1));
87 EXPECT_EQ(node2.get_info(), "test");
88}
89
90// =============================================================================
91// Graph_Aarc Tests
92// =============================================================================
93
94TEST(GraphAarc, DefaultConstruction)
95{
97 EXPECT_EQ(arc.get_info(), 0); // Default int value
98}
99
100TEST(GraphAarc, ConstructionWithInfo)
101{
102 Graph_Aarc<int> arc(100);
103 EXPECT_EQ(arc.get_info(), 100);
104}
105
106TEST(GraphAarc, CopyConstruction)
107{
108 Graph_Aarc<std::string> arc1(std::string("edge"));
109 Graph_Aarc<std::string> arc2(arc1);
110 EXPECT_EQ(arc2.get_info(), "edge");
111}
112
113// =============================================================================
114// Array_Graph Basic Operations
115// =============================================================================
116
117TEST(ArrayGraph, DefaultConstruction)
118{
119 IntGraph g;
120 EXPECT_EQ(g.vsize(), 0u);
121 EXPECT_EQ(g.esize(), 0u);
122 EXPECT_FALSE(g.is_digraph());
123}
124
125TEST(ArrayGraph, InsertNode)
126{
127 IntGraph g;
128 auto n1 = g.insert_node(1);
129 auto n2 = g.insert_node(2);
130 auto n3 = g.insert_node(3);
131
132 EXPECT_EQ(g.vsize(), 3u);
133 EXPECT_EQ(n1->get_info(), 1);
134 EXPECT_EQ(n2->get_info(), 2);
135 EXPECT_EQ(n3->get_info(), 3);
136}
137
138TEST(ArrayGraph, InsertArc)
139{
140 IntGraph g;
141 auto n1 = g.insert_node(1);
142 auto n2 = g.insert_node(2);
143
144 auto arc = g.insert_arc(n1, n2, 10);
145
146 EXPECT_EQ(g.esize(), 1u);
147 EXPECT_EQ(arc->get_info(), 10);
148 EXPECT_EQ(g.get_src_node(arc), n1);
149 EXPECT_EQ(g.get_tgt_node(arc), n2);
150}
151
152TEST(ArrayGraph, UndirectedArcSymmetry)
153{
154 IntGraph g;
155 auto n1 = g.insert_node(1);
156 auto n2 = g.insert_node(2);
157
158 g.insert_arc(n1, n2, 10);
159
160 // In undirected graph, arc appears in both nodes' adjacency lists
161 EXPECT_EQ(n1->num_arcs, 1u);
162 EXPECT_EQ(n2->num_arcs, 1u);
163}
164
165TEST(ArrayGraph, RemoveArc)
166{
167 IntGraph g;
168 auto n1 = g.insert_node(1);
169 auto n2 = g.insert_node(2);
170 auto arc = g.insert_arc(n1, n2, 10);
171
172 EXPECT_EQ(g.esize(), 1u);
173 g.remove_arc(arc);
174 EXPECT_EQ(g.esize(), 0u);
175 EXPECT_EQ(n1->num_arcs, 0u);
176 EXPECT_EQ(n2->num_arcs, 0u);
177}
178
179TEST(ArrayGraph, RemoveNode)
180{
181 IntGraph g;
182 auto n1 = g.insert_node(1);
183 auto n2 = g.insert_node(2);
184 auto n3 = g.insert_node(3);
185
186 g.insert_arc(n1, n2, 12);
187 g.insert_arc(n2, n3, 23);
188
189 EXPECT_EQ(g.vsize(), 3u);
190 EXPECT_EQ(g.esize(), 2u);
191
192 g.remove_node(n2);
193
194 EXPECT_EQ(g.vsize(), 2u);
195 EXPECT_EQ(g.esize(), 0u); // All arcs connected to n2 removed
196}
197
198TEST(ArrayGraph, MultipleArcs)
199{
200 IntGraph g;
201 auto n1 = g.insert_node(1);
202 auto n2 = g.insert_node(2);
203 auto n3 = g.insert_node(3);
204 auto n4 = g.insert_node(4);
205
206 g.insert_arc(n1, n2, 12);
207 g.insert_arc(n1, n3, 13);
208 g.insert_arc(n1, n4, 14);
209 g.insert_arc(n2, n3, 23);
210 g.insert_arc(n3, n4, 34);
211
212 EXPECT_EQ(g.esize(), 5u);
213 EXPECT_EQ(n1->num_arcs, 3u); // n1 connected to n2, n3, n4
214}
215
216// =============================================================================
217// Array_Digraph Tests
218// =============================================================================
219
220TEST(ArrayDigraph, IsDirected)
221{
222 IntDigraph g;
223 EXPECT_TRUE(g.is_digraph());
224}
225
226TEST(ArrayDigraph, DirectedArcAsymmetry)
227{
228 IntDigraph g;
229 auto n1 = g.insert_node(1);
230 auto n2 = g.insert_node(2);
231
232 g.insert_arc(n1, n2, 10);
233
234 // In directed graph, arc only appears in source node's adjacency list
235 EXPECT_EQ(n1->num_arcs, 1u);
236 EXPECT_EQ(n2->num_arcs, 0u);
237}
238
239TEST(ArrayDigraph, BidirectionalArcs)
240{
241 IntDigraph g;
242 auto n1 = g.insert_node(1);
243 auto n2 = g.insert_node(2);
244
245 g.insert_arc(n1, n2, 12);
246 g.insert_arc(n2, n1, 21);
247
248 EXPECT_EQ(g.esize(), 2u);
249 EXPECT_EQ(n1->num_arcs, 1u);
250 EXPECT_EQ(n2->num_arcs, 1u);
251}
252
253// =============================================================================
254// Iterator Tests
255// =============================================================================
256
257TEST(ArrayGraph, NodeIterator)
258{
259 IntGraph g;
260 g.insert_node(1);
261 g.insert_node(2);
262 g.insert_node(3);
263
264 int count = 0;
265 int sum = 0;
266 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
267 {
268 sum += it.get_curr()->get_info();
269 ++count;
270 }
271
272 EXPECT_EQ(count, 3);
273 EXPECT_EQ(sum, 6); // 1 + 2 + 3
274}
275
276TEST(ArrayGraph, ArcIterator)
277{
278 IntGraph g;
279 auto n1 = g.insert_node(1);
280 auto n2 = g.insert_node(2);
281 auto n3 = g.insert_node(3);
282
283 g.insert_arc(n1, n2, 10);
284 g.insert_arc(n2, n3, 20);
285
286 int count = 0;
287 int sum = 0;
288 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
289 {
290 sum += it.get_curr()->get_info();
291 ++count;
292 }
293
294 EXPECT_EQ(count, 2);
295 EXPECT_EQ(sum, 30); // 10 + 20
296}
297
299{
300 IntGraph g;
301 auto n1 = g.insert_node(1);
302 auto n2 = g.insert_node(2);
303 auto n3 = g.insert_node(3);
304 auto n4 = g.insert_node(4);
305
306 g.insert_arc(n1, n2, 12);
307 g.insert_arc(n1, n3, 13);
308 g.insert_arc(n1, n4, 14);
309
310 int count = 0;
311 int sum = 0;
312 for (IntGraph::Node_Arc_Iterator it(n1); it.has_curr(); it.next())
313 {
314 sum += it.get_curr()->get_info();
315 ++count;
316 }
317
318 EXPECT_EQ(count, 3);
319 EXPECT_EQ(sum, 39); // 12 + 13 + 14
320}
321
322// The iterator over the arcs of a node keeps only what it needs; it does
323// what the Array_Iterator over the array of the arcs, from which it was
324// derived, did, also where that one threw
325TEST(ArrayGraph, NodeArcIteratorDoesWhatTheArrayIteratorDid)
326{
327 IntGraph g;
328 auto hub = g.insert_node(0);
329 for (int k = 1; k <= 5; ++k)
330 g.insert_arc(hub, g.insert_node(k), 10 * k);
331 auto leaf = g.insert_node(6);
332 g.insert_arc(leaf, hub, 60);
333 auto lonely = g.insert_node(7);
334
335 std::mt19937 gen(5);
336 for (auto p : {hub, leaf, lonely})
337 {
338 const long n = long(p->num_arcs);
339 IntGraph::Node_Arc_Iterator it(p);
340 Array_Iterator<void *> ref(no_exception_ctor, p->arc_array, p->arcs_dim,
341 p->num_arcs);
342 EXPECT_EQ(it.get_base(), p->arc_array);
343 EXPECT_EQ(std::as_const(it).get_base(), p->arc_array);
344
345 // what get_curr() gives or throws, as a number
346 auto curr_of = [](auto & i) -> long
347 {
348 try
349 {
350 return long(static_cast<IntGraph::Arc *>(i.get_curr())->get_info());
351 }
352 catch (const std::underflow_error &)
353 {
354 return -1;
355 }
356 catch (const std::overflow_error &)
357 {
358 return -2;
359 }
360 };
361
362 // what an operation that can throw does, as a number
363 auto run = [](auto op) -> int
364 {
365 try
366 {
367 op();
368 return 0;
369 }
370 catch (const std::underflow_error &)
371 {
372 return 1;
373 }
374 catch (const std::overflow_error &)
375 {
376 return 2;
377 }
378 };
379
380 for (int step = 0; step < 400; ++step)
381 {
382 const int what = int(gen() % 9);
383 const long new_pos = long(gen() % (n + 3)) - 1; // from -1 to n + 1
384 switch (what)
385 {
386 case 0:
387 EXPECT_EQ(run([&] { it.next(); }), run([&] { ref.next(); }));
388 break;
389 case 1:
390 EXPECT_EQ(run([&] { it.prev(); }), run([&] { ref.prev(); }));
391 break;
392 case 2:
393 if (ref.has_curr())
394 {
395 it.next_ne();
396 ref.next_ne();
397 }
398 break;
399 case 3:
400 if (ref.has_curr())
401 {
402 it.prev_ne();
403 ref.prev_ne();
404 }
405 break;
406 case 4:
407 it.reset();
408 ref.reset();
409 break;
410 case 5:
411 it.reset_first();
412 ref.reset_first();
413 break;
414 case 6:
415 it.reset_last();
416 ref.reset_last();
417 break;
418 case 7:
419 it.end();
420 ref.end();
421 break;
422 default:
423 it.set_pos(new_pos);
424 ref.set_pos(new_pos);
425 break;
426 }
427
428 ASSERT_EQ(it.has_curr(), ref.has_curr()) << "step " << step;
429 ASSERT_EQ(it.is_last(), ref.is_last()) << "step " << step;
430 ASSERT_EQ(it.get_pos(), ref.get_pos()) << "step " << step;
431 ASSERT_EQ(curr_of(it), curr_of(ref)) << "step " << step;
432 if (ref.has_curr())
433 {
434 ASSERT_EQ(it.get_curr_ne(), ref.get_curr_ne());
435 ASSERT_EQ(it.get_current_arc_ne(), ref.get_curr_ne());
436 ASSERT_EQ(it.get_tgt_node(),
437 g.get_connected_node(it.get_current_arc(), p));
438 ASSERT_EQ(it.get_tgt_node_ne(), it.get_tgt_node());
439 }
440 }
441 }
442}
443
444// Filter_Iterator goes over the arcs of a node backwards, too
445TEST(ArrayGraph, FilteredNodeArcIteratorGoesBothWays)
446{
447 IntGraph g;
448 auto hub = g.insert_node(0);
449 for (int k = 1; k <= 6; ++k)
450 g.insert_arc(hub, g.insert_node(k), 10 * k);
451
452 struct Even_Tens
453 {
454 bool operator () (IntGraph::Arc * a) const noexcept
455 {
456 return a->get_info() % 20 == 0;
457 }
458 };
459
460 std::vector<int> forward, backward;
462 for (; it.has_curr(); it.next())
463 forward.push_back(it.get_curr()->get_info());
464 for (it.reset_last(); it.has_curr(); it.prev())
465 backward.push_back(it.get_curr()->get_info());
466
467 EXPECT_EQ(forward, (std::vector<int>{20, 40, 60}));
468 EXPECT_EQ(backward, (std::vector<int>{60, 40, 20}));
469}
470
471// =============================================================================
472// Copy and Move Semantics
473// =============================================================================
474
475TEST(ArrayGraph, CopyConstruction)
476{
477 IntGraph g1;
478 auto n1 = g1.insert_node(1);
479 auto n2 = g1.insert_node(2);
480 g1.insert_arc(n1, n2, 10);
481
482 IntGraph g2(g1);
483
484 EXPECT_EQ(g2.vsize(), 2u);
485 EXPECT_EQ(g2.esize(), 1u);
486}
487
488TEST(ArrayGraph, MoveConstruction)
489{
490 IntGraph g1;
491 g1.insert_node(1);
492 g1.insert_node(2);
493
494 IntGraph g2(std::move(g1));
495
496 EXPECT_EQ(g2.vsize(), 2u);
497 EXPECT_EQ(g1.vsize(), 0u); // Moved-from state
498}
499
500TEST(ArrayGraph, CopyAssignment)
501{
502 IntGraph g1;
503 auto n1 = g1.insert_node(1);
504 auto n2 = g1.insert_node(2);
505 g1.insert_arc(n1, n2, 10);
506
507 IntGraph g2;
508 g2 = g1;
509
510 EXPECT_EQ(g2.vsize(), 2u);
511 EXPECT_EQ(g2.esize(), 1u);
512}
513
514TEST(ArrayGraph, MoveAssignment)
515{
516 IntGraph g1;
517 g1.insert_node(1);
518 g1.insert_node(2);
519
520 IntGraph g2;
521 g2 = std::move(g1);
522
523 EXPECT_EQ(g2.vsize(), 2u);
524 EXPECT_EQ(g1.vsize(), 0u);
525}
526
527TEST(ArrayGraph, SelfAssignment)
528{
529 IntGraph g;
530 auto n1 = g.insert_node(1);
531 auto n2 = g.insert_node(2);
532 g.insert_arc(n1, n2, 10);
533
534 size_t orig_nodes = g.vsize();
535 size_t orig_arcs = g.esize();
536
537 g = g; // Self-assignment should be safe
538
539 EXPECT_EQ(g.vsize(), orig_nodes);
540 EXPECT_EQ(g.esize(), orig_arcs);
541}
542
543TEST(ArrayGraph, DeepCopyVerification)
544{
545 IntGraph g;
546 auto n1 = g.insert_node(1);
547 auto n2 = g.insert_node(2);
548 g.insert_arc(n1, n2, 10);
549
550 IntGraph copy(g);
551
552 EXPECT_EQ(copy.vsize(), g.vsize());
553 EXPECT_EQ(copy.esize(), g.esize());
554
555 // Verify deep copy: modifying copy doesn't affect original
556 copy.insert_node(999);
557 EXPECT_NE(copy.vsize(), g.vsize());
558}
559
560TEST(ArrayGraph, CopyToNonEmptyGraph)
561{
562 IntGraph g;
563 auto n1 = g.insert_node(1);
564 auto n2 = g.insert_node(2);
565 g.insert_arc(n1, n2, 10);
566
567 // Create a non-empty target graph
568 IntGraph target;
569 for (int i = 0; i < 10; ++i)
570 target.insert_node(i * 100);
571
572 size_t orig_nodes = g.vsize();
573 size_t orig_arcs = g.esize();
574
575 // Copy should replace contents, not append
576 target = g;
577
578 EXPECT_EQ(target.vsize(), orig_nodes);
579 EXPECT_EQ(target.esize(), orig_arcs);
580}
581
582// =============================================================================
583// Sorting Tests
584// =============================================================================
585
586TEST(ArrayGraph, SortNodes)
587{
588 IntGraph g;
589 g.insert_node(3);
590 g.insert_node(1);
591 g.insert_node(2);
592
593 g.sort_nodes([](auto *a, auto *b) {
594 return a->get_info() < b->get_info();
595 });
596
597 std::vector<int> values;
598 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
599 values.push_back(it.get_curr()->get_info());
600
601 EXPECT_EQ(values, (std::vector<int>{1, 2, 3}));
602}
603
604TEST(ArrayGraph, SortArcs)
605{
606 IntGraph g;
607 auto n1 = g.insert_node(1);
608 auto n2 = g.insert_node(2);
609
610 g.insert_arc(n1, n2, 30);
611 g.insert_arc(n1, n2, 10);
612 g.insert_arc(n1, n2, 20);
613
614 g.sort_arcs([](auto *a, auto *b) {
615 return a->get_info() < b->get_info();
616 });
617
618 std::vector<int> values;
619 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
620 values.push_back(it.get_curr()->get_info());
621
622 EXPECT_EQ(values, (std::vector<int>{10, 20, 30}));
623}
624
625// =============================================================================
626// Stress Tests
627// =============================================================================
628
629TEST(ArrayGraph, LargeGraph)
630{
631 IntGraph g;
632 constexpr int N = 100;
633
634 // Insert N nodes
635 std::vector<IntGraph::Node*> nodes;
636 for (int i = 0; i < N; ++i)
637 nodes.push_back(g.insert_node(i));
638
639 EXPECT_EQ(g.vsize(), static_cast<size_t>(N));
640
641 // Create a complete graph (N*(N-1)/2 edges)
642 for (int i = 0; i < N; ++i)
643 for (int j = i + 1; j < N; ++j)
644 g.insert_arc(nodes[i], nodes[j], i * N + j);
645
646 EXPECT_EQ(g.esize(), static_cast<size_t>(N * (N - 1) / 2));
647}
648
649TEST(ArrayDigraph, LargeDigraph)
650{
651 IntDigraph g;
652 constexpr int N = 50;
653
654 std::vector<IntDigraph::Node*> nodes;
655 for (int i = 0; i < N; ++i)
656 nodes.push_back(g.insert_node(i));
657
658 // Create complete digraph (N*(N-1) edges)
659 for (int i = 0; i < N; ++i)
660 for (int j = 0; j < N; ++j)
661 if (i != j)
662 g.insert_arc(nodes[i], nodes[j], i * N + j);
663
664 EXPECT_EQ(g.esize(), static_cast<size_t>(N * (N - 1)));
665}
666
667// =============================================================================
668// Edge Cases
669// =============================================================================
670
671TEST(ArrayGraph, SelfLoop)
672{
673 IntGraph g;
674 auto n1 = g.insert_node(1);
675
676 auto arc = g.insert_arc(n1, n1, 11);
677
678 EXPECT_EQ(g.esize(), 1u);
679 EXPECT_EQ(n1->num_arcs, 1u); // Self-loop counted once
680 EXPECT_EQ(g.get_src_node(arc), n1);
681 EXPECT_EQ(g.get_tgt_node(arc), n1);
682}
683
684TEST(ArrayGraph, DisconnectReconnectArc)
685{
686 IntGraph g;
687 auto n1 = g.insert_node(1);
688 auto n2 = g.insert_node(2);
689
690 auto arc = g.insert_arc(n1, n2, 10);
691 EXPECT_EQ(g.esize(), 1u);
692
693 g.disconnect_arc(arc);
694 EXPECT_EQ(g.esize(), 0u);
695 EXPECT_EQ(n1->num_arcs, 0u);
696
697 g.connect_arc(arc);
698 EXPECT_EQ(g.esize(), 1u);
699 EXPECT_EQ(n1->num_arcs, 1u);
700
701 // Clean up the arc we disconnected/reconnected
702 g.remove_arc(arc);
703}
704
705TEST(ArrayGraph, EmptyGraphIterators)
706{
707 IntGraph g;
708
709 int node_count = 0;
710 for (IntGraph::Node_Iterator it(g); it.has_curr(); it.next())
711 ++node_count;
712 EXPECT_EQ(node_count, 0);
713
714 int arc_count = 0;
715 for (IntGraph::Arc_Iterator it(g); it.has_curr(); it.next())
716 ++arc_count;
717 EXPECT_EQ(arc_count, 0);
718}
719
720TEST(ArrayGraph, GetFirstNodeArc)
721{
722 IntGraph g;
723 auto n1 = g.insert_node(1);
724 auto n2 = g.insert_node(2);
725 auto arc = g.insert_arc(n1, n2, 10);
726
727 EXPECT_NE(g.get_first_node(), nullptr);
728 EXPECT_NE(g.get_first_arc(), nullptr);
729 EXPECT_EQ(g.get_first_arc(n1), arc);
730}
731
732// In an empty graph, get_first_node() and get_first_arc() gave the header of
733// the list as a node or an arc; List_Graph throws range_error
734TEST(ArrayGraph, GetFirstNodeArcOfAnEmptyGraphThrows)
735{
736 IntGraph g;
737 EXPECT_THROW(g.get_first_node(), std::range_error);
738 EXPECT_THROW(g.get_first_arc(), std::range_error);
739
740 auto n = g.insert_node(1);
741 EXPECT_EQ(g.get_first_node(), n);
742 EXPECT_THROW(g.get_first_arc(), std::range_error);
743}
744
745// compress() called realloc() with a size of 0 for a node without arcs;
746// glibc frees the array then and gives back nullptr, which compress() took
747// for a failure, so it kept the freed array, and the destructor freed it again
748TEST(ArrayGraph, CompressesNodesWithoutArcs)
749{
750 {
751 IntGraph g;
752 auto a = g.insert_node(1);
753 auto b = g.insert_node(2);
754 auto c = g.insert_node(3); // without arcs
755 g.insert_arc(a, b, 10);
756 g.compress();
757 EXPECT_EQ(g.get_num_arcs(c), 0u);
758 EXPECT_EQ(g.get_num_arcs(a), 1u);
759
760 auto ca = g.insert_arc(c, a, 20); // c has room again
761 EXPECT_EQ(g.get_first_arc(c), ca);
762 g.compress();
763 EXPECT_EQ(g.get_num_arcs(a), 2u);
764 } // the destructor frees each array once (ASan checks it)
765}
766
767// The removal of the arcs of a node halves its array down to none, and the
768// node takes arcs again
769TEST(ArrayGraph, RemovingEveryArcOfANodeAndInsertingAgain)
770{
771 IntDigraph g;
772 auto hub = g.insert_node(0);
773 std::vector<IntDigraph::Arc *> arcs;
774 for (int i = 1; i <= 40; ++i)
775 arcs.push_back(g.insert_arc(hub, g.insert_node(i), i));
776 for (auto a : arcs)
777 g.remove_arc(a);
778 EXPECT_EQ(g.get_num_arcs(hub), 0u);
779 g.compress();
780
781 auto n = g.get_first_node();
782 auto a = g.insert_arc(hub, n, 1);
783 EXPECT_EQ(g.get_num_arcs(hub), 1u);
784 EXPECT_EQ(g.get_first_arc(hub), a);
785}
786
787// =============================================================================
788// String Data Type Tests
789// =============================================================================
790
791TEST(ArrayGraph, StringNodeInfo)
792{
793 StringGraph g;
794 auto n1 = g.insert_node(std::string("node1"));
795 auto n2 = g.insert_node(std::string("node2"));
796
797 g.insert_arc(n1, n2, std::string("edge"));
798
799 EXPECT_EQ(n1->get_info(), "node1");
800 EXPECT_EQ(n2->get_info(), "node2");
801}
802
803// =============================================================================
804// Digraph Template Wrapper Tests (Array_Digraph)
805// =============================================================================
806
807// These tests validate the generic Digraph<BaseGraph> template wrapper
808// works correctly with Array_Graph as the base.
809
810TEST(DigraphWrapper, ArrayDigraphInheritsFromBaseGraph)
811{
812 IntDigraph dg;
813
814 auto n1 = dg.insert_node(1);
815 auto n2 = dg.insert_node(2);
816 auto arc = dg.insert_arc(n1, n2, 100);
817
818 EXPECT_EQ(dg.vsize(), 2u);
819 EXPECT_EQ(dg.esize(), 1u);
820 EXPECT_TRUE(dg.is_digraph());
821 EXPECT_EQ(dg.get_src_node(arc), n1);
822 EXPECT_EQ(dg.get_tgt_node(arc), n2);
823}
824
825TEST(DigraphWrapper, ArrayDigraphCopyPreservesDirectedFlag)
826{
827 IntDigraph dg1;
828 dg1.insert_node(1);
829 dg1.insert_node(2);
830
831 IntDigraph dg2(dg1);
832 EXPECT_TRUE(dg2.is_digraph());
833 EXPECT_EQ(dg2.vsize(), 2u);
834}
835
836TEST(DigraphWrapper, ArrayDigraphMovePreservesDirectedFlag)
837{
838 IntDigraph dg1;
839 dg1.insert_node(1);
840 dg1.insert_node(2);
841
842 IntDigraph dg2(std::move(dg1));
843 EXPECT_TRUE(dg2.is_digraph());
844 EXPECT_EQ(dg2.vsize(), 2u);
845}
846
847TEST(DigraphWrapper, ArrayDigraphAssignmentPreservesDirectedFlag)
848{
849 IntDigraph dg1;
850 dg1.insert_node(1);
851
852 IntDigraph dg2;
853 dg2.insert_node(10);
854
855 dg2 = dg1;
856 EXPECT_TRUE(dg2.is_digraph());
857 EXPECT_EQ(dg2.vsize(), 1u);
858}
859
860TEST(DigraphWrapper, ArrayDigraphMoveAssignment)
861{
862 IntDigraph dg1;
863 dg1.insert_node(1);
864
865 IntDigraph dg2;
866 dg2 = std::move(dg1);
867 EXPECT_TRUE(dg2.is_digraph());
868 EXPECT_EQ(dg2.vsize(), 1u);
869}
870
871// =============================================================================
872// C++20 Concepts Tests
873// =============================================================================
874
875// Compile-time verification that Array_Graph iterators satisfy concepts
877 "Array_Graph::Node_Iterator must satisfy BasicGraphIterator");
879 "Array_Graph::Arc_Iterator must satisfy BasicGraphIterator");
881 "Array_Graph::Node_Iterator must satisfy GraphNodeIterator");
883 "Array_Graph::Arc_Iterator must satisfy GraphArcIterator");
884
885TEST(ConceptsTest, ArrayGraphIteratorsSatisfyConcepts)
886{
887 IntGraph g;
888 auto n1 = g.insert_node(1);
889 auto n2 = g.insert_node(2);
890 g.insert_arc(n1, n2, 10);
891
892 // Verify node iterator works per concept
893 auto nit = g.get_node_it();
894 EXPECT_TRUE(nit.has_curr());
895 EXPECT_NE(nit.get_curr(), nullptr);
896
897 // Verify arc iterator works per concept
898 auto ait = g.get_arc_it();
899 EXPECT_TRUE(ait.has_curr());
900 EXPECT_NE(ait.get_curr(), nullptr);
901}
@ no_exception_ctor
Definition ahDefs.H:74
Iterator wrapper for C++ raw arrays.
Definition array_it.H:85
void next_ne() noexcept
Advance to the next item without bounds checking.
Definition array_it.H:280
void reset_last() noexcept
Reset the iterator to the last item.
Definition array_it.H:330
void reset() noexcept
Reset the iterator to the first item.
Definition array_it.H:317
T & get_curr_ne() const noexcept
Get the current item without bounds checking.
Definition array_it.H:258
void set_pos(const long new_pos) noexcept
Move the iterator to the logical position new_pos.
Definition array_it.H:350
void end() noexcept
Put the iterator at the end (past the last item).
Definition array_it.H:363
void prev()
Move to the previous item with bounds checking.
Definition array_it.H:310
void prev_ne() noexcept
Move to the previous item without bounds checking.
Definition array_it.H:300
void next()
Advance to the next item with bounds checking.
Definition array_it.H:290
void reset_first() noexcept
Reset the iterator to the first item (alias for reset()).
Definition array_it.H:324
bool has_curr() const noexcept
Check if there is a current valid item.
Definition array_it.H:231
bool is_last() const noexcept
Check if positioned at the last item.
Definition array_it.H:241
constexpr long get_pos() const noexcept
Get the current position index.
Definition array_it.H:249
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
void reset_last()
Resets the iterator to the last filtered element.
void next()
Advances the iterator to the next filtered element.
void prev()
Moves the iterator backward to the previous filtered element.
Node of Array_Graph
Definition tpl_agraph.H:64
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
virtual Arc * connect_arc(Arc *arc) noexcept
Connect a previously disconnected arc to the graph.
Definition tpl_graph.H:757
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
virtual void disconnect_arc(Arc *arc) noexcept
Disconnect an arc from graph.
Definition tpl_graph.H:723
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
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
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 sort_arcs(Compare &cmp) noexcept(nothrow_cmp_v< Compare, Arc >)
Sort all the arcs of the graph according to a specific criteria.
Definition graph-dry.H:4261
void sort_nodes(Compare &cmp) noexcept(nothrow_cmp_v< Compare, Node >)
Sort all the nodes of the graph according to a specific criteria.
Definition graph-dry.H:4225
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
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:1058
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
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
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
Main namespace for Aleph-w library functions.
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.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
static int * k
Array-based graph implementation.