1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <algorithm>
10
11// template<ForwardIterator Iter, class T>
12// requires HasLess<T, Iter::value_type>
13// && HasLess<Iter::value_type, T>
14// constexpr pair<Iter, Iter> // constexpr after c++17
15// equal_range(Iter first, Iter last, const T& value);
16
17#include <algorithm>
18#include <vector>
19#include <cassert>
20#include <cstddef>
21
22#include "test_macros.h"
23#include "test_iterators.h"
24
25#if TEST_STD_VER > 17
26TEST_CONSTEXPR bool lt(int a, int b) { return a < b; }
27
28TEST_CONSTEXPR bool test_constexpr() {
29 int ia[] = {1, 3, 3, 6, 7};
30
31 return (std::equal_range(std::begin(ia), std::end(ia), 1, lt) == std::pair<int *, int *>(ia+0, ia+1))
32 && (std::equal_range(std::begin(ia), std::end(ia), 3, lt) == std::pair<int *, int *>(ia+1, ia+3))
33 && (std::equal_range(std::begin(ia), std::end(ia), 9, lt) == std::pair<int *, int *>(std::end(ia), std::end(ia)))
34 ;
35 }
36#endif
37
38template <class Iter, class T>
39void
40test(Iter first, Iter last, const T& value)
41{
42 std::pair<Iter, Iter> i = std::equal_range(first, last, value);
43 for (Iter j = first; j != i.first; ++j)
44 assert(*j < value);
45 for (Iter j = i.first; j != last; ++j)
46 assert(!(*j < value));
47 for (Iter j = first; j != i.second; ++j)
48 assert(!(value < *j));
49 for (Iter j = i.second; j != last; ++j)
50 assert(value < *j);
51}
52
53template <class Iter>
54void
55test()
56{
57 const unsigned N = 1000;
58 const int M = 10;
59 std::vector<int> v(N);
60 int x = 0;
61 for (std::size_t i = 0; i < v.size(); ++i)
62 {
63 v[i] = x;
64 if (++x == M)
65 x = 0;
66 }
67 std::sort(first: v.begin(), last: v.end());
68 for (x = 0; x <= M; ++x)
69 test(Iter(v.data()), Iter(v.data()+v.size()), x);
70}
71
72int main(int, char**)
73{
74 int d[] = {0, 1, 2, 3};
75 for (int* e = d; e <= d+4; ++e)
76 for (int x = -1; x <= 4; ++x)
77 test(first: d, last: e, value: x);
78
79 test<forward_iterator<const int*> >();
80 test<bidirectional_iterator<const int*> >();
81 test<random_access_iterator<const int*> >();
82 test<const int*>();
83
84#if TEST_STD_VER > 17
85 static_assert(test_constexpr());
86#endif
87
88 return 0;
89}
90

source code of libcxx/test/std/algorithms/alg.sorting/alg.binary.search/equal.range/equal_range.pass.cpp