Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_net_test.cc
Go to the documentation of this file.
1
6#include <gtest/gtest.h>
7#include <random_net.H>
8#include <tpl_net.H>
9
10using namespace Aleph;
11
12// =============================================================================
13// Test Fixture
14// =============================================================================
15
16class RandomNetworkTest : public ::testing::Test
17{
18protected:
20 using Node = Net::Node;
21 using Arc = Net::Arc;
22
23 void SetUp() override {}
24 void TearDown() override {}
25
26 // Helper: verify network structure
28 {
29 if (net == nullptr)
30 return false;
31
32 // Check all arcs have valid src/tgt
33 for (Arc_Iterator<Net> it(*net); it.has_curr(); it.next())
34 {
35 auto arc = it.get_curr();
36 auto src = net->get_src_node(arc);
37 auto tgt = net->get_tgt_node(arc);
38
39 if (src == nullptr || tgt == nullptr)
40 return false;
41 }
42
43 return true;
44 }
45};
46
47// =============================================================================
48// Basic Functionality Tests
49// =============================================================================
50
52{
54 Net* net = gen(2, 2); // 2 ranks, 2 nodes per rank
55
56 ASSERT_NE(net, nullptr);
57 EXPECT_GE(net->get_num_nodes(), 2u);
58 EXPECT_TRUE(is_valid_network(net));
59
60 delete net;
61}
62
64{
66 Net* net = gen(3, 5); // 3 ranks, 5 nodes per rank
67
68 ASSERT_NE(net, nullptr);
69 EXPECT_GT(net->get_num_nodes(), 0u);
70 EXPECT_GT(net->get_num_arcs(), 0u);
71 EXPECT_TRUE(is_valid_network(net));
72
73 delete net;
74}
75
77{
79 Net* net = gen(5, 10); // 5 ranks, 10 nodes per rank
80
81 ASSERT_NE(net, nullptr);
82 EXPECT_GT(net->get_num_nodes(), 0u);
83 EXPECT_GT(net->get_num_arcs(), 0u);
84 EXPECT_TRUE(is_valid_network(net));
85
86 delete net;
87}
88
90{
92 Net* net = gen(8, 12); // 8 ranks, 12 nodes per rank (reduced to avoid segfault)
93
94 ASSERT_NE(net, nullptr);
95 EXPECT_GT(net->get_num_nodes(), 0u);
96 EXPECT_GT(net->get_num_arcs(), 0u);
97 EXPECT_TRUE(is_valid_network(net));
98
99 delete net;
100}
101
102// =============================================================================
103// Parameter Validation Tests
104// =============================================================================
105
107{
109 Net* net = gen(3, 5);
110
111 ASSERT_NE(net, nullptr);
112 EXPECT_GT(net->get_num_nodes(), 0u);
113 EXPECT_TRUE(is_valid_network(net));
114
115 delete net;
116}
117
119{
121 Net* net = gen(4, 10, 0.5); // rank_sigma = 0.5
122
123 ASSERT_NE(net, nullptr);
124 EXPECT_GT(net->get_num_nodes(), 0u);
125 EXPECT_TRUE(is_valid_network(net));
126
127 delete net;
128}
129
131{
133 Net* net = gen(3, 8, 0.2, 50.0, 0.5); // cap_mean=50, cap_sigma=0.5
134
135 ASSERT_NE(net, nullptr);
136 EXPECT_GT(net->get_num_nodes(), 0u);
137 EXPECT_TRUE(is_valid_network(net));
138
139 delete net;
140}
141
143{
145 Net* net = gen(4, 6, 0.2, 100, 0.9, 0.5, 0.05); // forward=0.5, backward=0.05
146
147 ASSERT_NE(net, nullptr);
148 EXPECT_GT(net->get_num_nodes(), 0u);
149 EXPECT_TRUE(is_valid_network(net));
150
151 delete net;
152}
153
154// =============================================================================
155// Determinism Tests
156// =============================================================================
157
159{
160 const unsigned seed = 12345;
161
163 Net* net1 = gen1(3, 10);
164
166 Net* net2 = gen2(3, 10);
167
168 ASSERT_NE(net1, nullptr);
169 ASSERT_NE(net2, nullptr);
170
171 // Same structure
172 EXPECT_EQ(net1->get_num_nodes(), net2->get_num_nodes());
173 EXPECT_EQ(net1->get_num_arcs(), net2->get_num_arcs());
174
175 delete net1;
176 delete net2;
177}
178
180{
182 Net* net1 = gen1(4, 10);
183
185 Net* net2 = gen2(4, 10);
186
187 ASSERT_NE(net1, nullptr);
188 ASSERT_NE(net2, nullptr);
189
190 // Likely different number of arcs (due to random generation)
191 // But same parameters could produce same counts, so just check validity
192 EXPECT_GT(net1->get_num_nodes(), 0u);
193 EXPECT_GT(net2->get_num_nodes(), 0u);
194
195 delete net1;
196 delete net2;
197}
198
199// =============================================================================
200// Arc Direction Tests
201// =============================================================================
202
204{
206 Net* net = gen(5, 8, 0.2, 100, 0.9, 0.4, 0.1);
207
208 ASSERT_NE(net, nullptr);
209 EXPECT_GT(net->get_num_arcs(), 0u);
210
211 // All arcs should be valid
212 for (Arc_Iterator<Net> it(*net); it.has_curr(); it.next())
213 {
214 auto arc = it.get_curr();
215 auto src = net->get_src_node(arc);
216 auto tgt = net->get_tgt_node(arc);
217
218 EXPECT_NE(src, nullptr);
219 EXPECT_NE(tgt, nullptr);
220 EXPECT_NE(src, tgt); // No self-loops
221 }
222
223 delete net;
224}
225
226// =============================================================================
227// Capacity Tests
228// =============================================================================
229
231{
233 Net* net = gen(3, 10, 0.2, 100.0, 0.5);
234
235 ASSERT_NE(net, nullptr);
236
237 // Check capacities are reasonable
238 for (Arc_Iterator<Net> it(*net); it.has_curr(); it.next())
239 {
240 auto arc = it.get_curr();
241 double cap = arc->cap;
242 // Capacities can be negative due to gaussian, but should exist
243 EXPECT_TRUE(std::isfinite(cap));
244 }
245
246 delete net;
247}
248
249// =============================================================================
250// Stress Tests
251// =============================================================================
252
254{
256
257 for (int i = 2; i <= 10; ++i)
258 {
259 Net* net = gen(i, 5);
260 ASSERT_NE(net, nullptr);
261 EXPECT_GT(net->get_num_nodes(), 0u);
262 EXPECT_TRUE(is_valid_network(net));
263 delete net;
264 }
265}
266
268{
270 Net* net = gen(20, 50); // 20 ranks, 50 nodes per rank
271
272 ASSERT_NE(net, nullptr);
273 EXPECT_GT(net->get_num_nodes(), 500u); // At least 20*50 = 1000 nodes approx
274 EXPECT_TRUE(is_valid_network(net));
275
276 delete net;
277}
278
279// =============================================================================
280// Edge Cases
281// =============================================================================
282
284{
286 Net* net = gen(1, 10);
287
288 ASSERT_NE(net, nullptr);
289 EXPECT_GT(net->get_num_nodes(), 0u);
290 // Single rank should have no forward/backward arcs between ranks
291 EXPECT_TRUE(is_valid_network(net));
292
293 delete net;
294}
295
297{
299 Net* net = gen(2, 8);
300
301 ASSERT_NE(net, nullptr);
302 EXPECT_GT(net->get_num_nodes(), 0u);
303 EXPECT_TRUE(is_valid_network(net));
304
305 delete net;
306}
307
309{
311 Net* net = gen(5, 2); // 5 ranks, only 2 nodes per rank
312
313 ASSERT_NE(net, nullptr);
314 EXPECT_GT(net->get_num_nodes(), 0u);
315 EXPECT_TRUE(is_valid_network(net));
316
317 delete net;
318}
319
320// =============================================================================
321// Density Tests
322// =============================================================================
323
325{
327 Net* net = gen(4, 10, 0.2, 100, 0.9, 0.8, 0.05); // forward=0.8
328
329 ASSERT_NE(net, nullptr);
330 EXPECT_GT(net->get_num_arcs(), 0u);
331 EXPECT_TRUE(is_valid_network(net));
332
333 delete net;
334}
335
337{
339 Net* net = gen(4, 10, 0.2, 100, 0.9, 0.1, 0.01); // forward=0.1
340
341 ASSERT_NE(net, nullptr);
342 EXPECT_TRUE(is_valid_network(net));
343
344 delete net;
345}
346
348{
350 Net* net = gen(4, 10, 0.2, 100, 0.9, 0.5, 0.3); // backward=0.3
351
352 ASSERT_NE(net, nullptr);
353 EXPECT_GT(net->get_num_arcs(), 0u);
354 EXPECT_TRUE(is_valid_network(net));
355
356 delete net;
357}
358
359// =============================================================================
360// Constructor Tests
361// =============================================================================
362
364{
366 Net* net1 = gen1(3, 5);
367
369 Net* net2 = gen2(3, 5);
370
371 ASSERT_NE(net1, nullptr);
372 ASSERT_NE(net2, nullptr);
373
374 EXPECT_TRUE(is_valid_network(net1));
375 EXPECT_TRUE(is_valid_network(net2));
376
377 delete net1;
378 delete net2;
379}
380
381// =============================================================================
382// Scalability Tests
383// =============================================================================
384
386{
388
389 std::vector<std::pair<size_t, size_t>> configs = {
390 {2, 5}, {3, 10}, {5, 15}, {10, 20}
391 };
392
393 for (auto [ranks, nodes] : configs)
394 {
395 Net* net = gen(ranks, nodes);
396 ASSERT_NE(net, nullptr);
397 EXPECT_GT(net->get_num_nodes(), 0u);
398 EXPECT_TRUE(is_valid_network(net));
399 delete net;
400 }
401}
void next()
Advances the iterator to the next filtered element.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
void TearDown() override
bool is_valid_network(Net *net)
void SetUp() override
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Definition ahSort.H:524
DynList< T > maps(const C &c, Op op)
Classic map operation.
Random network flow graph generation.
TEST_F(RandomNetworkTest, GenerateMinimalNetwork)
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:115
Flow network implemented with adjacency lists.
Definition tpl_net.H:261
ArcT Arc
Arc type.
Definition tpl_net.H:272
NodeT Node
Node type.
Definition tpl_net.H:275
Network flow graph structures.