xref: /llvm-project/libcxx/test/std/time/time.cal/time.cal.ymd/time.cal.ymd.nonmembers/comparisons.pass.cpp (revision 41f7bb9975bcaffae0267fa87b63c90b83ffd551)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 
10 // <chrono>
11 // class year_month_day;
12 
13 // constexpr bool operator==(const year_month_day& x, const year_month_day& y) noexcept;
14 // constexpr strong_ordering operator<=>(const year_month_day& x, const year_month_day& y) noexcept;
15 
16 #include <chrono>
17 #include <type_traits>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "test_comparisons.h"
22 
test()23 constexpr bool test() {
24   using day            = std::chrono::day;
25   using year           = std::chrono::year;
26   using month          = std::chrono::month;
27   using year_month_day = std::chrono::year_month_day;
28 
29   constexpr month January  = std::chrono::January;
30   constexpr month February = std::chrono::February;
31 
32   assert(testOrder(
33       year_month_day{year{1234}, January, day{1}},
34       year_month_day{year{1234}, January, day{1}},
35       std::strong_ordering::equal));
36 
37   // different day
38   assert(testOrder(
39       year_month_day{year{1234}, January, day{1}},
40       year_month_day{year{1234}, January, day{2}},
41       std::strong_ordering::less));
42 
43   // different month
44   assert(testOrder(
45       year_month_day{year{1234}, January, day{1}},
46       year_month_day{year{1234}, February, day{1}},
47       std::strong_ordering::less));
48 
49   // different year
50   assert(testOrder(
51       year_month_day{year{1234}, January, day{1}},
52       year_month_day{year{1235}, January, day{1}},
53       std::strong_ordering::less));
54 
55   // different month and day
56   assert(testOrder(
57       year_month_day{year{1234}, January, day{2}},
58       year_month_day{year{1234}, February, day{1}},
59       std::strong_ordering::less));
60 
61   // different year and month
62   assert(testOrder(
63       year_month_day{year{1234}, February, day{1}},
64       year_month_day{year{1235}, January, day{1}},
65       std::strong_ordering::less));
66 
67   // different year and day
68   assert(testOrder(
69       year_month_day{year{1234}, January, day{2}},
70       year_month_day{year{1235}, January, day{1}},
71       std::strong_ordering::less));
72 
73   // different year, month and day
74   assert(testOrder(
75       year_month_day{year{1234}, February, day{2}},
76       year_month_day{year{1235}, January, day{1}},
77       std::strong_ordering::less));
78 
79   // same year, different days
80   for (unsigned i = 1; i < 28; ++i)
81     for (unsigned j = 1; j < 28; ++j)
82       assert((testOrder(
83           year_month_day{year{1234}, January, day{i}},
84           year_month_day{year{1234}, January, day{j}},
85           i == j  ? std::strong_ordering::equal
86           : i < j ? std::strong_ordering::less
87                   : std::strong_ordering::greater)));
88 
89   // same year, different months
90   for (unsigned i = 1; i < 12; ++i)
91     for (unsigned j = 1; j < 12; ++j)
92       assert((testOrder(
93           year_month_day{year{1234}, month{i}, day{12}},
94           year_month_day{year{1234}, month{j}, day{12}},
95           i == j  ? std::strong_ordering::equal
96           : i < j ? std::strong_ordering::less
97                   : std::strong_ordering::greater)));
98 
99   // same month, different years
100   for (int i = -5; i < 5; ++i)
101     for (int j = -5; j < 5; ++j)
102       assert((testOrder(
103           year_month_day{year{i}, January, day{12}},
104           year_month_day{year{j}, January, day{12}},
105           i == j  ? std::strong_ordering::equal
106           : i < j ? std::strong_ordering::less
107                   : std::strong_ordering::greater)));
108 
109   return true;
110 }
111 
main(int,char **)112 int main(int, char**) {
113   using year_month_day = std::chrono::year_month_day;
114   AssertOrderAreNoexcept<year_month_day>();
115   AssertOrderReturn<std::strong_ordering, year_month_day>();
116 
117   test();
118   static_assert(test());
119 
120   return 0;
121 }
122