gcc/libstdc++-v3/testsuite/util/testsuite_containergen.h
Jonathan Wakely c7fc73ee45 libstdc++: Allow seeding random engines in testsuite
The testsuite utilities that use random numbers use a
default-constructed mersenne_twister_engine, meaning the values are
reproducable. This adds support for seeding them, controlledby an
environment variable. Defining GLIBCXX_SEED_TEST_RNG=val in the
environment will cause the engines to be seeded with atoi(val) if that
is non-zero, or with a value read from std::random_device otherwise.

Running with different seeds revealed some bugs in the tests, where a
randomly selected iterator was past-the-end (which can't be erased), or
where the randomly populated container was empty, and then we tried to
remove elements from it unconditionally.

libstdc++-v3/ChangeLog:

	* testsuite/util/exception/safety.h (setup_base::generate):
	Support seeding random engine.
	(erase_point, erase_range): Adjust range of random numbers to
	ensure dereferenceable iterators are used where required.
	(generation_prohibited::run): Do not try to erase from empty
	containers.
	* testsuite/util/testsuite_containergen.h (test_containers):
	Support seeding random engine.
2021-03-25 18:21:19 +00:00

112 lines
3.3 KiB
C++

// Copyright (C) 2013-2021 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 3, or (at your option) any later
// version.
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef _GLIBCXX_TESTSUITE_CONTAINER_GEN_H
#define _GLIBCXX_TESTSUITE_CONTAINER_GEN_H
#include <testsuite_container_traits.h>
#include <random>
#include <cstdlib> // getenv, atoi
#include <cstdio> // printf, fflush
namespace __gnu_test
{
template<typename ContainerType, typename Tester, typename RandomGen>
void
test_single_container(Tester test, RandomGen& rg, int length, int domain)
{
std::vector<int> values;
auto dist = std::uniform_int_distribution<>(0, domain - 1);
for(int i = 0; i < length; ++i)
values.push_back(dist(rg));
ContainerType con(values.data(), values.data() + length);
test(con, rg);
}
template<typename ContainerType, typename Tester, typename RandomGen>
void
test_special_containers(Tester test, RandomGen& rg, int length)
{
std::vector<int> values(length);
ContainerType con(values.data(), values.data() + length);
for(int i = 0; i < length; ++i)
values[i] = 0;
test(con, rg);
for(int i = 0; i < length; ++i)
values[i] = i;
test(con, rg);
for(int i = 0; i < length; ++i)
values[i] = -i;
test(con, rg);
}
template<typename ContainerType, typename Tester>
void
test_containers(Tester test)
{
std::mt19937_64 random_gen;
if (const char* v = std::getenv("GLIBCXX_SEED_TEST_RNG"))
{
// A single seed value is much smaller than the mt19937 state size,
// but we're not trying to be cryptographically secure here.
int s = std::atoi(v);
if (s == 0)
s = (int)std::random_device{}();
std::printf("Using random seed %d\n", s);
std::fflush(stdout);
random_gen.seed((unsigned)s);
}
#ifdef SIMULATOR_TEST
int loops = 10;
#else
int loops = 1000;
#endif
for(int i = 0; i < loops; ++i)
test_special_containers<ContainerType>(test, random_gen, i);
for(int i = 1; i < 100; ++i)
for(int j = 0; j < loops; ++j)
test_single_container<ContainerType>(test, random_gen, i, i);
for(int i = 0; i < loops; ++i)
{
test_single_container<ContainerType>(test, random_gen, 10, 10);
test_single_container<ContainerType>(test, random_gen, 100, 10);
test_single_container<ContainerType>(test, random_gen, 1000, 10);
test_single_container<ContainerType>(test, random_gen, 10, 1000);
}
#ifndef SIMULATOR_TEST
for(int i = 0; i < 1000; ++i)
{
test_single_container<ContainerType>(test, random_gen, 10000, 10);
test_single_container<ContainerType>(test, random_gen, 10000, 10000);
}
#endif
}
} // namespace __gnu_test
#endif