1 // Copyright (C) 2013-2025 Free Software Foundation, Inc.
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
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.
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/>.
18 #ifndef _GLIBCXX_TESTSUITE_CONTAINER_GEN_H
19 #define _GLIBCXX_TESTSUITE_CONTAINER_GEN_H
21 #include <testsuite_container_traits.h>
23 #include <cstdlib> // getenv, atoi
24 #include <cstdio> // printf, fflush
28 template<typename ContainerType
, typename Tester
, typename RandomGen
>
30 test_single_container(Tester test
, RandomGen
& rg
, int length
, int domain
)
32 std::vector
<int> values
;
33 auto dist
= std::uniform_int_distribution
<>(0, domain
- 1);
35 for(int i
= 0; i
< length
; ++i
)
36 values
.push_back(dist(rg
));
38 ContainerType
con(values
.data(), values
.data() + length
);
42 template<typename ContainerType
, typename Tester
, typename RandomGen
>
44 test_special_containers(Tester test
, RandomGen
& rg
, int length
)
46 std::vector
<int> values(length
);
47 ContainerType
con(values
.data(), values
.data() + length
);
49 for(int i
= 0; i
< length
; ++i
)
53 for(int i
= 0; i
< length
; ++i
)
57 for(int i
= 0; i
< length
; ++i
)
62 template<typename ContainerType
, typename Tester
>
64 test_containers(Tester test
)
66 std::mt19937_64 random_gen
;
68 if (const char* v
= std::getenv("GLIBCXX_SEED_TEST_RNG"))
70 // A single seed value is much smaller than the mt19937 state size,
71 // but we're not trying to be cryptographically secure here.
74 s
= (int)std::random_device
{}();
75 std::printf("Using random seed %d\n", s
);
77 random_gen
.seed((unsigned)s
);
86 for(int i
= 0; i
< loops
; ++i
)
87 test_special_containers
<ContainerType
>(test
, random_gen
, i
);
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
);
93 for(int i
= 0; i
< loops
; ++i
)
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);
101 #ifndef SIMULATOR_TEST
102 for(int i
= 0; i
< 1000; ++i
)
104 test_single_container
<ContainerType
>(test
, random_gen
, 10000, 10);
105 test_single_container
<ContainerType
>(test
, random_gen
, 10000, 10000);
109 } // namespace __gnu_test