Commit bc464641 by Patrick Palka

libstdc++: Implement C++20 constrained algorithms

This patch implements the C++20 ranges overloads for the algorithms in
[algorithms].  Most of the algorithms were reimplemented, with each of their
implementations very closely following the existing implementation in
bits/stl_algo.h and bits/stl_algobase.h.  The reason for reimplementing most of
the algorithms instead of forwarding to their STL-style overload is because
forwarding cannot be conformantly and efficiently performed for algorithms that
operate on non-random-access iterators.  But algorithms that operate on random
access iterators can safely and efficiently be forwarded to the STL-style
implementation, and this patch does so for push_heap, pop_heap, make_heap,
sort_heap, sort, stable_sort, nth_element, inplace_merge and stable_partition.

What's missing from this patch is debug-iterator and container specializations
that are present for some of the STL-style algorithms that need to be ported
over to the ranges algos.  I marked them missing at TODO comments.  There are
also some other minor outstanding TODOs.

The code that could use the most thorough review is ranges::__copy_or_move,
ranges::__copy_or_move_backward, ranges::__equal and
ranges::__lexicographical_compare.  In the tests, I tried to test the interface
of each new overload, as well as the correctness of the new implementation.

libstdc++-v3/ChangeLog:

	Implement C++20 constrained algorithms
	* include/Makefile.am: Add new header.
	* include/Makefile.in: Regenerate.
	* include/std/algorithm: Include <bits/ranges_algo.h>.
	* include/bits/ranges_algo.h: New file.
	* testsuite/25_algorithms/adjacent_find/constrained.cc: New test.
	* testsuite/25_algorithms/all_of/constrained.cc: New test.
	* testsuite/25_algorithms/any_of/constrained.cc: New test.
	* testsuite/25_algorithms/binary_search/constrained.cc: New test.
	* testsuite/25_algorithms/copy/constrained.cc: New test.
	* testsuite/25_algorithms/copy_backward/constrained.cc: New test.
	* testsuite/25_algorithms/copy_if/constrained.cc: New test.
	* testsuite/25_algorithms/copy_n/constrained.cc: New test.
	* testsuite/25_algorithms/count/constrained.cc: New test.
	* testsuite/25_algorithms/count_if/constrained.cc: New test.
	* testsuite/25_algorithms/equal/constrained.cc: New test.
	* testsuite/25_algorithms/equal_range/constrained.cc: New test.
	* testsuite/25_algorithms/fill/constrained.cc: New test.
	* testsuite/25_algorithms/fill_n/constrained.cc: New test.
	* testsuite/25_algorithms/find/constrained.cc: New test.
	* testsuite/25_algorithms/find_end/constrained.cc: New test.
	* testsuite/25_algorithms/find_first_of/constrained.cc: New test.
	* testsuite/25_algorithms/find_if/constrained.cc: New test.
	* testsuite/25_algorithms/find_if_not/constrained.cc: New test.
	* testsuite/25_algorithms/for_each/constrained.cc: New test.
	* testsuite/25_algorithms/generate/constrained.cc: New test.
	* testsuite/25_algorithms/generate_n/constrained.cc: New test.
	* testsuite/25_algorithms/heap/constrained.cc: New test.
	* testsuite/25_algorithms/includes/constrained.cc: New test.
	* testsuite/25_algorithms/inplace_merge/constrained.cc: New test.
	* testsuite/25_algorithms/is_partitioned/constrained.cc: New test.
	* testsuite/25_algorithms/is_permutation/constrained.cc: New test.
	* testsuite/25_algorithms/is_sorted/constrained.cc: New test.
	* testsuite/25_algorithms/is_sorted_until/constrained.cc: New test.
	* testsuite/25_algorithms/lexicographical_compare/constrained.cc: New
	test.
	* testsuite/25_algorithms/lower_bound/constrained.cc: New test.
	* testsuite/25_algorithms/max/constrained.cc: New test.
	* testsuite/25_algorithms/max_element/constrained.cc: New test.
	* testsuite/25_algorithms/merge/constrained.cc: New test.
	* testsuite/25_algorithms/min/constrained.cc: New test.
	* testsuite/25_algorithms/min_element/constrained.cc: New test.
	* testsuite/25_algorithms/minmax/constrained.cc: New test.
	* testsuite/25_algorithms/minmax_element/constrained.cc: New test.
	* testsuite/25_algorithms/mismatch/constrained.cc: New test.
	* testsuite/25_algorithms/move/constrained.cc: New test.
	* testsuite/25_algorithms/move_backward/constrained.cc: New test.
	* testsuite/25_algorithms/next_permutation/constrained.cc: New test.
	* testsuite/25_algorithms/none_of/constrained.cc: New test.
	* testsuite/25_algorithms/nth_element/constrained.cc: New test.
	* testsuite/25_algorithms/partial_sort/constrained.cc: New test.
	* testsuite/25_algorithms/partial_sort_copy/constrained.cc: New test.
	* testsuite/25_algorithms/partition/constrained.cc: New test.
	* testsuite/25_algorithms/partition_copy/constrained.cc: New test.
	* testsuite/25_algorithms/partition_point/constrained.cc: New test.
	* testsuite/25_algorithms/prev_permutation/constrained.cc: New test.
	* testsuite/25_algorithms/remove/constrained.cc: New test.
	* testsuite/25_algorithms/remove_copy/constrained.cc: New test.
	* testsuite/25_algorithms/remove_copy_if/constrained.cc: New test.
	* testsuite/25_algorithms/remove_if/constrained.cc: New test.
	* testsuite/25_algorithms/replace/constrained.cc: New test.
	* testsuite/25_algorithms/replace_copy/constrained.cc: New test.
	* testsuite/25_algorithms/replace_copy_if/constrained.cc: New test.
	* testsuite/25_algorithms/replace_if/constrained.cc: New test.
	* testsuite/25_algorithms/reverse/constrained.cc: New test.
	* testsuite/25_algorithms/reverse_copy/constrained.cc: New test.
	* testsuite/25_algorithms/rotate/constrained.cc: New test.
	* testsuite/25_algorithms/rotate_copy/constrained.cc: New test.
	* testsuite/25_algorithms/search/constrained.cc: New test.
	* testsuite/25_algorithms/search_n/constrained.cc: New test.
	* testsuite/25_algorithms/set_difference/constrained.cc: New test.
	* testsuite/25_algorithms/set_intersection/constrained.cc: New test.
	* testsuite/25_algorithms/set_symmetric_difference/constrained.cc: New
	test.
	* testsuite/25_algorithms/set_union/constrained.cc: New test.
	* testsuite/25_algorithms/shuffle/constrained.cc: New test.
	* testsuite/25_algorithms/sort/constrained.cc: New test.
	* testsuite/25_algorithms/stable_partition/constrained.cc: New test.
	* testsuite/25_algorithms/stable_sort/constrained.cc: New test.
	* testsuite/25_algorithms/swap_ranges/constrained.cc: New test.
	* testsuite/25_algorithms/transform/constrained.cc: New test.
	* testsuite/25_algorithms/unique/constrained.cc: New test.
	* testsuite/25_algorithms/unique_copy/constrained.cc: New test.
	* testsuite/25_algorithms/upper_bound/constrained.cc: New test.
parent 13f5b93e
2020-02-07 Patrick Palka <ppalka@redhat.com>
Jonathan Wakely <jwakely@redhat.com>
Implement C++20 constrained algorithms
* include/Makefile.am: Add new header.
* include/Makefile.in: Regenerate.
* include/std/algorithm: Include <bits/ranges_algo.h>.
* include/bits/ranges_algo.h: New file.
* testsuite/25_algorithms/adjacent_find/constrained.cc: New test.
* testsuite/25_algorithms/all_of/constrained.cc: New test.
* testsuite/25_algorithms/any_of/constrained.cc: New test.
* testsuite/25_algorithms/binary_search/constrained.cc: New test.
* testsuite/25_algorithms/copy/constrained.cc: New test.
* testsuite/25_algorithms/copy_backward/constrained.cc: New test.
* testsuite/25_algorithms/copy_if/constrained.cc: New test.
* testsuite/25_algorithms/copy_n/constrained.cc: New test.
* testsuite/25_algorithms/count/constrained.cc: New test.
* testsuite/25_algorithms/count_if/constrained.cc: New test.
* testsuite/25_algorithms/equal/constrained.cc: New test.
* testsuite/25_algorithms/equal_range/constrained.cc: New test.
* testsuite/25_algorithms/fill/constrained.cc: New test.
* testsuite/25_algorithms/fill_n/constrained.cc: New test.
* testsuite/25_algorithms/find/constrained.cc: New test.
* testsuite/25_algorithms/find_end/constrained.cc: New test.
* testsuite/25_algorithms/find_first_of/constrained.cc: New test.
* testsuite/25_algorithms/find_if/constrained.cc: New test.
* testsuite/25_algorithms/find_if_not/constrained.cc: New test.
* testsuite/25_algorithms/for_each/constrained.cc: New test.
* testsuite/25_algorithms/generate/constrained.cc: New test.
* testsuite/25_algorithms/generate_n/constrained.cc: New test.
* testsuite/25_algorithms/heap/constrained.cc: New test.
* testsuite/25_algorithms/includes/constrained.cc: New test.
* testsuite/25_algorithms/inplace_merge/constrained.cc: New test.
* testsuite/25_algorithms/is_partitioned/constrained.cc: New test.
* testsuite/25_algorithms/is_permutation/constrained.cc: New test.
* testsuite/25_algorithms/is_sorted/constrained.cc: New test.
* testsuite/25_algorithms/is_sorted_until/constrained.cc: New test.
* testsuite/25_algorithms/lexicographical_compare/constrained.cc: New
test.
* testsuite/25_algorithms/lower_bound/constrained.cc: New test.
* testsuite/25_algorithms/max/constrained.cc: New test.
* testsuite/25_algorithms/max_element/constrained.cc: New test.
* testsuite/25_algorithms/merge/constrained.cc: New test.
* testsuite/25_algorithms/min/constrained.cc: New test.
* testsuite/25_algorithms/min_element/constrained.cc: New test.
* testsuite/25_algorithms/minmax/constrained.cc: New test.
* testsuite/25_algorithms/minmax_element/constrained.cc: New test.
* testsuite/25_algorithms/mismatch/constrained.cc: New test.
* testsuite/25_algorithms/move/constrained.cc: New test.
* testsuite/25_algorithms/move_backward/constrained.cc: New test.
* testsuite/25_algorithms/next_permutation/constrained.cc: New test.
* testsuite/25_algorithms/none_of/constrained.cc: New test.
* testsuite/25_algorithms/nth_element/constrained.cc: New test.
* testsuite/25_algorithms/partial_sort/constrained.cc: New test.
* testsuite/25_algorithms/partial_sort_copy/constrained.cc: New test.
* testsuite/25_algorithms/partition/constrained.cc: New test.
* testsuite/25_algorithms/partition_copy/constrained.cc: New test.
* testsuite/25_algorithms/partition_point/constrained.cc: New test.
* testsuite/25_algorithms/prev_permutation/constrained.cc: New test.
* testsuite/25_algorithms/remove/constrained.cc: New test.
* testsuite/25_algorithms/remove_copy/constrained.cc: New test.
* testsuite/25_algorithms/remove_copy_if/constrained.cc: New test.
* testsuite/25_algorithms/remove_if/constrained.cc: New test.
* testsuite/25_algorithms/replace/constrained.cc: New test.
* testsuite/25_algorithms/replace_copy/constrained.cc: New test.
* testsuite/25_algorithms/replace_copy_if/constrained.cc: New test.
* testsuite/25_algorithms/replace_if/constrained.cc: New test.
* testsuite/25_algorithms/reverse/constrained.cc: New test.
* testsuite/25_algorithms/reverse_copy/constrained.cc: New test.
* testsuite/25_algorithms/rotate/constrained.cc: New test.
* testsuite/25_algorithms/rotate_copy/constrained.cc: New test.
* testsuite/25_algorithms/search/constrained.cc: New test.
* testsuite/25_algorithms/search_n/constrained.cc: New test.
* testsuite/25_algorithms/set_difference/constrained.cc: New test.
* testsuite/25_algorithms/set_intersection/constrained.cc: New test.
* testsuite/25_algorithms/set_symmetric_difference/constrained.cc: New
test.
* testsuite/25_algorithms/set_union/constrained.cc: New test.
* testsuite/25_algorithms/shuffle/constrained.cc: New test.
* testsuite/25_algorithms/sort/constrained.cc: New test.
* testsuite/25_algorithms/stable_partition/constrained.cc: New test.
* testsuite/25_algorithms/stable_sort/constrained.cc: New test.
* testsuite/25_algorithms/swap_ranges/constrained.cc: New test.
* testsuite/25_algorithms/transform/constrained.cc: New test.
* testsuite/25_algorithms/unique/constrained.cc: New test.
* testsuite/25_algorithms/unique_copy/constrained.cc: New test.
* testsuite/25_algorithms/upper_bound/constrained.cc: New test.
2020-02-06 Jonathan Wakely <jwakely@redhat.com>
* include/bits/stl_iterator.h (__detail::__common_iter_ptr): Fix PR
......
......@@ -157,6 +157,7 @@ bits_headers = \
${bits_srcdir}/random.tcc \
${bits_srcdir}/range_access.h \
${bits_srcdir}/range_cmp.h \
${bits_srcdir}/ranges_algo.h \
${bits_srcdir}/refwrap.h \
${bits_srcdir}/regex.h \
${bits_srcdir}/regex.tcc \
......
......@@ -502,6 +502,7 @@ bits_headers = \
${bits_srcdir}/random.tcc \
${bits_srcdir}/range_access.h \
${bits_srcdir}/range_cmp.h \
${bits_srcdir}/ranges_algo.h \
${bits_srcdir}/refwrap.h \
${bits_srcdir}/regex.h \
${bits_srcdir}/regex.tcc \
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -60,6 +60,9 @@
#include <utility> // UK-300.
#include <bits/stl_algobase.h>
#include <bits/stl_algo.h>
#if __cplusplus > 201703L
# include <bits/ranges_algo.h>
#endif
#if __cplusplus > 201402L
// Parallel STL algorithms
......
// 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} };
int y[] = { 2, 7, 8, 8, 9 };
VERIFY( ranges::adjacent_find(x, x+6, {}, &X::i) == x+0 );
VERIFY( ranges::adjacent_find(x+1, x+6, {}, &X::i) == x+6 );
VERIFY( ranges::adjacent_find(y) == y+2 );
VERIFY( ranges::adjacent_find(y, y+4) == y+2 );
test_container<X, forward_iterator_wrapper> c(x);
VERIFY( ranges::adjacent_find(c, {}, &X::i) == ranges::begin(c) );
test_range<int, forward_iterator_wrapper> r(y);
auto res = ranges::adjacent_find(r);
VERIFY( *res == 8 && *++res == 8 );
}
void
test02()
{
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
static constexpr X y[] = { {2}, {6}, {8}, {10}, {11} };
static_assert(ranges::adjacent_find(x, {}, &X::i) == x+0);
static_assert(ranges::adjacent_find(y, {}, &X::i) == y+5);
}
int
main()
{
test01();
test02();
}
// 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::all_of(x, x+5, XLess{11}) );
VERIFY( ranges::all_of(x, x+5, ILess{11}, &X::i) );
VERIFY( !ranges::all_of(x, x+6, ILess{11}, &X::i) );
VERIFY( !ranges::all_of(x, XLess{11}) );
VERIFY( ranges::all_of(x, XLess{12}) );
VERIFY( ranges::all_of(x, ILess{12}, &X::i) );
VERIFY( !ranges::all_of(x, ILess{11}, &X::i) );
test_container<X, forward_iterator_wrapper> c(x);
VERIFY( ranges::all_of(c, NotZero<int>{}, &X::i) );
test_range<X, input_iterator_wrapper> r(x);
VERIFY( ranges::all_of(r, NotZero<int>{}, &X::i) );
r.bounds.first = x;
VERIFY( ranges::all_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::all_of(y, [](int j) { return j%2 == 0; }, &Y::j));
static_assert(ranges::all_of(y, [](const Y& y) { return y.j == y.i * 2; }));
}
int
main()
{
test01();
test02();
}
// 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();
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
float x[] = {1, 2, 3, 4, 5, 5, 6, 7};
test_container<float, forward_iterator_wrapper> cx(x);
for (int i = 0; i < 7; i++)
{
VERIFY( ranges::binary_search(cx, i, {}, [] (int a) { return a-1; }) );
VERIFY( !ranges::binary_search(cx.begin(), cx.end(), i+0.5) );
}
VERIFY( !ranges::binary_search(cx, 0) );
ranges::reverse(x);
test_range<float, forward_iterator_wrapper> rx(x);
VERIFY( ranges::binary_search(rx, 5, ranges::greater{}) );
}
constexpr bool
test02()
{
int x[] = {1, 2, 3};
return (ranges::binary_search(x, 3)
&& !ranges::binary_search(x, x, 3));
}
int
main()
{
test01();
static_assert(test02());
}
// 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 <vector>
#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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
{
int x[7] = { 1, 2, 3, 4, 5, 6, 7 };
int y[7] = { 0 };
int z[7] = { 1, 2, 3, 4, 5, 6, 7 };
auto [in, out] = ranges::copy(x, y);
VERIFY( ranges::equal(x, y) && in == x+7 && out == y+7 );
VERIFY( ranges::equal(x, z) );
}
{
int x[3] = { 1, 2, 3 };
char y[4] = { 0 };
int z[3] = { 1, 2, 3 };
test_container<int, forward_iterator_wrapper> cx(x);
test_container<char, forward_iterator_wrapper> cy(y);
auto [in, out] = ranges::copy(cx, ranges::begin(cy));
VERIFY( ranges::equal(x, x+3, y, y+3) && in.ptr == x+3 && out.ptr == y+3 );
VERIFY( ranges::equal(x, z) );
}
{
char x[3] = { 1, 2, 3 };
int y[4] = { 0 };
int z[3] = { 1, 2, 3 };
test_range<char, input_iterator_wrapper> rx(x);
test_range<int, output_iterator_wrapper> ry(y);
auto [in, out] = ranges::copy(rx, ranges::begin(ry));
VERIFY( ranges::equal(x, x+3, y, y+3) && in.ptr == x+3 && out.ptr == y+3 );
VERIFY( ranges::equal(x, z) );
}
{
std::vector<char> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::copy(x, ranges::begin(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::copy(x, ranges::begin(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::copy(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.end()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<char> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::copy(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.end()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
}
struct X
{
int i;
constexpr X (int a) : i(a) { }
};
void
test02()
{
int x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
int z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::copy(x, y);
VERIFY( ranges::equal(x, x+5, y, y+5, {}, {}, &X::i) );
VERIFY( in == x+5 );
VERIFY( out == y+5 );
VERIFY( y[5].i == 2 );
VERIFY( ranges::equal(x, z) );
}
constexpr bool
test03()
{
bool ok = true;
int x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
int z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::copy(x, y);
ok &= ranges::equal(x, x+5, y, y+5, {}, {}, &X::i);
ok &= (in == x+5);
ok &= (out == y+5);
ok &= (y[5].i == 2);
ok &= ranges::equal(x, z);
return ok;
}
struct Y
{
int i;
int moved = 0;
constexpr Y(int a) : i(a) { }
constexpr Y(const Y&) = delete;
constexpr Y& operator=(const Y&) = delete;
constexpr Y(Y&& other)
{
*this = std::move(other);
}
constexpr Y&
operator=(Y&& other)
{
other.moved++;
i = other.i;
return *this;
}
friend constexpr bool
operator==(const Y& a, const Y& b)
{ return a.i == b.i; }
};
void
test04()
{
Y x[7] = { 1, 2, 3, 4, 5, 6, 7 };
Y y[7] = { 0, 0, 0, 0, 0, 0, 0 };
Y z[7] = { 1, 2, 3, 4, 5, 6, 7 };
test_range<Y, input_iterator_wrapper> rx(x);
auto [in, out] = ranges::copy(std::move_iterator{ranges::begin(rx)},
std::move_sentinel{ranges::end(rx)},
ranges::begin(y));
VERIFY( ranges::equal(x, y) && std::move(in).base().ptr == x+7 && out == y+7 );
VERIFY( ranges::equal(x, z) );
for (const auto& v : x)
VERIFY( v.moved == 1 );
for (const auto& v : y)
VERIFY( v.moved == 0 );
}
constexpr bool
test05()
{
bool ok = true;
Y x[7] = { 1, 2, 3, 4, 5, 6, 7 };
Y y[7] = { 0, 0, 0, 0, 0, 0, 0 };
Y z[7] = { 1, 2, 3, 4, 5, 6, 7 };
auto [in, out] = ranges::copy(std::move_iterator{ranges::begin(x)},
std::move_sentinel{ranges::end(x)},
ranges::begin(y));
ok &= ranges::equal(x, y);
ok &= in.base() == x+7;
ok &= out == y+7;
ok &= ranges::equal(x, z);
for (const auto& v : x)
ok &= v.moved == 1;
for (const auto& v : y)
ok &= v.moved == 0;
return ok;
}
int
main()
{
test01();
test02();
static_assert(test03());
test04();
static_assert(test05());
}
// 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 <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
{
int x[7] = { 1, 2, 3, 4, 5, 6, 7 };
int y[7] = { 0 };
int z[7] = { 1, 2, 3, 4, 5, 6, 7 };
auto [in, out] = ranges::copy_backward(x, ranges::end(y));
VERIFY( ranges::equal(x, y) && in == x+7 && out == y);
VERIFY( ranges::equal(x, z) );
}
{
int x[3] = { 1, 2, 3 };
char y[4] = { 0 };
int z[3] = { 1, 2, 3 };
test_container<int, bidirectional_iterator_wrapper> cx(x);
test_container<char, bidirectional_iterator_wrapper> cy(y);
auto [in, out] = ranges::copy_backward(cx, ranges::end(cy));
VERIFY( ranges::equal(x, x+3, y+1, y+4) && in.ptr == x+3 && out.ptr == y+1 );
VERIFY( ranges::equal(x, z) );
}
{
std::vector<char> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::copy_backward(x, ranges::end(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::copy_backward(x, ranges::end(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::copy_backward(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.begin()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<char> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::copy_backward(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.begin()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
}
constexpr bool
test02()
{
bool ok = true;
int x[] = { {2}, {2}, {6}, {8}, {10} };
int y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
const int z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::copy_backward(x, ranges::end(y));
ok &= ranges::equal(x, x+5, y+1, y+6);
ok &= (in == x+5);
ok &= (out == y+1);
ok &= (y[0] == 2);
ok &= ranges::equal(x, z);
return ok;
}
/* move_iterators are always input_iterators and therefore do not model
* bidirectional_iterator, so I think the following tests are rightly invalid.
struct Y
{
int i;
int moved = 0;
constexpr Y(int a) : i(a) { }
constexpr Y(const Y&) = delete;
constexpr Y& operator=(const Y&) = delete;
constexpr Y(Y&& other)
{
*this = std::move(other);
}
constexpr Y&
operator=(Y&& other)
{
other.moved++;
i = other.i;
return *this;
}
friend constexpr bool
operator==(const Y& a, const Y& b)
{ return a.i == b.i; }
};
void
test02()
{
Y x[7] = { 1, 2, 3, 4, 5, 6, 7 };
Y y[7] = { 0, 0, 0, 0, 0, 0, 0 };
Y z[7] = { 1, 2, 3, 4, 5, 6, 7 };
test_range<Y, bidirectional_iterator_wrapper> rx(x);
auto [in, out] = ranges::copy_backward(std::move_iterator{ranges::begin(rx)},
std::move_sentinel{ranges::end(rx)},
ranges::end(y));
VERIFY( ranges::equal(x, y) && std::move(in).base().ptr == x+7 && out == y );
VERIFY( ranges::equal(x, z) );
for (const auto& v : x)
VERIFY( v.moved == 1 );
for (const auto& v : y)
VERIFY( v.moved == 0 );
}
constexpr bool
test03()
{
bool ok = true;
Y x[7] = { 1, 2, 3, 4, 5, 6, 7 };
Y y[7] = { 0, 0, 0, 0, 0, 0, 0 };
Y z[7] = { 1, 2, 3, 4, 5, 6, 7 };
auto [in, out] = ranges::copy_backward(std::move_iterator{ranges::begin(x)},
std::move_sentinel{ranges::end(x)},
ranges::end(y));
ok &= ranges::equal(x, y);
ok &= in.base() == x+7;
ok &= out == y;
ok &= ranges::equal(x, z);
for (const auto& v : x)
ok &= v.moved == 1;
for (const auto& v : y)
ok &= v.moved == 0;
return ok;
}
*/
int
main()
{
test01();
static_assert(test02());
}
// 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 <vector>
#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::output_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1,2,3,4,5,6,7};
{
const int y[] = {2,4,6};
int w[7];
test_range<int, input_iterator_wrapper> rx(x);
test_range<int, output_iterator_wrapper> rw(w);
auto [in,out] = ranges::copy_if(rx, rw.begin(),
[] (int a) { return (a%2)==0; });
VERIFY( in == rx.end() && out.ptr == w+3 );
VERIFY( ranges::equal(w, w+3, y, y+3) );
}
{
const int y[] = {1,3,5,7};
int w[7];
test_range<int, input_iterator_wrapper> rx(x);
test_range<int, output_iterator_wrapper> rw(w);
auto [in,out] = ranges::copy_if(rx, rw.begin(),
[] (int a) { return (a%2)==0; },
[] (int a) { return a+1; });
VERIFY( in == rx.end() && out.ptr == w+4 );
VERIFY( ranges::equal(w, w+4, y, y+4) );
}
}
constexpr bool
test02()
{
int x[] = {1,2,3};
const int y[] = {1,3};
int w[3];
auto [in,out] = ranges::copy_if(x, w, [] (int a) { return (a%2)==1; });
return ranges::equal(w, out, y, y+2);
}
int
main()
{
test01();
static_assert(test02());
}
// 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 <vector>
#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::output_iterator_wrapper;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
template<template<typename> typename in_wrapper,
template<typename> typename out_wrapper>
void
test01()
{
int x[] = {1,2,3,4,5,6,7};
for (int i = -1; i <= 7; i++)
{
test_range<int, in_wrapper> rx(x);
int w[7];
test_range<int, out_wrapper> rw(w);
ranges::copy_n(rx.begin(), i, rw.begin());
if (i >= 0)
VERIFY( ranges::equal(x, x+i, w, w+i) );
}
}
constexpr bool
test02()
{
int x[] = {1,2,3};
int y[2];
auto [in,out] = ranges::copy_n(x, 2, y);
return (in == x+2
&& out == y+2
&& ranges::equal(x, x+2, y, y+2));
}
int
main()
{
test01<input_iterator_wrapper,
output_iterator_wrapper>();
test01<random_access_iterator_wrapper,
output_iterator_wrapper>();
test01<random_access_iterator_wrapper,
random_access_iterator_wrapper>();
static_assert(test02());
}
// 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}, {2} };
auto res = ranges::count(x, x+7, 2, &X::i);
VERIFY( res == 3 );
res = ranges::count(x, x+7, 8, &X::i);
VERIFY( res == 1 );
res = ranges::count(x, x+7, 9, &X::i);
VERIFY( res == 0 );
test_container<X, forward_iterator_wrapper> c(x);
res = ranges::count(c, 6, &X::i);
VERIFY( res == 1 );
res = ranges::count(c, 9, &X::i);
VERIFY( res == 0 );
test_range<X, input_iterator_wrapper> r(x);
res = ranges::count(r, 2, &X::i);
VERIFY( res == 3 );
r.bounds.first = x;
res = ranges::count(r, 9, &X::i);
VERIFY( res == 0 );
}
struct Y { int i; int j; };
void
test02()
{
static constexpr Y y[] = { {1,2}, {2,4}, {3,6}, {1,6} };
static_assert(ranges::count(y, 6, &Y::j) == 2);
static_assert(ranges::count(y, 5, &Y::j) == 0);
}
int
main()
{
test01();
test02();
}
// 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}, {2} };
auto res = ranges::count_if(x, x+7, [] (int i) { return i % 2 == 0; }, &X::i);
VERIFY( res == 6 );
res = ranges::count_if(x, x+7, [] (int i) { return i % 2 == 1; }, &X::i);
VERIFY( res == 1 );
res = ranges::count_if(x, x+7, [] (int i) { return i < 0; }, &X::i);
VERIFY( res == 0 );
test_container<X, forward_iterator_wrapper> c(x);
res = ranges::count_if(c, [] (int i) { return i == 2; }, &X::i);
VERIFY( res == 3 );
res = ranges::count_if(c, [] (int i) { return i < 0; }, &X::i);
VERIFY( res == 0 );
test_range<X, input_iterator_wrapper> r(x);
res = ranges::count_if(c, [] (int i) { return i == 2; }, &X::i);
VERIFY( res == 3 );
res = ranges::count_if(c, [] (int i) { return i < 0; }, &X::i);
VERIFY( res == 0 );
}
struct Y { int i; int j; };
void
test02()
{
static constexpr Y y[] = { {1,2}, {2,4}, {3,6}, {1,6} };
static_assert(ranges::count_if(y, [] (int i) { return i < 5; }, &Y::j) == 2);
static_assert(ranges::count_if(y, [] (int i) { return i != 4; }, &Y::j) == 3);
}
int
main()
{
test01();
test02();
}
// 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;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
struct X { int i; };
void
test01()
{
int x[] = { {2}, {2}, {6}, {8}, {10}, {11}, {11} };
int y[] = { {2}, {2}, {6}, {8}, {10}, {11}, {11} };
X z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
int w[] = { {1}, {1}, {1}, {1}, {1} };
VERIFY( ranges::equal(w, w+4, w+1, w+5) );
VERIFY( ranges::equal(w, w+5, w, w+5, ranges::greater{},
[] (int a) { return a+1; }) );
test_container<int, forward_iterator_wrapper> cx(x), cy(y);
test_container<X, forward_iterator_wrapper> cz(z);
VERIFY( ranges::equal(cx, cy) );
VERIFY( !ranges::equal(cx, cy, {}, [] (int a) { return a+1; }) );
VERIFY( !ranges::equal(cx, cz, {}, {}, &X::i) );
test_range<int, input_iterator_wrapper> rx(x), ry(y);
test_range<X, input_iterator_wrapper> rz(z);
VERIFY( ranges::equal(rx, ry) );
rx.bounds.first = x;
ry.bounds.first = y;
VERIFY( !ranges::equal(rx, ry, {}, {}, [] (int a) { return a+1; }) );
rx.bounds.first = x;
rz.bounds.first = z;
VERIFY( !ranges::equal(rx, rz, {}, {}, &X::i) );
}
void
test02()
{
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
static constexpr X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
static constexpr int w[] = { {2}, {6}, {8}, {10}, {2}, {2} };
static_assert(ranges::equal(z, w));
static_assert(!ranges::equal(z, z+5, w+1, w+6));
static_assert(!ranges::equal(z, z, {}, {}, [] (int a) { return a+1; }));
static_assert(!ranges::equal(x, y, {}, &X::i, &X::i));
}
void
test03()
{
std::vector<int> x = { {2}, {2}, {6}, {8}, {10}, {11} };
std::vector<int> y = { {2}, {2}, {6}, {8}, {10}, {11} };
std::vector<int> z = { {2}, {2}, {6}, {8}, {10}, {12} };
VERIFY( ranges::equal(x, y) );
VERIFY( !ranges::equal(x, z) );
}
int
main()
{
test01();
test02();
test03();
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1, 2, 3, 4, 5, 5, 6, 7};
for (unsigned i = 0; i < 5; i++)
for (unsigned j = 6; j < 8; j++)
{
test_container<int, forward_iterator_wrapper> cx(x);
auto range = ranges::equal_range(std::next(cx.begin(), i),
std::next(cx.begin(), j),
4, {}, [] (int a) { return a-1; });
VERIFY( range.begin().ptr == x+4 && range.end().ptr == x+6 );
}
ranges::reverse(x);
test_range<int, forward_iterator_wrapper> rx(x);
auto range = ranges::equal_range(rx, 5, ranges::greater{},
[] (int a) { return a+1; });
VERIFY( range.begin().ptr == x+4 && range.end().ptr == x+5 );
}
constexpr bool
test02()
{
int x[] = {1, 2, 3, 4, 5};
auto range1 = ranges::equal_range(x, 6);
auto range2 = ranges::equal_range(x, x, 6);
auto range3 = ranges::equal_range(x, 1);
return (range1.begin() == x+5 && range1.end() == x+5
&& range2.begin() == x && range2.end() == x
&& range3.begin() == x && range3.end() == x+1);
}
int
main()
{
test01();
static_assert(test02());
}
// 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>
#include <list>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
};
void
test01()
{
const int c[6] = { 17, 17, 17, 17, 17, 17 };
{
X x[6];
VERIFY( ranges::fill(x, X{17}) == x+6 );
VERIFY( ranges::equal(x, c, {}, &X::i) );
}
{
char x[6];
VERIFY( ranges::fill(x, 17) == x+6 );
VERIFY( ranges::equal(x, c) );
}
{
X x[6];
test_container<X, forward_iterator_wrapper> cx(x);
VERIFY( ranges::fill(cx, X{17}) == cx.end() );
VERIFY( ranges::equal(cx, c, {}, &X::i) );
}
{
int x[6];
test_range<int, output_iterator_wrapper> rx(x);
VERIFY( ranges::fill(rx, 17) == rx.end() );
VERIFY( ranges::equal(x, c) );
}
{
std::list<int> list(6);
ranges::fill(list, 17);
VERIFY( ranges::equal(list, c) );
}
}
constexpr bool
test02()
{
bool ok = true;
int x[5];
ranges::fill(x, 17);
for (auto v : x)
ok &= v == 17;
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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>
#include <list>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
};
void
test01()
{
const int c[6] = { 17, 17, 17, 4, 5, 6 };
{
X x[6] = { {1}, {2}, {3}, {4}, {5}, {6} };
VERIFY( ranges::fill_n(x, 3, X{17}) == x+3 );
VERIFY( ranges::equal(x, c, {}, &X::i) );
}
{
char x[6];
VERIFY( ranges::fill_n(x, 3, 17) == x+3 );
VERIFY( ranges::equal(x, x+3, c, c+3) );
}
{
X x[6] = { 1, 2, 3, 4, 5, 6 };
test_container<X, forward_iterator_wrapper> cx(x);
VERIFY( ranges::fill_n(cx.begin(), 3, X{17})->i == 4 );
VERIFY( ranges::equal(cx, c, {}, &X::i) );
}
{
int x[6] = { 1, 2, 3, 4, 5, 6 };;
test_range<int, output_iterator_wrapper> rx(x);
ranges::fill_n(ranges::begin(rx), 3, 17);
VERIFY( ranges::equal(x, c) );
}
{
std::list<int> list({1, 2, 3, 4, 5, 6});
ranges::fill_n(list.begin(), 3, 17);
VERIFY( ranges::equal(list, c) );
}
}
constexpr bool
test02()
{
bool ok = true;
int x[6] = { 1, 2, 3, 4, 5, 6 };
const int y[6] = { 1, 2, 3, 4, 5, 6 };
const int z[6] = { 17, 17, 17, 4, 5, 6 };
ranges::fill_n(x, 0, 17);
ranges::fill_n(x, -1, 17);
ok &= ranges::equal(x, y);
ranges::fill_n(x, 3, 17);
ok &= ranges::equal(x, z);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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 );
r.bounds.first = x;
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();
}
// 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;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
struct X { int i; };
void
test01()
{
X x[] = { {10}, {11}, {2}, {6}, {8}, {10}, {11} };
X y[] = { {10}, {11} };
{
test_container<X, forward_iterator_wrapper> c(x);
auto res = ranges::find_end(c, y, {}, &X::i, &X::i);
VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(c) );
res = ranges::find_end(c, c, {}, &X::i, &X::i);
VERIFY( std::get<0>(res) == ranges::begin(c)
&& std::get<1>(res) == ranges::end(c) );
}
{
test_range<X, forward_iterator_wrapper> r(x);
auto res = ranges::find_end(r, y, {}, &X::i, &X::i);
VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(r) );
res = ranges::find_end(r, r, {}, &X::i, &X::i);
VERIFY( std::get<0>(res) == ranges::begin(r)
&& std::get<1>(res) == ranges::end(r) );
}
{
test_range<X, bidirectional_iterator_wrapper> r(x);
auto res = ranges::find_end(r, y, {}, &X::i, &X::i);
VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(r) );
res = ranges::find_end(r, r, {}, &X::i, &X::i);
VERIFY( std::get<0>(res) == ranges::begin(r)
&& std::get<1>(res) == ranges::end(r) );
}
}
void
test02()
{
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {6}, {8}, {11} };
static constexpr X y[] = { {6}, {8} };
static constexpr int z[] = { 2, 8 };
static constexpr int w[] = { 2 };
static_assert(std::get<0>(ranges::find_end(x, y, {}, &X::i, &X::i)) == x+5);
static_assert(std::get<1>(ranges::find_end(x, y, {}, &X::i, &X::i)) == x+7);
static_assert(std::get<0>(ranges::find_end(x, z, {}, &X::i)) == x+8);
static_assert(std::get<1>(ranges::find_end(x, z, {}, &X::i)) == x+8);
static_assert(std::get<0>(ranges::find_end(x, w, {}, &X::i)) == x+1);
static_assert(std::get<1>(ranges::find_end(x, w, {}, &X::i)) == x+2);
static_assert(std::get<0>(ranges::find_end(x, x+6, w, w, {}, &X::i)) == x+6);
static_assert(std::get<1>(ranges::find_end(x, x+6, w, w, {}, &X::i)) == x+6);
static_assert(std::get<0>(ranges::find_end(x, x, w, w+1, {}, &X::i)) == x+0);
static_assert(std::get<1>(ranges::find_end(x, x, w, w+1, {}, &X::i)) == x+0);
}
int
main()
{
test01();
test02();
}
// 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} };
int y[] = { 2, 7, 8, 9 };
X w[] = { {2}, {7}, {8}, {9} };
auto res = ranges::find_first_of(x, x+6, y+1, y+4, {}, &X::i);
VERIFY( res == x+3 );
res = ranges::find_first_of(x, x+6, w, w+4, {}, &X::i, &X::i);
VERIFY( res == x+0 );
res = ranges::find_first_of(x, x+6, y+3, y+4, {}, &X::i);
VERIFY( res == x+6 );
test_container<X, forward_iterator_wrapper> c(x);
test_container<int, forward_iterator_wrapper> d1(y+1, y+4);
auto res2 = ranges::find_first_of(c, d1, {}, &X::i);
VERIFY( res2 != ranges::end(c) && res2->i == 8 );
test_container<X, forward_iterator_wrapper> d2(w+3, w+4);
res2 = ranges::find_first_of(c, d2, {}, &X::i, &X::i);
VERIFY( res2 == ranges::end(c) );
test_range<X, input_iterator_wrapper> r(x);
test_range<int, forward_iterator_wrapper> s1(y+1, y+4);
auto res3 = ranges::find_first_of(r, s1, {}, &X::i);
VERIFY( res3 != ranges::end(r) && res3->i == 8 );
test_range<X, forward_iterator_wrapper> s2(w+3, w+4);
r.bounds.first = x;
res3 = ranges::find_first_of(r, s2, {}, &X::i, &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_first_of(y, y, {}, &Y::j, &Y::i) == y);
static_assert(ranges::find_first_of(y, y, {}, &Y::i, &Y::j) == y+1);
}
int
main()
{
test01();
test02();
}
// 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 );
r.bounds.first = x;
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();
}
// 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 );
r.bounds.first = x;
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();
}
// 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();
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
const int c[6] = { 1, 2, 3, 4, 5, 6 };
{
int x[6];
int a = 1;
VERIFY( ranges::generate(x, [&] { return a++; }) == x+6 );
VERIFY( ranges::equal(x, c) );
}
{
int x[6];
int a = 1;
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::generate(cx, [&] { return a++; }) == cx.end() );
VERIFY( ranges::equal(cx, c) );
}
{
int x[6];
int a = 1;
test_range<int, output_iterator_wrapper> rx(x);
VERIFY( ranges::generate(rx, [&] { return a++; }) == rx.end() );
VERIFY( ranges::equal(x, c) );
}
}
constexpr bool
test02()
{
const int c[6] = { 1, 2, 3, 4, 5, 6 };
int x[6];
int a = 1;
ranges::generate(x, [&] { return a++; });
return ranges::equal(x, c);
}
int
main()
{
test01();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
const int c[6] = { 1, 2, 3, 4, 5, 6 };
{
int x[6] = { 7, 8, 9, 4, 5, 6 };
int a = 1;
VERIFY( ranges::generate_n(x, 3, [&] { return a++; }) == x+3 );
VERIFY( ranges::equal(x, c) );
}
{
int x[6] = { 7, 8, 9, 4, 5, 6 };
int a = 1;
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( *ranges::generate_n(cx.begin(), 3, [&] { return a++; })
== 4 );
VERIFY( ranges::equal(cx, c) );
}
{
int x[6] = { 7, 8, 9, 4, 5, 6 };
int a = 1;
test_range<int, output_iterator_wrapper> rx(x);
ranges::generate_n(ranges::begin(rx), 3, [&] { return a++; });
VERIFY( ranges::equal(x, c) );
}
}
constexpr bool
test02()
{
bool ok = true;
int c[6] = { 1, 2, 3, 4, 5, 6 };
int x[6];
int a = 1;
ranges::generate_n(x, 6, [&] { return a++; });
ok &= ranges::equal(x, c);
ranges::generate_n(c, 0, [] { return -1; });
ok &= ranges::equal(x, c);
ranges::generate_n(c, -2, [] { return -1; });
ok &= ranges::equal(x, c);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
template<template<typename, template<typename> typename> typename container>
void
test01()
{
int x[50];
auto pred = std::greater{};
auto proj = [] (int a) { return -a; };
for (int i = 0; i < 50; i++)
{
std::iota(x, x+50, 1);
container<int, random_access_iterator_wrapper> rx(x);
std::ranlux48_base g(i);
ranges::shuffle(rx, g);
auto iter = ranges::make_heap(rx, pred, proj);
VERIFY( iter == rx.end() );
VERIFY( ranges::is_heap(rx, pred, proj) );
VERIFY( ranges::is_heap_until(rx, pred, proj) == rx.end() );
iter = ranges::pop_heap(rx, pred, proj);
VERIFY( iter == rx.end() );
VERIFY( *(iter-1) == 50 );
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter-1 );
iter = ranges::pop_heap(rx.begin(), iter-1, pred, proj);
VERIFY( iter+1 == rx.end() );
VERIFY( *(iter-1) == 49 );
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter-1 );
*(iter-1) = i;
iter = ranges::push_heap(rx.begin(), iter, pred, proj);
VERIFY( iter+1 == rx.end() );
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter );
*iter = 2*i;
iter = ranges::push_heap(rx.begin(), rx.end(), pred, proj);
VERIFY( iter == rx.end() );
VERIFY( ranges::is_heap_until(rx, pred, proj) == iter );
*(rx.begin()+1) *= -1;
VERIFY( !ranges::is_heap(rx, pred, proj) );
*(rx.begin()+1) *= -1;
VERIFY( ranges::is_heap(rx, pred, proj) );
iter = ranges::sort_heap(rx, pred, proj);
VERIFY( iter == rx.end() );
VERIFY( ranges::is_sorted(rx, pred, proj) );
}
}
constexpr bool
test02()
{
bool ok = true;
int x[] = {1,2,3,4,5};
ranges::make_heap(x);
ranges::pop_heap(x);
x[4] = 7;
ranges::push_heap(x);
ok &= ranges::is_heap(x);
ok &= ranges::is_heap_until(x) == x+5;
ranges::sort_heap(x);
ok &= ranges::equal(x, (int[]){1,2,3,4,7});
return ok;
}
int
main()
{
test01<test_range>();
test01<test_container>();
static_assert(test02());
}
// 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;
void
test01()
{
int x[] = {1,2,3,4,5,6,7};
int y[] = {2,4,6};
test_range<int, input_iterator_wrapper> rx(x), ry(y);
VERIFY( ranges::includes(rx, ry) );
rx.bounds.first = x;
ry.bounds.first = y;
VERIFY( ranges::includes(rx, ry,
ranges::greater{},
std::negate<>{},
std::negate<>{}) );
test_container<int, forward_iterator_wrapper> cx(x), cy(y);
VERIFY( ranges::includes(cx.begin(), cx.end(),
cy.begin(), cy.end(),
{},
[] (int a) { return a+1; },
[] (int a) { return a+2; }) );
VERIFY( ranges::includes(x, x+1, y, y) );
}
constexpr bool
test03()
{
bool ok = true;
ok &= ranges::includes((int[]){1,2,3},
(int[]){1});
ok &= !ranges::includes((int[]){1,2,3},
(int[]){1,2,3,4});
return true;
}
int
main()
{
test01();
static_assert(test03());
}
// 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 <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1,2,3,4,5};
for (int i = 0; i <= 5; i++)
for (int j = 0; j <= 5; j++)
{
std::vector<int> v(x, x+i);
v.insert(v.end(), x, x+j);
ranges::sort(v);
test_range<int, bidirectional_iterator_wrapper> rz(&v[0], &v[0]+i+j);
auto result = ranges::inplace_merge(rz, next(ranges::begin(rz), i));
VERIFY( result == rz.end() );
VERIFY( ranges::is_sorted(rz) );
}
}
void
test02()
{
struct X { int i, j; };
X x[] = { {1, 1}, {3, 4}, {5, 5}, {2, 2}, {2, 3} };
auto comp = ranges::greater{};
auto proj = [] (X a) { return -a.i; };
ranges::inplace_merge(x, x+3, x+5, comp, proj);
VERIFY( ranges::is_sorted(x, {}, &X::i) );
VERIFY( ranges::is_sorted(x, {}, &X::j) );
}
int
main()
{
test01();
test02();
}
// 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;
void
test01()
{
int x[] = {2,4,6,1,3,5};
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::is_partitioned(cx, [] (int a) { return a%2==0; }) );
test_range<int, input_iterator_wrapper> rx(x);
VERIFY( ranges::is_partitioned(rx,
[] (int a) { return a%2==1; },
[] (int a) { return a+1; }) );
}
constexpr bool
test02()
{
int x[] = {1,2,3,4,5,6,1};
return (ranges::is_partitioned(x, x+6, [] (int a) { return a<=2; })
&& !ranges::is_partitioned(x, x+7, [] (int a) { return a<=2; }));
}
int
main()
{
test01();
static_assert(test02());
}
// 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;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
struct X { int i; };
void
test01()
{
int x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
int y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
VERIFY( ranges::is_permutation(x, x+6, y, y+6) );
VERIFY( !ranges::is_permutation(x, x+6, y, y+5) );
test_container<int, forward_iterator_wrapper> cx(x), cy(y), cz(z);
test_range<int, forward_iterator_wrapper> rx(x), ry(y), rz(z);
VERIFY( ranges::is_permutation(cx, ry) );
VERIFY( !ranges::is_permutation(rx, cz) );
VERIFY( ranges::is_permutation(rx, cy) );
VERIFY( !ranges::is_permutation(cx, rz) );
}
void
test02()
{
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
static constexpr X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
static_assert(ranges::is_permutation(x, y, {}, &X::i, &X::i));
static_assert(!ranges::is_permutation(x, z, {}, &X::i));
static_assert(!ranges::is_permutation(z, y, {}, {}, &X::i));
}
void
test03()
{
int x[] = { 1, 2, 3, 4 };
int y[] = { 1, 2, 3, 3 };
test_container<int, bidirectional_iterator_wrapper> cx(x);
do
do
{
VERIFY( ranges::is_permutation(cx, x) );
VERIFY( !ranges::is_permutation(y, cx) );
} while (std::next_permutation(y, y+4));
while (std::next_permutation(std::begin(cx), std::end(cx)));
}
int
main()
{
test01();
test02();
test03();
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {3,4,5,1};
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::is_sorted(cx.begin(), ranges::next(cx.begin(), 3)) );
VERIFY( !ranges::is_sorted(cx) );
VERIFY( !ranges::is_sorted(cx, ranges::greater{}) );
VERIFY( ranges::is_sorted(cx, {}, [] (int a) { return 0; }) );
}
void
test02()
{
int x[] = {1,2,3,4,5};
test_range<int, forward_iterator_wrapper> rx(x);
VERIFY( ranges::is_sorted(rx) );
VERIFY( !ranges::is_sorted(ranges::begin(rx),
next(ranges::begin(rx), 2),
ranges::greater{}) );
}
constexpr bool
test03()
{
int x[] = { 1,2 };
return (ranges::is_sorted(x)
&& ranges::is_sorted(x, x) );
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {3,4,5,1};
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::is_sorted_until(cx.begin(),
ranges::next(cx.begin(), 3))
== ranges::next(cx.begin(), 3) );
VERIFY( ranges::is_sorted_until(cx) == ranges::next(cx.begin(), 3) );
VERIFY( ranges::is_sorted_until(cx, ranges::greater{})
== ranges::next(cx.begin(), 1) );
VERIFY( ranges::is_sorted_until(cx, {}, [] (int a) { return 0; })
== cx.end() );
}
void
test02()
{
int x[] = {1,2,3,4,5};
test_range<int, forward_iterator_wrapper> rx(x);
VERIFY( ranges::is_sorted_until(rx) == ranges::end(rx) );
VERIFY( ranges::is_sorted_until(ranges::begin(rx),
next(ranges::begin(rx), 2),
ranges::greater{})
== next(ranges::begin(rx), 1) );
}
constexpr bool
test03()
{
int x[] = { 1,2 };
return (ranges::is_sorted_until(x) == x+2
&& ranges::is_sorted_until(x, x) == x );
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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;
void
test01()
{
int x[] = {1, 2, 3, 4, 5};
char y[] = {1, 2, 3, 5};
long z[] = {1, 2, 3, 4, 5, 6};
{
test_range<int, input_iterator_wrapper> rx(x);
test_range<char, input_iterator_wrapper> ry(y);
test_range<long, input_iterator_wrapper> rz(z);
VERIFY( ranges::lexicographical_compare(rx, ry) );
rx.bounds.first = x;
ry.bounds.first = y;
VERIFY( !ranges::lexicographical_compare(ry, rx) );
}
test_range<int, forward_iterator_wrapper> rx(x);
test_range<char, forward_iterator_wrapper> ry(y);
test_range<long, forward_iterator_wrapper> rz(z);
VERIFY( ranges::lexicographical_compare(rx, rz) );
VERIFY( !ranges::lexicographical_compare(rz, rx) );
VERIFY( !ranges::lexicographical_compare(rx, rx) );
VERIFY( ranges::lexicographical_compare(rx, rx, {}, std::negate<>{}) );
VERIFY( ranges::lexicographical_compare(rx, rx, std::greater{},
{}, std::negate<>{}) );
VERIFY( !ranges::lexicographical_compare(rx, ry, {},
std::negate<>{},
std::negate<>{}) );
VERIFY( ranges::lexicographical_compare(ry, rx, {},
std::negate<>{},
std::negate<>{}) );
VERIFY( ranges::lexicographical_compare(rx, rz, ranges::greater{}) );
VERIFY( !ranges::lexicographical_compare(rz, rx, ranges::greater{}) );
VERIFY( ranges::lexicographical_compare(rx, ry, ranges::greater{},
std::negate<>{},
std::negate<>{}) );
VERIFY( !ranges::lexicographical_compare(ry, rx, ranges::greater{},
std::negate<>{},
std::negate<>{}) );
}
void
test02()
{
int x[] = {1, 2, 3, 4, 5};
int y[] = {1, 2, 3, 5};
int z[] = {1, 2, 3, 4, 5, 6};
VERIFY( ranges::lexicographical_compare(x, y) );
VERIFY( !ranges::lexicographical_compare(y, x) );
VERIFY( ranges::lexicographical_compare(x, z) );
VERIFY( !ranges::lexicographical_compare(z, x) );
VERIFY( !ranges::lexicographical_compare(x, x) );
VERIFY( !ranges::lexicographical_compare(x, y, {},
std::negate<>{},
std::negate<>{}) );
VERIFY( ranges::lexicographical_compare(y, x, {},
std::negate<>{},
std::negate<>{}) );
VERIFY( ranges::lexicographical_compare(x, z, ranges::greater{}) );
VERIFY( !ranges::lexicographical_compare(z, x, ranges::greater{}) );
VERIFY( ranges::lexicographical_compare(x, y, ranges::greater{},
std::negate<>{},
std::negate<>{}) );
VERIFY( !ranges::lexicographical_compare(y, x, ranges::greater{},
std::negate<>{},
std::negate<>{}) );
}
void
test03()
{
int x[] = {1, 2, 3, 4, 5};
int y[] = {1, 2, 5, 3};
int z[] = {1, 2, 3, 5};
do
{
VERIFY( ranges::lexicographical_compare(x, y) );
VERIFY( !ranges::lexicographical_compare(x, y, ranges::greater{}) );
VERIFY( !ranges::lexicographical_compare(y, x) );
VERIFY( ranges::lexicographical_compare(y, x, ranges::greater{}) );
test_container<int, forward_iterator_wrapper> cy(y);
VERIFY( ranges::lexicographical_compare(x, cy) );
VERIFY( !ranges::lexicographical_compare(x, cy, ranges::greater{}) );
VERIFY( !ranges::lexicographical_compare(cy, x) );
VERIFY( ranges::lexicographical_compare(cy, x, ranges::greater{}) );
test_container<int, forward_iterator_wrapper> cz(z);
VERIFY( ranges::lexicographical_compare(cz.begin(), cz.end(),
cy.begin(), cy.end()) );
VERIFY( !ranges::lexicographical_compare(cy.begin(), cy.end(),
cz.begin(), cz.end()) );
std::vector<int> vx(x, x+5), vy(y, y+5);
VERIFY( ranges::lexicographical_compare(vx, vy) );
VERIFY( !ranges::lexicographical_compare(vx, vy, ranges::greater{}) );
VERIFY( !ranges::lexicographical_compare(vy, vx) );
VERIFY( ranges::lexicographical_compare(vy, vx, ranges::greater{}) );
} while (ranges::next_permutation(y).found);
}
constexpr bool
test04()
{
int x[] = {1};
int y[] = {1};
return (ranges::lexicographical_compare((int[]){1,2,3,5},
(int[]){1,2,4})
&& !ranges::lexicographical_compare(x, x, y, y));
}
int
main()
{
test01();
test02();
test03();
static_assert(test04());
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1, 2, 3, 4, 5, 5, 6, 7};
for (unsigned i = 0; i < 5; i++)
for (unsigned j = 5; j < 8; j++)
{
test_container<int, forward_iterator_wrapper> cx(x);
auto result = ranges::lower_bound(std::next(cx.begin(), i),
std::next(cx.begin(), j),
4, {}, [] (int a) { return a-1; });
VERIFY( result.ptr == x+4 );
}
ranges::reverse(x);
test_range<int, forward_iterator_wrapper> rx(x);
auto result = ranges::lower_bound(rx, 5, ranges::greater{},
[] (int a) { return a+1; });
VERIFY( result.ptr == x+4 );
}
constexpr bool
test02()
{
int x[] = {1, 2, 3, 4, 5};
return (ranges::lower_bound(x, 6) == x+5
&& ranges::lower_bound(x, x, 6) == x
&& ranges::lower_bound(x, 1) == x);
}
int
main()
{
test01();
static_assert(test02());
}
// 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;
namespace ranges = std::ranges;
struct X
{
int i, j;
};
void
test01()
{
VERIFY( ranges::max(1, 2) == 2);
VERIFY( ranges::max(2, 1) == 2);
VERIFY( ranges::max(1, 2, ranges::greater{}) == 1);
VERIFY( ranges::max(1, 2, ranges::greater{}, std::negate<>{}) == 2);
VERIFY( ranges::max(1, 2, {}, std::negate<>{}) == 1);
VERIFY( ranges::max(X{1,2}, X{1,3}, {}, &X::i).j == 2 );
}
void
test02()
{
int x[] = {1,2,3,4};
do
{
test_range<int, input_iterator_wrapper> cx(x);
VERIFY( ranges::max(cx) == 4 );
cx.bounds.first = x;
VERIFY( ranges::max(cx, ranges::greater{}) == 1 );
cx.bounds.first = x;
VERIFY( ranges::max(cx, {}, std::negate<>{}) == 1);
cx.bounds.first = x;
VERIFY( ranges::max(cx, ranges::greater{}, std::negate<>{}) == 4 );
} while (ranges::next_permutation(x).found);
constexpr X y[] = {{0,5},{1,2},{1,3}};
static_assert(ranges::max(y, {}, &X::i).j == 2);
}
void
test03()
{
VERIFY( ranges::max({2,3,1,4}) == 4 );
VERIFY( ranges::max({2,3,1,4}, ranges::greater{}) == 1 );
VERIFY( ranges::max({2,3,1,4}, {}, std::negate<>{}) == 1 );
VERIFY( ranges::max({2,3,1,4}, ranges::greater{}, std::negate<>{}) == 4 );
}
int
main()
{
test01();
test02();
test03();
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i, j;
};
void
test01()
{
int x[] = {1,2,3,4};
do
{
test_range<int, forward_iterator_wrapper> cx(x);
VERIFY( *ranges::max_element(cx) == 4 );
VERIFY( *ranges::max_element(cx, ranges::greater{}) == 1 );
VERIFY( *ranges::max_element(cx, {}, std::negate<>{}) == 1);
VERIFY( *ranges::max_element(cx, ranges::greater{}, std::negate<>{}) == 4 );
} while (ranges::next_permutation(x).found);
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::max_element(cx.begin(), cx.begin()) == cx.begin() );
constexpr X y[] = {{0,5},{1,2},{1,3}};
static_assert(ranges::max_element(y, y+3, {}, &X::i)->j == 2);
}
int
main()
{
test01();
}
// 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 <vector>
#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::output_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1,2,3,4,5};
for (int i = 0; i <= 5; i++)
for (int j = 0; j <= 5; j++)
{
int z[10];
test_range<int, input_iterator_wrapper> rx(x, x+i), ry(x, x+j);
test_range<int, output_iterator_wrapper> rz(z, z+i+j);
auto [in1,in2,out] = ranges::merge(rx, ry, rz.begin());
VERIFY( in1 == rx.end() );
VERIFY( in2 == ry.end() );
VERIFY( out == rz.end() );
std::vector<int> v(x, x+i);
v.insert(v.end(), x, x+j);
ranges::sort(v);
VERIFY( ranges::equal(v.begin(), v.end(), z, z+i+j) );
}
}
constexpr bool
test02()
{
int x[] = {-1,-3,-5};
int y[] = {2,4,6};
int z[6];
ranges::merge(x, x+3, y, y+3, z,
ranges::greater{}, {}, [] (int a) { return -a; });
const int w[6] = {-1, 2, -3, 4, -5, 6};
return ranges::equal(w, z);
}
int
main()
{
test01();
static_assert(test02());
}
// 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;
namespace ranges = std::ranges;
struct X
{
int i, j;
};
void
test01()
{
VERIFY( ranges::min(1, 2) == 1);
VERIFY( ranges::min(2, 1) == 1);
VERIFY( ranges::min(1, 2, ranges::greater{}) == 2);
VERIFY( ranges::min(1, 2, ranges::greater{}, std::negate<>{}) == 1);
VERIFY( ranges::min(1, 2, {}, std::negate<>{}) == 2);
VERIFY( ranges::min(X{1,2}, X{1,3}, {}, &X::i).j == 2 );
}
void
test02()
{
int x[] = {1,2,3,4};
do
{
test_range<int, input_iterator_wrapper> cx(x);
VERIFY( ranges::min(cx) == 1 );
cx.bounds.first = x;
VERIFY( ranges::min(cx, ranges::greater{}) == 4 );
cx.bounds.first = x;
VERIFY( ranges::min(cx, {}, std::negate<>{}) == 4);
cx.bounds.first = x;
VERIFY( ranges::min(cx, ranges::greater{}, std::negate<>{}) == 1 );
} while (ranges::next_permutation(x).found);
constexpr X y[] = {{5,0},{1,2},{1,3}};
static_assert(ranges::min(y, {}, &X::i).j == 2);
}
void
test03()
{
VERIFY( ranges::min({2,3,1,4}) == 1 );
VERIFY( ranges::min({2,3,1,4}, ranges::greater{}) == 4 );
VERIFY( ranges::min({2,3,1,4}, {}, std::negate<>{}) == 4 );
VERIFY( ranges::min({2,3,1,4}, ranges::greater{}, std::negate<>{}) == 1 );
}
int
main()
{
test01();
test02();
test03();
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i, j;
};
void
test01()
{
int x[] = {1,2,3,4};
do
{
test_range<int, forward_iterator_wrapper> cx(x);
VERIFY( *ranges::min_element(cx) == 1 );
VERIFY( *ranges::min_element(cx, ranges::greater{}) == 4 );
VERIFY( *ranges::min_element(cx, {}, std::negate<>{}) == 4);
VERIFY( *ranges::min_element(cx, ranges::greater{}, std::negate<>{}) == 1 );
} while (ranges::next_permutation(x).found);
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::min_element(cx.begin(), cx.begin()) == cx.begin() );
constexpr X y[] = {{5,0},{1,2},{1,3}};
static_assert(ranges::min_element(y, y+3, {}, &X::i)->j == 2);
}
int
main()
{
test01();
}
// 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;
namespace ranges = std::ranges;
template<typename T1, typename T2>
constexpr bool
operator==(const ranges::minmax_result<T1>& lhs,
const ranges::minmax_result<T2>& rhs)
{
return (lhs.min == rhs.min
&& rhs.max == rhs.max);
}
struct X
{
int i, j;
};
using res_t = ranges::minmax_result<int>;
void
test01()
{
VERIFY( ranges::minmax(1, 2) == res_t(1,2) );
VERIFY( ranges::minmax(2, 1) == res_t(1,2) );
VERIFY( ranges::minmax(1, 2, ranges::greater{}) == res_t(2,1) );
VERIFY( ranges::minmax(1, 2, ranges::greater{}, std::negate<>{}) == res_t(1,2) );
VERIFY( ranges::minmax(1, 2, {}, std::negate<>{}) == res_t(2,1) );
VERIFY( ranges::minmax(X{1,2}, X{1,3}, {}, &X::i).min.j == 2 );
VERIFY( ranges::minmax(X{1,2}, X{1,3}, {}, &X::i).max.j == 3 );
}
void
test02()
{
int x[] = {1,2,3,4};
do
{
test_range<int, input_iterator_wrapper> cx(x);
VERIFY( ranges::minmax(cx) == res_t(1,4) );
cx.bounds.first = x;
VERIFY( ranges::minmax(cx, ranges::greater{}) == res_t(4,1) );
cx.bounds.first = x;
VERIFY( ranges::minmax(cx, {}, std::negate<>{}) == res_t(4,1));
cx.bounds.first = x;
VERIFY( ranges::minmax(cx, ranges::greater{}, std::negate<>{})
== res_t(1,4) );
} while (ranges::next_permutation(x).found);
constexpr X y[] = {{1,5},{1,2},{1,3}};
static_assert(ranges::minmax(y, {}, &X::i).min.j == 5);
static_assert(ranges::minmax(y, {}, &X::i).max.j == 3);
}
void
test03()
{
VERIFY( ranges::minmax({2,3,1,4}) == res_t(1,4) );
VERIFY( ranges::minmax({2,3,1,4}, ranges::greater{}) == res_t(4,1) );
VERIFY( ranges::minmax({2,3,1,4}, {}, std::negate<>{}) == res_t(4,1) );
VERIFY( ranges::minmax({2,3,1,4}, ranges::greater{}, std::negate<>{})
== res_t(1,4) );
}
int
main()
{
test01();
test02();
test03();
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i, j;
};
void
test01()
{
int x[] = {1,2,3,4};
do
{
test_range<int, forward_iterator_wrapper> cx(x);
VERIFY( *ranges::minmax_element(cx).min == 1 );
VERIFY( *ranges::minmax_element(cx).max == 4 );
VERIFY( *ranges::minmax_element(cx, ranges::greater{}).min == 4 );
VERIFY( *ranges::minmax_element(cx, ranges::greater{}).max == 1 );
VERIFY( *ranges::minmax_element(cx, {}, std::negate<>{}).min == 4);
VERIFY( *ranges::minmax_element(cx, {}, std::negate<>{}).max == 1);
VERIFY( *ranges::minmax_element(cx, ranges::greater{}, std::negate<>{}).min
== 1 );
VERIFY( *ranges::minmax_element(cx, ranges::greater{}, std::negate<>{}).max
== 4 );
} while (ranges::next_permutation(x).found);
test_container<int, forward_iterator_wrapper> cx(x);
VERIFY( ranges::minmax_element(cx.begin(), cx.begin()).min == cx.begin() );
VERIFY( ranges::minmax_element(cx.begin(), cx.begin()).max == cx.begin() );
constexpr X y[] = {{1,5},{1,2},{1,3}};
static_assert(ranges::minmax_element(y, y+3, {}, &X::i).min->j == 5);
static_assert(ranges::minmax_element(y, y+3, {}, &X::i).max->j == 3);
}
int
main()
{
test01();
}
// 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 xa[] = { {1}, {2}, {3}, {4}, {5}, {6} };
X xb[] = { {1}, {2}, {3}, {3}, {5}, {6} };
auto res = ranges::mismatch(xa, xa+6, xb, xb+6, {}, &X::i, &X::i);
VERIFY( res.in1 == xa+3 && res.in2 == xb+3 );
test_container<X, forward_iterator_wrapper> ca(xa);
test_container<X, forward_iterator_wrapper> cb(xb);
auto res2 = ranges::mismatch(ca, cb, {}, &X::i, &X::i);
VERIFY( res2.in1->i == 4 && res2.in2->i == 3 );
res2 = ranges::mismatch(ca, ca, {}, &X::i, &X::i);
VERIFY( res2.in1 == ranges::end(ca) && res2.in2 == ranges::end(ca) );
test_range<X, input_iterator_wrapper> ra(xa);
test_range<X, input_iterator_wrapper> rb(xb);
auto res3 = ranges::mismatch(ra, rb, {}, &X::i, &X::i);
VERIFY( res3.in1->i == 4 && res3.in2->i == 3 );
test_range<X, input_iterator_wrapper> ra2(xa);
ra.bounds.first = xa;
res3 = ranges::mismatch(ra, ra2, {}, &X::i, &X::i);
VERIFY( res3.in1 == ranges::end(ra) && res3.in2 == ranges::end(ra2) );
}
struct Y { int i; int j; };
void
test02()
{
static constexpr Y ya[] = { {1,2}, {2,4}, {3,6}, {1,6} };
static constexpr Y yb[] = { {2,1}, {4,2}, {4,2}, {7,1} };
static_assert(ranges::mismatch(ya, yb, {}, &Y::i, &Y::j).in1 == ya+2);
static_assert(ranges::mismatch(ya, yb, {}, &Y::i, &Y::j).in2 == yb+2);
}
int
main()
{
test01();
test02();
}
// 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 <vector>
#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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
int moved = 0;
constexpr X() : i(0) { }
constexpr X(int a) : i(a) { }
constexpr X(const X&) = delete;
constexpr X& operator=(const X&) = delete;
constexpr X(X&& other)
{
*this = std::move(other);
}
constexpr X&
operator=(X&& other)
{
other.moved++;
i = other.i;
return *this;
}
friend constexpr bool
operator==(const X& a, const X& b)
{ return a.i == b.i; }
};
void
test01()
{
{
X x[7] = { 1, 2, 3, 4, 5, 6, 7 };
X y[7] = { 0, 0, 0, 0, 0, 0, 0 };
X z[7] = { 1, 2, 3, 4, 5, 6, 7 };
auto [in, out] = ranges::move(x, y);
VERIFY( ranges::equal(x, y) && in == x+7 && out == y+7 );
VERIFY( ranges::equal(x, z) );
}
{
int x[3] = { 1, 2, 3 };
char y[4] = { 0 };
int z[3] = { 1, 2, 3 };
test_container<int, forward_iterator_wrapper> cx(x);
test_container<char, forward_iterator_wrapper> cy(y);
auto [in, out] = ranges::move(cx, cy.begin());
VERIFY( ranges::equal(x, x+3, y, y+3) && in.ptr == x+3 && out.ptr == y+3 );
VERIFY( ranges::equal(x, z) );
}
{
char x[3] = { 1, 2, 3 };
int y[4] = { 0 };
int z[3] = { 1, 2, 3 };
test_range<char, input_iterator_wrapper> cx(x);
test_range<int, output_iterator_wrapper> cy(y);
auto [in, out] = ranges::move(cx, ranges::begin(cy));
VERIFY( ranges::equal(x, x+3, y, y+3) && in.ptr == x+3 && out.ptr == y+3 );
VERIFY( ranges::equal(x, z) );
}
{
std::vector<char> x= {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::move(x, ranges::begin(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::move(x, ranges::begin(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::move(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.end()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<char> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::move(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.end()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
}
void
test02()
{
X x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
X z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::move(x, y);
VERIFY( ranges::equal(x, x+5, y, y+5) );
VERIFY( in == x+5 );
VERIFY( out == y+5 );
VERIFY( y[5].i == 2 );
VERIFY( ranges::equal(x, z) );
VERIFY( ranges::count(x, 1, &X::moved) == 5 );
VERIFY( ranges::count(y, 0, &X::moved) == 6 );
}
constexpr bool
test03()
{
bool ok = true;
X x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
X z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::move(x, y);
ok &= ranges::equal(x, x+5, y, y+5);
ok &= (in == x+5);
ok &= (out == y+5);
ok &= (y[5].i == 2);
ok &= ranges::equal(x, z);
ok &= ranges::count(x, 1, &X::moved) == 5;
ok &= ranges::count(y, 0, &X::moved) == 6;
return ok;
}
void
test04()
{
X x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
X z[] = { {2}, {2}, {6}, {8}, {10} };
test_range<X, input_iterator_wrapper> rx(x);
auto [in, out] = ranges::move(std::move_iterator{ranges::begin(rx)},
std::move_sentinel{ranges::end(rx)},
ranges::begin(y));
VERIFY( ranges::equal(x, x+5, y, y+5) );
VERIFY( std::move(in).base().ptr == x+5 );
VERIFY( out == y+5 );
VERIFY( y[5].i == 2 );
VERIFY( ranges::equal(x, z) );
VERIFY( ranges::count(x, 1, &X::moved) == 5 );
VERIFY( ranges::count(y, 0, &X::moved) == 6 );
}
int
main()
{
test01();
test02();
static_assert(test03());
test04();
}
// 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 <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
int moved = 0;
constexpr X() : i(0) { }
constexpr X(int a) : i(a) { }
constexpr X(const X&) = delete;
constexpr X& operator=(const X&) = delete;
constexpr X(X&& other)
{
*this = std::move(other);
}
constexpr X&
operator=(X&& other)
{
other.moved++;
i = other.i;
return *this;
}
friend constexpr bool
operator==(const X& a, const X& b)
{ return a.i == b.i; }
};
void
test01()
{
{
X x[7] = { 1, 2, 3, 4, 5, 6, 7 };
X y[7] = { 0, 0, 0, 0, 0, 0, 0 };
X z[7] = { 1, 2, 3, 4, 5, 6, 7 };
auto [in, out] = ranges::move_backward(x, y+7);
VERIFY( ranges::equal(x, y) && in == x+7 && out == y );
VERIFY( ranges::equal(x, z) );
}
{
int x[3] = { 1, 2, 3 };
char y[4] = { 0 };
int z[3] = { 1, 2, 3 };
test_container<int, bidirectional_iterator_wrapper> cx(x);
test_container<char, bidirectional_iterator_wrapper> cy(y);
auto [in, out] = ranges::move_backward(cx, cy.end());
VERIFY( ranges::equal(x, x+3, y+1, y+4) && in.ptr == x+3 && out.ptr == y+1 );
VERIFY( ranges::equal(x, z) );
}
{
std::vector<char> x= {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::move_backward(x, ranges::end(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in, out] = ranges::move_backward(x, ranges::end(y));
VERIFY( in.base() == x.data()+3 );
VERIFY( out.base() == y.data() );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<int> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::move_backward(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.begin()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
{
std::vector<char> x = {1,2,3};
std::vector<int> y(3);
const int z[3] = { 1, 2, 3 };
auto [in,out] = ranges::move_backward(make_reverse_iterator(x.end()),
make_reverse_iterator(x.begin()),
make_reverse_iterator(y.begin()));
VERIFY( in.base().base() == x.data()+3 );
VERIFY( out.base().base() == y.data()+3 );
VERIFY( ranges::equal(y, z) && ranges::equal(x, z) );
}
}
void
test02()
{
X x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
const X z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::move_backward(x, ranges::end(y));
VERIFY( ranges::equal(x, x+5, y+1, y+6) );
VERIFY( in == x+5 );
VERIFY( out == y+1 );
VERIFY( y[0].i == 2 );
VERIFY( ranges::equal(x, z) );
VERIFY( ranges::count(x, 1, &X::moved) == 5 );
VERIFY( ranges::count(y, 0, &X::moved) == 6 );
}
constexpr bool
test03()
{
bool ok = true;
X x[] = { {2}, {2}, {6}, {8}, {10} };
X y[] = { {2}, {6}, {8}, {10}, {11}, {2} };
const X z[] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::move_backward(x, ranges::end(y));
ok &= ranges::equal(x, x+5, y+1, y+6);
ok &= (in == x+5);
ok &= (out == y+1);
ok &= (y[0].i == 2);
ok &= ranges::equal(x, z);
ok &= ranges::count(x, 1, &X::moved) == 5;
ok &= ranges::count(y, 0, &X::moved) == 6;
return ok;
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1, 2, 3, 4, 5};
int y[] = {1, 2, 3, 4, 5};
for (int i = 0; i <= 5; i++)
{
test_container<int, bidirectional_iterator_wrapper> cx(x, x+i);
test_container<int, bidirectional_iterator_wrapper> cy(y, y+i);
for (int j = 0; ; j++)
{
auto found1 = std::next_permutation(cx.begin(), cx.end());
auto [found2,last] = ranges::next_permutation(cy.begin(), cy.end());
VERIFY( found1 == found2 );
VERIFY( ranges::equal(cx, cy) );
if (!found2)
break;
}
}
}
void
test02()
{
int x[] = {5, 4, 3, 2, 1};
test_range<int, bidirectional_iterator_wrapper> rx(x);
auto [found,last] = ranges::next_permutation(rx, ranges::greater{});
VERIFY( found && last == rx.end() );
VERIFY( last == rx.end() );
VERIFY( ranges::equal(rx, (int[]){5,4,3,1,2}) );
ranges::next_permutation(rx, {}, [] (int a) { return -a; });
VERIFY( ranges::equal(rx, (int[]){5,4,2,3,1}) );
VERIFY( !ranges::next_permutation(x, x).found );
VERIFY( !ranges::next_permutation(x, x+1).found );
}
constexpr bool
test03()
{
int x[] = {1,2,3};
ranges::next_permutation(x);
return ranges::equal(x, (int[]){1,3,2});
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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();
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[50];
std::iota(x, x+50, 0);
auto pred = std::greater{};
auto proj = [] (int a) { return -a; };
for (int i = 0; i < 50; i++)
{
test_range<int, random_access_iterator_wrapper> rx(x);
std::ranlux48_base g(i);
ranges::shuffle(rx, g);
auto result = ranges::nth_element(rx, rx.begin()+i, pred, proj);
VERIFY( result == rx.end() );
VERIFY( x[i] == i );
for (int j = 0; j < i; j++)
for (int k = i; k < 50; k++)
VERIFY( !pred(proj(x[k]), proj(x[j])) );
result = ranges::nth_element(rx, rx.begin()+i, pred);
VERIFY( result == rx.end() );
VERIFY( x[i] == 49-i );
for (int j = 0; j < i; j++)
for (int k = i; k < 50; k++)
VERIFY( !pred(x[k], x[j]) );
}
}
constexpr bool
test02()
{
int x[] = {5,2,1,3,4};
ranges::nth_element(x, x+3);
return x[3] == 4;
}
int
main()
{
test01();
static_assert(test02());
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
for (unsigned size = 0; size < 50; ++size)
{
std::vector<int> vref(size);
std::iota(vref.begin(), vref.end(), 0);
std::vector<int> v1(vref), v2(vref);
test_container<int, random_access_iterator_wrapper> c
= {&v1[0], &v1[0] + size};
test_range<int, random_access_iterator_wrapper> r
= {&v2[0], &v2[0] + size};
std::ranlux48_base g1(size), g2(size + 1);
ranges::shuffle(c, g1);
ranges::shuffle(ranges::begin(r), ranges::end(r), g2);
for (unsigned middle = 0; middle < std::min(size, 10U); ++middle)
{
auto res1 = ranges::partial_sort(c.begin(), c.begin()+middle, c.end(),
{}, std::negate<>{});
VERIFY( res1 == c.end() );
auto res2 = ranges::partial_sort(r,
ranges::begin(r)+middle,
ranges::greater{});
VERIFY( res2 == ranges::end(r) );
VERIFY( ranges::equal(c.begin(), c.begin()+middle,
r.begin(), r.begin()+middle) );
VERIFY( ranges::equal(c.begin(), c.begin()+middle,
vref.rbegin(), vref.rbegin()+middle) );
}
}
}
constexpr bool
test02()
{
int x[] = { 5,4,1,3,2 };
const int y[] = { 1,2,3 };
ranges::partial_sort(x, x+3, x+5);
return ranges::equal(x, x+3, y, y+3);
}
int
main()
{
test01();
static_assert(test02());
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <vector>
#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;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
for (unsigned size = 0; size < 50; ++size)
{
std::vector<int> vref(size);
std::iota(vref.begin(), vref.end(), 0);
std::vector<int> v1(vref), v2(vref);
std::ranlux48_base g1(size), g2(size + 1);
ranges::shuffle(v1, g1);
ranges::shuffle(v2, g2);
for (unsigned middle = 0; middle < 10; ++middle)
{
test_container<int, forward_iterator_wrapper> c
= {&v1[0], &v1[0] + size};
test_range<int, input_iterator_wrapper> r
= {&v2[0], &v2[0] + size};
std::vector<int> o1(middle), o2(middle);
test_range<int, random_access_iterator_wrapper> w1
= {&o1[0], &o1[0]+middle};
test_range<int, random_access_iterator_wrapper> w2
= {&o2[0], &o2[0]+middle};
auto [in1, out1] = ranges::partial_sort_copy(c.begin(), c.end(),
w1.begin(), w1.end(),
{},
std::negate<>{},
std::negate<>{});
VERIFY( in1 == c.end() );
VERIFY( out1 == w1.begin() + std::min(size, middle) );
auto [in2,out2] = ranges::partial_sort_copy(r, w2, ranges::greater{});
VERIFY( in2 == ranges::end(r) );
VERIFY( out2 == w2.begin() + std::min(size, middle) );
VERIFY( ranges::equal(w1.begin(), out1, w2.begin(), out2) );
VERIFY( ranges::equal(w1.begin(), out1,
vref.rbegin(),
vref.rbegin()+(out1-w1.begin())) );
}
}
}
constexpr bool
test02()
{
int x[] = { 5,4,1,3,2 };
int w[3];
const int y[] = { 1,2,3 };
ranges::partial_sort_copy(x, x+5, w, w+3);
return ranges::equal(w, y);
}
int
main()
{
test01();
static_assert(test02());
}
// 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::forward_iterator_wrapper;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
template<template<typename> typename wrapper>
void
test01()
{
{
int x[] = {1,2,3,4,5,6,7,8,9,10};
test_container<int, wrapper> cx(x);
auto range = ranges::partition(cx, [] (int a) { return a%2==0; });
VERIFY( range.begin().ptr == x+5 );
VERIFY( range.end().ptr == x+10 );
VERIFY( ranges::is_partitioned(cx, [] (int a) { return a%2==0; }) );
}
{
int x[] = {1,2,3,4,5,6,7,8};
test_range<int, wrapper> rx(x);
auto range = ranges::partition(rx,
[] (int a) { return a%2==0; },
[] (int a) { return a+1; });
VERIFY( range.begin().ptr == x+4 );
VERIFY( range.end().ptr == x+8 );
VERIFY( ranges::is_partitioned(rx, [] (int a) { return a%2==1; }) );
}
}
constexpr bool
test02()
{
int x[] = {1,2,3,4,5,6,7,8,9,10};
auto range = ranges::partition(x, x+9, [] (int a) { return a < 100; });
return (range.begin() == x+9 && range.end() == x+9);
}
int
main()
{
test01<forward_iterator_wrapper>();
test01<bidirectional_iterator_wrapper>();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
{
int x[] = {1,2,3,4,5,6,7,8,9,10,11};
int y[5], z[6];
test_container<int, forward_iterator_wrapper> cx(x);
test_container<int, forward_iterator_wrapper> cy(y), cz(z);
auto pred = [] (int a) { return a%2==0; };
auto [in,out_true,out_false]
= ranges::partition_copy(cx, cy.begin(), cz.begin(), pred);
VERIFY( in.ptr == x+11 );
VERIFY( out_true.ptr == y+5 );
VERIFY( out_false.ptr == z+6 );
VERIFY( ranges::all_of(cy, pred) );
VERIFY( ranges::none_of(cz, pred) );
}
{
int x[] = {1,2,3,4,5,6,7,8,9,10,11};
int y[6], z[5];
test_range<int, input_iterator_wrapper> cx(x);
test_range<int, output_iterator_wrapper> cy(y), cz(z);
auto pred = [] (int a) { return a%2==0; };
auto proj = [] (int a) { return a+1; };
auto [in,out_true,out_false]
= ranges::partition_copy(cx, cy.begin(), cz.begin(), pred, proj);
VERIFY( in.ptr == x+11 );
VERIFY( out_true.ptr == y+6 );
VERIFY( out_false.ptr == z+5 );
VERIFY( ranges::none_of(y, pred) );
VERIFY( ranges::all_of(z, pred) );
}
}
constexpr bool
test02()
{
int x[] = {1,2,3,4,5,6,7,8,9,10};
auto range = ranges::partition(x, x+9, [] (int a) { return a < 100; });
return (range.begin() == x+9 && range.end() == x+9);
}
int
main()
{
test01();
static_assert(test02());
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
for (int k = 1; k <= 7; k++)
{
int x[] = {1,2,3,4,5,6,7};
test_container<int, forward_iterator_wrapper> cx(x);
auto pred = [&] (int a) { return a <= k; };
auto middle = ranges::partition_point(cx, pred);
VERIFY( middle.ptr == x+k );
}
for (int k = 1; k <= 8; k++)
{
int x[] = {1,2,3,4,5,6,7,8};
test_range<int, forward_iterator_wrapper> rx(x);
auto pred = [&] (int a) { return a > -k; };
auto proj = [] (int a) { return -a; };
auto middle = ranges::partition_point(rx, pred, proj);
VERIFY( middle.ptr == x+k-1 );
}
}
constexpr bool
test02()
{
int x[] = {1,2,3,4,5};
return (ranges::partition_point(x, x+5, [] (int a) { return a < 6; }) == x+5
&& ranges::partition_point(x, x+5, [] (int a) { return a < 0; }) == x);
}
int
main()
{
test01();
static_assert(test02());
}
// 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::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {5, 4, 3, 2, 1};
int y[] = {5, 4, 3, 2, 1};
for (int i = 0; i <= 5; i++)
{
test_container<int, bidirectional_iterator_wrapper> cx(x, x+i);
test_container<int, bidirectional_iterator_wrapper> cy(y, y+i);
for (int j = 0; ; j++)
{
auto found1 = std::prev_permutation(cx.begin(), cx.end());
auto [found2,last] = ranges::prev_permutation(cy.begin(), cy.end());
VERIFY( found1 == found2 );
VERIFY( ranges::equal(cx, cy) );
if (!found2)
break;
}
}
}
void
test02()
{
int x[] = {1, 2, 3, 4, 5};
test_range<int, bidirectional_iterator_wrapper> rx(x);
auto [found,last] = ranges::prev_permutation(rx, ranges::greater{});
VERIFY( found && last == rx.end() );
VERIFY( last == rx.end() );
VERIFY( ranges::equal(rx, (int[]){1,2,3,5,4}) );
ranges::prev_permutation(rx, {}, [] (int a) { return -a; });
VERIFY( ranges::equal(rx, (int[]){1,2,4,3,5}) );
VERIFY( !ranges::prev_permutation(x, x).found );
VERIFY( !ranges::prev_permutation(x, x+1).found );
}
constexpr bool
test03()
{
int x[] = {3,2,1};
ranges::prev_permutation(x);
return ranges::equal(x, (int[]){3,1,2});
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
};
void
test01()
{
int x[5] = { 1, 2, 3, 4, 5 };
const int y[4] = { 1, 2, 4, 5 };
auto res = ranges::remove(x, 3);
VERIFY( res.begin() == x+4 && res.end() == x+5 );
VERIFY( ranges::equal(x, x+4, y, y+4) );
}
void
test02()
{
int x[1];
test_container<int, forward_iterator_wrapper> c(x, x);
auto res = ranges::remove(c, 1);
VERIFY( res.begin().ptr == x && res.end().ptr == x );
}
void
test03()
{
int x[1] = {1};
test_container<int, forward_iterator_wrapper> c(x);
auto res = ranges::remove(c, 0);
VERIFY( res.begin().ptr == x+1 && res.end().ptr == x+1 );
res = ranges::remove(c, 1);
VERIFY( res.begin().ptr == x && res.end().ptr == x+1 );
}
void
test04()
{
X x[8] = { {0}, {1}, {0}, {1}, {0}, {0}, {1}, {1} };
const int y[4] = { 0, 0, 0, 0 };
test_container<X, forward_iterator_wrapper> c(x);
auto res = ranges::remove(c, 1, &X::i);
VERIFY( res.begin().ptr == x+4 && res.end().ptr == x+8 );
VERIFY( ranges::equal(x, x+4, y, y+4, {}, &X::i) );
}
constexpr bool
test05()
{
int x[6] = { 3, 2, 3, 3, 5, 3 };
const int y[2] = { 2, 5 };
auto res = ranges::remove(x, 3);
return ranges::equal(x, res.begin(), y, y+2);
}
int
main()
{
test01();
test02();
test03();
test04();
static_assert(test05());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
friend constexpr bool
operator==(const X& a, const X& b)
{
return a.i == b.i;
}
};
void
test01()
{
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[4];
X z[4] = { {2}, {2}, {6}, {10} };
auto [in,out] = ranges::remove_copy(x, x+5, y, 8, &X::i);
VERIFY( in == x+5 && out == y+4 );
VERIFY( ranges::equal(y, z) );
}
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[5];
X z[5] = { {2}, {2}, {6}, {8}, {10} };
auto [in,out] = ranges::remove_copy(x, x+5, y, 11, &X::i);
VERIFY( in == x+5 && out == y+5 );
VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {2} };
X y[3];
X z[3] = { {6}, {8}, {10} };
test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
auto [in, out] = ranges::remove_copy(cx, cy.begin(), 2, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(cy, cz) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[4];
const X z[4] = { {6}, {8}, {10}, {11} };
test_range<X, input_iterator_wrapper> cx(x);
test_range<X, output_iterator_wrapper> cy(y);
auto [in, out] = ranges::remove_copy(cx, cy.begin(), 2, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(y, z) );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
bool ok = true;
Y x[3] = { {3,2}, {2,4}, {3,6} };
Y y[1];
Y z[1] = { {2,4} };
auto [in, out] = ranges::remove_copy(x, y, 3, &Y::i);
ok &= in == x+3;
ok &= out == y+1;
ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
friend constexpr bool
operator==(const X& a, const X& b)
{
return a.i == b.i;
}
};
void
test01()
{
auto is_negative_p = [] (int a) { return a < 0; };
auto is_two_p = [] (int a) { return a == 2; };
{
const X x[6] = { {2}, {2}, {6}, {8}, {2}, {11} };
X y[2];
X z[2] = { {6}, {8} };
auto [in, out] = ranges::remove_copy_if(x, x+5, y, is_two_p, &X::i);
VERIFY( in == x+5 && out == y+2 );
VERIFY( ranges::equal(y, z) );
}
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[5];
X z[5] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::remove_copy_if(x, x+5, y, is_negative_p, &X::i);
VERIFY( in == x+5 && out == y+5 );
VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[4];
X z[4] = { {6}, {8}, {10}, {11} };
test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
auto [in, out] = ranges::remove_copy_if(cx, cy.begin(), is_two_p, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(cy, cz) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[4];
const X z[4] = { {6}, {8}, {10}, {11} };
test_range<X, input_iterator_wrapper> cx(x);
test_range<X, output_iterator_wrapper> cy(y);
auto [in, out] = ranges::remove_copy_if(cx, cy.begin(), is_two_p, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(y, z) );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
bool ok = true;
Y x[3] = { {3,2}, {2,4}, {3,6} };
Y y[1];
Y z[1] = { {2,4} };
auto [in, out]
= ranges::remove_copy_if(x, y, [] (int a) { return a%2 == 1; }, &Y::i);
ok &= in == x+3;
ok &= out == y+1;
ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
};
void
test01()
{
int x[5] = { 1, 2, 3, 4, 5 };
const int y[4] = { 1, 2, 4, 5 };
auto res = ranges::remove_if(x, [] (int a) { return a == 3; });
VERIFY( res.begin() == x+4 && res.end() == x+5 );
VERIFY( ranges::equal(x, x+4, y, y+4) );
}
void
test02()
{
int x[1];
test_container<int, forward_iterator_wrapper> c(x, x);
auto res = ranges::remove_if(c, [] (int a) { return a == 1; });
VERIFY( res.begin().ptr == x && res.end().ptr == x );
}
void
test03()
{
int x[1] = {1};
test_container<int, forward_iterator_wrapper> c(x);
auto res = ranges::remove_if(c, [] (int a) { return a == 0; });
VERIFY( res.begin().ptr == x+1 && res.end().ptr == x+1 );
res = ranges::remove_if(c, [] (int a) { return a == 1; });
VERIFY( res.begin().ptr == x && res.end().ptr == x+1 );
}
void
test04()
{
X x[8] = { {0}, {1}, {0}, {1}, {0}, {0}, {1}, {1} };
const int y[4] = { 0, 0, 0, 0 };
test_range<X, forward_iterator_wrapper> c(x);
auto res = ranges::remove_if(c, [] (int a) { return a == 1; }, &X::i);
VERIFY( res.begin().ptr == x+4 && res.end().ptr == x+8 );
VERIFY( ranges::equal(x, x+4, y, y+4, {}, &X::i) );
}
constexpr bool
test05()
{
int x[6] = { 3, 2, 3, 3, 5, 3 };
const int y[2] = { 2, 5 };
auto res = ranges::remove_if(x, [] (int a) { return a == 3; });
return ranges::equal(x, res.begin(), y, y+2);
}
int
main()
{
test01();
test02();
test03();
test04();
static_assert(test05());
}
// 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;
friend constexpr bool
operator==(const X& a, const X& b)
{
return a.i == b.i;
}
};
void
test01()
{
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6] = { {2}, {2}, {6}, {9}, {10}, {11} };
auto res = ranges::replace(x, x+5, 8, X{9}, &X::i);
VERIFY( res == x+5 );
VERIFY( ranges::equal(x, y) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
auto res = ranges::replace(x, x+5, 7, X{9}, &X::i);
VERIFY( res == x+5 );
VERIFY( ranges::equal(x, y) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
test_container<X, forward_iterator_wrapper> cx(x), cy(y);
auto res = ranges::replace(cx, 2, X{7}, &X::i);
VERIFY( res == cx.end() );
VERIFY( ranges::equal(cx, cy) );
}
{
int x[6] = { {2}, {2}, {6}, {8}, {10}, {2} };
int y[6] = { {7}, {7}, {6}, {8}, {10}, {7} };
test_range<int, input_iterator_wrapper> rx(x), ry(y);
auto res = ranges::replace(rx, 2, 7);
VERIFY( res == rx.end() );
rx.bounds.first = x;
ry.bounds.first = y;
VERIFY( ranges::equal(rx, ry) );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
bool ok = true;
Y x[] = { {3,2}, {2,4}, {3,6} };
Y y[] = { {4,5}, {2,4}, {4,5} };
auto res = ranges::replace(x, 3, Y{4,5}, &Y::i);
ok &= res == x+3;
ok &= ranges::equal(x, y, {}, &Y::i, &Y::i);
ok &= ranges::equal(x, y, {}, &Y::j, &Y::j);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
friend constexpr bool
operator==(const X& a, const X& b)
{
return a.i == b.i;
}
};
void
test01()
{
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[5];
X z[5] = { {2}, {2}, {6}, {9}, {10} };
auto [in,out] = ranges::replace_copy(x, x+5, y, 8, X{9}, &X::i);
VERIFY( in == x+5 && out == y+5 );
VERIFY( ranges::equal(y, z) );
}
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[5];
X z[5] = { {2}, {2}, {6}, {8}, {10} };
auto [in,out] = ranges::replace_copy(x, x+5, y, 7, X{9}, &X::i);
VERIFY( in == x+5 && out == y+5 );
VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6];
X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
auto [in, out] = ranges::replace_copy(cx, cy.begin(), 2, X{7}, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(cy, cz) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6];
const X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
test_range<X, input_iterator_wrapper> cx(x);
test_range<X, output_iterator_wrapper> cy(y);
auto [in, out] = ranges::replace_copy(cx, cy.begin(), 2, X{7}, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(y, z) );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
bool ok = true;
Y x[] = { {3,2}, {2,4}, {3,6} };
Y y[] = { {4,5}, {2,4}, {4,5} };
Y z[] = { {4,5}, {2,4}, {4,5} };
auto [in, out] = ranges::replace_copy(x, y, 3, Y{4,5}, &Y::i);
ok &= in == x+3;
ok &= out == y+3;
ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
friend constexpr bool
operator==(const X& a, const X& b)
{
return a.i == b.i;
}
};
void
test01()
{
auto is_negative_p = [] (int a) { return a < 0; };
auto is_two_p = [] (int a) { return a == 2; };
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[5];
X z[5] = { {9}, {9}, {6}, {8}, {10} };
auto [in, out] = ranges::replace_copy_if(x, x+5, y,
is_two_p, X{9}, &X::i);
VERIFY( in == x+5 && out == y+5 );
VERIFY( ranges::equal(y, z) );
}
{
const X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[5];
X z[5] = { {2}, {2}, {6}, {8}, {10} };
auto [in, out] = ranges::replace_copy_if(x, x+5, y,
is_negative_p, X{9}, &X::i);
VERIFY( in == x+5 && out == y+5 );
VERIFY( ranges::equal(x, x+5, y, y+5) && ranges::equal(y, z) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6];
X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
test_container<X, forward_iterator_wrapper> cx(x), cy(y), cz(z);
auto [in, out] = ranges::replace_copy_if(cx, cy.begin(),
is_two_p, X{7}, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(cy, cz) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6];
const X z[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
test_range<X, input_iterator_wrapper> cx(x);
test_range<X, output_iterator_wrapper> cy(y);
auto [in, out] = ranges::replace_copy_if(cx, cy.begin(),
is_two_p, X{7}, &X::i);
VERIFY( in == cx.end() && out == cy.end() );
VERIFY( ranges::equal(y, z) );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
bool ok = true;
Y x[] = { {3,2}, {2,4}, {3,6} };
Y y[] = { {4,5}, {2,4}, {4,5} };
Y z[] = { {4,5}, {2,4}, {4,5} };
auto [in, out]
= ranges::replace_copy_if(x, y,
[] (int a) { return a%2 == 1; }, Y{4,5}, &Y::i);
ok &= in == x+3;
ok &= out == y+3;
ok &= ranges::equal(y, z, {}, &Y::i, &Y::i);
ok &= ranges::equal(y, z, {}, &Y::j, &Y::j);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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;
friend constexpr bool
operator==(const X& a, const X& b)
{
return a.i == b.i;
}
};
void
test01()
{
auto is_even_p = [] (int a) { return a%2 == 0; };
auto is_negative_p = [] (int a) { return a < 0; };
auto is_two_p = [] (int a) { return a == 2; };
{
X x[6] = { {1}, {2}, {6}, {8}, {10}, {11} };
X y[6] = { {1}, {9}, {9}, {9}, {9}, {11} };
auto res = ranges::replace_if(x, x+5, is_even_p, X{9}, &X::i);
VERIFY( res == x+5 );
VERIFY( ranges::equal(x, y) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
auto res = ranges::replace_if(x, x+5, is_negative_p, X{9}, &X::i);
VERIFY( res == x+5 );
VERIFY( ranges::equal(x, y) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} };
X y[6] = { {7}, {7}, {6}, {8}, {10}, {11} };
test_container<X, forward_iterator_wrapper> cx(x), cy(y);
auto res = ranges::replace_if(cx, is_two_p, X{7}, &X::i);
VERIFY( res == cx.end() );
VERIFY( ranges::equal(cx, cy) );
}
{
int x[6] = { {2}, {2}, {6}, {8}, {10}, {2} };
int y[6] = { {7}, {7}, {6}, {8}, {10}, {7} };
test_range<int, input_iterator_wrapper> rx(x), ry(y);
auto res = ranges::replace_if(rx, is_two_p, 7);
VERIFY( res == rx.end() );
rx.bounds.first = x;
ry.bounds.first = y;
VERIFY( ranges::equal(rx, ry) );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
bool ok = true;
Y x[] = { {3,2}, {2,4}, {3,6} };
Y y[] = { {4,5}, {2,4}, {4,5} };
auto res = ranges::replace_if(x, [] (int a) { return a%2 == 1; },
Y{4,5}, &Y::i);
ok &= res == x+3;
ok &= ranges::equal(x, y, {}, &Y::i, &Y::i);
ok &= ranges::equal(x, y, {}, &Y::j, &Y::j);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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_range;
using __gnu_test::test_container;
using __gnu_test::bidirectional_iterator_wrapper;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
template<template<typename> typename wrapper>
void
test01()
{
int x[] = { 1, 2, 3, 4 };
test_container<int, wrapper> cx(x);
const int y[] = { 4, 3, 2, 1 };
auto res = ranges::reverse(cx);
VERIFY( res == ranges::end(cx) );
VERIFY( ranges::equal(cx, y) );
}
template<template<typename> typename wrapper>
void
test02()
{
int x[] = { 1, 2, 3, 4, 5 };
test_range<int, wrapper> rx(x);
const int y[] = { 5, 4, 3, 2, 1 };
auto res = ranges::reverse(rx);
VERIFY( res == ranges::end(rx) );
VERIFY( ranges::equal(rx, y) );
}
constexpr bool
test03()
{
int x[] = { 1, 2, 3 };
const int y[] = { 2, 1, 3 };
ranges::reverse(x, x+2);
return ranges::equal(x, y);
}
int
main()
{
test01<bidirectional_iterator_wrapper>();
test02<bidirectional_iterator_wrapper>();
test01<random_access_iterator_wrapper>();
test02<random_access_iterator_wrapper>();
static_assert(test03());
}
// 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_range;
using __gnu_test::test_container;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = { 1, 2, 3, 4 };
int w[4];
test_container<int, bidirectional_iterator_wrapper> cx(x), cw(w);
const int y[] = { 4, 3, 2, 1 };
auto [in,out] = ranges::reverse_copy(cx, cw.begin());
VERIFY( in == ranges::end(cx) && out == cw.end() );
VERIFY( ranges::equal(cw, y) );
}
void
test02()
{
int x[] = { 1, 2, 3, 4, 5 };
int w[5];
test_range<int, bidirectional_iterator_wrapper> rx(x), rw(w);
const int y[] = { 5, 4, 3, 2, 1 };
auto [in,out] = ranges::reverse_copy(rx, ranges::begin(rw));
VERIFY( in == ranges::end(rx) && out == ranges::end(rw) );
VERIFY( ranges::equal(rw, y) );
}
constexpr bool
test03()
{
const int x[] = { 1, 2, 3 };
int w[2];
const int y[] = { 2, 1 };
ranges::reverse_copy(x, x+2, w);
return ranges::equal(w, y);
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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::forward_iterator_wrapper;
using __gnu_test::bidirectional_iterator_wrapper;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
X (int a) : i(a) { }
friend bool
operator==(const X& lhs, const X& rhs)
{
return lhs.i == rhs.i;
}
};
static_assert(!std::is_trivial_v<X>);
template<template<typename, template<typename> typename> typename container,
template<typename> typename wrapper,
typename T = int>
void
test01()
{
for (int a = 0; a <= 7; a++)
{
T x[] = {1, 2, 3, 4, 5, 6, 7};
container<T, wrapper> rx(x);
auto i = ranges::begin(rx);
std::advance(i, a);
auto res = ranges::rotate(rx, i);
if (a == 0)
VERIFY( ranges::begin(res) == ranges::end(rx) );
else
VERIFY( ranges::begin(res)
== std::next(ranges::begin(rx),
ranges::distance(i, ranges::end(rx))) );
VERIFY( ranges::end(res) == ranges::end(rx) );
for (int k = 0; k < 7; k++)
VERIFY( x[k] == (k+a)%7 + 1 );
}
}
constexpr bool
test02()
{
int x[] = {1, 2, 3, 4};
const int y[] = { 2, 3, 1, 4 };
ranges::rotate(x, x+1, x+3);
return ranges::equal(x, y);
}
int
main()
{
test01<test_container, forward_iterator_wrapper>();
test01<test_range, forward_iterator_wrapper>();
test01<test_container, bidirectional_iterator_wrapper>();
test01<test_range, bidirectional_iterator_wrapper>();
test01<test_container, random_access_iterator_wrapper>();
test01<test_range, random_access_iterator_wrapper>();
test01<test_container, random_access_iterator_wrapper, X>();
test01<test_range, random_access_iterator_wrapper, X>();
static_assert(test02());
}
// 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::forward_iterator_wrapper;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
X () : i(0) { }
X (int a) : i(a) { }
friend bool
operator==(const X& lhs, const X& rhs)
{
return lhs.i == rhs.i;
}
};
static_assert(!std::is_trivial_v<X>);
template<template<typename, template<typename> typename> typename container,
template<typename> typename wrapper,
typename T = int>
void
test01()
{
for (int a = 0; a <= 7; a++)
{
T x[] = {1, 2, 3, 4, 5, 6, 7};
T w[7];
container<T, wrapper> rx(x), rw(w);
auto i = ranges::begin(rx);
std::advance(i, a);
auto [in,out] = ranges::rotate_copy(rx, i, ranges::begin(rw));
VERIFY( in == ranges::end(rx) );
VERIFY( out == ranges::end(rw) );
for (int k = 0; k < 7; k++)
VERIFY( w[k] == (k+a)%7 + 1 );
}
}
constexpr bool
test02()
{
const int x[] = {1, 2, 3, 4};
int w[3];
const int y[] = { 2, 3, 1};
auto [in,out] = ranges::rotate_copy(x, x+1, x+3, w);
return (in == x+3
&& out == w+3
&& ranges::equal(w, y));
}
int
main()
{
test01<test_container, forward_iterator_wrapper>();
test01<test_range, forward_iterator_wrapper>();
test01<test_container, random_access_iterator_wrapper>();
test01<test_range, random_access_iterator_wrapper>();
test01<test_container, random_access_iterator_wrapper, X>();
test01<test_range, random_access_iterator_wrapper, X>();
static_assert(test02());
}
// 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}, {6}, {8}, {10}, {11} };
X y[] = { {10}, {11} };
{
test_container<X, forward_iterator_wrapper> c(x);
auto res = ranges::search(c, y, {}, &X::i, &X::i);
VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(c) );
res = ranges::search(c, c, {}, &X::i, &X::i);
VERIFY( std::get<0>(res) == ranges::begin(c)
&& std::get<1>(res) == ranges::end(c) );
}
{
test_range<X, forward_iterator_wrapper> r(x);
auto res = ranges::search(r, y, {}, &X::i, &X::i);
VERIFY( std::get<0>(res)->i == 10 && std::get<1>(res) == ranges::end(r) );
res = ranges::search(r, r, {}, &X::i, &X::i);
VERIFY( std::get<0>(res) == ranges::begin(r)
&& std::get<1>(res) == ranges::end(r) );
}
}
void
test02()
{
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
static constexpr X y[] = { {6}, {8} };
static constexpr int z[] = { 2, 8 };
static constexpr int w[] = { 2 };
static_assert(std::get<0>(ranges::search(x, y, {}, &X::i, &X::i)) == x+2);
static_assert(std::get<1>(ranges::search(x, y, {}, &X::i, &X::i)) == x+4);
static_assert(std::get<0>(ranges::search(x, z, {}, &X::i)) == x+6);
static_assert(std::get<1>(ranges::search(x, z, {}, &X::i)) == x+6);
static_assert(std::get<0>(ranges::search(x, w, {}, &X::i)) == x+0);
static_assert(std::get<1>(ranges::search(x, w, {}, &X::i)) == x+1);
static_assert(std::get<0>(ranges::search(x, x+6, w, w, {}, &X::i)) == x+0);
static_assert(std::get<1>(ranges::search(x, x+6, w, w, {}, &X::i)) == x+0);
static_assert(std::get<0>(ranges::search(x, x, w, w+1, {}, &X::i)) == x+0);
static_assert(std::get<1>(ranges::search(x, x, w, w+1, {}, &X::i)) == x+0);
}
int
main()
{
test01();
test02();
}
// 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;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
struct X { int i; };
void
test01()
{
int x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
auto res = ranges::search_n(x+0, x+6, 2, 2);
VERIFY( res.begin() == x+0 && res.end() == x+2 );
int z[] = { {1}, {2}, {2}, {4}, {5}, {6} };
res = ranges::search_n(z, 3, 3, std::greater<int>());
VERIFY( res.begin() == z+3 && res.end() == z+6 );
test_container<int, forward_iterator_wrapper> cx(x);
auto res2 = ranges::search_n(cx, 2, 2);
VERIFY( res2.begin() == cx.begin() && *res2.end() == 6 );
int y[] = { {2}, {2}, {8}, {2}, {2}, {2}, {5} };
test_range<int, forward_iterator_wrapper> ry(y);
auto res3 = ranges::search_n(ry, 3, 2);
VERIFY( *res3.begin() == 2 && *res3.end() == 5 );
auto res4 = ranges::search_n(ry, 1, 8);
VERIFY( res4.begin().ptr == y+2 && res4.end().ptr == y+3 );
}
void
test02()
{
static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {2} };
static constexpr X y[] = { {2}, {6}, {8}, {8}, {8}, {2} };
static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
static_assert(ranges::search_n(z, 0, 5).end() == z+0);
static_assert(ranges::search_n(z, 1, 5).begin() == z+6);
static_assert(ranges::search_n(x, 2, 3, {}, &X::i).begin() == x+6);
static_assert(ranges::search_n(x, 2, 2, {}, &X::i).end() == x+2);
static_assert(ranges::search_n(y, 3, 8, {}, &X::i).begin() == y+2);
static_assert(ranges::search_n(y, 3, 8, {}, &X::i).end() == y+5);
}
int
main()
{
test01();
test02();
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {4,2,1,1,0};
int y[] = {3,2,1};
int z[3];
test_range<int, input_iterator_wrapper> rx(x), ry(y);
test_range<int, output_iterator_wrapper> rz(z);
auto [in,out]
= ranges::set_difference(rx, ry, rz.begin(), ranges::greater{});
VERIFY( in.ptr == x+5 );
VERIFY( out.ptr == z+3 );
VERIFY( ranges::equal(z, (int[]){4,1,0}) );
}
void
test02()
{
int x[] = {3,2,1,1,0};
int y[] = {3,2,2,1,0};
int z[1];
test_container<int, forward_iterator_wrapper> rx(x), ry(y);
test_container<int, forward_iterator_wrapper> rz(z);
auto [in,out]
= ranges::set_difference(rx.begin(), rx.end(),
ry.begin(), ry.end(),
rz.begin(),
{},
std::negate<>{},
std::negate<>{});
VERIFY( in.ptr == x+5 );
VERIFY( out.ptr == z+1 );
VERIFY( ranges::equal(z, (int[]){1}) );
}
constexpr bool
test03()
{
bool ok = true;
int x[1] = {0};
int y[1] = {1};
int z[1];
ok &= ranges::set_difference(x, x, y, y+1, z).out == z;
ok &= ranges::set_difference(x, x+1, y, y, z).out == z+1;
ok &= z[0] == 0;
return ok;
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {4,2,1,1,0};
int y[] = {3,2,1};
int z[2];
test_range<int, input_iterator_wrapper> rx(x), ry(y);
test_range<int, output_iterator_wrapper> rz(z);
auto [in1,in2,out]
= ranges::set_intersection(rx, ry, rz.begin(), ranges::greater{});
VERIFY( in1.ptr == x+5 );
VERIFY( in2.ptr == y+3 );
VERIFY( out.ptr == z+2 );
VERIFY( ranges::equal(z, (int[]){2,1}) );
}
void
test02()
{
int x[] = {3,2,1,1,0};
int y[] = {3,2,1,0};
int z[4];
test_container<int, forward_iterator_wrapper> rx(x), ry(y);
test_container<int, forward_iterator_wrapper> rz(z);
auto [in1,in2,out]
= ranges::set_intersection(rx.begin(), rx.end(),
ry.begin(), ry.end(),
rz.begin(),
{},
std::negate<>{},
std::negate<>{});
VERIFY( in1.ptr == x+5 );
VERIFY( in2.ptr == y+4 );
VERIFY( out.ptr == z+4 );
VERIFY( ranges::equal(z, (int[]){3,2,1,0}) );
}
constexpr bool
test03()
{
bool ok = true;
int x[1] = {0};
int y[1] = {1};
int z[1];
ok &= ranges::set_intersection(x, x, y, y+1, z).out == z;
ok &= ranges::set_intersection(x, x+1, y, y, z).out == z;
return ok;
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <vector>
#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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {4,2,1,1,0};
int y[] = {3,2,2,1};
int z[5];
test_range<int, input_iterator_wrapper> rx(x), ry(y);
test_range<int, output_iterator_wrapper> rz(z);
auto [in1,in2,out]
= ranges::set_symmetric_difference(rx, ry, rz.begin(), ranges::greater{});
VERIFY( in1.ptr == x+5 );
VERIFY( in2.ptr == y+4 );
VERIFY( out.ptr == z+5 );
VERIFY( ranges::equal(z, (int[]){4,3,2,1,0}) );
}
void
test02()
{
int x[] = {3,2,1,1,0};
int y[] = {3,2,1,0};
int z[1];
test_container<int, forward_iterator_wrapper> rx(x), ry(y);
test_container<int, forward_iterator_wrapper> rz(z);
auto [in1,in2,out]
= ranges::set_symmetric_difference(rx.begin(), rx.end(),
ry.begin(), ry.end(),
rz.begin(),
{},
std::negate<>{},
std::negate<>{});
VERIFY( in1.ptr == x+5 );
VERIFY( in2.ptr == y+4 );
VERIFY( out.ptr == z+1 );
VERIFY( ranges::equal(z, (int[]){1}) );
}
constexpr bool
test03()
{
bool ok = true;
int x[1] = {0};
int y[1] = {1};
int z[1];
ok &= ranges::set_symmetric_difference(x, x, y, y+1, z).out == z+1;
ok &= z[0] == 1;
ok &= ranges::set_symmetric_difference(x, x+1, y, y, z).out == z+1;
ok &= z[0] == 0;
return ok;
}
void
test04()
{
int x[15] = {5,5,5,5,5,4,4,4,4,3,3,3,2,2,1};
int y[15] = {5,5,5,5,5,4,4,4,4,3,3,3,2,2,1};
for (int k = 0; k < 100; k++)
{
std::ranlux48_base g(k);
ranges::shuffle(x, g);
ranges::shuffle(y, g);
ranges::sort(x, x+10);
ranges::sort(y, y+10);
int z[15];
auto z_out = ranges::set_symmetric_difference(x, x+10, y, y+10, z).out;
int w1[15];
auto w1_out = ranges::set_difference(x, x+10, y, y+10, w1).out;
int w2[15];
auto w2_out = ranges::set_difference(y, y+10, x, x+10, w2).out;
int w3[15];
auto w3_out = ranges::set_union(w1, w1_out, w2, w2_out, w3).out;
VERIFY( ranges::equal(z, z_out, w3, w3_out) );
}
}
int
main()
{
test01();
test02();
static_assert(test03());
test04();
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {4,2,1,1,0};
int y[] = {3,2,1};
int z[6];
test_range<int, input_iterator_wrapper> rx(x), ry(y);
test_range<int, output_iterator_wrapper> rz(z);
auto [in1,in2,out]
= ranges::set_union(rx, ry, rz.begin(),
ranges::greater{});
VERIFY( in1.ptr == x+5 );
VERIFY( in2.ptr == y+3 );
VERIFY( out.ptr == z+6 );
VERIFY( ranges::equal(z, (int[]){4,3,2,1,1,0}) );
}
void
test02()
{
int x[] = {4,2,1,1,0};
int y[] = {3,2,1,1,0};
int z[6];
test_container<int, forward_iterator_wrapper> rx(x), ry(y);
test_container<int, forward_iterator_wrapper> rz(z);
auto [in1,in2,out]
= ranges::set_union(rx.begin(), rx.end(),
ry.begin(), ry.end(),
rz.begin(),
{},
std::negate<>{},
std::negate<>{});
VERIFY( in1.ptr == x+5 );
VERIFY( in2.ptr == y+5 );
VERIFY( out.ptr == z+6 );
VERIFY( ranges::equal(z, (int[]){4,3,2,1,1,0}) );
}
constexpr bool
test03()
{
bool ok = true;
int x[1] = {0};
int y[1] = {1};
int z[1];
ranges::set_union(x, x, y, y+1, z);
ok &= z[0] == 1;
ranges::set_union(x, x+1, y, y, z);
ok &= z[0] == 0;
return ok;
}
int
main()
{
test01();
test02();
static_assert(test03());
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
// This test is adapted from 25_algorithms/shuffle/1.cc.
void
test01()
{
for (unsigned size = 0; size < 50; ++size)
{
std::vector<int> vref(size);
std::iota(vref.begin(), vref.end(), 0);
std::vector<int> v1(vref), v2(vref);
test_container<int, random_access_iterator_wrapper> c
= {&v1[0], &v1[0] + size};
test_range<int, random_access_iterator_wrapper> r
= {&v2[0], &v2[0] + size};
std::ranlux48_base g1(size), g2(size + 1);
VERIFY( ranges::shuffle(c, g1) == c.end() );
VERIFY( ranges::shuffle(ranges::begin(r), ranges::end(r), g2)
== ranges::end(r) );
if (size >= 10)
{
VERIFY( !ranges::equal(c, vref) );
VERIFY( !ranges::equal(r, vref) );
VERIFY( !ranges::equal(c, r) );
}
VERIFY( ranges::is_permutation(c, vref) );
VERIFY( ranges::is_permutation(r, vref) );
}
}
int
main()
{
test01();
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
for (unsigned size = 0; size < 50; ++size)
{
std::vector<int> vref(size);
std::iota(vref.begin(), vref.end(), 0);
std::vector<int> v1(vref), v2(vref);
test_container<int, random_access_iterator_wrapper> c
= {&v1[0], &v1[0] + size};
test_range<int, random_access_iterator_wrapper> r
= {&v2[0], &v2[0] + size};
std::ranlux48_base g1(size), g2(size + 1);
ranges::shuffle(c, g1);
ranges::shuffle(ranges::begin(r), ranges::end(r), g2);
VERIFY( ranges::sort(c) == c.end() );
VERIFY( ranges::sort(r) == ranges::end(r) );
VERIFY( ranges::equal(c, vref) );
VERIFY( ranges::equal(r, vref) );
}
}
struct X
{
int i;
constexpr X(int a) : i(a) { }
};
constexpr bool
test02()
{
X x[] = {3,4,2,1,5};
const X y[] = {4,3,2,1,5};
auto res = ranges::sort(x, x+4, ranges::greater{}, &X::i);
return (res == x+4
&& ranges::equal(x, y, {}, &X::i, &X::i));
}
int
main()
{
test01();
static_assert(test02());
}
// 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::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
{
int x[] = {1,2,3,4,5,6,7,8,9,10};
test_container<int, bidirectional_iterator_wrapper> cx(x);
auto pred = [] (int a) { return a%2==0; };
auto range = ranges::stable_partition(cx, pred);
VERIFY( ranges::all_of(cx.begin(), range.begin(), pred) );
VERIFY( ranges::none_of(range, pred) );
}
{
int x[] = {1,2,3,4,5,6,7,8,9,10,11};
test_range<int, bidirectional_iterator_wrapper> cx(x);
auto pred = [] (int a) { return a%2==0; };
auto range = ranges::stable_partition(cx, pred);
VERIFY( ranges::all_of(cx.begin(), range.begin(), pred) );
VERIFY( ranges::none_of(range, pred) );
}
}
void
test02()
{
for (int k = 1; k <= 10; k++)
{
int x[] = {1,2,3,4,5,6,7,8,9,10};
auto pred = [&] (int a) { return a >= k; };
auto proj = [] (int a) { return a-1; };
auto range = ranges::stable_partition(x, x+10, pred, proj);
VERIFY( ranges::all_of(x, range.begin(), pred, proj) );
VERIFY( ranges::none_of(range, pred, proj) );
int y[] = {0,1,2,3,4,5,6,7,8,9};
ranges::rotate(y, y+k);
VERIFY( ranges::equal(x, y, {}, proj) );
}
}
int
main()
{
test01();
test02();
}
// 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 } }
// { dg-require-cstdint "" }
#include <algorithm>
#include <random>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
using __gnu_test::test_container;
using __gnu_test::test_range;
using __gnu_test::random_access_iterator_wrapper;
namespace ranges = std::ranges;
// This test doesn't verify the stability property of ranges::stable_sort,
// because at the moment it's just defined to be a wrapper over
// std::stable_sort.
void
test01()
{
for (unsigned size = 0; size < 50; ++size)
{
std::vector<int> vref(size);
std::iota(vref.begin(), vref.end(), 0);
std::vector<int> v1(vref), v2(vref);
test_container<int, random_access_iterator_wrapper> c
= {&v1[0], &v1[0] + size};
test_range<int, random_access_iterator_wrapper> r
= {&v2[0], &v2[0] + size};
std::ranlux48_base g1(size), g2(size + 1);
ranges::shuffle(c, g1);
ranges::shuffle(ranges::begin(r), ranges::end(r), g2);
auto res1 = ranges::stable_sort(c.begin(), c.end(), {}, std::negate<>{});
VERIFY( res1 == c.end() );
auto res2 = ranges::stable_sort(r, ranges::greater{});
VERIFY( res2 == ranges::end(r) );
VERIFY( ranges::equal(c, r) );
VERIFY( ranges::equal(c.begin(), c.end(), vref.rbegin(), vref.rend()) );
}
}
int
main()
{
test01();
}
// 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;
using __gnu_test::bidirectional_iterator_wrapper;
namespace ranges = std::ranges;
struct X
{
int i;
int moved = 0;
constexpr X(int a) : i(a) { }
constexpr X(const X&) = delete;
constexpr X& operator=(const X&) = delete;
constexpr X(X&& other)
{
*this = std::move(other);
}
constexpr X&
operator=(X&& other)
{
other.moved++;
i = other.i;
return *this;
}
friend constexpr bool
operator==(const X& a, const X& b)
{ return a.i == b.i; }
};
void
test01()
{
{
X x[7] = { 1, 2, 3, 4, 5, 6, 7 };
X y[7] = { 2, 4, 3, 5, 8, 9, 1 };
X z[7] = { 1, 2, 3, 4, 5, 6, 7 };
X w[7] = { 2, 4, 3, 5, 8, 9, 1 };
auto [x_iter, y_iter] = ranges::swap_ranges(x, y);
VERIFY( ranges::equal(y, z) && x_iter == x+7 && y_iter == y+7 );
VERIFY( ranges::equal(x, w) );
}
{
int x[3] = { 1, 2, 3 };
int y[4] = { 2, 4, 6, 0 };
int z[3] = { 1, 2, 3 };
int w[3] = { 2, 4, 6 };
test_container<int, forward_iterator_wrapper> cx(x);
test_container<int, forward_iterator_wrapper> cy(y);
auto [x_iter, y_iter] = ranges::swap_ranges(cx, cy);
VERIFY( ranges::equal(y, y+3, z, z+3) );
VERIFY( x_iter.ptr == x+3 && y_iter.ptr == y+3 );
VERIFY( y[3] == 0 );
VERIFY( ranges::equal(x, w) );
}
{
int x[3] = { 1, 2, 3 };
int y[4] = { 2, 4, 6, 0 };
int z[3] = { 1, 2, 3 };
int w[3] = { 2, 4, 6 };
test_range<int, input_iterator_wrapper> cx(x);
test_range<int, input_iterator_wrapper> cy(y);
auto [y_iter, x_iter] = ranges::swap_ranges(cy, cx);
VERIFY( ranges::equal(y, y+3, z, z+3) );
VERIFY( x_iter.ptr == x+3 && y_iter.ptr == y+3 );
VERIFY( y[3] == 0 );
VERIFY( ranges::equal(x, w) );
}
}
constexpr bool
test02()
{
bool ok = true;
X x[7] = { 1, 2, 3, 4, 5, 6, 7 };
X y[7] = { 2, 4, 3, 5, 8, 9, 1 };
X z[7] = { 1, 2, 3, 4, 5, 6, 7 };
X w[7] = { 2, 4, 3, 5, 8, 9, 1 };
auto [x_iter, y_iter] = ranges::swap_ranges(x, y);
ok &= ranges::equal(y, z) && x_iter == x+7 && y_iter == y+7;
ok &= ranges::equal(x, w);
return ok;
}
int
main()
{
test01();
static_assert(test02());
}
// 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::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
struct X { int i; };
void
test01()
{
{
int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} };
auto [in, out] = ranges::transform(x, x, [] (int a) { return a+1; });
VERIFY( in == x+6 && out == x+6 );
VERIFY( ranges::equal(x, y) );
}
{
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
int z[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} };
test_container<X, forward_iterator_wrapper> cx(x);
test_container<int, forward_iterator_wrapper> cy(y), cz(z);
auto [in, out]
= ranges::transform(cx, cz.begin(), [] (int a) { return a+1; }, &X::i);
VERIFY( ranges::equal(cy, cz) );
VERIFY( in == cx.end() && ++out == cz.end() );
}
{
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
X y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
int z[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} };
test_range<X, input_iterator_wrapper> rx(x), ry(y);
test_range<int, output_iterator_wrapper> rz(z);
auto [in, out]
= ranges::transform(rx, rz.begin(), [] (int a) { return a+1; }, &X::i);
VERIFY( ranges::equal(ry, z, {}, &X::i) );
VERIFY( in == rx.end() && out.ptr == z+6 );
}
}
struct Y { int i; int j; };
constexpr bool
test02()
{
int x[] = { 1, 2, 3 };
Y y[] = { {1,2}, {2,4}, {3,6} };
ranges::transform(y, y+3, x, [] (int a) { return -a; }, &Y::i);
return x[0] == -1 && x[1] == -2 && x[2] == -3;
}
void
test03()
{
{
int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
const int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} };
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
auto [in1, in2, out] = ranges::transform(x, y, x, std::plus<>{});
VERIFY( in1 == x+6 && in2 == y+6 && out == x+6 );
VERIFY( ranges::equal(x, z) );
}
{
int x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
const int y[6] = { {3}, {5}, {7}, {9}, {11}, {12} };
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
auto [in1, in2, out] = ranges::transform(y, x, x, std::plus<>{});
VERIFY( in1 == y+6 && in2 == x+6 && out == x+6 );
VERIFY( ranges::equal(x, z) );
}
{
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
int w[6];
test_container<X, forward_iterator_wrapper> cx(x);
test_container<int, forward_iterator_wrapper> cy(y), cz(z), cw(w);
auto [in1, in2, out]
= ranges::transform(cx, cy, cw.begin(), std::plus<>{}, &X::i);
VERIFY( in1 == cx.end() && ++in2 == cy.end() && out == cw.end() );
VERIFY( ranges::equal(cw, cz) );
}
{
X x[6] = { {2}, {4}, {6}, {8}, {10}, {11} };
int y[7] = { {3}, {5}, {7}, {9}, {11}, {12}, {0} };
int z[6] = { {5}, {9}, {13}, {17}, {21}, {23} };
int w[6];
test_range<X, input_iterator_wrapper> rx(x);
test_range<int, input_iterator_wrapper> ry(y), rz(z);
test_range<int, output_iterator_wrapper> rw(w);
auto [in1, in2, out]
= ranges::transform(rx, ry, rw.begin(), std::plus<>{}, &X::i);
VERIFY( in1 == rx.end() && ++in2 == ry.end() && out.ptr == w+6 );
VERIFY( ranges::equal(w, rz) );
}
}
constexpr bool
test04()
{
int x[3];
const Y y[3] = { {1,2}, {2,4}, {3,6} };
ranges::transform(y, y+3, y, y+3, x, std::plus<>{}, &Y::i, &Y::j);
return x[0] == 3 && x[1] == 6 && x[2] == 9;
}
int
main()
{
test01();
static_assert(test02());
test03();
static_assert(test04());
}
// 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 <list>
#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[6] = { {2}, {2}, {6}, {8}, {2}, {11} };
const int y[5] = { {2}, {6}, {8}, {2}, {11} };
test_container<X, forward_iterator_wrapper> cx(x);
auto res = ranges::unique(cx, {}, &X::i);
VERIFY( res.end() == cx.end() );
VERIFY( ranges::equal(cx.begin(), res.begin(), y, y+5, {}, &X::i) );
}
{
X x[6] = { {2}, {2}, {6}, {8}, {2}, {11} };
const int y[5] = { {2}, {6}, {8}, {2}, {11} };
test_range<X, forward_iterator_wrapper> rx(x);
auto res = ranges::unique(rx, {}, &X::i);
VERIFY( res.end() == rx.end() );
VERIFY( ranges::equal(rx.begin(), res.begin(), y, y+5, {}, &X::i) );
}
}
constexpr bool
test02()
{
int x[2] = {2, 2};
const int y[1] = {2};
auto res = ranges::unique(x);
return ranges::equal(x, res.begin(), y, y+1, ranges::equal_to{});
}
/* The following is adapted from 25_algorithms/unique/2.cc. */
namespace two_dot_cc
{
const int T1[] = {1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 5, 4, 4};
const int T2[] = {1, 1, 1, 2, 2, 1, 1, 7, 6, 6, 7, 8, 8, 8, 8, 9, 9};
const int N = sizeof(T1) / sizeof(int);
const int A1[] = {1, 4, 6, 1, 2, 3, 1, 6, 5, 7, 5, 4};
const int A2[] = {1, 4, 4, 6, 6, 6, 6, 7};
const int A3[] = {1, 1, 1};
const int B1[] = {1, 2, 1, 7, 6, 7, 8, 9};
const int B2[] = {1, 1, 1, 2, 2, 7, 7, 8, 8, 8, 8, 9, 9};
const int B3[] = {9, 9, 8, 8, 8, 8, 7, 6, 6, 1, 1, 1, 1, 1};
void test01()
{
using namespace std;
list<int>::iterator pos;
list<int> coll(T1, T1 + N);
pos = ranges::unique(coll.begin(), coll.end()).begin();
VERIFY( equal(coll.begin(), pos, A1) );
list<int> coll2(T2, T2 + N);
pos = ranges::unique(coll2.begin(), coll2.end()).begin();
VERIFY( equal(coll2.begin(), pos, B1) );
}
void test02()
{
using namespace std;
list<int>::iterator pos;
list<int> coll(T1, T1 + N);
pos = ranges::unique(coll.begin(), coll.end(), greater<int>()).begin();
VERIFY( equal(coll.begin(), pos, A2) );
list<int> coll2(T2, T2 + N);
pos = ranges::unique(coll2.begin(), coll2.end(), greater<int>()).begin();
VERIFY( equal(coll2.begin(), pos, B2) );
}
void test03()
{
using namespace std;
list<int>::iterator pos;
list<int> coll(T1, T1 + N);
pos = ranges::unique(coll.begin(), coll.end(), less<int>()).begin();
VERIFY( equal(coll.begin(), pos, A3) );
list<int> coll2(T2, T2 + N);
reverse(coll2.begin(), coll2.end());
pos = ranges::unique(coll2.begin(), coll2.end(), less<int>()).begin();
VERIFY( equal(coll2.begin(), pos, B3) );
}
} // namespace two_dot_cc
int main()
{
test01();
static_assert(test02());
two_dot_cc::test01();
two_dot_cc::test02();
two_dot_cc::test03();
return 0;
}
// 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_range;
using __gnu_test::input_iterator_wrapper;
using __gnu_test::output_iterator_wrapper;
using __gnu_test::forward_iterator_wrapper;
namespace ranges = std::ranges;
template<template<typename> typename source_wrapper,
template<typename> typename dest_wrapper>
void
test01()
{
int x[6] = {0, 0, 0, 1, 1, 1};
int y[2];
const int z[2] = {0, 1};
test_range<int, source_wrapper> rx(x);
test_range<int, dest_wrapper> ry(y);
auto [in,out] = ranges::unique_copy(rx, ry.begin());
VERIFY( in == ranges::end(rx) && out == ranges::end(ry) );
VERIFY( ranges::equal(y, z) );
}
template<template<typename> typename source_wrapper,
template<typename> typename dest_wrapper>
void
test02()
{
int x[6] = {0, 0, 0, 1, 1, 1};
int y[2] = {0, 0};
const int z[2] = {0, 0};
test_range<int, source_wrapper> rx(x, x);
test_range<int, dest_wrapper> ry(y, y);
auto [in, out] = ranges::unique_copy(rx.begin(), rx.end(), ry.begin());
VERIFY( in.ptr == x && out.ptr == y );
VERIFY( ranges::equal(y, z) );
}
template<template<typename> typename source_wrapper,
template<typename> typename dest_wrapper>
void
test03()
{
struct X { int i; };
X x[6] = { {1}, {2}, {2}, {4}, {4}, {6} };
X y[4] = { {1}, {2}, {4}, {6} };
const X z[4] = { {1}, {2}, {4}, {6} };
test_range<X, source_wrapper> rx(x);
test_range<X, dest_wrapper> ry(y);
auto [in, out]
= ranges::unique_copy(rx, ry.begin(), ranges::equal_to{}, &X::i);
VERIFY( in == ranges::end(rx) && out == ranges::end(ry) );
VERIFY( ranges::equal(y, z, {}, &X::i, &X::i) );
}
constexpr bool
test04()
{
struct X { int i; };
X x[7] = { {1}, {2}, {2}, {2}, {4}, {4}, {6} };
X y[4] = { {1}, {2}, {4}, {6} };
const X z[4] = { {1}, {2}, {4}, {6} };
auto [in, out]
= ranges::unique_copy(x, x+7, y, ranges::equal_to{}, &X::i);
return (in == ranges::end(x)
&& out == ranges::end(y)
&& ranges::equal(y, z, {}, &X::i, &X::i));
}
int
main()
{
test01<input_iterator_wrapper, output_iterator_wrapper>();
test01<input_iterator_wrapper, forward_iterator_wrapper>();
test01<forward_iterator_wrapper, output_iterator_wrapper>();
test02<input_iterator_wrapper, output_iterator_wrapper>();
test02<input_iterator_wrapper, forward_iterator_wrapper>();
test02<forward_iterator_wrapper, output_iterator_wrapper>();
test03<input_iterator_wrapper, output_iterator_wrapper>();
test03<input_iterator_wrapper, forward_iterator_wrapper>();
test03<forward_iterator_wrapper, output_iterator_wrapper>();
static_assert(test04());
}
// 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::forward_iterator_wrapper;
namespace ranges = std::ranges;
void
test01()
{
int x[] = {1, 2, 3, 4, 5, 5, 6, 7};
for (unsigned i = 0; i < 5; i++)
for (unsigned j = 6; j < 8; j++)
{
test_container<int, forward_iterator_wrapper> cx(x);
auto result = ranges::upper_bound(std::next(cx.begin(), i),
std::next(cx.begin(), j),
4, {}, [] (int a) { return a-1; });
VERIFY( result.ptr == x+6 );
}
ranges::reverse(x);
test_range<int, forward_iterator_wrapper> rx(x);
auto result = ranges::upper_bound(rx, 5, ranges::greater{},
[] (int a) { return a+1; });
VERIFY( result.ptr == x+5 );
}
constexpr bool
test02()
{
int x[] = {1, 2, 3, 4, 5};
return (ranges::upper_bound(x, 6) == x+5
&& ranges::upper_bound(x, x, 6) == x
&& ranges::upper_bound(x, 1) == x+1);
}
int
main()
{
test01();
static_assert(test02());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment