Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/search_n/constrained.cc

raw
1 // Copyright (C) 2020-2025 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-do run { target c++20 } }
19
20 #include <algorithm>
21 #include <functional>
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 using __gnu_test::bidirectional_iterator_wrapper;
30
31 namespace ranges = std::ranges;
32
33 struct X { int i; };
34
35 void
36 test01()
37 {
38 int x[] = { {2}, {2}, {6}, {8}, {10}, {11} };
39 auto res = ranges::search_n(x+0, x+6, 2, 2);
40 VERIFY( res.begin() == x+0 && res.end() == x+2 );
41
42 int z[] = { {1}, {2}, {2}, {4}, {5}, {6} };
43 res = ranges::search_n(z, 3, 3, std::greater<int>());
44 VERIFY( res.begin() == z+3 && res.end() == z+6 );
45
46 test_container<int, forward_iterator_wrapper> cx(x);
47 auto res2 = ranges::search_n(cx, 2, 2);
48 VERIFY( res2.begin() == cx.begin() && *res2.end() == 6 );
49
50 int y[] = { {2}, {2}, {8}, {2}, {2}, {2}, {5} };
51 test_range<int, forward_iterator_wrapper> ry(y);
52 auto res3 = ranges::search_n(ry, 3, 2);
53 VERIFY( *res3.begin() == 2 && *res3.end() == 5 );
54
55 auto res4 = ranges::search_n(ry, 1, 8);
56 VERIFY( res4.begin().ptr == y+2 && res4.end().ptr == y+3 );
57 }
58
59 void
60 test02()
61 {
62 static constexpr X x[] = { {2}, {2}, {6}, {8}, {10}, {2} };
63 static constexpr X y[] = { {2}, {6}, {8}, {8}, {8}, {2} };
64 static constexpr int z[] = { {2}, {6}, {8}, {10}, {2}, {2} };
65
66 static_assert(ranges::search_n(z, 0, 5).end() == z+0);
67 static_assert(ranges::search_n(z, 1, 5).begin() == z+6);
68 static_assert(ranges::search_n(x, 2, 3, {}, &X::i).begin() == x+6);
69 static_assert(ranges::search_n(x, 2, 2, {}, &X::i).end() == x+2);
70 static_assert(ranges::search_n(y, 3, 8, {}, &X::i).begin() == y+2);
71 static_assert(ranges::search_n(y, 3, 8, {}, &X::i).end() == y+5);
72 }
73
74 int
75 main()
76 {
77 test01();
78 test02();
79 }
80