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 
24 #include <chrono>
25 #include <type_traits>
26 #include <cassert>
27 
28 #include "test_macros.h"
29 
main(int,char **)30 int main(int, char**)
31 {
32     using year       = std::chrono::year;
33     using years      = std::chrono::years;
34     using month      = std::chrono::month;
35     using months     = std::chrono::months;
36     using year_month = std::chrono::year_month;
37 
38     {   // year_month - years
39     ASSERT_NOEXCEPT(                      std::declval<year_month>() - std::declval<years>());
40     ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() - std::declval<years>()));
41 
42     // static_assert(testConstexprYears (year_month{year{1}, month{1}}), "");
43 
44     year_month ym{year{1234}, std::chrono::January};
45     for (int i = 0; i <= 10; ++i)
46     {
47         year_month ym1 = ym - years{i};
48         assert(static_cast<int>(ym1.year()) == 1234 - i);
49         assert(ym1.month() == std::chrono::January);
50     }
51     }
52 
53     {   // year_month - months
54     ASSERT_NOEXCEPT(                      std::declval<year_month>() - std::declval<months>());
55     ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() - std::declval<months>()));
56 
57     // static_assert(testConstexprMonths(year_month{year{1}, month{1}}), "");
58 
59     year_month ym{year{1234}, std::chrono::November};
60     for (int i = 0; i <= 10; ++i)  // TODO test wrap-around
61     {
62         year_month ym1 = ym - months{i};
63         assert(static_cast<int>(ym1.year()) == 1234);
64         assert(ym1.month() == month(11 - i));
65     }
66     }
67 
68     {   // year_month - year_month
69     ASSERT_NOEXCEPT(                  std::declval<year_month>() - std::declval<year_month>());
70     ASSERT_SAME_TYPE(months, decltype(std::declval<year_month>() - std::declval<year_month>()));
71 
72     // static_assert(testConstexprMonths(year_month{year{1}, month{1}}), "");
73 
74     //  Same year
75     year y{2345};
76     for (int i = 1; i <= 12; ++i)
77         for (int j = 1; j <= 12; ++j)
78     {
79         months diff = year_month{y, month(i)} - year_month{y, month(j)};
80         assert(diff.count() == i - j);
81     }
82 
83     // TODO: different year
84 
85     }
86 
87     return 0;
88 }
89