Debian Codegraph @

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