-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathTestingTests.cpp
More file actions
562 lines (482 loc) · 20.4 KB
/
Copy pathTestingTests.cpp
File metadata and controls
562 lines (482 loc) · 20.4 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include <algorithm>
#include "rapidcheck/detail/TestListenerAdapter.h"
#include "detail/Testing.h"
#include "util/Generators.h"
#include "util/GenUtils.h"
#include "util/ShrinkableUtils.h"
#include "util/MockTestListener.h"
using namespace rc;
using namespace rc::test;
using namespace rc::detail;
namespace {
TestListenerAdapter dummyListener;
} // namespace
template <typename Testable>
SearchResult searchTestable(Testable &&testable,
const TestParams ¶ms,
TestListener &listener = dummyListener) {
return searchProperty(
toProperty(std::forward<Testable>(testable)), params, listener);
}
template <typename Testable>
TestResult testTestable(Testable &&testable,
const TestParams ¶ms,
TestListener &listener = dummyListener) {
return testProperty(toProperty(std::forward<Testable>(testable)),
TestMetadata(),
params,
dummyListener);
}
TEST_CASE("searchProperty") {
prop("reports correct number of successes and discards",
[](const TestParams ¶ms, int limit) {
int successes = 0;
int discards = 0;
const auto result = searchTestable([&] {
const auto x = *gen::arbitrary<int>();
if ((x % 3) == 0) {
discards++;
RC_DISCARD("");
}
RC_ASSERT(x < limit);
successes++;
}, params);
RC_ASSERT(result.numSuccess == successes);
RC_ASSERT(result.numDiscarded == discards);
});
prop("runs all test cases if no cases fail",
[](const TestParams ¶ms) {
auto numCases = 0;
const auto result = searchTestable([&] { numCases++; }, params);
RC_ASSERT(numCases == params.maxSuccess);
RC_ASSERT(result.type == SearchResult::Type::Success);
RC_ASSERT(result.numSuccess == params.maxSuccess);
RC_ASSERT(!result.failure);
});
prop("returns correct information about failing case",
[](const TestParams ¶ms, const std::string &description) {
RC_PRE(params.maxSuccess > 0);
int size = 0;
int caseIndex = 0;
const auto targetSuccess = *gen::inRange<int>(0, params.maxSuccess);
const auto result = searchTestable([&] {
size = *genSize();
if (caseIndex >= targetSuccess) {
return CaseResult(CaseResult::Type::Failure, description);
}
caseIndex++;
return CaseResult(CaseResult::Type::Success);
}, params);
RC_ASSERT(result.type == SearchResult::Type::Failure);
RC_ASSERT(result.failure);
RC_ASSERT(result.failure->size == size);
RC_ASSERT(result.failure->shrinkable.value().result.description ==
description);
});
prop("gives up if too many test cases are discarded",
[](const TestParams ¶ms, const std::string &description) {
RC_PRE(params.maxSuccess > 0);
const auto targetSuccess = *gen::inRange<int>(0, params.maxSuccess);
int size = 0;
int numTests = 0;
const auto result = searchTestable([&] {
numTests++;
size = *genSize();
if (numTests > targetSuccess) {
return CaseResult(CaseResult::Type::Discard, description);
}
return CaseResult(CaseResult::Type::Success);
}, params);
RC_ASSERT(result.type == SearchResult::Type::GaveUp);
RC_ASSERT(result.failure);
RC_ASSERT(result.failure->size == size);
RC_ASSERT(result.failure->shrinkable.value().result.description ==
description);
});
prop("does not give up if not enough tests are discarded",
[](const TestParams ¶ms) {
const auto maxDiscards = params.maxSuccess * params.maxDiscardRatio;
const auto targetDiscard = *gen::inRange<int>(0, maxDiscards + 1);
int numTests = 0;
const auto result = searchTestable([&] {
numTests++;
RC_PRE(numTests > targetDiscard);
}, params);
RC_ASSERT(result.type == SearchResult::Type::Success);
RC_ASSERT(result.numSuccess == params.maxSuccess);
});
prop("if maxSuccess > 1, the max size used is maxSize",
[](const TestParams ¶ms) {
RC_PRE(params.maxSuccess > 1);
int usedMax = 0;
const auto property = [&] { usedMax = std::max(*genSize(), usedMax); };
searchTestable(property, params);
RC_ASSERT(usedMax == params.maxSize);
});
prop("if maxSuccess > maxSize, all sizes will be used",
[] {
TestParams params;
params.maxSize = *gen::inRange(0, 100);
params.maxSuccess = *gen::inRange(params.maxSuccess + 1, 200);
std::vector<int> frequencies(params.maxSize + 1, 0);
const auto property = [&] { frequencies[*genSize()]++; };
searchTestable(property, params);
RC_ASSERT(std::count(begin(frequencies), end(frequencies), 0) == 0);
});
prop("should increase size eventually if enough tests are discarded",
[](TestParams params) {
params.maxDiscardRatio = 100;
const auto result =
searchTestable([] { RC_PRE(*genSize() != 0); }, params);
RC_ASSERT(result.type == SearchResult::Type::Success);
});
prop("does not include empty tags in tags",
[](const TestParams ¶ms) {
const auto result = searchTestable([] {}, params);
RC_ASSERT(result.tags.empty());
});
prop("does not include tags for discarded tests in tags",
[](const TestParams ¶ms) {
const auto result = searchTestable([] {
RC_TAG(0);
RC_DISCARD("");
}, params);
RC_ASSERT(result.tags.empty());
});
prop("does not include tags for failed tests in tags",
[](const TestParams ¶ms) {
const auto result = searchTestable([] {
RC_TAG(0);
RC_FAIL("");
}, params);
RC_ASSERT(result.tags.empty());
});
prop("does not include empty tags in tags",
[](const TestParams ¶ms) {
const auto expected = *gen::container<std::vector<Tags>>(
params.maxSuccess, gen::nonEmpty<Tags>());
std::size_t i = 0;
const auto result = searchTestable([&] {
for (const auto &tag : expected[i++]) {
ImplicitParam<param::CurrentPropertyContext>::value()->addTag(tag);
}
}, params);
RC_ASSERT(result.tags == expected);
});
prop("does not include tags applied from generators",
[](const TestParams ¶ms) {
const auto result = searchTestable([&] {
*Gen<int>([](const Random &, int) {
ImplicitParam<param::CurrentPropertyContext>::value()->addTag(
"foobar");
return shrinkable::just(1337);
});
}, params);
RC_ASSERT(result.tags.empty());
});
prop("calls onTestCaseFinished for each successful test",
[](const TestParams ¶ms, int limit) {
std::vector<CaseDescription> descriptions;
MockTestListener listener;
listener.onTestCaseFinishedCallback =
[&](const CaseDescription &desc) { descriptions.push_back(desc); };
std::vector<CaseDescription> expected;
const auto result = searchTestable([&] {
CaseDescription desc;
const auto x = *gen::arbitrary<int>();
if ((x % 3) == 0) {
desc.result.type = CaseResult::Type::Discard;
} else if (x < limit) {
desc.result.type = CaseResult::Type::Failure;
} else {
desc.result.type = CaseResult::Type::Success;
}
const auto strx = std::to_string(x);
desc.result.description = strx;
desc.tags.push_back(strx);
desc.example = [=] { return Example{{"int", strx}}; };
ImplicitParam<param::CurrentPropertyContext>::value()->addTag(strx);
expected.push_back(desc);
return desc.result;
}, params, listener);
RC_ASSERT(descriptions == expected);
});
prop("the failure information reproduces identical shrinkables",
[](TestParams params) {
const auto max = *gen::inRange<int>(0, 2000);
const auto property = toProperty([=](int a, int b) {
if ((a > max) || (b > max)) {
throw std::to_string(a) + " " + std::to_string(b);
}
});
params.maxSuccess = 2000;
params.maxSize = kNominalSize;
const auto result = searchProperty(property, params, dummyListener);
RC_ASSERT(result.failure);
const auto shrinkable =
property(result.failure->random, result.failure->size);
RC_ASSERT(result.failure->shrinkable.value() == shrinkable.value());
});
}
namespace {
Shrinkable<CaseDescription> countdownEven(int start) {
return shrinkable::map(countdownShrinkable(start),
[=](int x) {
CaseDescription desc;
desc.result.type = ((x % 2) == 0)
? CaseResult::Type::Failure
: CaseResult::Type::Success;
desc.result.description = std::to_string(x);
return desc;
});
}
}
TEST_CASE("shrinkTestCase") {
prop("returns the minimum shrinkable",
[] {
const auto target = *gen::positive<int>();
const auto shrinkable =
shrinkable::map(shrinkable::shrinkRecur(
std::numeric_limits<int>::max(),
[](int x) { return shrink::towards(x, 0); }),
[=](int x) {
CaseDescription desc;
desc.result.type = (x >= target)
? CaseResult::Type::Failure
: CaseResult::Type::Success;
desc.result.description = std::to_string(x);
return desc;
});
const auto result = shrinkTestCase(shrinkable, dummyListener);
RC_ASSERT(result.first.value().result.type ==
CaseResult::Type::Failure);
RC_ASSERT(result.first.value().result.description ==
std::to_string(target));
});
prop("the path length is the number of successful shrinks",
[] {
const auto start = *gen::suchThat(gen::inRange<int>(0, 100),
[](int x) { return (x % 2) == 0; });
const auto shrinkable = countdownEven(start);
const auto result = shrinkTestCase(shrinkable, dummyListener);
RC_ASSERT(result.second.size() == std::size_t(start / 2));
});
prop("walking the path gives the same result",
[] {
const auto start = *gen::suchThat(gen::inRange<int>(0, 100),
[](int x) { return (x % 2) == 0; });
const auto shrinkable = countdownEven(start);
const auto shrinkResult = shrinkTestCase(shrinkable, dummyListener);
const auto walkResult =
shrinkable::walkPath(shrinkable, shrinkResult.second);
RC_ASSERT(walkResult);
RC_ASSERT(shrinkResult.first.value() == walkResult->value());
});
prop("calls onShrinkTried for each shrink tried",
[] {
const auto start = *gen::suchThat(gen::inRange<int>(0, 100),
[](int x) { return (x % 2) == 0; });
const auto shrinkable = countdownEven(start);
MockTestListener listener;
int acceptedBalance = 0;
listener.onShrinkTriedCallback =
[&](const CaseDescription &desc, bool accepted) {
const auto x = std::stoi(desc.result.description);
RC_ASSERT(((x % 2) == 0) == accepted);
acceptedBalance += accepted ? 1 : -1;
};
const auto result = shrinkTestCase(shrinkable, listener);
});
}
TEST_CASE("testProperty") {
prop("returns the correct shrink path on a failing case",
[](TestParams params) {
RC_PRE(params.maxSuccess > 0);
params.disableShrinking = false;
const auto evenInteger =
gen::scale(0.25,
gen::suchThat(gen::positive<int>(),
[](int x) { return (x % 2) == 0; }));
const auto values = *gen::pair(evenInteger, evenInteger);
const auto results = testTestable([&] {
const auto v1 = *genFixedCountdown(values.first);
const auto v2 = *genFixedCountdown(values.second);
return ((v1 % 2) != 0) || ((v2 % 2) != 0);
}, params, dummyListener);
FailureResult failure;
RC_ASSERT(results.match(failure));
const auto numShrinks = (values.first / 2) + (values.second / 2);
// Every shrink should be the second shrink, thus fill with 1
const auto expected = std::vector<std::size_t>(numShrinks, 1);
RC_ASSERT(failure.reproduce.shrinkPath == expected);
});
prop("returns a correct counter-example",
[](const TestParams ¶ms, std::vector<int> values) {
RC_PRE(params.maxSuccess > 0);
const auto results =
testTestable([&](FixedCountdown<0>, FixedCountdown<0>) {
for (auto value : values) {
*gen::just(value);
}
return false;
}, params, dummyListener);
Example expected;
expected.reserve(values.size() + 1);
std::tuple<FixedCountdown<0>, FixedCountdown<0>> expectedArgs(
FixedCountdown<0>{}, FixedCountdown<0>{});
expected.push_back(std::make_pair(
typeToString<decltype(expectedArgs)>(), toString(expectedArgs)));
std::transform(begin(values),
end(values),
std::back_inserter(expected),
[](int x) {
return std::make_pair(typeToString<int>(),
toString(x));
});
FailureResult failure;
RC_ASSERT(results.match(failure));
RC_ASSERT(failure.counterExample == expected);
});
prop("counter-example is not affected by nested tests",
[](const TestParams ¶ms1, const TestParams ¶ms2) {
RC_PRE(params1.maxSuccess > 0);
const auto results = testTestable([&] {
*gen::just<std::string>("foo");
auto innerResults = testTestable([&] {
*gen::just<std::string>("bar");
*gen::just<std::string>("baz");
}, params2, dummyListener);
return false;
}, params1, dummyListener);
FailureResult failure;
RC_ASSERT(results.match(failure));
Example expected{
{typeToString<std::string>(), toString(std::string("foo"))}};
RC_ASSERT(failure.counterExample == expected);
});
prop("on failure, description contains message",
[](const TestParams ¶ms, const std::string &description) {
RC_PRE(params.maxSuccess > 0);
const auto results = testTestable(
[&] { RC_FAIL(description); }, params, dummyListener);
FailureResult failure;
RC_ASSERT(results.match(failure));
RC_ASSERT(failure.description.find(description) != std::string::npos);
});
prop("on giving up, description contains message",
[](const TestParams ¶ms, const std::string &description) {
RC_PRE(params.maxSuccess > 0);
const auto results = testTestable(
[&] { RC_DISCARD(description); }, params, dummyListener);
GaveUpResult gaveUp;
RC_ASSERT(results.match(gaveUp));
RC_ASSERT(gaveUp.description.find(description) != std::string::npos);
});
prop("running the same test with the same TestParams yields identical runs",
[](const TestParams ¶ms) {
std::vector<std::vector<int>> values;
const auto property = [&] {
const auto x = *gen::arbitrary<std::vector<int>>();
values.push_back(x);
auto result = std::find(begin(x), end(x), 50);
return result == end(x);
};
const auto results1 = testTestable(property, params, dummyListener);
auto values1 = std::move(values);
values = std::vector<std::vector<int>>();
const auto results2 = testTestable(property, params, dummyListener);
auto values2 = std::move(values);
RC_ASSERT(results1 == results2);
RC_ASSERT(values1 == values2);
});
prop("correctly reports test case distribution",
[] {
auto allTags =
*gen::container<std::vector<std::vector<std::string>>>(
gen::scale(0.1, gen::arbitrary<std::vector<std::string>>()));
TestParams params;
params.maxSize = *gen::inRange(0, 200);
params.maxSuccess = static_cast<int>(allTags.size());
auto i = 0;
const auto property = [&] {
const auto &tags = allTags[i++];
for (const auto &tag : tags) {
ImplicitParam<param::CurrentPropertyContext>::value()->addTag(tag);
}
};
const auto result = testTestable(property, params, dummyListener);
Distribution expected;
for (auto &tags : allTags) {
if (!tags.empty()) {
expected[tags]++;
}
}
SuccessResult success;
RC_ASSERT(result.match(success));
RC_ASSERT(success.distribution == expected);
});
prop("does not include untagged cases in distribution",
[](const TestParams ¶ms) {
const auto result = testTestable([] {}, params, dummyListener);
SuccessResult success;
RC_ASSERT(result.match(success));
RC_ASSERT(success.distribution.empty());
});
prop("does not shrink result if disableShrinking is set",
[](TestParams params) {
RC_PRE(params.maxSuccess > 0);
params.disableShrinking = true;
const auto result = testTestable([] {
*Gen<int>([](const Random &, int) {
return shrinkable::just(1337, seq::just(shrinkable::just(0)));
});
RC_FAIL("oh noes");
}, params, dummyListener);
FailureResult failure;
RC_ASSERT(result.match(failure));
RC_ASSERT(failure.counterExample.front().second == "1337");
});
}
TEST_CASE("reproduceProperty") {
prop("reproduces result from testProperty",
[](const TestMetadata &metadata, TestParams params) {
const auto max = *gen::inRange<int>(0, 2000);
const auto property = toProperty([=](int a, int b) {
if ((a > max) || (b > max)) {
throw std::to_string(a) + " " + std::to_string(b);
}
});
params.maxSuccess = 2000;
params.maxSize = kNominalSize;
const auto result =
testProperty(property, metadata, params, dummyListener);
FailureResult failure;
RC_ASSERT(result.match(failure));
const auto reproduced = reproduceProperty(property, failure.reproduce);
FailureResult reproducedFailure;
RC_ASSERT(reproduced.match(reproducedFailure));
RC_ASSERT(failure.description == reproducedFailure.description);
RC_ASSERT(failure.reproduce == reproducedFailure.reproduce);
RC_ASSERT(failure.counterExample == reproducedFailure.counterExample);
RC_ASSERT(reproducedFailure.numSuccess == 0);
});
SECTION("returns error if reproduced result is not a failure") {
const auto property = toProperty([] {});
Reproduce repro;
repro.size = 0;
const auto result = reproduceProperty(property, repro);
Error error;
REQUIRE(result.match(error));
}
SECTION("returns error if shrink path is not valid") {
const auto property = toProperty([] { return false; });
Reproduce repro;
repro.size = 0;
repro.shrinkPath.push_back(100);
const auto result = reproduceProperty(property, repro);
Error error;
REQUIRE(result.match(error));
}
}