1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10
11 // <chrono>
12 // class weekday;
13
14 // constexpr weekday operator+(const days& x, const weekday& y) noexcept;
15 // Returns: weekday(int{x} + y.count()).
16 //
17 // constexpr weekday operator+(const weekday& x, const days& y) noexcept;
18 // Returns:
19 // weekday{modulo(static_cast<long long>(unsigned{x}) + y.count(), 7)}
20 // where modulo(n, 7) computes the remainder of n divided by 7 using Euclidean division.
21 // [Note: Given a divisor of 12, Euclidean division truncates towards negative infinity
22 // and always produces a remainder in the range of [0, 6].
23 // Assuming no overflow in the signed summation, this operation results in a weekday
24 // holding a value in the range [0, 6] even if !x.ok(). —end note]
25 // [Example: Monday + days{6} == Sunday. —end example]
26
27
28
29 #include <chrono>
30 #include <type_traits>
31 #include <cassert>
32
33 #include "test_macros.h"
34 #include "../../euclidian.h"
35
36 template <typename M, typename Ms>
testConstexpr()37 constexpr bool testConstexpr()
38 {
39 M m{1};
40 Ms offset{4};
41 if (m + offset != M{5}) return false;
42 if (offset + m != M{5}) return false;
43 // Check the example
44 if (M{1} + Ms{6} != M{0}) return false;
45 return true;
46 }
47
main()48 int main()
49 {
50 using weekday = std::chrono::weekday;
51 using days = std::chrono::days;
52
53 ASSERT_NOEXCEPT( std::declval<weekday>() + std::declval<days>());
54 ASSERT_SAME_TYPE(weekday, decltype(std::declval<weekday>() + std::declval<days>()));
55
56 ASSERT_NOEXCEPT( std::declval<days>() + std::declval<weekday>());
57 ASSERT_SAME_TYPE(weekday, decltype(std::declval<days>() + std::declval<weekday>()));
58
59 static_assert(testConstexpr<weekday, days>(), "");
60
61 for (unsigned i = 0; i <= 6; ++i)
62 for (unsigned j = 0; j <= 6; ++j)
63 {
64 weekday wd1 = weekday{i} + days{j};
65 weekday wd2 = days{j} + weekday{i};
66 assert(wd1 == wd2);
67 assert((static_cast<unsigned>(wd1) == euclidian_addition<unsigned, 0, 6>(i, j)));
68 assert((static_cast<unsigned>(wd2) == euclidian_addition<unsigned, 0, 6>(i, j)));
69 }
70 }
71