• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 year_month_weekday_last;
13 
14 // constexpr year_month_weekday_last operator-(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
15 //   Returns: ymwdl + (-dm).
16 //
17 // constexpr year_month_weekday_last operator-(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
18 //   Returns: ymwdl + (-dy).
19 
20 #include <chrono>
21 #include <type_traits>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 #include <iostream>
27 
testConstexprYears(std::chrono::year_month_weekday_last ym)28 constexpr bool testConstexprYears(std::chrono::year_month_weekday_last ym)
29 {
30     std::chrono::years offset{14};
31     if (static_cast<int>((ym         ).year()) != 66) return false;
32     if (static_cast<int>((ym - offset).year()) != 52) return false;
33     return true;
34 }
35 
testConstexprMonths(std::chrono::year_month_weekday_last ym)36 constexpr bool testConstexprMonths(std::chrono::year_month_weekday_last ym)
37 {
38     std::chrono::months offset{6};
39     if (static_cast<unsigned>((ym         ).month()) != 10) return false;
40     if (static_cast<unsigned>((ym - offset).month()) !=  4) return false;
41     return true;
42 }
43 
main()44 int main()
45 {
46     using year                    = std::chrono::year;
47     using month                   = std::chrono::month;
48     using weekday                 = std::chrono::weekday;
49     using weekday_last            = std::chrono::weekday_last;
50     using year_month_weekday_last = std::chrono::year_month_weekday_last;
51     using years                   = std::chrono::years;
52     using months                  = std::chrono::months;
53 
54     constexpr month October = std::chrono::October;
55     constexpr weekday Tuesday = std::chrono::Tuesday;
56 
57     { // year_month_weekday_last - years
58 
59     ASSERT_NOEXCEPT(                                   std::declval<year_month_weekday_last>() - std::declval<years>());
60     ASSERT_SAME_TYPE(year_month_weekday_last, decltype(std::declval<year_month_weekday_last>() - std::declval<years>()));
61 
62     static_assert(testConstexprYears(year_month_weekday_last{year{66}, October, weekday_last{Tuesday}}), "");
63 
64     year_month_weekday_last ym{year{1234}, October, weekday_last{Tuesday}};
65     for (int i = 0; i <= 10; ++i)
66     {
67         year_month_weekday_last ym1 = ym - years{i};
68         assert(ym1.year()         == year{1234 - i});
69         assert(ym1.month()        == October);
70         assert(ym1.weekday()      == Tuesday);
71         assert(ym1.weekday_last() == weekday_last{Tuesday});
72     }
73     }
74 
75     { // year_month_weekday_last - months
76 
77     ASSERT_NOEXCEPT(                                   std::declval<year_month_weekday_last>() - std::declval<months>());
78     ASSERT_SAME_TYPE(year_month_weekday_last, decltype(std::declval<year_month_weekday_last>() - std::declval<months>()));
79 
80     static_assert(testConstexprMonths(year_month_weekday_last{year{66}, October, weekday_last{Tuesday}}), "");
81 
82     year_month_weekday_last ym{year{1234}, October, weekday_last{Tuesday}};
83     for (unsigned i = 0; i < 10; ++i)
84     {
85         year_month_weekday_last ym1 = ym - months{i};
86         assert(ym1.year()         == year{1234});
87         assert(ym1.month()        == month{10 - i});
88         assert(ym1.weekday()      == Tuesday);
89         assert(ym1.weekday_last() == weekday_last{Tuesday});
90     }
91     }
92 
93 }
94