]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/duration_cast/rounding.cc
1 // Copyright (C) 2016-2024 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do compile { target c++17 } }
19 // { dg-add-options no_pch }
23 #if __cpp_lib_chrono < 201510
24 # error "__cpp_lib_chrono < 201510"
27 using namespace std::chrono_literals
;
28 using std::chrono::seconds
;
30 using fp_seconds
= std::chrono::duration
<float>;
32 static_assert( std::chrono::floor
<seconds
>(1000ms
) == 1s
);
33 static_assert( std::chrono::floor
<seconds
>(1001ms
) == 1s
);
34 static_assert( std::chrono::floor
<seconds
>(1500ms
) == 1s
);
35 static_assert( std::chrono::floor
<seconds
>(1999ms
) == 1s
);
36 static_assert( std::chrono::floor
<seconds
>(2000ms
) == 2s
);
37 static_assert( std::chrono::floor
<seconds
>(2001ms
) == 2s
);
38 static_assert( std::chrono::floor
<seconds
>(2500ms
) == 2s
);
39 static_assert( std::chrono::floor
<fp_seconds
>(500ms
) == fp_seconds
{0.5f
} );
41 static_assert( std::chrono::ceil
<seconds
>(1000ms
) == 1s
);
42 static_assert( std::chrono::ceil
<seconds
>(1001ms
) == 2s
);
43 static_assert( std::chrono::ceil
<seconds
>(1500ms
) == 2s
);
44 static_assert( std::chrono::ceil
<seconds
>(1999ms
) == 2s
);
45 static_assert( std::chrono::ceil
<seconds
>(2000ms
) == 2s
);
46 static_assert( std::chrono::ceil
<seconds
>(2001ms
) == 3s
);
47 static_assert( std::chrono::ceil
<seconds
>(2500ms
) == 3s
);
48 static_assert( std::chrono::ceil
<fp_seconds
>(500ms
) == fp_seconds
{0.5f
} );
50 static_assert( std::chrono::round
<seconds
>(1000ms
) == 1s
);
51 static_assert( std::chrono::round
<seconds
>(1001ms
) == 1s
);
52 static_assert( std::chrono::round
<seconds
>(1499ms
) == 1s
);
53 static_assert( std::chrono::round
<seconds
>(1500ms
) == 2s
);
54 static_assert( std::chrono::round
<seconds
>(1999ms
) == 2s
);
55 static_assert( std::chrono::round
<seconds
>(2000ms
) == 2s
);
56 static_assert( std::chrono::round
<seconds
>(2001ms
) == 2s
);
57 static_assert( std::chrono::round
<seconds
>(2500ms
) == 2s
);
58 static_assert( std::chrono::round
<seconds
>(2501ms
) == 3s
);
60 static_assert( std::chrono::abs(100ms
) == 100ms
);
61 static_assert( std::chrono::abs(-100ms
) == 100ms
);
63 // LWG 3741. std::chrono::abs(duration) is ill-formed with non-reduced periods
64 using D1000
= std::chrono::duration
<int, std::ratio
<1000, 1000>>;
65 static_assert( std::chrono::abs(D1000(-2)) == D1000(2) );
66 static_assert( std::is_same_v
<decltype(std::chrono::abs(D1000(-2))), D1000
> );