| 1 |
// Copyright (C) 2019-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 |
// { dg-require-cstdint "" } |
| 20 |
|
| 21 |
#include <random> |
| 22 |
|
| 23 |
static_assert( std::uniform_random_bit_generator<std::default_random_engine> ); |
| 24 |
static_assert( std::uniform_random_bit_generator<std::minstd_rand0> ); |
| 25 |
static_assert( std::uniform_random_bit_generator<std::mt19937> ); |
| 26 |
|
| 27 |
struct G1 |
| 28 |
{ |
| 29 |
unsigned char operator()(); |
| 30 |
static constexpr unsigned char min() { return 0; } |
| 31 |
static constexpr unsigned char max() { return 10; } |
| 32 |
}; |
| 33 |
|
| 34 |
static_assert( std::uniform_random_bit_generator<G1> ); |
| 35 |
|
| 36 |
struct G2 |
| 37 |
{ |
| 38 |
unsigned operator()(); |
| 39 |
static constexpr unsigned min() { return 0; } |
| 40 |
static constexpr unsigned max() { return -1U; } |
| 41 |
}; |
| 42 |
|
| 43 |
static_assert( std::uniform_random_bit_generator<G2> ); |
| 44 |
|
| 45 |
struct G3 |
| 46 |
{ |
| 47 |
unsigned long long operator()(); |
| 48 |
static constexpr unsigned long long min() { return 0; } |
| 49 |
static constexpr unsigned long long max() { return -1ULL; } |
| 50 |
}; |
| 51 |
|
| 52 |
static_assert( std::uniform_random_bit_generator<G3> ); |
| 53 |
|
| 54 |
struct G4 |
| 55 |
{ |
| 56 |
unsigned operator()(int = 0, int = 0); // extra params, with default args |
| 57 |
static constexpr unsigned min(long = 0) { return 0; } |
| 58 |
static constexpr unsigned max(void* = nullptr) { return -1U; } |
| 59 |
}; |
| 60 |
|
| 61 |
static_assert( std::uniform_random_bit_generator<G4> ); |
| 62 |
|
| 63 |
struct G5 |
| 64 |
{ |
| 65 |
unsigned operator()() &; // ref-qualifier |
| 66 |
static constexpr unsigned min() { return 0; } |
| 67 |
static constexpr unsigned max() { return 10; } |
| 68 |
}; |
| 69 |
|
| 70 |
static_assert( std::uniform_random_bit_generator<G5> ); |
| 71 |
|
| 72 |
struct G6 |
| 73 |
{ |
| 74 |
unsigned operator()() const; // cv-qualifier |
| 75 |
static constexpr unsigned min() { return 0; } |
| 76 |
static constexpr unsigned max() { return 10; } |
| 77 |
}; |
| 78 |
|
| 79 |
static_assert( std::uniform_random_bit_generator<G6> ); |
| 80 |
|
| 81 |
struct G7 |
| 82 |
{ |
| 83 |
unsigned operator()() volatile; // cv-qualifier |
| 84 |
static constexpr unsigned min() { return 0; } |
| 85 |
static constexpr unsigned max() { return 10; } |
| 86 |
}; |
| 87 |
|
| 88 |
static_assert( std::uniform_random_bit_generator<G7> ); |
| 89 |
|
| 90 |
struct G8 |
| 91 |
{ |
| 92 |
unsigned operator()() const volatile; // cv-qualifiers |
| 93 |
static constexpr unsigned min() { return 0; } |
| 94 |
static constexpr unsigned max() { return 10; } |
| 95 |
}; |
| 96 |
|
| 97 |
static_assert( std::uniform_random_bit_generator<G8> ); |
| 98 |
|
| 99 |
struct G9 |
| 100 |
{ |
| 101 |
unsigned operator()() const volatile; // cv-qualifiers |
| 102 |
static constexpr unsigned min() { return 0; } |
| 103 |
static constexpr unsigned max() { return 10; } |
| 104 |
}; |
| 105 |
|
| 106 |
static_assert( std::uniform_random_bit_generator<G9> ); |
| 107 |
|
| 108 |
struct G10 |
| 109 |
{ |
| 110 |
unsigned operator()() const volatile & noexcept; // cv/ref/noexcept |
| 111 |
static constexpr unsigned min() noexcept { return 0; } |
| 112 |
static constexpr unsigned max() noexcept { return 10; } |
| 113 |
}; |
| 114 |
|
| 115 |
static_assert( std::uniform_random_bit_generator<G10> ); |
| 116 |
|
| 117 |
// Negative tests. |
| 118 |
|
| 119 |
static_assert( ! std::uniform_random_bit_generator<void> ); |
| 120 |
static_assert( ! std::uniform_random_bit_generator<int> ); |
| 121 |
static_assert( ! std::uniform_random_bit_generator<unsigned(*)()> ); |
| 122 |
|
| 123 |
struct N1 |
| 124 |
{ |
| 125 |
unsigned operator()(); |
| 126 |
constexpr unsigned min() { return 0; } // non-static |
| 127 |
static constexpr unsigned max() { return 1; } |
| 128 |
}; |
| 129 |
|
| 130 |
static_assert( ! std::uniform_random_bit_generator<N1> ); |
| 131 |
|
| 132 |
struct N2 |
| 133 |
{ |
| 134 |
unsigned operator()(); |
| 135 |
static constexpr unsigned min() { return 0; } |
| 136 |
constexpr unsigned max() { return 1; } // non-static |
| 137 |
}; |
| 138 |
|
| 139 |
static_assert( ! std::uniform_random_bit_generator<N2> ); |
| 140 |
|
| 141 |
struct N3 |
| 142 |
{ |
| 143 |
unsigned operator()(); |
| 144 |
// no N3::min() |
| 145 |
static constexpr unsigned max() { return 1; } |
| 146 |
}; |
| 147 |
|
| 148 |
static_assert( ! std::uniform_random_bit_generator<N3> ); |
| 149 |
|
| 150 |
struct N4 |
| 151 |
{ |
| 152 |
unsigned operator()(); |
| 153 |
static constexpr unsigned min() { return 0; } |
| 154 |
// no N4::max() |
| 155 |
}; |
| 156 |
|
| 157 |
static_assert( ! std::uniform_random_bit_generator<N4> ); |
| 158 |
|
| 159 |
struct N5 |
| 160 |
{ |
| 161 |
// no operator() |
| 162 |
static constexpr unsigned min() { return 0; } |
| 163 |
static constexpr unsigned max() { return 1; } |
| 164 |
}; |
| 165 |
|
| 166 |
static_assert( ! std::uniform_random_bit_generator<N5> ); |
| 167 |
|
| 168 |
struct N6 |
| 169 |
{ |
| 170 |
int operator()(); // returns signed integral |
| 171 |
static constexpr unsigned min() { return 0; } |
| 172 |
static constexpr unsigned max() { return 1; } |
| 173 |
}; |
| 174 |
|
| 175 |
static_assert( ! std::uniform_random_bit_generator<N6> ); |
| 176 |
|
| 177 |
struct N7 |
| 178 |
{ |
| 179 |
unsigned operator()(); |
| 180 |
static constexpr unsigned long min() { return 0; } // different return type |
| 181 |
static constexpr unsigned max() { return 1; } |
| 182 |
}; |
| 183 |
|
| 184 |
static_assert( ! std::uniform_random_bit_generator<N7> ); |
| 185 |
|
| 186 |
struct N8 |
| 187 |
{ |
| 188 |
unsigned operator()(); |
| 189 |
static constexpr unsigned min() { return 0; } |
| 190 |
static constexpr unsigned long max() { return 1; } // different return type |
| 191 |
}; |
| 192 |
|
| 193 |
static_assert( ! std::uniform_random_bit_generator<N8> ); |
| 194 |
|
| 195 |
struct N9 |
| 196 |
{ |
| 197 |
unsigned operator()(); |
| 198 |
static constexpr unsigned long min() { return 0; } // different return type |
| 199 |
static constexpr unsigned long max() { return 1; } // different return type |
| 200 |
}; |
| 201 |
|
| 202 |
static_assert( ! std::uniform_random_bit_generator<N9> ); |
| 203 |
|
| 204 |
struct N10 |
| 205 |
{ |
| 206 |
unsigned operator()() &&; // ref-qualifier |
| 207 |
static constexpr unsigned min() { return 0; } |
| 208 |
static constexpr unsigned max() { return 1; } |
| 209 |
}; |
| 210 |
|
| 211 |
static_assert( ! std::uniform_random_bit_generator<N10> ); |
| 212 |
|
| 213 |
struct N11 |
| 214 |
{ |
| 215 |
unsigned operator()() const &&; // ref-qualifier |
| 216 |
static constexpr unsigned min() { return 0; } |
| 217 |
static constexpr unsigned max() { return 1; } |
| 218 |
}; |
| 219 |
|
| 220 |
static_assert( ! std::uniform_random_bit_generator<N11> ); |
| 221 |
|
| 222 |
struct N12 |
| 223 |
{ |
| 224 |
unsigned operator()(); |
| 225 |
static unsigned min() { return 0; } // not constexpr |
| 226 |
static constexpr unsigned max() { return 1; } |
| 227 |
}; |
| 228 |
|
| 229 |
#if __cpp_implicit_constexpr |
| 230 |
static_assert( std::uniform_random_bit_generator<N12> ); // LWG 3150 |
| 231 |
#else |
| 232 |
static_assert( ! std::uniform_random_bit_generator<N12> ); // LWG 3150 |
| 233 |
#endif |
| 234 |
|
| 235 |
struct N13 |
| 236 |
{ |
| 237 |
unsigned operator()(); |
| 238 |
static constexpr unsigned min() { return 0; } |
| 239 |
static unsigned max() { return 1; } // not constexpr |
| 240 |
}; |
| 241 |
|
| 242 |
#if __cpp_implicit_constexpr |
| 243 |
static_assert( std::uniform_random_bit_generator<N13> ); // LWG 3150 |
| 244 |
#else |
| 245 |
static_assert( ! std::uniform_random_bit_generator<N13> ); // LWG 3150 |
| 246 |
#endif |
| 247 |
|
| 248 |
struct N14 |
| 249 |
{ |
| 250 |
unsigned operator()(); |
| 251 |
static constexpr unsigned min() { return 1; } |
| 252 |
static constexpr unsigned max() { return 0; } // max not greater than min |
| 253 |
}; |
| 254 |
|
| 255 |
static_assert( ! std::uniform_random_bit_generator<N14> ); // LWG 3150 |