Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
test_case.h
1#pragma once
2
3#include <cstddef>
4#include <functional>
5#include <future>
6#include <memory>
7#include <string_view>
8#include <thread>
9#include <type_traits>
10#include <utility>
11
12namespace hegel::impl::test_case {
13 struct TestCaseData;
14}
15
16namespace hegel::generators {
17 template <typename T> class AnyGenerator;
18 template <typename T> class Generator;
19} // namespace hegel::generators
20
21namespace hegel {
22 class Printer;
23}
24
25namespace hegel {
26
46 class TestCase {
47 public:
48 TestCase(const TestCase&) = delete;
49 TestCase& operator=(const TestCase&) = delete;
51 TestCase(TestCase&&) noexcept;
56 TestCase& operator=(TestCase&&) noexcept;
57 ~TestCase();
58
77 template <typename T> T draw(const generators::Generator<T>& gen) const;
78
92 template <typename T>
93 T draw(std::string_view name,
94 const generators::Generator<T>& gen) const;
95
97 template <typename T>
98 T draw(const generators::AnyGenerator<T>& gen) const;
99 template <typename T>
100 T draw(std::string_view name,
101 const generators::AnyGenerator<T>& gen) const;
103
120 template <typename T>
121 T draw_silent(const generators::AnyGenerator<T>& gen) const;
122
137 template <typename T>
138 T draw_and_print(const generators::Generator<T>& gen,
139 Printer& printer) const;
140
153 void assume(bool condition) const;
154
166 [[noreturn]] void reject() const;
167
185 void target(double score, std::string_view label = "") const;
186
203 void event(std::string_view label) const;
204
223 void event_value(std::string_view label, double value) const;
224
251 [[noreturn]] void repeat(const std::function<void()>& body) const;
252
263 void note(std::string_view message) const;
264
288 TestCase clone() const;
289
317 void set_worker(std::size_t worker_index);
318
340 template <typename F> auto spawn(F fn) const;
341
343 explicit TestCase(std::unique_ptr<impl::test_case::TestCaseData> data);
344
345 // Generators reach through this accessor to talk to the backend.
346 // Not part of the user-facing API.
347 impl::test_case::TestCaseData* data() const { return data_.get(); }
349
350 private:
351 std::unique_ptr<impl::test_case::TestCaseData> data_;
352 };
353
361 template <typename T> class Worker {
362 public:
364 Worker(std::thread thread, std::future<T> future)
365 : thread_(std::move(thread)), future_(std::move(future)) {}
368 Worker(Worker&&) noexcept = default;
369 Worker(const Worker&) = delete;
370 Worker& operator=(const Worker&) = delete;
371 ~Worker() {
372 if (thread_.joinable()) {
373 thread_.join();
374 }
375 }
376
385 T join() {
386 if (thread_.joinable()) {
387 thread_.join();
388 }
389 return future_.get();
390 }
391
392 private:
393 std::thread thread_;
394 std::future<T> future_;
395 };
396
397 template <typename F> auto TestCase::spawn(F fn) const {
398 using R = std::invoke_result_t<F, TestCase&>;
399 std::packaged_task<R()> task(
400 [fn = std::move(fn), cloned_tc = clone()]() mutable {
401 return fn(cloned_tc);
402 });
403 std::future<R> future = task.get_future();
404 return Worker<R>(std::thread(std::move(task)), std::move(future));
405 }
406
407} // namespace hegel
Writes to the document that a failure report shows.
Definition printer.h:55
auto spawn(F fn) const
Run fn on a clone of this test case in a new thread.
Definition test_case.h:397
void set_worker(std::size_t worker_index)
Attribute what this test case prints from now on to a worker.
void repeat(const std::function< void()> &body) const
Run body over and over until the engine ends the test case. It never returns.
void event(std::string_view label) const
Record an event for the end-of-run statistics report.
void reject() const
Reject the current test case unconditionally.
void assume(bool condition) const
Reject the current test case if condition is false.
void target(double score, std::string_view label="") const
Record a score for the engine's targeted-search phase to maximize.
T draw_and_print(const generators::Generator< T > &gen, Printer &printer) const
Draw a value from a printable generator and print it to printer.
Definition core.h:1063
TestCase clone() const
Creates a clone of this test case with an independent choice sequence forked from this test case.
TestCase(TestCase &&) noexcept
Move-constructs, transferring ownership of the underlying handle.
void event_value(std::string_view label, double value) const
Record a numeric observation for the end-of-run statistics report.
void note(std::string_view message) const
Record a message that will be printed on the final replay of a failing test case.
T draw_silent(const generators::AnyGenerator< T > &gen) const
Draw a random value from any generator, without printing it.
Definition core.h:1053
T draw(const generators::Generator< T > &gen) const
Draw a random value from a printable generator.
Definition core.h:993
Handle to a worker started by TestCase::spawn().
Definition test_case.h:361
T join()
Await the worker and return its result.
Definition test_case.h:385
Worker(Worker &&) noexcept=default
Move-constructs, transferring ownership of the running worker.
A generator: a handle to an IGenerator.
Definition core.h:311
A printable generator: TestCase::draw() accepts it.
Definition core.h:470
Hegel generators.
Definition core.h:25
Main namespace.
Definition config.h:29