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 | // constexpr Iter // constexpr after c++17 |
13 | // lower_bound(Iter first, Iter last, const T& value); |
14 | |
15 | #include <algorithm> |
16 | #include <vector> |
17 | #include <cassert> |
18 | #include <cstddef> |
19 | |
20 | #include "test_macros.h" |
21 | #include "test_iterators.h" |
22 | |
23 | #if TEST_STD_VER > 17 |
24 | |
25 | TEST_CONSTEXPR bool eq(int a, int b) { return a == b; } |
26 | |
27 | TEST_CONSTEXPR bool test_constexpr() { |
28 | int ia[] = {1, 3, 6, 7}; |
29 | |
30 | return (std::lower_bound(std::begin(ia), std::end(ia), 2) == ia+1) |
31 | && (std::lower_bound(std::begin(ia), std::end(ia), 3) == ia+1) |
32 | && (std::lower_bound(std::begin(ia), std::end(ia), 9) == std::end(ia)) |
33 | ; |
34 | } |
35 | #endif |
36 | |
37 | |
38 | template <class Iter, class T> |
39 | void |
40 | test(Iter first, Iter last, const T& value) |
41 | { |
42 | Iter i = std::lower_bound(first, last, value); |
43 | for (Iter j = first; j != i; ++j) |
44 | assert(*j < value); |
45 | for (Iter j = i; j != last; ++j) |
46 | assert(!(*j < value)); |
47 | } |
48 | |
49 | template <class Iter> |
50 | void |
51 | test() |
52 | { |
53 | const unsigned N = 1000; |
54 | const int M = 10; |
55 | std::vector<int> v(N); |
56 | int x = 0; |
57 | for (std::size_t i = 0; i < v.size(); ++i) |
58 | { |
59 | v[i] = x; |
60 | if (++x == M) |
61 | x = 0; |
62 | } |
63 | std::sort(first: v.begin(), last: v.end()); |
64 | for (x = 0; x <= M; ++x) |
65 | test(Iter(v.data()), Iter(v.data()+v.size()), x); |
66 | } |
67 | |
68 | int main(int, char**) |
69 | { |
70 | int d[] = {0, 1, 2, 3}; |
71 | for (int* e = d; e <= d+4; ++e) |
72 | for (int x = -1; x <= 4; ++x) |
73 | test(first: d, last: e, value: x); |
74 | |
75 | test<forward_iterator<const int*> >(); |
76 | test<bidirectional_iterator<const int*> >(); |
77 | test<random_access_iterator<const int*> >(); |
78 | test<const int*>(); |
79 | |
80 | #if TEST_STD_VER > 17 |
81 | static_assert(test_constexpr()); |
82 | #endif |
83 | |
84 | return 0; |
85 | } |
86 | |