diff options
author | Robert Hensing <robert@roberthensing.nl> | 2024-01-30 18:18:27 +0100 |
---|---|---|
committer | eldritch horrors <pennae@lix.systems> | 2024-05-02 19:34:04 +0200 |
commit | 2eec547d7d79be3a100d0a1f7f3d3a9a551f94fb (patch) | |
tree | d1617ffc8bf185d64423562805d37d2798dfdff2 /tests/unit/libstore-support | |
parent | fae6ae2122b882b77d441b31a0021a6f15f6b03b (diff) |
test: Generate distinct path names
Gen: :just is the constant generator. Don't just return that!
(cherry picked from commit 69bbd5852af9b2f0b794162bd1debcdf64fc6648)
Change-Id: Id6e58141f5a42a1f67bd11d48c87b32a3ebd0500
Diffstat (limited to 'tests/unit/libstore-support')
-rw-r--r-- | tests/unit/libstore-support/tests/path.cc | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/tests/unit/libstore-support/tests/path.cc b/tests/unit/libstore-support/tests/path.cc index bf45ffd91..8ddda8027 100644 --- a/tests/unit/libstore-support/tests/path.cc +++ b/tests/unit/libstore-support/tests/path.cc @@ -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 |