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