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

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