libcxx

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

plus_minus_equal_month.pass.cpp (3105B)


      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 months& m) noexcept;
     15 // constexpr year_month_weekday_last& 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_last            = std::chrono::weekday_last;
     42     using year_month_weekday_last = std::chrono::year_month_weekday_last;
     43     using months                  = std::chrono::months;
     44 
     45     ASSERT_NOEXCEPT(std::declval<year_month_weekday_last&>() += std::declval<months>());
     46     ASSERT_NOEXCEPT(std::declval<year_month_weekday_last&>() -= std::declval<months>());
     47 
     48     ASSERT_SAME_TYPE(year_month_weekday_last&, decltype(std::declval<year_month_weekday_last&>() += std::declval<months>()));
     49     ASSERT_SAME_TYPE(year_month_weekday_last&, decltype(std::declval<year_month_weekday_last&>() -= std::declval<months>()));
     50 
     51     constexpr weekday Tuesday = std::chrono::Tuesday;
     52     static_assert(testConstexpr<year_month_weekday_last, months>(year_month_weekday_last{year{1234}, month{1}, weekday_last{Tuesday}}), "");
     53 
     54     for (unsigned i = 0; i <= 10; ++i)
     55     {
     56         year y{1234};
     57         year_month_weekday_last ymwd(y, month{i}, weekday_last{Tuesday});
     58 
     59         assert(static_cast<unsigned>((ymwd += months{2}).month()) == i + 2);
     60         assert(ymwd.year()     == y);
     61         assert(ymwd.weekday()  == Tuesday);
     62 
     63         assert(static_cast<unsigned>((ymwd             ).month()) == i + 2);
     64         assert(ymwd.year()     == y);
     65         assert(ymwd.weekday()  == Tuesday);
     66 
     67         assert(static_cast<unsigned>((ymwd -= months{1}).month()) == i + 1);
     68         assert(ymwd.year()     == y);
     69         assert(ymwd.weekday()  == Tuesday);
     70 
     71         assert(static_cast<unsigned>((ymwd             ).month()) == i + 1);
     72         assert(ymwd.year()     == y);
     73         assert(ymwd.weekday()  == Tuesday);
     74     }
     75 }