| 1 |
// { dg-do run { target c++14 } } |
| 2 |
// { dg-require-cstdint "" } |
| 3 |
// { dg-require-effective-target random_device } |
| 4 |
// { dg-require-effective-target tls_runtime } |
| 5 |
// { dg-add-options tls } |
| 6 |
|
| 7 |
// Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 8 |
// |
| 9 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 10 |
// software; you can redistribute it and/or modify it under the |
| 11 |
// terms of the GNU General Public License as published by the |
| 12 |
// Free Software Foundation; either version 3, or (at your option) |
| 13 |
// any later version. |
| 14 |
|
| 15 |
// This library is distributed in the hope that it will be useful, |
| 16 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
// GNU General Public License for more details. |
| 19 |
|
| 20 |
// You should have received a copy of the GNU General Public License along |
| 21 |
// with this library; see the file COPYING3. If not see |
| 22 |
// <http://www.gnu.org/licenses/>. |
| 23 |
|
| 24 |
#include <experimental/random> |
| 25 |
#include <testsuite_hooks.h> |
| 26 |
|
| 27 |
void |
| 28 |
test01() |
| 29 |
{ |
| 30 |
for (int i = 0; i < 100; ++i) |
| 31 |
{ |
| 32 |
const int n = std::experimental::randint(-10, i); |
| 33 |
VERIFY( -10 <= n && n <= i ); |
| 34 |
} |
| 35 |
|
| 36 |
std::experimental::reseed(99u); |
| 37 |
const int n1[] = { |
| 38 |
std::experimental::randint(0, 100), |
| 39 |
std::experimental::randint(0, 100), |
| 40 |
std::experimental::randint(0, 100), |
| 41 |
std::experimental::randint(0, 100), |
| 42 |
std::experimental::randint(0, 100) |
| 43 |
}; |
| 44 |
std::experimental::reseed(99u); |
| 45 |
const int n2[] = { |
| 46 |
std::experimental::randint(0, 100), |
| 47 |
std::experimental::randint(0, 100), |
| 48 |
std::experimental::randint(0, 100), |
| 49 |
std::experimental::randint(0, 100), |
| 50 |
std::experimental::randint(0, 100) |
| 51 |
}; |
| 52 |
for (int i = 0; i < 5; ++i) |
| 53 |
VERIFY( n1[i] == n2[i] ); |
| 54 |
|
| 55 |
do |
| 56 |
{ |
| 57 |
std::experimental::reseed(); |
| 58 |
} |
| 59 |
while (std::experimental::randint(0, 100) == n1[0] |
| 60 |
&& std::experimental::randint(0, 100) == n1[1] |
| 61 |
&& std::experimental::randint(0, 100) == n1[2]); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
test02() |
| 66 |
{ |
| 67 |
auto check = [](auto v) { |
| 68 |
auto n = std::experimental::randint(decltype(v)(0), v); |
| 69 |
static_assert(std::is_same<decltype(n), decltype(v)>::value, |
| 70 |
"return type is correct"); |
| 71 |
VERIFY(0 <= n && n <= v); |
| 72 |
}; |
| 73 |
check( (short)10 ); |
| 74 |
check( 100 ); |
| 75 |
check( 1000L ); |
| 76 |
check( 10000LL ); |
| 77 |
check( (unsigned short)10 ); |
| 78 |
check( 100U ); |
| 79 |
check( 1000UL ); |
| 80 |
check( 10000ULL ); |
| 81 |
} |
| 82 |
|
| 83 |
int main() |
| 84 |
{ |
| 85 |
test01(); |
| 86 |
test02(); |
| 87 |
} |