ZeroErr
Loading...
Searching...
No Matches
element_of.h
Go to the documentation of this file.
1#pragma once
2
5
7
8namespace zeroerr {
9
27template <typename T>
28class ElementOf : public Domain<T, uint64_t> {
29public:
30 using ValueType = T;
31 using CorpusType = uint64_t;
32
33 std::vector<T> elements;
34
35 ElementOf(std::vector<T> elements) : elements(elements) {}
36
37 CorpusType GetRandomCorpus(Rng& rng) override { return rng.bounded(elements.size()); }
38
39 ValueType GetValue(const CorpusType& v) const override { return elements[v]; }
40
41 CorpusType FromValue(const ValueType& v) const override {
42 for (size_t i = 0; i < elements.size(); i++) {
43 if (elements[i] == v) return i;
44 }
45 return 0;
46 }
47
48 void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
49 if (elements.size() <= 1) return;
50 if (only_shrink) {
51 v = rng.bounded(v);
52 } else {
53 v = rng.bounded(elements.size());
54 }
55 }
56};
57
58} // namespace zeroerr
59
Domain class for generating random values of a specific type.
Definition domain.h:21
ElementOf is a domain that generates random values from a fixed set of elements.
Definition element_of.h:28
ElementOf(std::vector< T > elements)
Definition element_of.h:35
T ValueType
Definition element_of.h:30
ValueType GetValue(const CorpusType &v) const override
Definition element_of.h:39
CorpusType FromValue(const ValueType &v) const override
Definition element_of.h:41
CorpusType GetRandomCorpus(Rng &rng) override
Definition element_of.h:37
uint64_t CorpusType
Definition element_of.h:31
std::vector< T > elements
Definition element_of.h:33
void Mutate(Rng &rng, CorpusType &v, bool only_shrink) const override
Definition element_of.h:48
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