diff options
author | Charlie Beattie <cbeattie@google.com> | 2024-07-01 11:31:14 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-07-01 11:31:52 -0700 |
commit | a27662352e9caafc264747562162a8a32ef36cb9 (patch) | |
tree | a894acd86b3b736767c67f78db58d519e196e8f7 /absl/time/duration_test.cc | |
parent | 649f58927db8f223098be81ab5d8a70469848806 (diff) |
Add operator<=> comparison to absl::Time and absl::Duration.
PiperOrigin-RevId: 648433954
Change-Id: I32e47a89685419ae8d37dfadb354cfaab2a35ae9
Diffstat (limited to 'absl/time/duration_test.cc')
-rw-r--r-- | absl/time/duration_test.cc | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/absl/time/duration_test.cc b/absl/time/duration_test.cc index dcf7aad6..4c801a8d 100644 --- a/absl/time/duration_test.cc +++ b/absl/time/duration_test.cc @@ -19,6 +19,11 @@ #include <array> #include <cfloat> #include <chrono> // NOLINT(build/c++11) + +#ifdef __cpp_impl_three_way_comparison +#include <compare> +#endif // __cpp_impl_three_way_comparison + #include <cmath> #include <cstdint> #include <ctime> @@ -431,6 +436,15 @@ TEST(Duration, InfinityComparison) { EXPECT_LT(-inf, any_dur); EXPECT_LT(-inf, inf); EXPECT_GT(inf, -inf); + +#ifdef __cpp_impl_three_way_comparison + EXPECT_EQ(inf <=> inf, std::strong_ordering::equal); + EXPECT_EQ(-inf <=> -inf, std::strong_ordering::equal); + EXPECT_EQ(-inf <=> inf, std::strong_ordering::less); + EXPECT_EQ(inf <=> -inf, std::strong_ordering::greater); + EXPECT_EQ(any_dur <=> inf, std::strong_ordering::less); + EXPECT_EQ(any_dur <=> -inf, std::strong_ordering::greater); +#endif // __cpp_impl_three_way_comparison } TEST(Duration, InfinityAddition) { @@ -496,9 +510,20 @@ TEST(Duration, InfinitySubtraction) { // Interesting case absl::Duration almost_neg_inf = sec_min; EXPECT_LT(-inf, almost_neg_inf); + +#ifdef __cpp_impl_three_way_comparison + EXPECT_EQ(-inf <=> almost_neg_inf, std::strong_ordering::less); + EXPECT_EQ(almost_neg_inf <=> -inf, std::strong_ordering::greater); +#endif // __cpp_impl_three_way_comparison + almost_neg_inf -= -absl::Nanoseconds(1); EXPECT_LT(-inf, almost_neg_inf); +#ifdef __cpp_impl_three_way_comparison + EXPECT_EQ(-inf <=> almost_neg_inf, std::strong_ordering::less); + EXPECT_EQ(almost_neg_inf <=> -inf, std::strong_ordering::greater); +#endif // __cpp_impl_three_way_comparison + // For reference: IEEE 754 behavior const double dbl_inf = std::numeric_limits<double>::infinity(); EXPECT_TRUE(std::isnan(dbl_inf - dbl_inf)); // We return inf @@ -857,6 +882,21 @@ TEST(Duration, Range) { EXPECT_LT(neg_full_range, full_range); EXPECT_EQ(neg_full_range, -full_range); + +#ifdef __cpp_impl_three_way_comparison + EXPECT_EQ(range_future <=> absl::InfiniteDuration(), + std::strong_ordering::less); + EXPECT_EQ(range_past <=> -absl::InfiniteDuration(), + std::strong_ordering::greater); + EXPECT_EQ(full_range <=> absl::ZeroDuration(), // + std::strong_ordering::greater); + EXPECT_EQ(full_range <=> -absl::InfiniteDuration(), + std::strong_ordering::greater); + EXPECT_EQ(neg_full_range <=> -absl::InfiniteDuration(), + std::strong_ordering::greater); + EXPECT_EQ(neg_full_range <=> full_range, std::strong_ordering::less); + EXPECT_EQ(neg_full_range <=> -full_range, std::strong_ordering::equal); +#endif // __cpp_impl_three_way_comparison } TEST(Duration, RelationalOperators) { @@ -880,6 +920,27 @@ TEST(Duration, RelationalOperators) { #undef TEST_REL_OPS } + +#ifdef __cpp_impl_three_way_comparison + +TEST(Duration, SpaceshipOperators) { +#define TEST_REL_OPS(UNIT) \ + static_assert(UNIT(2) <=> UNIT(2) == std::strong_ordering::equal, ""); \ + static_assert(UNIT(1) <=> UNIT(2) == std::strong_ordering::less, ""); \ + static_assert(UNIT(3) <=> UNIT(2) == std::strong_ordering::greater, ""); + + TEST_REL_OPS(absl::Nanoseconds); + TEST_REL_OPS(absl::Microseconds); + TEST_REL_OPS(absl::Milliseconds); + TEST_REL_OPS(absl::Seconds); + TEST_REL_OPS(absl::Minutes); + TEST_REL_OPS(absl::Hours); + +#undef TEST_REL_OPS +} + +#endif // __cpp_impl_three_way_comparison + TEST(Duration, Addition) { #define TEST_ADD_OPS(UNIT) \ do { \ |