Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.ym/time.cal.ym.nonmembers/minus.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 + -dy.
15 //
16 // constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;
17 // Returns: ym + -dm.
18 //
19 // constexpr months operator-(const year_month& x, const year_month& y) noexcept;
20 // Returns: x.year() - y.year() + months{static_cast<int>(unsigned{x.month()}) -
21 // static_cast<int>(unsigned{y.month()})}
22
23 #include <chrono>
24 #include <type_traits>
25 #include <cassert>
26
27 #include "test_macros.h"
28
29 using year = std::chrono::year;
30 using years = std::chrono::years;
31 using month = std::chrono::month;
32 using months = std::chrono::months;
33 using year_month = std::chrono::year_month;
34
35 constexpr bool test() {
36 { // year_month - years
37
38 year_month ym{year{1234}, std::chrono::January};
39 for (int i = 0; i <= 10; ++i) {
40 year_month ym1 = ym - years{i};
41 assert(static_cast<int>(ym1.year()) == 1234 - i);
42 assert(ym1.month() == std::chrono::January);
43 }
44 }
45
46 { // year_month - months
47
48 year_month ym{year{1234}, std::chrono::November};
49 for (int i = 0; i <= 10; ++i) {
50 year_month ym1 = ym - months{i};
51 assert(static_cast<int>(ym1.year()) == 1234);
52 assert(ym1.month() == month(11 - i));
53 }
54 // Test the year wraps around.
55 for (int i = 12; i <= 15; ++i) {
56 year_month ym1 = ym - months{i};
57 assert(static_cast<int>(ym1.year()) == 1233);
58 assert(ym1.month() == month(11 - i + 12));
59 }
60 }
61
62 { // year_month - year_month
63
64 // Same year
65 year y{2345};
66 for (int i = 1; i <= 12; ++i)
67 for (int j = 1; j <= 12; ++j) {
68 months diff = year_month{y, month(i)} - year_month{y, month(j)};
69 assert(diff.count() == i - j);
70 }
71
72 // TODO: different year
73 }
74 return true;
75 }
76
77 int main(int, char**) {
78 // year_month - years
79 ASSERT_NOEXCEPT(std::declval<year_month>() - std::declval<years>());
80 ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() - std::declval<years>()));
81
82 // year_month - months
83 ASSERT_NOEXCEPT(std::declval<year_month>() - std::declval<months>());
84 ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() - std::declval<months>()));
85
86 // year_month - year_month
87 ASSERT_NOEXCEPT(std::declval<year_month>() - std::declval<year_month>());
88 ASSERT_SAME_TYPE(months, decltype(std::declval<year_month>() - std::declval<year_month>()));
89
90 test();
91 static_assert(test());
92
93 return 0;
94 }