]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/random/random_device/cons/token.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / random / random_device / cons / token.cc
1 // { dg-do run { target c++11 } }
2 // { dg-require-cstdint "" }
3 //
4 // 2008-11-24 Edward M. Smith-Rowland <3dw4rd@verizon.net>
5 //
6 // Copyright (C) 2008-2024 Free Software Foundation, Inc.
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
11 // Free Software Foundation; either version 3, or (at your option)
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
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // C++11 26.5.6 class random_device [rand.device]
24
25 #include <random>
26 #include <stdexcept>
27 #include <cstdio>
28 #include <testsuite_hooks.h>
29 #include <testsuite_random.h>
30
31 void
32 test01()
33 {
34 std::random_device x("default");
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() );
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");
46 VERIFY( x1() != x2() || x1() != x2() );
47 #endif
48 }
49
50 void
51 test03()
52 {
53 // At least one of these tokens should be valid.
54 const std::string tokens[] = {
55 "rdseed", "rdrand", "darn",
56 "rand_s", "/dev/urandom", "/dev/random",
57 "getentropy", "arc4random",
58 "mt19937", "prng"
59 };
60 int count = 0;
61 for (const std::string& token : tokens)
62 {
63 std::printf("checking std::random_device(\"%s\"):\t", token.c_str());
64 if (__gnu_test::random_device_available(token))
65 {
66 std::puts("yes");
67 ++count;
68 }
69 else
70 std::puts("no");
71 }
72 VERIFY( count != 0 );
73 }
74
75 void
76 test04()
77 {
78 if (__gnu_test::random_device_available("mt19937"))
79 {
80 std::random_device x("mt19937");
81 std::random_device::result_type xval = x();
82
83 // If "mt19937" is a valid token then numeric seeds should be too.
84 std::random_device x1("0");
85 std::random_device x2("1234");
86 std::random_device x3("0xc0fefe");
87 VERIFY( xval != x1() );
88 VERIFY( x2() != x3() );
89 }
90 }
91
92 int main()
93 {
94 test01();
95 test02();
96 test03();
97 test04();
98 }