|
| 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 | +// REQUIRES: std-at-least-c++20 |
| 10 | + |
| 11 | +#include <chrono> |
| 12 | +#include <ratio> |
| 13 | + |
| 14 | +#include "test_macros.h" |
| 15 | + |
| 16 | +struct EmptyStruct {}; |
| 17 | + |
| 18 | +// Test structs missing required members |
| 19 | +struct MissingRep { |
| 20 | + using period = std::ratio<1>; |
| 21 | + using duration = std::chrono::seconds; |
| 22 | + using time_point = std::chrono::time_point<MissingRep>; |
| 23 | + static constexpr bool is_steady = false; |
| 24 | + static time_point now(); |
| 25 | +}; |
| 26 | + |
| 27 | +struct MissingPeriod { |
| 28 | + using rep = long; |
| 29 | + using duration = std::chrono::seconds; |
| 30 | + using time_point = std::chrono::time_point<MissingPeriod>; |
| 31 | + static constexpr bool is_steady = false; |
| 32 | + static time_point now(); |
| 33 | +}; |
| 34 | + |
| 35 | +struct MissingDuration { |
| 36 | + using rep = long; |
| 37 | + using time_point = long; |
| 38 | + static constexpr bool is_steady = false; |
| 39 | + static time_point now(); |
| 40 | +}; |
| 41 | + |
| 42 | +struct MissingTimePoint { |
| 43 | + using rep = long; |
| 44 | + using period = std::ratio<1>; |
| 45 | + using duration = std::chrono::seconds; |
| 46 | + static constexpr bool is_steady = false; |
| 47 | + static std::chrono::time_point<MissingTimePoint> now(); |
| 48 | +}; |
| 49 | + |
| 50 | +struct MissingIsSteady { |
| 51 | + using rep = long; |
| 52 | + using period = std::ratio<1>; |
| 53 | + using duration = std::chrono::seconds; |
| 54 | + using time_point = std::chrono::time_point<MissingIsSteady>; |
| 55 | + static time_point now(); |
| 56 | +}; |
| 57 | + |
| 58 | +struct MissingNow { |
| 59 | + using rep = long; |
| 60 | + using period = std::ratio<1>; |
| 61 | + using duration = std::chrono::seconds; |
| 62 | + using time_point = std::chrono::time_point<MissingNow>; |
| 63 | + static constexpr bool is_steady = false; |
| 64 | +}; |
| 65 | + |
| 66 | +// Valid clock types |
| 67 | +struct ValidSteadyClock { |
| 68 | + using rep = long long; |
| 69 | + using period = std::nano; |
| 70 | + using duration = std::chrono::nanoseconds; |
| 71 | + using time_point = std::chrono::time_point<ValidSteadyClock>; |
| 72 | + static constexpr bool is_steady = true; |
| 73 | + static time_point now(); |
| 74 | +}; |
| 75 | + |
| 76 | +struct ValidSystemClock { |
| 77 | + using rep = long long; |
| 78 | + using period = std::micro; |
| 79 | + using duration = std::chrono::microseconds; |
| 80 | + using time_point = std::chrono::time_point<ValidSystemClock>; |
| 81 | + static constexpr bool is_steady = false; |
| 82 | + static time_point now(); |
| 83 | +}; |
| 84 | + |
| 85 | +// Test clocks with invalid is_steady type |
| 86 | +struct WrongIsSteadyType { |
| 87 | + using rep = long; |
| 88 | + using period = std::ratio<1>; |
| 89 | + using duration = std::chrono::seconds; |
| 90 | + using time_point = std::chrono::time_point<WrongIsSteadyType>; |
| 91 | + static bool is_steady; // Not const bool |
| 92 | + static time_point now(); |
| 93 | +}; |
| 94 | + |
| 95 | +struct WrongIsSteadyNonBool { |
| 96 | + using rep = long; |
| 97 | + using period = std::ratio<1>; |
| 98 | + using duration = std::chrono::seconds; |
| 99 | + using time_point = std::chrono::time_point<WrongIsSteadyNonBool>; |
| 100 | + static constexpr int is_steady = 1; // Not bool |
| 101 | + static time_point now(); |
| 102 | +}; |
| 103 | + |
| 104 | +// Test clocks with invalid now() return type |
| 105 | +struct WrongNowReturnType { |
| 106 | + using rep = long; |
| 107 | + using period = std::ratio<1>; |
| 108 | + using duration = std::chrono::seconds; |
| 109 | + using time_point = std::chrono::time_point<WrongNowReturnType>; |
| 110 | + static constexpr bool is_steady = false; |
| 111 | + static int now(); // Wrong return type |
| 112 | +}; |
| 113 | + |
| 114 | +// Test clocks with invalid period type |
| 115 | +struct WrongPeriodType { |
| 116 | + using rep = long; |
| 117 | + using period = int; // Not a ratio |
| 118 | + using duration = std::chrono::seconds; |
| 119 | + using time_point = std::chrono::time_point<WrongPeriodType>; |
| 120 | + static constexpr bool is_steady = false; |
| 121 | + static time_point now(); |
| 122 | +}; |
| 123 | + |
| 124 | +// Test clocks with wrong duration type |
| 125 | +struct WrongDurationType { |
| 126 | + using rep = long; |
| 127 | + using period = std::ratio<1>; |
| 128 | + using duration = std::chrono::milliseconds; // Should be duration<long, ratio<1>> |
| 129 | + using time_point = std::chrono::time_point<WrongDurationType>; |
| 130 | + static constexpr bool is_steady = false; |
| 131 | + static time_point now(); |
| 132 | +}; |
| 133 | + |
| 134 | +// Test clocks with wrong time_point type |
| 135 | +struct WrongTimePointType { |
| 136 | + using rep = long; |
| 137 | + using period = std::ratio<1>; |
| 138 | + using duration = std::chrono::duration<long, std::ratio<1>>; |
| 139 | + using time_point = int; // Not a time_point |
| 140 | + static constexpr bool is_steady = false; |
| 141 | + static time_point now(); |
| 142 | +}; |
| 143 | + |
| 144 | +struct WrongTimePointClock { |
| 145 | + using rep = long; |
| 146 | + using period = std::ratio<1>; |
| 147 | + using duration = std::chrono::duration<long, std::ratio<1>>; |
| 148 | + using time_point = std::chrono::time_point<ValidSystemClock>; // Wrong clock type |
| 149 | + static constexpr bool is_steady = false; |
| 150 | + static time_point now(); |
| 151 | +}; |
| 152 | + |
| 153 | +// Valid clock with time_point that has matching duration instead of matching clock |
| 154 | +struct ValidClockWithDurationMatch { |
| 155 | + using rep = int; |
| 156 | + using period = std::milli; |
| 157 | + using duration = std::chrono::duration<int, std::milli>; |
| 158 | + using time_point = std::chrono::time_point<ValidSystemClock, duration>; // Valid: matches duration |
| 159 | + static constexpr bool is_steady = false; |
| 160 | + static time_point now(); |
| 161 | +}; |
| 162 | + |
| 163 | +// Test both is_clock and is_clock_v |
| 164 | +static_assert(std::chrono::is_clock<std::chrono::system_clock>::value); |
| 165 | +static_assert(std::chrono::is_clock_v<std::chrono::system_clock>); |
| 166 | + |
| 167 | +// Test standard clock types |
| 168 | +static_assert(std::chrono::is_clock_v<std::chrono::system_clock>); |
| 169 | +static_assert(std::chrono::is_clock_v<std::chrono::high_resolution_clock>); |
| 170 | + |
| 171 | +// Test non-clock types |
| 172 | +static_assert(!std::chrono::is_clock_v<EmptyStruct>); |
| 173 | +static_assert(!std::chrono::is_clock_v<int>); |
| 174 | +static_assert(!std::chrono::is_clock_v<void>); |
| 175 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock::time_point>); |
| 176 | +static_assert(!std::chrono::is_clock_v<std::chrono::seconds>); |
| 177 | +static_assert(!std::chrono::is_clock_v<std::chrono::milliseconds>); |
| 178 | + |
| 179 | +// Test structs missing required members |
| 180 | +static_assert(!std::chrono::is_clock_v<MissingRep>); |
| 181 | +static_assert(!std::chrono::is_clock_v<MissingPeriod>); |
| 182 | +static_assert(!std::chrono::is_clock_v<MissingDuration>); |
| 183 | +static_assert(!std::chrono::is_clock_v<MissingTimePoint>); |
| 184 | +static_assert(!std::chrono::is_clock_v<MissingIsSteady>); |
| 185 | +static_assert(!std::chrono::is_clock_v<MissingNow>); |
| 186 | + |
| 187 | +// Test valid custom clocks |
| 188 | +static_assert(std::chrono::is_clock_v<ValidSteadyClock>); |
| 189 | +static_assert(std::chrono::is_clock_v<ValidSystemClock>); |
| 190 | +static_assert(std::chrono::is_clock_v<ValidClockWithDurationMatch>); |
| 191 | + |
| 192 | +// cv-qualified and reference types |
| 193 | +static_assert(std::chrono::is_clock_v<const std::chrono::system_clock>); |
| 194 | +static_assert(std::chrono::is_clock_v<volatile std::chrono::system_clock>); |
| 195 | +static_assert(std::chrono::is_clock_v<const volatile std::chrono::system_clock>); |
| 196 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock&>); |
| 197 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock&&>); |
| 198 | +static_assert(!std::chrono::is_clock_v<const std::chrono::system_clock&>); |
| 199 | + |
| 200 | +// array and pointer types |
| 201 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock[]>); |
| 202 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock[10]>); |
| 203 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock*>); |
| 204 | +static_assert(!std::chrono::is_clock_v<std::chrono::system_clock* const>); |
| 205 | + |
| 206 | +// The Standard defined a minimum set of checks and allowed implementation to perform stricter checks. The following |
| 207 | +// static asserts are implementation specific and a conforming standard library implementation doesn't have to produce |
| 208 | +// the same outcome. |
| 209 | + |
| 210 | +// Test clocks with invalid is_steady type |
| 211 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongIsSteadyType>); // is_steady not const bool |
| 212 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongIsSteadyNonBool>); // is_steady not bool type |
| 213 | + |
| 214 | +// Test clocks with invalid now() return type |
| 215 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongNowReturnType>); // now() doesn't return time_point |
| 216 | + |
| 217 | +// Test clocks with invalid period type |
| 218 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongPeriodType>); // period is not a ratio |
| 219 | + |
| 220 | +// Test clocks with wrong duration type |
| 221 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongDurationType>); // duration doesn't match duration<rep, period> |
| 222 | + |
| 223 | +// Test clocks with wrong time_point type |
| 224 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongTimePointType>); // time_point is not a time_point |
| 225 | +LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongTimePointClock>); // time_point has wrong clock and wrong duration |
0 commit comments