From a05724a1f84853fc655950c36453f200fba8af75 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Tue, 16 Jul 2024 19:01:00 +0200 Subject: [PATCH] [libc++][chrono] Adds year_month_day_last&::operator<=>. (#98169) 41f7bb9975bcaffae0267fa87b63c90b83ffd551 claimed it implemented this change but the code was not adjusted. The other spaceship operators in the calendar code have been validated too. Implements parts of - P1614R2 The Mothership has Landed --- libcxx/include/__chrono/year_month_day.h | 32 +---- libcxx/modules/std/chrono.inc | 1 - .../comparisons.pass.cpp | 127 +++++++++--------- 3 files changed, 65 insertions(+), 95 deletions(-) diff --git a/libcxx/include/__chrono/year_month_day.h b/libcxx/include/__chrono/year_month_day.h index 75884f3654d870..b06c0be03e0de4 100644 --- a/libcxx/include/__chrono/year_month_day.h +++ b/libcxx/include/__chrono/year_month_day.h @@ -239,33 +239,11 @@ operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) n return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last(); } -_LIBCPP_HIDE_FROM_ABI inline constexpr bool -operator!=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { - return !(__lhs == __rhs); -} - -_LIBCPP_HIDE_FROM_ABI inline constexpr bool -operator<(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { - if (__lhs.year() < __rhs.year()) - return true; - if (__lhs.year() > __rhs.year()) - return false; - return __lhs.month_day_last() < __rhs.month_day_last(); -} - -_LIBCPP_HIDE_FROM_ABI inline constexpr bool -operator>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { - return __rhs < __lhs; -} - -_LIBCPP_HIDE_FROM_ABI inline constexpr bool -operator<=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { - return !(__rhs < __lhs); -} - -_LIBCPP_HIDE_FROM_ABI inline constexpr bool -operator>=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { - return !(__lhs < __rhs); +_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering +operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { + if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0) + return __c; + return __lhs.month_day_last() <=> __rhs.month_day_last(); } _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept { diff --git a/libcxx/modules/std/chrono.inc b/libcxx/modules/std/chrono.inc index 3ba3c46150c9cf..8d313f755f7253 100644 --- a/libcxx/modules/std/chrono.inc +++ b/libcxx/modules/std/chrono.inc @@ -37,7 +37,6 @@ export namespace std { // [time.duration.comparisons], duration comparisons using std::chrono::operator==; - using std::chrono::operator!=; using std::chrono::operator<; using std::chrono::operator>; using std::chrono::operator<=; diff --git a/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp b/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp index 45d7e51fbe7182..e28b6d8609bc4d 100644 --- a/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp +++ b/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp @@ -5,19 +5,14 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// -// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 // // class year_month_day_last; // constexpr bool operator==(const year_month_day_last& x, const year_month_day_last& y) noexcept; -// Returns: x.year() == y.year() && x.month_day_last() == y.month_day_last(). -// -// constexpr bool operator< (const year_month_day_last& x, const year_month_day_last& y) noexcept; -// Returns: -// If x.year() < y.year(), returns true. -// Otherwise, if x.year() > y.year(), returns false. -// Otherwise, returns x.month_day_last() < y.month_day_last() +// constexpr bool operator<=>(const year_month_day_last& x, const year_month_day_last& y) noexcept; #include #include @@ -26,63 +21,61 @@ #include "test_macros.h" #include "test_comparisons.h" -int main(int, char**) -{ - using year = std::chrono::year; - using month = std::chrono::month; - using month_day_last = std::chrono::month_day_last; - using year_month_day_last = std::chrono::year_month_day_last; - - AssertComparisonsAreNoexcept(); - AssertComparisonsReturnBool(); - - constexpr month January = std::chrono::January; - constexpr month February = std::chrono::February; - - static_assert( testComparisons( - year_month_day_last{year{1234}, month_day_last{January}}, - year_month_day_last{year{1234}, month_day_last{January}}, - true, false), ""); - - // different month - static_assert( testComparisons( - year_month_day_last{year{1234}, month_day_last{January}}, - year_month_day_last{year{1234}, month_day_last{February}}, - false, true), ""); - - // different year - static_assert( testComparisons( - year_month_day_last{year{1234}, month_day_last{January}}, - year_month_day_last{year{1235}, month_day_last{January}}, - false, true), ""); - - // different month - static_assert( testComparisons( - year_month_day_last{year{1234}, month_day_last{January}}, - year_month_day_last{year{1234}, month_day_last{February}}, - false, true), ""); - - // different year and month - static_assert( testComparisons( - year_month_day_last{year{1234}, month_day_last{February}}, - year_month_day_last{year{1235}, month_day_last{January}}, - false, true), ""); - - // same year, different months - for (unsigned i = 1; i < 12; ++i) - for (unsigned j = 1; j < 12; ++j) - assert((testComparisons( - year_month_day_last{year{1234}, month_day_last{month{i}}}, - year_month_day_last{year{1234}, month_day_last{month{j}}}, - i == j, i < j ))); - - // same month, different years - for (int i = 1000; i < 2000; ++i) - for (int j = 1000; j < 2000; ++j) - assert((testComparisons( - year_month_day_last{year{i}, month_day_last{January}}, - year_month_day_last{year{j}, month_day_last{January}}, - i == j, i < j ))); - - return 0; +constexpr bool test() { + using year = std::chrono::year; + using month = std::chrono::month; + using month_day_last = std::chrono::month_day_last; + using year_month_day_last = std::chrono::year_month_day_last; + + constexpr month January = std::chrono::January; + constexpr month February = std::chrono::February; + + assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}}, + year_month_day_last{year{1234}, month_day_last{January}}, + std::strong_ordering::equal)); + + // different month + assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}}, + year_month_day_last{year{1234}, month_day_last{February}}, + std::strong_ordering::less)); + + // different year + assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}}, + year_month_day_last{year{1235}, month_day_last{January}}, + std::strong_ordering::less)); + + // different year and month + assert(testOrder(year_month_day_last{year{1234}, month_day_last{February}}, + year_month_day_last{year{1235}, month_day_last{January}}, + std::strong_ordering::less)); + + // same year, different months + for (unsigned i = 1; i < 12; ++i) + for (unsigned j = 1; j < 12; ++j) + assert((testOrder(year_month_day_last{year{1234}, month_day_last{month{i}}}, + year_month_day_last{year{1234}, month_day_last{month{j}}}, + i == j ? std::strong_ordering::equal + : i < j ? std::strong_ordering::less + : std::strong_ordering::greater))); + + // same month, different years + for (int i = 1000; i < 20; ++i) + for (int j = 1000; j < 20; ++j) + assert((testOrder(year_month_day_last{year{i}, month_day_last{January}}, + year_month_day_last{year{j}, month_day_last{January}}, + i == j ? std::strong_ordering::equal + : i < j ? std::strong_ordering::less + : std::strong_ordering::greater))); + return true; +} + +int main(int, char**) { + using year_month_day_last = std::chrono::year_month_day_last; + AssertOrderAreNoexcept(); + AssertOrderReturn(); + + test(); + static_assert(test()); + + return 0; }