libcxx

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

plus_minus_equal_year.pass.cpp (2416B)


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