| 1 | // Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 2 | // |
| 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) |
| 7 | // any later version. |
| 8 | |
| 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. |
| 13 | |
| 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/>. |
| 17 | |
| 18 | // { dg-do compile { target c++17 } } |
| 19 | // { dg-add-options no_pch } |
| 20 | |
| 21 | #include <chrono> |
| 22 | |
| 23 | #if __cpp_lib_chrono < 201510 |
| 24 | # error "__cpp_lib_chrono < 201510" |
| 25 | #endif |
| 26 | |
| 27 | using namespace std::chrono_literals; |
| 28 | using std::chrono::seconds; |
| 29 | |
| 30 | using fp_seconds = std::chrono::duration<float>; |
| 31 | |
| 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} ); |
| 40 | |
| 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} ); |
| 49 | |
| 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 ); |
| 59 | |
| 60 | static_assert( std::chrono::abs(100ms) == 100ms ); |
| 61 | static_assert( std::chrono::abs(-100ms) == 100ms ); |
| 62 | |
| 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> ); |