Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_functional_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
45#include <gtest/gtest.h>
46#include <tpl_graph.H>
47#include <tpl_sgraph.H>
48#include <tpl_agraph.H>
49
50using namespace Aleph;
51
52// ==================== Type definitions ====================
53
54// List-based implementations with Dlink (use Graph_Node / Graph_Arc)
59
60// Sparse list implementations with DynList (use Graph_Snode / Graph_Sarc)
65
66// Array-based implementations (use Graph_Anode / Graph_Aarc)
71
72// For backward compatibility with existing tests
75
76class GraphFunctionalTest : public ::testing::Test
77{
78protected:
81
84
87
88 void SetUp() override
89 {
90 // Build undirected graph:
91 // n1(1) --1.0-- n2(2) --2.0-- n3(3)
92 // | |
93 // 3.0 4.0
94 // | |
95 // n4(4) -------n5(5)
96
97 n1 = g.insert_node(1);
98 n2 = g.insert_node(2);
99 n3 = g.insert_node(3);
100 n4 = g.insert_node(4);
101 n5 = g.insert_node(5);
102
103 a1 = g.insert_arc(n1, n2, 1.0);
104 a2 = g.insert_arc(n2, n3, 2.0);
105 a3 = g.insert_arc(n1, n4, 3.0);
106 a4 = g.insert_arc(n2, n5, 4.0);
107
108 // Build directed graph:
109 // dn1(10) --1.5--> dn2(20) --2.5--> dn3(30)
110 // | ^ |
111 // | | |
112 // 0.5 3.5 4.5
113 // | | |
114 // v | v
115 // dn4(40) -----------+ dn3
116
117 dn1 = dg.insert_node(10);
118 dn2 = dg.insert_node(20);
119 dn3 = dg.insert_node(30);
120 dn4 = dg.insert_node(40);
121
122 da1 = dg.insert_arc(dn1, dn2, 1.5);
123 da2 = dg.insert_arc(dn2, dn3, 2.5);
124 da3 = dg.insert_arc(dn1, dn4, 0.5);
125 da4 = dg.insert_arc(dn4, dn2, 3.5);
126 da5 = dg.insert_arc(dn3, dn3, 4.5); // self-loop
127 }
128};
129
130// ==================== none_node tests ====================
131
132TEST_F(GraphFunctionalTest, NoneNodeReturnsTrueWhenNoMatch)
133{
134 // No node has value > 100
135 EXPECT_TRUE(g.none_node([](auto p) { return p->get_info() > 100; }));
136 EXPECT_TRUE(dg.none_node([](auto p) { return p->get_info() > 100; }));
137}
138
139TEST_F(GraphFunctionalTest, NoneNodeReturnsFalseWhenSomeMatch)
140{
141 // Some nodes have value > 2
142 EXPECT_FALSE(g.none_node([](auto p) { return p->get_info() > 2; }));
143 EXPECT_FALSE(dg.none_node([](auto p) { return p->get_info() > 20; }));
144}
145
146TEST_F(GraphFunctionalTest, NoneNodeOnEmptyGraph)
147{
148 TestGraph empty;
149 EXPECT_TRUE(empty.none_node([](auto) { return true; }));
150}
151
152// ==================== none_arc tests ====================
153
154TEST_F(GraphFunctionalTest, NoneArcReturnsTrueWhenNoMatch)
155{
156 // No arc has weight > 100
157 EXPECT_TRUE(g.none_arc([](auto a) { return a->get_info() > 100.0; }));
158 EXPECT_TRUE(dg.none_arc([](auto a) { return a->get_info() > 100.0; }));
159}
160
161TEST_F(GraphFunctionalTest, NoneArcReturnsFalseWhenSomeMatch)
162{
163 // Some arcs have weight > 2
164 EXPECT_FALSE(g.none_arc([](auto a) { return a->get_info() > 2.0; }));
165 EXPECT_FALSE(dg.none_arc([](auto a) { return a->get_info() > 2.0; }));
166}
167
168TEST_F(GraphFunctionalTest, NoneArcAdjacentToNode)
169{
170 // n1 has arcs with weights 1.0 and 3.0, none > 10
171 EXPECT_TRUE(g.none_arc(n1, [](auto a) { return a->get_info() > 10.0; }));
172
173 // n1 has arc with weight 3.0
174 EXPECT_FALSE(g.none_arc(n1, [](auto a) { return a->get_info() > 2.5; }));
175}
176
177// ==================== count_nodes tests ====================
178
180{
181 EXPECT_EQ(g.count_nodes(), 5u);
182 EXPECT_EQ(dg.count_nodes(), 4u);
183}
184
185TEST_F(GraphFunctionalTest, CountNodesWithPredicate)
186{
187 // Count nodes with value > 2
188 EXPECT_EQ(g.count_nodes([](auto p) { return p->get_info() > 2; }), 3u);
189
190 // Count nodes with even value
191 EXPECT_EQ(g.count_nodes([](auto p) { return p->get_info() % 2 == 0; }), 2u);
192
193 // Count digraph nodes > 25
194 EXPECT_EQ(dg.count_nodes([](auto p) { return p->get_info() > 25; }), 2u);
195}
196
197TEST_F(GraphFunctionalTest, CountNodesNoneMatch)
198{
199 EXPECT_EQ(g.count_nodes([](auto p) { return p->get_info() > 100; }), 0u);
200}
201
202TEST_F(GraphFunctionalTest, CountNodesEmptyGraph)
203{
204 TestGraph empty;
205 EXPECT_EQ(empty.count_nodes(), 0u);
206}
207
208// ==================== count_arcs tests ====================
209
211{
212 EXPECT_EQ(g.count_arcs(), 4u);
213 EXPECT_EQ(dg.count_arcs(), 5u);
214}
215
216TEST_F(GraphFunctionalTest, CountArcsWithPredicate)
217{
218 // Count arcs with weight > 2
219 EXPECT_EQ(g.count_arcs([](auto a) { return a->get_info() > 2.0; }), 2u);
220
221 // Count arcs with weight <= 2
222 EXPECT_EQ(g.count_arcs([](auto a) { return a->get_info() <= 2.0; }), 2u);
223
224 // Count digraph arcs > 2
225 EXPECT_EQ(dg.count_arcs([](auto a) { return a->get_info() > 2.0; }), 3u);
226}
227
228TEST_F(GraphFunctionalTest, CountArcsAdjacentToNode)
229{
230 // n2 has 3 adjacent arcs
231 EXPECT_EQ(g.count_arcs(n2), 3u);
232
233 // n1 has 2 adjacent arcs
234 EXPECT_EQ(g.count_arcs(n1), 2u);
235
236 // Count arcs adjacent to n2 with weight > 1.5
237 EXPECT_EQ(g.count_arcs(n2, [](auto a) { return a->get_info() > 1.5; }), 2u);
238}
239
240TEST_F(GraphFunctionalTest, CountArcsEmptyGraph)
241{
242 TestGraph empty;
243 EXPECT_EQ(empty.count_arcs(), 0u);
244}
245
246// ==================== sum_arcs tests ====================
247
248TEST_F(GraphFunctionalTest, SumArcsAdjacentToNode)
249{
250 // n1 has arcs with weights 1.0 + 3.0 = 4.0
251 double sum = g.sum_arcs<double>(n1);
252 EXPECT_DOUBLE_EQ(sum, 4.0);
253
254 // n2 has arcs with weights 1.0 + 2.0 + 4.0 = 7.0
255 sum = g.sum_arcs<double>(n2);
256 EXPECT_DOUBLE_EQ(sum, 7.0);
257}
258
259TEST_F(GraphFunctionalTest, SumArcsIsolatedNode)
260{
261 auto isolated = g.insert_node(100);
262 double sum = g.sum_arcs<double>(isolated);
263 EXPECT_DOUBLE_EQ(sum, 0.0);
264}
265
266TEST_F(GraphFunctionalTest, SumArcsWithCustomExtractor)
267{
268 // Sum using custom extractor
269 double sum = g.sum_arcs<double>(n2, [](auto a) { return a->get_info() * 2; });
270 EXPECT_DOUBLE_EQ(sum, 14.0); // (1 + 2 + 4) * 2
271}
272
273// ==================== min_arc tests ====================
274
275TEST_F(GraphFunctionalTest, MinArcAdjacentToNode)
276{
277 // n2's minimum arc should be weight 1.0 (to n1)
278 auto min_a = g.min_arc(n2);
279 ASSERT_NE(min_a, nullptr);
280 EXPECT_DOUBLE_EQ(min_a->get_info(), 1.0);
281}
282
284{
285 // Global minimum arc is weight 1.0
286 auto min_a = g.min_arc();
287 ASSERT_NE(min_a, nullptr);
288 EXPECT_DOUBLE_EQ(min_a->get_info(), 1.0);
289
290 // Digraph global minimum is 0.5
291 auto min_da = dg.min_arc();
292 ASSERT_NE(min_da, nullptr);
293 EXPECT_DOUBLE_EQ(min_da->get_info(), 0.5);
294}
295
296TEST_F(GraphFunctionalTest, MinArcIsolatedNode)
297{
298 auto isolated = g.insert_node(100);
299 auto min_a = g.min_arc(isolated);
300 EXPECT_EQ(min_a, nullptr);
301}
302
303TEST_F(GraphFunctionalTest, MinArcEmptyGraph)
304{
305 TestGraph empty;
306 auto min_a = empty.min_arc();
307 EXPECT_EQ(min_a, nullptr);
308}
309
310TEST_F(GraphFunctionalTest, MinArcWithCustomComparator)
311{
312 // Find arc with maximum weight (reverse comparator)
313 auto max_via_min = g.min_arc(n2, [](auto a, auto b) {
314 return a->get_info() > b->get_info();
315 });
316 ASSERT_NE(max_via_min, nullptr);
317 EXPECT_DOUBLE_EQ(max_via_min->get_info(), 4.0);
318}
319
320// ==================== max_arc tests ====================
321
322TEST_F(GraphFunctionalTest, MaxArcAdjacentToNode)
323{
324 // n2's maximum arc should be weight 4.0 (to n5)
325 auto max_a = g.max_arc(n2);
326 ASSERT_NE(max_a, nullptr);
327 EXPECT_DOUBLE_EQ(max_a->get_info(), 4.0);
328}
329
331{
332 // Global maximum arc is weight 4.0
333 auto max_a = g.max_arc();
334 ASSERT_NE(max_a, nullptr);
335 EXPECT_DOUBLE_EQ(max_a->get_info(), 4.0);
336
337 // Digraph global maximum is 4.5 (self-loop)
338 auto max_da = dg.max_arc();
339 ASSERT_NE(max_da, nullptr);
340 EXPECT_DOUBLE_EQ(max_da->get_info(), 4.5);
341}
342
343TEST_F(GraphFunctionalTest, MaxArcIsolatedNode)
344{
345 auto isolated = g.insert_node(100);
346 auto max_a = g.max_arc(isolated);
347 EXPECT_EQ(max_a, nullptr);
348}
349
350// ==================== partition_nodes tests ====================
351
352TEST_F(GraphFunctionalTest, PartitionNodesByValue)
353{
354 auto [high, low] = g.partition_nodes([](auto p) { return p->get_info() > 2; });
355
356 EXPECT_EQ(high.size(), 3u); // 3, 4, 5
357 EXPECT_EQ(low.size(), 2u); // 1, 2
358
359 // Verify contents
360 EXPECT_TRUE(high.exists([](auto p) { return p->get_info() == 3; }));
361 EXPECT_TRUE(high.exists([](auto p) { return p->get_info() == 4; }));
362 EXPECT_TRUE(high.exists([](auto p) { return p->get_info() == 5; }));
363
364 EXPECT_TRUE(low.exists([](auto p) { return p->get_info() == 1; }));
365 EXPECT_TRUE(low.exists([](auto p) { return p->get_info() == 2; }));
366}
367
368TEST_F(GraphFunctionalTest, PartitionNodesAllMatch)
369{
370 auto [match, nomatch] = g.partition_nodes([](auto p) { return p->get_info() > 0; });
371
372 EXPECT_EQ(match.size(), 5u);
373 EXPECT_EQ(nomatch.size(), 0u);
374}
375
376TEST_F(GraphFunctionalTest, PartitionNodesNoneMatch)
377{
378 auto [match, nomatch] = g.partition_nodes([](auto p) { return p->get_info() > 100; });
379
380 EXPECT_EQ(match.size(), 0u);
381 EXPECT_EQ(nomatch.size(), 5u);
382}
383
384TEST_F(GraphFunctionalTest, PartitionNodesEmptyGraph)
385{
386 TestGraph empty;
387 auto [match, nomatch] = empty.partition_nodes([](auto) { return true; });
388
389 EXPECT_EQ(match.size(), 0u);
390 EXPECT_EQ(nomatch.size(), 0u);
391}
392
393// ==================== partition_arcs tests ====================
394
395TEST_F(GraphFunctionalTest, PartitionArcsByWeight)
396{
397 auto [heavy, light] = g.partition_arcs([](auto a) { return a->get_info() > 2.0; });
398
399 EXPECT_EQ(heavy.size(), 2u); // 3.0, 4.0
400 EXPECT_EQ(light.size(), 2u); // 1.0, 2.0
401}
402
403TEST_F(GraphFunctionalTest, PartitionArcsDigraph)
404{
405 auto [high, low] = dg.partition_arcs([](auto a) { return a->get_info() > 2.0; });
406
407 EXPECT_EQ(high.size(), 3u); // 2.5, 3.5, 4.5
408 EXPECT_EQ(low.size(), 2u); // 1.5, 0.5
409}
410
411// ==================== adjacent_nodes tests ====================
412
413TEST_F(GraphFunctionalTest, AdjacentNodesUndirected)
414{
415 // n2 is connected to n1, n3, n5
416 auto neighbors = g.adjacent_nodes(n2);
417 EXPECT_EQ(neighbors.size(), 3u);
418
419 EXPECT_TRUE(neighbors.exists([this](auto p) { return p == n1; }));
420 EXPECT_TRUE(neighbors.exists([this](auto p) { return p == n3; }));
421 EXPECT_TRUE(neighbors.exists([this](auto p) { return p == n5; }));
422}
423
424TEST_F(GraphFunctionalTest, AdjacentNodesDirected)
425{
426 // dn1 has outgoing arcs to dn2 and dn4
427 auto neighbors = dg.adjacent_nodes(dn1);
428 EXPECT_EQ(neighbors.size(), 2u);
429
430 EXPECT_TRUE(neighbors.exists([this](auto p) { return p == dn2; }));
431 EXPECT_TRUE(neighbors.exists([this](auto p) { return p == dn4; }));
432}
433
434TEST_F(GraphFunctionalTest, AdjacentNodesIsolated)
435{
436 auto isolated = g.insert_node(100);
437 auto neighbors = g.adjacent_nodes(isolated);
438 EXPECT_EQ(neighbors.size(), 0u);
439}
440
441TEST_F(GraphFunctionalTest, AdjacentNodesSelfLoop)
442{
443 // dn3 has a self-loop, so itself is a neighbor
444 auto neighbors = dg.adjacent_nodes(dn3);
445 EXPECT_TRUE(neighbors.exists([this](auto p) { return p == dn3; }));
446}
447
448// ==================== Combination tests ====================
449
450TEST_F(GraphFunctionalTest, CountAndPartitionConsistency)
451{
452 auto pred = [](auto p) { return p->get_info() > 2; };
453
454 size_t count = g.count_nodes(pred);
455 auto [yes, no] = g.partition_nodes(pred);
456
457 EXPECT_EQ(count, yes.size());
458 EXPECT_EQ(g.vsize() - count, no.size());
459}
460
461TEST_F(GraphFunctionalTest, NoneAndExistsConsistency)
462{
463 auto pred = [](auto p) { return p->get_info() > 100; };
464
465 EXPECT_EQ(g.none_node(pred), not g.exists_node(pred));
466 EXPECT_EQ(g.none_arc([](auto a) { return a->get_info() > 100; }),
467 not g.exists_arc([](auto a) { return a->get_info() > 100; }));
468}
469
470TEST_F(GraphFunctionalTest, MinMaxConsistency)
471{
472 auto min_a = g.min_arc();
473 auto max_a = g.max_arc();
474
475 ASSERT_NE(min_a, nullptr);
476 ASSERT_NE(max_a, nullptr);
477
478 // Min should be <= max
479 EXPECT_LE(min_a->get_info(), max_a->get_info());
480
481 // No arc should be smaller than min
482 EXPECT_TRUE(g.none_arc([min_a](auto a) {
483 return a->get_info() < min_a->get_info();
484 }));
485
486 // No arc should be larger than max
487 EXPECT_TRUE(g.none_arc([max_a](auto a) {
488 return a->get_info() > max_a->get_info();
489 }));
490}
491
492// ==================== Edge cases ====================
493
495{
496 TestGraph single;
497 auto n = single.insert_node(42);
498
499 EXPECT_EQ(single.count_nodes(), 1u);
500 EXPECT_EQ(single.count_arcs(), 0u);
501 EXPECT_TRUE(single.none_arc([](auto) { return true; }));
502 EXPECT_EQ(single.adjacent_nodes(n).size(), 0u);
503 EXPECT_EQ(single.min_arc(), nullptr);
504 EXPECT_EQ(single.max_arc(), nullptr);
505}
506
508{
509 auto n = g.insert_node(99);
510 auto self = g.insert_arc(n, n, 10.0);
511
512 // Self-loop should be counted
513 EXPECT_EQ(g.count_arcs(n), 1u);
514
515 // min/max of single arc
516 EXPECT_EQ(g.min_arc(n), self);
517 EXPECT_EQ(g.max_arc(n), self);
518
519 // Sum of self-loop
520 EXPECT_DOUBLE_EQ(g.sum_arcs<double>(n), 10.0);
521}
522
523// ==================== nodes_map / arcs_map tests ====================
524
526{
527 // Map node values to strings
528 auto strings = nodes_map<TestGraph, std::string>(g, [](auto p) {
529 return std::to_string(p->get_info());
530 });
531
532 EXPECT_EQ(strings.size(), 5u);
533 EXPECT_TRUE(strings.exists([](const auto& s) { return s == "1"; }));
534 EXPECT_TRUE(strings.exists([](const auto& s) { return s == "5"; }));
535}
536
538{
539 // Map arc weights to integers
540 auto weights = arcs_map<TestGraph, int>(g, [](auto a) {
541 return static_cast<int>(a->get_info());
542 });
543
544 EXPECT_EQ(weights.size(), 4u);
545 EXPECT_TRUE(weights.exists([](int w) { return w == 1; }));
546 EXPECT_TRUE(weights.exists([](int w) { return w == 4; }));
547}
548
550{
551 // Map arcs adjacent to n2 to their weights
552 auto weights = arcs_map<TestGraph, double>(g, n2, [](auto a) {
553 return a->get_info();
554 });
555
556 EXPECT_EQ(weights.size(), 3u); // n2 has 3 adjacent arcs
557}
558
559// ==================== filter_nodes / filter_arcs tests ====================
560
561TEST_F(GraphFunctionalTest, FilterNodesMethod)
562{
563 // Filter nodes with value > 2
564 auto filtered = g.filter_nodes([](auto p) { return p->get_info() > 2; });
565
566 EXPECT_EQ(filtered.size(), 3u);
567 EXPECT_TRUE(filtered.exists([](auto p) { return p->get_info() == 3; }));
568 EXPECT_TRUE(filtered.exists([](auto p) { return p->get_info() == 4; }));
569 EXPECT_TRUE(filtered.exists([](auto p) { return p->get_info() == 5; }));
570}
571
572TEST_F(GraphFunctionalTest, FilterArcsMethod)
573{
574 // Filter arcs with weight > 2
575 auto filtered = g.filter_arcs([](auto a) { return a->get_info() > 2.0; });
576
577 EXPECT_EQ(filtered.size(), 2u);
578}
579
580TEST_F(GraphFunctionalTest, FilterArcsFromNodeMethod)
581{
582 // Filter arcs adjacent to n2 with weight > 1.5
583 auto filtered = g.filter_arcs(n2, [](auto a) { return a->get_info() > 1.5; });
584
585 EXPECT_EQ(filtered.size(), 2u); // weights 2.0 and 4.0
586}
587
588// ==================== all_nodes / all_arcs method tests ====================
589
591{
592 // All nodes have positive values
593 EXPECT_TRUE(g.all_nodes([](auto p) { return p->get_info() > 0; }));
594
595 // Not all nodes have value > 3
596 EXPECT_FALSE(g.all_nodes([](auto p) { return p->get_info() > 3; }));
597}
598
600{
601 // All arcs have positive weights
602 EXPECT_TRUE(g.all_arcs([](auto a) { return a->get_info() > 0.0; }));
603
604 // Not all arcs have weight > 2
605 EXPECT_FALSE(g.all_arcs([](auto a) { return a->get_info() > 2.0; }));
606}
607
608TEST_F(GraphFunctionalTest, AllArcsFromNodeMethod)
609{
610 // All arcs from n1 have weight > 0
611 EXPECT_TRUE(g.all_arcs(n1, [](auto a) { return a->get_info() > 0.0; }));
612
613 // Not all arcs from n1 have weight > 2
614 EXPECT_FALSE(g.all_arcs(n1, [](auto a) { return a->get_info() > 2.0; }));
615}
616
617// ==================== exists_node / exists_arc method tests ====================
618
619TEST_F(GraphFunctionalTest, ExistsNodeMethod)
620{
621 // Exists node with value 3
622 EXPECT_TRUE(g.exists_node([](auto p) { return p->get_info() == 3; }));
623
624 // No node with value 100
625 EXPECT_FALSE(g.exists_node([](auto p) { return p->get_info() == 100; }));
626}
627
629{
630 // Exists arc with weight 3.0
631 EXPECT_TRUE(g.exists_arc([](auto a) { return a->get_info() == 3.0; }));
632
633 // No arc with weight 100.0
634 EXPECT_FALSE(g.exists_arc([](auto a) { return a->get_info() == 100.0; }));
635}
636
637TEST_F(GraphFunctionalTest, ExistsArcFromNodeMethod)
638{
639 // n1 has arc with weight 3.0
640 EXPECT_TRUE(g.exists_arc(n1, [](auto a) { return a->get_info() == 3.0; }));
641
642 // n1 has no arc with weight 100.0
643 EXPECT_FALSE(g.exists_arc(n1, [](auto a) { return a->get_info() == 100.0; }));
644}
645
646// ==================== map_in_arcs / map_out_arcs tests (digraph) ====================
647
648TEST_F(GraphFunctionalTest, MapOutArcsDigraph)
649{
650 // Map outgoing arcs from dn1 to weights using method
651 double sum = 0;
652 dg.for_each_out_arc(dn1, [&sum](auto a) { sum += a->get_info(); });
653 EXPECT_DOUBLE_EQ(sum, 2.0); // 1.5 + 0.5
654
655 // Verify out_arcs collection
656 auto outs = dg.out_arcs(dn1);
657 EXPECT_EQ(outs.size(), 2u);
658}
659
660TEST_F(GraphFunctionalTest, MapInArcsDigraph)
661{
662 // A List_Digraph keeps each arc only in the list of its source: the in_
663 // methods find the arcs into a node among the arcs of the graph
664 auto weights = dg.template in_arcs_map<double>(dn2, [](auto a) { return a->get_info(); });
665 ASSERT_EQ(weights.size(), 2u); // 1.5 from dn1 and 3.5 from dn4
666 EXPECT_DOUBLE_EQ(weights.foldl(0.0, [](double acc, double w) { return acc + w; }), 5.0);
667
668 auto outs = dg.out_arcs(dn1);
669 EXPECT_EQ(outs.size(), 2u); // dn1 has 2 outgoing arcs
670}
671
672// ==================== foldl_in_arcs / foldl_out_arcs tests ====================
673
674TEST_F(GraphFunctionalTest, FoldlOutArcsDigraph)
675{
676 // Sum outgoing arc weights from dn1 using for_each
677 double sum = 0.0;
678 dg.for_each_out_arc(dn1, [&sum](auto a) { sum += a->get_info(); });
679
680 EXPECT_DOUBLE_EQ(sum, 2.0); // 1.5 + 0.5
681}
682
683TEST_F(GraphFunctionalTest, FoldlInArcsDigraph)
684{
685 // The arcs into dn2 come from dn1 and dn4
686 const double sum = dg.foldl_in_arcs(dn2, 0.0, [](const double & acc, auto a)
687 { return acc + a->get_info(); });
688 EXPECT_DOUBLE_EQ(sum, 5.0); // 1.5 + 3.5
689
690 // and those into dn3, from dn2 and from the loop of dn3
691 double into_dn3 = 0.0;
692 dg.for_each_in_arc(dn3, [&into_dn3](auto a) { into_dn3 += a->get_info(); });
693 EXPECT_DOUBLE_EQ(into_dn3, 7.0); // 2.5 + 4.5
694}
695
696// ==================== filter_in_arcs / filter_out_arcs tests ====================
697
698TEST_F(GraphFunctionalTest, FilterOutArcsDigraph)
699{
700 // Filter outgoing arcs from dn1 with weight > 1.0
701 auto filtered = dg.filter_out_arcs(dn1, [](auto a) {
702 return a->get_info() > 1.0;
703 });
704
705 EXPECT_EQ(filtered.size(), 1u); // Only 1.5
706}
707
708TEST_F(GraphFunctionalTest, FilterInArcsDigraph)
709{
710 // Filter incoming arcs to dn2 with weight > 2.0
711 auto filtered = dg.filter_in_arcs(dn2, [](auto a) {
712 return a->get_info() > 2.0;
713 });
714
715 ASSERT_EQ(filtered.size(), 1u); // 3.5, from dn4
716 EXPECT_EQ(filtered.get_first(), da4);
717}
718
719// ==================== search_node / find_node tests ====================
720
721TEST_F(GraphFunctionalTest, SearchNodeMethod)
722{
723 // Search node with value 3
724 auto found = g.search_node([](auto p) { return p->get_info() == 3; });
725 ASSERT_NE(found, nullptr);
726 EXPECT_EQ(found->get_info(), 3);
727
728 // Search non-existent
729 auto not_found = g.search_node([](auto p) { return p->get_info() == 100; });
730 EXPECT_EQ(not_found, nullptr);
731}
732
734{
735 // Find node with value 3
736 auto found = g.find_node(3);
737 ASSERT_NE(found, nullptr);
738 EXPECT_EQ(found->get_info(), 3);
739
740 // Find non-existent
741 auto not_found = g.find_node(100);
742 EXPECT_EQ(not_found, nullptr);
743}
744
745// ==================== search_arc / find_arc tests ====================
746
748{
749 // Search arc with weight 3.0
750 auto found = g.search_arc([](auto a) { return a->get_info() == 3.0; });
751 ASSERT_NE(found, nullptr);
752 EXPECT_DOUBLE_EQ(found->get_info(), 3.0);
753
754 // Search non-existent
755 auto not_found = g.search_arc([](auto a) { return a->get_info() == 100.0; });
756 EXPECT_EQ(not_found, nullptr);
757}
758
760{
761 // Find arc with weight 3.0
762 auto found = g.find_arc(3.0);
763 ASSERT_NE(found, nullptr);
764 EXPECT_DOUBLE_EQ(found->get_info(), 3.0);
765
766 // Find non-existent
767 auto not_found = g.find_arc(100.0);
768 EXPECT_EQ(not_found, nullptr);
769}
770
771TEST_F(GraphFunctionalTest, SearchArcFromNodeMethod)
772{
773 // Search arc from n1 with weight 3.0
774 auto found = g.search_arc(n1, [](auto a) { return a->get_info() == 3.0; });
775 ASSERT_NE(found, nullptr);
776 EXPECT_DOUBLE_EQ(found->get_info(), 3.0);
777
778 // Search non-existent from n1
779 auto not_found = g.search_arc(n1, [](auto a) { return a->get_info() == 100.0; });
780 EXPECT_EQ(not_found, nullptr);
781}
782
783// ==================== traverse_nodes / traverse_arcs tests ====================
784
785TEST_F(GraphFunctionalTest, TraverseNodesStopsOnFalse)
786{
787 int count = 0;
788 bool completed = g.traverse_nodes([&count](auto p) {
789 ++count;
790 return p->get_info() < 3; // Stop when value >= 3
791 });
792
793 EXPECT_FALSE(completed); // Should have stopped early
794 EXPECT_LT(count, 5); // Didn't visit all nodes
795}
796
797TEST_F(GraphFunctionalTest, TraverseNodesCompletesOnAllTrue)
798{
799 int count = 0;
800 bool completed = g.traverse_nodes([&count](auto) {
801 ++count;
802 return true; // Always continue
803 });
804
805 EXPECT_TRUE(completed);
806 EXPECT_EQ(count, 5);
807}
808
809TEST_F(GraphFunctionalTest, TraverseArcsStopsOnFalse)
810{
811 int count = 0;
812 bool completed = g.traverse_arcs([&count](auto a) {
813 ++count;
814 return a->get_info() < 3.0; // Stop when weight >= 3.0
815 });
816
817 EXPECT_FALSE(completed);
818 EXPECT_LT(count, 4);
819}
820
821TEST_F(GraphFunctionalTest, TraverseArcsFromNode)
822{
823 int count = 0;
824 bool completed = g.traverse_arcs(n2, [&count](auto) {
825 ++count;
826 return true;
827 });
828
829 EXPECT_TRUE(completed);
830 EXPECT_EQ(count, 3); // n2 has 3 adjacent arcs
831}
832
833// ==================== for_each method tests ====================
834
835TEST_F(GraphFunctionalTest, ForEachNodeMethod)
836{
837 int sum = 0;
838 g.for_each_node([&sum](auto p) { sum += p->get_info(); });
839 EXPECT_EQ(sum, 15); // 1+2+3+4+5
840}
841
842TEST_F(GraphFunctionalTest, ForEachArcMethod)
843{
844 double sum = 0;
845 g.for_each_arc([&sum](auto a) { sum += a->get_info(); });
846 EXPECT_DOUBLE_EQ(sum, 10.0); // 1+2+3+4
847}
848
849TEST_F(GraphFunctionalTest, ForEachArcFromNodeMethod)
850{
851 double sum = 0;
852 g.for_each_arc(n2, [&sum](auto a) { sum += a->get_info(); });
853 EXPECT_DOUBLE_EQ(sum, 7.0); // 1+2+4
854}
855
856// traverse_arcs<Itor>() and for_each_arc<Itor>() go through the arcs that
857// the iterator gives: with an iterator that has get_curr_ne(); with one that
858// has only get_curr(), which is not const; and with one whose get_curr_ne()
859// gives another type, which they must not take
860TEST(GraphFunctionalItor, TheArcsOfANodeThroughAGivenIterator)
861{
862 ListDigraph g;
863 auto a = g.insert_node(1);
864 auto b = g.insert_node(2);
865 auto c = g.insert_node(3);
866 g.insert_arc(a, b, 1);
867 g.insert_arc(a, c, 2);
868 g.insert_arc(b, a, 4);
869
870 // the iterator over the arcs of a node, without get_curr_ne(), and with a
871 // get_curr() that is not const
872 struct Checked_Only
873 {
874 ListDigraph::Node_Arc_Iterator it;
875
876 explicit Checked_Only(ListDigraph::Node * p) : it(p) {}
877
878 bool has_curr() const { return it.has_curr(); }
879
880 void next_ne() noexcept { it.next_ne(); }
881
882 ListDigraph::Arc * get_curr() { return it.get_curr(); }
883 };
884
885 // the iterator over the arcs of a node, whose get_curr_ne() gives the
886 // node of the list of arcs instead of the arc
887 struct Other_Ne : ListDigraph::Node_Arc_Iterator
888 {
889 using ListDigraph::Node_Arc_Iterator::Node_Arc_Iterator;
890
891 void * get_curr_ne() const noexcept { return nullptr; }
892 };
893 static_assert(not AlephUncheckedIterator<Checked_Only>);
894 static_assert(not AlephUncheckedIterator<Other_Ne>);
895 static_assert(AlephUncheckedIterator<ListDigraph::Node_Arc_Iterator>);
896
897 double sum = 0;
898 auto add = [&sum](ListDigraph::Arc * arc) { sum += arc->get_info(); };
899 auto add_all = [&sum](ListDigraph::Arc * arc)
900 {
901 sum += arc->get_info();
902 return true;
903 };
904 auto add_first = [&sum](ListDigraph::Arc * arc)
905 {
906 sum += arc->get_info();
907 return false;
908 };
909
910 g.for_each_arc<ListDigraph::Out_Iterator>(a, add);
911 EXPECT_DOUBLE_EQ(sum, 3.0);
912
913 sum = 0;
914 EXPECT_TRUE(g.traverse_arcs<ListDigraph::Out_Iterator>(a, add_all));
915 EXPECT_DOUBLE_EQ(sum, 3.0);
916
917 sum = 0;
918 g.for_each_arc<Checked_Only>(b, add);
919 EXPECT_DOUBLE_EQ(sum, 4.0);
920
921 sum = 0;
922 EXPECT_TRUE(g.traverse_arcs<Checked_Only>(a, add_all));
923 EXPECT_DOUBLE_EQ(sum, 3.0);
924
925 sum = 0;
926 EXPECT_FALSE(g.traverse_arcs<Checked_Only>(a, add_first));
927 EXPECT_GT(sum, 0.0);
928 EXPECT_LT(sum, 3.0);
929
930 sum = 0;
931 g.for_each_arc<Other_Ne>(a, add);
932 EXPECT_DOUBLE_EQ(sum, 3.0);
933
934 sum = 0;
935 EXPECT_TRUE(g.traverse_arcs<Other_Ne>(a, add_all));
936 EXPECT_DOUBLE_EQ(sum, 3.0);
937}
938
939// ==================== foldl method tests ====================
940
941TEST_F(GraphFunctionalTest, FoldlNodesMethod)
942{
943 // Using free function since method requires std::function
944 int sum = foldl_nodes<TestGraph, int>(g, 0,
945 std::function<int(const int&, TestGraph::Node*)>(
946 [](const int& acc, auto p) { return acc + p->get_info(); }
947 ));
948 EXPECT_EQ(sum, 15);
949}
950
952{
953 // Using free function since method requires std::function
954 double sum = foldl_arcs<TestGraph, double>(g, 0.0,
955 std::function<double(const double&, TestGraph::Arc*)>(
956 [](const double& acc, auto a) { return acc + a->get_info(); }
957 ));
958 EXPECT_DOUBLE_EQ(sum, 10.0);
959}
960
961TEST_F(GraphFunctionalTest, FoldlArcsFromNodeMethod)
962{
963 // Manual fold using for_each_arc
964 double sum = 0.0;
965 g.for_each_arc(n2, [&sum](auto a) { sum += a->get_info(); });
966 EXPECT_DOUBLE_EQ(sum, 7.0);
967}
968
969// ==================== nodes() / arcs() collection tests ====================
970
971TEST_F(GraphFunctionalTest, NodesCollectionMethod)
972{
973 auto all = g.nodes();
974 EXPECT_EQ(all.size(), 5u);
975}
976
977TEST_F(GraphFunctionalTest, ArcsCollectionMethod)
978{
979 auto all = g.arcs();
980 EXPECT_EQ(all.size(), 4u);
981}
982
983TEST_F(GraphFunctionalTest, ArcsFromNodeMethod)
984{
985 auto adj = g.arcs(n2);
986 EXPECT_EQ(adj.size(), 3u);
987}
988
989// ==================== in_nodes / out_nodes / in_arcs / out_arcs tests ====================
990
992{
993 auto outs = dg.out_nodes(dn1);
994 EXPECT_EQ(outs.size(), 2u); // dn2, dn4
995}
996
998{
999 // A List_Digraph keeps each arc only in the list of its source: the in_
1000 // methods find the arcs into a node among the arcs of the graph
1001 auto ins = dg.in_nodes(dn2);
1002 ASSERT_EQ(ins.size(), 2u);
1003 EXPECT_TRUE(ins.exists([this](auto p) { return p == dn1; }));
1004 EXPECT_TRUE(ins.exists([this](auto p) { return p == dn4; }));
1005}
1006
1008{
1009 auto outs = dg.out_arcs(dn1);
1010 EXPECT_EQ(outs.size(), 2u);
1011}
1012
1014{
1015 auto ins = dg.in_arcs(dn2);
1016 ASSERT_EQ(ins.size(), 2u); // da1 and da4
1017 EXPECT_TRUE(ins.exists([this](auto a) { return a == da1; }));
1018 EXPECT_TRUE(ins.exists([this](auto a) { return a == da4; }));
1019}
1020
1021// ==================== in_degree / out_degree tests ====================
1022
1024{
1025 // The in degree is computed among the arcs of the digraph
1026 EXPECT_EQ(dg.in_degree(dn1), 0u);
1027 EXPECT_EQ(dg.in_degree(dn2), 2u); // From dn1 and dn4
1028 EXPECT_EQ(dg.in_degree(dn3), 2u); // From dn2, and its loop
1029 EXPECT_EQ(dg.in_degree(dn4), 1u); // From dn1
1030 EXPECT_EQ(in_degree(dg, dn2), 2u);
1031}
1032
1033TEST_F(GraphFunctionalTest, OutDegreeDigraph)
1034{
1035 EXPECT_EQ(dg.out_degree(dn1), 2u); // To dn2, dn4
1036 EXPECT_EQ(dg.out_degree(dn2), 1u); // To dn3
1037 EXPECT_EQ(dg.out_degree(dn3), 1u); // Self-loop
1038}
1039
1040TEST_F(GraphFunctionalTest, DegreeUndirected)
1041{
1042 EXPECT_EQ(g.degree(n1), 2u);
1043 EXPECT_EQ(g.degree(n2), 3u);
1044 EXPECT_EQ(g.degree(n3), 1u);
1045}
1046
1047// =============================================================================
1048// TYPED TESTS: Test all graph implementations (List and Array)
1049// =============================================================================
1050
1051template <typename GraphType>
1052class UndirectedGraphTest : public ::testing::Test
1053{
1054protected:
1055 GraphType g;
1056 typename GraphType::Node *n1, *n2, *n3;
1057 typename GraphType::Arc *a1, *a2;
1058
1059 void SetUp() override
1060 {
1061 // Simple triangle: n1 -- n2 -- n3 -- n1
1062 n1 = g.insert_node(1);
1063 n2 = g.insert_node(2);
1064 n3 = g.insert_node(3);
1065
1066 a1 = g.insert_arc(n1, n2, 1.0);
1067 a2 = g.insert_arc(n2, n3, 2.0);
1068 }
1069};
1070
1071template <typename GraphType>
1072class DirectedGraphTest : public ::testing::Test
1073{
1074protected:
1075 GraphType g;
1076 typename GraphType::Node *n1, *n2, *n3;
1077 typename GraphType::Arc *a1, *a2;
1078
1079 void SetUp() override
1080 {
1081 // Simple chain: n1 -> n2 -> n3
1082 n1 = g.insert_node(10);
1083 n2 = g.insert_node(20);
1084 n3 = g.insert_node(30);
1085
1086 a1 = g.insert_arc(n1, n2, 1.5);
1087 a2 = g.insert_arc(n2, n3, 2.5);
1088 }
1089};
1090
1091// Define type lists for parameterized tests
1092// All three implementations are tested: List, Sparse, and Array
1093using UndirectedGraphTypes = ::testing::Types<ListGraph, SparseGraph, ArrayGraph>;
1094using DirectedGraphTypes = ::testing::Types<ListDigraph, SparseDigraph, ArrayDigraph>;
1095
1098
1099// ==================== Undirected Graph Tests (List & Array) ====================
1100
1102{
1103 EXPECT_EQ(this->g.get_num_nodes(), 3u);
1104 size_t count = this->g.count_nodes([](auto) { return true; });
1105 EXPECT_EQ(count, 3u);
1106}
1107
1109{
1110 EXPECT_EQ(this->g.get_num_arcs(), 2u);
1111 size_t count = this->g.count_arcs([](auto) { return true; });
1112 EXPECT_EQ(count, 2u);
1113}
1114
1116{
1117 EXPECT_TRUE(this->g.exists_node([](auto p) { return p->get_info() == 2; }));
1118 EXPECT_FALSE(this->g.exists_node([](auto p) { return p->get_info() == 99; }));
1119}
1120
1122{
1123 EXPECT_TRUE(this->g.exists_arc([](auto a) { return a->get_info() == 1.0; }));
1124 EXPECT_FALSE(this->g.exists_arc([](auto a) { return a->get_info() == 99.0; }));
1125}
1126
1128{
1129 EXPECT_TRUE(this->g.none_node([](auto p) { return p->get_info() > 100; }));
1130 EXPECT_FALSE(this->g.none_node([](auto p) { return p->get_info() == 1; }));
1131}
1132
1134{
1135 EXPECT_TRUE(this->g.none_arc([](auto a) { return a->get_info() > 100.0; }));
1136 EXPECT_FALSE(this->g.none_arc([](auto a) { return a->get_info() == 1.0; }));
1137}
1138
1140{
1141 EXPECT_TRUE(this->g.all_nodes([](auto p) { return p->get_info() > 0; }));
1142 EXPECT_FALSE(this->g.all_nodes([](auto p) { return p->get_info() > 2; }));
1143}
1144
1146{
1147 EXPECT_TRUE(this->g.all_arcs([](auto a) { return a->get_info() > 0.0; }));
1148 EXPECT_FALSE(this->g.all_arcs([](auto a) { return a->get_info() > 1.5; }));
1149}
1150
1152{
1153 auto filtered = this->g.filter_nodes([](auto p) { return p->get_info() > 1; });
1154 EXPECT_EQ(filtered.size(), 2u);
1155}
1156
1158{
1159 auto filtered = this->g.filter_arcs([](auto a) { return a->get_info() > 1.0; });
1160 EXPECT_EQ(filtered.size(), 1u);
1161}
1162
1164{
1165 auto found = this->g.search_node([](auto p) { return p->get_info() == 2; });
1166 ASSERT_NE(found, nullptr);
1167 EXPECT_EQ(found->get_info(), 2);
1168
1169 auto not_found = this->g.search_node([](auto p) { return p->get_info() == 99; });
1170 EXPECT_EQ(not_found, nullptr);
1171}
1172
1174{
1175 auto found = this->g.search_arc([](auto a) { return a->get_info() == 2.0; });
1176 ASSERT_NE(found, nullptr);
1177 EXPECT_DOUBLE_EQ(found->get_info(), 2.0);
1178}
1179
1181{
1182 auto [matching, not_matching] = this->g.partition_nodes(
1183 [](auto p) { return p->get_info() > 1; });
1184 EXPECT_EQ(matching.size(), 2u);
1185 EXPECT_EQ(not_matching.size(), 1u);
1186}
1187
1189{
1190 auto [matching, not_matching] = this->g.partition_arcs(
1191 [](auto a) { return a->get_info() > 1.0; });
1192 EXPECT_EQ(matching.size(), 1u);
1193 EXPECT_EQ(not_matching.size(), 1u);
1194}
1195
1197{
1198 int sum = 0;
1199 this->g.for_each_node([&sum](auto p) { sum += p->get_info(); });
1200 EXPECT_EQ(sum, 6); // 1 + 2 + 3
1201}
1202
1204{
1205 double sum = 0;
1206 this->g.for_each_arc([&sum](auto a) { sum += a->get_info(); });
1207 EXPECT_DOUBLE_EQ(sum, 3.0); // 1.0 + 2.0
1208}
1209
1211{
1212 EXPECT_EQ(this->g.degree(this->n1), 1u);
1213 EXPECT_EQ(this->g.degree(this->n2), 2u);
1214 EXPECT_EQ(this->g.degree(this->n3), 1u);
1215}
1216
1218{
1219 auto adj = this->g.adjacent_nodes(this->n2);
1220 EXPECT_EQ(adj.size(), 2u);
1221}
1222
1224{
1225 auto min = this->g.min_arc();
1226 ASSERT_NE(min, nullptr);
1227 EXPECT_DOUBLE_EQ(min->get_info(), 1.0);
1228}
1229
1231{
1232 auto max = this->g.max_arc();
1233 ASSERT_NE(max, nullptr);
1234 EXPECT_DOUBLE_EQ(max->get_info(), 2.0);
1235}
1236
1237// ==================== Directed Graph Tests (List & Array) ====================
1238
1240{
1241 EXPECT_EQ(this->g.get_num_nodes(), 3u);
1242 size_t count = this->g.count_nodes([](auto) { return true; });
1243 EXPECT_EQ(count, 3u);
1244}
1245
1247{
1248 EXPECT_EQ(this->g.get_num_arcs(), 2u);
1249 size_t count = this->g.count_arcs([](auto) { return true; });
1250 EXPECT_EQ(count, 2u);
1251}
1252
1254{
1255 EXPECT_TRUE(this->g.exists_node([](auto p) { return p->get_info() == 20; }));
1256 EXPECT_FALSE(this->g.exists_node([](auto p) { return p->get_info() == 99; }));
1257}
1258
1260{
1261 EXPECT_TRUE(this->g.exists_arc([](auto a) { return a->get_info() == 1.5; }));
1262 EXPECT_FALSE(this->g.exists_arc([](auto a) { return a->get_info() == 99.0; }));
1263}
1264
1266{
1267 EXPECT_TRUE(this->g.none_node([](auto p) { return p->get_info() > 100; }));
1268 EXPECT_FALSE(this->g.none_node([](auto p) { return p->get_info() == 10; }));
1269}
1270
1272{
1273 EXPECT_TRUE(this->g.none_arc([](auto a) { return a->get_info() > 100.0; }));
1274 EXPECT_FALSE(this->g.none_arc([](auto a) { return a->get_info() == 1.5; }));
1275}
1276
1278{
1279 EXPECT_TRUE(this->g.all_nodes([](auto p) { return p->get_info() > 0; }));
1280 EXPECT_FALSE(this->g.all_nodes([](auto p) { return p->get_info() > 20; }));
1281}
1282
1284{
1285 EXPECT_TRUE(this->g.all_arcs([](auto a) { return a->get_info() > 0.0; }));
1286 EXPECT_FALSE(this->g.all_arcs([](auto a) { return a->get_info() > 2.0; }));
1287}
1288
1290{
1291 auto filtered = this->g.filter_nodes([](auto p) { return p->get_info() > 10; });
1292 EXPECT_EQ(filtered.size(), 2u);
1293}
1294
1296{
1297 auto filtered = this->g.filter_arcs([](auto a) { return a->get_info() > 1.5; });
1298 EXPECT_EQ(filtered.size(), 1u);
1299}
1300
1302{
1303 auto found = this->g.search_node([](auto p) { return p->get_info() == 20; });
1304 ASSERT_NE(found, nullptr);
1305 EXPECT_EQ(found->get_info(), 20);
1306}
1307
1309{
1310 auto found = this->g.search_arc([](auto a) { return a->get_info() == 2.5; });
1311 ASSERT_NE(found, nullptr);
1312 EXPECT_DOUBLE_EQ(found->get_info(), 2.5);
1313}
1314
1316{
1317 auto [matching, not_matching] = this->g.partition_nodes(
1318 [](auto p) { return p->get_info() > 10; });
1319 EXPECT_EQ(matching.size(), 2u);
1320 EXPECT_EQ(not_matching.size(), 1u);
1321}
1322
1324{
1325 auto [matching, not_matching] = this->g.partition_arcs(
1326 [](auto a) { return a->get_info() > 1.5; });
1327 EXPECT_EQ(matching.size(), 1u);
1328 EXPECT_EQ(not_matching.size(), 1u);
1329}
1330
1332{
1333 int sum = 0;
1334 this->g.for_each_node([&sum](auto p) { sum += p->get_info(); });
1335 EXPECT_EQ(sum, 60); // 10 + 20 + 30
1336}
1337
1339{
1340 double sum = 0;
1341 this->g.for_each_arc([&sum](auto a) { sum += a->get_info(); });
1342 EXPECT_DOUBLE_EQ(sum, 4.0); // 1.5 + 2.5
1343}
1344
1346{
1347 EXPECT_EQ(this->g.out_degree(this->n1), 1u);
1348 EXPECT_EQ(this->g.out_degree(this->n2), 1u);
1349 EXPECT_EQ(this->g.out_degree(this->n3), 0u);
1350}
1351
1353{
1354 auto outs = this->g.out_nodes(this->n1);
1355 EXPECT_EQ(outs.size(), 1u);
1356}
1357
1359{
1360 auto outs = this->g.out_arcs(this->n1);
1361 EXPECT_EQ(outs.size(), 1u);
1362}
1363
1365{
1366 auto min = this->g.min_arc();
1367 ASSERT_NE(min, nullptr);
1368 EXPECT_DOUBLE_EQ(min->get_info(), 1.5);
1369}
1370
1372{
1373 auto max = this->g.max_arc();
1374 ASSERT_NE(max, nullptr);
1375 EXPECT_DOUBLE_EQ(max->get_info(), 2.5);
1376}
1377
1378int main(int argc, char **argv)
1379{
1380 ::testing::InitGoogleTest(&argc, argv);
1381 return RUN_ALL_TESTS();
1382}
1383
int main()
long double w
Definition btreepic.C:153
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
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Node of Array_Graph
Definition tpl_agraph.H:64
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
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
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
Arc * min_arc(Node *p, Compare cmp=[](Arc *a, Arc *b) { return a->get_info()< b->get_info();}) const
Find the minimum arc adjacent to a node.
Definition graph-dry.H:2719
std::pair< DynList< Node * >, DynList< Node * > > partition_nodes(Operation op) const
Partition nodes into two groups based on a predicate.
Definition graph-dry.H:2815
Arc * max_arc(Node *p, Compare cmp=[](Arc *a, Arc *b) { return a->get_info()< b->get_info();}) const
Find the maximum arc adjacent to a node.
Definition graph-dry.H:2748
bool none_node(Operation &op) const
Determine if no node satisfies a condition.
Definition graph-dry.H:2539
DynList< Node * > adjacent_nodes(Node *p) const
Get all adjacent nodes (neighbors) of a node.
Definition graph-dry.H:2864
size_t count_arcs(Operation op=[](Arc *) { return true;}) const
Count the arcs satisfying a condition.
Definition graph-dry.H:2646
bool none_arc(Operation &op) const
Determine if no arc satisfies a condition.
Definition graph-dry.H:2568
size_t count_nodes(Operation op=[](Node *) { return true;}) const
Count the nodes satisfying a condition.
Definition graph-dry.H:2621
TestDigraph::Node * dn2
TestDigraph::Node * dn3
TestDigraph::Node * dn4
TestDigraph::Node * dn1
#define TEST(name)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
TEST_F(GraphFunctionalTest, NoneNodeReturnsTrueWhenNoMatch)
List_Digraph< IntNode, DoubleArc > ListDigraph
List_Graph< IntNode, DoubleArc > ListGraph
::testing::Types< ListGraph, SparseGraph, ArrayGraph > UndirectedGraphTypes
::testing::Types< ListDigraph, SparseDigraph, ArrayDigraph > DirectedGraphTypes
TYPED_TEST(UndirectedGraphTest, CountNodes)
TYPED_TEST_SUITE(UndirectedGraphTest, UndirectedGraphTypes)
size_t in_degree(typename GT::Node *p, SA sa=SA())
Compute the filtered in degree of node p.
Definition tpl_graph.H:2806
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
bool completed() const noexcept
Return true if all underlying iterators are finished.
Definition ah-zip.H:183
auto get_curr_ne() const noexcept(zip_detail::nothrow_curr_copy< Aleph::const_iterator_t< HeadC >, T > and zip_detail::nothrow_item_copy< T > and zip_detail::nothrow_tail_curr< ZipIterator< TailC... > >)
Return the current tuple (unchecked variant).
Definition ah-zip.H:216
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:192
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
Definition ah-zip.H:252
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:126
Node for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:93
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.