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 months& d) noexcept;
14// constexpr year_month& operator-=(const months& d) noexcept;
15
16#include <chrono>
17#include <cassert>
18#include <type_traits>
19#include <utility>
20
21#include "test_macros.h"
22
23using month = std::chrono::month;
24using months = std::chrono::months;
25using year = std::chrono::year;
26using year_month = std::chrono::year_month;
27
28constexpr bool test() {
29 for (unsigned i = 0; i <= 10; ++i) {
30 year y{1234};
31 year_month ym(y, month{i});
32 assert(static_cast<unsigned>((ym += months{2}).month()) == i + 2);
33 assert(ym.year() == y);
34 assert(static_cast<unsigned>((ym).month()) == i + 2);
35 assert(ym.year() == y);
36 assert(static_cast<unsigned>((ym -= months{1}).month()) == i + 1);
37 assert(ym.year() == y);
38 assert(static_cast<unsigned>((ym).month()) == i + 1);
39 assert(ym.year() == y);
40 }
41
42 { // Test year wrapping
43 year_month ym{year{2020}, month{4}};
44
45 ym += months{12};
46 assert((ym == year_month{year{2021}, month{4}}));
47
48 ym -= months{12};
49 assert((ym == year_month{year{2020}, month{4}}));
50 }
51
52 return true;
53}
54
55int main(int, char**) {
56 ASSERT_NOEXCEPT(std::declval<year_month&>() += std::declval<months>());
57 ASSERT_SAME_TYPE(year_month&, decltype(std::declval<year_month&>() += std::declval<months>()));
58
59 ASSERT_NOEXCEPT(std::declval<year_month&>() -= std::declval<months>());
60 ASSERT_SAME_TYPE(year_month&, decltype(std::declval<year_month&>() -= std::declval<months>()));
61
62 test();
63 static_assert(test());
64
65 return 0;
66}
67

source code of libcxx/test/std/time/time.cal/time.cal.ym/time.cal.ym.members/plus_minus_equal_month.pass.cpp