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

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