| 1 | // Copyright (C) 2001-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 | // 25.3.3 [lib.alg.binary.search] Binary search algorithms. |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | const int A[] = {1, 2, 3, 3, 3, 5, 8}; |
| 24 | const int C[] = {8, 5, 3, 3, 3, 2, 1}; |
| 25 | const int N = sizeof(A) / sizeof(int); |
| 26 | |
| 27 | // A comparison, equalivalent to std::greater<int> without the |
| 28 | // dependency on <functional>. |
| 29 | struct gt |
| 30 | { |
| 31 | bool |
| 32 | operator()(const int& x, const int& y) const |
| 33 | { return x > y; } |
| 34 | }; |
| 35 | |
| 36 | // Each test performs general-case, bookend, not-found condition, |
| 37 | // and predicate functional checks. |
| 38 | |
| 39 | // 25.3.3.3 equal_range, with and without comparison predicate |
| 40 | void |
| 41 | test03() |
| 42 | { |
| 43 | using std::equal_range; |
| 44 | typedef std::pair<const int*, const int*> Ipair; |
| 45 | |
| 46 | const int first = A[0]; |
| 47 | const int last = A[N - 1]; |
| 48 | |
| 49 | Ipair p = equal_range(A, A + N, 3); |
| 50 | VERIFY(p.first == A + 2); |
| 51 | VERIFY(p.second == A + 5); |
| 52 | |
| 53 | Ipair q = equal_range(A, A + N, first); |
| 54 | VERIFY(q.first == A + 0); |
| 55 | VERIFY(q.second == A + 1); |
| 56 | |
| 57 | Ipair r = equal_range(A, A + N, last); |
| 58 | VERIFY(r.first == A + N - 1); |
| 59 | VERIFY(r.second == A + N); |
| 60 | |
| 61 | Ipair s = equal_range(A, A + N, 4); |
| 62 | VERIFY(s.first == A + 5); |
| 63 | VERIFY(s.second == A + 5); |
| 64 | |
| 65 | Ipair t = equal_range(C, C + N, 3, gt()); |
| 66 | VERIFY(t.first == C + 2); |
| 67 | VERIFY(t.second == C + 5); |
| 68 | |
| 69 | Ipair u = equal_range(C, C + N, first, gt()); |
| 70 | VERIFY(u.first == C + N - 1); |
| 71 | VERIFY(u.second == C + N); |
| 72 | |
| 73 | Ipair v = equal_range(C, C + N, last, gt()); |
| 74 | VERIFY(v.first == C + 0); |
| 75 | VERIFY(v.second == C + 1); |
| 76 | |
| 77 | Ipair w = equal_range(C, C + N, 4, gt()); |
| 78 | VERIFY(w.first == C + 2); |
| 79 | VERIFY(w.second == C + 2); |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | main() |
| 84 | { |
| 85 | test03(); |
| 86 | return 0; |
| 87 | } |