]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
453def3e | 2 | // { dg-require-cstdint "" } |
8e79468d BK |
3 | // |
4 | // 2008-11-24 Edward M. Smith-Rowland <3dw4rd@verizon.net> | |
5 | // | |
7adcbafe | 6 | // Copyright (C) 2008-2022 Free Software Foundation, Inc. |
8e79468d BK |
7 | // |
8 | // This file is part of the GNU ISO C++ Library. This library is free | |
9 | // software; you can redistribute it and/or modify it under the | |
10 | // terms of the GNU General Public License as published by the | |
748086b7 | 11 | // Free Software Foundation; either version 3, or (at your option) |
8e79468d BK |
12 | // any later version. |
13 | // | |
14 | // This library is distributed in the hope that it will be useful, | |
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | // GNU General Public License for more details. | |
18 | // | |
19 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
20 | // with this library; see the file COPYING3. If not see |
21 | // <http://www.gnu.org/licenses/>. | |
8e79468d | 22 | |
ea16f6ac | 23 | // C++11 26.5.6 class random_device [rand.device] |
8e79468d BK |
24 | |
25 | #include <random> | |
ea16f6ac | 26 | #include <stdexcept> |
95e8fcd3 | 27 | #include <cstdio> |
8e79468d | 28 | #include <testsuite_hooks.h> |
5f070ba2 | 29 | #include <testsuite_random.h> |
8e79468d BK |
30 | |
31 | void | |
32 | test01() | |
33 | { | |
ea16f6ac | 34 | std::random_device x("default"); |
aeedf077 JW |
35 | using result_type = std::random_device::result_type; |
36 | VERIFY( x.min() == std::numeric_limits<result_type>::min() ); | |
37 | VERIFY( x.max() == std::numeric_limits<result_type>::max() ); | |
ea16f6ac JW |
38 | } |
39 | ||
40 | void | |
41 | test02() | |
42 | { | |
43 | #ifdef _GLIBCXX_USE_DEV_RANDOM | |
44 | std::random_device x1("/dev/urandom"); | |
45 | std::random_device x2("/dev/random"); | |
8d2d0a6c | 46 | VERIFY( x1() != x2() || x1() != x2() ); |
ea16f6ac JW |
47 | #endif |
48 | } | |
49 | ||
50 | void | |
51 | test03() | |
52 | { | |
53 | // At least one of these tokens should be valid. | |
54 | const std::string tokens[] = { | |
5997e6a6 JW |
55 | "rdseed", "rdrand", "darn", |
56 | "rand_s", "/dev/urandom", "/dev/random", | |
3439657b | 57 | "getentropy", "arc4random", |
5997e6a6 | 58 | "mt19937", "prng" |
ea16f6ac JW |
59 | }; |
60 | int count = 0; | |
61 | for (const std::string& token : tokens) | |
62 | { | |
95e8fcd3 | 63 | std::printf("checking std::random_device(\"%s\"):\t", token.c_str()); |
5f070ba2 | 64 | if (__gnu_test::random_device_available(token)) |
95e8fcd3 JW |
65 | { |
66 | std::puts("yes"); | |
ea16f6ac | 67 | ++count; |
95e8fcd3 JW |
68 | } |
69 | else | |
70 | std::puts("no"); | |
ea16f6ac JW |
71 | } |
72 | VERIFY( count != 0 ); | |
73 | } | |
74 | ||
75 | void | |
76 | test04() | |
77 | { | |
5f070ba2 | 78 | if (__gnu_test::random_device_available("mt19937")) |
ea16f6ac JW |
79 | { |
80 | std::random_device x("mt19937"); | |
5f070ba2 | 81 | std::random_device::result_type xval = x(); |
ea16f6ac | 82 | |
5f070ba2 | 83 | // If "mt19937" is a valid token then numeric seeds should be too. |
ea16f6ac JW |
84 | std::random_device x1("0"); |
85 | std::random_device x2("1234"); | |
86 | std::random_device x3("0xc0fefe"); | |
aeedf077 JW |
87 | VERIFY( xval != x1() ); |
88 | VERIFY( x2() != x3() ); | |
ea16f6ac | 89 | } |
8e79468d BK |
90 | } |
91 | ||
92 | int main() | |
93 | { | |
94 | test01(); | |
ea16f6ac JW |
95 | test02(); |
96 | test03(); | |
97 | test04(); | |
8e79468d | 98 | } |