1 // Copyright (C) 2001 Free Software Foundation, Inc.
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)
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,
19 // 25.3.3 [lib.alg.binary.search] Binary search algorithms.
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>.
35 operator()(const int& x
, const int& y
) const
39 // Each test performs general-case, bookend, not-found condition,
40 // and predicate functional checks.
42 // 25.3.3.3 equal_range, with and without comparison predicate
46 using std::equal_range
;
47 typedef std::pair
<const int*, const int*> Ipair
;
49 const int first
= A
[0];
50 const int last
= A
[N
- 1];
52 Ipair p
= equal_range(A
, A
+ N
, 3);
53 VERIFY(p
.first
== A
+ 2);
54 VERIFY(p
.second
== A
+ 5);
56 Ipair q
= equal_range(A
, A
+ N
, first
);
57 VERIFY(q
.first
== A
+ 0);
58 VERIFY(q
.second
== A
+ 1);
60 Ipair r
= equal_range(A
, A
+ N
, last
);
61 VERIFY(r
.first
== A
+ N
- 1);
62 VERIFY(r
.second
== A
+ N
);
64 Ipair s
= equal_range(A
, A
+ N
, 4);
65 VERIFY(s
.first
== A
+ 5);
66 VERIFY(s
.second
== A
+ 5);
68 Ipair t
= equal_range(C
, C
+ N
, 3, gt());
69 VERIFY(t
.first
== C
+ 2);
70 VERIFY(t
.second
== C
+ 5);
72 Ipair u
= equal_range(C
, C
+ N
, first
, gt());
73 VERIFY(u
.first
== C
+ N
- 1);
74 VERIFY(u
.second
== C
+ N
);
76 Ipair v
= equal_range(C
, C
+ N
, last
, gt());
77 VERIFY(v
.first
== C
+ 0);
78 VERIFY(v
.second
== C
+ 1);
80 Ipair w
= equal_range(C
, C
+ N
, 4, gt());
81 VERIFY(w
.first
== C
+ 2);
82 VERIFY(w
.second
== C
+ 2);