49# include <gtest/gtest.h>
65# if defined(__SANITIZE_ADDRESS__) or defined(__SANITIZE_THREAD__) \
66 or defined(__SANITIZE_HWADDRESS__)
67# define PROBE_ALLOCATIONS 0
68# elif defined(__has_feature)
69# if __has_feature(address_sanitizer) or __has_feature(thread_sanitizer) \
70 or __has_feature(memory_sanitizer) or __has_feature(hwaddress_sanitizer)
71# define PROBE_ALLOCATIONS 0
74# ifndef PROBE_ALLOCATIONS
75# define PROBE_ALLOCATIONS 1
82 struct Allocation_Probe
101 Allocation_Probe probe;
104# if PROBE_ALLOCATIONS
108# if defined(__GNUC__)
109# define PROBE_NOINLINE __attribute__((noinline))
111# define PROBE_NOINLINE
120 if (
size > probe.largest)
122 probe.second = probe.largest;
123 probe.largest =
size;
125 else if (
size > probe.second)
128 return std::malloc(
size == 0 ? 1 :
size);
139 std::fputs(
"howard_fallback_memory_test: out of memory\n",
stderr);
154PROBE_NOINLINE void operator delete(
void * p, std::size_t)
noexcept { std::free(p); }
155PROBE_NOINLINE void operator delete[](
void * p, std::size_t)
noexcept { std::free(p); }
156PROBE_NOINLINE void operator delete(
void * p,
const std::nothrow_t &)
noexcept { std::free(p); }
157PROBE_NOINLINE void operator delete[](
void * p,
const std::nothrow_t &)
noexcept { std::free(p); }
165 size_t reserved(
const size_t count)
175 template <
class GT,
typename W,
class Weight>
178 std::vector<std::tuple<size_t, size_t, W>>
arcs;
179 for (
size_t i = 0; i < n; ++i)
181 arcs.emplace_back(i, (i + 1) % n, weight(
rng));
182 arcs.emplace_back(i,
static_cast<size_t>(
rng() % n), weight(
rng));
189 template <
bool With_Witness,
class GT>
196 auto r = howard_detail::minimum_mean_cycle<GT, D, S, With_Witness>(g, D(),
S(), 0, limit);
201 template <
bool With_Witness,
class GT>
202 bool refused(
const GT & g,
const size_t limit)
208 (
void) howard_detail::minimum_mean_cycle<GT, D, S, With_Witness>(g, D(),
S(), 0, limit);
210 catch (
const std::length_error &)
222 GTEST_SKIP() <<
"a sanitizer provides the allocation functions";
229 std::mt19937_64
rng(0xC3);
230 const auto small = [](std::mt19937_64 &
r) {
return static_cast<long long>(
r() % 2001) - 1000; };
231 const auto unit = [](std::mt19937_64 &
r) {
return std::uniform_real_distribution<double>(-1, 1)(
r); };
233 for (
const size_t n : {120u, 400u, 1100u})
235 const size_t entries = reserved((n + 1) * n);
238 const size_t sums = entries *
sizeof(
long long);
239 const size_t preds = entries *
sizeof(size_t);
242 EXPECT_EQ(probe.largest, sums) <<
"n=" << n;
248 EXPECT_EQ(v.minimum_mean,
r.minimum_mean) <<
"n=" << n;
249 EXPECT_EQ(probe.largest, sums) <<
"n=" << n;
250 EXPECT_LT(probe.second, sums / 4) <<
"n=" << n;
266 GTEST_SKIP() <<
"a sanitizer provides the allocation functions";
271 std::mt19937_64
rng(0x16);
272 const auto big = [](std::mt19937_64 &
r)
274 const long long m = (1LL << 61) +
static_cast<long long>(
r() % (1ULL << 60));
275 return r() % 2 ?
m : -
m;
277 const size_t n = 300;
279# if ALEPH_KARP_INT128
280 const size_t sums = reserved((n + 1) * n) * 16;
282 const size_t sums = reserved((n + 1) * n) *
sizeof(
long long);
289 catch (
const std::overflow_error &)
292 GTEST_SKIP() <<
"the checked long long sums overflowed";
303 GTEST_SKIP() <<
"a sanitizer provides the allocation functions";
311 const long long big = 3100000000000000000LL;
312 for (
const size_t isolated : {1000u, 3000u})
314 const size_t nodes = 6 + isolated;
315 auto built =
build_graph(
nodes, {{0, 1, big}, {1, 2, -big}, {2, 0, 1},
316 {3, 4, -1}, {4, 5, -1}, {5, 3, -2}});
321 ASSERT_EQ(
r.fallback_reason, Howard_Fallback_Reason::Arithmetic_Overflow);
Minimum mean cycle by Howard's policy iteration.
Default distance accessor for arc weights.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > howard_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle by Howard's policy iteration.
#define PROBE_ALLOCATIONS
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
Graph types and probe helpers shared by negative_cycles_test.cc and bounded_negative_cycle_test....
Default filter for filtered iterators on arcs.
Result of howard_minimum_mean_cycle().
bool used_karp
True if Karp's algorithm produced the answer: fallback_reason is not None.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Generic graph and digraph implementations.