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