ZeroErr
Loading...
Searching...
No Matches
aggregate_of.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <tuple>
7
8#if defined(ZEROERR_ENABLE_PFR) && (ZEROERR_CXX_STANDARD >= 14)
9#include "pfr.hpp"
10#endif
11
13
14namespace zeroerr {
15
16
40template <typename T, typename... Inner>
41class AggregateOf : public Domain<T, std::tuple<typename Inner::CorpusType...>> {
42public:
43 using ValueType = T;
44 using CorpusType = std::tuple<typename Inner::CorpusType...>;
45
46private:
47 template <unsigned... I>
48 inline CorpusType get_random(Rng& rng, detail::seq<I...>) const {
49 return CorpusType{std::get<I>(inner_domains).GetRandomCorpus(rng)...};
50 }
51
52 template <unsigned... I>
53 inline ValueType get_tuple(const CorpusType& v, detail::seq<I...>) const {
54 return ValueType{std::get<I>(inner_domains).GetValue(std::get<I>(v))...};
55 }
56
57 template <unsigned... I>
58 inline CorpusType from_tuple(const ValueType& v, detail::seq<I...>) const {
59 return CorpusType{std::get<I>(inner_domains).FromValue(std::get<I>(v))...};
60 }
61
62 std::tuple<Inner...> inner_domains;
63
64public:
65 AggregateOf(Inner&&... inner) : inner_domains(std::make_tuple(std::move(inner)...)) {}
66
67 CorpusType GetRandomCorpus(Rng& rng) const override {
68 return get_random(rng, detail::gen_seq<sizeof...(Inner)>{});
69 }
70
71 ValueType GetValue(const CorpusType& v) const override {
72 return get_tuple(v, detail::gen_seq<sizeof...(Inner)>{});
73 }
74
75 CorpusType FromValue(const ValueType& v) const override {
76 return from_tuple(v, detail::gen_seq<sizeof...(Inner)>{});
77 }
78
82
83 template <typename D, typename H>
84 void operator()(const D& domain, H& value) {
85 domain.Mutate(rng, value, only_shrink);
86 }
87 };
88
89 void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
90 unsigned index = rng.bounded(sizeof...(Inner));
91 GetTupleDomainMapValue visitor{rng, only_shrink};
92 detail::visit2_at(inner_domains, v, index, visitor);
93 }
94};
95
96
97#ifdef ZEROERR_ENABLE_PFR
98
99template <typename T, typename... Inner>
100AggregateOfImpl<T, Inner...> StructOf(Inner&&... inner) {
101 return AggregateOfImpl<T, Inner...>(std::move(inner)...);
102}
103
104#endif
105
106template <typename... Inner>
107AggregateOf<std::tuple<typename Inner::ValueType...>, Inner...> TupleOf(Inner&&... inner) {
108 return AggregateOf<std::tuple<typename Inner::ValueType...>, Inner...>(std::move(inner)...);
109}
110
111template <typename K, typename V>
116
117
118} // namespace zeroerr
119
AggregateOf is a domain that combines multiple inner domains into a tuple or aggregate type.
Definition aggregate_of.h:41
void Mutate(Rng &rng, CorpusType &v, bool only_shrink) const override
Definition aggregate_of.h:89
T ValueType
Definition aggregate_of.h:43
CorpusType GetRandomCorpus(Rng &rng) const override
Definition aggregate_of.h:67
std::tuple< typename Inner::CorpusType... > CorpusType
Definition aggregate_of.h:44
CorpusType FromValue(const ValueType &v) const override
Definition aggregate_of.h:75
AggregateOf(Inner &&... inner)
Definition aggregate_of.h:65
ValueType GetValue(const CorpusType &v) const override
Definition aggregate_of.h:71
Domain class for generating random values of a specific type.
Definition domain.h:21
virtual CorpusType GetRandomCorpus(Rng &rng) const =0
Definition rng.h:30
uint32_t bounded(uint32_t range) noexcept
Definition rng.h:126
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:265
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:218
STL namespace.
void visit2_at(const std::tuple< Ts... > &tup1, const std::tuple< T2s... > &tup2, size_t idx, F &&fun)
Definition typetraits.h:255
Definition benchmark.cpp:17
AggregateOf< std::tuple< typename Inner::ValueType... >, Inner... > TupleOf(Inner &&... inner)
Definition aggregate_of.h:107
AggregateOf< std::pair< typename K::ValueType, typename V::ValueType >, K, V > PairOf(K &&k, V &&v)
Definition aggregate_of.h:112
Rng & rng
Definition aggregate_of.h:80
bool only_shrink
Definition aggregate_of.h:81
void operator()(const D &domain, H &value)
Definition aggregate_of.h:84
Definition typetraits.h:77
Definition typetraits.h:74