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;
12
13 // constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;
14 // Returns: (ym.year() + dy) / ym.month().
15 //
16 // constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;
17 // Returns: ym + dy.
18 //
19 // constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;
20 // Returns: A year_month value z such that z.ok() && z - ym == dm is true.
21 // Complexity: O(1) with respect to the value of dm.
22 //
23 // constexpr year_month operator+(const months& dm, const year_month& ym) noexcept;
24 // Returns: ym + dm.
25
26 #include <chrono>
27 #include <type_traits>
28 #include <cassert>
29
30 #include "test_macros.h"
31
32 using year = std::chrono::year;
33 using years = std::chrono::years;
34 using month = std::chrono::month;
35 using months = std::chrono::months;
36 using year_month = std::chrono::year_month;
37
38 // year_month + years
test_ym_plus_y()39 constexpr bool test_ym_plus_y() {
40 ASSERT_NOEXCEPT(std::declval<year_month>() + std::declval<years>());
41 ASSERT_NOEXCEPT(std::declval<years>() + std::declval<year_month>());
42
43 ASSERT_SAME_TYPE(
44 year_month, decltype(std::declval<year_month>() + std::declval<years>()));
45 ASSERT_SAME_TYPE(
46 year_month, decltype(std::declval<years>() + std::declval<year_month>()));
47
48 year_month ym{year{1234}, std::chrono::January};
49 for (int i = 0; i <= 10; ++i) {
50 year_month ym1 = ym + years{i};
51 year_month ym2 = years{i} + ym;
52 assert(static_cast<int>(ym1.year()) == i + 1234);
53 assert(static_cast<int>(ym2.year()) == i + 1234);
54 assert(ym1.month() == std::chrono::January);
55 assert(ym2.month() == std::chrono::January);
56 assert(ym1 == ym2);
57 }
58
59 return true;
60 }
61
62 // year_month + months
test_ym_plus_m()63 constexpr bool test_ym_plus_m() {
64 ASSERT_NOEXCEPT(std::declval<year_month>() + std::declval<months>());
65 ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month>());
66
67 ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() +
68 std::declval<months>()));
69 ASSERT_SAME_TYPE(year_month, decltype(std::declval<months>() +
70 std::declval<year_month>()));
71
72 year_month ym{year{1234}, std::chrono::January};
73 for (int i = 0; i <= 11; ++i) {
74 year_month ym1 = ym + months{i};
75 year_month ym2 = months{i} + ym;
76 assert(static_cast<int>(ym1.year()) == 1234);
77 assert(static_cast<int>(ym2.year()) == 1234);
78 assert(ym1.month() == month(1 + i));
79 assert(ym2.month() == month(1 + i));
80 assert(ym1 == ym2);
81 }
82
83 for (int i = 12; i < 23; ++i) {
84 year_month ym1 = ym + months{i};
85 year_month ym2 = months{i} + ym;
86 assert(static_cast<int>(ym1.year()) == 1235);
87 assert(static_cast<int>(ym2.year()) == 1235);
88 assert(ym1.month() == month(1 + i % 12));
89 assert(ym2.month() == month(1 + i % 12));
90 assert(ym1 == ym2);
91 }
92
93 return true;
94 }
95
test()96 constexpr bool test() {
97 test_ym_plus_y();
98 test_ym_plus_m();
99 return true;
100 }
101
main(int,char **)102 int main(int, char**) {
103 test();
104 static_assert(test());
105
106 return 0;
107 }
108