libcxx

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

plus.pass.cpp (2352B)


      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 month;
     13 
     14 // constexpr month operator+(const month& x, const months& y) noexcept;
     15 //   Returns: month(int{x} + y.count()).
     16 //
     17 // constexpr month operator+(const months& x, const month& y) noexcept;
     18 //   Returns:
     19 //     month{modulo(static_cast<long long>(int{x}) + (y.count() - 1), 12) + 1}
     20 //   where modulo(n, 12) computes the remainder of n divided by 12 using Euclidean division.
     21 //   [Note: Given a divisor of 12, Euclidean division truncates towards negative infinity
     22 //   and always produces a remainder in the range of [0, 11].
     23 //   Assuming no overflow in the signed summation, this operation results in a month
     24 //   holding a value in the range [1, 12] even if !x.ok(). —end note]
     25 //   [Example: February + months{11} == January. —end example]
     26 
     27 
     28 
     29 #include <chrono>
     30 #include <type_traits>
     31 #include <cassert>
     32 
     33 #include "test_macros.h"
     34 
     35 template <typename M, typename Ms>
     36 constexpr bool testConstexpr()
     37 {
     38     M m{1};
     39     Ms offset{4};
     40     if (m + offset != M{5}) return false;
     41     if (offset + m != M{5}) return false;
     42 //  Check the example
     43     if (M{2} + Ms{11} != M{1}) return false;
     44     return true;
     45 }
     46 
     47 int main()
     48 {
     49     using month  = std::chrono::month;
     50     using months = std::chrono::months;
     51 
     52     ASSERT_NOEXCEPT(std::declval<month>() + std::declval<months>());
     53     ASSERT_NOEXCEPT(std::declval<months>() + std::declval<month>());
     54 
     55     ASSERT_SAME_TYPE(month, decltype(std::declval<month>()  + std::declval<months>()));
     56     ASSERT_SAME_TYPE(month, decltype(std::declval<months>() + std::declval<month>() ));
     57 
     58     static_assert(testConstexpr<month, months>(), "");
     59 
     60     month my{2};
     61     for (unsigned i = 0; i <= 15; ++i)
     62     {
     63         month m1 = my + months{i};
     64         month m2 = months{i} + my;
     65         assert(m1 == m2);
     66         unsigned exp = i + 2;
     67         while (exp > 12)
     68             exp -= 12;
     69         assert(static_cast<unsigned>(m1) == exp);
     70         assert(static_cast<unsigned>(m2) == exp);
     71     }
     72 }