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
27
28 #include <chrono>
29 #include <type_traits>
30 #include <cassert>
31
32 #include "test_macros.h"
33 #include "../../euclidian.h"
34
35 template <typename M, typename Ms>
testConstexpr()36 constexpr bool testConstexpr()
37 {
38 M m{1};
39 Ms offset{4};
40 assert(m + offset == M{5});
41 assert(offset + m == M{5});
42 // Check the example
43 assert(M{1} + Ms{6} == M{0});
44 return true;
45 }
46
main(int,char **)47 int main(int, char**)
48 {
49 using weekday = std::chrono::weekday;
50 using days = std::chrono::days;
51
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 static_assert(testConstexpr<weekday, days>(), "");
59
60 for (unsigned i = 0; i <= 6; ++i)
61 for (unsigned j = 0; j <= 6; ++j)
62 {
63 weekday wd1 = weekday{i} + days{j};
64 weekday wd2 = days{j} + weekday{i};
65 assert(wd1 == wd2);
66 assert((wd1.c_encoding() == euclidian_addition<unsigned, 0, 6>(i, j)));
67 assert((wd2.c_encoding() == euclidian_addition<unsigned, 0, 6>(i, j)));
68 }
69
70 return 0;
71 }
72