Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_stress_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
40#include <Dijkstra.H>
41#include <tpl_graph.H>
42
43#include <cstdlib>
44#include <limits>
45#include <vector>
46
47using namespace Aleph;
48
49namespace
50{
52using Node = Graph::Node;
53
54struct BinHeapTag
55{
56 template <class G, class D, class A>
57 using Heap = ArcHeap<G, D, A>;
58};
59
60using DijkstraInt =
62 Dft_Show_Arc<Graph>, BinHeapTag::template Heap>;
63
64static bool should_run_stress()
65{
66 return std::getenv("ALEPH_RUN_GRAPH_STRESS") != nullptr;
67}
68
69static size_t stress_nodes()
70{
71 const char *env = std::getenv("ALEPH_GRAPH_STRESS_NODES");
72 if (env == nullptr)
73 return 1000000;
74
75 char *end = nullptr;
76 const unsigned long long v = std::strtoull(env, &end, 10);
77 if (end == env || *end != '\0')
78 return 1000000;
79
80 return static_cast<size_t>(v);
81}
82} // namespace
83
85{
86 if (!should_run_stress())
87 GTEST_SKIP() << "Set ALEPH_RUN_GRAPH_STRESS=1 (and optionally ALEPH_GRAPH_STRESS_NODES=N) to enable.";
88
89 const size_t N = stress_nodes();
90 ASSERT_GE(N, 2u);
91
92 Graph g;
93 std::vector<Node *> nodes;
94 nodes.reserve(N);
95
96 for (size_t i = 0; i < N; ++i)
97 nodes.push_back(g.insert_node(static_cast<int>(i)));
98
99 for (size_t i = 0; i + 1 < N; ++i)
100 g.insert_arc(nodes[i], nodes[i + 1], 1);
101
103 const bool found = dij.paint_partial_min_paths_tree(g, nodes.front(), nodes.back());
105
106 const int dist = dij.get_distance(nodes.back());
107 ASSERT_EQ(dist, static_cast<int>(N - 1));
108}
Dijkstra's shortest path algorithm.
WeightedDigraph::Node Node
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:101
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Node Node
The graph type.
Definition tpl_graph.H:432
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
#define TEST(name)
#define N
Definition fib.C:294
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1119
ArcHeap< G, D, A > Heap
Definition astar_test.cc:60
Generic graph and digraph implementations.