libcxx

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

minus.pass.cpp (1635B)


      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 day& x, const days& y) noexcept;
     15 //   Returns: x + -y.
     16 //
     17 // constexpr days operator-(const day& x, const day& y) noexcept;
     18 //   Returns: days{int(unsigned{x}) - int(unsigned{y}).
     19 
     20 
     21 #include <chrono>
     22 #include <type_traits>
     23 #include <cassert>
     24 
     25 #include "test_macros.h"
     26 
     27 template <typename D, typename Ds>
     28 constexpr bool testConstexpr()
     29 {
     30     D d{23};
     31     Ds offset{6};
     32     if (d - offset != D{17}) return false;
     33     if (d - D{17} != offset) return false;
     34     return true;
     35 }
     36 
     37 int main()
     38 {
     39     using day  = std::chrono::day;
     40     using days = std::chrono::days;
     41 
     42     ASSERT_NOEXCEPT(std::declval<day>() - std::declval<days>());
     43     ASSERT_NOEXCEPT(std::declval<day>() - std::declval<day>());
     44 
     45     ASSERT_SAME_TYPE(day,  decltype(std::declval<day>() - std::declval<days>()));
     46     ASSERT_SAME_TYPE(days, decltype(std::declval<day>() - std::declval<day>()));
     47 
     48     static_assert(testConstexpr<day, days>(), "");
     49 
     50     day dy{12};
     51     for (unsigned i = 0; i <= 10; ++i)
     52     {
     53         day d1   = dy - days{i};
     54         days off = dy - day {i};
     55         assert(static_cast<unsigned>(d1) == 12 - i);
     56         assert(off.count() == static_cast<int>(12 - i)); // days is signed
     57     }
     58 }