Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions include/yk/compare/comparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,14 @@ struct promote_comparator : comparator_interface {
const auto res = std::invoke(comp1, x, y);

if constexpr (std::same_as<From, To>) {
if (res == From::equivalent) return std::invoke(comp2, x, y);
return res;
if constexpr (std::same_as<From, std::partial_ordering>) {
if (res == From::equivalent || res == From::unordered) return std::invoke(comp2, x, y);
return res;

} else {
if (res == From::equivalent) return std::invoke(comp2, x, y);
return res;
}

} else {
if (res == From::less) return To::less;
Expand Down
20 changes: 17 additions & 3 deletions test/compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,17 +434,31 @@ BOOST_AUTO_TEST_CASE(ClosureTypeTraits)
}
}

struct PartialPartial
{
double a, b;
std::partial_ordering operator<=>(const PartialPartial&) const noexcept = default;
};

BOOST_AUTO_TEST_CASE(promotion)
{
using namespace yk::comparators;

constexpr double NaN = std::numeric_limits<double>::quiet_NaN();

// partial | promote(partial)
{
const auto partial_comparator = [](double a, double b) -> std::partial_ordering { return a <=> b; };

auto comp = partial_comparator | promote(partial_comparator);
BOOST_TEST((comp(3.14, 1.41) == std::partial_ordering::greater));
BOOST_TEST((comp(3.14, std::numeric_limits<double>::quiet_NaN()) == std::partial_ordering::unordered));
BOOST_TEST((comp(3.14, NaN) == std::partial_ordering::unordered));
}
{
auto comp = extract(&PartialPartial::a) | promote(extract(&PartialPartial::b));
BOOST_TEST((comp(PartialPartial{1.0, NaN}, PartialPartial{2.0, NaN}) == std::partial_ordering::less));
BOOST_TEST(((PartialPartial{NaN, 1.0} <=> PartialPartial{NaN, 2.0}) == std::partial_ordering::unordered));
BOOST_TEST((comp(PartialPartial{NaN, 1.0}, PartialPartial{NaN, 2.0}) == std::partial_ordering::less));
}

// partial | promote(weak)
Expand All @@ -454,7 +468,7 @@ BOOST_AUTO_TEST_CASE(promotion)

auto comp = partial_comparator | promote(weak_comparator);
BOOST_TEST((comp(3.14, 1.41) == std::weak_ordering::greater));
BOOST_TEST((comp(3.14, std::numeric_limits<double>::quiet_NaN()) == std::weak_ordering::less));
BOOST_TEST((comp(3.14, NaN) == std::weak_ordering::less));
}

// partial | promote(strong)
Expand All @@ -464,7 +478,7 @@ BOOST_AUTO_TEST_CASE(promotion)

auto comp = partial_comparator | promote(strong_comparator);
BOOST_TEST((comp(3.14, 1.41) == std::strong_ordering::greater));
BOOST_TEST((comp(3.14, std::numeric_limits<double>::quiet_NaN()) == std::strong_ordering::less));
BOOST_TEST((comp(3.14, NaN) == std::strong_ordering::less));
}

// weak | promote(weak)
Expand Down