libcxx

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

plus_minus_equal.pass.cpp (1899B)


      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 day;
     13 
     14 // constexpr day& operator+=(const days& d) noexcept;
     15 // constexpr day& operator-=(const days& 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()
     25 {
     26     D d1{1};
     27     if (static_cast<unsigned>(d1 += Ds{ 1}) !=  2) return false;
     28     if (static_cast<unsigned>(d1 += Ds{ 2}) !=  4) return false;
     29     if (static_cast<unsigned>(d1 += Ds{22}) != 26) return false;
     30     if (static_cast<unsigned>(d1 -= Ds{ 1}) != 25) return false;
     31     if (static_cast<unsigned>(d1 -= Ds{ 2}) != 23) return false;
     32     if (static_cast<unsigned>(d1 -= Ds{22}) !=  1) return false;
     33     return true;
     34 }
     35 
     36 int main()
     37 {
     38     using day  = std::chrono::day;
     39     using days = std::chrono::days;
     40 
     41     ASSERT_NOEXCEPT(std::declval<day&>() += std::declval<days>());
     42     ASSERT_NOEXCEPT(std::declval<day&>() -= std::declval<days>());
     43 
     44     ASSERT_SAME_TYPE(day&, decltype(std::declval<day&>() += std::declval<days>()));
     45     ASSERT_SAME_TYPE(day&, decltype(std::declval<day&>() -= std::declval<days>()));
     46 
     47     static_assert(testConstexpr<day, days>(), "");
     48 
     49     for (unsigned i = 0; i <= 10; ++i)
     50     {
     51         day day(i);
     52         assert(static_cast<unsigned>(day += days{22}) == i + 22);
     53         assert(static_cast<unsigned>(day)             == i + 22);
     54         assert(static_cast<unsigned>(day -= days{12}) == i + 10);
     55         assert(static_cast<unsigned>(day)             == i + 10);
     56     }
     57 }