1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 using __gnu_test::test_container
;
27 using __gnu_test::test_range
;
28 using __gnu_test::input_iterator_wrapper
;
29 using __gnu_test::forward_iterator_wrapper
;
30 using __gnu_test::random_access_iterator_wrapper
;
32 namespace ranges
= std::ranges
;
47 X x
[] = { {2}, {4}, {6}, {8}, {10}, {11} };
49 auto res
= ranges::for_each(x
, x
+6, f
, &X::i
);
50 VERIFY( res
.in
== x
+6 );
51 VERIFY( res
.fun
== &f
);
54 test_container
<X
, forward_iterator_wrapper
> c(x
);
56 ranges::for_each(c
, [&p
](int i
) { ++p
; }, &X::i
);
59 test_range
<X
, input_iterator_wrapper
> r(x
);
61 ranges::for_each(r
, [&q
](X
&) { ++q
; });
65 struct Y
{ int i
; int j
; };
72 Y y
[] = { {1,2}, {2,4}, {3,6} };
74 ranges::for_each(y
, [&a
](int i
) { a
+= i
; }, &Y::i
);
77 static_assert(f() == 6);
80 template<template<typename
> typename wrapper
>
84 int x
[] = {1,2,3,4,5};
85 test_range
<int, wrapper
> rx(x
);
87 auto func
= [&s
](int i
){ s
+= i
; };
88 auto [i
,f
] = ranges::for_each_n(rx
.begin(), 3, func
);
89 VERIFY( i
.ptr
= x
+3 );
92 VERIFY( s
== 1+2+3+1 );
96 auto [j
,g
] = ranges::for_each_n(rx
.begin(), -1, func
);
104 auto [k
,h
] = ranges::for_each_n(rx
.begin(), 5, func
, std::negate
<>{});
105 VERIFY( k
.ptr
== x
+5 );
106 VERIFY( s
== -(1+2+3+4+5) );
108 VERIFY( s
== -(1+2+3+4+5+6) );
114 int x
[] = {1,2,3,4,5};
116 ranges::for_each_n(x
+1, 4, [&p
](int i
){ p
*=i
; }, [](int i
){ return i
+1; });
125 test03
<input_iterator_wrapper
>();
126 test03
<random_access_iterator_wrapper
>();
127 static_assert(test04());