libcxx

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

plus_minus_equal.pass.cpp (1882B)


      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;
     13 
     14 // constexpr year& operator+=(const years& d) noexcept;
     15 // constexpr year& 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 Y, typename Ys>
     24 constexpr bool testConstexpr()
     25 {
     26     Y y1{1};
     27     if (static_cast<int>(y1 += Ys{ 1}) !=  2) return false;
     28     if (static_cast<int>(y1 += Ys{ 2}) !=  4) return false;
     29     if (static_cast<int>(y1 += Ys{ 8}) != 12) return false;
     30     if (static_cast<int>(y1 -= Ys{ 1}) != 11) return false;
     31     if (static_cast<int>(y1 -= Ys{ 2}) !=  9) return false;
     32     if (static_cast<int>(y1 -= Ys{ 8}) !=  1) return false;
     33     return true;
     34 }
     35 
     36 int main()
     37 {
     38     using year  = std::chrono::year;
     39     using years = std::chrono::years;
     40 
     41     ASSERT_NOEXCEPT(std::declval<year&>() += std::declval<years>());
     42     ASSERT_NOEXCEPT(std::declval<year&>() -= std::declval<years>());
     43 
     44     ASSERT_SAME_TYPE(year&, decltype(std::declval<year&>() += std::declval<years>()));
     45     ASSERT_SAME_TYPE(year&, decltype(std::declval<year&>() -= std::declval<years>()));
     46 
     47     static_assert(testConstexpr<year, years>(), "");
     48 
     49     for (int i = 10000; i <= 10020; ++i)
     50     {
     51         year year(i);
     52         assert(static_cast<int>(year += years{10}) == i + 10);
     53         assert(static_cast<int>(year)              == i + 10);
     54         assert(static_cast<int>(year -= years{ 9}) == i +  1);
     55         assert(static_cast<int>(year)              == i +  1);
     56     }
     57 }