Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_matgraph_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
38#include <gtest/gtest.h>
39#include <tpl_matgraph.H>
40#include <tpl_graph.H>
41#include <algorithm>
42#include <limits>
43#include <utility>
44#include <vector>
45
46using namespace Aleph;
47
48// =============================================================================
49// Type Aliases
50// =============================================================================
51
52// Graph with int node attributes and int arc attributes
56
57// =============================================================================
58// Helper Functions
59// =============================================================================
60
61class MatGraphTestBase : public ::testing::Test
62{
63protected:
65 IntGraph::Node* n0 = nullptr;
66 IntGraph::Node* n1 = nullptr;
67 IntGraph::Node* n2 = nullptr;
68 IntGraph::Node* n3 = nullptr;
69
70 void SetUp() override
71 {
72 // Create a simple graph:
73 // 0 -- 1 -- 2
74 // |
75 // 3
76 n0 = g.insert_node(0);
77 n1 = g.insert_node(1);
78 n2 = g.insert_node(2);
79 n3 = g.insert_node(3);
80
81 g.insert_arc(n0, n1, 10);
82 g.insert_arc(n1, n2, 20);
83 g.insert_arc(n1, n3, 30);
84 }
85};
86
87class DigraphTestBase : public ::testing::Test
88{
89protected:
91 IntDigraph::Node* n0 = nullptr;
92 IntDigraph::Node* n1 = nullptr;
93 IntDigraph::Node* n2 = nullptr;
94
95 void SetUp() override
96 {
97 // Create a simple digraph:
98 // 0 -> 1 -> 2
99 // ^ |
100 // |____|
101 n0 = g.insert_node(0);
102 n1 = g.insert_node(1);
103 n2 = g.insert_node(2);
104
105 g.insert_arc(n0, n1, 10);
106 g.insert_arc(n1, n2, 20);
107 g.insert_arc(n2, n1, 30);
108 }
109};
110
111// =============================================================================
112// matgraph_detail Helper Tests
113// =============================================================================
114
115TEST(MatgraphDetailTest, IndexArray)
116{
117 // Row-major indexing: i + j*n
118 EXPECT_EQ(matgraph_detail::index_array(0L, 0L, 4L), 0L);
119 EXPECT_EQ(matgraph_detail::index_array(1L, 0L, 4L), 1L);
120 EXPECT_EQ(matgraph_detail::index_array(0L, 1L, 4L), 4L);
121 EXPECT_EQ(matgraph_detail::index_array(3L, 3L, 4L), 15L);
122}
123
124TEST(MatgraphDetailTest, CheckedIndexArrayValid)
125{
126 EXPECT_NO_THROW((void)matgraph_detail::checked_index_array(0L, 0L, 4L));
127 EXPECT_NO_THROW((void)matgraph_detail::checked_index_array(3L, 3L, 4L));
128}
129
130TEST(MatgraphDetailTest, CheckedIndexArrayOutOfRange)
131{
132 EXPECT_THROW((void)matgraph_detail::checked_index_array(4L, 0L, 4L), std::out_of_range);
133 EXPECT_THROW((void)matgraph_detail::checked_index_array(0L, 4L, 4L), std::out_of_range);
134 EXPECT_THROW((void)matgraph_detail::checked_index_array(-1L, 0L, 4L), std::out_of_range);
135}
136
137// =============================================================================
138// Map_Matrix_Graph Tests
139// =============================================================================
140
142{
143};
144
146{
148 EXPECT_EQ(mat.get_num_nodes(), 4u);
149}
150
152{
154
155 // Nodes are sorted by pointer, so order may vary
156 EXPECT_NE(mat(0), nullptr);
157 EXPECT_NE(mat(1), nullptr);
158 EXPECT_NE(mat(2), nullptr);
159 EXPECT_NE(mat(3), nullptr);
160}
161
163{
165
166 // Get indices and verify they're valid
167 long idx0 = mat(n0);
168 long idx1 = mat(n1);
169 long idx2 = mat(n2);
170 long idx3 = mat(n3);
171
172 EXPECT_GE(idx0, 0);
173 EXPECT_LT(idx0, 4);
174 EXPECT_GE(idx1, 0);
175 EXPECT_LT(idx1, 4);
176 EXPECT_GE(idx2, 0);
177 EXPECT_LT(idx2, 4);
178 EXPECT_GE(idx3, 0);
179 EXPECT_LT(idx3, 4);
180
181 // All indices should be unique
182 std::set<long> indices{idx0, idx1, idx2, idx3};
183 EXPECT_EQ(indices.size(), 4u);
184}
185
186TEST_F(MapMatrixGraphTest, GetArcByIndices)
187{
189
190 long i0 = mat(n0);
191 long i1 = mat(n1);
192 long i2 = mat(n2);
193 long i3 = mat(n3);
194
195 // Arc between n0 and n1 should exist (undirected)
196 EXPECT_NE(mat(i0, i1), nullptr);
197 EXPECT_NE(mat(i1, i0), nullptr); // Undirected
198
199 // Arc between n1 and n2
200 EXPECT_NE(mat(i1, i2), nullptr);
201
202 // No direct arc between n0 and n2
203 EXPECT_EQ(mat(i0, i2), nullptr);
204 EXPECT_EQ(mat(i2, i0), nullptr);
205
206 // No arc between n0 and n3
207 EXPECT_EQ(mat(i0, i3), nullptr);
208}
209
211{
213
214 EXPECT_NE(mat(n0, n1), nullptr);
215 EXPECT_NE(mat(n1, n0), nullptr);
216 EXPECT_NE(mat(n1, n2), nullptr);
217 EXPECT_NE(mat(n1, n3), nullptr);
218 EXPECT_EQ(mat(n0, n2), nullptr);
219 EXPECT_EQ(mat(n0, n3), nullptr);
220}
221
223{
225 EXPECT_EQ(&mat.get_list_graph(), &g);
226}
227
228TEST_F(MapMatrixGraphTest, CopyConstructor)
229{
232
233 EXPECT_EQ(mat2.get_num_nodes(), mat1.get_num_nodes());
234 EXPECT_EQ(&mat2.get_list_graph(), &mat1.get_list_graph());
235}
236
237TEST_F(MapMatrixGraphTest, AssignmentOperator)
238{
240 IntGraph g2;
241 g2.insert_node(100);
242 g2.insert_node(200);
244
245 mat2 = mat1;
246 EXPECT_EQ(mat2.get_num_nodes(), 4u);
247}
248
249TEST_F(MapMatrixGraphTest, OutOfRangeIndex)
250{
252 EXPECT_THROW((void)mat(10), std::out_of_range);
253}
254
255// =============================================================================
256// Matrix_Graph Tests
257// =============================================================================
258
260{
261};
262
264{
265 Matrix_Graph<IntGraph> mat(g, -1);
266 EXPECT_EQ(mat.get_num_nodes(), 4u);
267 EXPECT_EQ(mat.null_value(), -1);
268}
269
270TEST_F(MatrixGraphTest, GetNodeAttribute)
271{
272 Matrix_Graph<IntGraph> mat(g, -1);
273
274 // Node attributes are stored by index
275 std::set<int> values;
276 for (size_t i = 0; i < 4; ++i)
277 values.insert(mat(static_cast<long>(i)));
278
279 // Should have node values 0, 1, 2, 3
280 EXPECT_TRUE(values.count(0));
281 EXPECT_TRUE(values.count(1));
282 EXPECT_TRUE(values.count(2));
283 EXPECT_TRUE(values.count(3));
284}
285
286TEST_F(MatrixGraphTest, GetArcAttribute)
287{
288 Matrix_Graph<IntGraph> mat(g, -1);
289
290 // Find indices of nodes
291 long idx0 = -1, idx1 = -1;
292 for (size_t i = 0; i < 4; ++i)
293 {
294 if (mat(static_cast<long>(i)) == 0) idx0 = i;
295 if (mat(static_cast<long>(i)) == 1) idx1 = i;
296 }
297
298 ASSERT_NE(idx0, -1);
299 ASSERT_NE(idx1, -1);
300
301 // Arc between nodes 0 and 1 has weight 10
302 EXPECT_EQ(mat(idx0, idx1), 10);
303 EXPECT_EQ(mat(idx1, idx0), 10); // Undirected
304}
305
306TEST_F(MatrixGraphTest, NoArcReturnsNull)
307{
308 Matrix_Graph<IntGraph> mat(g, -1);
309
310 long idx0 = -1, idx2 = -1;
311 for (size_t i = 0; i < 4; ++i)
312 {
313 if (mat(static_cast<long>(i)) == 0) idx0 = i;
314 if (mat(static_cast<long>(i)) == 2) idx2 = i;
315 }
316
317 EXPECT_EQ(mat(idx0, idx2), -1);
318}
319
321{
322 Matrix_Graph<IntGraph> mat(g, -1);
323
324 mat(0, 3) = 999;
325 EXPECT_EQ(mat(0, 3), 999);
326}
327
328TEST_F(MatrixGraphTest, CopyConstructor)
329{
330 Matrix_Graph<IntGraph> mat1(g, -1);
331 Matrix_Graph<IntGraph> mat2(mat1);
332
333 EXPECT_EQ(mat2.get_num_nodes(), mat1.get_num_nodes());
334 EXPECT_EQ(mat2.null_value(), mat1.null_value());
335}
336
337// =============================================================================
338// Ady_Mat Tests
339// =============================================================================
340
342{
343};
344
345TEST_F(AdyMatTest, ConstructorWithoutNull)
346{
348 EXPECT_EQ(mat.get_num_nodes(), 3u);
349}
350
351TEST_F(AdyMatTest, ConstructorWithNull)
352{
353 Ady_Mat<IntDigraph, double> mat(g, -1.0);
354 EXPECT_EQ(mat.get_num_nodes(), 3u);
355 EXPECT_DOUBLE_EQ(mat.null_value(), -1.0);
356}
357
358TEST_F(AdyMatTest, GetNodeByIndex)
359{
360 Ady_Mat<IntDigraph, int> mat(g, -1);
361
362 EXPECT_NE(mat(0), nullptr);
363 EXPECT_NE(mat(1), nullptr);
364 EXPECT_NE(mat(2), nullptr);
365}
366
367TEST_F(AdyMatTest, GetIndexByNode)
368{
369 Ady_Mat<IntDigraph, int> mat(g, -1);
370
371 long i0 = mat(n0);
372 long i1 = mat(n1);
373 long i2 = mat(n2);
374
375 EXPECT_GE(i0, 0);
376 EXPECT_LT(i0, 3);
377 EXPECT_GE(i1, 0);
378 EXPECT_LT(i1, 3);
379 EXPECT_GE(i2, 0);
380 EXPECT_LT(i2, 3);
381}
382
383TEST_F(AdyMatTest, AccessByIndices)
384{
385 Ady_Mat<IntDigraph, int> mat(g, -1);
386
387 mat(0L, 1L) = 100;
388 mat(1L, 2L) = 200;
389
390 EXPECT_EQ(mat(0L, 1L), 100);
391 EXPECT_EQ(mat(1L, 2L), 200);
392}
393
394TEST_F(AdyMatTest, AccessByNodes)
395{
396 Ady_Mat<IntDigraph, int> mat(g, -1);
397
398 mat(n0, n1) = 100;
399 EXPECT_EQ(mat(n0, n1), 100);
400}
401
402TEST_F(AdyMatTest, ConstAccess)
403{
404 Ady_Mat<IntDigraph, int> mat(g, -1);
405 mat(0L, 0L) = 42;
406
407 const Ady_Mat<IntDigraph, int>& cmat = mat;
408 EXPECT_EQ(cmat(0L, 0L), 42);
409}
410
411TEST_F(AdyMatTest, GetListGraph)
412{
413 Ady_Mat<IntDigraph, int> mat(g, -1);
414 EXPECT_EQ(&mat.get_list_graph(), &g);
415}
416
417TEST_F(AdyMatTest, SetNullValue)
418{
420 mat.set_null_value(-999);
421 EXPECT_EQ(mat.null_value(), -999);
422}
423
424// Operation functor for operate_all_arcs_list_graph
426{
428 IntDigraph::Arc* arc,
429 long i, long j,
430 int& entry)
431 {
432 (void)mat;
433 (void)i;
434 (void)j;
435 entry = arc->get_info();
436 }
437};
438
439TEST_F(AdyMatTest, OperateAllArcsListGraph)
440{
441 Ady_Mat<IntDigraph, int> mat(g, -1);
443
444 // Check that arc values were copied
445 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2);
446
447 EXPECT_EQ(mat(i0, i1), 10); // Arc 0->1
448 EXPECT_EQ(mat(i1, i2), 20); // Arc 1->2
449 EXPECT_EQ(mat(i2, i1), 30); // Arc 2->1
450}
451
452// Operation functor for operate_all_arcs_matrix
454{
457 long i, long j,
458 int& entry)
459 {
460 (void)mat;
461 (void)src;
462 (void)tgt;
463 entry = i * 10 + j;
464 }
465};
466
467TEST_F(AdyMatTest, OperateAllArcsMatrix)
468{
469 Ady_Mat<IntDigraph, int> mat(g, -1);
471
472 EXPECT_EQ(mat(0L, 0L), 0);
473 EXPECT_EQ(mat(0L, 1L), 1);
474 EXPECT_EQ(mat(1L, 0L), 10);
475 EXPECT_EQ(mat(2L, 2L), 22);
476}
477
478// =============================================================================
479// Ady_Mat: the arcs that operate_all_arcs_list_graph() visits
480// =============================================================================
481
482namespace
483{
484 using Node_Pair = std::pair<IntGraph::Node *, IntGraph::Node *>;
485
486 // The ends of the entries that an operation touched, in order: the
487 // functor is built anew for each arc, so that it cannot keep them
488 std::vector<Node_Pair> touched_entries;
489
490 template <class Mat>
491 struct Note_Entry
492 {
493 void operator()(Mat & mat, typename Mat::Arc * a, long i, long j, int & e)
494 {
495 e = a->get_info();
496 touched_entries.emplace_back(mat(i), mat(j));
497 }
498
499 void operator()(Mat & mat, typename Mat::Arc * a, long i, long j, int & e,
500 void *)
501 {
502 (*this)(mat, a, i, j, e);
503 }
504 };
505
506 // Hides the arcs whose info is hidden
507 struct Hide_Info_Of
508 {
509 int hidden = 0;
510 bool operator()(IntGraph::Arc * a) const { return a->get_info() != hidden; }
511 };
512
513 // The entries that each overload of operate_all_arcs_list_graph() touches,
514 // sorted: first without ptr, then with it
515 template <class Mat>
516 std::pair<std::vector<Node_Pair>, std::vector<Node_Pair>> entries_of(Mat & mat)
517 {
518 touched_entries.clear();
519 mat.template operate_all_arcs_list_graph<Note_Entry<Mat>>();
520 std::vector<Node_Pair> without_ptr = touched_entries;
521 touched_entries.clear();
522 mat.template operate_all_arcs_list_graph<Note_Entry<Mat>>(nullptr);
523 std::vector<Node_Pair> with_ptr = touched_entries;
524 std::sort(without_ptr.begin(), without_ptr.end());
525 std::sort(with_ptr.begin(), with_ptr.end());
526 return {without_ptr, with_ptr};
527 }
528
529 // The ends of the arcs of the triangle a, b, c, both ways, but those of
530 // the arc whose info is hidden
531 template <class GT>
532 std::vector<Node_Pair> both_ways(GT & g, int hidden = 0)
533 {
534 std::vector<Node_Pair> want;
535 for (Arc_Iterator<GT> it(g); it.has_curr(); it.next_ne())
536 if (auto * arc = it.get_curr_ne(); arc->get_info() != hidden)
537 {
538 want.emplace_back(g.get_src_node(arc), g.get_tgt_node(arc));
539 if (not g.is_digraph())
540 want.emplace_back(g.get_tgt_node(arc), g.get_src_node(arc));
541 }
542 std::sort(want.begin(), want.end());
543 return want;
544 }
545
546 // A triangle, whose arcs have the infos 10, 20 and 30
547 template <class GT>
548 void triangle(GT & g)
549 {
550 auto * a = g.insert_node(0);
551 auto * b = g.insert_node(1);
552 auto * c = g.insert_node(2);
553 g.insert_arc(a, b, 10);
554 g.insert_arc(b, c, 20);
555 g.insert_arc(c, a, 30);
556 }
557} // namespace
558
559// On a graph that is not directed, the overload with ptr took the target of
560// the arc for its other end: from the target it touched the diagonal
561TEST(AdyMatArcs, BothOverloadsTouchTheEntriesOfTheArcs)
562{
563 IntGraph g;
564 triangle(g);
565 Ady_Mat<IntGraph, int> mat(g, -1);
566 const auto [without_ptr, with_ptr] = entries_of(mat);
567 EXPECT_EQ(without_ptr, both_ways(g));
568 EXPECT_EQ(with_ptr, both_ways(g));
569
570 IntDigraph dg;
571 triangle(dg);
572 Ady_Mat<IntDigraph, int> dmat(dg, -1);
573 const auto [d_without_ptr, d_with_ptr] = entries_of(dmat);
574 EXPECT_EQ(d_without_ptr, both_ways(dg));
575 EXPECT_EQ(d_with_ptr, both_ways(dg));
576}
577
578// The matrix took the filter only as a type: the constructors take the
579// object, which hides here the arc of info 20, as in a Bit_Mat_Graph
580TEST(AdyMatArcs, TheFilterObjectChoosesTheArcs)
581{
582 IntGraph g;
583 triangle(g);
584 Ady_Mat<IntGraph, int, Hide_Info_Of> mat(g, -1, Hide_Info_Of{20});
585 const auto [without_ptr, with_ptr] = entries_of(mat);
586 EXPECT_EQ(without_ptr, both_ways(g, 20));
587 EXPECT_EQ(with_ptr, both_ways(g, 20));
588
589 Bit_Mat_Graph<IntGraph, Hide_Info_Of> bits(g, Hide_Info_Of{20});
590 std::vector<Node_Pair> set;
591 for (long i = 0; i < 3; ++i)
592 for (long j = 0; j < 3; ++j)
593 if (static_cast<int>(bits(i, j)) != 0)
594 set.emplace_back(bits(i), bits(j));
595 std::sort(set.begin(), set.end());
596 EXPECT_EQ(without_ptr, set);
597
598 Ady_Mat<IntGraph, int, Hide_Info_Of> without_null(g, Hide_Info_Of{30});
599 EXPECT_EQ(entries_of(without_null).first, both_ways(g, 30));
600}
601
602// The copy constructor threw, since the new matrix had no graph, and the
603// copies took none of the entries; a copy keeps the entries, the null value
604// and the filter
605TEST(AdyMatArcs, ACopyKeepsTheEntriesTheNullValueAndTheFilter)
606{
607 IntGraph g;
608 triangle(g);
609 Ady_Mat<IntGraph, int, Hide_Info_Of> mat(g, -1, Hide_Info_Of{20});
610 mat(0L, 1L) = 7;
611
613 EXPECT_EQ(std::as_const(copy)(0L, 1L), 7);
614 EXPECT_EQ(copy.null_value(), -1);
615 EXPECT_EQ(entries_of(copy).first, both_ways(g, 20));
616
617 Ady_Mat<IntGraph, int, Hide_Info_Of> assigned(g, 0, Hide_Info_Of{10});
618 assigned(0L, 1L) = 3;
619 assigned = mat;
620 EXPECT_EQ(std::as_const(assigned)(0L, 1L), std::as_const(mat)(0L, 1L));
621 EXPECT_EQ(assigned.null_value(), -1);
622 EXPECT_EQ(entries_of(assigned).first, both_ways(g, 20));
623
624 // As before, the assignment takes a matrix of the same graph
625 IntGraph h;
626 h.insert_node(0);
628 EXPECT_THROW(of_h = mat, std::domain_error);
629}
630
631// =============================================================================
632// Bit_Mat_Graph Tests
633// =============================================================================
634
636{
637};
638
639TEST_F(BitMatGraphTest, DefaultConstructor)
640{
642 EXPECT_EQ(mat.get_num_nodes(), 0u);
643 EXPECT_EQ(mat.get_list_graph(), nullptr);
644}
645
646TEST_F(BitMatGraphTest, ConstructFromGraph)
647{
649 EXPECT_EQ(mat.get_num_nodes(), 4u);
650 EXPECT_EQ(mat.get_list_graph(), &g);
651}
652
653TEST_F(BitMatGraphTest, ConstructWithDimension)
654{
656 EXPECT_EQ(mat.get_num_nodes(), 5u);
657 EXPECT_EQ(mat.get_list_graph(), nullptr);
658}
659
660TEST_F(BitMatGraphTest, GetNodeByIndex)
661{
663
664 EXPECT_NE(mat(0), nullptr);
665 EXPECT_NE(mat(1), nullptr);
666}
667
668TEST_F(BitMatGraphTest, GetIndexByNode)
669{
671
672 long i0 = mat(n0);
673 long i1 = mat(n1);
674
675 EXPECT_GE(i0, 0);
676 EXPECT_LT(i0, 4);
677 EXPECT_GE(i1, 0);
678 EXPECT_LT(i1, 4);
679 EXPECT_NE(i0, i1);
680}
681
682TEST_F(BitMatGraphTest, CheckConnectivity)
683{
685
686 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2), i3 = mat(n3);
687
688 // Arc between n0 and n1
689 EXPECT_EQ(static_cast<int>(mat(i0, i1)), 1);
690 EXPECT_EQ(static_cast<int>(mat(i1, i0)), 1);
691
692 // Arc between n1 and n2
693 EXPECT_EQ(static_cast<int>(mat(i1, i2)), 1);
694
695 // Arc between n1 and n3
696 EXPECT_EQ(static_cast<int>(mat(i1, i3)), 1);
697
698 // No arc between n0 and n2
699 EXPECT_EQ(static_cast<int>(mat(i0, i2)), 0);
700}
701
702TEST_F(BitMatGraphTest, ModifyEntries)
703{
705
706 long i0 = mat(n0), i2 = mat(n2);
707
708 EXPECT_EQ(static_cast<int>(mat(i0, i2)), 0);
709
710 mat(i0, i2) = 1;
711 EXPECT_EQ(static_cast<int>(mat(i0, i2)), 1);
712
713 mat(i0, i2) = 0;
714 EXPECT_EQ(static_cast<int>(mat(i0, i2)), 0);
715}
716
717TEST_F(BitMatGraphTest, AccessByNodes)
718{
720
721 EXPECT_EQ(static_cast<int>(mat(n0, n1)), 1);
722 EXPECT_EQ(static_cast<int>(mat(n0, n2)), 0);
723}
724
726{
728 EXPECT_EQ(mat.get_num_nodes(), 0u);
729
730 mat.set_list_graph(g);
731 EXPECT_EQ(mat.get_num_nodes(), 4u);
732}
733
734// The bits are those of the arcs of the graph alone: none is kept from the
735// graph that the matrix had, of the same order
736TEST_F(BitMatGraphTest, AnotherGraphKeepsNoBitOfTheFormer)
737{
738 IntGraph h;
739 for (int i = 0; i < 4; ++i)
740 h.insert_node(i);
741
742 auto bits_set = [] (Bit_Mat_Graph<IntGraph> & mat)
743 {
744 int count = 0;
745 for (long i = 0; i < 4; ++i)
746 for (long j = 0; j < 4; ++j)
747 count += static_cast<int>(mat(i, j));
748 return count;
749 };
750
752 ASSERT_EQ(bits_set(mat), 6); // three arcs, both ways
753 mat.set_list_graph(h);
754 EXPECT_EQ(bits_set(mat), 0);
755
756 Bit_Mat_Graph<IntGraph> assigned(g);
757 assigned = h;
758 EXPECT_EQ(bits_set(assigned), 0);
759}
760
761// Only the arcs that the filter object shows set a bit
762TEST_F(BitMatGraphTest, TheFilterObjectChoosesTheArcs)
763{
764 struct Hide_Info
765 {
766 int hidden = 0;
767 bool operator () (IntGraph::Arc * a) const { return a->get_info() != hidden; }
768 };
769
770 Bit_Mat_Graph<IntGraph, Hide_Info> mat(g, Hide_Info{20});
771 EXPECT_EQ(static_cast<int>(mat(n0, n1)), 1);
772 EXPECT_EQ(static_cast<int>(mat(n1, n2)), 0);
773 EXPECT_EQ(static_cast<int>(mat(n1, n3)), 1);
774
775 mat.set_list_graph(g, Hide_Info{10});
776 EXPECT_EQ(static_cast<int>(mat(n0, n1)), 0);
777 EXPECT_EQ(static_cast<int>(mat(n1, n2)), 1);
778}
779
780// The assignment from a graph takes the filter SA(), and the arcs that the
781// graph has now, also when the matrix is already of that graph: it kept the
782// bits of the filter given before
783TEST_F(BitMatGraphTest, AssigningItsOwnGraphTakesTheDefaultFilter)
784{
785 struct Hide_Info
786 {
787 int hidden = 0;
788 bool operator () (IntGraph::Arc * a) const { return a->get_info() != hidden; }
789 };
790
791 Bit_Mat_Graph<IntGraph, Hide_Info> mat(g, Hide_Info{20});
792 ASSERT_EQ(static_cast<int>(mat(n1, n2)), 0);
793 mat = g;
794 EXPECT_EQ(static_cast<int>(mat(n1, n2)), 1);
795
796 g.insert_arc(n2, n3, 40);
797 ASSERT_EQ(static_cast<int>(mat(n2, n3)), 0);
798 mat = g;
799 EXPECT_EQ(static_cast<int>(mat(n2, n3)), 1);
800 EXPECT_EQ(static_cast<int>(mat(n0, n1)), 1);
801}
802
803TEST_F(BitMatGraphTest, CopyConstructor)
804{
806 Bit_Mat_Graph<IntGraph> mat2(mat1);
807
808 EXPECT_EQ(mat2.get_num_nodes(), mat1.get_num_nodes());
809
810 long i0 = mat1(n0), i1 = mat1(n1);
811 EXPECT_EQ(static_cast<int>(mat2(i0, i1)), static_cast<int>(mat1(i0, i1)));
812}
813
814TEST_F(BitMatGraphTest, AssignmentOperator)
815{
818
819 mat2 = mat1;
820 EXPECT_EQ(mat2.get_num_nodes(), 4u);
821}
822
823TEST_F(BitMatGraphTest, NoGraphThrows)
824{
826
827 EXPECT_THROW((void)mat(0), std::domain_error);
828 EXPECT_THROW((void)mat(n0), std::domain_error);
829}
830
831// =============================================================================
832// Digraph Tests
833// =============================================================================
834
836{
837};
838
839TEST_F(DigraphMatrixTest, MapMatrixDirected)
840{
842
843 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2);
844
845 // 0 -> 1 exists
846 EXPECT_NE(mat(i0, i1), nullptr);
847 // 1 -> 0 does not exist (directed)
848 EXPECT_EQ(mat(i1, i0), nullptr);
849
850 // 1 -> 2 exists
851 EXPECT_NE(mat(i1, i2), nullptr);
852 // 2 -> 1 exists
853 EXPECT_NE(mat(i2, i1), nullptr);
854}
855
856TEST_F(DigraphMatrixTest, BitMatDirected)
857{
859
860 long i0 = mat(n0), i1 = mat(n1), i2 = mat(n2);
861
862 EXPECT_EQ(static_cast<int>(mat(i0, i1)), 1);
863 EXPECT_EQ(static_cast<int>(mat(i1, i0)), 0);
864 EXPECT_EQ(static_cast<int>(mat(i1, i2)), 1);
865 EXPECT_EQ(static_cast<int>(mat(i2, i1)), 1);
866}
867
868// =============================================================================
869// Edge Cases
870// =============================================================================
871
872TEST(MatGraphEdgeCases, SingleNodeGraph)
873{
874 IntGraph g;
875 g.insert_node(42);
876
878 EXPECT_EQ(mat.get_num_nodes(), 1u);
879 EXPECT_EQ(mat(0L, 0L), nullptr); // No self-loop
880
881 Ady_Mat<IntGraph, int> amat(g, -1);
882 EXPECT_EQ(amat.get_num_nodes(), 1u);
883 amat(0L, 0L) = 100;
884 EXPECT_EQ(amat(0L, 0L), 100);
885}
886
887TEST(MatGraphEdgeCases, DisconnectedGraph)
888{
889 IntGraph g;
890 auto* n0 = g.insert_node(0);
891 auto* n1 = g.insert_node(1);
892 auto* n2 = g.insert_node(2);
893 (void)n0; (void)n1; (void)n2;
894 // No arcs
895
897 EXPECT_EQ(mat.get_num_nodes(), 3u);
898
899 for (long i = 0; i < 3; ++i)
900 for (long j = 0; j < 3; ++j)
901 EXPECT_EQ(mat(i, j), nullptr);
902}
903
904TEST(MatGraphEdgeCases, CompleteGraph)
905{
906 IntGraph g;
908 for (int i = 0; i < 4; ++i)
909 nodes[i] = g.insert_node(i);
910
911 // Add all edges
912 for (int i = 0; i < 4; ++i)
913 for (int j = i + 1; j < 4; ++j)
914 g.insert_arc(nodes[i], nodes[j], i * 10 + j);
915
917 EXPECT_EQ(mat.get_num_nodes(), 4u);
918
919 // All pairs should have arcs (undirected)
920 for (long i = 0; i < 4; ++i)
921 for (long j = 0; j < 4; ++j)
922 if (i != j)
923 EXPECT_NE(mat(i, j), nullptr);
924}
925
926TEST(MatGraphEdgeCases, SelfLoop)
927{
928 IntDigraph g;
929 auto* n = g.insert_node(0);
930 g.insert_arc(n, n, 99); // Self-loop
931
933 long idx = mat(n);
934 EXPECT_NE(mat(idx, idx), nullptr);
935 EXPECT_EQ(mat(idx, idx)->get_info(), 99);
936}
937
938// =============================================================================
939// Performance / Stress Tests
940// =============================================================================
941
942TEST(MatGraphStress, LargeGraph)
943{
944 constexpr int N = 100;
945
946 IntDigraph g;
947 std::vector<IntDigraph::Node*> nodes(N);
948
949 for (int i = 0; i < N; ++i)
950 nodes[i] = g.insert_node(i);
951
952 // Create chain: 0 -> 1 -> 2 -> ... -> N-1
953 for (int i = 0; i < N - 1; ++i)
954 g.insert_arc(nodes[i], nodes[i + 1], i);
955
957 EXPECT_EQ(mat.get_num_nodes(), static_cast<size_t>(N));
958
959 Ady_Mat<IntDigraph, int> amat(g, -1);
961
962 // Verify chain structure
963 for (int i = 0; i < N - 1; ++i)
964 {
965 long si = amat(nodes[i]);
966 long ti = amat(nodes[i + 1]);
967 EXPECT_EQ(amat(si, ti), i);
968 }
969}
970
971int main(int argc, char** argv)
972{
973 ::testing::InitGoogleTest(&argc, argv);
974 return RUN_ALL_TESTS();
975}
int main()
long double h
Definition btreepic.C:154
Auxiliary adjacency matrix with custom entry type.
void operate_all_arcs_list_graph()
Apply operation to all arcs from the graph.
void set_null_value(const Entry_Type &null) noexcept
Set the null value.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
void operate_all_arcs_matrix()
Apply operation to all matrix entries.
const Entry_Type & null_value() const noexcept
Get the null value.
GT & get_list_graph() noexcept
Get reference to underlying graph.
Bit matrix for graph connectivity.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
GT * get_list_graph() noexcept
Get pointer to associated graph (nullptr if none)
void set_list_graph(GT &g, SA sa=SA())
Associate with a graph.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
typename BaseGraph::Arc Arc
Definition graph-dry.H:4381
typename BaseGraph::Node Node
Definition graph-dry.H:4380
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Adjacency matrix mapped to an adjacency list graph.
GT & get_list_graph() noexcept
Get reference to underlying graph.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Adjacency matrix storing copies of graph attributes.
const Arc_Type & null_value() const noexcept
Get the null value indicating no arc.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
IntDigraph::Node * n2
IntDigraph::Node * n0
void SetUp() override
IntDigraph::Node * n1
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
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_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
IntGraph::Node * n2
IntGraph::Node * n3
void SetUp() override
IntGraph::Node * n1
IntGraph::Node * n0
#define TEST(name)
#define N
Definition fib.C:294
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
long index_array(long i, long j, long n) noexcept
Convert 2D matrix indices to 1D array index.
long checked_index_array(long i, long j, long n)
Validate and convert indices to linear index.
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
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
void operator()(Ady_Mat< IntDigraph, int > &mat, IntDigraph::Node *src, IntDigraph::Node *tgt, long i, long j, int &entry)
void operator()(Ady_Mat< IntDigraph, int > &mat, IntDigraph::Arc *arc, long i, long j, int &entry)
Generic graph and digraph implementations.
Adjacency matrix representations for graphs.
TEST_F(MapMatrixGraphTest, Constructor)