xref: /llvm-project/libcxx/test/std/time/time.cal/time.cal.operators/year_month_day_last.pass.cpp (revision 355e0ce3c5366bd0b564a25e4b6675353da3c53e)
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 // <chrono>
10 // class year_month_day_last;
11 
12 // constexpr year_month_day_last
13 //   operator/(const year_month& ym, last_spec) noexcept;
14 // Returns: {ym.year(), month_day_last{ym.month()}}.
15 
16 
17 // constexpr year_month_day_last
18 //   operator/(const year& y, const month_day_last& mdl) noexcept;
19 // Returns: {y, mdl}.
20 //
21 // constexpr year_month_day_last
22 //   operator/(int y, const month_day_last& mdl) noexcept;
23 // Returns: year(y) / mdl.
24 //
25 // constexpr year_month_day_last
26 //   operator/(const month_day_last& mdl, const year& y) noexcept;
27 // Returns: y / mdl.
28 //
29 // constexpr year_month_day_last
30 //   operator/(const month_day_last& mdl, int y) noexcept;
31 // Returns: year(y) / mdl.
32 
33 #include <chrono>
34 #include <type_traits>
35 #include <cassert>
36 
37 #include "test_macros.h"
38 
main(int,char **)39 int main(int, char**)
40 {
41     using day                 = std::chrono::day;
42     using month               = std::chrono::month;
43     using year_month          = std::chrono::year_month;
44     using year                = std::chrono::year;
45     using month_day_last      = std::chrono::month_day_last;
46     using year_month_day_last = std::chrono::year_month_day_last;
47 
48     constexpr month February = std::chrono::February;
49     constexpr std::chrono::last_spec last = std::chrono::last;
50 
51     { // operator/(const year_month& ym, last_spec)
52         constexpr year_month Feb2018{year{2018}, February};
53 
54         ASSERT_NOEXCEPT (                              Feb2018/last);
55         ASSERT_SAME_TYPE(year_month_day_last, decltype(Feb2018/last));
56 
57         static_assert((Feb2018/last).year()  == year{2018}, "");
58         static_assert((Feb2018/last).month() == February,   "");
59 
60         for (int i = 1000; i < 1010; ++i)
61             for (unsigned j = 1; j <= 12; ++j)
62             {
63                 year y{i};
64                 month m{j};
65                 year_month_day_last ymdl = year_month{y,m}/last;
66                 assert(ymdl.year()  == y);
67                 assert(ymdl.month() == m);
68             }
69     }
70 
71 
72     { // operator/(const year& y, const month_day_last& mdl) (and switched)
73         ASSERT_NOEXCEPT (                              year{2018}/month_day_last{February});
74         ASSERT_SAME_TYPE(year_month_day_last, decltype(year{2018}/month_day_last{February}));
75         ASSERT_NOEXCEPT (                              month_day_last{February}/year{2018});
76         ASSERT_SAME_TYPE(year_month_day_last, decltype(month_day_last{February}/year{2018}));
77 
78         static_assert((year{2018}/month_day_last{February}).month() == February,   "");
79         static_assert((year{2018}/month_day_last{February}).year()  == year{2018}, "");
80         static_assert((month_day_last{February}/year{2018}).month() == February,   "");
81         static_assert((month_day_last{February}/year{2018}).year()  == year{2018}, "");
82 
83         for (int i = 1000; i < 1010; ++i)
84             for (unsigned j = 1; j <= 12; ++j)
85             {
86                 year y{i};
87                 month m{j};
88                 year_month_day_last ymdl1 = y/month_day_last{m};
89                 year_month_day_last ymdl2 = month_day_last{m}/y;
90                 assert(ymdl1.month() == m);
91                 assert(ymdl2.month() == m);
92                 assert(ymdl2.year()  == y);
93                 assert(ymdl1.year()  == y);
94                 assert(ymdl1 == ymdl2);
95             }
96     }
97 
98     { // operator/(int y, const month_day_last& mdl) (and switched)
99         ASSERT_NOEXCEPT (                              2018/month_day_last{February});
100         ASSERT_SAME_TYPE(year_month_day_last, decltype(2018/month_day_last{February}));
101         ASSERT_NOEXCEPT (                              month_day_last{February}/2018);
102         ASSERT_SAME_TYPE(year_month_day_last, decltype(month_day_last{February}/2018));
103 
104         static_assert((2018/month_day_last{February}).month() == February,   "");
105         static_assert((2018/month_day_last{February}).year()  == year{2018}, "");
106         static_assert((month_day_last{February}/2018).month() == February,   "");
107         static_assert((month_day_last{February}/2018).year()  == year{2018}, "");
108 
109         for (int i = 1000; i < 1010; ++i)
110             for (unsigned j = 1; j <= 12; ++j)
111             {
112                 year y{i};
113                 month m{j};
114                 year_month_day_last ymdl1 = i/month_day_last{m};
115                 year_month_day_last ymdl2 = month_day_last{m}/i;
116                 assert(ymdl1.month() == m);
117                 assert(ymdl2.month() == m);
118                 assert(ymdl2.year()  == y);
119                 assert(ymdl1.year()  == y);
120                 assert(ymdl1 == ymdl2);
121             }
122     }
123 
124     // the result of year_month_day_last::day() is unspecified when !ok(),
125     // but it shouldn't crash.
126     {
127         year_month_day_last ymdl = year{2020}/month{13}/last;
128         assert(!ymdl.ok());
129         day d = ymdl.day(); (void)d; // doesn't crash
130     }
131 
132     return 0;
133 }
134