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 weekday& x, const days& y) noexcept;
15 //   Returns: x + -y.
16 //
17 // constexpr days operator-(const weekday& x, const weekday& y) noexcept;
18 // Returns: If x.ok() == true and y.ok() == true, returns a value d in the range
19 //    [days{0}, days{6}] satisfying y + d == x.
20 // Otherwise the value returned is unspecified.
21 // [Example: Sunday - Monday == days{6}. —end example]
22 
23 
24 extern "C" int printf(const char *, ...);
25 
26 #include <chrono>
27 #include <type_traits>
28 #include <cassert>
29 
30 #include "test_macros.h"
31 #include "../../euclidian.h"
32 
33 template <typename WD, typename Ds>
testConstexpr()34 constexpr bool testConstexpr()
35 {
36     {
37     WD wd{5};
38     Ds offset{3};
39     if (wd - offset != WD{2}) return false;
40     if (wd - WD{2} != offset) return false;
41     }
42 
43 //  Check the example
44     if (WD{0} - WD{1} != Ds{6}) 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<weekday>() - std::declval<weekday>());
57     ASSERT_SAME_TYPE(days,    decltype(std::declval<weekday>() - 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 wd = weekday{i} - days{j};
65             assert(wd + days{j} == weekday{i});
66             assert((static_cast<unsigned>(wd) == euclidian_subtraction<unsigned, 0, 6>(i, j)));
67         }
68 
69     for (unsigned i = 0; i <= 6; ++i)
70         for (unsigned j = 0; j <= 6; ++j)
71         {
72             days d = weekday{j} - weekday{i};
73             assert(weekday{i} + d == weekday{j});
74         }
75 
76 }
77