1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // class year_month_day;
13 // constexpr bool ok() const noexcept;
14 // Returns: m_.ok() && y_.ok().
17 #include <type_traits>
20 #include "test_macros.h"
24 using year
= std::chrono::year
;
25 using month
= std::chrono::month
;
26 using day
= std::chrono::day
;
27 using year_month_day
= std::chrono::year_month_day
;
29 constexpr month January
= std::chrono::January
;
31 ASSERT_NOEXCEPT( std::declval
<const year_month_day
>().ok());
32 ASSERT_SAME_TYPE(bool, decltype(std::declval
<const year_month_day
>().ok()));
34 static_assert(!year_month_day
{year
{-32768}, month
{}, day
{}}.ok(), ""); // All three bad
36 static_assert(!year_month_day
{year
{-32768}, January
, day
{1}}.ok(), ""); // Bad year
37 static_assert(!year_month_day
{year
{2019}, month
{}, day
{1}}.ok(), ""); // Bad month
38 static_assert(!year_month_day
{year
{2019}, January
, day
{} }.ok(), ""); // Bad day
40 static_assert(!year_month_day
{year
{-32768}, month
{}, day
{1}}.ok(), ""); // Bad year & month
41 static_assert(!year_month_day
{year
{2019}, month
{}, day
{} }.ok(), ""); // Bad month & day
42 static_assert(!year_month_day
{year
{-32768}, January
, day
{} }.ok(), ""); // Bad year & day
44 static_assert( year_month_day
{year
{2019}, January
, day
{1}}.ok(), ""); // All OK
46 // Some months have a 31st
47 static_assert( year_month_day
{year
{2020}, month
{ 1}, day
{31}}.ok(), "");
48 static_assert(!year_month_day
{year
{2020}, month
{ 2}, day
{31}}.ok(), "");
49 static_assert( year_month_day
{year
{2020}, month
{ 3}, day
{31}}.ok(), "");
50 static_assert(!year_month_day
{year
{2020}, month
{ 4}, day
{31}}.ok(), "");
51 static_assert( year_month_day
{year
{2020}, month
{ 5}, day
{31}}.ok(), "");
52 static_assert(!year_month_day
{year
{2020}, month
{ 6}, day
{31}}.ok(), "");
53 static_assert( year_month_day
{year
{2020}, month
{ 7}, day
{31}}.ok(), "");
54 static_assert( year_month_day
{year
{2020}, month
{ 8}, day
{31}}.ok(), "");
55 static_assert(!year_month_day
{year
{2020}, month
{ 9}, day
{31}}.ok(), "");
56 static_assert( year_month_day
{year
{2020}, month
{10}, day
{31}}.ok(), "");
57 static_assert(!year_month_day
{year
{2020}, month
{11}, day
{31}}.ok(), "");
58 static_assert( year_month_day
{year
{2020}, month
{12}, day
{31}}.ok(), "");
60 // Everyone except FEB has a 30th
61 static_assert( year_month_day
{year
{2020}, month
{ 1}, day
{30}}.ok(), "");
62 static_assert(!year_month_day
{year
{2020}, month
{ 2}, day
{30}}.ok(), "");
63 static_assert( year_month_day
{year
{2020}, month
{ 3}, day
{30}}.ok(), "");
64 static_assert( year_month_day
{year
{2020}, month
{ 4}, day
{30}}.ok(), "");
65 static_assert( year_month_day
{year
{2020}, month
{ 5}, day
{30}}.ok(), "");
66 static_assert( year_month_day
{year
{2020}, month
{ 6}, day
{30}}.ok(), "");
67 static_assert( year_month_day
{year
{2020}, month
{ 7}, day
{30}}.ok(), "");
68 static_assert( year_month_day
{year
{2020}, month
{ 8}, day
{30}}.ok(), "");
69 static_assert( year_month_day
{year
{2020}, month
{ 9}, day
{30}}.ok(), "");
70 static_assert( year_month_day
{year
{2020}, month
{10}, day
{30}}.ok(), "");
71 static_assert( year_month_day
{year
{2020}, month
{11}, day
{30}}.ok(), "");
72 static_assert( year_month_day
{year
{2020}, month
{12}, day
{30}}.ok(), "");
74 static_assert(!year_month_day
{year
{2019}, std::chrono::February
, day
{29}}.ok(), ""); // Not a leap year
75 static_assert( year_month_day
{year
{2020}, std::chrono::February
, day
{29}}.ok(), ""); // Ok; 2020 is a leap year
77 for (unsigned i
= 0; i
<= 50; ++i
)
79 year_month_day ym
{year
{2019}, January
, day
{i
}};
80 assert( ym
.ok() == day
{i
}.ok());
83 for (unsigned i
= 0; i
<= 50; ++i
)
85 year_month_day ym
{year
{2019}, month
{i
}, day
{12}};
86 assert( ym
.ok() == month
{i
}.ok());
89 const int ymax
= static_cast<int>(year::max());
90 for (int i
= ymax
- 100; i
<= ymax
+ 100; ++i
)
92 year_month_day ym
{year
{i
}, January
, day
{12}};
93 assert( ym
.ok() == year
{i
}.ok());