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 | |
11 | // <chrono> |
12 | |
13 | // abs |
14 | |
15 | // template <class Rep, class Period> |
16 | // constexpr duration<Rep, Period> abs(duration<Rep, Period> d) |
17 | |
18 | #include <chrono> |
19 | #include <cassert> |
20 | #include <ratio> |
21 | #include <type_traits> |
22 | |
23 | #include "test_macros.h" |
24 | |
25 | template <class Duration> |
26 | void |
27 | test(const Duration& f, const Duration& d) |
28 | { |
29 | { |
30 | typedef decltype(std::chrono::abs(f)) R; |
31 | static_assert((std::is_same<R, Duration>::value), "" ); |
32 | assert(std::chrono::abs(f) == d); |
33 | } |
34 | } |
35 | |
36 | int main(int, char**) |
37 | { |
38 | // 7290000ms is 2 hours, 1 minute, and 30 seconds |
39 | test(f: std::chrono::milliseconds( 7290000), d: std::chrono::milliseconds( 7290000)); |
40 | test(f: std::chrono::milliseconds(-7290000), d: std::chrono::milliseconds( 7290000)); |
41 | test(f: std::chrono::minutes( 122), d: std::chrono::minutes( 122)); |
42 | test(f: std::chrono::minutes(-122), d: std::chrono::minutes( 122)); |
43 | test(f: std::chrono::hours(0), d: std::chrono::hours(0)); |
44 | |
45 | { |
46 | // 9000000ms is 2 hours and 30 minutes |
47 | constexpr std::chrono::hours h1 = std::chrono::abs(d: std::chrono::hours(-3)); |
48 | static_assert(h1.count() == 3, "" ); |
49 | constexpr std::chrono::hours h2 = std::chrono::abs(d: std::chrono::hours(3)); |
50 | static_assert(h2.count() == 3, "" ); |
51 | } |
52 | |
53 | { |
54 | // Make sure it works for durations that are not LCD'ed - example from LWG3091 |
55 | constexpr auto d = std::chrono::abs(d: std::chrono::duration<int, std::ratio<60, 100>>{2}); |
56 | static_assert(d.count() == 2, "" ); |
57 | } |
58 | |
59 | return 0; |
60 | } |
61 | |