Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
euclidian_graph_test.cc
Go to the documentation of this file.
1
6#include <gtest/gtest.h>
8#include <point.H>
9
10using namespace Aleph;
11
12// =============================================================================
13// Type Definitions
14// =============================================================================
15
20
21// =============================================================================
22// Test Fixture
23// =============================================================================
24
25class EuclidianGraphTest : public ::testing::Test
26{
27protected:
28 void SetUp() override {}
29 void TearDown() override {}
30};
31
32// =============================================================================
33// Node Tests
34// =============================================================================
35
36TEST_F(EuclidianGraphTest, NodeDefaultConstructor)
37{
38 ENode node;
39 Point p = node.get_position();
40 EXPECT_EQ(p.get_x(), 0.0);
41 EXPECT_EQ(p.get_y(), 0.0);
42}
43
45{
46 ENode node(42);
47 EXPECT_EQ(node.get_info(), 42);
48}
49
50TEST_F(EuclidianGraphTest, NodeWithPosition)
51{
52 Point p(10.5, 20.3);
53 ENode node(p);
54
55 Point retrieved = node.get_position();
56 EXPECT_DOUBLE_EQ(retrieved.get_x().get_d(), 10.5);
57 EXPECT_DOUBLE_EQ(retrieved.get_y().get_d(), 20.3);
58}
59
60TEST_F(EuclidianGraphTest, NodeWithInfoAndPosition)
61{
62 Point p(5.0, 15.0);
63 ENode node(100, p);
64
65 EXPECT_EQ(node.get_info(), 100);
66 Point retrieved = node.get_position();
67 EXPECT_DOUBLE_EQ(retrieved.get_x().get_d(), 5.0);
68 EXPECT_DOUBLE_EQ(retrieved.get_y().get_d(), 15.0);
69}
70
71TEST_F(EuclidianGraphTest, NodeCopyConstructor)
72{
73 Point p(7.5, 8.5);
74 ENode node1(50, p);
75 ENode node2(&node1);
76
77 EXPECT_EQ(node2.get_info(), 50);
78 Point retrieved = node2.get_position();
79 EXPECT_DOUBLE_EQ(retrieved.get_x().get_d(), 7.5);
80 EXPECT_DOUBLE_EQ(retrieved.get_y().get_d(), 8.5);
81}
82
83// =============================================================================
84// Arc Tests
85// =============================================================================
86
87TEST_F(EuclidianGraphTest, ArcDefaultConstructor)
88{
89 EArc arc;
90 EXPECT_EQ(arc.get_info(), 0.0);
91}
92
94{
95 EArc arc(3.14);
96 EXPECT_DOUBLE_EQ(arc.get_info(), 3.14);
97}
98
99// =============================================================================
100// Graph Construction Tests
101// =============================================================================
102
104{
105 EGraph g;
106 EXPECT_EQ(g.get_num_nodes(), 0u);
107 EXPECT_EQ(g.get_num_arcs(), 0u);
108}
109
110TEST_F(EuclidianGraphTest, InsertNodeWithInfo)
111{
112 EGraph g;
113 auto node = g.insert_node(42);
114
115 ASSERT_NE(node, nullptr);
116 EXPECT_EQ(node->get_info(), 42);
117 EXPECT_EQ(g.get_num_nodes(), 1u);
118}
119
120TEST_F(EuclidianGraphTest, InsertNodeWithPosition)
121{
122 EGraph g;
123 Point p(10.0, 20.0);
124 auto node = g.insert_node(p);
125
126 ASSERT_NE(node, nullptr);
127 Point retrieved = node->get_position();
128 EXPECT_DOUBLE_EQ(retrieved.get_x().get_d(), 10.0);
129 EXPECT_DOUBLE_EQ(retrieved.get_y().get_d(), 20.0);
130 EXPECT_EQ(g.get_num_nodes(), 1u);
131}
132
133TEST_F(EuclidianGraphTest, InsertNodeWithInfoAndPosition)
134{
135 EGraph g;
136 Point p(5.5, 7.5);
137 auto node = g.insert_node(100, p);
138
139 ASSERT_NE(node, nullptr);
140 EXPECT_EQ(node->get_info(), 100);
141 Point retrieved = node->get_position();
142 EXPECT_DOUBLE_EQ(retrieved.get_x().get_d(), 5.5);
143 EXPECT_DOUBLE_EQ(retrieved.get_y().get_d(), 7.5);
144 EXPECT_EQ(g.get_num_nodes(), 1u);
145}
146
147TEST_F(EuclidianGraphTest, InsertMultipleNodes)
148{
149 EGraph g;
150
151 auto n1 = g.insert_node(Point(0.0, 0.0));
152 auto n2 = g.insert_node(Point(10.0, 0.0));
153 auto n3 = g.insert_node(Point(10.0, 10.0));
154
155 ASSERT_NE(n1, nullptr);
156 ASSERT_NE(n2, nullptr);
157 ASSERT_NE(n3, nullptr);
158 EXPECT_EQ(g.get_num_nodes(), 3u);
159}
160
161// =============================================================================
162// Arc Insertion Tests
163// =============================================================================
164
166{
167 EGraph g;
168 auto n1 = g.insert_node(Point(0.0, 0.0));
169 auto n2 = g.insert_node(Point(3.0, 4.0));
170
171 auto arc = g.insert_arc(n1, n2);
172
173 ASSERT_NE(arc, nullptr);
174 EXPECT_EQ(g.get_num_arcs(), 1u);
175 EXPECT_EQ(g.get_src_node(arc), n1);
176 EXPECT_EQ(g.get_tgt_node(arc), n2);
177}
178
179TEST_F(EuclidianGraphTest, InsertArcWithInfo)
180{
181 EGraph g;
182 auto n1 = g.insert_node(Point(0.0, 0.0));
183 auto n2 = g.insert_node(Point(1.0, 1.0));
184
185 auto arc = g.insert_arc(n1, n2, 99.9);
186
187 ASSERT_NE(arc, nullptr);
188 EXPECT_DOUBLE_EQ(arc->get_info(), 99.9);
189}
190
191TEST_F(EuclidianGraphTest, InsertMultipleArcs)
192{
193 EGraph g;
194 auto n1 = g.insert_node(Point(0.0, 0.0));
195 auto n2 = g.insert_node(Point(1.0, 0.0));
196 auto n3 = g.insert_node(Point(1.0, 1.0));
197
198 g.insert_arc(n1, n2);
199 g.insert_arc(n2, n3);
200 g.insert_arc(n1, n3);
201
202 EXPECT_EQ(g.get_num_arcs(), 3u);
203}
204
205// =============================================================================
206// Distance Calculation Tests
207// =============================================================================
208
209TEST_F(EuclidianGraphTest, CalculateDistance)
210{
211 EGraph g;
212 auto n1 = g.insert_node(Point(0.0, 0.0));
213 auto n2 = g.insert_node(Point(3.0, 4.0));
214
215 auto arc = g.insert_arc(n1, n2);
216 Geom_Number dist = g.get_distance(arc);
217
218 // Distance = sqrt(3^2 + 4^2) = 5.0
219 EXPECT_DOUBLE_EQ(dist.get_d(), 5.0);
220}
221
222TEST_F(EuclidianGraphTest, CalculateDistanceZero)
223{
224 EGraph g;
225 auto n1 = g.insert_node(Point(5.0, 5.0));
226 auto n2 = g.insert_node(Point(5.0, 5.0));
227
228 auto arc = g.insert_arc(n1, n2);
229 Geom_Number dist = g.get_distance(arc);
230
231 EXPECT_DOUBLE_EQ(dist.get_d(), 0.0);
232}
233
234TEST_F(EuclidianGraphTest, CalculateDistanceHorizontal)
235{
236 EGraph g;
237 auto n1 = g.insert_node(Point(0.0, 5.0));
238 auto n2 = g.insert_node(Point(10.0, 5.0));
239
240 auto arc = g.insert_arc(n1, n2);
241 Geom_Number dist = g.get_distance(arc);
242
243 EXPECT_DOUBLE_EQ(dist.get_d(), 10.0);
244}
245
246TEST_F(EuclidianGraphTest, CalculateDistanceVertical)
247{
248 EGraph g;
249 auto n1 = g.insert_node(Point(5.0, 0.0));
250 auto n2 = g.insert_node(Point(5.0, 8.0));
251
252 auto arc = g.insert_arc(n1, n2);
253 Geom_Number dist = g.get_distance(arc);
254
255 EXPECT_DOUBLE_EQ(dist.get_d(), 8.0);
256}
257
258// =============================================================================
259// Search Tests
260// =============================================================================
261
262TEST_F(EuclidianGraphTest, SearchNodeByPosition)
263{
264 EGraph g;
265 Point p1(10.0, 20.0);
266 Point p2(30.0, 40.0);
267
268 auto n1 = g.insert_node(p1);
269 auto n2 = g.insert_node(p2);
270
271 auto found1 = g.search_node(p1);
272 auto found2 = g.search_node(p2);
273
274 EXPECT_EQ(found1, n1);
275 EXPECT_EQ(found2, n2);
276}
277
278TEST_F(EuclidianGraphTest, SearchNodeNotFound)
279{
280 EGraph g;
281 g.insert_node(Point(10.0, 20.0));
282
283 Point search_point(99.0, 99.0);
284 auto found = g.search_node(search_point);
285
286 EXPECT_EQ(found, nullptr);
287}
288
289TEST_F(EuclidianGraphTest, SearchInEmptyGraph)
290{
291 EGraph g;
292 Point p(5.0, 5.0);
293
294 auto found = g.search_node(p);
295 EXPECT_EQ(found, nullptr);
296}
297
298// =============================================================================
299// Copy Constructor Tests
300// =============================================================================
301
302TEST_F(EuclidianGraphTest, CopyConstructor)
303{
304 EGraph g1;
305 auto n1 = g1.insert_node(Point(0.0, 0.0));
306 auto n2 = g1.insert_node(Point(1.0, 1.0));
307 g1.insert_arc(n1, n2);
308
309 EGraph g2(g1);
310
311 EXPECT_EQ(g2.get_num_nodes(), g1.get_num_nodes());
312 EXPECT_EQ(g2.get_num_arcs(), g1.get_num_arcs());
313}
314
315// =============================================================================
316// Assignment Operator Tests
317// =============================================================================
318
319TEST_F(EuclidianGraphTest, AssignmentOperator)
320{
321 EGraph g1;
322 auto n1 = g1.insert_node(Point(0.0, 0.0));
323 auto n2 = g1.insert_node(Point(5.0, 5.0));
324 g1.insert_arc(n1, n2);
325
326 EGraph g2;
327 g2 = g1;
328
329 EXPECT_EQ(g2.get_num_nodes(), 2u);
330 EXPECT_EQ(g2.get_num_arcs(), 1u);
331}
332
334{
335 EGraph g;
336 auto n1 = g.insert_node(Point(0.0, 0.0));
337 auto n2 = g.insert_node(Point(1.0, 1.0));
338 g.insert_arc(n1, n2);
339
340 g = g; // Self-assignment
341
342 EXPECT_EQ(g.get_num_nodes(), 2u);
343 EXPECT_EQ(g.get_num_arcs(), 1u);
344}
345
346// =============================================================================
347// Digraph Tests
348// =============================================================================
349
350TEST_F(EuclidianGraphTest, DigraphConstruction)
351{
352 EDigraph dg;
353 EXPECT_TRUE(dg.is_digraph());
354 EXPECT_EQ(dg.get_num_nodes(), 0u);
355}
356
357TEST_F(EuclidianGraphTest, DigraphInsertNodes)
358{
359 EDigraph dg;
360 dg.insert_node(Point(0.0, 0.0));
361 dg.insert_node(Point(10.0, 10.0));
362
363 EXPECT_EQ(dg.get_num_nodes(), 2u);
364 EXPECT_TRUE(dg.is_digraph());
365}
366
367TEST_F(EuclidianGraphTest, DigraphInsertArcs)
368{
369 EDigraph dg;
370 auto n1 = dg.insert_node(Point(0.0, 0.0));
371 auto n2 = dg.insert_node(Point(5.0, 5.0));
372
373 auto arc = dg.insert_arc(n1, n2);
374
375 ASSERT_NE(arc, nullptr);
376 EXPECT_EQ(dg.get_num_arcs(), 1u);
377}
378
379TEST_F(EuclidianGraphTest, DigraphCopyConstructor)
380{
381 EDigraph dg1;
382 dg1.insert_node(Point(0.0, 0.0));
383 dg1.insert_node(Point(1.0, 1.0));
384
385 EDigraph dg2(dg1);
386
387 EXPECT_TRUE(dg2.is_digraph());
388 EXPECT_EQ(dg2.get_num_nodes(), dg1.get_num_nodes());
389}
390
391namespace
392{
393 // Whether each node of dg has its arcs, and only the arcs that leave it,
394 // in its list, as the nodes of a digraph do
395 bool each_arc_leaves_its_node(const EDigraph & dg, const size_t degree)
396 {
397 for (auto it = dg.get_node_it(); it.has_curr(); it.next_ne())
398 {
399 auto p = it.get_curr();
400 size_t num = 0;
401 for (Node_Arc_Iterator<EDigraph> ait(p); ait.has_curr(); ait.next_ne(), ++num)
402 if (dg.get_src_node(ait.get_curr()) != p)
403 return false;
404 if (num != degree)
405 return false;
406 }
407 return true;
408 }
409
410 // The positions of the nodes of g, in the order of its nodes
411 template <class G>
412 std::vector<std::string> positions(const G & g)
413 {
414 std::vector<std::string> ret;
415 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
416 ret.push_back(it.get_curr()->get_position().to_string());
417 return ret;
418 }
419}
420
421// A copy, made by copy_graph(), which copies only the info of the nodes,
422// put every node at the origin
423TEST_F(EuclidianGraphTest, CopyAndAssignmentKeepThePositions)
424{
425 EGraph g1;
426 auto n1 = g1.insert_node(Point(3.0, 4.0));
427 auto n2 = g1.insert_node(Point(6.0, 8.0));
428 g1.insert_arc(n1, n2);
429 const std::vector<std::string> where = positions(g1);
430 ASSERT_EQ(where.size(), 2u);
431
432 const EGraph & source = g1;
433 EGraph g2(source);
434 EXPECT_EQ(positions(g2), where);
435
436 EGraph g3;
437 g3.insert_node(Point(1.0, 1.0));
438 g3 = source;
439 EXPECT_FALSE(g3.is_digraph());
440 EXPECT_EQ(g3.get_num_arcs(), 1u);
441 EXPECT_EQ(positions(g3), where);
442 for (auto it = g3.get_arc_it(); it.has_curr(); it.next_ne())
443 EXPECT_EQ(g3.get_distance(it.get_curr()), g1.get_distance(g1.get_first_arc()));
444}
445
446// The copy of a digraph was made as of an undirected graph, which puts each
447// arc in the lists of its two nodes, and then marked directed: destroying
448// the copy freed each arc twice. The assignment of a digraph did not
449// compile.
450TEST_F(EuclidianGraphTest, DigraphCopyAndAssignmentWithArcs)
451{
452 EDigraph dg1;
453 auto n1 = dg1.insert_node(Point(0.0, 0.0));
454 auto n2 = dg1.insert_node(Point(3.0, 4.0));
455 auto n3 = dg1.insert_node(Point(6.0, 8.0));
456 dg1.insert_arc(n1, n2);
457 dg1.insert_arc(n2, n3);
458 dg1.insert_arc(n3, n1);
459
460 const EDigraph & source = dg1;
461 EDigraph dg2(source);
462 EXPECT_TRUE(dg2.is_digraph());
463 EXPECT_EQ(dg2.get_num_nodes(), 3u);
464 EXPECT_EQ(dg2.get_num_arcs(), 3u);
465 EXPECT_TRUE(each_arc_leaves_its_node(dg2, 1));
466 EXPECT_EQ(positions(dg2), positions(dg1));
467
468 EDigraph dg3;
469 dg3.insert_node(Point(1.0, 1.0));
470 dg3 = source;
471 EXPECT_TRUE(dg3.is_digraph());
472 EXPECT_EQ(dg3.get_num_nodes(), 3u);
473 EXPECT_EQ(dg3.get_num_arcs(), 3u);
474 EXPECT_TRUE(each_arc_leaves_its_node(dg3, 1));
475 EXPECT_EQ(positions(dg3), positions(dg1));
476}
477
478// =============================================================================
479// Stress Tests
480// =============================================================================
481
483{
484 EGraph g;
485 const size_t n = 1000;
486
487 for (size_t i = 0; i < n; ++i)
488 {
489 double x = static_cast<double>(i);
490 double y = static_cast<double>(i * 2);
491 g.insert_node(Point(x, y));
492 }
493
494 EXPECT_EQ(g.get_num_nodes(), n);
495}
496
498{
499 EGraph g;
500 const size_t n = 100;
501
502 std::vector<ENode*> nodes;
503 for (size_t i = 0; i < n; ++i)
504 {
505 nodes.push_back(g.insert_node(Point(static_cast<double>(i), static_cast<double>(i))));
506 }
507
508 // Create complete graph
509 for (size_t i = 0; i < n; ++i)
510 {
511 for (size_t j = i + 1; j < n; ++j)
512 {
513 g.insert_arc(nodes[i], nodes[j]);
514 }
515 }
516
517 size_t expected_arcs = n * (n - 1) / 2;
518 EXPECT_EQ(g.get_num_arcs(), expected_arcs);
519}
520
521// =============================================================================
522// Edge Cases
523// =============================================================================
524
525TEST_F(EuclidianGraphTest, NegativeCoordinates)
526{
527 EGraph g;
528 auto n1 = g.insert_node(Point(-5.0, -10.0));
529 auto n2 = g.insert_node(Point(-1.0, -2.0));
530
531 ASSERT_NE(n1, nullptr);
532 ASSERT_NE(n2, nullptr);
533
534 auto arc = g.insert_arc(n1, n2);
535 Geom_Number dist = g.get_distance(arc);
536
537 EXPECT_GT(dist.get_d(), 0.0);
538}
539
540TEST_F(EuclidianGraphTest, LargeCoordinates)
541{
542 EGraph g;
543 auto n1 = g.insert_node(Point(1e6, 1e6));
544 auto n2 = g.insert_node(Point(1e6 + 1, 1e6 + 1));
545
546 auto arc = g.insert_arc(n1, n2);
547 Geom_Number dist = g.get_distance(arc);
548
549 EXPECT_DOUBLE_EQ(dist.get_d(), std::sqrt(2.0));
550}
Geom_Number get_distance(Arc *arc)
Node * insert_node(Node *node) noexcept override
Insertion of a node already allocated.
Node * search_node(const Point &)
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
Definition tpl_graph.H:609
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:448
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:457
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
TEST_F(EuclidianGraphTest, NodeDefaultConstructor)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
int num(Node *p)
The number of a node of a built graph.
2D point and geometric utilities.
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Euclidean graph with 2D coordinates.