halcheck 1.0
Loading...
Searching...
No Matches
discard.hpp
1#ifndef HALCHECK_GEN_DISCARD_HPP
2#define HALCHECK_GEN_DISCARD_HPP
3
10#include <halcheck/gen/label.hpp>
11#include <halcheck/lib/atom.hpp>
12#include <halcheck/lib/effect.hpp>
13#include <halcheck/lib/functional.hpp>
14#include <halcheck/lib/type_traits.hpp>
15
16#include <cstdint>
17#include <exception>
18
19namespace halcheck { namespace gen {
20
26void guard(bool cond);
27
34
40 const char *what() const noexcept override { return "halcheck::gen::discard_exception"; }
41};
42
51 void fallback() const {}
52};
53
59
70template<typename F, HALCHECK_REQUIRE(lib::is_invocable<F, lib::atom>())>
72 auto _ = gen::label(id);
73 for (std::uintmax_t i = 0;; i++) {
74 try {
75 return lib::invoke(func, i);
76 } catch (const gen::discard_exception &) {
77 if (max > 0 && i >= max)
78 throw;
79 }
80 }
81}
82
92template<typename F, HALCHECK_REQUIRE(lib::is_invocable<F, lib::atom>())>
94 return gen::retry(id, 100, std::move(func));
95}
96
97}} // namespace halcheck::gen
98
99#endif
static lib::effect_result_t< T > invoke(T args)
Invokes an effect.
Definition effect.hpp:118
void guard(bool cond)
Throws a gen::discard_exception if the given condition is false.
lib::invoke_result_t< F, lib::atom > retry(lib::atom id, std::uintmax_t max, F func)
Executes a function. If the function calls discard, then it is retried, up to a certain number of tim...
Definition discard.hpp:71
void succeed()
Indicates that no more test cases should be generated for the current test.
Definition discard.hpp:58
static struct halcheck::gen::@9 label
Extends the unique identifiers passed to other random generation functions.
lib::variant< lib::symbol, lib::number > atom
An atom is either a symbol or a number.
Definition atom.hpp:194
HALCHECK_INLINE_CONSTEXPR struct halcheck::lib::@20 invoke
An implementation of std::invoke.
decltype(lib::invoke(std::declval< F >(), std::declval< Args >()...)) invoke_result_t
An implementation of std::invoke_result_t.
Definition invoke.hpp:42
An exception type thrown to indicate that the current test case is invalid.
Definition discard.hpp:39
A special exception type used to indicate an abnormal but non-failing result.
Definition discard.hpp:33
An effect for indicating that no more test cases should be generated for the current test.
Definition discard.hpp:47
void fallback() const
This effect does nothing by default.
Definition discard.hpp:51