[libc++][chrono] Adds operator<=> for day.

Since the calendar classes were introduced in C++20 there's no need to
keep the old comparison operators.

This commit does the day calender class, the other calendar classes will
be in a followup commit.

Implements parts of:
- P1614R2 The mothership has landed

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D128603
This commit is contained in:
Mark de Wever 2022-06-26 10:58:02 +02:00
parent 6b50f2bbdb
commit fd36a3d48d
3 changed files with 32 additions and 41 deletions

View File

@ -12,6 +12,7 @@
#include <__chrono/duration.h>
#include <__config>
#include <compare>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@ -45,25 +46,9 @@ _LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator==(const day& __lhs, const day& __rhs) noexcept
{ return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs); }
_LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator!=(const day& __lhs, const day& __rhs) noexcept
{ return !(__lhs == __rhs); }
_LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator< (const day& __lhs, const day& __rhs) noexcept
{ return static_cast<unsigned>(__lhs) < static_cast<unsigned>(__rhs); }
_LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator> (const day& __lhs, const day& __rhs) noexcept
{ return __rhs < __lhs; }
_LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator<=(const day& __lhs, const day& __rhs) noexcept
{ return !(__rhs < __lhs);}
_LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator>=(const day& __lhs, const day& __rhs) noexcept
{ return !(__lhs < __rhs); }
_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const day& __lhs, const day& __rhs) noexcept {
return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
}
_LIBCPP_HIDE_FROM_ABI inline constexpr
day operator+ (const day& __lhs, const days& __rhs) noexcept

View File

@ -13,6 +13,8 @@
/*
chrono synopsis
#include <compare> // C++20
namespace std
{
namespace chrono
@ -325,11 +327,7 @@ struct last_spec;
class day;
constexpr bool operator==(const day& x, const day& y) noexcept;
constexpr bool operator!=(const day& x, const day& y) noexcept;
constexpr bool operator< (const day& x, const day& y) noexcept;
constexpr bool operator> (const day& x, const day& y) noexcept;
constexpr bool operator<=(const day& x, const day& y) noexcept;
constexpr bool operator>=(const day& x, const day& y) noexcept;
constexpr strong_ordering operator<=>(const day& x, const day& y) noexcept;
constexpr day operator+(const day& x, const days& y) noexcept;
constexpr day operator+(const days& x, const day& y) noexcept;
constexpr day operator-(const day& x, const days& y) noexcept;
@ -715,9 +713,11 @@ constexpr chrono::year operator ""y(unsigned lo
#include <__chrono/year_month_day.h>
#include <__chrono/year_month_weekday.h>
#include <__config>
#include <compare>
#include <version>
// standard-mandated includes
#include <compare>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

View File

@ -12,9 +12,8 @@
// constexpr bool operator==(const day& x, const day& y) noexcept;
// Returns: unsigned{x} == unsigned{y}.
// constexpr bool operator<(const day& x, const day& y) noexcept;
// Returns: unsigned{x} < unsigned{y}.
// constexpr strong_ordering operator<=>(const day& x, const day& y) noexcept;
// Returns: unsigned{x} <=> unsigned{y}.
#include <chrono>
#include <type_traits>
@ -23,23 +22,30 @@
#include "test_macros.h"
#include "test_comparisons.h"
int main(int, char**)
{
using day = std::chrono::day;
constexpr bool test() {
using day = std::chrono::day;
AssertComparisonsAreNoexcept<day>();
AssertComparisonsReturnBool<day>();
// Validate invalid values. The range [0, 255] is guaranteed to be allowed.
assert(testOrderValues<day>(0U, 0U));
assert(testOrderValues<day>(0U, 1U));
assert(testOrderValues<day>(254U, 255U));
assert(testOrderValues<day>(255U, 255U));
static_assert(testComparisonsValues<day>(0U, 0U), "");
static_assert(testComparisonsValues<day>(0U, 1U), "");
// Validate some valid values.
for (unsigned i = 1; i < 10; ++i)
for (unsigned j = 1; j < 10; ++j)
assert(testOrderValues<day>(i, j));
// Some 'ok' values as well
static_assert(testComparisonsValues<day>( 5U, 5U), "");
static_assert(testComparisonsValues<day>( 5U, 10U), "");
return true;
}
for (unsigned i = 1; i < 10; ++i)
for (unsigned j = 1; j < 10; ++j)
assert(testComparisonsValues<day>(i, j));
int main(int, char**) {
using day = std::chrono::day;
AssertOrderAreNoexcept<day>();
AssertOrderReturn<std::strong_ordering, day>();
test();
static_assert(test());
return 0;
}