Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_exhaustive_index_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
46#include <algorithm>
47#include <map>
48#include <set>
49#include <stdexcept>
50#include <string>
51#include <utility>
52#include <vector>
53
54#include <gtest/gtest.h>
55
56#include <tpl_graph.H>
57#include <tpl_indexGraph.H>
58
59#include "graph_harness.H"
60#include "graph_oracles.H"
61
62using namespace Aleph;
63using namespace graph_harness;
64
65namespace
66{
68 std::pair<int, int> key(int u, int v, bool directed)
69 {
70 return directed ? std::pair(u, v) : std::pair(std::min(u, v), std::max(u, v));
71 }
72
75 template <class GT>
76 struct Indexed
77 {
78 GT g;
80 std::vector<typename GT::Node *> nodes;
81 std::map<int, typename GT::Arc *> arcs; // the arcs that went in
82 std::set<int> rejected;
83
84 explicit Indexed(const Spec & s) : idx(g)
85 {
86 std::set<std::pair<int, int>> keys;
87 for (int x = 0; x < s.n; ++x)
88 nodes.push_back(idx.insert_node(x));
89 for (size_t i = 0; i < s.arcs.size(); ++i)
90 {
91 const int u = s.arcs[i][0], v = s.arcs[i][1];
92 try
93 {
94 arcs[int(i)] = idx.insert_arc(nodes[size_t(u)], nodes[size_t(v)], int(i));
95 keys.insert(key(u, v, g.is_digraph()));
96 }
97 catch (const std::domain_error &)
98 {
99 rejected.insert(int(i));
100 }
101 }
102 }
103 };
104
108 template <class GT>
109 std::string index_error(Indexed<GT> & x, const Spec & s, const std::set<int> & nodes_left,
110 const std::map<int, typename GT::Arc *> & arcs_left, bool searches)
111 {
112 if (x.idx.get_num_nodes() != nodes_left.size() or x.idx.get_num_arcs() != arcs_left.size())
113 return "the index has " + std::to_string(x.idx.get_num_nodes()) + " nodes and "
114 + std::to_string(x.idx.get_num_arcs()) + " arcs, not " + std::to_string(nodes_left.size())
115 + " and " + std::to_string(arcs_left.size());
116 if (x.g.get_num_nodes() != nodes_left.size() or x.g.get_num_arcs() != arcs_left.size())
117 return "the graph has " + std::to_string(x.g.get_num_nodes()) + " nodes and "
118 + std::to_string(x.g.get_num_arcs()) + " arcs";
119 if (not searches)
120 return "";
121
122 const bool directed = x.g.is_digraph();
123 for (int v = 0; v < s.n; ++v)
124 {
125 auto * p = x.idx.search_node(v);
126 if ((p != nullptr) != (nodes_left.count(v) > 0) or (p != nullptr and num(p) != v))
127 return "search_node(" + std::to_string(v) + ") is wrong";
128 if (p != nullptr and x.idx.search_node(p) != p)
129 return "search_node(p) does not give p";
130 }
131 for (int u : nodes_left)
132 for (int v : nodes_left)
133 {
134 int want = -1;
135 for (const auto & [i, a] : arcs_left)
136 if (key(s.arcs[size_t(i)][0], s.arcs[size_t(i)][1], directed) == key(u, v, directed))
137 want = i;
138 auto * a = x.idx.search_arc(x.nodes[size_t(u)], x.nodes[size_t(v)]);
139 if ((a == nullptr ? -1 : num(a)) != want)
140 return "search_arc(" + std::to_string(u) + ", " + std::to_string(v) + ") gives "
141 + std::to_string(a == nullptr ? -1 : num(a)) + ", not " + std::to_string(want);
142 }
143 return "";
144 }
145}
146
147template <class GT>
148class ExhaustiveIndex : public ::testing::Test {};
149
151
152TYPED_TEST(ExhaustiveIndex, SearchesMatchWhatTheGraphHas)
153{
154 using GT = TypeParam;
155 const bool directed = GT().is_digraph();
156 Check build("Index_Graph, the insertions");
157 Check arcs("Index_Graph::remove_arc()");
158 Check nodes("Index_Graph::remove_node()");
159 Check arcs_in("Index_Graph::remove_node() of a node with arcs in");
160
161 for_each_spec(domain(4, 3), directed, [&] (const Spec & s)
162 {
163 {
164 build.run();
165 Indexed<GT> x(s);
166 // A second arc between the same ends is the one rejected
167 std::set<std::pair<int, int>> seen;
168 bool ok = true;
169 for (size_t i = 0; i < s.arcs.size(); ++i)
170 {
171 const bool repeated
172 = not seen.insert(key(s.arcs[i][0], s.arcs[i][1], directed)).second;
173 ok = ok and repeated == (x.rejected.count(int(i)) > 0);
174 }
175 std::set<int> all;
176 for (int v = 0; v < s.n; ++v)
177 all.insert(v);
178 if (not ok)
179 build.fail(s, "it takes a repeated pair of ends, or rejects a new one");
180 else if (auto e = index_error(x, s, all, x.arcs, true); not e.empty())
181 build.fail(s, e);
182 }
183
184 // Each arc that went in, removed
185 for (size_t i = 0; i < s.arcs.size(); ++i)
186 {
187 Indexed<GT> x(s);
188 if (not x.arcs.count(int(i)))
189 continue;
190 arcs.run();
191 auto left = x.arcs;
192 x.idx.remove_arc(left[int(i)]);
193 left.erase(int(i));
194 std::set<int> all;
195 for (int v = 0; v < s.n; ++v)
196 all.insert(v);
197 if (auto e = index_error(x, s, all, left, true); not e.empty())
198 arcs.fail(s, "without the arc " + std::to_string(i) + ": " + e);
199 }
200
201 // Each node removed, with its arcs
202 for (int v = 0; v < s.n; ++v)
203 {
204 Indexed<GT> x(s);
205 auto left = x.arcs;
206 bool in_from_others = false;
207 for (auto it = left.begin(); it != left.end(); )
208 {
209 const auto & a = s.arcs[size_t(it->first)];
210 in_from_others = in_from_others or (a[1] == v and a[0] != v);
211 it = a[0] == v or a[1] == v ? left.erase(it) : std::next(it);
212 }
213 Check & check = in_from_others ? arcs_in : nodes;
214 check.run();
215 x.idx.remove_node(x.nodes[size_t(v)]);
216 std::set<int> rest;
217 for (int y = 0; y < s.n; ++y)
218 if (y != v)
219 rest.insert(y);
220 // A search would read the arcs that the index kept and the graph
221 // freed: only the sizes, if the sizes are wrong
222 if (auto e = index_error(x, s, rest, left, false); not e.empty())
223 check.fail(s, "without the node " + std::to_string(v) + ": " + e);
224 else if (auto e2 = index_error(x, s, rest, left, true); not e2.empty())
225 check.fail(s, "without the node " + std::to_string(v) + ": " + e2);
226 }
227 });
228
229 build.expect();
230 arcs.expect();
231 nodes.expect();
232 arcs_in.expect();
233}
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
T & insert(const T &item)
Definition htlist.H:1308
Builds node and arc indices for fast lookup and retrieval.
GT_Node * insert_node(const GT_Node_Type &info)
Creates a new node and inserts it into the graph and the index.
GT_Arc * insert_arc(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info=GT_Arc_Type())
Creates a new arc between two nodes and inserts it into the graph and the index.
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void run() noexcept
Counts a run of the property.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
TYPED_TEST(ExhaustiveIndex, SearchesMatchWhatTheGraphHas)
TYPED_TEST_SUITE(ExhaustiveIndex, All_Kinds)
Every small multigraph, on every kind of graph, for the tests.
Answers computed from a Spec of graph_harness.H, without Aleph.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
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.
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
int num(Node *p)
The number of a node of a built graph.
Domain domain(int max_nodes, int max_arcs)
The domain of max_nodes nodes and max_arcs arcs, of one more of each in heavy mode,...
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > >, Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > All_Kinds
The six kinds, with int infos.
STL namespace.
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
bool check(GT &g)
int keys[]
Generic graph and digraph implementations.
Graph indexing utilities for O(log n) node/arc lookup.