Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions include/beman/inplace_vector/inplace_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Software.
*/
#include <algorithm> // for rotate...
#include <array>
#include <compare>
#include <concepts> // for lots...
#include <cstddef> // for size_t
#include <cstdint> // for fixed-width integer types
Expand Down Expand Up @@ -309,6 +310,11 @@ concept container_compatible_range =
template <typename T, std::size_t N>
concept satify_constexpr = N == 0 || std::is_trivial_v<T>;

template <typename T>
concept lessthan_comparable = requires(const T &a, const T &b) {
{ a < b } -> std::convertible_to<bool>;
};

} // namespace beman::details::inplace_vector

// Types implementing the `inplace_vector`'s storage
Expand Down Expand Up @@ -1001,27 +1007,20 @@ struct inplace_vector
insert_range(begin(), il);
}

constexpr friend int /*synth-three-way-result<T>*/
operator<=>(const inplace_vector & x, const inplace_vector & y) {
if (x.size() < y.size())
return -1;
if (x.size() > y.size())
return +1;

bool all_equal = true;
bool all_less = true;
for (size_type i = 0; i < x.size(); ++i) {
constexpr friend auto operator<=>(const inplace_vector &x,
const inplace_vector &y)
requires(std::equality_comparable<T> &&
beman::details::inplace_vector::lessthan_comparable<T>)
{
const auto sz = std::min(x.size(), y.size());
for (std::size_t i = 0; i < sz; ++i) {
if (x[i] < y[i])
all_equal = false;
if (x[i] == y[i])
all_less = false;
return std::strong_ordering::less;
if (y[i] < x[i])
return std::strong_ordering::greater;
}

if (all_equal)
return 0;
if (all_less)
return -1;
return 1;
return x.size() <=> y.size();
}
};

Expand Down
1 change: 1 addition & 0 deletions tests/beman/inplace_vector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ endfunction()
# Tests for official specs
add_gtest(container_requirements)
add_gtest(triviality)
add_gtest(compare)
add_gtest(constructors)
add_gtest(size_n_data)
add_gtest(erasure)
Expand Down
181 changes: 181 additions & 0 deletions tests/beman/inplace_vector/compare.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#include <beman/inplace_vector/inplace_vector.hpp>
#include <gtest/gtest.h>

#include <compare>

using namespace beman;

template <class T>
concept has_threeway = requires(const T &t) {
{ t <=> t };
};

TEST(Compare, threeway_int) {
using T = inplace_vector<int, 4>;
T empty{};
T base{1, 2, 3};
T copy = base;

T greater{4, 5, 6};
T lesser{0, 0, 0};

T bigger{1, 2, 3, 0};
T smaller{1, 2};

T greater_smaller{2, 2};
T lesser_bigger{0, 2, 3, 0};

EXPECT_TRUE((base <=> copy) == 0);
EXPECT_TRUE((base <=> greater) < 0);
EXPECT_TRUE((base <=> lesser) > 0);

EXPECT_TRUE((base <=> bigger) < 0);
EXPECT_TRUE((base <=> smaller) > 0);

EXPECT_TRUE((base <=> greater_smaller) < 0);
EXPECT_TRUE((base <=> lesser_bigger) > 0);
}

TEST(COmpare, threeway_float) {
using T = inplace_vector<float, 4>;
T empty{};
T base{1.0f, 2.0f, 3.0f};
T copy = base;

T greater{4.0f, 5.0f, 6.0f};
T lesser{0.0f, 0.0f, 0.0f};

T bigger{1.0f, 2.0f, 3.0f, 0.0f};
T smaller{1.0f, 2.0f};

T greater_smaller{2.0f, 2.0f};
T lesser_bigger{0.0f, 2.0f, 3.0f, 0.0f};

EXPECT_TRUE((base <=> copy) == 0);

EXPECT_TRUE((base <=> greater) < 0);
EXPECT_TRUE((base <=> lesser) > 0);

EXPECT_TRUE((base <=> bigger) < 0);
EXPECT_TRUE((base <=> smaller) > 0);

EXPECT_TRUE((base <=> greater_smaller) < 0);
EXPECT_TRUE((base <=> lesser_bigger) > 0);
}

TEST(Compare, threeway_comparable1) {
struct comparable1 {
int a;
int b;
constexpr auto operator<=>(const comparable1 &) const = default;
};

static_assert(std::three_way_comparable<comparable1>);
static_assert(has_threeway<comparable1>);
static_assert(std::three_way_comparable<inplace_vector<comparable1, 4>>);
static_assert(has_threeway<inplace_vector<comparable1, 4>>);

using T = inplace_vector<comparable1, 4>;

T empty{};

T base{{1, 2}, {3, 4}};
T copy = base;
T greater{{5, 6}, {7, 8}};
T lesser{{0, 0}, {0, 0}};

T bigger{{1, 2}, {3, 4}, {5, 6}};
T smaller{{1, 2}};

T greater_smaller{{2, 2}, {3, 3}};
T lesser_bigger{{0, 2}, {3, 3}, {0, 0}};

EXPECT_TRUE((empty <=> empty) == 0);
EXPECT_TRUE((base <=> copy) == 0);

EXPECT_TRUE((base <=> greater) < 0);
EXPECT_TRUE((base <=> lesser) > 0);

EXPECT_TRUE((base <=> bigger) < 0);
EXPECT_TRUE((base <=> smaller) > 0);

EXPECT_TRUE((base <=> greater_smaller) < 0);
EXPECT_TRUE((base <=> lesser_bigger) > 0);
}

TEST(Compare, threeway_comparable2) {

struct comparable2 {
int a;
int b;
constexpr bool operator==(const comparable2 &) const = default;
constexpr bool operator<(const comparable2 &other) const {
return a < other.a || (a == other.a && b < other.b);
};
};

static_assert(!std::three_way_comparable<comparable2>);
static_assert(!has_threeway<comparable2>);
static_assert(std::three_way_comparable<inplace_vector<comparable2, 4>>);
static_assert(has_threeway<inplace_vector<comparable2, 4>>);

using T = inplace_vector<comparable2, 4>;

T empty{};
T base{{1, 2}, {3, 4}};
T copy = base;
T greater{{5, 6}, {7, 8}};
T lesser{{0, 0}, {0, 0}};

T bigger{{1, 2}, {3, 4}, {5, 6}};
T smaller{{1, 2}};

T greater_smaller{{2, 2}, {3, 3}};
T lesser_bigger{{0, 2}, {3, 3}, {0, 0}};

EXPECT_TRUE((empty <=> empty) == 0);
EXPECT_TRUE((base <=> copy) == 0);

EXPECT_TRUE((base <=> greater) < 0);
EXPECT_TRUE((base <=> lesser) > 0);

EXPECT_TRUE((base <=> bigger) < 0);
EXPECT_TRUE((base <=> smaller) > 0);

EXPECT_TRUE((base <=> greater_smaller) < 0);
EXPECT_TRUE((base <=> lesser_bigger) > 0);
}

TEST(Compare, threeway_uncomparable) {

struct uncomparable1 {
int a;
};

static_assert(!std::three_way_comparable<uncomparable1>);
static_assert(!has_threeway<uncomparable1>);
static_assert(!std::three_way_comparable<inplace_vector<uncomparable1, 4>>);
static_assert(!has_threeway<inplace_vector<uncomparable1, 4>>);

struct uncomparable2 {
int a;
constexpr bool operator==(const uncomparable2 &) const = default;
};

static_assert(!std::three_way_comparable<uncomparable2>);
static_assert(!has_threeway<uncomparable2>);
static_assert(!std::three_way_comparable<inplace_vector<uncomparable2, 4>>);
static_assert(!has_threeway<inplace_vector<uncomparable2, 4>>);

struct uncomparable3 {
int a;
constexpr auto operator<=>(const uncomparable3 &) const {
return std::partial_ordering::unordered;
}
};

static_assert(!std::three_way_comparable<uncomparable3>);
static_assert(has_threeway<uncomparable3>); // has op but returns unordered
static_assert(!std::three_way_comparable<inplace_vector<uncomparable3, 4>>);
static_assert(!has_threeway<inplace_vector<uncomparable3, 4>>);
}