Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
net_utils_test.cc
Go to the documentation of this file.
1/* Tests for net_utils.H - Network flow utilities
2 *
3 * Tests cover:
4 * - Random network generation
5 * - Grid network generation
6 * - Bipartite network generation
7 * - DOT export
8 * - JSON export/import
9 * - DIMACS export/import
10 * - Benchmarking utilities
11 */
12
13#include <gtest/gtest.h>
14#include <net_utils.H>
15#include <tpl_maxflow.H>
16#include <filesystem>
17#include <fstream>
18#include <string>
19#if defined(_WIN32)
20# include <process.h>
21#else
22# include <unistd.h>
23#endif
24
25using namespace Aleph;
26
27// Network type for tests
30
31class NetUtilsTest : public ::testing::Test
32{
33protected:
34 std::filesystem::path dot_file;
35 std::filesystem::path json_file;
36 std::filesystem::path dimacs_file;
37
38 static long long process_id() noexcept
39 {
40#if defined(_WIN32)
41 return static_cast<long long>(_getpid());
42#else
43 return static_cast<long long>(getpid());
44#endif
45 }
46
47 void SetUp() override
48 {
49 const auto *test_info = ::testing::UnitTest::GetInstance()->current_test_info();
50 std::string base = std::string("alephw_") + test_info->test_suite_name() + "_" +
51 test_info->name() + "_" + std::to_string(process_id());
52
53 for (auto &ch : base)
54 if (ch == '/' || ch == '\\' || ch == ' ')
55 ch = '_';
56
57 auto dir = std::filesystem::temp_directory_path();
58 dot_file = dir / (base + ".dot");
59 json_file = dir / (base + ".json");
60 dimacs_file = dir / (base + ".dimacs");
61 }
62
63 // Cleanup temp files
64 void TearDown() override
65 {
66 std::error_code ec;
67 std::filesystem::remove(dot_file, ec);
68 std::filesystem::remove(json_file, ec);
69 std::filesystem::remove(dimacs_file, ec);
70 }
71};
72
73
74//==============================================================================
75// Random Network Generator Tests
76//==============================================================================
77
78TEST_F(NetUtilsTest, GenerateRandomNetworkBasic)
79{
80 NetworkGenParams params;
81 params.num_nodes = 10;
82 params.num_arcs = 20;
83 params.min_capacity = 1;
84 params.max_capacity = 100;
85 params.seed = 42;
86
87 auto net = generate_random_network<TestNet>(params);
88
89 EXPECT_EQ(net.vsize(), 10);
90 EXPECT_GE(net.esize(), params.num_nodes - 1); // At least path arcs
91 // Note: Random networks may have multiple sources/sinks
92 // Use make_super_source/make_super_sink if needed
93}
94
95TEST_F(NetUtilsTest, GenerateRandomNetworkConvenience)
96{
97 auto net = generate_random_network<TestNet>(50, 200, 10, 1000, 123);
98
99 EXPECT_EQ(net.vsize(), 50);
100 EXPECT_GT(net.esize(), 0);
101
102 // Verify capacity bounds
103 for (Arc_Iterator<TestNet> it(net); it.has_curr(); it.next_ne())
104 {
105 auto arc = it.get_curr();
106 EXPECT_GE(arc->cap, 10.0);
107 EXPECT_LE(arc->cap, 1000.0);
108 }
109}
110
111TEST_F(NetUtilsTest, GenerateRandomNetworkConnected)
112{
113 NetworkGenParams params;
114 params.num_nodes = 20;
115 params.num_arcs = 50;
116 params.ensure_connected = true;
117 params.seed = 42;
118
119 auto net = generate_random_network<TestNet>(params);
120
121 // Verify connectivity was attempted
122 EXPECT_GE(net.esize(), params.num_nodes - 1);
123}
124
125
126//==============================================================================
127// Grid Network Generator Tests
128//==============================================================================
129
130TEST_F(NetUtilsTest, GenerateGridNetwork)
131{
132 // Use unidirectional to ensure single source/sink
133 auto net = generate_grid_network<TestNet>(5, 5, 10.0, false);
134
135 EXPECT_EQ(net.vsize(), 25);
136 EXPECT_TRUE(net.is_single_source());
137 EXPECT_TRUE(net.is_single_sink());
138
139 // Unidirectional: 4*5 + 5*4 = 40 arcs
140 EXPECT_EQ(net.esize(), 40);
141}
142
143TEST_F(NetUtilsTest, GenerateGridNetworkUnidirectional)
144{
145 auto net = generate_grid_network<TestNet>(3, 3, 5.0, false);
146
147 EXPECT_EQ(net.vsize(), 9);
148
149 // Unidirectional: 2*3 + 3*2 = 12 arcs
150 EXPECT_EQ(net.esize(), 12);
151}
152
153TEST_F(NetUtilsTest, GenerateGridNetworkFlowComputation)
154{
155 // Use unidirectional to ensure single source/sink
156 auto net = generate_grid_network<TestNet>(4, 4, 100.0, false);
157
158 auto flow = dinic_maximum_flow(net);
159 EXPECT_GT(flow, 0.0);
160}
161
162
163//==============================================================================
164// Bipartite Network Generator Tests
165//==============================================================================
166
167TEST_F(NetUtilsTest, GenerateBipartiteNetwork)
168{
169 auto net = generate_bipartite_network<TestNet>(10, 10, 0.5, 42);
170
171 // Source + sink + 10 left + 10 right = 22 nodes
172 EXPECT_EQ(net.vsize(), 22);
173 EXPECT_TRUE(net.is_single_source());
174 EXPECT_TRUE(net.is_single_sink());
175}
176
177TEST_F(NetUtilsTest, GenerateBipartiteNetworkMatching)
178{
179 auto net = generate_bipartite_network<TestNet>(5, 5, 1.0, 42);
180
181 // With probability 1.0, all edges exist
182 auto flow = dinic_maximum_flow(net);
183
184 // Max matching can be at most min(left, right) = 5
185 EXPECT_LE(flow, 5.0);
186}
187
188
189//==============================================================================
190// Layered Network Generator Tests
191//==============================================================================
192
193TEST_F(NetUtilsTest, GenerateLayeredNetwork)
194{
195 // Use probability 1.0 to ensure all nodes are connected
196 std::vector<size_t> layers = {1, 5, 10, 5, 1};
197 auto net = generate_layered_network<TestNet>(layers, 10.0, 1.0, 42);
198
199 EXPECT_EQ(net.vsize(), 22);
200 EXPECT_GT(net.esize(), 0);
201}
202
203TEST_F(NetUtilsTest, GenerateLayeredNetworkFlow)
204{
205 std::vector<size_t> layers = {1, 3, 3, 1};
206 auto net = generate_layered_network<TestNet>(layers, 10.0, 1.0, 42);
207
208 auto flow = dinic_maximum_flow(net);
209 EXPECT_GT(flow, 0.0);
210}
211
212TEST_F(NetUtilsTest, GenerateLayeredNetworkRejectsEmptyInteriorLayer)
213{
214 std::vector<size_t> layers = {1, 0, 1};
215 EXPECT_THROW(
216 (generate_layered_network<TestNet>(layers, 10.0, 0.5, 42)),
217 std::invalid_argument);
218}
219
220
221//==============================================================================
222// DOT Export Tests
223//==============================================================================
224
225TEST_F(NetUtilsTest, ExportToDotBasic)
226{
227 auto net = generate_grid_network<TestNet>(3, 3, 10.0, false); // unidirectional
229
230 export_network_to_dot(net, dot_file.string());
231
232 std::ifstream file(dot_file);
233 EXPECT_TRUE(file.good());
234
235 std::string content((std::istreambuf_iterator<char>(file)),
236 std::istreambuf_iterator<char>());
237
238 EXPECT_TRUE(content.find("digraph") != std::string::npos);
239 EXPECT_TRUE(content.find("->") != std::string::npos);
240}
241
242TEST_F(NetUtilsTest, ExportToDotWithOptions)
243{
244 auto net = generate_grid_network<TestNet>(2, 2, 10.0, false); // unidirectional
246
248 options.show_flow = true;
249 options.show_capacity = true;
250 options.highlight_saturated = true;
251
252 export_network_to_dot(net, dot_file.string(), options);
253
254 std::ifstream file(dot_file);
255 EXPECT_TRUE(file.good());
256}
257
258TEST_F(NetUtilsTest, NetworkToDotString)
259{
260 auto net = generate_grid_network<TestNet>(2, 2, 10.0);
261
262 std::string dot = network_to_dot_string(net);
263
264 EXPECT_TRUE(dot.find("digraph") != std::string::npos);
265 EXPECT_TRUE(dot.find("rankdir=LR") != std::string::npos);
266}
267
268
269//==============================================================================
270// JSON Export Tests
271//==============================================================================
272
273TEST_F(NetUtilsTest, ExportToJsonBasic)
274{
275 auto net = generate_grid_network<TestNet>(2, 2, 10.0);
276
277 export_network_to_json(net, json_file.string());
278
279 std::ifstream file(json_file);
280 EXPECT_TRUE(file.good());
281
282 std::string content((std::istreambuf_iterator<char>(file)),
283 std::istreambuf_iterator<char>());
284
285 EXPECT_TRUE(content.find("\"num_nodes\"") != std::string::npos);
286 EXPECT_TRUE(content.find("\"arcs\"") != std::string::npos);
287 EXPECT_TRUE(content.find("\"cap\"") != std::string::npos);
288}
289
290TEST_F(NetUtilsTest, NetworkToJsonString)
291{
292 auto net = generate_grid_network<TestNet>(2, 2, 10.0, false); // unidirectional
293
294 std::string json = network_to_json_string(net);
295
296 EXPECT_TRUE(json.find("\"num_nodes\": 4") != std::string::npos);
297 EXPECT_TRUE(json.find("\"source\"") != std::string::npos);
298 EXPECT_TRUE(json.find("\"sink\"") != std::string::npos);
299}
300
301
302//==============================================================================
303// DIMACS Export/Import Tests
304//==============================================================================
305
306TEST_F(NetUtilsTest, ExportToDimacsBasic)
307{
308 auto net = generate_grid_network<TestNet>(3, 3, 10.0, false);
309
310 export_network_to_dimacs(net, dimacs_file.string());
311
312 std::ifstream file(dimacs_file);
313 EXPECT_TRUE(file.good());
314
315 std::string content((std::istreambuf_iterator<char>(file)),
316 std::istreambuf_iterator<char>());
317
318 EXPECT_TRUE(content.find("p max 9") != std::string::npos);
319 EXPECT_TRUE(content.find("n") != std::string::npos);
320 EXPECT_TRUE(content.find("a") != std::string::npos);
321}
322
323TEST_F(NetUtilsTest, DimacsRoundTrip)
324{
325 // Create and export
326 auto net1 = generate_grid_network<TestNet>(3, 3, 10.0, false);
327 export_network_to_dimacs(net1, dimacs_file.string());
328
329 // Import
330 auto net2 = import_network_from_dimacs<TestNet>(dimacs_file.string());
331
332 EXPECT_EQ(net2.vsize(), net1.vsize());
333 EXPECT_EQ(net2.esize(), net1.esize());
334 EXPECT_TRUE(net2.is_single_source());
335 EXPECT_TRUE(net2.is_single_sink());
336}
337
338TEST_F(NetUtilsTest, DimacsFlowEquivalence)
339{
340 // Create network, compute flow, export
341 auto net1 = generate_grid_network<TestNet>(4, 4, 10.0, false);
342 auto flow1 = dinic_maximum_flow(net1);
343
344 export_network_to_dimacs(net1, dimacs_file.string());
345
346 // Import and compute flow
347 auto net2 = import_network_from_dimacs<TestNet>(dimacs_file.string());
348 auto flow2 = dinic_maximum_flow(net2);
349
350 // Flows should be equal
351 EXPECT_DOUBLE_EQ(flow1, flow2);
352}
353
354// Comments, blank lines and lines of other types are skipped
355TEST_F(NetUtilsTest, DimacsImportSkipsCommentsAndBlankLines)
356{
357 std::ofstream(dimacs_file) << "c a network\n\n \np max 3 2\nc between\n"
358 << "n 1 s\nn 3 t\na 1 2 5\nx unknown\na 2 3 4\n";
359 auto net = import_network_from_dimacs<TestNet>(dimacs_file.string());
360 EXPECT_EQ(net.vsize(), 3u);
361 EXPECT_EQ(net.esize(), 2u);
362 EXPECT_DOUBLE_EQ(dinic_maximum_flow(net), 4.0);
363}
364
365// A malformed file throws instead of indexing past the nodes or reading
366// fields that were never set, which crashed
367TEST_F(NetUtilsTest, DimacsImportRejectsMalformedFiles)
368{
369 for (const char *text : {"p max 3 1\na 1 9 5\n", // node out of range
370 "p max 3 1\na 0 2 5\n", // nodes start at 1
371 "p max 3 1\na 1 -1 5\n", // negative node
372 "a 1 2 5\np max 3 1\n", // arc before the problem
373 "n 1 s\np max 3 1\n", // node before the problem
374 "p max\na 1 2 5\n", // incomplete problem line
375 "p max 3 1\np max 3 1\n", // a second problem line
376 "p max 3 1\na 1 2\n", // arc without capacity
377 "p max 3 1\nn 4 s\n", // designated node out of range
378 "p max 3 1\nn 1 x\n"}) // neither s nor t
379 {
380 std::ofstream(dimacs_file) << text;
381 EXPECT_THROW(import_network_from_dimacs<TestNet>(dimacs_file.string()),
382 std::runtime_error) << text;
383 }
384}
385
386
387//==============================================================================
388// Benchmark Utilities Tests
389//==============================================================================
390
391TEST_F(NetUtilsTest, BenchmarkMaxFlow)
392{
393 auto net = generate_grid_network<TestNet>(5, 5, 10.0, false); // unidirectional
394
395 auto result = benchmark_maxflow(net, [](auto& n) {
396 return dinic_maximum_flow(n);
397 }, "Dinic");
398
399 EXPECT_GT(result.flow_value, 0.0);
400 EXPECT_GE(result.elapsed_ms, 0.0);
401 EXPECT_EQ(result.algorithm_name, "Dinic");
402}
403
404TEST_F(NetUtilsTest, PrintBenchmarkResults)
405{
406 std::vector<MaxFlowBenchmarkResult<double>> results;
407
408 results.push_back({100.0, 1.5, "Algorithm A"});
409 results.push_back({100.0, 2.3, "Algorithm B"});
410
411 // Just verify it doesn't crash
412 testing::internal::CaptureStdout();
414 std::string output = testing::internal::GetCapturedStdout();
415
416 EXPECT_TRUE(output.find("Algorithm A") != std::string::npos);
417 EXPECT_TRUE(output.find("Algorithm B") != std::string::npos);
418}
419
420
421//==============================================================================
422// Stress Tests
423//==============================================================================
424
425TEST_F(NetUtilsTest, LargeRandomNetwork)
426{
427 NetworkGenParams params;
428 params.num_nodes = 100;
429 params.num_arcs = 500;
430 params.seed = 42;
431
432 auto net = generate_random_network<TestNet>(params);
433
434 EXPECT_EQ(net.vsize(), 100);
435 EXPECT_GE(net.esize(), 99); // At least connected path
436 // Don't run flow since multi-source/multi-sink
437}
438
439TEST_F(NetUtilsTest, LargeGridNetwork)
440{
441 auto net = generate_grid_network<TestNet>(20, 20, 100.0, false); // unidirectional
442
443 EXPECT_EQ(net.vsize(), 400);
444
445 // Should complete in reasonable time
446 auto flow = dinic_maximum_flow(net);
447 EXPECT_GT(flow, 0.0);
448}
449
450
451int main(int argc, char** argv)
452{
453 ::testing::InitGoogleTest(&argc, argv);
454 return RUN_ALL_TESTS();
455}
int main()
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
std::filesystem::path json_file
static long long process_id() noexcept
std::filesystem::path dot_file
void TearDown() override
std::filesystem::path dimacs_file
void SetUp() override
Main namespace for Aleph-w library functions.
void export_network_to_dimacs(const Net &net, const std::string &filename)
Export network to DIMACS max-flow format.
Definition net_utils.H:821
void export_network_to_dot(const Net &net, const std::string &filename, const DotExportOptions &options=DotExportOptions())
Export network to DOT format for GraphViz visualization.
Definition net_utils.H:485
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
void export_network_to_json(const Net &net, const std::string &filename)
Export network to JSON format.
Definition net_utils.H:695
std::string network_to_json_string(const Net &net)
Export network to JSON string.
Definition net_utils.H:751
MaxFlowBenchmarkResult< typename Net::Flow_Type > benchmark_maxflow(Net &net, Algo algo, const std::string &name)
Run and time a max-flow algorithm.
Definition net_utils.H:971
void print_benchmark_results(const std::vector< MaxFlowBenchmarkResult< Flow_Type > > &results)
Print benchmark results.
Definition net_utils.H:990
std::string network_to_dot_string(const Net &net, const DotExportOptions &options=DotExportOptions())
Generate DOT string for network (instead of file).
Definition net_utils.H:580
Network flow utilities: generators, visualization, serialization.
TEST_F(NetUtilsTest, GenerateRandomNetworkBasic)
static struct argp_option options[]
Definition ntreepic.C:1890
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Options for DOT export.
Definition net_utils.H:450
bool show_flow
Show flow values on arcs.
Definition net_utils.H:451
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:160
Flow network implemented with adjacency lists.
Definition tpl_net.H:311
Parameters for random network generation.
Definition net_utils.H:102
double max_capacity
Maximum arc capacity.
Definition net_utils.H:106
size_t num_arcs
Target number of arcs.
Definition net_utils.H:104
bool ensure_connected
Ensure source can reach sink.
Definition net_utils.H:109
double min_capacity
Minimum arc capacity.
Definition net_utils.H:105
unsigned seed
Random seed (0 = use time)
Definition net_utils.H:110
size_t num_nodes
Number of nodes.
Definition net_utils.H:103
Advanced maximum flow algorithms.
ofstream output
Definition writeHeap.C:215
ofstream file
Definition writeRb.C:51