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