Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp

raw
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_weekday;
12
13 // constexpr bool ok() const noexcept;
14 // Returns: If any of y_.ok(), m_.ok(), or wdi_.ok() is false, returns false.
15 // Otherwise, if *this represents a valid date, returns true.
16 // Otherwise, returns false.
17
18 #include <chrono>
19 #include <type_traits>
20 #include <cassert>
21
22 #include "test_macros.h"
23
24 int main(int, char**)
25 {
26 using year = std::chrono::year;
27 using month = std::chrono::month;
28 using weekday = std::chrono::weekday;
29 using weekday_indexed = std::chrono::weekday_indexed;
30 using year_month_weekday = std::chrono::year_month_weekday;
31
32 constexpr month January = std::chrono::January;
33 constexpr weekday Monday = std::chrono::Monday;
34 constexpr weekday Tuesday = std::chrono::Tuesday;
35 constexpr weekday Wednesday = std::chrono::Wednesday;
36 constexpr weekday Thursday = std::chrono::Thursday;
37 constexpr weekday Friday = std::chrono::Friday;
38 constexpr weekday Saturday = std::chrono::Saturday;
39 constexpr weekday Sunday = std::chrono::Sunday;
40
41 ASSERT_NOEXCEPT( std::declval<const year_month_weekday>().ok());
42 ASSERT_SAME_TYPE(bool, decltype(std::declval<const year_month_weekday>().ok()));
43
44 static_assert(!year_month_weekday{}.ok(), "");
45
46 static_assert(!year_month_weekday{year{-32768}, month{}, weekday_indexed{}}.ok(), ""); // All three bad
47
48 static_assert(!year_month_weekday{year{-32768}, January, weekday_indexed{Tuesday, 1}}.ok(), ""); // Bad year
49 static_assert(!year_month_weekday{year{2019}, month{}, weekday_indexed{Tuesday, 1}}.ok(), ""); // Bad month
50 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{} }.ok(), ""); // Bad day
51
52 static_assert(!year_month_weekday{year{-32768}, month{}, weekday_indexed{Tuesday, 1}}.ok(), ""); // Bad year & month
53 static_assert(!year_month_weekday{year{2019}, month{}, weekday_indexed{} }.ok(), ""); // Bad month & day
54 static_assert(!year_month_weekday{year{-32768}, January, weekday_indexed{} }.ok(), ""); // Bad year & day
55
56 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, static_cast<unsigned>(-1)}}.ok(), ""); // Bad index.
57 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Wednesday, 0}}.ok(), ""); // Bad index.
58
59 static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, 1}}.ok(), ""); // All OK
60 static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, 4}}.ok(), ""); // All OK
61
62 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Monday, 5}}.ok(), ""); // Bad index
63 static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, 5}}.ok(), ""); // All OK
64 static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Wednesday, 5}}.ok(), ""); // All OK
65 static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Thursday, 5}}.ok(), ""); // All OK
66 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Friday, 5}}.ok(), ""); // Bad index
67 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Saturday, 5}}.ok(), ""); // Bad index
68 static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Sunday, 5}}.ok(), ""); // Bad index
69
70 for (unsigned i = 0; i <= 50; ++i)
71 {
72 year_month_weekday ym{year{2019}, January, weekday_indexed{Tuesday, i}};
73 assert((ym.ok() == weekday_indexed{Tuesday, i}.ok()));
74 }
75
76 for (unsigned i = 0; i <= 50; ++i)
77 {
78 year_month_weekday ym{year{2019}, January, weekday_indexed{weekday{i}, 1}};
79 assert((ym.ok() == weekday_indexed{weekday{i}, 1}.ok()));
80 }
81
82 for (unsigned i = 0; i <= 50; ++i)
83 {
84 year_month_weekday ym{year{2019}, month{i}, weekday_indexed{Tuesday, 1}};
85 assert((ym.ok() == month{i}.ok()));
86 }
87
88 const int ymax = static_cast<int>(year::max());
89 for (int i = ymax - 100; i <= ymax + 100; ++i)
90 {
91 year_month_weekday ym{year{i}, January, weekday_indexed{Tuesday, 1}};
92 assert((ym.ok() == year{i}.ok()));
93 }
94
95 return 0;
96 }