xref: /llvm-project/libcxx/test/std/time/time.point/time.point.comparisons/compare.three_way.pass.cpp (revision d94a770f7b05161e0b8bc87975cfbc7f79d4b497)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // <chrono>
12 
13 // time_point
14 
15 // template<class Clock, class Duration1,
16 //          three_way_comparable_with<Duration1> Duration2>
17 //   constexpr auto operator<=>(const time_point<Clock, Duration1>& lhs,
18 //                              const time_point<Clock, Duration2>& rhs);
19 
20 #include <cassert>
21 #include <chrono>
22 #include <ratio>
23 
24 #include "test_comparisons.h"
25 
test_with_integral_ticks_value()26 constexpr void test_with_integral_ticks_value() {
27   using Clock = std::chrono::system_clock;
28 
29   using Duration1 = std::chrono::milliseconds;
30   using Duration2 = std::chrono::microseconds;
31   using T1        = std::chrono::time_point<Clock, Duration1>;
32   using T2        = std::chrono::time_point<Clock, Duration2>;
33 
34   {
35     T1 t1(Duration1(3));
36     T1 t2(Duration1(3));
37     assert((t1 <=> t2) == std::strong_ordering::equal);
38     assert(testOrder(t1, t2, std::strong_ordering::equal));
39   }
40   {
41     T1 t1(Duration1(3));
42     T1 t2(Duration1(4));
43     assert((t1 <=> t2) == std::strong_ordering::less);
44     assert(testOrder(t1, t2, std::strong_ordering::less));
45   }
46   {
47     T1 t1(Duration1(3));
48     T2 t2(Duration2(3000));
49     assert((t1 <=> t2) == std::strong_ordering::equal);
50     assert(testOrder(t1, t2, std::strong_ordering::equal));
51   }
52   {
53     T1 t1(Duration1(3));
54     T2 t2(Duration2(3001));
55     assert((t1 <=> t2) == std::strong_ordering::less);
56     assert(testOrder(t1, t2, std::strong_ordering::less));
57     assert((t2 <=> t1) == std::strong_ordering::greater);
58     assert(testOrder(t2, t1, std::strong_ordering::greater));
59   }
60 }
61 
test_with_integral_ticks_value_and_custom_period_value()62 constexpr void test_with_integral_ticks_value_and_custom_period_value() {
63   using Clock = std::chrono::system_clock;
64 
65   using DInt30Hz = std::chrono::duration<int, std::ratio<1, 30>>;
66   using DInt60Hz = std::chrono::duration<int, std::ratio<1, 60>>;
67 
68   using TIntR1 = std::chrono::time_point<Clock, DInt30Hz>;
69   using TIntR2 = std::chrono::time_point<Clock, DInt60Hz>;
70 
71   {
72     TIntR1 t1(DInt30Hz(10));
73     TIntR2 t2(DInt60Hz(20));
74     assert((t1 <=> t2) == std::strong_ordering::equal);
75     assert(testOrder(t1, t2, std::strong_ordering::equal));
76   }
77   {
78     TIntR1 t1(DInt30Hz(10));
79     TIntR2 t2(DInt60Hz(21));
80     assert((t1 <=> t2) == std::strong_ordering::less);
81     assert(testOrder(t1, t2, std::strong_ordering::less));
82   }
83   {
84     TIntR1 t1(DInt30Hz(11));
85     TIntR2 t2(DInt60Hz(20));
86     assert((t1 <=> t2) == std::strong_ordering::greater);
87     assert(testOrder(t1, t2, std::strong_ordering::greater));
88   }
89 }
90 
test_with_floating_point_ticks_value()91 constexpr void test_with_floating_point_ticks_value() {
92   using Clock = std::chrono::system_clock;
93 
94   using DF30Hz = std::chrono::duration<double, std::ratio<1, 30>>;
95   using DF60Hz = std::chrono::duration<double, std::ratio<1, 60>>;
96   using F1     = std::chrono::time_point<Clock, DF30Hz>;
97   using F2     = std::chrono::time_point<Clock, DF60Hz>;
98 
99   // No equality comparison test for floating point values.
100 
101   {
102     F1 t1(DF30Hz(3.5));
103     F2 t2(DF60Hz(7.1));
104     assert((t1 <=> t2) == std::weak_ordering::less);
105     assert(testOrder(t1, t2, std::weak_ordering::less));
106   }
107   {
108     F1 t1(DF30Hz(3.6));
109     F2 t2(DF60Hz(7.0));
110     assert((t1 <=> t2) == std::weak_ordering::greater);
111     assert(testOrder(t1, t2, std::weak_ordering::greater));
112   }
113 }
114 
test()115 constexpr bool test() {
116   test_with_integral_ticks_value();
117   test_with_integral_ticks_value_and_custom_period_value();
118   test_with_floating_point_ticks_value();
119 
120   return true;
121 }
122 
main(int,char **)123 int main(int, char**) {
124   assert(test());
125   static_assert(test());
126   return 0;
127 }
128