82 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 9// UNSUPPORTED: c++03, c++11, c++1410// <chrono>11 12// ceil13 14// template <class ToDuration, class Clock, class Duration>15// time_point<Clock, ToDuration>16// ceil(const time_point<Clock, Duration>& t);17 18#include <chrono>19#include <type_traits>20#include <cassert>21 22#include "test_macros.h"23 24template <class T, class = void>25inline constexpr bool has_ceil_v = false;26 27template <class T>28inline constexpr bool has_ceil_v<T, decltype((void)std::chrono::ceil<T>(std::chrono::system_clock::now()))> = true;29 30static_assert(has_ceil_v<std::chrono::seconds>);31static_assert(!has_ceil_v<int>);32 33template <class FromDuration, class ToDuration>34void35test(const FromDuration& df, const ToDuration& d)36{37 typedef std::chrono::system_clock Clock;38 typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;39 typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;40 {41 FromTimePoint f(df);42 ToTimePoint t(d);43 typedef decltype(std::chrono::ceil<ToDuration>(f)) R;44 static_assert((std::is_same<R, ToTimePoint>::value), "");45 assert(std::chrono::ceil<ToDuration>(f) == t);46 }47}48 49template<class FromDuration, long long From, class ToDuration, long long To>50void test_constexpr ()51{52 typedef std::chrono::system_clock Clock;53 typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;54 typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;55 {56 constexpr FromTimePoint f{FromDuration{From}};57 constexpr ToTimePoint t{ToDuration{To}};58 static_assert(std::chrono::ceil<ToDuration>(f) == t, "");59 }60}61 62 63int main(int, char**)64{65// 7290000ms is 2 hours, 1 minute, and 30 seconds66 test(std::chrono::milliseconds( 7290000), std::chrono::hours( 3));67 test(std::chrono::milliseconds(-7290000), std::chrono::hours(-2));68 test(std::chrono::milliseconds( 7290000), std::chrono::minutes( 122));69 test(std::chrono::milliseconds(-7290000), std::chrono::minutes(-121));70 71// 9000000ms is 2 hours and 30 minutes72 test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::hours, 3> ();73 test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::hours, -2> ();74 test_constexpr<std::chrono::milliseconds, 9000001, std::chrono::minutes, 151> ();75 test_constexpr<std::chrono::milliseconds,-9000001, std::chrono::minutes,-150> ();76 77 test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::seconds, 9000> ();78 test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::seconds,-9000> ();79 80 return 0;81}82