1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10
11 // <chrono>
12 // class year_month_day;
13
14 // constexpr bool ok() const noexcept;
15 // Returns: m_.ok() && y_.ok().
16
17 #include <chrono>
18 #include <type_traits>
19 #include <cassert>
20
21 #include "test_macros.h"
22
main()23 int main()
24 {
25 using year = std::chrono::year;
26 using month = std::chrono::month;
27 using day = std::chrono::day;
28 using year_month_day = std::chrono::year_month_day;
29
30 constexpr month January = std::chrono::January;
31
32 ASSERT_NOEXCEPT( std::declval<const year_month_day>().ok());
33 ASSERT_SAME_TYPE(bool, decltype(std::declval<const year_month_day>().ok()));
34
35 static_assert(!year_month_day{year{-32768}, month{}, day{}}.ok(), ""); // All three bad
36
37 static_assert(!year_month_day{year{-32768}, January, day{1}}.ok(), ""); // Bad year
38 static_assert(!year_month_day{year{2019}, month{}, day{1}}.ok(), ""); // Bad month
39 static_assert(!year_month_day{year{2019}, January, day{} }.ok(), ""); // Bad day
40
41 static_assert(!year_month_day{year{-32768}, month{}, day{1}}.ok(), ""); // Bad year & month
42 static_assert(!year_month_day{year{2019}, month{}, day{} }.ok(), ""); // Bad month & day
43 static_assert(!year_month_day{year{-32768}, January, day{} }.ok(), ""); // Bad year & day
44
45 static_assert( year_month_day{year{2019}, January, day{1}}.ok(), ""); // All OK
46
47 // Some months have a 31st
48 static_assert( year_month_day{year{2020}, month{ 1}, day{31}}.ok(), "");
49 static_assert(!year_month_day{year{2020}, month{ 2}, day{31}}.ok(), "");
50 static_assert( year_month_day{year{2020}, month{ 3}, day{31}}.ok(), "");
51 static_assert(!year_month_day{year{2020}, month{ 4}, day{31}}.ok(), "");
52 static_assert( year_month_day{year{2020}, month{ 5}, day{31}}.ok(), "");
53 static_assert(!year_month_day{year{2020}, month{ 6}, day{31}}.ok(), "");
54 static_assert( year_month_day{year{2020}, month{ 7}, day{31}}.ok(), "");
55 static_assert( year_month_day{year{2020}, month{ 8}, day{31}}.ok(), "");
56 static_assert(!year_month_day{year{2020}, month{ 9}, day{31}}.ok(), "");
57 static_assert( year_month_day{year{2020}, month{10}, day{31}}.ok(), "");
58 static_assert(!year_month_day{year{2020}, month{11}, day{31}}.ok(), "");
59 static_assert( year_month_day{year{2020}, month{12}, day{31}}.ok(), "");
60
61 // Everyone except FEB has a 30th
62 static_assert( year_month_day{year{2020}, month{ 1}, day{30}}.ok(), "");
63 static_assert(!year_month_day{year{2020}, month{ 2}, day{30}}.ok(), "");
64 static_assert( year_month_day{year{2020}, month{ 3}, day{30}}.ok(), "");
65 static_assert( year_month_day{year{2020}, month{ 4}, day{30}}.ok(), "");
66 static_assert( year_month_day{year{2020}, month{ 5}, day{30}}.ok(), "");
67 static_assert( year_month_day{year{2020}, month{ 6}, day{30}}.ok(), "");
68 static_assert( year_month_day{year{2020}, month{ 7}, day{30}}.ok(), "");
69 static_assert( year_month_day{year{2020}, month{ 8}, day{30}}.ok(), "");
70 static_assert( year_month_day{year{2020}, month{ 9}, day{30}}.ok(), "");
71 static_assert( year_month_day{year{2020}, month{10}, day{30}}.ok(), "");
72 static_assert( year_month_day{year{2020}, month{11}, day{30}}.ok(), "");
73 static_assert( year_month_day{year{2020}, month{12}, day{30}}.ok(), "");
74
75 static_assert(!year_month_day{year{2019}, std::chrono::February, day{29}}.ok(), ""); // Not a leap year
76 static_assert( year_month_day{year{2020}, std::chrono::February, day{29}}.ok(), ""); // Ok; 2020 is a leap year
77
78 for (unsigned i = 0; i <= 50; ++i)
79 {
80 year_month_day ym{year{2019}, January, day{i}};
81 assert( ym.ok() == day{i}.ok());
82 }
83
84 for (unsigned i = 0; i <= 50; ++i)
85 {
86 year_month_day ym{year{2019}, month{i}, day{12}};
87 assert( ym.ok() == month{i}.ok());
88 }
89
90 const int ymax = static_cast<int>(year::max());
91 for (int i = ymax - 100; i <= ymax + 100; ++i)
92 {
93 year_month_day ym{year{i}, January, day{12}};
94 assert( ym.ok() == year{i}.ok());
95 }
96 }
97