]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/random/randint.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / random / randint.cc
CommitLineData
52066eae 1// { dg-do run { target c++14 } }
49ba2588 2// { dg-require-cstdint "" }
89bc4ab1 3// { dg-require-effective-target random_device }
5ae465c5 4// { dg-require-effective-target tls_runtime }
bfc6afd9 5// { dg-add-options tls }
5ae465c5 6
8d9254fc 7// Copyright (C) 2015-2020 Free Software Foundation, Inc.
5ae465c5
JW
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
c7cbb4da 20// You should have received a copy of the GNU General Public License along
5ae465c5
JW
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
27void
28test01()
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 long 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 long 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 std::experimental::reseed();
56 const long n3[] = {
57 std::experimental::randint(0, 100),
58 std::experimental::randint(0, 100),
59 std::experimental::randint(0, 100)
60 };
61 VERIFY( !(n3[0] == n1[0] && n3[1] == n1[1] && n3[2] == n1[2]) );
62}
63
64void
65test02()
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
83int main()
84{
85 test01();
86 test02();
87}