• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_last;
12 
13 // constexpr year_month_weekday_last operator+(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
14 //   Returns: (ymwdl.year() / ymwdl.month() + dm) / ymwdl.weekday_last().
15 //
16 // constexpr year_month_weekday_last operator+(const months& dm, const year_month_weekday_last& ymwdl) noexcept;
17 //   Returns: ymwdl + dm.
18 //
19 // constexpr year_month_weekday_last operator+(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
20 //   Returns: {ymwdl.year()+dy, ymwdl.month(), ymwdl.weekday_last()}.
21 //
22 // constexpr year_month_weekday_last operator+(const years& dy, const year_month_weekday_last& ymwdl) noexcept;
23 //   Returns: ymwdl + dy.
24 
25 #include <chrono>
26 #include <type_traits>
27 #include <cassert>
28 
29 #include "test_macros.h"
30 
testConstexprYears(std::chrono::year_month_weekday_last ym)31 constexpr bool testConstexprYears(std::chrono::year_month_weekday_last ym)
32 {
33     std::chrono::years offset{23};
34     if (static_cast<int>((ym         ).year()) !=  1) return false;
35     if (static_cast<int>((ym + offset).year()) != 24) return false;
36     if (static_cast<int>((offset + ym).year()) != 24) return false;
37     return true;
38 }
39 
testConstexprMonths(std::chrono::year_month_weekday_last ym)40 constexpr bool testConstexprMonths(std::chrono::year_month_weekday_last ym)
41 {
42     std::chrono::months offset{6};
43     if (static_cast<unsigned>((ym         ).month()) !=  1) return false;
44     if (static_cast<unsigned>((ym + offset).month()) !=  7) return false;
45     if (static_cast<unsigned>((offset + ym).month()) !=  7) return false;
46     return true;
47 }
48 
49 
main(int,char **)50 int main(int, char**)
51 {
52     using year                    = std::chrono::year;
53     using month                   = std::chrono::month;
54     using weekday                 = std::chrono::weekday;
55     using weekday_last            = std::chrono::weekday_last;
56     using year_month_weekday_last = std::chrono::year_month_weekday_last;
57     using years                   = std::chrono::years;
58     using months                  = std::chrono::months;
59 
60     constexpr weekday Tuesday = std::chrono::Tuesday;
61     constexpr month January = std::chrono::January;
62 
63     {   // year_month_weekday_last + months
64     ASSERT_NOEXCEPT(std::declval<year_month_weekday_last>() + std::declval<months>());
65     ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month_weekday_last>());
66 
67     ASSERT_SAME_TYPE(year_month_weekday_last, decltype(std::declval<year_month_weekday_last>() + std::declval<months>()));
68     ASSERT_SAME_TYPE(year_month_weekday_last, decltype(std::declval<months>() + std::declval<year_month_weekday_last>()));
69 
70     static_assert(testConstexprMonths(year_month_weekday_last{year{1}, January, weekday_last{Tuesday}}), "");
71 
72     year_month_weekday_last ym{year{1234}, January, weekday_last{Tuesday}};
73     for (int i = 0; i <= 10; ++i)  // TODO test wrap-around
74     {
75         year_month_weekday_last ym1 = ym + months{i};
76         year_month_weekday_last ym2 = months{i} + ym;
77         assert(ym1.year()         == year(1234));
78         assert(ym2.year()         == year(1234));
79         assert(ym1.month()        == month(1 + i));
80         assert(ym2.month()        == month(1 + i));
81         assert(ym1.weekday()      == Tuesday);
82         assert(ym2.weekday()      == Tuesday);
83         assert(ym1.weekday_last() == weekday_last{Tuesday});
84         assert(ym2.weekday_last() == weekday_last{Tuesday});
85         assert(ym1 == ym2);
86     }
87     }
88 
89     {   // year_month_weekday_last + years
90     ASSERT_NOEXCEPT(std::declval<year_month_weekday_last>() + std::declval<years>());
91     ASSERT_NOEXCEPT(std::declval<years>() + std::declval<year_month_weekday_last>());
92 
93     ASSERT_SAME_TYPE(year_month_weekday_last, decltype(std::declval<year_month_weekday_last>() + std::declval<years>()));
94     ASSERT_SAME_TYPE(year_month_weekday_last, decltype(std::declval<years>() + std::declval<year_month_weekday_last>()));
95 
96     static_assert(testConstexprYears (year_month_weekday_last{year{1}, January, weekday_last{Tuesday}}), "");
97 
98     year_month_weekday_last ym{year{1234}, std::chrono::January, weekday_last{Tuesday}};
99     for (int i = 0; i <= 10; ++i)
100     {
101         year_month_weekday_last ym1 = ym + years{i};
102         year_month_weekday_last ym2 = years{i} + ym;
103         assert(ym1.year()         == year(1234 + i));
104         assert(ym2.year()         == year(1234 + i));
105         assert(ym1.month()        == January);
106         assert(ym2.month()        == January);
107         assert(ym1.weekday()      == Tuesday);
108         assert(ym2.weekday()      == Tuesday);
109         assert(ym1.weekday_last() == weekday_last{Tuesday});
110         assert(ym2.weekday_last() == weekday_last{Tuesday});
111         assert(ym1 == ym2);
112     }
113     }
114 
115 
116   return 0;
117 }
118