1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10
11 // <chrono>
12 // class year_month;
13
14 // constexpr year_month operator-(const year_month& ym, const years& dy) noexcept;
15 // Returns: ym + -dy.
16 //
17 // constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;
18 // Returns: ym + -dm.
19 //
20 // constexpr months operator-(const year_month& x, const year_month& y) noexcept;
21 // Returns: x.year() - y.year() + months{static_cast<int>(unsigned{x.month()}) -
22 // static_cast<int>(unsigned{y.month()})}
23
24
25 #include <chrono>
26 #include <type_traits>
27 #include <cassert>
28
29 #include "test_macros.h"
30
31 #include <iostream>
32
main()33 int main()
34 {
35 using year = std::chrono::year;
36 using years = std::chrono::years;
37 using month = std::chrono::month;
38 using months = std::chrono::months;
39 using year_month = std::chrono::year_month;
40
41 { // year_month - years
42 ASSERT_NOEXCEPT( std::declval<year_month>() - std::declval<years>());
43 ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() - std::declval<years>()));
44
45 // static_assert(testConstexprYears (year_month{year{1}, month{1}}), "");
46
47 year_month ym{year{1234}, std::chrono::January};
48 for (int i = 0; i <= 10; ++i)
49 {
50 year_month ym1 = ym - years{i};
51 assert(static_cast<int>(ym1.year()) == 1234 - i);
52 assert(ym1.month() == std::chrono::January);
53 }
54 }
55
56 { // year_month - months
57 ASSERT_NOEXCEPT( std::declval<year_month>() - std::declval<months>());
58 ASSERT_SAME_TYPE(year_month, decltype(std::declval<year_month>() - std::declval<months>()));
59
60 // static_assert(testConstexprMonths(year_month{year{1}, month{1}}), "");
61
62 year_month ym{year{1234}, std::chrono::November};
63 for (int i = 0; i <= 10; ++i) // TODO test wrap-around
64 {
65 year_month ym1 = ym - months{i};
66 assert(static_cast<int>(ym1.year()) == 1234);
67 assert(ym1.month() == month(11 - i));
68 }
69 }
70
71 { // year_month - year_month
72 ASSERT_NOEXCEPT( std::declval<year_month>() - std::declval<year_month>());
73 ASSERT_SAME_TYPE(months, decltype(std::declval<year_month>() - std::declval<year_month>()));
74
75 // static_assert(testConstexprMonths(year_month{year{1}, month{1}}), "");
76
77 // Same year
78 year y{2345};
79 for (int i = 1; i <= 12; ++i)
80 for (int j = 1; j <= 12; ++j)
81 {
82 months diff = year_month{y, month(i)} - year_month{y, month(j)};
83 std::cout << "i: " << i << " j: " << j << " -> " << diff.count() << std::endl;
84 assert(diff.count() == i - j);
85 }
86
87 // TODO: different year
88
89 }
90 }
91