|
| 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 | +// REQUIRES: std-at-least-c++26 |
| 10 | + |
| 11 | +#include <ranges> |
| 12 | + |
| 13 | +#include <array> |
| 14 | +#include <concepts> |
| 15 | +#include <functional> |
| 16 | + |
| 17 | +#include "../../range_adaptor_types.h" |
| 18 | + |
| 19 | +template <class T, class U> |
| 20 | +concept canPlusEqual = requires(T& t, U& u) { t += u; }; |
| 21 | + |
| 22 | +template <class T, class U> |
| 23 | +concept canMinusEqual = requires(T& t, U& u) { t -= u; }; |
| 24 | + |
| 25 | +constexpr bool test() { |
| 26 | + int buffer1[5] = {1, 2, 3, 4, 5}; |
| 27 | + int buffer2[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 28 | + |
| 29 | + SimpleCommonRandomAccessSized a{buffer1}; |
| 30 | + SimpleCommonRandomAccessSized b{buffer2}; |
| 31 | + |
| 32 | + { |
| 33 | + // operator+(x, n) and operator+= |
| 34 | + std::ranges::concat_view v(a, b); |
| 35 | + auto it1 = v.begin(); |
| 36 | + |
| 37 | + auto it2 = it1 + 3; |
| 38 | + auto x2 = *it2; |
| 39 | + assert(x2 == buffer1[3]); |
| 40 | + |
| 41 | + auto it3 = 3 + it1; |
| 42 | + auto x3 = *it3; |
| 43 | + assert(x3 == buffer1[3]); |
| 44 | + |
| 45 | + it1 += 3; |
| 46 | + assert(it1 == it2); |
| 47 | + auto x1 = *it2; |
| 48 | + assert(x1 == buffer1[3]); |
| 49 | + |
| 50 | + using Iter = decltype(it1); |
| 51 | + static_assert(canPlusEqual<Iter, std::intptr_t>); |
| 52 | + } |
| 53 | + |
| 54 | + { |
| 55 | + // operator-(x, n) and operator-= |
| 56 | + std::ranges::concat_view v(a, b); |
| 57 | + auto it1 = v.end(); |
| 58 | + |
| 59 | + auto it2 = it1 - 3; |
| 60 | + auto x2 = *it2; |
| 61 | + assert(x2 == buffer2[6]); |
| 62 | + |
| 63 | + it1 -= 3; |
| 64 | + assert(it1 == it2); |
| 65 | + auto x1 = *it2; |
| 66 | + assert(x1 == buffer2[6]); |
| 67 | + |
| 68 | + using Iter = decltype(it1); |
| 69 | + static_assert(canMinusEqual<Iter, std::intptr_t>); |
| 70 | + } |
| 71 | + |
| 72 | + { |
| 73 | + // operator-(x, y) |
| 74 | + std::ranges::concat_view v(a, b); |
| 75 | + assert((v.end() - v.begin()) == 14); |
| 76 | + |
| 77 | + auto it1 = v.begin() + 2; |
| 78 | + auto it2 = v.end() - 1; |
| 79 | + assert((it1 - it2) == -11); |
| 80 | + } |
| 81 | + |
| 82 | + { |
| 83 | + // One of the ranges is not random access |
| 84 | + std::ranges::concat_view v(a, b, ForwardSizedView{buffer1}); |
| 85 | + using Iter = decltype(v.begin()); |
| 86 | + static_assert(!std::invocable<std::plus<>, Iter, std::intptr_t>); |
| 87 | + static_assert(!std::invocable<std::plus<>, std::intptr_t, Iter>); |
| 88 | + static_assert(!canPlusEqual<Iter, std::intptr_t>); |
| 89 | + static_assert(!std::invocable<std::minus<>, Iter, std::intptr_t>); |
| 90 | + static_assert(!std::invocable<std::minus<>, Iter, Iter>); |
| 91 | + static_assert(!canMinusEqual<Iter, std::intptr_t>); |
| 92 | + } |
| 93 | + |
| 94 | + { |
| 95 | + // One of the ranges does not have sized sentinel |
| 96 | + std::ranges::concat_view v(a, b, InputCommonView{buffer1}); |
| 97 | + using Iter = decltype(v.begin()); |
| 98 | + static_assert(!std::invocable<std::minus<>, Iter, Iter>); |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +int main(int, char**) { |
| 105 | + test(); |
| 106 | + static_assert(test()); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
0 commit comments