Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2019-10-07 20:24:37 +0200
committerMartin Hořeňovský2019-10-07 21:53:07 +0200
commita5a22cdadb0e0573de7dc2547165736349439298 (patch)
tree048f9210f2f08d453037c3d634c559ce227f4f44 /projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
parent535da5c5132bcbba6f29c60ed6bb7ded29b9ec3e (diff)
downloadcatch3-a5a22cdadb0e0573de7dc2547165736349439298.tar.gz
Have the random generators use the global rng instance
This means that if you nest multiple random generators inside one test case, they will not return the same sequence of numbers. Idea taken from #1736 by Amit Herman. Closes #1736 Closes #1734
Diffstat (limited to 'projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp')
-rw-r--r--projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp27
1 files changed, 26 insertions, 1 deletions
diff --git a/projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp b/projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
index dbc1d957..076c91a9 100644
--- a/projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
+++ b/projects/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
@@ -250,7 +250,7 @@ int const& TestGen::get() const {
}
-TEST_CASE("GENERATE capture macros", "[generators][internals][.approvals]") {
+TEST_CASE("GENERATE capture macros", "[generators][internals][approvals]") {
auto value = GENERATE(take(10, random(0, 10)));
non_copyable nc; nc.value = value;
@@ -258,3 +258,28 @@ TEST_CASE("GENERATE capture macros", "[generators][internals][.approvals]") {
auto value2 = GENERATE_REF(Catch::Generators::GeneratorWrapper<int>(std::unique_ptr<Catch::Generators::IGenerator<int>>(new TestGen(nc))));
REQUIRE(value == value2);
}
+
+TEST_CASE("Multiple random generators in one test case output different values", "[generators][internals][approvals]") {
+ SECTION("Integer") {
+ auto random1 = Catch::Generators::random(0, 1000);
+ auto random2 = Catch::Generators::random(0, 1000);
+ size_t same = 0;
+ for (size_t i = 0; i < 1000; ++i) {
+ same += random1.get() == random2.get();
+ random1.next(); random2.next();
+ }
+ // 0.5% seems like a sane bound for random identical elements within 1000 runs
+ REQUIRE(same < 5);
+ }
+ SECTION("Float") {
+ auto random1 = Catch::Generators::random(0., 1000.);
+ auto random2 = Catch::Generators::random(0., 1000.);
+ size_t same = 0;
+ for (size_t i = 0; i < 1000; ++i) {
+ same += random1.get() == random2.get();
+ random1.next(); random2.next();
+ }
+ // 0.5% seems like a sane bound for random identical elements within 1000 runs
+ REQUIRE(same < 5);
+ }
+}