Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
index_graph_test.cc
Go to the documentation of this file.
1
6#include <gtest/gtest.h>
7#include <tpl_indexGraph.H>
8#include <tpl_graph.H>
9
10using namespace Aleph;
11
12// =============================================================================
13// Type Definitions
14// =============================================================================
15
17using Node = GT::Node;
18using Arc = GT::Arc;
19
20// =============================================================================
21// Test Fixture
22// =============================================================================
23
24class IndexGraphTest : public ::testing::Test
25{
26protected:
28
29 void SetUp() override
30 {
31 g = GT();
32 }
33
34 void TearDown() override {}
35};
36
37// =============================================================================
38// Basic Construction Tests
39// =============================================================================
40
42{
43 Index_Graph<GT> idx(g);
44
45 EXPECT_EQ(idx.get_num_nodes(), 0u);
46 EXPECT_EQ(idx.get_num_arcs(), 0u);
47}
48
50{
51 Index_Graph<GT> idx(g);
52
53 auto n = idx.insert_node(42);
54
55 ASSERT_NE(n, nullptr);
56 EXPECT_EQ(n->get_info(), 42);
57 EXPECT_EQ(idx.get_num_nodes(), 1u);
58 EXPECT_EQ(g.get_num_nodes(), 1u);
59}
60
62{
63 Index_Graph<GT> idx(g);
64
65 auto n1 = idx.insert_node(10);
66 auto n2 = idx.insert_node(20);
67 auto n3 = idx.insert_node(30);
68
69 ASSERT_NE(n1, nullptr);
70 ASSERT_NE(n2, nullptr);
71 ASSERT_NE(n3, nullptr);
72 EXPECT_EQ(idx.get_num_nodes(), 3u);
73}
74
75// =============================================================================
76// Node Search Tests
77// =============================================================================
78
80{
81 Index_Graph<GT> idx(g);
82
83 auto n1 = idx.insert_node(100);
84 auto n2 = idx.insert_node(200);
85
86 auto found1 = idx.search_node(n1);
87 auto found2 = idx.search_node(n2);
88
89 EXPECT_EQ(found1, n1);
90 EXPECT_EQ(found2, n2);
91}
92
94{
95 Index_Graph<GT> idx(g);
96
97 auto n1 = idx.insert_node(100);
98 auto n2 = idx.insert_node(200);
99
100 auto found1 = idx.search_node(100);
101 auto found2 = idx.search_node(200);
102
103 EXPECT_EQ(found1, n1);
104 EXPECT_EQ(found2, n2);
105}
106
108{
109 Index_Graph<GT> idx(g);
110
111 idx.insert_node(10);
112 idx.insert_node(20);
113
114 auto found = idx.search_node(999);
115
116 EXPECT_EQ(found, nullptr);
117}
118
120{
121 Index_Graph<GT> idx(g);
122
123 auto found = idx.search_node(42);
124
125 EXPECT_EQ(found, nullptr);
126}
127
128// =============================================================================
129// Arc Insertion Tests
130// =============================================================================
131
133{
134 Index_Graph<GT> idx(g);
135
136 auto n1 = idx.insert_node(1);
137 auto n2 = idx.insert_node(2);
138
139 auto arc = idx.insert_arc(n1, n2);
140
141 ASSERT_NE(arc, nullptr);
142 EXPECT_EQ(idx.get_num_arcs(), 1u);
143 EXPECT_EQ(g.get_num_arcs(), 1u);
144}
145
147{
148 Index_Graph<GT> idx(g);
149
150 auto n1 = idx.insert_node(10);
151 auto n2 = idx.insert_node(20);
152
153 auto arc = idx.insert_arc(n1, n2, 999);
154
155 ASSERT_NE(arc, nullptr);
156 EXPECT_EQ(arc->get_info(), 999);
157}
158
160{
161 Index_Graph<GT> idx(g);
162
163 auto n1 = idx.insert_node(1);
164 auto n2 = idx.insert_node(2);
165 auto n3 = idx.insert_node(3);
166
167 idx.insert_arc(n1, n2);
168 idx.insert_arc(n2, n3);
169 idx.insert_arc(n1, n3);
170
171 EXPECT_EQ(idx.get_num_arcs(), 3u);
172}
173
174// =============================================================================
175// Arc Search Tests
176// =============================================================================
177
179{
180 Index_Graph<GT> idx(g);
181
182 auto n1 = idx.insert_node(1);
183 auto n2 = idx.insert_node(2);
184
185 auto arc = idx.insert_arc(n1, n2);
186
187 auto found = idx.search_arc(n1, n2);
188
189 EXPECT_EQ(found, arc);
190}
191
193{
194 Index_Graph<GT> idx(g);
195
196 auto n1 = idx.insert_node(1);
197 auto n2 = idx.insert_node(2);
198 auto n3 = idx.insert_node(3);
199
200 idx.insert_arc(n1, n2);
201
202 auto found = idx.search_arc(n1, n3);
203
204 EXPECT_EQ(found, nullptr);
205}
206
208{
209 Index_Graph<GT> idx(g);
210
211 auto n1 = idx.insert_node(1);
212 auto n2 = idx.insert_node(2);
213
214 auto found = idx.search_arc(n1, n2);
215
216 EXPECT_EQ(found, nullptr);
217}
218
219// =============================================================================
220// Node Removal Tests
221// =============================================================================
222
224{
225 Index_Graph<GT> idx(g);
226
227 auto n1 = idx.insert_node(10);
228 idx.insert_node(20);
229
230 idx.remove_node(n1);
231
232 EXPECT_EQ(idx.get_num_nodes(), 1u);
233 EXPECT_EQ(g.get_num_nodes(), 1u);
234
235 auto found = idx.search_node(10);
236 EXPECT_EQ(found, nullptr);
237}
238
240{
241 Index_Graph<GT> idx(g);
242
243 auto n1 = idx.insert_node(1);
244 auto n2 = idx.insert_node(2);
245 auto n3 = idx.insert_node(3);
246
247 idx.insert_arc(n1, n2);
248 idx.insert_arc(n1, n3);
249
250 idx.remove_node(n1);
251
252 EXPECT_EQ(idx.get_num_nodes(), 2u);
253 EXPECT_EQ(idx.get_num_arcs(), 0u); // Arcs connected to n1 removed
254}
255
257{
258 Index_Graph<GT> idx(g);
259
260 auto n1 = idx.insert_node(1);
261 auto n2 = idx.insert_node(2);
262 idx.insert_node(3);
263
264 idx.remove_node(n1);
265 idx.remove_node(n2);
266
267 EXPECT_EQ(idx.get_num_nodes(), 1u);
268}
269
270// =============================================================================
271// Arc Removal Tests
272// =============================================================================
273
275{
276 Index_Graph<GT> idx(g);
277
278 auto n1 = idx.insert_node(1);
279 auto n2 = idx.insert_node(2);
280
281 auto arc = idx.insert_arc(n1, n2);
282
283 idx.remove_arc(arc);
284
285 EXPECT_EQ(idx.get_num_arcs(), 0u);
286 EXPECT_EQ(g.get_num_arcs(), 0u);
287
288 auto found = idx.search_arc(n1, n2);
289 EXPECT_EQ(found, nullptr);
290}
291
293{
294 Index_Graph<GT> idx(g);
295
296 auto n1 = idx.insert_node(1);
297 auto n2 = idx.insert_node(2);
298 auto n3 = idx.insert_node(3);
299
300 auto arc1 = idx.insert_arc(n1, n2);
301 auto arc2 = idx.insert_arc(n2, n3);
302
303 idx.remove_arc(arc1);
304
305 EXPECT_EQ(idx.get_num_arcs(), 1u);
306
307 idx.remove_arc(arc2);
308
309 EXPECT_EQ(idx.get_num_arcs(), 0u);
310}
311
312// =============================================================================
313// Stress Tests
314// =============================================================================
315
317{
318 Index_Graph<GT> idx(g);
319
320 const size_t n = 1000;
321
322 for (size_t i = 0; i < n; ++i)
323 {
324 idx.insert_node(i);
325 }
326
327 EXPECT_EQ(idx.get_num_nodes(), n);
328
329 // Search for random nodes
330 for (size_t i = 0; i < n; i += 100)
331 {
332 auto found = idx.search_node(static_cast<int>(i));
333 EXPECT_NE(found, nullptr);
334 }
335}
336
338{
339 Index_Graph<GT> idx(g);
340
341 const size_t n = 100;
342 std::vector<Node*> nodes;
343
344 for (size_t i = 0; i < n; ++i)
345 {
346 nodes.push_back(idx.insert_node(i));
347 }
348
349 // Create chain
350 for (size_t i = 0; i < n - 1; ++i)
351 {
352 idx.insert_arc(nodes[i], nodes[i + 1]);
353 }
354
355 EXPECT_EQ(idx.get_num_arcs(), n - 1);
356}
357
359{
360 Index_Graph<GT> idx(g);
361
362 const size_t n = 20;
363 std::vector<Node*> nodes;
364
365 for (size_t i = 0; i < n; ++i)
366 {
367 nodes.push_back(idx.insert_node(i));
368 }
369
370 for (size_t i = 0; i < n; ++i)
371 {
372 for (size_t j = i + 1; j < n; ++j)
373 {
374 idx.insert_arc(nodes[i], nodes[j]);
375 }
376 }
377
378 size_t expected = n * (n - 1) / 2;
380}
381
382// =============================================================================
383// Mixed Operations Tests
384// =============================================================================
385
387{
388 Index_Graph<GT> idx(g);
389
390 auto n1 = idx.insert_node(100);
391 idx.insert_node(200);
392
393 auto found = idx.search_node(100);
394 EXPECT_EQ(found, n1);
395
396 idx.remove_node(n1);
397
398 found = idx.search_node(100);
399 EXPECT_EQ(found, nullptr);
400}
401
403{
404 Index_Graph<GT> idx(g);
405
406 auto n1 = idx.insert_node(42);
407 idx.remove_node(n1);
408
409 auto n2 = idx.insert_node(42);
410
411 ASSERT_NE(n2, nullptr);
412 EXPECT_EQ(idx.get_num_nodes(), 1u);
413}
414
415// =============================================================================
416// Duplicate Handling
417// =============================================================================
418
420{
421 Index_Graph<GT> idx(g);
422
423 auto n1 = idx.insert_node(42);
424 auto n2 = idx.insert_node(42); // Same value
425
426 // Index_Graph uses value-based indexing: duplicate value doesn't add new node
427 // Second insert may return different pointer but doesn't increase node count
428 EXPECT_EQ(idx.get_num_nodes(), 1u);
429
430 // Both refer to nodes with value 42
431 EXPECT_EQ(n1->get_info(), 42);
432 EXPECT_EQ(n2->get_info(), 42);
433}
434
435// =============================================================================
436// Edge Cases
437// =============================================================================
438
440{
441 Index_Graph<GT> idx(g);
442
443 EXPECT_EQ(idx.get_num_nodes(), 0u);
444 EXPECT_EQ(idx.get_num_arcs(), 0u);
445}
446
448{
449 Index_Graph<GT> idx(g);
450
451 auto n = idx.insert_node(1);
452
453 EXPECT_EQ(idx.get_num_nodes(), 1u);
454 EXPECT_EQ(idx.get_num_arcs(), 0u);
455
456 auto found = idx.search_node(1);
457 EXPECT_EQ(found, n);
458}
459
461{
462 Index_Graph<GT> idx(g);
463
464 auto n = idx.insert_node(1);
465 auto arc = idx.insert_arc(n, n);
466
467 ASSERT_NE(arc, nullptr);
468 EXPECT_EQ(g.get_src_node(arc), n);
469 EXPECT_EQ(g.get_tgt_node(arc), n);
470}
471
472// =============================================================================
473// Graph Equality Tests (are_equal function)
474// =============================================================================
475
482
484{
485 GT g1, g2;
486
487 auto n1_g1 = g1.insert_node(1);
488 auto n2_g1 = g1.insert_node(2);
489 g1.insert_arc(n1_g1, n2_g1);
490
491 auto n1_g2 = g2.insert_node(1);
492 auto n2_g2 = g2.insert_node(2);
493 g2.insert_arc(n1_g2, n2_g2);
494
496}
497
499{
500 GT g1, g2;
501
502 g1.insert_node(1);
503 g1.insert_node(2);
504
505 g2.insert_node(1);
506 g2.insert_node(3); // Different
507
509}
510
512{
513 GT g1, g2;
514
515 auto n1_g1 = g1.insert_node(1);
516 auto n2_g1 = g1.insert_node(2);
517 g1.insert_arc(n1_g1, n2_g1);
518
519 g2.insert_node(1);
520 g2.insert_node(2);
521 // No arc in g2
522
524}
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
Construye índices de nodos y arcos para su rápida búsqueda y recuperación.
size_t get_num_arcs() const
Retorna el número de arcos que contiene el índice.
GT_Node * search_node(GT_Node *p)
Busca en el índice un nodo.
GT_Arc * search_arc(GT_Node *src, GT_Node *tgt)
Busca en el índice un arco dados dos nodos.
void remove_node(GT_Node *p)
Elimina el nodo p del grafo y del índice.
size_t get_num_nodes() const
Retorna el número de nodos que contiene el índice.
GT_Node * insert_node(const GT_Node_Type &info)
Crea un nuevo nodo y lo inserta en grafo y en el índice.
void remove_arc(GT_Arc *a)
Elimina del grafo y del índice el arco a.
GT_Arc * insert_arc(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info=GT_Arc_Type())
Crea un nuevo arco entre dos nodos y lo inserta en el grafo y en el índice.
Graph_Arc< int > Arc
The node class type.
Definition tpl_graph.H:433
void TearDown() override
void SetUp() override
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
TEST_F(IndexGraphTest, Construction)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool are_equal(const GT &g1, const GT &g2)
Fast graph comparison.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.
Graph indexing utilities for O(log n) node/arc lookup.