testsuite_containergen.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2013-2022 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. // This library is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. #ifndef _GLIBCXX_TESTSUITE_CONTAINER_GEN_H
  16. #define _GLIBCXX_TESTSUITE_CONTAINER_GEN_H
  17. #include <testsuite_container_traits.h>
  18. #include <random>
  19. #include <cstdlib> // getenv, atoi
  20. #include <cstdio> // printf, fflush
  21. namespace __gnu_test
  22. {
  23. template<typename ContainerType, typename Tester, typename RandomGen>
  24. void
  25. test_single_container(Tester test, RandomGen& rg, int length, int domain)
  26. {
  27. std::vector<int> values;
  28. auto dist = std::uniform_int_distribution<>(0, domain - 1);
  29. for(int i = 0; i < length; ++i)
  30. values.push_back(dist(rg));
  31. ContainerType con(values.data(), values.data() + length);
  32. test(con, rg);
  33. }
  34. template<typename ContainerType, typename Tester, typename RandomGen>
  35. void
  36. test_special_containers(Tester test, RandomGen& rg, int length)
  37. {
  38. std::vector<int> values(length);
  39. ContainerType con(values.data(), values.data() + length);
  40. for(int i = 0; i < length; ++i)
  41. values[i] = 0;
  42. test(con, rg);
  43. for(int i = 0; i < length; ++i)
  44. values[i] = i;
  45. test(con, rg);
  46. for(int i = 0; i < length; ++i)
  47. values[i] = -i;
  48. test(con, rg);
  49. }
  50. template<typename ContainerType, typename Tester>
  51. void
  52. test_containers(Tester test)
  53. {
  54. std::mt19937_64 random_gen;
  55. if (const char* v = std::getenv("GLIBCXX_SEED_TEST_RNG"))
  56. {
  57. // A single seed value is much smaller than the mt19937 state size,
  58. // but we're not trying to be cryptographically secure here.
  59. int s = std::atoi(v);
  60. if (s == 0)
  61. s = (int)std::random_device{}();
  62. std::printf("Using random seed %d\n", s);
  63. std::fflush(stdout);
  64. random_gen.seed((unsigned)s);
  65. }
  66. #ifdef SIMULATOR_TEST
  67. int loops = 10;
  68. #else
  69. int loops = 1000;
  70. #endif
  71. for(int i = 0; i < loops; ++i)
  72. test_special_containers<ContainerType>(test, random_gen, i);
  73. for(int i = 1; i < 100; ++i)
  74. for(int j = 0; j < loops; ++j)
  75. test_single_container<ContainerType>(test, random_gen, i, i);
  76. for(int i = 0; i < loops; ++i)
  77. {
  78. test_single_container<ContainerType>(test, random_gen, 10, 10);
  79. test_single_container<ContainerType>(test, random_gen, 100, 10);
  80. test_single_container<ContainerType>(test, random_gen, 1000, 10);
  81. test_single_container<ContainerType>(test, random_gen, 10, 1000);
  82. }
  83. #ifndef SIMULATOR_TEST
  84. for(int i = 0; i < 1000; ++i)
  85. {
  86. test_single_container<ContainerType>(test, random_gen, 10000, 10);
  87. test_single_container<ContainerType>(test, random_gen, 10000, 10000);
  88. }
  89. #endif
  90. }
  91. } // namespace __gnu_test
  92. #endif