ZeroErr
δΈ/En
Loading...
Searching...
No Matches
element_of.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
zeroerr/domains/domain.h
"
4
#include "
zeroerr/internal/config.h
"
5
6
ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
7
8
namespace
zeroerr
{
9
27
template
<
typename
T>
28
class
ElementOf
:
public
Domain
<T, uint64_t> {
29
public
:
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
62
ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
zeroerr::Domain
Domain class for generating random values of a specific type.
Definition
domain.h:21
zeroerr::ElementOf::ElementOf
ElementOf(std::vector< T > elements)
Definition
element_of.h:35
zeroerr::ElementOf::ValueType
T ValueType
Definition
element_of.h:30
zeroerr::ElementOf::GetValue
ValueType GetValue(const CorpusType &v) const override
Definition
element_of.h:41
zeroerr::ElementOf::FromValue
CorpusType FromValue(const ValueType &v) const override
Definition
element_of.h:43
zeroerr::ElementOf::CorpusType
uint64_t CorpusType
Definition
element_of.h:31
zeroerr::ElementOf::elements
std::vector< T > elements
Definition
element_of.h:33
zeroerr::ElementOf::Mutate
void Mutate(Rng &rng, CorpusType &v, bool only_shrink) const override
Definition
element_of.h:50
zeroerr::ElementOf::GetRandomCorpus
CorpusType GetRandomCorpus(Rng &rng) const override
Definition
element_of.h:39
zeroerr::Rng
Definition
rng.h:30
zeroerr::Rng::bounded
uint32_t bounded(uint32_t range) noexcept
Definition
rng.h:126
config.h
ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition
config.h:283
ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition
config.h:235
domain.h
std
STL namespace.
zeroerr
Definition
assert_text.cpp:5
include
zeroerr
domains
element_of.h
Generated by
1.15.0