1// Copyright (C) 2001-2015 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
23bool test __attribute__((unused)) = true;
24
25const int A[] = {1, 2, 3, 3, 3, 5, 8};
26const int C[] = {8, 5, 3, 3, 3, 2, 1};
27const int N = sizeof(A) / sizeof(int);
28
29// A comparison, equalivalent to std::greater<int> without the
30// dependency on <functional>.
31struct gt
32{
33    bool
34    operator()(const int& x, const int& y) const
35    { return x > y; }
36};
37
38// Each test performs general-case, bookend, not-found condition,
39// and predicate functional checks.
40
41// 25.3.3.2 upper_bound, with and without comparison predicate
42void
43test02()
44{
45    using std::upper_bound;
46
47    const int first = A[0];
48    const int last = A[N - 1];
49
50    const int* p = upper_bound(A, A + N, 3);
51    VERIFY(p == A + 5);
52
53    const int* q = upper_bound(A, A + N, first);
54    VERIFY(q == A + 1);
55
56    const int* r = upper_bound(A, A + N, last);
57    VERIFY(r == A + N);
58
59    const int* s = upper_bound(A, A + N, 4);
60    VERIFY(s == A + 5);
61
62    const int* t = upper_bound(C, C + N, 3, gt());
63    VERIFY(t == C + 5);
64
65    const int* u = upper_bound(C, C + N, first, gt());
66    VERIFY(u == C + N);
67
68    const int* v = upper_bound(C, C + N, last, gt());
69    VERIFY(v == C + 1);
70
71    const int* w = upper_bound(C, C + N, 4, gt());
72    VERIFY(w == C + 2);
73}
74
75int
76main()
77{
78  test02();
79  return 0;
80}
81