Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.month/time.cal.month.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 month;
12
13 // constexpr month operator-(const month& x, const months& y) noexcept;
14 // Returns: x + -y.
15 //
16 // constexpr months operator-(const month& x, const month& y) noexcept;
17 // Returns: If x.ok() == true and y.ok() == true, returns a value m in the range
18 // [months{0}, months{11}] satisfying y + m == x.
19 // Otherwise the value returned is unspecified.
20 // [Example: January - February == months{11}. -end example]
21
22 #include <chrono>
23 #include <type_traits>
24 #include <cassert>
25
26 #include "test_macros.h"
27
28 using month = std::chrono::month;
29 using months = std::chrono::months;
30
31 constexpr bool test() {
32 month m{6};
33 for (unsigned i = 1; i <= 12; ++i) {
34 month m1 = m - months{i};
35 assert(m1.ok());
36 int exp = 6 - i;
37 if (exp < 1)
38 exp += 12;
39 assert(static_cast<unsigned>(m1) == static_cast<unsigned>(exp));
40 }
41
42 // Check the example
43 assert(month{1} - month{2} == months{11});
44
45 return true;
46 }
47
48 int main(int, char**) {
49 ASSERT_NOEXCEPT(std::declval<month>() - std::declval<months>());
50 ASSERT_NOEXCEPT(std::declval<month>() - std::declval<month>());
51
52 ASSERT_SAME_TYPE(month, decltype(std::declval<month>() - std::declval<months>()));
53 ASSERT_SAME_TYPE(months, decltype(std::declval<month>() - std::declval<month>()));
54
55 test();
56 static_assert(test());
57
58 return 0;
59 }