Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.ym/time.cal.ym.nonmembers/plus.pass.cpp

raw
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
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(year_month, decltype(std::declval<year_month>() + std::declval<years>()));
44 ASSERT_SAME_TYPE(year_month, decltype(std::declval<years>() + std::declval<year_month>()));
45
46 year_month ym{year{1234}, std::chrono::January};
47 for (int i = 0; i <= 10; ++i) {
48 year_month ym1 = ym + years{i};
49 year_month ym2 = years{i} + ym;
50 assert(static_cast<int>(ym1.year()) == i + 1234);
51 assert(static_cast<int>(ym2.year()) == i + 1234);
52 assert(ym1.month() == std::chrono::January);
53 assert(ym2.month() == std::chrono::January);
54 assert(ym1 == ym2);
55 }
56
57 return true;
58 }
59
60 // year_month + months
61 constexpr bool test_ym_plus_m() {
62 ASSERT_NOEXCEPT(std::declval<year_month>() + std::declval<months>());
63 ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month>());
64
65 ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() + std::declval<months>()));
66 ASSERT_SAME_TYPE(year_month, decltype(std::declval<months>() + std::declval<year_month>()));
67
68 {
69 // [time.cal.ym.nonmembers]/4
70 // Returns: A year_month value z such that z.ok() && z - ym == dm is true.
71 year_month ym = {year{1234}, std::chrono::January};
72 months dm = months{42};
73 year_month z = ym + dm;
74 assert(z.ok() && z - ym == dm);
75 }
76
77 year_month ym{year{1234}, std::chrono::January};
78 for (int i = 0; i <= 11; ++i) {
79 year_month ym1 = ym + months{i};
80 year_month ym2 = months{i} + ym;
81 assert(static_cast<int>(ym1.year()) == 1234);
82 assert(static_cast<int>(ym2.year()) == 1234);
83 assert(ym1.month() == month(1 + i));
84 assert(ym2.month() == month(1 + i));
85 assert(ym1 == ym2);
86 }
87
88 for (int i = 12; i < 23; ++i) {
89 year_month ym1 = ym + months{i};
90 year_month ym2 = months{i} + ym;
91 assert(static_cast<int>(ym1.year()) == 1235);
92 assert(static_cast<int>(ym2.year()) == 1235);
93 assert(ym1.month() == month(1 + i % 12));
94 assert(ym2.month() == month(1 + i % 12));
95 assert(ym1 == ym2);
96 }
97 return true;
98 }
99
100 constexpr bool test() {
101 test_ym_plus_y();
102 test_ym_plus_m();
103 return true;
104 }
105
106 int main(int, char**) {
107 test();
108 static_assert(test());
109
110 return 0;
111 }