Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.month/time.cal.month.nonmembers/plus.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: month(int{x} + y.count()).
15 //
16 // constexpr month operator+(const months& x, const month& y) noexcept;
17 // Returns:
18 // month{modulo(static_cast<long long>(int{x}) + (y.count() - 1), 12) + 1}
19 // where modulo(n, 12) computes the remainder of n divided by 12 using Euclidean division.
20 // [Note: Given a divisor of 12, Euclidean division truncates towards negative infinity
21 // and always produces a remainder in the range of [0, 11].
22 // Assuming no overflow in the signed summation, this operation results in a month
23 // holding a value in the range [1, 12] even if !x.ok(). -end note]
24 // [Example: February + months{11} == January. -end example]
25
26 #include <chrono>
27 #include <type_traits>
28 #include <cassert>
29
30 #include "test_macros.h"
31
32 constexpr bool test() {
33 using month = std::chrono::month;
34 using months = std::chrono::months;
35
36 month my{2};
37 for (unsigned i = 0; i <= 15; ++i) {
38 month m1 = my + months{i};
39 month m2 = months{i} + my;
40 assert(m1.ok());
41 assert(m2.ok());
42 assert(m1 == m2);
43 unsigned exp = i + 2;
44 while (exp > 12)
45 exp -= 12;
46 assert(static_cast<unsigned>(m1) == exp);
47 assert(static_cast<unsigned>(m2) == exp);
48 }
49
50 return true;
51 }
52
53 int main(int, char**) {
54 using month = std::chrono::month;
55 using months = std::chrono::months;
56
57 ASSERT_NOEXCEPT(std::declval<month>() + std::declval<months>());
58 ASSERT_NOEXCEPT(std::declval<months>() + std::declval<month>());
59
60 ASSERT_SAME_TYPE(month, decltype(std::declval<month>() + std::declval<months>()));
61 ASSERT_SAME_TYPE(month, decltype(std::declval<months>() + std::declval<month>()));
62
63 test();
64 static_assert(test());
65
66 return 0;
67 }