ZeroErr
element_of.h
Go to the documentation of this file.
1 #pragma once
2 
5 
7 
8 namespace zeroerr {
9 
10 template <typename T>
11 class ElementOf : public Domain<T, uint64_t> {
12 public:
13  using ValueType = T;
14  using CorpusType = uint64_t;
15 
16  std::vector<T> elements;
17 
18  ElementOf(std::vector<T> elements) : elements(elements) {}
19 
20  CorpusType GetRandomCorpus(Rng& rng) override { return rng.bounded(elements.size()); }
21 
22  ValueType GetValue(const CorpusType& v) const override { return elements[v]; }
23 
24  CorpusType FromValue(const ValueType& v) const override {
25  for (size_t i = 0; i < elements.size(); i++) {
26  if (elements[i] == v) return i;
27  }
28  return 0;
29  }
30 
31  void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
32  if (elements.size() <= 1) return;
33  if (only_shrink) {
34  v = rng.bounded(v);
35  } else {
36  v = rng.bounded(elements.size());
37  }
38  }
39 };
40 
41 } // namespace zeroerr
42 
Domain class for generating random values of a specific type.
Definition: domain.h:22
Definition: element_of.h:11
ElementOf(std::vector< T > elements)
Definition: element_of.h:18
T ValueType
Definition: element_of.h:13
ValueType GetValue(const CorpusType &v) const override
Definition: element_of.h:22
CorpusType FromValue(const ValueType &v) const override
Definition: element_of.h:24
CorpusType GetRandomCorpus(Rng &rng) override
Definition: element_of.h:20
uint64_t CorpusType
Definition: element_of.h:14
std::vector< T > elements
Definition: element_of.h:16
void Mutate(Rng &rng, CorpusType &v, bool only_shrink) const override
Definition: element_of.h:31
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
Definition: benchmark.cpp:17