62#ifndef ALEPH_TESTS_GRAPH_HARNESS_H
63#define ALEPH_TESTS_GRAPH_HARNESS_H
77#include <gtest/gtest.h>
85#if defined(__unix__) || defined(__APPLE__)
87# define ALEPH_GRAPH_HARNESS_HAS_SMALL_STACK 1
89# define ALEPH_GRAPH_HARNESS_HAS_SMALL_STACK 0
93#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
94# define ALEPH_GRAPH_HARNESS_SANITIZED 1
95#elif defined(__has_feature)
96# if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || \
97 __has_feature(memory_sanitizer)
98# define ALEPH_GRAPH_HARNESS_SANITIZED 1
101#ifndef ALEPH_GRAPH_HARNESS_SANITIZED
102# define ALEPH_GRAPH_HARNESS_SANITIZED 0
113 std::vector<std::array<int, 2>>
arcs;
114 std::vector<long long>
w;
120 for (
const auto & a :
arcs)
127 [[nodiscard]] std::string
str()
const
129 std::ostringstream s;
130 s <<
"n=" <<
n <<
" arcs={";
131 for (
size_t i = 0; i <
arcs.size(); ++i)
133 s << (i == 0 ?
"" :
" ") <<
arcs[i][0] <<
"->" <<
arcs[i][1];
160 const char * e = std::getenv(
"ALEPH_GRAPH_HARNESS");
161 const std::string
m = e ==
nullptr ?
"" : e;
188 const char * e = std::getenv(
"ALEPH_GRAPH_HARNESS_SEED");
189 return e ==
nullptr ?
seed : unsigned(std::strtoul(e,
nullptr, 10));
195 const char * e = std::getenv(
"ALEPH_GRAPH_HARNESS_SHOW");
196 return e ==
nullptr ? 8 : size_t(std::strtoul(e,
nullptr, 10));
210 std::vector<std::array<int, 2>> places;
211 for (
int u = 0; u < n; ++u)
212 for (
int v = directed ? 0 : u; v < n; ++v)
213 if (u != v or d.
loops)
214 places.push_back({u, v});
215 const int p = int(places.size());
224 std::vector<int> pick(
size_t(
m), 0);
226 for (
int i = 0; i <
m; ++i)
231 std::vector<int> flippable;
232 for (
int i = 0; i <
m; ++i)
233 if (places[
size_t(pick[
size_t(i)])][0] != places[
size_t(pick[
size_t(i)])][1])
234 flippable.push_back(i);
235 const unsigned masks =
236 directed or not d.
directions ? 1U : 1U << flippable.size();
238 for (
unsigned mask = 0; mask < masks; ++mask)
242 for (
int i = 0; i <
m; ++i)
243 s.
arcs.push_back(places[
size_t(pick[
size_t(i)])]);
244 for (
size_t k = 0;
k < flippable.size(); ++
k)
245 if (mask & (1U <<
k))
246 std::swap(s.
arcs[
size_t(flippable[
k])][0],
247 s.
arcs[
size_t(flippable[
k])][1]);
248 f(
static_cast<const Spec &
>(s));
252 std::reverse(s.
arcs.begin(), s.
arcs.end());
253 f(
static_cast<const Spec &
>(s));
262 while (i >= 0
and pick[
size_t(i)] == p - 1)
266 const int x = pick[size_t(i)] + 1;
267 for (
int j = i; j <
m; ++j)
272 while (i >= 0
and pick[
size_t(i)] == p -
m + i)
277 for (
int j = i + 1; j <
m; ++j)
278 pick[
size_t(j)] = pick[size_t(j - 1)] + 1;
294 for (
int n = min_nodes; n <= max_nodes; ++n)
296 std::vector<std::array<int, 2>> pairs;
297 for (
int u = 0; u < n; ++u)
298 for (
int v = directed ? 0 : u + 1; v < n; ++v)
300 pairs.push_back({u, v});
301 for (
unsigned long set = 0; set < (1UL << pairs.size()); ++set, ++
count)
305 for (
size_t i = 0; i < pairs.size(); ++i)
306 if (set & (1UL << i))
307 s.
arcs.push_back(pairs[i]);
308 f(
static_cast<const Spec &
>(s));
317 bool loops =
true,
bool parallel =
true)
323 std::vector<std::array<int, 2>> places;
324 for (
int u = 0; u < n; ++u)
325 for (
int v = directed ? 0 : u; v < n; ++v)
327 places.push_back({u, v});
330 if (not parallel
and size_t(
m) > places.size())
332 std::vector<bool> taken(places.size(),
false);
333 std::uniform_int_distribution<size_t> pick(0, places.size() - 1);
334 while (s.
arcs.size() <
size_t(
m))
336 const size_t k = pick(
rng);
337 if (not parallel
and taken[
k])
341 if (not directed
and rng() % 2 == 1)
342 std::swap(a[0], a[1]);
351 std::uniform_int_distribution<long long>
w(lo, hi);
353 for (
size_t i = 0; i < s.
arcs.size(); ++i)
354 s.
w.push_back(
w(
rng));
362 std::vector<typename GT::Node *>
nodes;
363 std::vector<typename GT::Arc *>
arcs;
367 for (
int i = 0; i < s.
n; ++i)
369 for (
size_t i = 0; i < s.
arcs.size(); ++i)
379 template <
class Node>
380 [[nodiscard]]
int num(
Node * p) {
return p->get_info(); }
386 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
387 if (it.get_curr_ne()->get_info() == i)
388 return it.get_curr_ne();
418#if ALEPH_GRAPH_HARNESS_HAS_SMALL_STACK
420 if (pthread_attr_init(&attr) != 0)
422 pthread_attr_setstacksize(&attr, kb * 1024);
423 using Fn = std::remove_reference_t<F>;
424 auto start = [] (
void * arg) ->
void *
426 (*
static_cast<Fn *
>(arg))();
430 const int created = pthread_create(&thread, &attr, start, &f);
431 pthread_attr_destroy(&attr);
434 return pthread_join(thread,
nullptr) == 0;
459 explicit Check(std::string what,
bool known =
false)
469 void fail(
const Spec & s,
const std::string & what)
473 first.push_back(s.
str() +
": " + what);
481 std::ostringstream s;
482 for (
const auto & f :
first)
487 <<
name <<
": a known failure no longer fails; remove its mark";
491 std::cout <<
", " <<
skipped <<
" left out";
492 std::cout << s.str() << std::endl;
497 <<
skipped <<
" left out" << s.str();
506 template <
class F,
class Filt>
510 Filter_Throws::throw_at = 0;
511 Filter_Throws::calls = 0;
513 const long n = Filter_Throws::calls;
514 for (
long k = 1;
k <= n; ++
k)
516 Filter_Throws::calls = 0;
517 Filter_Throws::throw_at =
k;
527 Filter_Throws::throw_at = 0;
531 EXPECT_TRUE(
out or Filter_Throws::calls <
k)
532 << name <<
": what the filter threw at its call " <<
k <<
" of " << n
533 <<
" did not come out";
542#define EXPECT_THROW_OR_FILTER(statement, type) \
544 if (::graph_filters::Filter_Throws::throw_at == 0) \
545 EXPECT_THROW(statement, type); \
551 catch (const type &) \
WeightedDigraph::Node Node
size_t size_t int32_t * out
Generic directed graph (digraph) wrapper template.
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
size_t num_failures() const noexcept
void skip() noexcept
Counts a run left out because it would end the program.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
std::vector< std::string > first
Check(std::string what, bool known=false)
The kinds of arc filter and of distance that the graph algorithms must accept.
#define ALEPH_GRAPH_HARNESS_SANITIZED
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
bool run_with_small_stack(F &&f, size_t kb=256)
Runs f in a thread whose stack has kb KiB, and returns false if the thread could not run; without POS...
GT::Node * node_of(const GT &g, int i)
The node of a graph whose number is i.
::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.
size_t failures_shown()
How many failures a check prints: ALEPH_GRAPH_HARNESS_SHOW, or 8.
Mode mode()
ALEPH_GRAPH_HARNESS=light, normal or heavy, or else light in a build with a sanitizer and normal with...
::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.
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.
void with_each_throw(F &f, const Filt &filt, const char *name)
Calls f(filt, name), as the loop over the filters of a test does, and again for each call k of the fi...
Mode
The sizes of the domains.
Spec random_spec(std::mt19937 &rng, int n, int m, bool directed, bool loops=true, bool parallel=true)
A random spec of n nodes and m arcs, with loops and parallel arcs if asked; parallel is ignored when ...
size_t for_each_simple_graph(int min_nodes, int max_nodes, bool directed, F &&f)
Calls f(spec) with every simple graph of n nodes, for n from min_nodes to max_nodes: each set of pair...
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,...
bool heavy()
Whether the mode is heavy, which asks for the larger domains.
void random_weights(std::mt19937 &rng, Spec &s, long long lo, long long hi)
Gives each arc of s a weight from lo to hi.
::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.
Arc of graph implemented with double-linked adjacency lists.
The calls, counted from 1, of the filters whose call is not noexcept, and the one that throws Thrown_...
What a filter whose call is not noexcept throws at the call Filter_Throws::throw_at.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
Built(const Built &)=delete
std::vector< typename GT::Arc * > arcs
Built & operator=(const Built &)=delete
Which multigraphs for_each_spec() gives.
bool loops
arcs from a node to itself
bool parallel
several arcs between the same nodes
bool reversed
also the arcs in the reverse order
bool directions
each arc from either end (not directed)
A small multigraph: its nodes are 0 .
std::string str() const
The spec as text, ready to be written back as a test case.
std::vector< long long > w
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
bool has_loop() const noexcept
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.