| 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 | #include <algorithm> |
| 12 | #include <cstddef> |
| 13 | #include <functional> |
| 14 | #include <iterator> |
| 15 | |
| 16 | #include "test_macros.h" |
| 17 | |
| 18 | struct Incomplete; |
| 19 | template<class T> struct Holder { T t; }; |
| 20 | |
| 21 | template<class> |
| 22 | struct ConvertibleToIntegral { |
| 23 | TEST_CONSTEXPR operator int() const { return 1; } |
| 24 | }; |
| 25 | |
| 26 | struct Tester { |
| 27 | using Element = Holder<Incomplete>*; |
| 28 | Element data[10] = {}; |
| 29 | }; |
| 30 | |
| 31 | struct UnaryVoid { TEST_CONSTEXPR_CXX14 void operator()(void*) const {} }; |
| 32 | struct UnaryTrue { TEST_CONSTEXPR bool operator()(void*) const { return true; } }; |
| 33 | struct NullaryValue { TEST_CONSTEXPR std::nullptr_t operator()() const { return nullptr; } }; |
| 34 | struct UnaryTransform { TEST_CONSTEXPR std::nullptr_t operator()(void*) const { return nullptr; } }; |
| 35 | struct BinaryTransform { TEST_CONSTEXPR std::nullptr_t operator()(void*, void*) const { return nullptr; } }; |
| 36 | |
| 37 | TEST_CONSTEXPR_CXX20 bool all_the_algorithms() |
| 38 | { |
| 39 | Tester t; |
| 40 | Tester u; |
| 41 | Holder<Incomplete> **first = t.data; |
| 42 | Holder<Incomplete> **mid = t.data+5; |
| 43 | Holder<Incomplete> **last = t.data+10; |
| 44 | Holder<Incomplete> **first2 = u.data; |
| 45 | Holder<Incomplete> **mid2 = u.data+5; |
| 46 | Holder<Incomplete> **last2 = u.data+10; |
| 47 | Tester::Element value = nullptr; |
| 48 | ConvertibleToIntegral<Tester::Element> count; |
| 49 | |
| 50 | (void)std::adjacent_find(first: first, last: last); |
| 51 | (void)std::adjacent_find(first: first, last: last, binary_pred: std::equal_to<void*>()); |
| 52 | #if TEST_STD_VER >= 11 |
| 53 | (void)std::all_of(first, last, UnaryTrue()); |
| 54 | (void)std::any_of(first, last, UnaryTrue()); |
| 55 | #endif |
| 56 | (void)std::binary_search(first: first, last: last, val: value); |
| 57 | (void)std::binary_search(first: first, last: last, val: value, comp: std::less<void*>()); |
| 58 | #if TEST_STD_VER > 17 |
| 59 | (void)std::clamp(value, value, value); |
| 60 | (void)std::clamp(value, value, value, std::less<void*>()); |
| 61 | #endif |
| 62 | (void)std::copy(first: first, last: last, result: first2); |
| 63 | (void)std::copy_backward(first: first, last: last, result: last2); |
| 64 | (void)std::copy_n(first: first, n: count, result: first2); |
| 65 | (void)std::count(first: first, last: last, value: value); |
| 66 | (void)std::count_if(first: first, last: last, pred: UnaryTrue()); |
| 67 | (void)std::distance(first: first, last: last); |
| 68 | (void)std::equal(first1: first, last1: last, first2: first2); |
| 69 | (void)std::equal(first1: first, last1: last, first2: first2, binary_pred: std::equal_to<void*>()); |
| 70 | #if TEST_STD_VER > 11 |
| 71 | (void)std::equal(first, last, first2, last2); |
| 72 | (void)std::equal(first, last, first2, last2, std::equal_to<void*>()); |
| 73 | #endif |
| 74 | (void)std::equal_range(first: first, last: last, val: value); |
| 75 | (void)std::equal_range(first: first, last: last, val: value, comp: std::less<void*>()); |
| 76 | (void)std::fill(first: first, last: last, value: value); |
| 77 | (void)std::fill_n(first: first, n: count, value: value); |
| 78 | (void)std::find(first: first, last: last, val: value); |
| 79 | (void)std::find_end(first1: first, last1: last, first2: first2, last2: mid2); |
| 80 | (void)std::find_end(first1: first, last1: last, first2: first2, last2: mid2, comp: std::equal_to<void*>()); |
| 81 | (void)std::find_if(first: first, last: last, pred: UnaryTrue()); |
| 82 | (void)std::find_if_not(first: first, last: last, pred: UnaryTrue()); |
| 83 | (void)std::for_each(first: first, last: last, f: UnaryVoid()); |
| 84 | #if TEST_STD_VER > 14 |
| 85 | (void)std::for_each_n(first, count, UnaryVoid()); |
| 86 | #endif |
| 87 | (void)std::generate(first: first, last: last, gen: NullaryValue()); |
| 88 | (void)std::generate_n(first: first, n: count, gen: NullaryValue()); |
| 89 | (void)std::includes(first1: first, last1: last, first2: first2, last2: last2); |
| 90 | (void)std::includes(first1: first, last1: last, first2: first2, last2: last2, comp: std::less<void*>()); |
| 91 | (void)std::is_heap(first: first, last: last); |
| 92 | (void)std::is_heap(first: first, last: last, comp: std::less<void*>()); |
| 93 | (void)std::is_heap_until(first: first, last: last); |
| 94 | (void)std::is_heap_until(first: first, last: last, comp: std::less<void*>()); |
| 95 | (void)std::is_partitioned(first: first, last: last, pred: UnaryTrue()); |
| 96 | (void)std::is_permutation(first1: first, last1: last, first2: first2); |
| 97 | (void)std::is_permutation(first1: first, last1: last, first2: first2, pred: std::equal_to<void*>()); |
| 98 | #if TEST_STD_VER > 11 |
| 99 | (void)std::is_permutation(first, last, first2, last2); |
| 100 | (void)std::is_permutation(first, last, first2, last2, std::equal_to<void*>()); |
| 101 | #endif |
| 102 | (void)std::is_sorted(first: first, last: last); |
| 103 | (void)std::is_sorted(first: first, last: last, comp: std::less<void*>()); |
| 104 | (void)std::is_sorted_until(first: first, last: last); |
| 105 | (void)std::is_sorted_until(first: first, last: last, comp: std::less<void*>()); |
| 106 | // RELIES ON ADL SWAP (void)std::inplace_merge(first, mid, last); |
| 107 | // RELIES ON ADL SWAP (void)std::inplace_merge(first, mid, last, std::less<void*>()); |
| 108 | // RELIES ON ADL SWAP (void)std::iter_swap(first, mid); |
| 109 | (void)std::lexicographical_compare(first1: first, last1: last, first2: first2, last2: last2); |
| 110 | (void)std::lexicographical_compare(first1: first, last1: last, first2: first2, last2: last2, comp: std::less<void*>()); |
| 111 | #if TEST_STD_VER > 17 |
| 112 | (void)std::lexicographical_compare_three_way(first, last, first2, last2); |
| 113 | (void)std::lexicographical_compare_three_way(first, last, first2, last2, std::compare_three_way()); |
| 114 | #endif |
| 115 | (void)std::lower_bound(first: first, last: last, val: value); |
| 116 | (void)std::lower_bound(first: first, last: last, val: value, comp: std::less<void*>()); |
| 117 | (void)std::make_heap(first: first, last: last); |
| 118 | (void)std::make_heap(first: first, last: last, comp: std::less<void*>()); |
| 119 | (void)std::max(a: value, b: value); |
| 120 | (void)std::max(a: value, b: value, comp: std::less<void*>()); |
| 121 | #if TEST_STD_VER >= 11 |
| 122 | (void)std::max({ value, value }); |
| 123 | (void)std::max({ value, value }, std::less<void*>()); |
| 124 | #endif |
| 125 | (void)std::max_element(first: first, last: last); |
| 126 | (void)std::max_element(first: first, last: last, comp: std::less<void*>()); |
| 127 | (void)std::merge(first1: first, last1: mid, first2: mid, last2: last, result: first2); |
| 128 | (void)std::merge(first1: first, last1: mid, first2: mid, last2: last, result: first2, comp: std::less<void*>()); |
| 129 | (void)std::min(a: value, b: value); |
| 130 | (void)std::min(a: value, b: value, comp: std::less<void*>()); |
| 131 | #if TEST_STD_VER >= 11 |
| 132 | (void)std::min({ value, value }); |
| 133 | (void)std::min({ value, value }, std::less<void*>()); |
| 134 | #endif |
| 135 | (void)std::min_element(first: first, last: last); |
| 136 | (void)std::min_element(first: first, last: last, comp: std::less<void*>()); |
| 137 | (void)std::minmax(a: value, b: value); |
| 138 | (void)std::minmax(a: value, b: value, comp: std::less<void*>()); |
| 139 | #if TEST_STD_VER >= 11 |
| 140 | (void)std::minmax({ value, value }); |
| 141 | (void)std::minmax({ value, value }, std::less<void*>()); |
| 142 | #endif |
| 143 | (void)std::minmax_element(first: first, last: last); |
| 144 | (void)std::minmax_element(first: first, last: last, comp: std::less<void*>()); |
| 145 | (void)std::mismatch(first1: first, last1: last, first2: first2); |
| 146 | (void)std::mismatch(first1: first, last1: last, first2: first2, binary_pred: std::equal_to<void*>()); |
| 147 | #if TEST_STD_VER > 11 |
| 148 | (void)std::mismatch(first, last, first2, last2); |
| 149 | (void)std::mismatch(first, last, first2, last2, std::equal_to<void*>()); |
| 150 | #endif |
| 151 | (void)std::move(first: first, last: last, result: first2); |
| 152 | (void)std::move_backward(first: first, last: last, result: last2); |
| 153 | // RELIES ON ADL SWAP (void)std::next_permutation(first, last); |
| 154 | // RELIES ON ADL SWAP (void)std::next_permutation(first, last, std::less<void*>()); |
| 155 | #if TEST_STD_VER >= 11 |
| 156 | (void)std::none_of(first, last, UnaryTrue()); |
| 157 | #endif |
| 158 | // RELIES ON ADL SWAP (void)std::nth_element(first, mid, last); |
| 159 | // RELIES ON ADL SWAP (void)std::nth_element(first, mid, last, std::less<void*>()); |
| 160 | // RELIES ON ADL SWAP (void)std::partial_sort(first, mid, last); |
| 161 | // RELIES ON ADL SWAP (void)std::partial_sort(first, mid, last, std::less<void*>()); |
| 162 | (void)std::partial_sort_copy(first: first, last: last, result_first: first2, result_last: mid2); |
| 163 | (void)std::partial_sort_copy(first: first, last: last, result_first: first2, result_last: mid2, comp: std::less<void*>()); |
| 164 | // RELIES ON ADL SWAP (void)std::partition(first, last, UnaryTrue()); |
| 165 | (void)std::partition_copy(first: first, last: last, out_true: first2, out_false: last2, pred: UnaryTrue()); |
| 166 | (void)std::partition_point(first: first, last: last, pred: UnaryTrue()); |
| 167 | (void)std::pop_heap(first: first, last: last); |
| 168 | (void)std::pop_heap(first: first, last: last, comp: std::less<void*>()); |
| 169 | // RELIES ON ADL SWAP (void)std::prev_permutation(first, last); |
| 170 | // RELIES ON ADL SWAP (void)std::prev_permutation(first, last, std::less<void*>()); |
| 171 | (void)std::push_heap(first: first, last: last); |
| 172 | (void)std::push_heap(first: first, last: last, comp: std::less<void*>()); |
| 173 | (void)std::remove(first: first, last: last, value: value); |
| 174 | (void)std::remove_copy(first: first, last: last, result: first2, value: value); |
| 175 | (void)std::remove_copy_if(first: first, last: last, result: first2, pred: UnaryTrue()); |
| 176 | (void)std::remove_if(first: first, last: last, pred: UnaryTrue()); |
| 177 | (void)std::replace(first: first, last: last, old_value: value, new_value: value); |
| 178 | (void)std::replace_copy(first: first, last: last, result: first2, old_value: value, new_value: value); |
| 179 | (void)std::replace_copy_if(first: first, last: last, result: first2, pred: UnaryTrue(), new_value: value); |
| 180 | (void)std::replace_if(first: first, last: last, pred: UnaryTrue(), new_value: value); |
| 181 | // RELIES ON ADL SWAP (void)std::reverse(first, last); |
| 182 | (void)std::reverse_copy(first: first, last: last, result: first2); |
| 183 | // RELIES ON ADL SWAP (void)std::rotate(first, mid, last); |
| 184 | (void)std::rotate_copy(first: first, middle: mid, last: last, result: first2); |
| 185 | (void)std::search(first1: first, last1: last, first2: first2, last2: mid2); |
| 186 | (void)std::search(first1: first, last1: last, first2: first2, last2: mid2, predicate: std::equal_to<void*>()); |
| 187 | (void)std::search_n(first: first, last: last, count: count, val: value); |
| 188 | (void)std::search_n(first: first, last: last, count: count, val: value, binary_pred: std::equal_to<void*>()); |
| 189 | (void)std::set_difference(first1: first, last1: mid, first2: mid, last2: last, result: first2); |
| 190 | (void)std::set_difference(first1: first, last1: mid, first2: mid, last2: last, result: first2, comp: std::less<void*>()); |
| 191 | (void)std::set_intersection(first1: first, last1: mid, first2: mid, last2: last, result: first2); |
| 192 | (void)std::set_intersection(first1: first, last1: mid, first2: mid, last2: last, result: first2, comp: std::less<void*>()); |
| 193 | (void)std::set_symmetric_difference(first1: first, last1: mid, first2: mid, last2: last, result: first2); |
| 194 | (void)std::set_symmetric_difference(first1: first, last1: mid, first2: mid, last2: last, result: first2, comp: std::less<void*>()); |
| 195 | (void)std::set_union(first1: first, last1: mid, first2: mid, last2: last, result: first2); |
| 196 | (void)std::set_union(first1: first, last1: mid, first2: mid, last2: last, result: first2, comp: std::less<void*>()); |
| 197 | #if TEST_STD_VER > 17 |
| 198 | (void)std::shift_left(first, last, count); |
| 199 | (void)std::shift_right(first, last, count); |
| 200 | #endif |
| 201 | // RELIES ON ADL SWAP (void)std::sort(first, last); |
| 202 | // RELIES ON ADL SWAP (void)std::sort(first, last, std::less<void*>()); |
| 203 | (void)std::sort_heap(first: first, last: last); |
| 204 | (void)std::sort_heap(first: first, last: last, comp: std::less<void*>()); |
| 205 | // RELIES ON ADL SWAP (void)std::stable_partition(first, last, UnaryTrue()); |
| 206 | // RELIES ON ADL SWAP (void)std::stable_sort(first, last); |
| 207 | // RELIES ON ADL SWAP (void)std::stable_sort(first, last, std::less<void*>()); |
| 208 | // RELIES ON ADL SWAP (void)std::swap_ranges(first, last, first2); |
| 209 | (void)std::transform(first: first, last: last, result: first2, unary_op: UnaryTransform()); |
| 210 | (void)std::transform(first1: first, last1: mid, first2: mid, result: first2, binary_op: BinaryTransform()); |
| 211 | (void)std::unique(first: first, last: last); |
| 212 | (void)std::unique(first: first, last: last, binary_pred: std::equal_to<void*>()); |
| 213 | (void)std::unique_copy(first: first, last: last, result: first2); |
| 214 | (void)std::unique_copy(first: first, last: last, result: first2, binary_pred: std::equal_to<void*>()); |
| 215 | (void)std::upper_bound(first: first, last: last, val: value); |
| 216 | (void)std::upper_bound(first: first, last: last, val: value, comp: std::less<void*>()); |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | void test() |
| 222 | { |
| 223 | all_the_algorithms(); |
| 224 | #if TEST_STD_VER > 17 |
| 225 | static_assert(all_the_algorithms()); |
| 226 | #endif |
| 227 | } |
| 228 | |