Merged with mainline at revision 128810.
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / binary_search / 2.cc
blob66567f23afd89281df9323670f5c8891f2586bb8
1 // Copyright (C) 2001 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 2, or (at your option)
7 // any later version.
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.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
19 // 25.3.3 [lib.alg.binary.search] Binary search algorithms.
21 #include <algorithm>
22 #include <testsuite_hooks.h>
24 bool test __attribute__((unused)) = true;
26 const int A[] = {1, 2, 3, 3, 3, 5, 8};
27 const int C[] = {8, 5, 3, 3, 3, 2, 1};
28 const int N = sizeof(A) / sizeof(int);
30 // A comparison, equalivalent to std::greater<int> without the
31 // dependency on <functional>.
32 struct gt
34 bool
35 operator()(const int& x, const int& y) const
36 { return x > y; }
39 // Each test performs general-case, bookend, not-found condition,
40 // and predicate functional checks.
42 // 25.3.3.1 lower_bound, with and without comparison predicate
43 void
44 test01()
46 using std::lower_bound;
48 const int first = A[0];
49 const int last = A[N - 1];
51 const int* p = lower_bound(A, A + N, 3);
52 VERIFY(p == A + 2);
54 const int* q = lower_bound(A, A + N, first);
55 VERIFY(q == A + 0);
57 const int* r = lower_bound(A, A + N, last);
58 VERIFY(r == A + N - 1);
60 const int* s = lower_bound(A, A + N, 4);
61 VERIFY(s == A + 5);
63 const int* t = lower_bound(C, C + N, 3, gt());
64 VERIFY(t == C + 2);
66 const int* u = lower_bound(C, C + N, first, gt());
67 VERIFY(u == C + N - 1);
69 const int* v = lower_bound(C, C + N, last, gt());
70 VERIFY(v == C + 0);
72 const int* w = lower_bound(C, C + N, 4, gt());
73 VERIFY(w == C + 2);
76 // 25.3.3.2 upper_bound, with and without comparison predicate
77 void
78 test02()
80 using std::upper_bound;
82 const int first = A[0];
83 const int last = A[N - 1];
85 const int* p = upper_bound(A, A + N, 3);
86 VERIFY(p == A + 5);
88 const int* q = upper_bound(A, A + N, first);
89 VERIFY(q == A + 1);
91 const int* r = upper_bound(A, A + N, last);
92 VERIFY(r == A + N);
94 const int* s = upper_bound(A, A + N, 4);
95 VERIFY(s == A + 5);
97 const int* t = upper_bound(C, C + N, 3, gt());
98 VERIFY(t == C + 5);
100 const int* u = upper_bound(C, C + N, first, gt());
101 VERIFY(u == C + N);
103 const int* v = upper_bound(C, C + N, last, gt());
104 VERIFY(v == C + 1);
106 const int* w = upper_bound(C, C + N, 4, gt());
107 VERIFY(w == C + 2);
110 // 25.3.3.3 equal_range, with and without comparison predicate
111 void
112 test03()
114 using std::equal_range;
115 typedef std::pair<const int*, const int*> Ipair;
117 const int first = A[0];
118 const int last = A[N - 1];
120 Ipair p = equal_range(A, A + N, 3);
121 VERIFY(p.first == A + 2);
122 VERIFY(p.second == A + 5);
124 Ipair q = equal_range(A, A + N, first);
125 VERIFY(q.first == A + 0);
126 VERIFY(q.second == A + 1);
128 Ipair r = equal_range(A, A + N, last);
129 VERIFY(r.first == A + N - 1);
130 VERIFY(r.second == A + N);
132 Ipair s = equal_range(A, A + N, 4);
133 VERIFY(s.first == A + 5);
134 VERIFY(s.second == A + 5);
136 Ipair t = equal_range(C, C + N, 3, gt());
137 VERIFY(t.first == C + 2);
138 VERIFY(t.second == C + 5);
140 Ipair u = equal_range(C, C + N, first, gt());
141 VERIFY(u.first == C + N - 1);
142 VERIFY(u.second == C + N);
144 Ipair v = equal_range(C, C + N, last, gt());
145 VERIFY(v.first == C + 0);
146 VERIFY(v.second == C + 1);
148 Ipair w = equal_range(C, C + N, 4, gt());
149 VERIFY(w.first == C + 2);
150 VERIFY(w.second == C + 2);
153 // 25.3.3.4 binary_search, with and without comparison predicate
154 void
155 test04()
157 using std::binary_search;
159 const int first = A[0];
160 const int last = A[N - 1];
162 VERIFY(binary_search(A, A + N, 5));
163 VERIFY(binary_search(A, A + N, first));
164 VERIFY(binary_search(A, A + N, last));
165 VERIFY(!binary_search(A, A + N, 4));
167 VERIFY(binary_search(C, C + N, 5, gt()));
168 VERIFY(binary_search(C, C + N, first, gt()));
169 VERIFY(binary_search(C, C + N, last, gt()));
170 VERIFY(!binary_search(C, C + N, 4, gt()));
174 main()
176 test01();
177 test02();
178 test03();
179 test04();
181 return 0;