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