libcxx

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

plus_minus_equal_year.pass.cpp (3083B)


      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 years& d) noexcept;
     15 // constexpr year_month_weekday_last& operator-=(const years& d) 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<int>((d1          ).year()) !=  1) return false;
     27     if (static_cast<int>((d1 += Ds{ 1}).year()) !=  2) return false;
     28     if (static_cast<int>((d1 += Ds{ 2}).year()) !=  4) return false;
     29     if (static_cast<int>((d1 += Ds{12}).year()) != 16) return false;
     30     if (static_cast<int>((d1 -= Ds{ 1}).year()) != 15) return false;
     31     if (static_cast<int>((d1 -= Ds{ 2}).year()) != 13) return false;
     32     if (static_cast<int>((d1 -= Ds{12}).year()) !=  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 years                   = std::chrono::years;
     44 
     45     ASSERT_NOEXCEPT(std::declval<year_month_weekday_last&>() += std::declval<years>());
     46     ASSERT_NOEXCEPT(std::declval<year_month_weekday_last&>() -= std::declval<years>());
     47 
     48     ASSERT_SAME_TYPE(year_month_weekday_last&, decltype(std::declval<year_month_weekday_last&>() += std::declval<years>()));
     49     ASSERT_SAME_TYPE(year_month_weekday_last&, decltype(std::declval<year_month_weekday_last&>() -= std::declval<years>()));
     50 
     51     constexpr weekday Tuesday = std::chrono::Tuesday;
     52     constexpr month January = std::chrono::January;
     53 
     54     static_assert(testConstexpr<year_month_weekday_last, years>(year_month_weekday_last{year{1}, January, weekday_last{Tuesday}}), "");
     55 
     56     for (int i = 1000; i <= 1010; ++i)
     57     {
     58         year_month_weekday_last ymwd(year{i}, January, weekday_last{Tuesday});
     59 
     60         assert(static_cast<int>((ymwd += years{2}).year()) == i + 2);
     61         assert(ymwd.month()    == January);
     62         assert(ymwd.weekday()  == Tuesday);
     63 
     64         assert(static_cast<int>((ymwd            ).year()) == i + 2);
     65         assert(ymwd.month()    == January);
     66         assert(ymwd.weekday()  == Tuesday);
     67 
     68         assert(static_cast<int>((ymwd -= years{1}).year()) == i + 1);
     69         assert(ymwd.month()    == January);
     70         assert(ymwd.weekday()  == Tuesday);
     71 
     72         assert(static_cast<int>((ymwd            ).year()) == i + 1);
     73         assert(ymwd.month()    == January);
     74         assert(ymwd.weekday()  == Tuesday);
     75     }
     76 }