Skip to content

Commit

Permalink
fix comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h committed Mar 18, 2024
1 parent 569a8ac commit 57ad91b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/dice/template-library/flex_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ namespace dice::template_library {
size_t size_ = 0;
std::array<T, max_extent> data_;

constexpr auto operator<=>(flex_array_inner const &) const noexcept = default;
constexpr auto operator<=>(flex_array_inner const &other) const noexcept requires (std::three_way_comparable<T>) {
std::span<T const> const self_s{data_.data(), size_};
std::span<T const> const other_s{other.data_.data(), other.size_};

return std::lexicographical_compare_three_way(self_s.begin(), self_s.end(), other_s.begin(), other_s.end());
}
};
} // namespace detail_flex_array

Expand Down
29 changes: 29 additions & 0 deletions tests/tests_flex_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ TEST_SUITE("flex_array") {
CHECK(std::ranges::equal(f, std::array{6, 7, 8, 9, 10}));
CHECK(std::ranges::equal(f2, std::array{1, 2, 3, 4, 5}));
}

SUBCASE("cmp") {
flex_array<int, 5> f{1, 2, 3, 4, 5};
flex_array<int, 5> f2{6, 7, 8, 9, 10};

CHECK_EQ(f <=> f2, std::strong_ordering::less);
CHECK_EQ(f <=> f, std::strong_ordering::equal);
CHECK_EQ(f2 <=> f, std::strong_ordering::greater);
}

SUBCASE("no-cmp") {
struct uncomparable {};
flex_array<uncomparable, 5> f; // checking if this compiles
}
}

TEST_CASE("dynamic size") {
Expand Down Expand Up @@ -152,6 +166,21 @@ TEST_SUITE("flex_array") {
CHECK(std::ranges::equal(f, std::array{6, 7, 8}));
CHECK(std::ranges::equal(f2, std::array{1, 2, 3, 4, 5}));
}

SUBCASE("cmp") {
flex_array<int, dynamic_extent, 5> f{1, 2, 3, 4, 5};
flex_array<int, dynamic_extent, 5> f2{6, 7, 8};
flex_array<int, dynamic_extent, 5> f3{5, 6, 7, 8, 9};

CHECK_EQ(f <=> f2, std::strong_ordering::less);
CHECK_EQ(f <=> f, std::strong_ordering::equal);
CHECK_EQ(f3 <=> f, std::strong_ordering::greater);
}

SUBCASE("no-cmp") {
struct uncomparable {};
flex_array<uncomparable, dynamic_extent, 5> f; // checking if this compiles
}
}

TEST_CASE("converting ctors") {
Expand Down

0 comments on commit 57ad91b

Please sign in to comment.