libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

plus_minus_equal_month.pass.cpp (3240B)


      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 months& m) noexcept;
     15 // constexpr year_month_weekday& operator-=(const months& m) noexcept;
     16 
     17 #include <chrono>
     18 #include <type_traits>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 
     23 template <typename D, typename Ds>
     24 constexpr bool testConstexpr(D d1)
     25 {
     26     if (static_cast<unsigned>((d1          ).month()) !=  1) return false;
     27     if (static_cast<unsigned>((d1 += Ds{ 1}).month()) !=  2) return false;
     28     if (static_cast<unsigned>((d1 += Ds{ 2}).month()) !=  4) return false;
     29     if (static_cast<unsigned>((d1 += Ds{12}).month()) !=  4) return false;
     30     if (static_cast<unsigned>((d1 -= Ds{ 1}).month()) !=  3) return false;
     31     if (static_cast<unsigned>((d1 -= Ds{ 2}).month()) !=  1) return false;
     32     if (static_cast<unsigned>((d1 -= Ds{12}).month()) !=  1) return false;
     33     return true;
     34 }
     35 
     36 int main()
     37 {
     38     using year               = std::chrono::year;
     39     using month              = std::chrono::month;
     40     using weekday            = std::chrono::weekday;
     41     using weekday_indexed    = std::chrono::weekday_indexed;
     42     using year_month_weekday = std::chrono::year_month_weekday;
     43     using months             = std::chrono::months;
     44 
     45 
     46     ASSERT_NOEXCEPT(                               std::declval<year_month_weekday&>() += std::declval<months>());
     47     ASSERT_SAME_TYPE(year_month_weekday&, decltype(std::declval<year_month_weekday&>() += std::declval<months>()));
     48 
     49     ASSERT_NOEXCEPT(                               std::declval<year_month_weekday&>() -= std::declval<months>());
     50     ASSERT_SAME_TYPE(year_month_weekday&, decltype(std::declval<year_month_weekday&>() -= std::declval<months>()));
     51 
     52     constexpr weekday Tuesday = std::chrono::Tuesday;
     53     static_assert(testConstexpr<year_month_weekday, months>(year_month_weekday{year{1234}, month{1}, weekday_indexed{Tuesday, 2}}), "");
     54 
     55     for (unsigned i = 0; i <= 10; ++i)
     56     {
     57         year y{1234};
     58         year_month_weekday ymwd(y, month{i}, weekday_indexed{Tuesday, 2});
     59 
     60         assert(static_cast<unsigned>((ymwd += months{2}).month()) == i + 2);
     61         assert(ymwd.year()     == y);
     62         assert(ymwd.weekday()  == Tuesday);
     63         assert(ymwd.index()    == 2);
     64 
     65         assert(static_cast<unsigned>((ymwd             ).month()) == i + 2);
     66         assert(ymwd.year()     == y);
     67         assert(ymwd.weekday()  == Tuesday);
     68         assert(ymwd.index()    == 2);
     69 
     70         assert(static_cast<unsigned>((ymwd -= months{1}).month()) == i + 1);
     71         assert(ymwd.year()     == y);
     72         assert(ymwd.weekday()  == Tuesday);
     73         assert(ymwd.index()    == 2);
     74 
     75         assert(static_cast<unsigned>((ymwd             ).month()) == i + 1);
     76         assert(ymwd.year()     == y);
     77         assert(ymwd.weekday()  == Tuesday);
     78         assert(ymwd.index()    == 2);
     79     }
     80 }