| 1 |
// Copyright (C) 2013-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 terms |
| 5 |
// of the GNU General Public License as published by the Free Software |
| 6 |
// Foundation; either version 3, or (at your option) any later |
| 7 |
// version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, but |
| 10 |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
// 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 |
#ifndef _GLIBCXX_TESTSUITE_CONTAINER_GEN_H |
| 19 |
#define _GLIBCXX_TESTSUITE_CONTAINER_GEN_H |
| 20 |
|
| 21 |
#include <testsuite_container_traits.h> |
| 22 |
#include <random> |
| 23 |
#include <cstdlib> // getenv, atoi |
| 24 |
#include <cstdio> // printf, fflush |
| 25 |
|
| 26 |
namespace __gnu_test |
| 27 |
{ |
| 28 |
template<typename ContainerType, typename Tester, typename RandomGen> |
| 29 |
void |
| 30 |
test_single_container(Tester test, RandomGen& rg, int length, int domain) |
| 31 |
{ |
| 32 |
std::vector<int> values; |
| 33 |
auto dist = std::uniform_int_distribution<>(0, domain - 1); |
| 34 |
|
| 35 |
for(int i = 0; i < length; ++i) |
| 36 |
values.push_back(dist(rg)); |
| 37 |
|
| 38 |
ContainerType con(values.data(), values.data() + length); |
| 39 |
test(con, rg); |
| 40 |
} |
| 41 |
|
| 42 |
template<typename ContainerType, typename Tester, typename RandomGen> |
| 43 |
void |
| 44 |
test_special_containers(Tester test, RandomGen& rg, int length) |
| 45 |
{ |
| 46 |
std::vector<int> values(length); |
| 47 |
ContainerType con(values.data(), values.data() + length); |
| 48 |
|
| 49 |
for(int i = 0; i < length; ++i) |
| 50 |
values[i] = 0; |
| 51 |
test(con, rg); |
| 52 |
|
| 53 |
for(int i = 0; i < length; ++i) |
| 54 |
values[i] = i; |
| 55 |
test(con, rg); |
| 56 |
|
| 57 |
for(int i = 0; i < length; ++i) |
| 58 |
values[i] = -i; |
| 59 |
test(con, rg); |
| 60 |
} |
| 61 |
|
| 62 |
template<typename ContainerType, typename Tester> |
| 63 |
void |
| 64 |
test_containers(Tester test) |
| 65 |
{ |
| 66 |
std::mt19937_64 random_gen; |
| 67 |
|
| 68 |
if (const char* v = std::getenv("GLIBCXX_SEED_TEST_RNG")) |
| 69 |
{ |
| 70 |
// A single seed value is much smaller than the mt19937 state size, |
| 71 |
// but we're not trying to be cryptographically secure here. |
| 72 |
int s = std::atoi(v); |
| 73 |
if (s == 0) |
| 74 |
s = (int)std::random_device{}(); |
| 75 |
std::printf("Using random seed %d\n", s); |
| 76 |
std::fflush(stdout); |
| 77 |
random_gen.seed((unsigned)s); |
| 78 |
} |
| 79 |
|
| 80 |
#ifdef SIMULATOR_TEST |
| 81 |
int loops = 10; |
| 82 |
#else |
| 83 |
int loops = 1000; |
| 84 |
#endif |
| 85 |
|
| 86 |
for(int i = 0; i < loops; ++i) |
| 87 |
test_special_containers<ContainerType>(test, random_gen, i); |
| 88 |
|
| 89 |
for(int i = 1; i < 100; ++i) |
| 90 |
for(int j = 0; j < loops; ++j) |
| 91 |
test_single_container<ContainerType>(test, random_gen, i, i); |
| 92 |
|
| 93 |
for(int i = 0; i < loops; ++i) |
| 94 |
{ |
| 95 |
test_single_container<ContainerType>(test, random_gen, 10, 10); |
| 96 |
test_single_container<ContainerType>(test, random_gen, 100, 10); |
| 97 |
test_single_container<ContainerType>(test, random_gen, 1000, 10); |
| 98 |
test_single_container<ContainerType>(test, random_gen, 10, 1000); |
| 99 |
} |
| 100 |
|
| 101 |
#ifndef SIMULATOR_TEST |
| 102 |
for(int i = 0; i < 1000; ++i) |
| 103 |
{ |
| 104 |
test_single_container<ContainerType>(test, random_gen, 10000, 10); |
| 105 |
test_single_container<ContainerType>(test, random_gen, 10000, 10000); |
| 106 |
} |
| 107 |
#endif |
| 108 |
} |
| 109 |
} // namespace __gnu_test |
| 110 |
|
| 111 |
#endif |