29 template <
typename U,
bool SourcePr
intable>
class Mapped;
51 template <
typename T>
class IGenerator {
56 IGenerator() =
default;
57 virtual ~IGenerator() =
default;
74 virtual uint64_t
label()
const {
75 return internal::type_label(
typeid(*
this));
92 const std::function<
bool(
const T&)>& predicate)
const {
94 internal::label(internal::LabelName::FilterAttempt);
95 for (
int i = 0; i < internal::filter_attempts; ++i) {
96 internal::start_span(tc, attempt);
98 if (predicate(value)) {
99 internal::stop_span(tc);
102 internal::stop_span(tc,
true);
191 const TestCase& tc,
const std::function<
bool(
const T&)>& predicate,
194 internal::label(internal::LabelName::FilterAttempt);
195 for (
int i = 0; i < internal::filter_attempts; ++i) {
196 internal::start_span(tc, attempt);
199 if (predicate(value)) {
201 internal::stop_span(tc);
205 internal::stop_span(tc,
true);
214 template <
typename T>
215 class ValuePrinted :
public IPrintableGenerator<T> {
217 T do_draw(
const TestCase& tc)
const override = 0;
219 T do_draw_and_print(
const TestCase& tc,
220 Printer& printer)
const override {
221 T value = this->do_draw(tc);
226 T do_draw_filtered_and_print(
228 const std::function<
bool(
const T&)>& predicate,
229 Printer& printer)
const override {
230 T value = this->do_draw_filtered(tc, predicate);
236 template <
typename T,
bool Pr
intable>
238 std::conditional_t<Printable, ValuePrinted<T>, IGenerator<T>>;
240 template <
typename T,
bool Pr
intable>
class Structural;
242 template <
typename T>
243 class Structural<T, true> :
public IPrintableGenerator<T> {
245 T do_draw_and_print(
const TestCase& tc,
246 Printer& printer)
const override {
247 return draw_printing(tc, printer);
251 virtual T draw_printing(
const TestCase& tc,
252 Printer& printer)
const = 0;
255 template <
typename T>
256 class Structural<T, false> :
public IGenerator<T> {
258 T do_draw(
const TestCase& tc)
const override {
259 Printer silent = Printer::noop();
260 return draw_printing(tc, silent);
264 virtual T draw_printing(
const TestCase& tc,
265 Printer& printer)
const = 0;
268 template <
typename H>
269 inline constexpr bool is_printable_handle_v =
270 std::is_base_of_v<Generator<typename H::value_type>, H>;
272 template <
bool Pr
intable,
typename T>
274 std::conditional_t<Printable, Generator<T>, AnyGenerator<T>>;
276 template <
typename H>
277 typename H::value_type draw_component(
const TestCase& tc,
const H& gen,
279 if constexpr (is_printable_handle_v<H>) {
280 return tc.draw_and_print(gen, printer);
282 return tc.draw_silent(gen);
286 template <
typename T>
287 std::string open_text(
const Printer& printer, std::string_view open) {
288 if (!printer.should_print()) {
289 return std::string();
291 return internal::cpp_type_name<T>() + std::string(open);
318 : inner_(std::move(impl)) {}
321 T do_draw(
const TestCase& tc)
const {
return inner_->do_draw(tc); }
323 const std::shared_ptr<IGenerator<T>>& inner()
const {
return inner_; }
326 do_draw_filtered(
const TestCase& tc,
327 const std::function<
bool(
const T&)>& predicate)
const {
328 return inner_->do_draw_filtered(tc, predicate);
331 uint64_t label()
const {
return inner_->label(); }
354 template <
typename F>
355 Mapped<std::invoke_result_t<F, T>,
false>
map(F&& f)
const;
382 template <
typename F>
auto flat_map(F&& f)
const;
405 AnyGenerator<T>
filter(std::function<
bool(
const T&)> pred)
const;
426 template <
typename F> Generator<T>
print_with(F&& print)
const;
440 std::shared_ptr<IGenerator<T>> inner_;
479 return printable_->do_draw_and_print(tc, printer);
482 T do_draw_filtered_and_print(
483 const TestCase& tc,
const std::function<
bool(
const T&)>& predicate,
485 return printable_->do_draw_filtered_and_print(tc, predicate,
501 template <
typename F>
515 std::shared_ptr<IPrintableGenerator<T>> printable_;
521 template <
typename T,
typename U,
bool Pr
intable>
522 class MapImpl :
public value_base_t<U, Printable> {
525 : source_(std::move(source)), f_(std::move(f)),
527 {internal::label(internal::LabelName::Map),
528 source_.label()})) {}
530 U do_draw(
const TestCase& tc)
const override {
531 return f_(tc.draw_silent(source_));
534 uint64_t label()
const override {
return label_; }
537 AnyGenerator<T> source_;
538 std::function<U(T)> f_;
542 template <
typename T,
typename U>
543 class PrintedAsCallImpl :
public IPrintableGenerator<U> {
545 PrintedAsCallImpl(Generator<T> source, std::function<U(T)> f,
546 std::string open, uint64_t label)
547 : source_(std::move(source)), f_(std::move(f)),
548 open_(std::move(open)), label_(label) {}
550 U do_draw_and_print(
const TestCase& tc,
551 Printer& printer)
const override {
552 printer.begin_group(open_.size(), open_);
553 T input = tc.draw_and_print(source_, printer);
554 printer.end_group(
")");
555 return f_(std::move(input));
558 uint64_t label()
const override {
return label_; }
561 Generator<T> source_;
562 std::function<U(T)> f_;
567 template <
typename T>
class SilentFilterImpl :
public IGenerator<T> {
569 using Predicate = std::function<bool(
const T&)>;
571 SilentFilterImpl(AnyGenerator<T> source, Predicate predicate)
572 : source_(std::move(source)), predicate_(std::move(predicate)),
574 {internal::label(internal::LabelName::Filter),
575 source_.label()})) {}
577 T do_draw(
const TestCase& tc)
const override {
578 return source_.do_draw_filtered(tc, predicate_);
581 T do_draw_filtered(
const TestCase& tc,
582 const Predicate& predicate)
const override {
583 return source_.do_draw_filtered(tc, both(predicate));
586 uint64_t label()
const override {
return label_; }
589 Predicate both(
const Predicate& predicate)
const {
590 return [first = predicate_, predicate](
const T& value) {
591 return first(value) && predicate(value);
595 AnyGenerator<T> source_;
596 Predicate predicate_;
600 template <
typename T>
601 class PrintedFilterImpl :
public IPrintableGenerator<T> {
603 using Predicate = std::function<bool(
const T&)>;
605 PrintedFilterImpl(Generator<T> source, Predicate predicate)
606 : source_(std::move(source)), predicate_(std::move(predicate)),
608 {internal::label(internal::LabelName::Filter),
609 source_.label()})) {}
611 T do_draw(
const TestCase& tc)
const override {
612 return source_.do_draw_filtered(tc, predicate_);
615 T do_draw_filtered(
const TestCase& tc,
616 const Predicate& predicate)
const override {
617 return source_.do_draw_filtered(tc, both(predicate));
620 T do_draw_and_print(
const TestCase& tc,
621 Printer& printer)
const override {
622 return source_.do_draw_filtered_and_print(tc, predicate_,
626 T do_draw_filtered_and_print(
const TestCase& tc,
627 const Predicate& predicate,
628 Printer& printer)
const override {
629 return source_.do_draw_filtered_and_print(tc, both(predicate),
633 uint64_t label()
const override {
return label_; }
636 Predicate both(
const Predicate& predicate)
const {
637 return [first = predicate_, predicate](
const T& value) {
638 return first(value) && predicate(value);
642 Generator<T> source_;
643 Predicate predicate_;
647 template <
typename T>
648 class PrintedWithImpl :
public IPrintableGenerator<T> {
650 using Print = std::function<void(
const T&, Printer&)>;
652 PrintedWithImpl(AnyGenerator<T> source, Print print)
653 : source_(std::move(source)), print_(std::move(print)) {}
655 T do_draw(
const TestCase& tc)
const override {
656 return source_.do_draw(tc);
661 const std::function<
bool(
const T&)>& predicate)
const override {
662 return source_.do_draw_filtered(tc, predicate);
665 T do_draw_and_print(
const TestCase& tc,
666 Printer& printer)
const override {
667 T value = source_.do_draw(tc);
668 print(value, printer);
672 T do_draw_filtered_and_print(
674 const std::function<
bool(
const T&)>& predicate,
675 Printer& printer)
const override {
676 T value = source_.do_draw_filtered(tc, predicate);
677 print(value, printer);
681 uint64_t label()
const override {
return source_.label(); }
684 void print(
const T& value, Printer& printer)
const {
685 if (printer.should_print()) {
686 print_(value, printer);
690 AnyGenerator<T> source_;
694 template <
typename T>
class ReprPrintedImpl :
public ValuePrinted<T> {
696 explicit ReprPrintedImpl(AnyGenerator<T> source)
697 : source_(std::move(source)) {}
699 T do_draw(
const TestCase& tc)
const override {
700 return source_.do_draw(tc);
705 const std::function<
bool(
const T&)>& predicate)
const override {
706 return source_.do_draw_filtered(tc, predicate);
709 uint64_t label()
const override {
return source_.label(); }
712 AnyGenerator<T> source_;
715 template <
typename T,
typename U,
bool Pr
intable>
716 class FlatMapImpl :
public Structural<U, Printable> {
718 using Next = std::function<handle_t<Printable, U>(T)>;
720 FlatMapImpl(AnyGenerator<T> source, Next f)
721 : source_(std::move(source)), f_(std::move(f)),
723 {internal::label(internal::LabelName::FlatMap),
724 source_.label()})) {}
726 uint64_t label()
const override {
return label_; }
729 U draw_printing(
const TestCase& tc,
730 Printer& printer)
const override {
731 handle_t<Printable, U> next = f_(tc.draw_silent(source_));
732 return draw_component(tc, next, printer);
736 AnyGenerator<T> source_;
741 template <
typename T,
bool Pr
intable>
742 class CompositeImpl :
public value_base_t<T, Printable> {
744 CompositeImpl(std::function<T(
const TestCase&)> fn, uint64_t label)
745 : fn_(std::move(fn)), label_(label) {}
747 T do_draw(
const TestCase& tc)
const override {
return fn_(tc); }
749 uint64_t label()
const override {
return label_; }
752 std::function<T(
const TestCase&)> fn_;
769 template <
typename U,
bool SourcePr
intable>
770 class Mapped :
public detail::handle_t<is_representable_v<U>, U> {
773 using Base = detail::handle_t<is_representable_v<U>, U>;
774 using AsCall = std::function<Generator<U>(std::string, uint64_t)>;
776 Mapped(Base base, AsCall as_call)
777 : Base(std::move(base)), as_call_(std::move(as_call)) {}
803 "print_as_call() needs a printable mapped generator (a "
804 "hegel::generators::Generator<T>), but this map() is of an "
805 "AnyGenerator<T>. Make the mapped generator printable, or use "
806 ".print_with(...) on the result of map().");
807 if (function.empty()) {
808 throw std::invalid_argument(
809 "print_as_call requires a non-empty function name");
811 return as_call_(std::string(function) +
"(", this->label());
821 template <
typename T,
typename U,
bool SourcePr
intable>
822 Mapped<U, SourcePrintable>
823 make_mapped(handle_t<SourcePrintable, T> source,
824 std::function<U(T)> f) {
825 using Result = Mapped<U, SourcePrintable>;
826 typename Result::Base base(
829 typename Result::AsCall as_call;
830 if constexpr (SourcePrintable) {
831 as_call = [source, f](std::string open, uint64_t label) {
833 std::make_shared<PrintedAsCallImpl<T, U>>(
834 source, f, std::move(open), label));
837 return Result(std::move(base), std::move(as_call));
843 template <
typename T>
844 template <
typename F>
847 using U = std::invoke_result_t<F, T>;
848 return detail::make_mapped<T, U, false>(
849 *
this, std::function<U(T)>(std::forward<F>(f)));
852 template <
typename T>
853 template <
typename F>
855 using U = std::invoke_result_t<F, T>;
856 return detail::make_mapped<T, U, true>(
857 *
this, std::function<U(T)>(std::forward<F>(f)));
860 template <
typename T>
861 template <
typename F>
863 using Result = std::decay_t<std::invoke_result_t<F, T>>;
864 using U =
typename Result::value_type;
865 constexpr bool printable = detail::is_printable_handle_v<Result>;
866 using Next =
typename detail::FlatMapImpl<T, U, printable>::Next;
867 return detail::handle_t<printable, U>(
868 std::make_shared<detail::FlatMapImpl<T, U, printable>>(
869 *
this, Next(std::forward<F>(f))));
872 template <
typename T>
876 *
this, std::move(pred)));
879 template <
typename T>
882 return Generator<T>(std::make_shared<detail::PrintedFilterImpl<T>>(
883 *
this, std::move(pred)));
886 template <
typename T>
887 template <
typename F>
889 return Generator<T>(std::make_shared<detail::PrintedWithImpl<T>>(
890 *
this,
typename detail::PrintedWithImpl<T>::Print(
891 std::forward<F>(print))));
896 "print_as_repr() needs a type with a C++ "
897 "representation (hegel::is_representable_v). Use "
898 ".print_with(...) to print it another way.");
900 std::make_shared<detail::ReprPrintedImpl<T>>(*
this));
918 template <
typename Impl,
typename... Args>
920 using T =
typename Impl::value_type;
921 std::shared_ptr<Impl> impl =
922 std::make_shared<Impl>(std::forward<Args>(args)...);
923 if constexpr (std::is_base_of_v<IPrintableGenerator<T>, Impl>) {
948 return internal::combine_labels(labels);
980 using T = std::invoke_result_t<F, const TestCase&>;
982 return detail::handle_t<printable, T>(
983 std::make_shared<detail::CompositeImpl<T, printable>>(
984 std::function<T(
const TestCase&)>(std::forward<F>(fn)),
985 internal::label(internal::LabelName::Composite)));