38#include <gtest/gtest.h>
67 bool operator==(
const ArcSnapshot & other)
const
69 return std::tie(x1, y1, x2, y2, w) == std::tie(other.x1, other.y1, other.x2, other.y2, other.w);
72 bool operator<(
const ArcSnapshot & other)
const
74 return std::tie(x1, y1, x2, y2, w) < std::tie(other.x1, other.y1, other.x2, other.y2, other.w);
78std::vector<std::pair<int, int>> collect_positions(
const G & g)
80 std::vector<std::pair<int, int>> pos;
81 pos.reserve(g.
vsize());
85 auto * n = it.get_curr();
86 const auto & p = n->get_info();
87 pos.emplace_back(p.x, p.y);
90 std::sort(pos.begin(), pos.end());
94std::vector<ArcSnapshot> collect_arcs_normalized(
const G & g)
96 std::vector<ArcSnapshot>
arcs;
101 auto * a = it.get_curr();
104 const auto & ps = s->get_info();
105 const auto & pt = t->get_info();
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);
112 arcs.push_back({x1,
y1, x2, y2, a->get_info()});
115 std::sort(
arcs.begin(),
arcs.end());
120TEST(EuclidianGraphCommon, RejectsImpossibleUniquePlacement)
123 EXPECT_THROW((
void)gen_random_euclidian_graph<G>(5, 1, 2, 2, 123u), std::domain_error);
126TEST(EuclidianGraphCommon, NodeCoordinatesAreUniqueAndInRange)
128 constexpr int w = 40;
129 constexpr int h = 30;
130 const auto g = gen_random_euclidian_graph<G>(200, 300,
w,
h, 7u);
132 auto pos = collect_positions(g);
133 ASSERT_EQ(pos.size(), 200u);
136 EXPECT_EQ(std::unique(pos.begin(), pos.end()), pos.end());
139 for (
const auto & [x,
y] : pos)
148TEST(EuclidianGraphCommon, ArcWeightsAreWithinBounds)
150 constexpr int w = 60;
151 constexpr int h = 80;
152 const auto g = gen_random_euclidian_graph<G>(100, 200,
w,
h, 9u);
154 const int max_offset = std::max(1,
int(std::ceil(std::hypot(
double(
w),
double(
h)))));
158 auto * a = it.get_curr();
161 const auto & ps = s->get_info();
162 const auto & pt = t->get_info();
164 const double dist = std::hypot(
double(ps.x - pt.x),
double(ps.y - pt.y));
165 const double wgt = a->get_info();
167 EXPECT_GE(wgt, dist);
168 EXPECT_LT(wgt, dist + max_offset);
172TEST(EuclidianGraphCommon, ConsistentStructureForFixedSeed)
177 constexpr int w = 50;
178 constexpr int h = 50;
179 constexpr unsigned seed = 42u;
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);
185 EXPECT_EQ(collect_positions(g1), collect_positions(g2));
188 EXPECT_EQ(g1.vsize(), g2.vsize()) <<
"Node count should match";
189 EXPECT_EQ(g1.esize(), g2.esize()) <<
"Arc count should match";
192 auto arcs1 = collect_arcs_normalized(g1);
193 auto arcs2 = collect_arcs_normalized(g2);
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)
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) <<
"%";
212int main(
int argc,
char ** argv)
214 ::testing::InitGoogleTest(&argc, argv);
215 return RUN_ALL_TESTS();
bool operator<(const Time &l, const Time &r)
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Arc for graphs implemented with simple adjacency lists.
Graph implemented with double-linked adjacency lists.
Graph class implemented with singly-linked adjacency lists.
Filtered iterator on the nodes of a graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
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)
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
Filtered iterator on all the arcs of a graph.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.