• 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 year_month_weekday;
12 
13 // constexpr year_month_weekday operator-(const year_month_weekday& ymwd, const months& dm) noexcept;
14 //   Returns: ymwd + (-dm).
15 //
16 // constexpr year_month_weekday operator-(const year_month_weekday& ymwd, const years& dy) noexcept;
17 //   Returns: ymwd + (-dy).
18 
19 
20 #include <chrono>
21 #include <type_traits>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 
testConstexprYears()27 constexpr bool testConstexprYears ()
28 {
29     std::chrono::year_month_weekday ym0{std::chrono::year{1234}, std::chrono::January, std::chrono::weekday_indexed{std::chrono::Tuesday, 1}};
30     std::chrono::year_month_weekday ym1 = ym0 - std::chrono::years{10};
31     return
32         ym1.year()    == std::chrono::year{1234-10}
33      && ym1.month()   == std::chrono::January
34      && ym1.weekday() == std::chrono::Tuesday
35      && ym1.index()   == 1
36         ;
37 }
38 
testConstexprMonths()39 constexpr bool testConstexprMonths ()
40 {
41     std::chrono::year_month_weekday ym0{std::chrono::year{1234}, std::chrono::November, std::chrono::weekday_indexed{std::chrono::Tuesday, 1}};
42     std::chrono::year_month_weekday ym1 = ym0 - std::chrono::months{6};
43     return
44         ym1.year()    == std::chrono::year{1234}
45      && ym1.month()   == std::chrono::May
46      && ym1.weekday() == std::chrono::Tuesday
47      && ym1.index()   == 1
48         ;
49 }
50 
51 
main(int,char **)52 int main(int, char**)
53 {
54     using year               = std::chrono::year;
55     using month              = std::chrono::month;
56     using weekday            = std::chrono::weekday;
57     using weekday_indexed    = std::chrono::weekday_indexed;
58     using year_month_weekday = std::chrono::year_month_weekday;
59     using years              = std::chrono::years;
60     using months             = std::chrono::months;
61 
62     constexpr month November  = std::chrono::November;
63     constexpr weekday Tuesday = std::chrono::Tuesday;
64 
65     {  // year_month_weekday - years
66     ASSERT_NOEXCEPT(                              std::declval<year_month_weekday>() - std::declval<years>());
67     ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<year_month_weekday>() - std::declval<years>()));
68 
69     static_assert(testConstexprYears(), "");
70 
71     year_month_weekday ym{year{1234}, November, weekday_indexed{Tuesday, 1}};
72     for (int i = 0; i <= 10; ++i)
73     {
74         year_month_weekday ym1 = ym - years{i};
75         assert(static_cast<int>(ym1.year()) == 1234 - i);
76         assert(ym1.month()   == November);
77         assert(ym1.weekday() == Tuesday);
78         assert(ym1.index()   == 1);
79     }
80     }
81 
82     {  // year_month_weekday - months
83     ASSERT_NOEXCEPT(                              std::declval<year_month_weekday>() - std::declval<months>());
84     ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<year_month_weekday>() - std::declval<months>()));
85 
86     static_assert(testConstexprMonths(), "");
87 
88     year_month_weekday ym{year{1234}, November, weekday_indexed{Tuesday, 2}};
89     for (unsigned i = 1; i <= 10; ++i)
90     {
91         year_month_weekday ym1 = ym - months{i};
92         assert(ym1.year()    == year{1234});
93         assert(ym1.month()   == month{11-i});
94         assert(ym1.weekday() == Tuesday);
95         assert(ym1.index()   == 2);
96     }
97     }
98 
99   return 0;
100 }
101