55#include <gtest/gtest.h>
93 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
95 auto * a = it.get_curr();
102 std::set<std::pair<int, int>> pairs_of(
const Spec & s,
bool directed)
104 std::set<std::pair<int, int>>
r;
105 for (
const auto & a : s.
arcs)
113 std::string simple_error(
const Spec & s,
int n,
bool directed)
116 return std::to_string(s.
n) +
" nodes, not " + std::to_string(n);
117 for (
const auto & a : s.
arcs)
118 if (a[0] < 0 or a[0] >= n or a[1] < 0 or a[1] >= n)
119 return
"a node is not numbered";
120 else if (a[0] == a[1])
122 if (pairs_of(s, directed).
size() != s.
arcs.size())
123 return "parallel arcs";
129 bool connected(
const Spec & s,
bool directed)
132 for (
int u = 0; u < s.
n; ++u)
133 for (
int v = 0; v < s.
n; ++v)
144 bool can_be_made_eulerian(
const Spec & s,
bool directed)
148 const auto have = pairs_of(s,
false);
151 for (
int u = 0; u < s.
n; ++u)
152 for (
int v = u + 1; v < s.
n; ++v)
153 if (not have.count({u, v}))
154 complement.
arcs.push_back({u, v});
156 std::vector<int> odd(
size_t(s.
n), 0);
157 for (
int x = 0; x < s.
n; ++x)
159 return std::all_of(odd.begin(), odd.end(), [] (
int k) { return k % 2 == 0; });
163 bool ore(
const Spec & s)
167 const auto have = pairs_of(s,
false);
168 for (
int u = 0; u < s.
n; ++u)
169 for (
int v = u + 1; v < s.
n; ++v)
176 bool hamiltonian(
const Spec & s)
180 const auto have = pairs_of(s,
false);
181 std::vector<int> order(
static_cast<size_t>(s.
n));
182 std::iota(order.begin(), order.end(), 0);
186 for (
int i = 0; i < s.
n and ok; ++i)
188 const int u = order[size_t(i)], v = order[size_t((i + 1) % s.
n)];
189 ok = have.count({std::min(u, v), std::max(u, v)}) > 0;
194 while (std::next_permutation(order.begin() + 1, order.end()));
201 template <
class GT,
template <
class,
class,
class>
class Gen>
202 void check_generators(
bool directed,
int max_nodes,
int seeds)
204 using Make = Gen<GT, Number_Nodes<GT>, Number_Arcs<GT>>;
205 Check sparse(
"operator()(n, m, connected)");
206 Check dense(
"operator()(n, p, connected)");
210 Check same(
"the same seed gives the same graph");
211 Check eulerian_m(
"eulerian(n, m)");
212 Check eulerian_p(
"eulerian(n, p)");
215 Check kept_m(
"eulerian(n, m), from the graph that the seed gives");
216 Check kept_p(
"eulerian(n, p), from the graph that the seed gives");
217 Check hamilton(
"sufficient_hamiltonian(n, p)");
220 auto make = [] (
unsigned long seed,
auto && f)
222 Number_Nodes<GT>
nodes;
223 Number_Arcs<GT>
arcs;
225 return spec_of(f(gen));
228 for (
int n = 2; n <= max_nodes; ++n)
230 const int pairs = directed ? n * (n - 1) : n * (n - 1) / 2;
231 for (
int m = 0;
m <= pairs;
m += 1 + pairs / 8)
232 for (
unsigned long seed = 1;
seed <= unsigned(seeds); ++
seed)
233 for (
bool conn : {
false,
true})
235 const std::string what =
"n=" + std::to_string(n) +
" m=" + std::to_string(
m)
236 +
" seed=" + std::to_string(
seed) + (conn ?
" connected" :
"") +
": ";
241 const Spec s = make(
seed, [&] (Make & g) {
return g(
size_t(n),
size_t(
m), conn); });
242 if (
auto e = simple_error(s, n, directed); not e.empty())
243 sparse.fail(at, what + e);
244 else if (conn
and not connected(s, directed))
245 sparse.fail(at, what +
"not connected");
248 if (pairs_of(make(
seed, [&] (Make & g) {
return g(
size_t(n),
size_t(
m), conn); }),
249 directed) != pairs_of(s, directed))
250 same.fail(at, what +
"another graph");
253 for (
double p : {0.1, 0.3, 0.5, 0.8, 1.0})
254 for (
unsigned long seed = 1;
seed <= unsigned(seeds); ++
seed)
256 const std::string what =
"n=" + std::to_string(n) +
" p=" + std::to_string(p)
257 +
" seed=" + std::to_string(
seed) +
": ";
262 const Spec s = make(
seed, [&] (Make & g) {
return g(
size_t(n), p,
true); });
263 if (
auto e = simple_error(s, n, directed); not e.empty())
264 dense.fail(at, what + e);
265 else if (not connected(s, directed))
266 dense.fail(at, what +
"not connected");
267 else if (p == 1.0
and int(s.
arcs.size()) != (directed ? n * (n - 1) : n * (n - 1) / 2))
268 dense.fail(at, what +
"p = 1 without every arc");
270 if (not directed
and n >= 3)
273 const Spec h = make(
seed, [&] (Make & g) {
return g.sufficient_hamiltonian(
size_t(n), p); });
274 if (
auto e = simple_error(
h, n,
false); not e.empty())
275 hamilton.fail(at, what + e);
276 else if (not ore(
h) or not hamiltonian(
h))
277 hamilton.fail(at, what +
"neither Ore's condition nor a Hamilton cycle");
283 auto check_eulerian = [&] (
Check &
check,
Check & kept,
const std::string & what,
284 const Spec & base,
bool known_base,
auto && f)
292 if (
auto err = simple_error(e, n, directed); not err.empty())
293 check.fail(at, what + err);
295 or not connected(e, directed))
296 check.fail(at, what +
"not Eulerian");
300 const auto have = pairs_of(e, directed);
301 for (
const auto & pr : pairs_of(base, directed))
302 if (not have.
count(pr))
304 kept.fail(at, what +
"an arc of the graph it starts from is gone");
309 catch (
const std::domain_error &)
314 if (can_be_made_eulerian(base, directed))
315 kept.fail(at, what +
"throws, but arcs added would do");
320 for (
int m = 0;
m <= pairs;
m += 1 + pairs / 8)
321 for (
unsigned long seed = 1;
seed <= unsigned(seeds); ++
seed)
323 const std::string what =
"n=" + std::to_string(n) +
" m=" + std::to_string(
m)
324 +
" seed=" + std::to_string(
seed) +
": ";
325 const Spec base = make(
seed, [&] (Make & g) {
return g(
size_t(n),
size_t(
m),
true); });
326 const bool known_base = pairs_of(base, directed) == pairs_of
327 (make(
seed, [&] (Make & g) {
return g(
size_t(n),
size_t(
m),
true); }), directed);
328 check_eulerian(eulerian_m, kept_m, what, base, known_base, [&]
330 return make(
seed, [&] (Make & g) {
return g.eulerian(
size_t(n),
size_t(
m)); });
334 for (
double p : {0.1, 0.3, 0.5, 0.8, 1.0})
335 for (
unsigned long seed = 1;
seed <= unsigned(seeds); ++
seed)
337 const std::string what =
"n=" + std::to_string(n) +
" p=" + std::to_string(p)
338 +
" seed=" + std::to_string(
seed) +
": ";
339 const Spec base = make(
seed, [&] (Make & g) {
return g(
size_t(n), p,
true); });
340 const bool known_base = pairs_of(base, directed) == pairs_of
341 (make(
seed, [&] (Make & g) {
return g(
size_t(n), p,
true); }), directed);
342 check_eulerian(eulerian_p, kept_p, what, base, known_base, [&]
344 return make(
seed, [&] (Make & g) {
return g.eulerian(
size_t(n), p); });
367 check_generators<TypeParam, Random_Graph>(
false,
heavy() ? 9 : 8,
heavy() ? 60 : 12);
377 check_generators<TypeParam, Random_Digraph>(
true,
heavy() ? 8 : 7,
heavy() ? 60 : 12);
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
The failures of one property over a domain.
__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(ExhaustiveGenerators, RandomGraphsKeepTheirPromises)
TYPED_TEST_SUITE(ExhaustiveGenerators, Undirected_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.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
void next()
Advance all underlying iterators (bounds-checked).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
::testing::Types< 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 > > > Directed_Kinds
The directed kinds, with int infos.
::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 > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
int num(Node *p)
The number of a node of a built graph.
bool heavy()
Whether the mode is heavy, which asks for the larger domains.
Euler euler_class(const Spec &s, bool directed)
The Eulerian class of s: the arcs must be in one component (weakly, in a digraph),...
std::vector< std::vector< bool > > closure(const Spec &s, bool directed)
reach[u][v]: v is reached from u by a directed path of one arc or more (in a graph that is not direct...
std::vector< int > component_of(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of the component of each node, as that of its root, of the graph without the node skip_nod...
bool reaches(const std::vector< std::vector< bool > > &r, int u, int v)
Whether there is a path, of zero arcs or more, from u to v.
int degree(const Spec &s, int x)
The degree of x: a loop counts 2.
Random graph generation utilities.
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.