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