116 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 year_month_weekday;12 13// constexpr year_month_weekday operator+(const year_month_weekday& ymd, const months& dm) noexcept;14// Returns: (ymd.year() / ymd.month() + dm) / ymd.day().15//16// constexpr year_month_weekday operator+(const months& dm, const year_month_weekday& ymd) noexcept;17// Returns: ymd + dm.18//19//20// constexpr year_month_weekday operator+(const year_month_weekday& ymd, const years& dy) noexcept;21// Returns: (ymd.year() + dy) / ymd.month() / ymd.day().22//23// constexpr year_month_weekday operator+(const years& dy, const year_month_weekday& ymd) noexcept;24// Returns: ym + dm.25 26#include <chrono>27#include <cassert>28#include <type_traits>29#include <utility>30 31#include "test_macros.h"32 33using year = std::chrono::year;34using month = std::chrono::month;35using weekday = std::chrono::weekday;36using weekday_indexed = std::chrono::weekday_indexed;37using year_month_weekday = std::chrono::year_month_weekday;38using years = std::chrono::years;39using months = std::chrono::months;40 41constexpr bool test() {42 constexpr weekday Tuesday = std::chrono::Tuesday;43 constexpr month January = std::chrono::January;44 45 { // year_month_weekday + months (and switched)46 year_month_weekday ym{year{1234}, January, weekday_indexed{Tuesday, 3}};47 for (int i = 0; i <= 10; ++i) {48 year_month_weekday ymwd1 = ym + months{i};49 year_month_weekday ymwd2 = months{i} + ym;50 assert(static_cast<int>(ymwd1.year()) == 1234);51 assert(static_cast<int>(ymwd2.year()) == 1234);52 assert(ymwd1.month() == month(1 + i));53 assert(ymwd2.month() == month(1 + i));54 assert(ymwd1.weekday() == Tuesday);55 assert(ymwd2.weekday() == Tuesday);56 assert(ymwd1.index() == 3);57 assert(ymwd2.index() == 3);58 assert(ymwd1 == ymwd2);59 }60 // Test the year wraps around.61 for (int i = 12; i <= 15; ++i) {62 year_month_weekday ymwd1 = ym + months{i};63 year_month_weekday ymwd2 = months{i} + ym;64 assert(static_cast<int>(ymwd1.year()) == 1235);65 assert(static_cast<int>(ymwd2.year()) == 1235);66 assert(ymwd1.month() == month(1 + i - 12));67 assert(ymwd2.month() == month(1 + i - 12));68 assert(ymwd1.weekday() == Tuesday);69 assert(ymwd2.weekday() == Tuesday);70 assert(ymwd1.index() == 3);71 assert(ymwd2.index() == 3);72 assert(ymwd1 == ymwd2);73 }74 }75 76 { // year_month_weekday + years (and switched)77 year_month_weekday ym{year{1234}, std::chrono::January, weekday_indexed{Tuesday, 3}};78 for (int i = 0; i <= 10; ++i) {79 year_month_weekday ymwd1 = ym + years{i};80 year_month_weekday ymwd2 = years{i} + ym;81 assert(static_cast<int>(ymwd1.year()) == i + 1234);82 assert(static_cast<int>(ymwd2.year()) == i + 1234);83 assert(ymwd1.month() == January);84 assert(ymwd2.month() == January);85 assert(ymwd1.weekday() == Tuesday);86 assert(ymwd2.weekday() == Tuesday);87 assert(ymwd1.index() == 3);88 assert(ymwd2.index() == 3);89 assert(ymwd1 == ymwd2);90 }91 }92 93 return true;94}95 96int main(int, char**) {97 // year_month_weekday + months (and switched)98 ASSERT_NOEXCEPT(std::declval<year_month_weekday>() + std::declval<months>());99 ASSERT_NOEXCEPT(std::declval<months>() + std::declval<year_month_weekday>());100 101 ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<year_month_weekday>() + std::declval<months>()));102 ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<months>() + std::declval<year_month_weekday>()));103 104 // year_month_weekday + years (and switched)105 ASSERT_NOEXCEPT(std::declval<year_month_weekday>() + std::declval<years>());106 ASSERT_NOEXCEPT(std::declval<years>() + std::declval<year_month_weekday>());107 108 ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<year_month_weekday>() + std::declval<years>()));109 ASSERT_SAME_TYPE(year_month_weekday, decltype(std::declval<years>() + std::declval<year_month_weekday>()));110 111 test();112 static_assert(test());113 114 return 0;115}116