|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cassert> |
| 6 | +#include <concepts> |
| 7 | +#include <ranges> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +#include <range_algorithm_support.hpp> |
| 11 | + |
| 12 | +using namespace std; |
| 13 | +using P = pair<int, int>; |
| 14 | + |
| 15 | +// Validate that merge_result aliases in_in_out_result |
| 16 | +STATIC_ASSERT(same_as<ranges::merge_result<int, void*, double>, ranges::in_in_out_result<int, void*, double>>); |
| 17 | + |
| 18 | +// Validate dangling story |
| 19 | +STATIC_ASSERT(same_as<decltype(ranges::merge(borrowed<false>{}, borrowed<false>{}, nullptr_to<int>)), |
| 20 | + ranges::merge_result<ranges::dangling, ranges::dangling, int*>>); |
| 21 | +STATIC_ASSERT(same_as<decltype(ranges::merge(borrowed<false>{}, borrowed<true>{}, nullptr_to<int>)), |
| 22 | + ranges::merge_result<ranges::dangling, int*, int*>>); |
| 23 | +STATIC_ASSERT(same_as<decltype(ranges::merge(borrowed<true>{}, borrowed<false>{}, nullptr_to<int>)), |
| 24 | + ranges::merge_result<int*, ranges::dangling, int*>>); |
| 25 | +STATIC_ASSERT(same_as<decltype(ranges::merge(borrowed<true>{}, borrowed<true>{}, nullptr_to<int>)), |
| 26 | + ranges::merge_result<int*, int*, int*>>); |
| 27 | + |
| 28 | +struct instantiator { |
| 29 | + static constexpr P elements1[] = {{0, 10}, {0, 11}, {0, 12}, {1, 10}, {1, 11}, {3, 10}}; |
| 30 | + static constexpr P elements2[] = {{13, 0}, {14, 0}, {10, 2}, {11, 3}, {12, 3}}; |
| 31 | + static constexpr P expected[] = { |
| 32 | + {0, 10}, {0, 11}, {0, 12}, {13, 0}, {14, 0}, {1, 10}, {1, 11}, {10, 2}, {3, 10}, {11, 3}, {12, 3}}; |
| 33 | + |
| 34 | + static constexpr auto counting_compare(size_t& counter) { |
| 35 | + return [&counter](auto&& x, auto&& y) { |
| 36 | + ++counter; |
| 37 | + return ranges::less{}(x, y); |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + template <ranges::input_range R1, ranges::input_range R2, weakly_incrementable O> |
| 42 | + static constexpr void call() { |
| 43 | + using ranges::merge, ranges::merge_result, ranges::end, ranges::equal, ranges::iterator_t, ranges::size; |
| 44 | + |
| 45 | + { // Validate range overload |
| 46 | + P output[size(expected)]{}; |
| 47 | + R1 range1{elements1}; |
| 48 | + R2 range2{elements2}; |
| 49 | + size_t counter = 0; |
| 50 | + |
| 51 | + const same_as<merge_result<iterator_t<R1>, iterator_t<R2>, O>> auto result = |
| 52 | + merge(range1, range2, O{output}, counting_compare(counter), get_first, get_second); |
| 53 | + assert(result.in1 == range1.end()); |
| 54 | + assert(result.in2 == range2.end()); |
| 55 | + assert(result.out.peek() == end(output)); |
| 56 | + assert(equal(output, expected)); |
| 57 | + assert(counter <= size(elements1) + size(elements2) - 1); |
| 58 | + } |
| 59 | + { // Validate iterator overload |
| 60 | + P output[size(expected)]{}; |
| 61 | + R1 range1{elements1}; |
| 62 | + R2 range2{elements2}; |
| 63 | + size_t counter = 0; |
| 64 | + |
| 65 | + const same_as<merge_result<iterator_t<R1>, iterator_t<R2>, O>> auto result = |
| 66 | + merge(range1.begin(), range1.end(), range2.begin(), range2.end(), O{output}, counting_compare(counter), |
| 67 | + get_first, get_second); |
| 68 | + assert(result.in1 == range1.end()); |
| 69 | + assert(result.in2 == range2.end()); |
| 70 | + assert(result.out.peek() == end(output)); |
| 71 | + assert(equal(output, expected)); |
| 72 | + assert(counter <= size(elements1) + size(elements2) - 1); |
| 73 | + } |
| 74 | + |
| 75 | + { // Validate range overload, empty range1 |
| 76 | + P output[size(elements2)]{}; |
| 77 | + R1 range1{}; |
| 78 | + R2 range2{elements2}; |
| 79 | + size_t counter = 0; |
| 80 | + |
| 81 | + const same_as<merge_result<iterator_t<R1>, iterator_t<R2>, O>> auto result = |
| 82 | + merge(range1, range2, O{output}, counting_compare(counter), get_first, get_second); |
| 83 | + assert(result.in1 == range1.end()); |
| 84 | + assert(result.in2 == range2.end()); |
| 85 | + assert(result.out.peek() == end(output)); |
| 86 | + assert(equal(output, elements2)); |
| 87 | + assert(counter == 0); |
| 88 | + } |
| 89 | + { // Validate iterator overload, empty range2 |
| 90 | + P output[size(elements1)]{}; |
| 91 | + R1 range1{elements1}; |
| 92 | + R2 range2{}; |
| 93 | + size_t counter = 0; |
| 94 | + |
| 95 | + const same_as<merge_result<iterator_t<R1>, iterator_t<R2>, O>> auto result = |
| 96 | + merge(range1.begin(), range1.end(), range2.begin(), range2.end(), O{output}, counting_compare(counter), |
| 97 | + get_first, get_second); |
| 98 | + assert(result.in1 == range1.end()); |
| 99 | + assert(result.in2 == range2.end()); |
| 100 | + assert(result.out.peek() == end(output)); |
| 101 | + assert(equal(output, elements1)); |
| 102 | + assert(counter == 0); |
| 103 | + } |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +template <class Continuation> |
| 108 | +struct generate_readable_ranges { |
| 109 | + template <class... Args> |
| 110 | + static constexpr void call() { |
| 111 | + using namespace test; |
| 112 | + using test::range; |
| 113 | + |
| 114 | + // The algorithm is completely oblivious to: |
| 115 | + // * categories stronger than input |
| 116 | + // * whether the end sentinel is an iterator |
| 117 | + // * size information |
| 118 | + // * iterator and/or sentinel differencing |
| 119 | + // so let's vary proxyness for coverage and call it good. |
| 120 | + |
| 121 | + Continuation::template call<Args..., |
| 122 | + range<input, const P, Sized::no, CanDifference::no, Common::no, CanCompare::no, ProxyRef::no>>(); |
| 123 | + Continuation::template call<Args..., |
| 124 | + range<input, const P, Sized::no, CanDifference::no, Common::no, CanCompare::no, ProxyRef::yes>>(); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +template <class Continuation> |
| 129 | +struct generate_writable_iterators { |
| 130 | + template <class... Args> |
| 131 | + static constexpr void call() { |
| 132 | + using namespace test; |
| 133 | + using test::iterator; |
| 134 | + |
| 135 | + // The algorithm is completely oblivious to all properties except for proxyness, |
| 136 | + // so again we'll vary that property, and we'll also get coverage from input iterators to ensure the algorithm |
| 137 | + // doesn't inadvertently depend on the output_iterator-only `*i++ = meow` expression. |
| 138 | + |
| 139 | + Continuation::template call<Args..., iterator<output, P, CanDifference::no, CanCompare::no, ProxyRef::no>>(); |
| 140 | + Continuation::template call<Args..., iterator<output, P, CanDifference::no, CanCompare::no, ProxyRef::yes>>(); |
| 141 | + |
| 142 | + Continuation::template call<Args..., iterator<input, P, CanDifference::no, CanCompare::no, ProxyRef::no>>(); |
| 143 | + Continuation::template call<Args..., iterator<input, P, CanDifference::no, CanCompare::no, ProxyRef::yes>>(); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +constexpr void run_tests() { |
| 148 | + generate_readable_ranges<generate_readable_ranges<generate_writable_iterators<instantiator>>>::call(); |
| 149 | +} |
| 150 | + |
| 151 | +int main() { |
| 152 | + STATIC_ASSERT((run_tests(), true)); |
| 153 | + run_tests(); |
| 154 | +} |
0 commit comments