1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <algorithm>
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2>
// requires indirectly_swappable<I1, I2>
// constexpr ranges::swap_ranges_result<I1, I2>
// ranges::swap_ranges(I1 first1, S1 last1, I2 first2, S2 last2);
// template<input_range R1, input_range R2>
// requires indirectly_swappable<iterator_t<R1>, iterator_t<R2>>
// constexpr ranges::swap_ranges_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
// ranges::swap_ranges(R1&& r1, R2&& r2);
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "test_iterators.h"
constexpr void test_different_lengths() {
using Expected = std::ranges::swap_ranges_result<int*, int*>;
int i[3] = {1, 2, 3};
int j[1] = {4};
std::same_as<Expected> auto r = std::ranges::swap_ranges(i, i + 3, j, j + 1);
assert(r.in1 == i + 1);
assert(r.in2 == j + 1);
assert(i[0] == 4);
assert(i[1] == 2);
assert(i[2] == 3);
assert(j[0] == 1);
std::same_as<Expected> auto r2 = std::ranges::swap_ranges(i, j);
assert(r2.in1 == i + 1);
assert(r2.in2 == j + 1);
assert(i[0] == 1);
assert(i[1] == 2);
assert(i[2] == 3);
assert(j[0] == 4);
std::same_as<Expected> auto r3 = std::ranges::swap_ranges(j, j + 1, i, i + 3);
assert(r3.in1 == j + 1);
assert(r3.in2 == i + 1);
assert(i[0] == 4);
assert(i[1] == 2);
assert(i[2] == 3);
assert(j[0] == 1);
std::same_as<Expected> auto r4 = std::ranges::swap_ranges(j, i);
assert(r4.in1 == j + 1);
assert(r4.in2 == i + 1);
assert(i[0] == 1);
assert(i[1] == 2);
assert(i[2] == 3);
assert(j[0] == 4);
}
constexpr void test_range() {
std::array r1 = {1, 2, 3};
std::array r2 = {4, 5, 6};
std::same_as<std::ranges::in_in_result<int*, int*>> auto r = std::ranges::swap_ranges(r1, r2);
assert(r.in1 == r1.end());
assert(r.in2 == r2.end());
assert((r1 == std::array{4, 5, 6}));
assert((r2 == std::array{1, 2, 3}));
}
constexpr void test_borrowed_input_range() {
{
int r1[] = {1, 2, 3};
int r2[] = {4, 5, 6};
std::ranges::swap_ranges(std::views::all(r1), r2);
assert(r1[0] == 4);
assert(r1[1] == 5);
assert(r1[2] == 6);
assert(r2[0] == 1);
assert(r2[1] == 2);
assert(r2[2] == 3);
}
{
int r1[] = {1, 2, 3};
int r2[] = {4, 5, 6};
std::ranges::swap_ranges(r1, std::views::all(r2));
assert(r1[0] == 4);
assert(r1[1] == 5);
assert(r1[2] == 6);
assert(r2[0] == 1);
assert(r2[1] == 2);
assert(r2[2] == 3);
}
{
int r1[] = {1, 2, 3};
int r2[] = {4, 5, 6};
std::ranges::swap_ranges(std::views::all(r1), std::views::all(r2));
assert(r1[0] == 4);
assert(r1[1] == 5);
assert(r1[2] == 6);
assert(r2[0] == 1);
assert(r2[1] == 2);
assert(r2[2] == 3);
}
}
constexpr void test_sentinel() {
int i[3] = {1, 2, 3};
int j[3] = {4, 5, 6};
using It = cpp17_input_iterator<int*>;
using Sent = sentinel_wrapper<It>;
using Expected = std::ranges::swap_ranges_result<It, It>;
std::same_as<Expected> auto r =
std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3)));
assert(base(r.in1) == i + 3);
assert(base(r.in2) == j + 3);
assert(i[0] == 4);
assert(i[1] == 5);
assert(i[2] == 6);
assert(j[0] == 1);
assert(j[1] == 2);
assert(j[2] == 3);
}
template <class Iter1, class Iter2>
constexpr void test_iterators() {
using Expected = std::ranges::swap_ranges_result<Iter1, Iter2>;
int i[3] = {1, 2, 3};
int j[3] = {4, 5, 6};
std::same_as<Expected> auto r =
std::ranges::swap_ranges(Iter1(i), sentinel_wrapper(Iter1(i + 3)), Iter2(j), sentinel_wrapper(Iter2(j + 3)));
assert(base(r.in1) == i + 3);
assert(base(r.in2) == j + 3);
assert(i[0] == 4);
assert(i[1] == 5);
assert(i[2] == 6);
assert(j[0] == 1);
assert(j[1] == 2);
assert(j[2] == 3);
}
constexpr void test_rval_range() {
{
using Expected = std::ranges::swap_ranges_result<int*, std::ranges::dangling>;
std::array<int, 3> r = {1, 2, 3};
std::same_as<Expected> auto a = std::ranges::swap_ranges(r, std::array{4, 5, 6});
assert((r == std::array{4, 5, 6}));
assert(a.in1 == r.begin() + 3);
}
{
std::array<int, 3> r = {1, 2, 3};
using Expected = std::ranges::swap_ranges_result<std::ranges::dangling, int*>;
std::same_as<Expected> auto b = std::ranges::swap_ranges(std::array{4, 5, 6}, r);
assert((r == std::array{4, 5, 6}));
assert(b.in2 == r.begin() + 3);
}
}
template <class Out>
constexpr void test_proxy_in_iterators() {
test_iterators<ProxyIterator<cpp20_input_iterator<int*>>, Out>();
test_iterators<ProxyIterator<forward_iterator<int*>>, Out>();
test_iterators<ProxyIterator<bidirectional_iterator<int*>>, Out>();
test_iterators<ProxyIterator<random_access_iterator<int*>>, Out>();
test_iterators<ProxyIterator<contiguous_iterator<int*>>, Out>();
}
constexpr bool test() {
test_range();
test_iterators<cpp20_input_iterator<int*>, cpp20_input_iterator<int*>>();
test_iterators<cpp20_input_iterator<int*>, forward_iterator<int*>>();
test_iterators<cpp20_input_iterator<int*>, bidirectional_iterator<int*>>();
test_iterators<cpp20_input_iterator<int*>, random_access_iterator<int*>>();
test_iterators<cpp20_input_iterator<int*>, int*>();
test_iterators<forward_iterator<int*>, cpp20_input_iterator<int*>>();
test_iterators<forward_iterator<int*>, forward_iterator<int*>>();
test_iterators<forward_iterator<int*>, bidirectional_iterator<int*>>();
test_iterators<forward_iterator<int*>, random_access_iterator<int*>>();
test_iterators<forward_iterator<int*>, int*>();
test_iterators<bidirectional_iterator<int*>, cpp20_input_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>, forward_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>, bidirectional_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>, random_access_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>, int*>();
test_iterators<random_access_iterator<int*>, cpp20_input_iterator<int*>>();
test_iterators<random_access_iterator<int*>, forward_iterator<int*>>();
test_iterators<random_access_iterator<int*>, bidirectional_iterator<int*>>();
test_iterators<random_access_iterator<int*>, random_access_iterator<int*>>();
test_iterators<random_access_iterator<int*>, int*>();
test_iterators<int*, cpp20_input_iterator<int*>>();
test_iterators<int*, forward_iterator<int*>>();
test_iterators<int*, bidirectional_iterator<int*>>();
test_iterators<int*, random_access_iterator<int*>>();
test_iterators<int*, int*>();
test_proxy_in_iterators<ProxyIterator<cpp20_input_iterator<int*>>>();
test_proxy_in_iterators<ProxyIterator<forward_iterator<int*>>>();
test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>();
test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>();
test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>();
test_sentinel();
test_different_lengths();
test_borrowed_input_range();
test_rval_range();
return true;
}
static_assert(std::same_as<std::ranges::swap_ranges_result<int, char>, std::ranges::in_in_result<int, char>>);
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|