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

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