brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · 85ef094 Raw
98 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 bool ok() const noexcept;14//  Returns: If any of y_.ok(), m_.ok(), or wdi_.ok() is false, returns false.15//           Otherwise, if *this represents a valid date, returns true.16//           Otherwise, returns false.17 18#include <chrono>19#include <cassert>20#include <type_traits>21#include <utility>22 23#include "test_macros.h"24 25int main(int, char**)26{27    using year               = std::chrono::year;28    using month              = std::chrono::month;29    using weekday            = std::chrono::weekday;30    using weekday_indexed    = std::chrono::weekday_indexed;31    using year_month_weekday = std::chrono::year_month_weekday;32 33    constexpr month January     = std::chrono::January;34    constexpr weekday Monday    = std::chrono::Monday;35    constexpr weekday Tuesday   = std::chrono::Tuesday;36    constexpr weekday Wednesday = std::chrono::Wednesday;37    constexpr weekday Thursday  = std::chrono::Thursday;38    constexpr weekday Friday    = std::chrono::Friday;39    constexpr weekday Saturday  = std::chrono::Saturday;40    constexpr weekday Sunday    = std::chrono::Sunday;41 42    ASSERT_NOEXCEPT(                std::declval<const year_month_weekday>().ok());43    ASSERT_SAME_TYPE(bool, decltype(std::declval<const year_month_weekday>().ok()));44 45    static_assert(!year_month_weekday{}.ok(), "");46 47    static_assert(!year_month_weekday{year{-32768}, month{}, weekday_indexed{}}.ok(),           ""); // All three bad48 49    static_assert(!year_month_weekday{year{-32768}, January, weekday_indexed{Tuesday, 1}}.ok(), ""); // Bad year50    static_assert(!year_month_weekday{year{2019},   month{}, weekday_indexed{Tuesday, 1}}.ok(), ""); // Bad month51    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{} }.ok(),          ""); // Bad day52 53    static_assert(!year_month_weekday{year{-32768}, month{}, weekday_indexed{Tuesday, 1}}.ok(), ""); // Bad year & month54    static_assert(!year_month_weekday{year{2019},   month{}, weekday_indexed{} }.ok(),          ""); // Bad month & day55    static_assert(!year_month_weekday{year{-32768}, January, weekday_indexed{} }.ok(),          ""); // Bad year & day56 57    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{Tuesday, static_cast<unsigned>(-1)}}.ok(), ""); // Bad index.58    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{Wednesday, 0}}.ok(), "");                       // Bad index.59 60    static_assert( year_month_weekday{year{2019},   January, weekday_indexed{Tuesday, 1}}.ok(), ""); // All OK61    static_assert( year_month_weekday{year{2019},   January, weekday_indexed{Tuesday, 4}}.ok(), ""); // All OK62 63    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{Monday, 5}}.ok(),    ""); // Bad index64    static_assert( year_month_weekday{year{2019},   January, weekday_indexed{Tuesday, 5}}.ok(),   ""); // All OK65    static_assert( year_month_weekday{year{2019},   January, weekday_indexed{Wednesday, 5}}.ok(), ""); // All OK66    static_assert( year_month_weekday{year{2019},   January, weekday_indexed{Thursday, 5}}.ok(),  ""); // All OK67    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{Friday, 5}}.ok(),    ""); // Bad index68    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{Saturday, 5}}.ok(),  ""); // Bad index69    static_assert(!year_month_weekday{year{2019},   January, weekday_indexed{Sunday, 5}}.ok(),    ""); // Bad index70 71    for (unsigned i = 0; i <= 50; ++i)72    {73        year_month_weekday ym{year{2019}, January, weekday_indexed{Tuesday, i}};74        assert((ym.ok() == weekday_indexed{Tuesday, i}.ok()));75    }76 77    for (unsigned i = 0; i <= 50; ++i)78    {79        year_month_weekday ym{year{2019}, January, weekday_indexed{weekday{i}, 1}};80        assert((ym.ok() == weekday_indexed{weekday{i}, 1}.ok()));81    }82 83    for (unsigned i = 0; i <= 50; ++i)84    {85        year_month_weekday ym{year{2019}, month{i}, weekday_indexed{Tuesday, 1}};86        assert((ym.ok() == month{i}.ok()));87    }88 89    const int ymax = static_cast<int>(year::max());90    for (int i = ymax - 100; i <= ymax + 100; ++i)91    {92        year_month_weekday ym{year{i}, January, weekday_indexed{Tuesday, 1}};93        assert((ym.ok() == year{i}.ok()));94    }95 96  return 0;97}98