]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/upper_bound/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / upper_bound / 2.cc
1 // Copyright (C) 2001-2017 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.2 upper_bound, with and without comparison predicate
40 void
41 test02()
42 {
43 using std::upper_bound;
44
45 const int first = A[0];
46 const int last = A[N - 1];
47
48 const int* p = upper_bound(A, A + N, 3);
49 VERIFY(p == A + 5);
50
51 const int* q = upper_bound(A, A + N, first);
52 VERIFY(q == A + 1);
53
54 const int* r = upper_bound(A, A + N, last);
55 VERIFY(r == A + N);
56
57 const int* s = upper_bound(A, A + N, 4);
58 VERIFY(s == A + 5);
59
60 const int* t = upper_bound(C, C + N, 3, gt());
61 VERIFY(t == C + 5);
62
63 const int* u = upper_bound(C, C + N, first, gt());
64 VERIFY(u == C + N);
65
66 const int* v = upper_bound(C, C + N, last, gt());
67 VERIFY(v == C + 1);
68
69 const int* w = upper_bound(C, C + N, 4, gt());
70 VERIFY(w == C + 2);
71 }
72
73 int
74 main()
75 {
76 test02();
77 return 0;
78 }