Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
euclidian_graph_common_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
41
42#include <algorithm>
43#include <cmath>
44#include <cstdint>
45#include <string>
46#include <tuple>
47#include <utility>
48#include <vector>
49
50#include <tpl_graph.H>
51#include <tpl_sgraph.H>
52
53using namespace Aleph;
54
55namespace
56{
58
59struct ArcSnapshot
60{
61 int x1;
62 int y1;
63 int x2;
64 int y2;
65 double w;
66
67 bool operator==(const ArcSnapshot & other) const
68 {
69 return std::tie(x1, y1, x2, y2, w) == std::tie(other.x1, other.y1, other.x2, other.y2, other.w);
70 }
71
72 bool operator<(const ArcSnapshot & other) const
73 {
74 return std::tie(x1, y1, x2, y2, w) < std::tie(other.x1, other.y1, other.x2, other.y2, other.w);
75 }
76};
77
78std::vector<std::pair<int, int>> collect_positions(const G & g)
79{
80 std::vector<std::pair<int, int>> pos;
81 pos.reserve(g.vsize());
82
83 for (Node_Iterator<G> it(g); it.has_curr(); it.next_ne())
84 {
85 auto * n = it.get_curr();
86 const auto & p = n->get_info();
87 pos.emplace_back(p.x, p.y);
88 }
89
90 std::sort(pos.begin(), pos.end());
91 return pos;
92}
93
94std::vector<ArcSnapshot> collect_arcs_normalized(const G & g)
95{
96 std::vector<ArcSnapshot> arcs;
97 arcs.reserve(g.esize());
98
99 for (Arc_Iterator<G> it(g); it.has_curr(); it.next_ne())
100 {
101 auto * a = it.get_curr();
102 auto * s = g.get_src_node(a);
103 auto * t = g.get_tgt_node(a);
104 const auto & ps = s->get_info();
105 const auto & pt = t->get_info();
106
107 int x1 = ps.x, y1 = ps.y;
108 int x2 = pt.x, y2 = pt.y;
109 if (std::tie(x2, y2) < std::tie(x1, y1))
110 std::swap(x1, x2), std::swap(y1, y2);
111
112 arcs.push_back({x1, y1, x2, y2, a->get_info()});
113 }
114
115 std::sort(arcs.begin(), arcs.end());
116 return arcs;
117}
118} // namespace
119
121{
122 // With w*h = 4, n=5 is impossible.
123 EXPECT_THROW((void)gen_random_euclidian_graph<G>(5, 1, 2, 2, 123u), std::domain_error);
124}
125
127{
128 constexpr int w = 40;
129 constexpr int h = 30;
130 const auto g = gen_random_euclidian_graph<G>(200, 300, w, h, 7u);
131
132 auto pos = collect_positions(g);
133 ASSERT_EQ(pos.size(), 200u);
134
135 // Uniqueness
136 EXPECT_EQ(std::unique(pos.begin(), pos.end()), pos.end());
137
138 // Range
139 for (const auto & [x, y] : pos)
140 {
141 EXPECT_GE(x, 0);
142 EXPECT_LT(x, w);
143 EXPECT_GE(y, 0);
144 EXPECT_LT(y, h);
145 }
146}
147
149{
150 constexpr int w = 60;
151 constexpr int h = 80;
152 const auto g = gen_random_euclidian_graph<G>(100, 200, w, h, 9u);
153
154 const int max_offset = std::max(1, int(std::ceil(std::hypot(double(w), double(h)))));
155
156 for (Arc_Iterator<G> it(g); it.has_curr(); it.next_ne())
157 {
158 auto * a = it.get_curr();
159 auto * s = g.get_src_node(a);
160 auto * t = g.get_tgt_node(a);
161 const auto & ps = s->get_info();
162 const auto & pt = t->get_info();
163
164 const double dist = std::hypot(double(ps.x - pt.x), double(ps.y - pt.y));
165 const double wgt = a->get_info();
166
167 EXPECT_GE(wgt, dist);
168 EXPECT_LT(wgt, dist + max_offset);
169 }
170}
171
173{
174 // Note: The random graph generator uses multiple RNGs (global rand_gen for
175 // node positions, internal RNG for arc selection) which may not produce
176 // byte-identical graphs. This test verifies structural consistency.
177 constexpr int w = 50;
178 constexpr int h = 50;
179 constexpr unsigned seed = 42u;
180
181 const auto g1 = gen_random_euclidian_graph<G>(120, 220, w, h, seed);
182 const auto g2 = gen_random_euclidian_graph<G>(120, 220, w, h, seed);
183
184 // Node positions should be identical (uses single RNG path)
186
187 // Graph structure should be consistent
188 EXPECT_EQ(g1.vsize(), g2.vsize()) << "Node count should match";
189 EXPECT_EQ(g1.esize(), g2.esize()) << "Arc count should match";
190
191 // Most arcs should match (allowing small variance due to internal RNG state)
194
195 size_t matching = 0;
196 for (const auto & a1 : arcs1)
197 for (const auto & a2 : arcs2)
198 if (a1.x1 == a2.x1 && a1.y1 == a2.y1 &&
199 a1.x2 == a2.x2 && a1.y2 == a2.y2 &&
200 std::abs(a1.w - a2.w) < 1e-9)
201 {
202 ++matching;
203 break;
204 }
205
206 // At least 99% of arcs should match
207 double match_ratio = static_cast<double>(matching) / arcs1.size();
208 EXPECT_GE(match_ratio, 0.99) << "Expected >= 99% arc match, got "
209 << (match_ratio * 100) << "%";
210}
211
212int main(int argc, char ** argv)
213{
214 ::testing::InitGoogleTest(&argc, argv);
215 return RUN_ALL_TESTS();
216}
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:142
int main()
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1206
#define TEST(name)
Common utilities for Euclidean graphs.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4103
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.