Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/lower_bound/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 <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
23
24 using __gnu_test::test_container;
25 using __gnu_test::test_range;
26 using __gnu_test::forward_iterator_wrapper;
27
28 namespace ranges = std::ranges;
29
30 void
31 test01()
32 {
33 int x[] = {1, 2, 3, 4, 5, 5, 6, 7};
34 for (unsigned i = 0; i < 5; i++)
35 for (unsigned j = 5; j < 8; j++)
36 {
37 test_container<int, forward_iterator_wrapper> cx(x);
38 auto result = ranges::lower_bound(std::next(cx.begin(), i),
39 std::next(cx.begin(), j),
40 4, {}, [] (int a) { return a-1; });
41 VERIFY( result.ptr == x+4 );
42 }
43
44 ranges::reverse(x);
45 test_range<int, forward_iterator_wrapper> rx(x);
46 auto result = ranges::lower_bound(rx, 5, ranges::greater{},
47 [] (int a) { return a+1; });
48 VERIFY( result.ptr == x+4 );
49 }
50
51 constexpr bool
52 test02()
53 {
54 int x[] = {1, 2, 3, 4, 5};
55 return (ranges::lower_bound(x, 6) == x+5
56 && ranges::lower_bound(x, x, 6) == x
57 && ranges::lower_bound(x, 1) == x);
58 }
59
60 int
61 main()
62 {
63 test01();
64 static_assert(test02());
65 }