Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.weekday/time.cal.weekday.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 weekday;
12
13 // constexpr weekday operator+(const days& x, const weekday& y) noexcept;
14 // Returns: weekday(int{x} + y.count()).
15 //
16 // constexpr weekday operator+(const weekday& x, const days& y) noexcept;
17 // Returns:
18 // weekday{modulo(static_cast<long long>(unsigned{x}) + y.count(), 7)}
19 // where modulo(n, 7) computes the remainder of n divided by 7 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, 6].
22 // Assuming no overflow in the signed summation, this operation results in a weekday
23 // holding a value in the range [0, 6] even if !x.ok(). -end note]
24 // [Example: Monday + days{6} == Sunday. -end example]
25
26 #include <chrono>
27 #include <type_traits>
28 #include <cassert>
29
30 #include "test_macros.h"
31 #include "../../euclidian.h"
32
33 using weekday = std::chrono::weekday;
34 using days = std::chrono::days;
35
36 constexpr bool test() {
37 for (unsigned i = 0; i <= 6; ++i)
38 for (unsigned j = 0; j <= 6; ++j) {
39 weekday wd1 = weekday{i} + days{j};
40 weekday wd2 = days{j} + weekday{i};
41 assert(wd1 == wd2);
42 assert((wd1.c_encoding() == euclidian_addition<unsigned, 0, 6>(i, j)));
43 assert((wd2.c_encoding() == euclidian_addition<unsigned, 0, 6>(i, j)));
44 }
45
46 // Check the example
47 assert(weekday{1} + days{6} == weekday{0});
48 return true;
49 }
50
51 int main(int, char**) {
52 ASSERT_NOEXCEPT(std::declval<weekday>() + std::declval<days>());
53 ASSERT_SAME_TYPE(weekday, decltype(std::declval<weekday>() + std::declval<days>()));
54
55 ASSERT_NOEXCEPT(std::declval<days>() + std::declval<weekday>());
56 ASSERT_SAME_TYPE(weekday, decltype(std::declval<days>() + std::declval<weekday>()));
57
58 test();
59 static_assert(test());
60
61 return 0;
62 }