]>
Commit | Line | Data |
---|---|---|
ed920373 JW |
1 | // { dg-options "-std=gnu++17" } |
2 | // { dg-do run { target c++17 } } | |
3 | ||
8d9254fc | 4 | // Copyright (C) 2019-2020 Free Software Foundation, Inc. |
ed920373 JW |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 3, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING3. If not see | |
19 | // <http://www.gnu.org/licenses/>. | |
20 | ||
21 | #include <algorithm> | |
22 | #include <testsuite_hooks.h> | |
23 | #include <testsuite_iterators.h> | |
24 | ||
25 | void test01() | |
26 | { | |
27 | using __gnu_test::test_container; | |
28 | using __gnu_test::input_iterator_wrapper; | |
29 | int array[5] = { 1, 2, 3, 4, 5 }; | |
30 | test_container<int, input_iterator_wrapper> con(array); | |
31 | ||
32 | int sum = 0; | |
33 | struct Func | |
34 | { | |
35 | Func(int& i) : i(i) { } | |
36 | Func(Func&&) = default; | |
37 | Func& operator=(Func&&) = delete; | |
38 | void operator()(int n) const { i += n; } | |
39 | int& i; | |
40 | }; | |
41 | ||
42 | struct Size | |
43 | { | |
44 | Size(short v) : val(v) { } | |
45 | operator short() const { return val; } | |
46 | short val; | |
47 | }; | |
48 | auto res = std::for_each_n(con.begin(), Size(con.size()), Func(sum)); | |
49 | ||
52f6afe0 | 50 | VERIFY( res == con.end() ); |
ed920373 JW |
51 | VERIFY( sum == 15 ); |
52 | } | |
53 | ||
52f6afe0 JW |
54 | void |
55 | test02() | |
56 | { | |
57 | using __gnu_test::test_container; | |
58 | using __gnu_test::random_access_iterator_wrapper; | |
59 | int array[5] = { 2, 4, 6, 8, 10 }; | |
60 | test_container<int, random_access_iterator_wrapper> con(array); | |
61 | ||
62 | int prod = 1; | |
63 | struct Func | |
64 | { | |
65 | Func(int& i) : i(i) { } | |
66 | Func(Func&&) = default; | |
67 | Func& operator=(Func&&) = delete; | |
68 | void operator()(int n) const { i *= n; } | |
69 | int& i; | |
70 | }; | |
71 | ||
72 | struct Size | |
73 | { | |
74 | Size(short v) : val(v) { } | |
75 | operator short() const { return val; } | |
76 | short val; | |
77 | }; | |
78 | auto res = std::for_each_n(con.begin(), Size(con.size()), Func(prod)); | |
79 | ||
80 | VERIFY( res == con.end() ); | |
81 | VERIFY( prod == 3840 ); | |
82 | } | |
83 | ||
ed920373 JW |
84 | int main() |
85 | { | |
86 | test01(); | |
52f6afe0 | 87 | test02(); |
ed920373 | 88 | } |