brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · ae365ad Raw
69 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8// UNSUPPORTED: c++03, c++11, c++14, c++179 10// <chrono>11// class month;12 13// constexpr month operator+(const month& x, const months& y) noexcept;14//   Returns: month(int{x} + y.count()).15//16// constexpr month operator+(const months& x, const month& y) noexcept;17//   Returns:18//     month{modulo(static_cast<long long>(int{x}) + (y.count() - 1), 12) + 1}19//   where modulo(n, 12) computes the remainder of n divided by 12 using Euclidean division.20//   [Note: Given a divisor of 12, Euclidean division truncates towards negative infinity21//   and always produces a remainder in the range of [0, 11].22//   Assuming no overflow in the signed summation, this operation results in a month23//   holding a value in the range [1, 12] even if !x.ok(). -end note]24//   [Example: February + months{11} == January. -end example]25 26#include <chrono>27#include <cassert>28#include <type_traits>29#include <utility>30 31#include "test_macros.h"32 33constexpr bool test() {34  using month  = std::chrono::month;35  using months = std::chrono::months;36 37  month my{2};38  for (unsigned i = 0; i <= 15; ++i) {39    month m1 = my + months{i};40    month m2 = months{i} + my;41    assert(m1.ok());42    assert(m2.ok());43    assert(m1 == m2);44    unsigned exp = i + 2;45    while (exp > 12)46      exp -= 12;47    assert(static_cast<unsigned>(m1) == exp);48    assert(static_cast<unsigned>(m2) == exp);49  }50 51  return true;52}53 54int main(int, char**) {55  using month  = std::chrono::month;56  using months = std::chrono::months;57 58  ASSERT_NOEXCEPT(std::declval<month>() + std::declval<months>());59  ASSERT_NOEXCEPT(std::declval<months>() + std::declval<month>());60 61  ASSERT_SAME_TYPE(month, decltype(std::declval<month>() + std::declval<months>()));62  ASSERT_SAME_TYPE(month, decltype(std::declval<months>() + std::declval<month>()));63 64  test();65  static_assert(test());66 67  return 0;68}69