]>
Commit | Line | Data |
---|---|---|
0aae8cfd PP |
1 | // Copyright (C) 2020 Free Software Foundation, Inc. |
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> | |
22 | #include <testsuite_hooks.h> | |
23 | #include <testsuite_iterators.h> | |
24 | ||
25 | using __gnu_test::test_container; | |
26 | using __gnu_test::test_range; | |
27 | using __gnu_test::input_iterator_wrapper; | |
28 | using __gnu_test::forward_iterator_wrapper; | |
29 | ||
30 | namespace ranges = std::ranges; | |
31 | ||
32 | struct X | |
33 | { | |
34 | int i; | |
35 | ||
36 | friend constexpr bool | |
37 | operator==(const X& a, const X& b) | |
38 | { | |
39 | return a.i == b.i; | |
40 | } | |
41 | }; | |
42 | ||
43 | void | |
44 | test01() | |
45 | { | |
46 | auto is_even_p = [] (int a) { return a%2 == 0; }; | |
47 | auto is_negative_p = [] (int a) { return a < 0; }; | |
48 | auto is_two_p = [] (int a) { return a == 2; }; | |
49 | { | |
50 | X x[6] = { {1}, {2}, {6}, {8}, {10}, {11} }; | |
51 | X y[6] = { {1}, {9}, {9}, {9}, {9}, {11} }; | |
52 | auto res = ranges::replace_if(x, x+5, is_even_p, X{9}, &X::i); | |
53 | VERIFY( res == x+5 ); | |
54 | VERIFY( ranges::equal(x, y) ); | |
55 | } | |
56 | ||
57 | { | |
58 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; | |
59 | X y[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; | |
60 | auto res = ranges::replace_if(x, x+5, is_negative_p, X{9}, &X::i); | |
61 | VERIFY( res == x+5 ); | |
62 | VERIFY( ranges::equal(x, y) ); | |
63 | } | |
64 | ||
65 | { | |
66 | X x[6] = { {2}, {2}, {6}, {8}, {10}, {11} }; | |
67 | X y[6] = { {7}, {7}, {6}, {8}, {10}, {11} }; | |
68 | test_container<X, forward_iterator_wrapper> cx(x), cy(y); | |
69 | auto res = ranges::replace_if(cx, is_two_p, X{7}, &X::i); | |
70 | VERIFY( res == cx.end() ); | |
71 | VERIFY( ranges::equal(cx, cy) ); | |
72 | } | |
73 | ||
74 | { | |
75 | int x[6] = { {2}, {2}, {6}, {8}, {10}, {2} }; | |
76 | int y[6] = { {7}, {7}, {6}, {8}, {10}, {7} }; | |
77 | test_range<int, input_iterator_wrapper> rx(x), ry(y); | |
78 | auto res = ranges::replace_if(rx, is_two_p, 7); | |
79 | VERIFY( res == rx.end() ); | |
3664a80b PP |
80 | |
81 | rx.bounds.first = x; | |
82 | ry.bounds.first = y; | |
0aae8cfd PP |
83 | VERIFY( ranges::equal(rx, ry) ); |
84 | } | |
85 | } | |
86 | ||
87 | struct Y { int i; int j; }; | |
88 | ||
89 | constexpr bool | |
90 | test02() | |
91 | { | |
92 | bool ok = true; | |
93 | Y x[] = { {3,2}, {2,4}, {3,6} }; | |
94 | Y y[] = { {4,5}, {2,4}, {4,5} }; | |
95 | auto res = ranges::replace_if(x, [] (int a) { return a%2 == 1; }, | |
96 | Y{4,5}, &Y::i); | |
97 | ok &= res == x+3; | |
98 | ok &= ranges::equal(x, y, {}, &Y::i, &Y::i); | |
99 | ok &= ranges::equal(x, y, {}, &Y::j, &Y::j); | |
100 | return ok; | |
101 | } | |
102 | ||
103 | int | |
104 | main() | |
105 | { | |
106 | test01(); | |
107 | static_assert(test02()); | |
108 | } | |
109 |