| 1 | // Copyright (C) 2020-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++20 } } |
| 19 | |
| 20 | #include <chrono> |
| 21 | #include <slow_clock.h> |
| 22 | |
| 23 | namespace chrono = std::chrono; |
| 24 | |
| 25 | static_assert( chrono::is_clock<chrono::system_clock>::value ); |
| 26 | static_assert( chrono::is_clock_v<chrono::system_clock> ); |
| 27 | |
| 28 | static_assert( chrono::is_clock<chrono::high_resolution_clock>::value ); |
| 29 | static_assert( chrono::is_clock_v<chrono::high_resolution_clock> ); |
| 30 | |
| 31 | static_assert( chrono::is_clock<chrono::steady_clock>::value ); |
| 32 | static_assert( chrono::is_clock_v<chrono::steady_clock> ); |
| 33 | |
| 34 | static_assert(chrono::is_clock<chrono::file_clock>::value); |
| 35 | static_assert(chrono::is_clock_v<chrono::file_clock>); |
| 36 | |
| 37 | // Clock<xxx_clock> will not use the specialization of is_clock<xxx_clock> |
| 38 | template<typename C> struct Clock : C { }; |
| 39 | |
| 40 | static_assert( chrono::is_clock<Clock<chrono::system_clock>>::value ); |
| 41 | static_assert( chrono::is_clock_v<Clock<chrono::system_clock>> ); |
| 42 | |
| 43 | static_assert( chrono::is_clock<Clock<chrono::high_resolution_clock>>::value ); |
| 44 | static_assert( chrono::is_clock_v<Clock<chrono::high_resolution_clock>> ); |
| 45 | |
| 46 | static_assert( chrono::is_clock<Clock<chrono::steady_clock>>::value ); |
| 47 | static_assert( chrono::is_clock_v<Clock<chrono::steady_clock>> ); |
| 48 | |
| 49 | static_assert(chrono::is_clock<Clock<chrono::file_clock>>::value); |
| 50 | static_assert(chrono::is_clock_v<Clock<chrono::file_clock>>); |
| 51 | |
| 52 | static_assert( chrono::is_clock<__gnu_test::slow_clock>::value ); |
| 53 | static_assert( chrono::is_clock_v<__gnu_test::slow_clock> ); |
| 54 | |
| 55 | // Negative tests |
| 56 | |
| 57 | static_assert( ! chrono::is_clock<int>::value ); |
| 58 | static_assert( ! chrono::is_clock_v<int> ); |
| 59 | |
| 60 | static_assert( ! chrono::is_clock<void>::value ); |
| 61 | static_assert( ! chrono::is_clock_v<void> ); |
| 62 | |
| 63 | struct not_a_clock_1 |
| 64 | { |
| 65 | using rep = int; |
| 66 | using period = std::ratio<4, 2>; |
| 67 | using duration = chrono::duration<long, period>; // different rep |
| 68 | using time_point = chrono::time_point<not_a_clock_1>; |
| 69 | static constexpr bool is_steady = false; |
| 70 | static time_point now(); |
| 71 | }; |
| 72 | |
| 73 | static_assert( ! chrono::is_clock<not_a_clock_1>::value ); |
| 74 | static_assert( ! chrono::is_clock_v<not_a_clock_1> ); |
| 75 | |
| 76 | struct not_a_clock_2 |
| 77 | { |
| 78 | using rep = int; |
| 79 | using period = int; // not a std::ratio |
| 80 | using duration = chrono::duration<rep>; |
| 81 | using time_point = chrono::time_point<not_a_clock_2>; |
| 82 | static constexpr bool is_steady = false; |
| 83 | static time_point now(); |
| 84 | }; |
| 85 | |
| 86 | static_assert( ! chrono::is_clock<not_a_clock_2>::value ); |
| 87 | static_assert( ! chrono::is_clock_v<not_a_clock_2> ); |
| 88 | |
| 89 | struct not_a_clock_3 |
| 90 | { |
| 91 | using rep = int; |
| 92 | using period = std::ratio<1>; |
| 93 | using duration = chrono::duration<rep>; |
| 94 | // wrong duration: |
| 95 | using time_point = chrono::time_point<not_a_clock_3, chrono::duration<long>>; |
| 96 | static constexpr bool is_steady = false; |
| 97 | static time_point now(); |
| 98 | }; |
| 99 | |
| 100 | static_assert( ! chrono::is_clock<not_a_clock_3>::value ); |
| 101 | static_assert( ! chrono::is_clock_v<not_a_clock_3> ); |
| 102 | |
| 103 | struct not_a_clock_4 |
| 104 | { |
| 105 | using rep = int; |
| 106 | using period = std::ratio<1>; |
| 107 | using duration = chrono::duration<rep>; |
| 108 | using time_point = chrono::time_point<not_a_clock_4>; |
| 109 | static constexpr int is_steady = 0; // not a const bool |
| 110 | static time_point now(); |
| 111 | }; |
| 112 | |
| 113 | static_assert( ! chrono::is_clock<not_a_clock_4>::value ); |
| 114 | static_assert( ! chrono::is_clock_v<not_a_clock_4> ); |
| 115 | |
| 116 | struct not_a_clock_5 |
| 117 | { |
| 118 | using rep = int; |
| 119 | using period = std::ratio<1>; |
| 120 | using duration = chrono::duration<rep>; |
| 121 | using time_point = chrono::time_point<not_a_clock_5>; |
| 122 | static constexpr bool is_steady = false; |
| 123 | static int now(); // wrong return type |
| 124 | }; |
| 125 | |
| 126 | static_assert( ! chrono::is_clock<not_a_clock_5>::value ); |
| 127 | static_assert( ! chrono::is_clock_v<not_a_clock_5> ); |
| 128 | |
| 129 | struct not_a_clock_6 |
| 130 | { |
| 131 | using rep = int; |
| 132 | using period = std::ratio<1>; |
| 133 | using duration = chrono::duration<rep>; |
| 134 | using time_point = chrono::time_point<not_a_clock_6>; |
| 135 | const bool is_steady = false; // not static |
| 136 | static time_point now(); |
| 137 | }; |
| 138 | |
| 139 | static_assert( ! chrono::is_clock<not_a_clock_6>::value ); |
| 140 | static_assert( ! chrono::is_clock_v<not_a_clock_6> ); |