test: Generate distinct path names

Gen: :just is the constant generator. Don't just return that!

(cherry picked from commit 69bbd5852af9b2f0b794162bd1debcdf64fc6648)
Change-Id: Id6e58141f5a42a1f67bd11d48c87b32a3ebd0500
This commit is contained in:
Robert Hensing 2024-01-30 18:18:27 +01:00 committed by eldritch horrors
parent fae6ae2122
commit 2eec547d7d

View file

@ -1,3 +1,4 @@
#include <rapidcheck/gen/Arbitrary.h>
#include <regex> #include <regex>
#include <rapidcheck.h> #include <rapidcheck.h>
@ -20,60 +21,60 @@ void showValue(const StorePath & p, std::ostream & os)
namespace rc { namespace rc {
using namespace nix; using namespace nix;
Gen<StorePathName> Arbitrary<StorePathName>::arbitrary() Gen<char> storePathChar()
{ {
auto len = *gen::inRange<size_t>( return rc::gen::apply([](uint8_t i) -> char {
1, switch (i) {
StorePath::MaxPathLen - StorePath::HashLen);
std::string pre;
pre.reserve(len);
for (size_t c = 0; c < len; ++c) {
switch (auto i = *gen::inRange<uint8_t>(0, 10 + 2 * 26 + 6)) {
case 0 ... 9: case 0 ... 9:
pre += static_cast<uint8_t>('0' + i); return '0' + i;
break;
case 10 ... 35: case 10 ... 35:
pre += static_cast<uint8_t>('A' + (i - 10)); return 'A' + (i - 10);
break;
case 36 ... 61: case 36 ... 61:
pre += static_cast<uint8_t>('a' + (i - 36)); return 'a' + (i - 36);
break;
case 62: case 62:
pre += '+'; return '+';
break;
case 63: case 63:
pre += '-'; return '-';
break;
case 64: case 64:
pre += '.'; return '.';
break;
case 65: case 65:
pre += '_'; return '_';
break;
case 66: case 66:
pre += '?'; return '?';
break;
case 67: case 67:
pre += '='; return '=';
break;
default: default:
assert(false); assert(false);
} }
} },
gen::inRange<uint8_t>(0, 10 + 2 * 26 + 6));
}
return gen::just(StorePathName { Gen<StorePathName> Arbitrary<StorePathName>::arbitrary()
.name = std::move(pre), {
}); return gen::construct<StorePathName>(
gen::suchThat(
gen::container<std::string>(storePathChar()),
[](const std::string & s) {
return
!( s == ""
|| s == "."
|| s == ".."
|| s.starts_with(".-")
|| s.starts_with("..-")
);
}
)
);
} }
Gen<StorePath> Arbitrary<StorePath>::arbitrary() Gen<StorePath> Arbitrary<StorePath>::arbitrary()
{ {
return gen::just(StorePath { return
*gen::arbitrary<Hash>(), gen::construct<StorePath>(
(*gen::arbitrary<StorePathName>()).name, gen::arbitrary<Hash>(),
}); gen::apply([](StorePathName n){ return n.name; }, gen::arbitrary<StorePathName>())
);
} }
} // namespace rc } // namespace rc