From c2240038a67c0e5887c0d9aef55a8b3497751b71 Mon Sep 17 00:00:00 2001 From: Chris Jefferson Date: Wed, 23 Oct 2013 22:19:30 +0000 Subject: [PATCH] testsuite_containergen.h: New. 2013-10-23 Chris Jefferson Paolo Carlini * testsuite/util/testsuite_containergen.h: New. * testsuite/util/testsuite_iterators.h (test_container<>::val): Add. * testsuite/25_algorithms/nth_element/random_test.cc: New, use the above. * testsuite/25_algorithms/partial_sort/random_test.cc: Likewise. * testsuite/25_algorithms/partial_sort_copy/random_test.cc: Likewise. * testsuite/25_algorithms/sort/random_test.cc: Likewise. Co-Authored-By: Paolo Carlini From-SVN: r203995 --- libstdc++-v3/ChangeLog | 11 +++ .../25_algorithms/nth_element/random_test.cc | 63 ++++++++++++ .../25_algorithms/partial_sort/random_test.cc | 62 ++++++++++++ .../partial_sort_copy/random_test.cc | 67 +++++++++++++ .../25_algorithms/sort/random_test.cc | 51 ++++++++++ .../testsuite/util/testsuite_containergen.h | 97 +++++++++++++++++++ .../testsuite/util/testsuite_iterators.h | 4 + 7 files changed, 355 insertions(+) create mode 100644 libstdc++-v3/testsuite/25_algorithms/nth_element/random_test.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/partial_sort/random_test.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/partial_sort_copy/random_test.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/sort/random_test.cc create mode 100644 libstdc++-v3/testsuite/util/testsuite_containergen.h diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0dcda6e907c..31ada067658 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2013-10-23 Chris Jefferson + Paolo Carlini + + * testsuite/util/testsuite_containergen.h: New. + * testsuite/util/testsuite_iterators.h (test_container<>::val): Add. + * testsuite/25_algorithms/nth_element/random_test.cc: New, use the + above. + * testsuite/25_algorithms/partial_sort/random_test.cc: Likewise. + * testsuite/25_algorithms/partial_sort_copy/random_test.cc: Likewise. + * testsuite/25_algorithms/sort/random_test.cc: Likewise. + 2013-10-23 Jason Merrill * include/bits/hashtable.h (_Hashtable): Force EBO for _Hash_code_base. diff --git a/libstdc++-v3/testsuite/25_algorithms/nth_element/random_test.cc b/libstdc++-v3/testsuite/25_algorithms/nth_element/random_test.cc new file mode 100644 index 00000000000..ef1d0b6c4e6 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/nth_element/random_test.cc @@ -0,0 +1,63 @@ +// Copyright (C) 2013 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 +// . + +// { dg-options "-std=gnu++11" } +// { dg-options "-std=gnu++11 -DSIMULATOR_TEST" { target simulator } } +// { dg-require-cstdint "" } + +// 25.4.2 [lib.alg.nth.element] + +#include +#include +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::random_access_iterator_wrapper; + +typedef test_container Container; + +struct testNthElement +{ + template + void operator()(Container con, RandomGen& rg) + { + bool test __attribute__((unused)) = true; + + const int size = con.end() - con.begin(); + auto dist = std::uniform_int_distribution<>(0, size); + const int element = dist(rg); + + std::nth_element(con.begin(), con.begin() + element, con.end()); + + if (element < size) + { + for (int i = 0; i < element; ++i) + VERIFY( con.val(i) <= con.val(element) ); + + for (int i = element + 1; i < size; ++i) + VERIFY( con.val(i) >= con.val(element) ); + } + } +}; + +int +main() +{ + __gnu_test::test_containers(testNthElement()); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/partial_sort/random_test.cc b/libstdc++-v3/testsuite/25_algorithms/partial_sort/random_test.cc new file mode 100644 index 00000000000..855e92121b3 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/partial_sort/random_test.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2013 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 +// . + +// { dg-options "-std=gnu++11" } +// { dg-options "-std=gnu++11 -DSIMULATOR_TEST" { target simulator } } +// { dg-require-cstdint "" } + +// 25.4.1.3 [lib.alg.partial.sort] + +#include +#include +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::random_access_iterator_wrapper; + +typedef test_container Container; + +struct testPartialSort +{ + template + void operator()(Container con, RandomGen& rg) + { + bool test __attribute__((unused)) = true; + + const int size = con.end() - con.begin(); + auto dist = std::uniform_int_distribution<>(0, size); + const int element = dist(rg); + + std::partial_sort(con.begin(), con.begin() + element, con.end()); + + VERIFY( std::is_sorted(con.begin(), con.begin() + element) ); + + if (element > 0) + { + for (int i = element; i < size; ++i) + VERIFY( con.val(element - 1) <= con.val(i) ); + } + } +}; + +int +main() +{ + __gnu_test::test_containers(testPartialSort()); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/partial_sort_copy/random_test.cc b/libstdc++-v3/testsuite/25_algorithms/partial_sort_copy/random_test.cc new file mode 100644 index 00000000000..cca4c568cbb --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/partial_sort_copy/random_test.cc @@ -0,0 +1,67 @@ +// Copyright (C) 2013 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 +// . + +// { dg-options "-std=gnu++11" } +// { dg-options "-std=gnu++11 -DSIMULATOR_TEST" { target simulator } } +// { dg-require-cstdint "" } + +// 25.4.1.4 [lib.alg.partial.sort.copy] + +#include +#include +#include +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::random_access_iterator_wrapper; + +typedef test_container Container; + +struct testPartialSortCopy +{ + template + void operator()(Container con, RandomGen& rg) + { + bool test __attribute__((unused)) = true; + + const int size = con.end() - con.begin(); + auto dist = std::uniform_int_distribution<>(0, size); + const int element = dist(rg); + + std::vector outvec(element + 1); // add +1 to avoid empty issues + + Container out(outvec.data(), outvec.data() + element); + + std::partial_sort_copy(con.begin(), con.end(), + out.begin(), out.begin() + element); + + VERIFY( std::is_sorted(out.begin(), out.begin() + element) ); + + std::sort(con.begin(), con.end()); + + for (int i = 0; i < element; ++i) + VERIFY( con.val(i) == out.val(i) ); + } +}; + +int +main() +{ + __gnu_test::test_containers(testPartialSortCopy()); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/sort/random_test.cc b/libstdc++-v3/testsuite/25_algorithms/sort/random_test.cc new file mode 100644 index 00000000000..ff3d06aa35c --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/sort/random_test.cc @@ -0,0 +1,51 @@ +// Copyright (C) 2013 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 +// . + +// { dg-options "-std=gnu++11" } +// { dg-options "-std=gnu++11 -DSIMULATOR_TEST" { target simulator } } +// { dg-require-cstdint "" } + +// 25.4.1 [lib.alg.sort] + +#include +#include +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::random_access_iterator_wrapper; + +typedef test_container Container; + +struct testSort +{ + template + void operator()(Container con, RandomGen&) + { + bool test __attribute__((unused)) = true; + + std::sort(con.begin(), con.end()); + VERIFY( std::is_sorted(con.begin(), con.end()) ); + } +}; + +int +main() +{ + __gnu_test::test_containers(testSort()); +} diff --git a/libstdc++-v3/testsuite/util/testsuite_containergen.h b/libstdc++-v3/testsuite/util/testsuite_containergen.h new file mode 100644 index 00000000000..ff512ac8a8e --- /dev/null +++ b/libstdc++-v3/testsuite/util/testsuite_containergen.h @@ -0,0 +1,97 @@ +// Copyright (C) 2013 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 +// . + +#ifndef _GLIBCXX_TESTSUITE_CONTAINER_GEN_H +#define _GLIBCXX_TESTSUITE_CONTAINER_GEN_H + +#include +#include + +namespace __gnu_test +{ + template + void + test_single_container(Tester test, RandomGen& rg, int length, int domain) + { + std::vector 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 + void + test_special_containers(Tester test, RandomGen& rg, int length) + { + std::vector 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 + void + test_containers(Tester test) + { + std::mt19937_64 random_gen; + +#ifdef SIMULATOR_TEST + int loops = 10; +#else + int loops = 1000; +#endif + + for(int i = 0; i < loops; ++i) + test_special_containers(test, random_gen, i); + + for(int i = 1; i < 100; ++i) + for(int j = 0; j < loops; ++j) + test_single_container(test, random_gen, i, i); + + for(int i = 0; i < loops; ++i) + { + test_single_container(test, random_gen, 10, 10); + test_single_container(test, random_gen, 100, 10); + test_single_container(test, random_gen, 1000, 10); + test_single_container(test, random_gen, 10, 1000); + } + +#ifndef SIMULATOR_TEST + for(int i = 0; i < 1000; ++i) + { + test_single_container(test, random_gen, 10000, 10); + test_single_container(test, random_gen, 10000, 10000); + } +#endif + } +} // namespace __gnu_test + +#endif diff --git a/libstdc++-v3/testsuite/util/testsuite_iterators.h b/libstdc++-v3/testsuite/util/testsuite_iterators.h index 9bb58588651..c9dccb79227 100644 --- a/libstdc++-v3/testsuite/util/testsuite_iterators.h +++ b/libstdc++-v3/testsuite/util/testsuite_iterators.h @@ -539,6 +539,10 @@ namespace __gnu_test return ItType(pos, &bounds); } + const T& + val(int pos) + { return (bounds.first)[pos]; } + ItType begin() { return it(bounds.first); }