Merged revisions 195034,195219,195245,195357,195374,195428,195599,195673,195809 via...
[official-gcc.git] / main / libstdc++-v3 / testsuite / 25_algorithms / binary_search / 2.cc
blobf2af5cbd7200b5f96e40c514d88ec501156ca36a
1 // Copyright (C) 2001-2013 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.
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 COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // 25.3.3 [lib.alg.binary.search] Binary search algorithms.
20 #include <algorithm>
21 #include <testsuite_hooks.h>
23 bool test __attribute__((unused)) = true;
25 const int A[] = {1, 2, 3, 3, 3, 5, 8};
26 const int C[] = {8, 5, 3, 3, 3, 2, 1};
27 const int N = sizeof(A) / sizeof(int);
29 // A comparison, equalivalent to std::greater<int> without the
30 // dependency on <functional>.
31 struct gt
33 bool
34 operator()(const int& x, const int& y) const
35 { return x > y; }
38 // 25.3.3.4 binary_search, with and without comparison predicate
39 void
40 test04()
42 using std::binary_search;
44 const int first = A[0];
45 const int last = A[N - 1];
47 VERIFY(binary_search(A, A + N, 5));
48 VERIFY(binary_search(A, A + N, first));
49 VERIFY(binary_search(A, A + N, last));
50 VERIFY(!binary_search(A, A + N, 4));
52 VERIFY(binary_search(C, C + N, 5, gt()));
53 VERIFY(binary_search(C, C + N, first, gt()));
54 VERIFY(binary_search(C, C + N, last, gt()));
55 VERIFY(!binary_search(C, C + N, 4, gt()));
58 int
59 main()
61 test04();
62 return 0;