]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/25_algorithms/for_each/constrained.cc
libstdc++: Reduce header dependencies from PSTL headers [PR92546]
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / for_each / constrained.cc
CommitLineData
7adcbafe 1// Copyright (C) 2020-2022 Free Software Foundation, Inc.
bc464641
PP
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
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.
13
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/>.
17
18// { dg-options "-std=gnu++2a" }
19// { dg-do run { target c++2a } }
20
21#include <algorithm>
ac73c944 22#include <functional>
bc464641
PP
23#include <testsuite_hooks.h>
24#include <testsuite_iterators.h>
25
26using __gnu_test::test_container;
27using __gnu_test::test_range;
28using __gnu_test::input_iterator_wrapper;
29using __gnu_test::forward_iterator_wrapper;
f3169941 30using __gnu_test::random_access_iterator_wrapper;
bc464641
PP
31
32namespace ranges = std::ranges;
33
34struct X { int i; };
35
36static int a;
37
38void
39f(int& i)
40{
41 a += i;
42}
43
44void
45test01()
46{
47 X x[] = { {2}, {4}, {6}, {8}, {10}, {11} };
48
49 auto res = ranges::for_each(x, x+6, f, &X::i);
50 VERIFY( res.in == x+6 );
51 VERIFY( res.fun == &f );
52 VERIFY( a == 41 );
53
54 test_container<X, forward_iterator_wrapper> c(x);
55 int p = 0;
56 ranges::for_each(c, [&p](int i) { ++p; }, &X::i);
57 VERIFY( p == 6 );
58
59 test_range<X, input_iterator_wrapper> r(x);
60 int q = 0;
61 ranges::for_each(r, [&q](X&) { ++q; });
62 VERIFY( q == 6 );
63}
64
65struct Y { int i; int j; };
66
67void
68test02()
69{
70 auto f = []
71 {
72 Y y[] = { {1,2}, {2,4}, {3,6} };
73 int a = 0;
74 ranges::for_each(y, [&a](int i) { a += i; }, &Y::i);
75 return a;
76 };
77 static_assert(f() == 6);
78}
79
f3169941
PP
80template<template<typename> typename wrapper>
81void
82test03()
83{
84 int x[] = {1,2,3,4,5};
85 test_range<int, wrapper> rx(x);
86 int s = 0;
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 );
90 VERIFY( s == 1+2+3 );
91 f(1);
92 VERIFY( s == 1+2+3+1 );
93
94 s = 0;
95 rx.bounds.first = x;
96 auto [j,g] = ranges::for_each_n(rx.begin(), -1, func);
97 VERIFY( j.ptr == x );
98 VERIFY( s == 0 );
99 g(1);
100 VERIFY( s == 1 );
101
102 s = 0;
103 rx.bounds.first = x;
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) );
107 h(-6);
108 VERIFY( s == -(1+2+3+4+5+6) );
109}
110
111constexpr bool
112test04()
113{
114 int x[] = {1,2,3,4,5};
115 int p = 1;
116 ranges::for_each_n(x+1, 4, [&p](int i){ p*=i; }, [](int i){ return i+1; });
117 return p == 3*4*5*6;
118}
119
bc464641
PP
120int
121main()
122{
123 test01();
124 test02();
f3169941
PP
125 test03<input_iterator_wrapper>();
126 test03<random_access_iterator_wrapper>();
127 static_assert(test04());
bc464641 128}