-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathFixed.cpp
More file actions
111 lines (97 loc) · 4.21 KB
/
Copy pathFixed.cpp
File metadata and controls
111 lines (97 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include "Common.h"
using namespace rc;
using namespace rc::test;
namespace {
template <typename Factory>
struct GenericFixedProperties {
template <typename T>
static void exec() {
templatedProp<T>(
"generated value always has the requested number of elements",
[](const GenParams ¶ms) {
const auto count = *gen::inRange<std::size_t>(0, 10);
const auto shrinkable = Factory::template makeGen<T>(
count, genCountdown())(params.random, params.size);
onAnyPath(
shrinkable,
[=](const Shrinkable<T> &value, const Shrinkable<T> &shrink) {
RC_ASSERT(containerSize(shrink.value()) == count);
});
});
templatedProp<T>("none of the shrinks equal the original value",
[](const GenParams ¶ms) {
const auto count = *gen::inRange<std::size_t>(0, 10);
const auto shrinkable = Factory::template makeGen<T>(
count, genCountdown())(params.random, params.size);
onAnyPath(shrinkable,
[](const Shrinkable<T> &value,
const Shrinkable<T> &shrink) {
RC_ASSERT(value.value() != shrink.value());
});
});
}
};
template <typename Factory>
struct ParamsFixedProperties {
template <typename T>
static void exec() {
using Element = typename T::value_type;
templatedProp<T>("passes the correct size to the element generators",
[](const GenParams ¶ms) {
const auto count = *gen::inRange<std::size_t>(0, 10);
const auto value = Factory::template makeGen<T>(
count, genPassedParams())(
params.random, params.size)
.value();
RC_ASSERT(std::all_of(begin(value),
end(value),
[&](const Element &x) {
return hasSize(params.size, x);
}));
});
templatedProp<T>(
"the random generators passed to element generators are unique",
[](const GenParams ¶ms) {
const auto count = *gen::inRange<std::size_t>(0, 10);
const auto value =
Factory::template makeGen<T>(count, genPassedParams())(
params.random, params.size)
.value();
std::unordered_set<Random> randoms;
RC_ASSERT(std::all_of(
begin(value),
end(value),
[&](const Element &x) { return insertRandoms(randoms, x); }));
});
}
};
} // namespace
TEST_CASE("gen::container(std::size_t)") {
forEachType<GenericFixedProperties<ContainerFactory>,
RC_SEQUENCE_CONTAINERS(int),
RC_SET_CONTAINERS(int),
std::basic_string<int>>();
forEachType<GenericFixedProperties<MapFactory>, RC_MAP_CONTAINERS(int)>();
forEachType<ParamsFixedProperties<ContainerFactory>,
RC_SEQUENCE_CONTAINERS(GenParams),
RC_SET_CONTAINERS(GenParams)>();
forEachType<ParamsFixedProperties<MapFactory>,
RC_MAP_CONTAINERS(GenParams)>();
forEachType<RetrialProperties<FixedMapFactory<2, 15>>,
std::map<int, int>,
std::unordered_map<int, int>>();
forEachType<RetrialProperties<FixedContainerFactory<2, 15>>,
std::set<int>,
std::unordered_set<int>>();
prop("throws GenerationFailure for std::array if count != N",
[](const GenParams ¶ms) {
const auto count = *gen::distinctFrom(3);
const auto gen =
gen::container<std::array<int, 3>>(count, gen::arbitrary<int>());
const auto shrinkable = gen(params.random, params.size);
RC_ASSERT_THROWS_AS(shrinkable.value(), GenerationFailure);
});
// TODO shrink tests?
}