|
| 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 | +struct EmptyStruct {}; |
| 15 | + |
| 16 | +// Test structs missing required members |
| 17 | +struct MissingRep { |
| 18 | + using period = std::ratio<1>; |
| 19 | + using duration = std::chrono::seconds; |
| 20 | + using time_point = std::chrono::time_point<MissingRep>; |
| 21 | + static constexpr bool is_steady = false; |
| 22 | + static time_point now(); |
| 23 | +}; |
| 24 | + |
| 25 | +struct MissingPeriod { |
| 26 | + using rep = long; |
| 27 | + using duration = std::chrono::seconds; |
| 28 | + using time_point = std::chrono::time_point<MissingPeriod>; |
| 29 | + static constexpr bool is_steady = false; |
| 30 | + static time_point now(); |
| 31 | +}; |
| 32 | + |
| 33 | +struct MissingDuration { |
| 34 | + using rep = long; |
| 35 | + using time_point = long; |
| 36 | + static constexpr bool is_steady = false; |
| 37 | + static time_point now(); |
| 38 | +}; |
| 39 | + |
| 40 | +struct MissingTimePoint { |
| 41 | + using rep = long; |
| 42 | + using period = std::ratio<1>; |
| 43 | + using duration = std::chrono::seconds; |
| 44 | + static constexpr bool is_steady = false; |
| 45 | + static std::chrono::time_point<MissingTimePoint> now(); |
| 46 | +}; |
| 47 | + |
| 48 | +struct MissingIsSteady { |
| 49 | + using rep = long; |
| 50 | + using period = std::ratio<1>; |
| 51 | + using duration = std::chrono::seconds; |
| 52 | + using time_point = std::chrono::time_point<MissingIsSteady>; |
| 53 | + static time_point now(); |
| 54 | +}; |
| 55 | + |
| 56 | +struct MissingNow { |
| 57 | + using rep = long; |
| 58 | + using period = std::ratio<1>; |
| 59 | + using duration = std::chrono::seconds; |
| 60 | + using time_point = std::chrono::time_point<MissingNow>; |
| 61 | + static constexpr bool is_steady = false; |
| 62 | +}; |
| 63 | + |
| 64 | +// Valid clock types |
| 65 | +struct ValidSteadyClock { |
| 66 | + using rep = long long; |
| 67 | + using period = std::nano; |
| 68 | + using duration = std::chrono::nanoseconds; |
| 69 | + using time_point = std::chrono::time_point<ValidSteadyClock>; |
| 70 | + static constexpr bool is_steady = true; |
| 71 | + static time_point now(); |
| 72 | +}; |
| 73 | + |
| 74 | +struct ValidSystemClock { |
| 75 | + using rep = int64_t; |
| 76 | + using period = std::micro; |
| 77 | + using duration = std::chrono::microseconds; |
| 78 | + using time_point = std::chrono::time_point<ValidSystemClock>; |
| 79 | + static constexpr bool is_steady = false; |
| 80 | + static time_point now(); |
| 81 | +}; |
| 82 | + |
| 83 | +int main(int, char**) { |
| 84 | + // Test both is_clock and is_clock_v |
| 85 | + static_assert(std::chrono::is_clock<std::chrono::system_clock>::value); |
| 86 | + static_assert(std::chrono::is_clock_v<std::chrono::system_clock>); |
| 87 | + |
| 88 | + // Test standard clock types |
| 89 | + static_assert(std::chrono::is_clock_v<std::chrono::system_clock>); |
| 90 | + static_assert(std::chrono::is_clock_v<std::chrono::steady_clock>); |
| 91 | + static_assert(std::chrono::is_clock_v<std::chrono::high_resolution_clock>); |
| 92 | + |
| 93 | + // Test non-clock types |
| 94 | + static_assert(!std::chrono::is_clock_v<EmptyStruct>); |
| 95 | + static_assert(!std::chrono::is_clock_v<int>); |
| 96 | + static_assert(!std::chrono::is_clock_v<void>); |
| 97 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock::time_point>); |
| 98 | + static_assert(!std::chrono::is_clock_v<std::chrono::steady_clock::time_point>); |
| 99 | + static_assert(!std::chrono::is_clock_v<std::chrono::seconds>); |
| 100 | + static_assert(!std::chrono::is_clock_v<std::chrono::milliseconds>); |
| 101 | + |
| 102 | + // Test structs missing required members |
| 103 | + static_assert(!std::chrono::is_clock_v<MissingRep>); |
| 104 | + static_assert(!std::chrono::is_clock_v<MissingPeriod>); |
| 105 | + static_assert(!std::chrono::is_clock_v<MissingDuration>); |
| 106 | + static_assert(!std::chrono::is_clock_v<MissingTimePoint>); |
| 107 | + static_assert(!std::chrono::is_clock_v<MissingIsSteady>); |
| 108 | + static_assert(!std::chrono::is_clock_v<MissingNow>); |
| 109 | + |
| 110 | + // Test valid custom clocks |
| 111 | + static_assert(std::chrono::is_clock_v<ValidSteadyClock>); |
| 112 | + static_assert(std::chrono::is_clock_v<ValidSystemClock>); |
| 113 | + |
| 114 | + // cv-qualified and reference types |
| 115 | + static_assert(std::chrono::is_clock_v<const std::chrono::system_clock>); |
| 116 | + static_assert(std::chrono::is_clock_v<volatile std::chrono::system_clock>); |
| 117 | + static_assert(std::chrono::is_clock_v<const volatile std::chrono::system_clock>); |
| 118 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock&>); |
| 119 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock&&>); |
| 120 | + static_assert(!std::chrono::is_clock_v<const std::chrono::system_clock&>); |
| 121 | + |
| 122 | + // array and pointer types |
| 123 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock[]>); |
| 124 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock[10]>); |
| 125 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock*>); |
| 126 | + static_assert(!std::chrono::is_clock_v<std::chrono::system_clock* const>); |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
0 commit comments