54#include <gtest/gtest.h>
68 std::pair<int, int> key(
int u,
int v,
bool directed)
80 std::vector<typename GT::Node *>
nodes;
81 std::map<int, typename GT::Arc *>
arcs;
82 std::set<int> rejected;
84 explicit Indexed(
const Spec & s) : idx(g)
86 std::set<std::pair<int, int>>
keys;
87 for (
int x = 0; x < s.
n; ++x)
89 for (
size_t i = 0; i < s.
arcs.size(); ++i)
91 const int u = s.
arcs[i][0], v = s.
arcs[i][1];
94 arcs[int(i)] = idx.
insert_arc(nodes[
size_t(u)], nodes[
size_t(v)],
int(i));
97 catch (
const std::domain_error &)
99 rejected.insert(
int(i));
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)
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";
122 const bool directed = x.g.is_digraph();
123 for (
int v = 0; v < s.
n; ++v)
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";
131 for (
int u : nodes_left)
132 for (int v : nodes_left)
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))
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)
154 using GT = TypeParam;
156 Check build(
"Index_Graph, the insertions");
159 Check arcs_in(
"Index_Graph::remove_node() of a node with arcs in");
167 std::set<std::pair<int, int>> seen;
169 for (
size_t i = 0; i < s.
arcs.size(); ++i)
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);
176 for (
int v = 0; v < s.
n; ++v)
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())
185 for (
size_t i = 0; i < s.
arcs.size(); ++i)
188 if (not x.arcs.count(
int(i)))
192 x.idx.remove_arc(left[
int(i)]);
195 for (
int v = 0; v < s.
n; ++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);
202 for (
int v = 0; v < s.
n; ++v)
206 bool in_from_others =
false;
207 for (
auto it = left.begin(); it != left.end(); )
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);
215 x.idx.remove_node(x.nodes[
size_t(v)]);
217 for (
int y = 0;
y < s.
n; ++
y)
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);
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
void insert(Dlink *node) noexcept
Insert node after this.
T & insert(const T &item)
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.
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)
__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)
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
DynArray< Graph::Arc * > arcs
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.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
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.
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
Generic graph and digraph implementations.
Graph indexing utilities for O(log n) node/arc lookup.