|
| 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 binary_transform_result aliases in_in_out_result |
| 16 | +STATIC_ASSERT(same_as<ranges::binary_transform_result<int, int, double>, ranges::in_in_out_result<int, int, double>>); |
| 17 | + |
| 18 | +// Validate dangling story |
| 19 | +STATIC_ASSERT( |
| 20 | + same_as<decltype(ranges::transform(borrowed<false>{}, borrowed<false>{}, static_cast<int*>(nullptr), plus{})), |
| 21 | + ranges::binary_transform_result<ranges::dangling, ranges::dangling, int*>>); |
| 22 | +STATIC_ASSERT( |
| 23 | + same_as<decltype(ranges::transform(borrowed<false>{}, borrowed<true>{}, static_cast<int*>(nullptr), plus{})), |
| 24 | + ranges::binary_transform_result<ranges::dangling, int*, int*>>); |
| 25 | +STATIC_ASSERT( |
| 26 | + same_as<decltype(ranges::transform(borrowed<true>{}, borrowed<false>{}, static_cast<int*>(nullptr), plus{})), |
| 27 | + ranges::binary_transform_result<int*, ranges::dangling, int*>>); |
| 28 | +STATIC_ASSERT( |
| 29 | + same_as<decltype(ranges::transform(borrowed<true>{}, borrowed<true>{}, static_cast<int*>(nullptr), plus{})), |
| 30 | + ranges::binary_transform_result<int*, int*, int*>>); |
| 31 | + |
| 32 | +struct instantiator { |
| 33 | + static constexpr P input1[3] = {{1, 99}, {4, 98}, {5, 97}}; |
| 34 | + static constexpr P input2[3] = {{99, 6}, {98, 7}, {97, 8}}; |
| 35 | + static constexpr int expected[3] = {7, 11, 13}; |
| 36 | + |
| 37 | + static constexpr P shortInput1[2] = {{1, 99}, {4, 98}}; |
| 38 | + static constexpr P shortInput2[2] = {{99, 6}, {98, 7}}; |
| 39 | + static constexpr int shortExpected[2] = {7, 11}; |
| 40 | + |
| 41 | + template <ranges::input_range Read1, ranges::input_range Read2, weakly_incrementable Write> |
| 42 | + static constexpr void call() { |
| 43 | + using ranges::transform, ranges::binary_transform_result, ranges::iterator_t; |
| 44 | + { // Validate iterator + sentinel overload |
| 45 | + int output[3] = {-1, -1, -1}; |
| 46 | + Read1 wrapped_in1{input1}; |
| 47 | + Read2 wrapped_in2{input2}; |
| 48 | + |
| 49 | + auto result = transform(wrapped_in1.begin(), wrapped_in1.end(), wrapped_in2.begin(), wrapped_in2.end(), |
| 50 | + Write{output}, plus{}, get_first, get_second); |
| 51 | + STATIC_ASSERT( |
| 52 | + same_as<decltype(result), binary_transform_result<iterator_t<Read1>, iterator_t<Read2>, Write>>); |
| 53 | + assert(result.in1 == wrapped_in1.end()); |
| 54 | + assert(result.in2 == wrapped_in2.end()); |
| 55 | + assert(result.out.peek() == output + 3); |
| 56 | + assert(ranges::equal(output, expected)); |
| 57 | + } |
| 58 | + { // Validate range overload |
| 59 | + int output[3] = {-1, -1, -1}; |
| 60 | + Read1 wrapped_in1{input1}; |
| 61 | + Read2 wrapped_in2{input2}; |
| 62 | + |
| 63 | + auto result = transform(wrapped_in1, wrapped_in2, Write{output}, plus{}, get_first, get_second); |
| 64 | + STATIC_ASSERT( |
| 65 | + same_as<decltype(result), binary_transform_result<iterator_t<Read1>, iterator_t<Read2>, Write>>); |
| 66 | + assert(result.in1 == wrapped_in1.end()); |
| 67 | + assert(result.in2 == wrapped_in2.end()); |
| 68 | + assert(result.out.peek() == output + 3); |
| 69 | + assert(ranges::equal(output, expected)); |
| 70 | + } |
| 71 | + { // Validate range overload first range shorter |
| 72 | + int output[2] = {-1, -1}; |
| 73 | + Read1 wrapped_in1{shortInput1}; |
| 74 | + Read2 wrapped_in2{input2}; |
| 75 | + |
| 76 | + auto result = transform(wrapped_in1, wrapped_in2, Write{output}, plus{}, get_first, get_second); |
| 77 | + STATIC_ASSERT( |
| 78 | + same_as<decltype(result), binary_transform_result<iterator_t<Read1>, iterator_t<Read2>, Write>>); |
| 79 | + assert(result.in1 == wrapped_in1.end()); |
| 80 | + assert(next(result.in2) == wrapped_in2.end()); |
| 81 | + assert(result.out.peek() == output + 2); |
| 82 | + assert(ranges::equal(output, shortExpected)); |
| 83 | + } |
| 84 | + { // Validate range overload second range shorter |
| 85 | + int output[2] = {-1, -1}; |
| 86 | + Read1 wrapped_in1{input1}; |
| 87 | + Read2 wrapped_in2{shortInput2}; |
| 88 | + |
| 89 | + auto result = transform(wrapped_in1, wrapped_in2, Write{output}, plus{}, get_first, get_second); |
| 90 | + STATIC_ASSERT( |
| 91 | + same_as<decltype(result), binary_transform_result<iterator_t<Read1>, iterator_t<Read2>, Write>>); |
| 92 | + assert(next(result.in1) == wrapped_in1.end()); |
| 93 | + assert(result.in2 == wrapped_in2.end()); |
| 94 | + assert(result.out.peek() == output + 2); |
| 95 | + assert(ranges::equal(output, shortExpected)); |
| 96 | + } |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +using Elem1 = const P; |
| 101 | +using Elem2 = const P; |
| 102 | +using Elem3 = int; |
| 103 | + |
| 104 | +#ifdef TEST_EVERYTHING |
| 105 | +int main() { |
| 106 | + // No constexpr test here; the test_in_in_write call exceeds the maximum number of steps in a constexpr computation. |
| 107 | + test_in_in_write<instantiator, Elem1, Elem2, Elem3>(); |
| 108 | +} |
| 109 | +#else // ^^^ test all range combinations // test only interesting range combos vvv |
| 110 | +template <class Elem, test::Sized IsSized> |
| 111 | +using fwd_test_range = test::range<forward_iterator_tag, Elem, IsSized, test::CanDifference::no, test::Common::no, |
| 112 | + test::CanCompare::yes, test::ProxyRef::yes>; |
| 113 | +template <class Elem, test::Sized IsSized, test::Common IsCommon> |
| 114 | +using random_test_range = test::range<random_access_iterator_tag, Elem, IsSized, test::CanDifference::no, IsCommon, |
| 115 | + test::CanCompare::yes, test::ProxyRef::no>; |
| 116 | +template <class Elem> |
| 117 | +using out_test_iterator = |
| 118 | + test::iterator<output_iterator_tag, Elem, test::CanDifference::no, test::CanCompare::yes, test::ProxyRef::yes>; |
| 119 | + |
| 120 | +constexpr bool run_tests() { |
| 121 | + // All (except contiguous) proxy reference types, since the algorithm doesn't really care. |
| 122 | + using test::Common, test::Sized; |
| 123 | + |
| 124 | + // both forward, non-common, and sized or unsized |
| 125 | + instantiator::call<fwd_test_range<Elem1, Sized::no>, fwd_test_range<Elem2, Sized::no>, out_test_iterator<Elem3>>(); |
| 126 | + instantiator::call<fwd_test_range<Elem1, Sized::yes>, fwd_test_range<Elem2, Sized::yes>, |
| 127 | + out_test_iterator<Elem3>>(); |
| 128 | + |
| 129 | + // both random-access, and sized or unsized; all permutations of common |
| 130 | + instantiator::call<random_test_range<Elem1, Sized::no, Common::no>, random_test_range<Elem2, Sized::no, Common::no>, |
| 131 | + out_test_iterator<Elem3>>(); |
| 132 | + instantiator::call<random_test_range<Elem1, Sized::no, Common::no>, |
| 133 | + random_test_range<Elem2, Sized::no, Common::yes>, out_test_iterator<Elem3>>(); |
| 134 | + instantiator::call<random_test_range<Elem1, Sized::no, Common::yes>, |
| 135 | + random_test_range<Elem2, Sized::no, Common::no>, out_test_iterator<Elem3>>(); |
| 136 | + instantiator::call<random_test_range<Elem1, Sized::no, Common::yes>, |
| 137 | + random_test_range<Elem2, Sized::no, Common::yes>, out_test_iterator<Elem3>>(); |
| 138 | + instantiator::call<random_test_range<Elem1, Sized::yes, Common::no>, |
| 139 | + random_test_range<Elem2, Sized::yes, Common::no>, out_test_iterator<Elem3>>(); |
| 140 | + instantiator::call<random_test_range<Elem1, Sized::yes, Common::no>, |
| 141 | + random_test_range<Elem2, Sized::yes, Common::yes>, out_test_iterator<Elem3>>(); |
| 142 | + instantiator::call<random_test_range<Elem1, Sized::yes, Common::yes>, |
| 143 | + random_test_range<Elem2, Sized::yes, Common::no>, out_test_iterator<Elem3>>(); |
| 144 | + instantiator::call<random_test_range<Elem1, Sized::yes, Common::yes>, |
| 145 | + random_test_range<Elem2, Sized::yes, Common::yes>, out_test_iterator<Elem3>>(); |
| 146 | + |
| 147 | + return true; |
| 148 | +} |
| 149 | + |
| 150 | +int main() { |
| 151 | + STATIC_ASSERT(run_tests()); |
| 152 | + run_tests(); |
| 153 | +} |
| 154 | +#endif // TEST_EVERYTHING |
0 commit comments