libcxx

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

plus_minus_equal_month.pass.cpp (2495B)


      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;
     13 
     14 // constexpr year_month& operator+=(const months& d) noexcept;
     15 // constexpr year_month& operator-=(const months& 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<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 month      = std::chrono::month;
     39     using months     = std::chrono::months;
     40     using year       = std::chrono::year;
     41     using year_month = std::chrono::year_month;
     42 
     43     ASSERT_NOEXCEPT(                       std::declval<year_month&>() += std::declval<months>());
     44     ASSERT_SAME_TYPE(year_month&, decltype(std::declval<year_month&>() += std::declval<months>()));
     45 
     46     ASSERT_NOEXCEPT(                       std::declval<year_month&>() -= std::declval<months>());
     47     ASSERT_SAME_TYPE(year_month&, decltype(std::declval<year_month&>() -= std::declval<months>()));
     48 
     49     static_assert(testConstexpr<year_month, months>(year_month{year{1234}, month{1}}), "");
     50 
     51     for (unsigned i = 0; i <= 10; ++i)
     52     {
     53         year y{1234};
     54         year_month ym(y, month{i});
     55         assert(static_cast<unsigned>((ym += months{2}).month()) == i + 2);
     56         assert(ym.year() == y);
     57         assert(static_cast<unsigned>((ym             ).month()) == i + 2);
     58         assert(ym.year() == y);
     59         assert(static_cast<unsigned>((ym -= months{1}).month()) == i + 1);
     60         assert(ym.year() == y);
     61         assert(static_cast<unsigned>((ym             ).month()) == i + 1);
     62         assert(ym.year() == y);
     63     }
     64 }