Skip to content

Commit

Permalink
Fix rapidcheck allow_dups issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rroelke committed Feb 6, 2025
1 parent 2a6839b commit d9f7610
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
24 changes: 18 additions & 6 deletions test/src/unit-sparse-global-order-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3169,8 +3169,7 @@ struct Arbitrary<FxRun1D> {
templates::Dimension<DIMENSION_TYPE>,
std::vector<templates::Domain<CoordType>>,
std::vector<templates::Fragment1D<int, int>>> fragments,
int num_user_cells,
bool allow_dups) {
int num_user_cells) {
FxRun1D instance;
std::tie(
instance.array.allow_dups_,
Expand All @@ -3179,13 +3178,11 @@ struct Arbitrary<FxRun1D> {
instance.fragments) = fragments;

instance.num_user_cells = num_user_cells;
instance.array.allow_dups_ = allow_dups;

return instance;
},
fragments,
num_user_cells,
gen::arbitrary<bool>());
num_user_cells);
}
};

Expand Down Expand Up @@ -3232,7 +3229,22 @@ struct Arbitrary<FxRun2D> {
auto d0 = gen::arbitrary<templates::Dimension<Dim0Type>>();
auto d1 = gen::arbitrary<templates::Dimension<Dim1Type>>();

auto fragments = gen::mapcat(gen::tuple(allow_dups, d0, d1), [](auto arg) {
auto domain =
gen::suchThat(gen::tuple(allow_dups, d0, d1), [](const auto& arg) {
bool allow_dups;
templates::Dimension<Dim0Type> d0;
templates::Dimension<Dim1Type> d1;
std::tie(allow_dups, d0, d1) = arg;
if (allow_dups) {
return true;
} else {
// need to ensure that rapidcheck uniqueness can generate enough
// cases
return (d0.domain.num_cells() + d1.domain.num_cells()) >= 12;
}
});

auto fragments = gen::mapcat(domain, [](auto arg) {
bool allow_dups;
templates::Dimension<Dim0Type> d0;
templates::Dimension<Dim1Type> d1;
Expand Down
10 changes: 8 additions & 2 deletions test/support/rapidcheck/array_templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ Gen<Fragment1D<D, Att...>> make_fragment_1d(

using Cell = std::tuple<D, Att...>;

auto uniqueCoords = [](const Cell& cell) { return std::get<0>(cell); };

auto cells = gen::nonEmpty(
allow_duplicates ? gen::container<std::vector<Cell>>(cell) :
gen::unique<std::vector<Cell>>(cell));
gen::uniqueBy<std::vector<Cell>>(cell, uniqueCoords));

return gen::map(cells, [](std::vector<Cell> cells) {
std::vector<D> coords;
Expand Down Expand Up @@ -204,9 +206,13 @@ Gen<Fragment2D<D1, D2, Att...>> make_fragment_2d(

auto cell = gen::tuple(coord_d1, coord_d2, gen::arbitrary<Att>()...);

auto uniqueCoords = [](const Cell& cell) {
return std::make_pair(std::get<0>(cell), std::get<1>(cell));
};

auto cells = gen::nonEmpty(
allow_duplicates ? gen::container<std::vector<Cell>>(cell) :
gen::unique<std::vector<Cell>>(cell));
gen::uniqueBy<std::vector<Cell>>(cell, uniqueCoords));

return gen::map(cells, [](std::vector<Cell> cells) {
std::vector<D1> coords_d1;
Expand Down
11 changes: 11 additions & 0 deletions test/support/src/array_templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ struct Domain {
, upper_bound(std::max(d1, d2)) {
}

uint64_t num_cells() const {
// FIXME: this is incorrect for 64-bit domains which need to check overflow
if (std::is_signed<D>::value) {
return static_cast<int64_t>(upper_bound) -
static_cast<int64_t>(lower_bound) + 1;
} else {
return static_cast<uint64_t>(upper_bound) -
static_cast<uint64_t>(lower_bound) + 1;
}
}

bool contains(D point) const {
return lower_bound <= point && point <= upper_bound;
}
Expand Down

0 comments on commit d9f7610

Please sign in to comment.