Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
index-node.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#include "../tpl_indexNode.H"
40
41#include "../tpl_graph.H"
42
43// A simple node data structure for our tests
45 int id;
46 std::string payload;
47
48 bool operator<(const TestNodeData& other) const {
49 return id < other.id;
50 }
51};
52
53// Define the graph type we'll use for testing
55
56// Custom comparator for nodes based on their ID
58 bool operator()(const TestGraph::Node* p1, const TestGraph::Node* p2) const {
59 return p1->get_info().id < p2->get_info().id;
60 }
61};
62
63class IndexNodeTest : public ::testing::Test {
64protected:
67
68 void SetUp() override {
69 // The index is initialized with an empty graph
70 }
71};
72
74 EXPECT_EQ(index.size(), 0);
75}
76
78 auto* node1 = index.insert_in_graph({1, "A"});
79 EXPECT_EQ(index.size(), 1);
80 EXPECT_EQ(g.get_num_nodes(), 1);
81
82 auto* foundNode = index.search({1, ""});
83 ASSERT_NE(foundNode, nullptr);
85 EXPECT_EQ(foundNode->get_info().payload, "A");
86}
87
89 index.insert_in_graph({1, "A"});
90 auto* foundNode = index.search({2, ""});
91 EXPECT_EQ(foundNode, nullptr);
92}
93
95 auto* node1 = index.insert_in_graph({1, "A"});
96 index.insert_in_graph({2, "B"});
97 EXPECT_EQ(index.size(), 2);
98
99 index.remove(node1);
100 EXPECT_EQ(index.size(), 1);
101 EXPECT_EQ(g.get_num_nodes(), 2); // Still in graph
102 EXPECT_EQ(index.search({1, ""}), nullptr);
103 EXPECT_NE(index.search({2, ""}), nullptr);
104}
105
107 auto* node1 = index.insert_in_graph({1, "A"});
108 index.insert_in_graph({2, "B"});
109 EXPECT_EQ(index.size(), 2);
110 EXPECT_EQ(g.get_num_nodes(), 2);
111
112 index.remove_from_graph(node1);
113 EXPECT_EQ(index.size(), 1);
114 EXPECT_EQ(g.get_num_nodes(), 1);
115 EXPECT_EQ(index.search({1, ""}), nullptr);
116}
117
119 auto* node1 = g.insert_node({1, "A"});
120 // Node is in graph but not in index
121 EXPECT_THROW(index.remove_from_graph(node1), std::domain_error);
122}
123
125 g.insert_node({10, "X"});
126 g.insert_node({20, "Y"});
127
129 // The constructor should call build_index implicitly via init()
131 EXPECT_NE(new_index.search({10, ""}), nullptr);
132 EXPECT_NE(new_index.search({20, ""}), nullptr);
133
134 // Manually add a node and rebuild
135 g.insert_node({30, "Z"});
136 new_index.build_index();
138 EXPECT_NE(new_index.search({30, ""}), nullptr);
139}
140
142 index.insert_in_graph({1, "A"});
143 index.insert_in_graph({2, "B"});
144 EXPECT_EQ(index.size(), 2);
145 EXPECT_EQ(g.get_num_nodes(), 2);
146
147 index.clear_index();
148 EXPECT_EQ(index.size(), 0);
149 EXPECT_EQ(g.get_num_nodes(), 2); // Graph is untouched
150}
151
153 index.insert_in_graph({1, "A"});
154 index.insert_in_graph({2, "B"});
155 EXPECT_EQ(index.size(), 2);
156 EXPECT_EQ(g.get_num_nodes(), 2);
157
158 index.clear_graph();
159 EXPECT_EQ(index.size(), 0);
160 EXPECT_EQ(g.get_num_nodes(), 0);
161}
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Builds an index of nodes for fast search and retrieval.
_Graph_Node Node
The graph type.
Definition tpl_graph.H:432
TestGraph g
Definition index-node.cc:65
void SetUp() override
Definition index-node.cc:68
Aleph::IndexNode< TestGraph, TestNodeCmp > index
Definition index-node.cc:66
TEST_F(IndexNodeTest, InitialState)
Definition index-node.cc:73
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
bool operator()(const TestGraph::Node *p1, const TestGraph::Node *p2) const
Definition index-node.cc:58
std::string payload
Definition index-node.cc:46
bool operator<(const TestNodeData &other) const
Definition index-node.cc:48