libcxx

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

plus_minus_equal.pass.cpp (2240B)


      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 weekday;
     13 
     14 // constexpr weekday& operator+=(const days& d) noexcept;
     15 // constexpr weekday& operator-=(const days& d) noexcept;
     16 
     17 #include <chrono>
     18 #include <type_traits>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 #include "../../euclidian.h"
     23 
     24 template <typename M, typename Ms>
     25 constexpr bool testConstexpr()
     26 {
     27     M m1{1};
     28     if (static_cast<unsigned>(m1 += Ms{ 1}) !=  2) return false;
     29     if (static_cast<unsigned>(m1 += Ms{ 2}) !=  4) return false;
     30     if (static_cast<unsigned>(m1 += Ms{ 4}) !=  1) return false;
     31     if (static_cast<unsigned>(m1 -= Ms{ 1}) !=  0) return false;
     32     if (static_cast<unsigned>(m1 -= Ms{ 2}) !=  5) return false;
     33     if (static_cast<unsigned>(m1 -= Ms{ 4}) !=  1) return false;
     34     return true;
     35 }
     36 
     37 int main()
     38 {
     39     using weekday = std::chrono::weekday;
     40     using days    = std::chrono::days;
     41 
     42     ASSERT_NOEXCEPT(                    std::declval<weekday&>() += std::declval<days&>());
     43     ASSERT_SAME_TYPE(weekday&, decltype(std::declval<weekday&>() += std::declval<days&>()));
     44 
     45     ASSERT_NOEXCEPT(                    std::declval<weekday&>() -= std::declval<days&>());
     46     ASSERT_SAME_TYPE(weekday&, decltype(std::declval<weekday&>() -= std::declval<days&>()));
     47 
     48     static_assert(testConstexpr<weekday, days>(), "");
     49 
     50     for (unsigned i = 0; i <= 6; ++i)
     51     {
     52         weekday wd(i);
     53         assert((static_cast<unsigned>(wd += days{3}) == euclidian_addition<unsigned, 0, 6>(i, 3)));
     54         assert((static_cast<unsigned>(wd)            == euclidian_addition<unsigned, 0, 6>(i, 3)));
     55     }
     56 
     57     for (unsigned i = 0; i <= 6; ++i)
     58     {
     59         weekday wd(i);
     60         assert((static_cast<unsigned>(wd -= days{4}) == euclidian_subtraction<unsigned, 0, 6>(i, 4)));
     61         assert((static_cast<unsigned>(wd)            == euclidian_subtraction<unsigned, 0, 6>(i, 4)));
     62     }
     63 }