Home | History | Annotate | Download | only in time.cal.ymwd.nonmembers
      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;
     13 
     14 // constexpr year_month_weekday operator+(const year_month_weekday& ymd, const months& dm) noexcept;
     15 //   Returns: (ymd.year() / ymd.month() + dm) / ymd.day().
     16 //
     17 // constexpr year_month_weekday operator+(const months& dm, const year_month_weekday& ymd) noexcept;
     18 //   Returns: ymd + dm.
     19 //
     20 //
     21 // constexpr year_month_weekday operator+(const year_month_weekday& ymd, const years& dy) noexcept;
     22 //   Returns: (ymd.year() + dy) / ymd.month() / ymd.day().
     23 //
     24 // constexpr year_month_weekday operator+(const years& dy, const year_month_weekday& ymd) noexcept;
     25 //   Returns: ym + dm.
     26 
     27 
     28 
     29 #include <chrono>
     30 #include <type_traits>
     31 #include <cassert>
     32 
     33 #include "test_macros.h"
     34 
     35 constexpr bool testConstexprYears(std::chrono::year_month_weekday ym)
     36 {
     37     std::chrono::years offset{23};
     38     if (static_cast<int>((ym         ).year()) !=  1) return false;
     39     if (static_cast<int>((ym + offset).year()) != 24) return false;
     40     if (static_cast<int>((offset + ym).year()) != 24) return false;
     41     return true;
     42 }
     43 
     44 
     45 constexpr bool testConstexprMonths(std::chrono::year_month_weekday ym)
     46 {
     47     std::chrono::months offset{6};
     48     if (static_cast<unsigned>((ym         ).month()) !=  1) return false;
     49     if (static_cast<unsigned>((ym + offset).month()) !=  7) return false;
     50     if (static_cast<unsigned>((offset + ym).month()) !=  7) return false;
     51     return true;
     52 }
     53 
     54 
     55 int main()
     56 {
     57     using year               = std::chrono::year;
     58     using month              = std::chrono::month;
     59     using weekday            = std::chrono::weekday;
     60     using weekday_indexed    = std::chrono::weekday_indexed;
     61     using year_month_weekday = std::chrono::year_month_weekday;
     62     using years              = std::chrono::years;
     63     using months             = std::chrono::months;
     64 
     65     constexpr weekday Tuesday = std::chrono::Tuesday;
     66     constexpr month January = std::chrono::January;
     67 
     68     {   // year_month_weekday + months (and switched)
     69     ASSERT_NOEXCEPT(std::declval<year_month_weekday>() + std::declval<months>());
     70     ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month_weekday>());
     71 
     72     ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<year_month_weekday>() + std::declval<months>()));
     73     ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<months>() + std::declval<year_month_weekday>()));
     74 
     75     static_assert(testConstexprMonths(year_month_weekday{year{1}, January, weekday_indexed{Tuesday, 1}}), "");
     76 
     77     year_month_weekday ym{year{1234}, January, weekday_indexed{Tuesday, 3}};
     78     for (int i = 0; i <= 10; ++i)  // TODO test wrap-around
     79     {
     80         year_month_weekday ym1 = ym + months{i};
     81         year_month_weekday ym2 = months{i} + ym;
     82         assert(static_cast<int>(ym1.year()) == 1234);
     83         assert(static_cast<int>(ym2.year()) == 1234);
     84         assert(ym1.month()   == month(1 + i));
     85         assert(ym2.month()   == month(1 + i));
     86         assert(ym1.weekday() == Tuesday);
     87         assert(ym2.weekday() == Tuesday);
     88         assert(ym1.index()   == 3);
     89         assert(ym2.index()   == 3);
     90         assert(ym1 == ym2);
     91     }
     92     }
     93 
     94     {   // year_month_weekday + years (and switched)
     95     ASSERT_NOEXCEPT(std::declval<year_month_weekday>() + std::declval<years>());
     96     ASSERT_NOEXCEPT(std::declval<years>() + std::declval<year_month_weekday>());
     97 
     98     ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<year_month_weekday>() + std::declval<years>()));
     99     ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<years>() + std::declval<year_month_weekday>()));
    100 
    101     static_assert(testConstexprYears (year_month_weekday{year{1}, January, weekday_indexed{Tuesday, 1}}), "");
    102 
    103     year_month_weekday ym{year{1234}, std::chrono::January, weekday_indexed{Tuesday, 3}};
    104     for (int i = 0; i <= 10; ++i)
    105     {
    106         year_month_weekday ym1 = ym + years{i};
    107         year_month_weekday ym2 = years{i} + ym;
    108         assert(static_cast<int>(ym1.year()) == i + 1234);
    109         assert(static_cast<int>(ym2.year()) == i + 1234);
    110         assert(ym1.month()   == January);
    111         assert(ym2.month()   == January);
    112         assert(ym1.weekday() == Tuesday);
    113         assert(ym2.weekday() == Tuesday);
    114         assert(ym1.index()   == 3);
    115         assert(ym2.index()   == 3);
    116         assert(ym1 == ym2);
    117     }
    118     }
    119 
    120 }
    121