-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathExecRawTests.cpp
More file actions
240 lines (203 loc) · 7.81 KB
/
Copy pathExecRawTests.cpp
File metadata and controls
240 lines (203 loc) · 7.81 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include <algorithm>
#include <numeric>
#include "util/Predictable.h"
#include "util/GenUtils.h"
#include "util/ArbitraryRandom.h"
#include "rapidcheck/gen/detail/ExecRaw.h"
#include "rapidcheck/shrinkable/Operations.h"
#include "rapidcheck/seq/Operations.h"
using namespace rc;
using namespace rc::test;
using namespace rc::gen::detail;
namespace {
template <int N>
Gen<std::pair<std::vector<int>, Recipe>> testExecGen() {
return execRaw([=](const FixedCountdown<N> &n) {
std::vector<int> values;
values.push_back(n.value);
while (values.size() < static_cast<std::size_t>(n.value + 1)) {
values.push_back(*genFixedCountdown(N));
}
return values;
});
}
} // namespace
TEST_CASE("execRaw") {
SECTION("uses correct arbitrary instance for arguments") {
const auto values = execRaw([](
const Predictable &a, const Predictable &b, const Predictable &c) {
return std::make_tuple(a, b, c);
})(Random(), 0).value().first;
REQUIRE(isArbitraryPredictable(std::get<0>(values)));
REQUIRE(isArbitraryPredictable(std::get<1>(values)));
REQUIRE(isArbitraryPredictable(std::get<2>(values)));
}
SECTION("shrinks arguments like a tuple") {
using TupleT =
std::tuple<FixedCountdown<1>, FixedCountdown<2>, FixedCountdown<3>>;
const auto execShrinkable = execRaw([](const FixedCountdown<1> &a,
const FixedCountdown<2> &b,
const FixedCountdown<3> &c) {
return std::make_tuple(a, b, c);
})(Random(), 0);
const auto tupleShrinkable =
gen::tuple(gen::arbitrary<FixedCountdown<1>>(),
gen::arbitrary<FixedCountdown<2>>(),
gen::arbitrary<FixedCountdown<3>>())(Random(), 0);
const auto mappedShrinkable = shrinkable::map(
execShrinkable,
[](std::pair<TupleT, Recipe> &&x) { return std::move(x.first); });
REQUIRE(mappedShrinkable == tupleShrinkable);
}
prop(
"traversing a random path through the shrink tree yields the expected"
" values",
[] {
const auto shrinkable = testExecGen<5>()(Random(), 0);
const auto path =
*gen::container<std::vector<bool>>(gen::arbitrary<bool>());
auto accepted = shrinkable.value().first;
auto acceptedShrinkable = shrinkable;
auto shrinks = shrinkable.shrinks();
auto i = std::size_t(0);
auto x = 5;
for (bool accept : path) {
if (i >= accepted.size()) {
RC_ASSERT(!shrinks.next());
break;
}
auto shrink = *shrinks.next();
auto actual = shrink.value().first;
auto expected = accepted;
expected[i] = --x;
// First element determines the size
expected.resize(expected[0] + 1);
RC_ASSERT(actual == expected);
if (accept) {
accepted = expected;
acceptedShrinkable = shrink;
shrinks = shrink.shrinks();
}
if (x == 0) {
x = 5;
i++;
}
}
});
prop(
"the number of shrinks is never never more than the sum of the number"
" of shrinks for the requested values",
[] {
const auto shrinkable = testExecGen<5>()(Random(), 0);
const auto i = *gen::inRange<int>(0, 5);
const auto shrink = *seq::at(shrinkable.shrinks(), i);
const auto value = shrink.value().first;
const auto maxShrinks =
std::accumulate(begin(value), end(value), std::size_t(0));
RC_ASSERT(seq::length(shrink.shrinks()) <= maxShrinks);
});
prop("passes on the correct size",
[] {
const auto expectedSize = *gen::nonNegative<int>();
const auto n = *gen::inRange<std::size_t>(1, 10);
const auto shrinkable = execRaw([=](const PassedSize &sz) {
*genFixedCountdown(3); // Force some shrinks
std::vector<int> sizes;
sizes.push_back(sz.value);
while (sizes.size() < n) {
sizes.push_back(*genSize());
}
return sizes;
})(Random(), expectedSize);
auto valueShrinkable =
shrinkable::map(shrinkable,
[](std::pair<std::vector<int>, Recipe> &&x) {
return std::move(x.first);
});
RC_ASSERT(shrinkable::all(
valueShrinkable,
[=](const Shrinkable<std::vector<int>> &x) {
auto sizes = x.value();
return std::all_of(begin(sizes),
end(sizes),
[=](int sz) { return sz == expectedSize; });
}));
});
prop("passed generators are unique",
[](const Random &initial) {
const auto n = *gen::inRange<std::size_t>(1, 10);
const auto randoms = execRaw([=](const PassedRandom &rnd) {
std::set<Random> lambdaRandoms;
lambdaRandoms.insert(rnd.value);
while (lambdaRandoms.size() < n) {
lambdaRandoms.insert(*genRandom());
}
return lambdaRandoms;
})(initial, 0).value().first;
RC_ASSERT(randoms.size() == n);
});
prop("passed randoms do not change with shrinking",
[](const Random &initial) {
const auto n = *gen::inRange<std::size_t>(1, 10);
const auto shrinkable = execRaw([=](const PassedRandom &rnd) {
std::vector<Random> randoms;
randoms.push_back(rnd.value);
while (randoms.size() < n) {
randoms.push_back(*genRandom());
}
*genFixedCountdown(3);
return randoms;
})(initial, 0);
const auto valueShrinkable =
shrinkable::map(shrinkable,
[](std::pair<std::vector<Random>, Recipe> &&x) {
return std::move(x.first);
});
const auto head = valueShrinkable.value();
RC_ASSERT(
shrinkable::all(valueShrinkable,
[=](const Shrinkable<std::vector<Random>> &x) {
return x.value() == head;
}));
});
SECTION("the ingredients of the recipe exactly match the generated value") {
REQUIRE(shrinkable::all(
testExecGen<3>()(Random(), 0),
[](const Shrinkable<std::pair<std::vector<int>, Recipe>> &x) {
using ArgTuple = std::tuple<FixedCountdown<3>>;
const auto pair = x.value();
const auto recipe = pair.second;
std::vector<int> actual;
const auto argTuple =
recipe.ingredients.front().value().get<ArgTuple>();
actual.push_back(std::get<0>(argTuple).value);
auto it = recipe.ingredients.begin() + 1;
for (; it != end(recipe.ingredients); it++) {
actual.push_back(it->value().get<int>());
}
return actual == pair.first;
}));
}
SECTION("works with non-copyable types") {
const auto shrinkable =
execRaw([=](NonCopyable nc) { return nc; })(Random(), 0);
REQUIRE(isArbitraryPredictable(shrinkable.value().first));
}
SECTION("empty arguments don't show up in tuple") {
const auto value = execRaw([] { return 0; })(Random(), 0).value();
REQUIRE(value.second.ingredients.empty());
}
prop("disallows nested use of operator*",
[](const GenParams ¶ms) {
const auto gen = execRaw([] {
return *Gen<int>([](const Random &, int) {
*gen::just(1337);
return shrinkable::just(1337);
});
});
const auto shrinkable = gen(params.random, params.size);
RC_ASSERT_THROWS(shrinkable.value());
});
}