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