From bbd7d5de090e200dbcf7e925e25b0b273c4290ff Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 11 May 2023 23:22:35 -0400 Subject: [PATCH] Fix some bounds in rapid check instances `inRange` is exclusive not inclusive: https://github.com/emil-e/rapidcheck/blob/master/doc/generators.md#usage Furthermore, use `std::variant_size_v` so we use the right number automatically. Finally, make the `switch` assert the discriminant is in bounds as expected. --- src/libexpr/tests/value/context.cc | 6 ++++-- src/libstore/tests/derived-path.cc | 6 ++++-- src/libstore/tests/outputs-spec.cc | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/libexpr/tests/value/context.cc b/src/libexpr/tests/value/context.cc index 27d6920b0..0d9381577 100644 --- a/src/libexpr/tests/value/context.cc +++ b/src/libexpr/tests/value/context.cc @@ -95,13 +95,15 @@ Gen Arbitrary::arbitra Gen Arbitrary::arbitrary() { - switch (*gen::inRange(0, 2)) { + switch (*gen::inRange(0, std::variant_size_v)) { case 0: return gen::just(*gen::arbitrary()); case 1: return gen::just(*gen::arbitrary()); - default: + case 2: return gen::just(*gen::arbitrary()); + default: + assert(false); } } diff --git a/src/libstore/tests/derived-path.cc b/src/libstore/tests/derived-path.cc index e6d32dbd0..160443ec1 100644 --- a/src/libstore/tests/derived-path.cc +++ b/src/libstore/tests/derived-path.cc @@ -27,11 +27,13 @@ Gen Arbitrary::arbitrary() Gen Arbitrary::arbitrary() { - switch (*gen::inRange(0, 1)) { + switch (*gen::inRange(0, std::variant_size_v)) { case 0: return gen::just(*gen::arbitrary()); - default: + case 1: return gen::just(*gen::arbitrary()); + default: + assert(false); } } diff --git a/src/libstore/tests/outputs-spec.cc b/src/libstore/tests/outputs-spec.cc index 984d1d963..bf8deaa9d 100644 --- a/src/libstore/tests/outputs-spec.cc +++ b/src/libstore/tests/outputs-spec.cc @@ -206,15 +206,17 @@ using namespace nix; Gen Arbitrary::arbitrary() { - switch (*gen::inRange(0, 1)) { + switch (*gen::inRange(0, std::variant_size_v)) { case 0: return gen::just((OutputsSpec) OutputsSpec::All { }); - default: + case 1: return gen::just((OutputsSpec) OutputsSpec::Names { *gen::nonEmpty(gen::container(gen::map( gen::arbitrary(), [](StorePathName n) { return n.name; }))), }); + default: + assert(false); } }