brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · ecf7a5e Raw
64 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 weekday;12 13// constexpr weekday operator+(const days& x, const weekday& y) noexcept;14//   Returns: weekday(int{x} + y.count()).15//16// constexpr weekday operator+(const weekday& x, const days& y) noexcept;17//   Returns:18//      weekday{modulo(static_cast<long long>(unsigned{x}) + y.count(), 7)}19//   where modulo(n, 7) computes the remainder of n divided by 7 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, 6].22//   Assuming no overflow in the signed summation, this operation results in a weekday23//   holding a value in the range [0, 6] even if !x.ok(). -end note]24//   [Example: Monday + days{6} == Sunday. -end example]25 26#include <chrono>27#include <cassert>28#include <type_traits>29#include <utility>30 31#include "test_macros.h"32#include "../../euclidian.h"33 34using weekday = std::chrono::weekday;35using days    = std::chrono::days;36 37constexpr bool test() {38  for (unsigned i = 0; i <= 6; ++i)39    for (unsigned j = 0; j <= 6; ++j) {40      weekday wd1 = weekday{i} + days{j};41      weekday wd2 = days{j} + weekday{i};42      assert(wd1 == wd2);43      assert((wd1.c_encoding() == euclidian_addition<unsigned, 0, 6>(i, j)));44      assert((wd2.c_encoding() == euclidian_addition<unsigned, 0, 6>(i, j)));45    }46 47  //  Check the example48  assert(weekday{1} + days{6} == weekday{0});49  return true;50}51 52int main(int, char**) {53  ASSERT_NOEXCEPT(std::declval<weekday>() + std::declval<days>());54  ASSERT_SAME_TYPE(weekday, decltype(std::declval<weekday>() + std::declval<days>()));55 56  ASSERT_NOEXCEPT(std::declval<days>() + std::declval<weekday>());57  ASSERT_SAME_TYPE(weekday, decltype(std::declval<days>() + std::declval<weekday>()));58 59  test();60  static_assert(test());61 62  return 0;63}64