ZeroErr
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 
14 namespace zeroerr {
15 
16 template <typename T, typename... Inner>
17 class AggregateOf : public Domain<T, std::tuple<typename Inner::CorpusType...>> {
18 public:
19  using ValueType = T;
20  using CorpusType = std::tuple<typename Inner::CorpusType...>;
21 
22 private:
23  template <unsigned... I>
24  inline CorpusType get_random(Rng& rng, detail::seq<I...>) const {
25  return CorpusType{std::get<I>(inner_domains).GetRandomCorpus(rng)...};
26  }
27 
28  template <unsigned... I>
29  inline ValueType get_tuple(const CorpusType& v, detail::seq<I...>) const {
30  return ValueType{std::get<I>(inner_domains).GetValue(std::get<I>(v))...};
31  }
32 
33  template <unsigned... I>
34  inline CorpusType from_tuple(const ValueType& v, detail::seq<I...>) const {
35  return CorpusType{std::get<I>(inner_domains).FromValue(std::get<I>(v))...};
36  }
37 
38  std::tuple<Inner...> inner_domains;
39 
40 public:
41  AggregateOf(Inner&&... inner) : inner_domains(std::make_tuple(std::move(inner)...)) {}
42 
43  CorpusType GetRandomCorpus(Rng& rng) const override {
44  return get_random(rng, detail::gen_seq<sizeof...(Inner)>{});
45  }
46 
47  ValueType GetValue(const CorpusType& v) const override {
48  return get_tuple(v, detail::gen_seq<sizeof...(Inner)>{});
49  }
50 
51  CorpusType FromValue(const ValueType& v) const override {
52  return from_tuple(v, detail::gen_seq<sizeof...(Inner)>{});
53  }
54 
56  Rng& rng;
58 
59  template <typename D, typename H>
60  void operator()(const D& domain, H& value) {
61  domain.Mutate(rng, value, only_shrink);
62  }
63  };
64 
65  void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
66  unsigned index = rng.bounded(sizeof...(Inner));
67  GetTupleDomainMapValue visitor{rng, only_shrink};
68  detail::visit2_at(inner_domains, v, index, visitor);
69  }
70 };
71 
72 
73 #ifdef ZEROERR_ENABLE_PFR
74 
75 template <typename T, typename... Inner>
76 AggregateOfImpl<T, Inner...> StructOf(Inner&&... inner) {
77  return AggregateOfImpl<T, Inner...>(std::move(inner)...);
78 }
79 
80 #endif
81 
82 template <typename... Inner>
83 AggregateOf<std::tuple<typename Inner::ValueType...>, Inner...> TupleOf(Inner&&... inner) {
84  return AggregateOf<std::tuple<typename Inner::ValueType...>, Inner...>(std::move(inner)...);
85 }
86 
87 template <typename K, typename V>
90  std::move(v));
91 }
92 
93 
94 } // namespace zeroerr
95 
Definition: aggregate_of.h:17
void Mutate(Rng &rng, CorpusType &v, bool only_shrink) const override
Definition: aggregate_of.h:65
T ValueType
Definition: aggregate_of.h:19
CorpusType GetRandomCorpus(Rng &rng) const override
Definition: aggregate_of.h:43
std::tuple< typename Inner::CorpusType... > CorpusType
Definition: aggregate_of.h:20
CorpusType FromValue(const ValueType &v) const override
Definition: aggregate_of.h:51
AggregateOf(Inner &&... inner)
Definition: aggregate_of.h:41
ValueType GetValue(const CorpusType &v) const override
Definition: aggregate_of.h:47
Domain class for generating random values of a specific type.
Definition: domain.h:22
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
void visit2_at(const std::tuple< Ts... > &tup1, const std::tuple< T2s... > &tup2, size_t idx, F &&fun)
Definition: typetraits.h:220
Definition: benchmark.cpp:17
AggregateOf< std::pair< typename K::ValueType, typename V::ValueType >, K, V > PairOf(K &&k, V &&v)
Definition: aggregate_of.h:88
AggregateOf< std::tuple< typename Inner::ValueType... >, Inner... > TupleOf(Inner &&... inner)
Definition: aggregate_of.h:83
Rng & rng
Definition: aggregate_of.h:56
bool only_shrink
Definition: aggregate_of.h:57
void operator()(const D &domain, H &value)
Definition: aggregate_of.h:60
Definition: typetraits.h:58
Definition: typetraits.h:55