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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 // <chrono>
11 
12 // ceil
13 
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 
24 template <class FromDuration, class ToDuration>
25 void
test(const FromDuration & df,const ToDuration & d)26 test(const FromDuration& df, const ToDuration& d)
27 {
28     typedef std::chrono::system_clock Clock;
29     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
30     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
31     {
32     FromTimePoint f(df);
33     ToTimePoint t(d);
34     typedef decltype(std::chrono::ceil<ToDuration>(f)) R;
35     static_assert((std::is_same<R, ToTimePoint>::value), "");
36     assert(std::chrono::ceil<ToDuration>(f) == t);
37     }
38 }
39 
40 template<class FromDuration, long long From, class ToDuration, long long To>
test_constexpr()41 void test_constexpr ()
42 {
43     typedef std::chrono::system_clock Clock;
44     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
45     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
46     {
47     constexpr FromTimePoint f{FromDuration{From}};
48     constexpr ToTimePoint   t{ToDuration{To}};
49     static_assert(std::chrono::ceil<ToDuration>(f) == t, "");
50     }
51 }
52 
53 
main(int,char **)54 int main(int, char**)
55 {
56 //  7290000ms is 2 hours, 1 minute, and 30 seconds
57     test(std::chrono::milliseconds( 7290000), std::chrono::hours( 3));
58     test(std::chrono::milliseconds(-7290000), std::chrono::hours(-2));
59     test(std::chrono::milliseconds( 7290000), std::chrono::minutes( 122));
60     test(std::chrono::milliseconds(-7290000), std::chrono::minutes(-121));
61 
62 //  9000000ms is 2 hours and 30 minutes
63     test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::hours,    3> ();
64     test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::hours,   -2> ();
65     test_constexpr<std::chrono::milliseconds, 9000001, std::chrono::minutes, 151> ();
66     test_constexpr<std::chrono::milliseconds,-9000001, std::chrono::minutes,-150> ();
67 
68     test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::seconds, 9000> ();
69     test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::seconds,-9000> ();
70 
71   return 0;
72 }
73