[gcc(refs/users/ppalka/heads/libstdcxx-constrained-algos)] Add tests for the existing constrained algorithms

Patrick Palka ppalka@gcc.gnu.org
Thu Jan 16 19:42:00 GMT 2020


https://gcc.gnu.org/g:4bd55aded370f113e307ce7b141e71c224035f7c

commit 4bd55aded370f113e307ce7b141e71c224035f7c
Author: Patrick Palka <ppalka@gcc.gnu.org>
Date:   Mon Jan 13 16:30:56 2020 -0500

    Add tests for the existing constrained algorithms

Diff:
---
 .../testsuite/25_algorithms/any_of/constrained.cc  | 88 ++++++++++++++++++++++
 .../testsuite/25_algorithms/find/constrained.cc    | 73 ++++++++++++++++++
 .../testsuite/25_algorithms/find_if/constrained.cc | 75 ++++++++++++++++++
 .../25_algorithms/find_if_not/constrained.cc       | 75 ++++++++++++++++++
 .../25_algorithms/for_each/constrained.cc          | 83 ++++++++++++++++++++
 .../testsuite/25_algorithms/none_of/constrained.cc | 88 ++++++++++++++++++++++
 6 files changed, 482 insertions(+)

diff --git a/libstdc++-v3/testsuite/25_algorithms/any_of/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/any_of/constrained.cc
new file mode 100644
index 0000000..b234692
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/any_of/constrained.cc
@@ -0,0 +1,88 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+struct XLess
+{
+  int val;
+  bool operator()(X& x) const { return x.i < val; }
+};
+
+struct ILess
+{
+  int val;
+  bool operator()(int& i) const { return i < val; }
+};
+
+template<typename T>
+struct NotZero
+{
+  bool operator()(T& t) const { return t != 0; }
+};
+
+void
+test01()
+{
+  X x[] = { {2}, {4}, {6}, {8}, {10}, {11} };
+
+  VERIFY( ranges::any_of(x, x+6, XLess{3}) );
+  VERIFY( ranges::any_of(x, x+6, ILess{3}, &X::i) );
+  VERIFY( !ranges::any_of(x+1, x+6, XLess{3}) );
+  VERIFY( !ranges::any_of(x+1, x+6, ILess{3}, &X::i) );
+  VERIFY( ranges::any_of(x, XLess{5}) );
+  VERIFY( ranges::any_of(x, ILess{5}, &X::i) );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  VERIFY( ranges::any_of(c, NotZero<int>{}, &X::i) );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  VERIFY( ranges::any_of(r, NotZero<int>{}, &X::i) );
+  VERIFY( ranges::any_of(r, NotZero<X* const>{}, [](X& x) { return &x; }) );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  static constexpr Y y[] = { {1,2}, {2,4}, {3,6} };
+  static_assert(ranges::any_of(y, [](int i) { return i%2 == 0; }, &Y::i));
+  static_assert(ranges::any_of(y, [](const Y& y) { return y.i + y.j == 3; }));
+  static_assert(!ranges::any_of(y, [](const Y& y) { return y.i == y.j; }));
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/find/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/find/constrained.cc
new file mode 100644
index 0000000..5042138
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/find/constrained.cc
@@ -0,0 +1,73 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+void
+test01()
+{
+  X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
+  auto res = ranges::find(x, x+6, 8, &X::i);
+  VERIFY( res == x+3 );
+  res = ranges::find(x, x+6, 2, &X::i);
+  VERIFY( res == x+0 );
+  res = ranges::find(x, x+6, 9, &X::i);
+  VERIFY( res == x+6 );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  auto res2 = ranges::find(c, 8, &X::i);
+  VERIFY( res2 != ranges::end(c) && res2->i == 8 );
+  res2 = ranges::find(c, 9, &X::i);
+  VERIFY( res2 == ranges::end(c) );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  auto res3 = ranges::find(r, 8, &X::i);
+  VERIFY( res3 != ranges::end(r) && res3->i == 8 );
+  res3 = ranges::find(r, 9, &X::i);
+  VERIFY( res3 == ranges::end(r) );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  static constexpr Y y[] = { {1,2}, {2,4}, {3,6} };
+  static_assert(ranges::find(y, 4, &Y::j) == y+1);
+  static_assert(ranges::find(y, 5, &Y::j) == y+3);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/find_if/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/find_if/constrained.cc
new file mode 100644
index 0000000..26f4311
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/find_if/constrained.cc
@@ -0,0 +1,75 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+void
+test01()
+{
+  X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
+  auto res = ranges::find_if(x, x+6, [] (X& v) { return v.i == 8; });
+  VERIFY( res == x+3 );
+  res = ranges::find_if(x, x+6, [] (X& v) { return v.i % 2 == 0; });
+  VERIFY( res == x+0 );
+  res = ranges::find_if(x, x+6, [] (X& v) { return v.i == 9; });
+  VERIFY( res == x+6 );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  auto res2 = ranges::find_if(c, [] (int i) { return i > 7; }, &X::i);
+  VERIFY( res2 != ranges::end(c) && res2->i == 8 );
+  res2 = ranges::find_if(c, [] (int i) { return i > 11; }, &X::i);
+  VERIFY( res2 == ranges::end(c) );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  auto res3 = ranges::find_if(r, [] (int i) { return i > 10; }, &X::i);
+  VERIFY( res3 != ranges::end(r) && res3->i == 11 );
+  res3 = ranges::find_if(r, [] (int i) { return i == 9; }, &X::i);
+  VERIFY( res3 == ranges::end(r) );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  static constexpr Y y[] = { {1,2}, {2,4}, {3,6} };
+  static_assert(ranges::find_if(y, [] (int i) { return i > 3; }, &Y::j)
+		== y+1);
+  static_assert(ranges::find_if(y, [] (int i) { return i == 5; }, &Y::j)
+		== y+3);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/find_if_not/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/find_if_not/constrained.cc
new file mode 100644
index 0000000..3c16a5d
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/find_if_not/constrained.cc
@@ -0,0 +1,75 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+void
+test01()
+{
+  X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
+  auto res = ranges::find_if_not(x, x+6, [] (X& v) { return v.i != 8; });
+  VERIFY( res == x+3 );
+  res = ranges::find_if_not(x, x+6, [] (X& v) { return v.i % 2 == 1; });
+  VERIFY( res == x+0 );
+  res = ranges::find_if_not(x, x+6, [] (X& v) { return v.i != 9; });
+  VERIFY( res == x+6 );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  auto res2 = ranges::find_if_not(c, [] (int i) { return i <= 7; }, &X::i);
+  VERIFY( res2 != ranges::end(c) && res2->i == 8 );
+  res2 = ranges::find_if_not(c, [] (int i) { return i <= 11; }, &X::i);
+  VERIFY( res2 == ranges::end(c) );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  auto res3 = ranges::find_if_not(r, [] (int i) { return i <= 10; }, &X::i);
+  VERIFY( res3 != ranges::end(r) && res3->i == 11 );
+  res3 = ranges::find_if_not(r, [] (int i) { return i != 9; }, &X::i);
+  VERIFY( res3 == ranges::end(r) );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  static constexpr Y y[] = { {1,2}, {2,4}, {3,6} };
+  static_assert(ranges::find_if_not(y, [] (int i) { return i <= 3; }, &Y::j)
+		== y+1);
+  static_assert(ranges::find_if_not(y, [] (int i) { return i != 5; }, &Y::j)
+		== y+3);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/for_each/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/for_each/constrained.cc
new file mode 100644
index 0000000..142ad2e5
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/for_each/constrained.cc
@@ -0,0 +1,83 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+static int a;
+
+void
+f(int& i)
+{
+  a += i;
+}
+
+void
+test01()
+{
+  X x[] = { {2}, {4}, {6}, {8}, {10}, {11} };
+
+  auto res = ranges::for_each(x, x+6, f, &X::i);
+  VERIFY( res.in == x+6 );
+  VERIFY( res.fun == &f );
+  VERIFY( a == 41 );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  int p = 0;
+  ranges::for_each(c, [&p](int i) { ++p; }, &X::i);
+  VERIFY( p == 6 );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  int q = 0;
+  ranges::for_each(r, [&q](X&) { ++q; });
+  VERIFY( q == 6 );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  auto f = []
+  {
+    Y y[] = { {1,2}, {2,4}, {3,6} };
+    int a = 0;
+    ranges::for_each(y, [&a](int i) { a += i; }, &Y::i);
+    return a;
+  };
+  static_assert(f() == 6);
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/none_of/constrained.cc b/libstdc++-v3/testsuite/25_algorithms/none_of/constrained.cc
new file mode 100644
index 0000000..55617a9
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/none_of/constrained.cc
@@ -0,0 +1,88 @@
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+
+using __gnu_test::test_container;
+using __gnu_test::test_range;
+using __gnu_test::input_iterator_wrapper;
+using __gnu_test::forward_iterator_wrapper;
+
+namespace ranges = std::ranges;
+
+struct X { int i; };
+
+struct XLess
+{
+  int val;
+  bool operator()(X& x) const { return x.i < val; }
+};
+
+struct ILess
+{
+  int val;
+  bool operator()(int& i) const { return i < val; }
+};
+
+template<typename T>
+struct NotZero
+{
+  bool operator()(T& t) const { return t != 0; }
+};
+
+void
+test01()
+{
+  X x[] = { {2}, {4}, {6}, {8}, {10}, {11} };
+
+  VERIFY( !ranges::none_of(x, x+6, XLess{3}) );
+  VERIFY( !ranges::none_of(x, x+6, ILess{3}, &X::i) );
+  VERIFY( ranges::none_of(x+1, x+6, XLess{3}) );
+  VERIFY( ranges::none_of(x+1, x+6, ILess{3}, &X::i) );
+  VERIFY( !ranges::none_of(x, XLess{5}) );
+  VERIFY( !ranges::none_of(x, ILess{5}, &X::i) );
+
+  test_container<X, forward_iterator_wrapper> c(x);
+  VERIFY( !ranges::none_of(c, NotZero<int>{}, &X::i) );
+
+  test_range<X, input_iterator_wrapper> r(x);
+  VERIFY( !ranges::none_of(r, NotZero<int>{}, &X::i) );
+  VERIFY( !ranges::none_of(r, NotZero<X* const>{}, [](X& x) { return &x; }) );
+}
+
+struct Y { int i; int j; };
+
+void
+test02()
+{
+  static constexpr Y y[] = { {1,2}, {2,4}, {3,6} };
+  static_assert(!ranges::none_of(y, [](int i) { return i%2 == 0; }, &Y::i));
+  static_assert(!ranges::none_of(y, [](const Y& y) { return y.i + y.j == 3; }));
+  static_assert(ranges::none_of(y, [](const Y& y) { return y.i == y.j; }));
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}



More information about the Libstdc++-cvs mailing list