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