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(std::move(elements)) {
36 if (this->elements.empty()) throw std::invalid_argument("ElementOf requires elements");
37 }
38
39 CorpusType GetRandomCorpus(Rng& rng) const override { return rng.bounded(elements.size()); }
40
41 ValueType GetValue(const CorpusType& v) const override { return elements[v]; }
42
43 CorpusType FromValue(const ValueType& v) const override {
44 for (size_t i = 0; i < elements.size(); i++) {
45 if (elements[i] == v) return i;
46 }
47 return 0;
48 }
49
50 void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
51 if (elements.size() <= 1) return;
52 if (only_shrink) {
53 v = rng.bounded(v);
54 } else {
55 v = rng.bounded(elements.size());
56 }
57 }
58};
59
60} // namespace zeroerr
61
Domain class for generating random values of a specific type.
Definition domain.h:21
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:41
CorpusType FromValue(const ValueType &v) const override
Definition element_of.h:43
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:50
CorpusType GetRandomCorpus(Rng &rng) const override
Definition element_of.h:39
Definition rng.h:30
uint32_t bounded(uint32_t range) noexcept
Definition rng.h:126
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:283
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:235
STL namespace.
Definition assert_text.cpp:5