aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/algorithms/alg.modifying.operations
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/test/std/algorithms/alg.modifying.operations')
-rw-r--r--libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp55
-rw-r--r--libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp62
-rw-r--r--libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp108
-rw-r--r--libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp119
4 files changed, 250 insertions, 94 deletions
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
index b1ad687..1afaa1a 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move.pass.cpp
@@ -20,6 +20,7 @@
#include <cassert>
#include <iterator>
#include <memory>
+#include <vector>
#include "MoveOnly.h"
#include "test_iterators.h"
@@ -45,15 +46,15 @@ struct Test {
template <class OutIter>
TEST_CONSTEXPR_CXX20 void operator()() {
const unsigned N = 1000;
- int ia[N] = {};
+ int ia[N] = {};
for (unsigned i = 0; i < N; ++i)
- ia[i] = i;
+ ia[i] = i;
int ib[N] = {0};
- OutIter r = std::move(InIter(ia), InIter(ia+N), OutIter(ib));
- assert(base(r) == ib+N);
+ OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib));
+ assert(base(r) == ib + N);
for (unsigned i = 0; i < N; ++i)
- assert(ia[i] == ib[i]);
+ assert(ia[i] == ib[i]);
}
};
@@ -73,13 +74,13 @@ struct Test1 {
const unsigned N = 100;
std::unique_ptr<int> ia[N];
for (unsigned i = 0; i < N; ++i)
- ia[i].reset(new int(i));
+ ia[i].reset(new int(i));
std::unique_ptr<int> ib[N];
- OutIter r = std::move(InIter(ia), InIter(ia+N), OutIter(ib));
- assert(base(r) == ib+N);
+ OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib));
+ assert(base(r) == ib + N);
for (unsigned i = 0; i < N; ++i)
- assert(*ib[i] == static_cast<int>(i));
+ assert(*ib[i] == static_cast<int>(i));
}
};
@@ -92,6 +93,28 @@ struct Test1OutIters {
}
};
+TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
+ std::vector<bool> v(N, false);
+ for (std::size_t i = 0; i < N; i += 2)
+ v[i] = true;
+
+ { // Test move with aligned bytes
+ std::vector<bool> in(v);
+ std::vector<bool> out(N);
+ std::move(in.begin(), in.end(), out.begin());
+ assert(out == v);
+ }
+ { // Test move with unaligned bytes
+ std::vector<bool> in(v);
+ std::vector<bool> out(N);
+ std::move(in.begin() + 4, in.end(), out.begin());
+ for (std::size_t i = 0; i < N - 4; ++i)
+ assert(v[i + 4] == out[i]);
+ }
+
+ return true;
+}
+
TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::cpp17_input_iterator_list<int*>(), TestOutIters());
if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
@@ -118,7 +141,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When non-trivial
{
MoveOnly from[3] = {1, 2, 3};
- MoveOnly to[3] = {};
+ MoveOnly to[3] = {};
std::move(std::begin(from), std::end(from), std::begin(to));
assert(to[0] == MoveOnly(1));
assert(to[1] == MoveOnly(2));
@@ -127,7 +150,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When trivial
{
TrivialMoveOnly from[3] = {1, 2, 3};
- TrivialMoveOnly to[3] = {};
+ TrivialMoveOnly to[3] = {};
std::move(std::begin(from), std::end(from), std::begin(to));
assert(to[0] == TrivialMoveOnly(1));
assert(to[1] == TrivialMoveOnly(2));
@@ -135,6 +158,16 @@ TEST_CONSTEXPR_CXX20 bool test() {
}
}
+ { // Test vector<bool>::iterator optimization
+ assert(test_vector_bool(8));
+ assert(test_vector_bool(19));
+ assert(test_vector_bool(32));
+ assert(test_vector_bool(49));
+ assert(test_vector_bool(64));
+ assert(test_vector_bool(199));
+ assert(test_vector_bool(256));
+ }
+
return true;
}
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
index 61dea47..3c0fcad 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/move_backward.pass.cpp
@@ -19,6 +19,7 @@
#include <cassert>
#include <iterator>
#include <memory>
+#include <vector>
#include "MoveOnly.h"
#include "test_iterators.h"
@@ -44,24 +45,22 @@ struct Test {
template <class OutIter>
TEST_CONSTEXPR_CXX20 void operator()() {
const unsigned N = 1000;
- int ia[N] = {};
+ int ia[N] = {};
for (unsigned i = 0; i < N; ++i)
- ia[i] = i;
+ ia[i] = i;
int ib[N] = {0};
- OutIter r = std::move_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
+ OutIter r = std::move_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
assert(base(r) == ib);
for (unsigned i = 0; i < N; ++i)
- assert(ia[i] == ib[i]);
+ assert(ia[i] == ib[i]);
}
};
struct TestOutIters {
template <class InIter>
TEST_CONSTEXPR_CXX20 void operator()() {
- types::for_each(
- types::concatenate_t<types::bidirectional_iterator_list<int*> >(),
- Test<InIter>());
+ types::for_each(types::concatenate_t<types::bidirectional_iterator_list<int*> >(), Test<InIter>());
}
};
@@ -72,24 +71,46 @@ struct Test1 {
const unsigned N = 100;
std::unique_ptr<int> ia[N];
for (unsigned i = 0; i < N; ++i)
- ia[i].reset(new int(i));
+ ia[i].reset(new int(i));
std::unique_ptr<int> ib[N];
- OutIter r = std::move_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
+ OutIter r = std::move_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
assert(base(r) == ib);
for (unsigned i = 0; i < N; ++i)
- assert(*ib[i] == static_cast<int>(i));
+ assert(*ib[i] == static_cast<int>(i));
}
};
struct Test1OutIters {
template <class InIter>
TEST_CONSTEXPR_CXX23 void operator()() {
- types::for_each(types::concatenate_t<types::bidirectional_iterator_list<std::unique_ptr<int>*> >(),
- Test1<InIter>());
+ types::for_each(
+ types::concatenate_t<types::bidirectional_iterator_list<std::unique_ptr<int>*> >(), Test1<InIter>());
}
};
+TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
+ std::vector<bool> v(N, false);
+ for (std::size_t i = 0; i < N; i += 2)
+ v[i] = true;
+
+ { // Test move_backward with aligned bytes
+ std::vector<bool> in(v);
+ std::vector<bool> out(N);
+ std::move_backward(in.begin(), in.end(), out.end());
+ assert(out == v);
+ }
+ { // Test move_backward with unaligned bytes
+ std::vector<bool> in(v);
+ std::vector<bool> out(N);
+ std::move_backward(in.begin(), in.end() - 4, out.end());
+ for (std::size_t i = 0; i < N - 4; ++i)
+ assert(out[i + 4] == v[i]);
+ }
+
+ return true;
+}
+
TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::bidirectional_iterator_list<int*>(), TestOutIters());
if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
@@ -117,7 +138,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When non-trivial
{
MoveOnly from[3] = {1, 2, 3};
- MoveOnly to[3] = {};
+ MoveOnly to[3] = {};
std::move_backward(std::begin(from), std::end(from), std::end(to));
assert(to[0] == MoveOnly(1));
assert(to[1] == MoveOnly(2));
@@ -126,7 +147,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
// When trivial
{
TrivialMoveOnly from[3] = {1, 2, 3};
- TrivialMoveOnly to[3] = {};
+ TrivialMoveOnly to[3] = {};
std::move_backward(std::begin(from), std::end(from), std::end(to));
assert(to[0] == TrivialMoveOnly(1));
assert(to[1] == TrivialMoveOnly(2));
@@ -134,11 +155,20 @@ TEST_CONSTEXPR_CXX20 bool test() {
}
}
+ { // Test vector<bool>::iterator optimization
+ assert(test_vector_bool(8));
+ assert(test_vector_bool(19));
+ assert(test_vector_bool(32));
+ assert(test_vector_bool(49));
+ assert(test_vector_bool(64));
+ assert(test_vector_bool(199));
+ assert(test_vector_bool(256));
+ }
+
return true;
}
-int main(int, char**)
-{
+int main(int, char**) {
test();
#if TEST_STD_VER >= 20
static_assert(test());
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
index a0d1473..1a89408 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
@@ -31,6 +31,7 @@
#include "almost_satisfies_types.h"
#include "MoveOnly.h"
#include "test_iterators.h"
+#include "test_macros.h"
template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
concept HasMoveIt = requires(In in, Sent sent, Out out) { std::ranges::move(in, sent, out); };
@@ -65,7 +66,7 @@ constexpr void test(std::array<int, N> in) {
{
std::array<int, N> out;
std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
- std::ranges::move(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()));
+ std::ranges::move(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()));
assert(in == out);
assert(base(ret.in) == in.data() + in.size());
assert(base(ret.out) == out.data() + out.size());
@@ -73,8 +74,7 @@ constexpr void test(std::array<int, N> in) {
{
std::array<int, N> out;
auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));
- std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
- std::ranges::move(range, Out(out.data()));
+ std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret = std::ranges::move(range, Out(out.data()));
assert(in == out);
assert(base(ret.in) == in.data() + in.size());
assert(base(ret.out) == out.data() + out.size());
@@ -84,16 +84,16 @@ constexpr void test(std::array<int, N> in) {
template <class InContainer, class OutContainer, class In, class Out, class Sent = In>
constexpr void test_containers() {
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
- std::ranges::move(In(in.begin()), Sent(In(in.end())), Out(out.begin()));
+ std::ranges::move(In(in.begin()), Sent(In(in.end())), Out(out.begin()));
assert(std::ranges::equal(in, out));
assert(base(ret.in) == in.end());
assert(base(ret.out) == out.end());
}
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end())));
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move(range, Out(out.begin()));
@@ -165,22 +165,52 @@ constexpr void test_proxy_in_iterators() {
}
struct IteratorWithMoveIter {
- using value_type = int;
- using difference_type = int;
+ using value_type = int;
+ using difference_type = int;
explicit IteratorWithMoveIter() = default;
int* ptr;
constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
- constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
- constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
+ constexpr IteratorWithMoveIter& operator++() {
+ ++ptr;
+ return *this;
+ }
+ constexpr IteratorWithMoveIter operator++(int) {
+ auto ret = *this;
+ ++*this;
+ return ret;
+ }
friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
};
+#if TEST_STD_VER >= 23
+constexpr bool test_vector_bool(std::size_t N) {
+ std::vector<bool> v(N, false);
+ for (std::size_t i = 0; i < N; i += 2)
+ v[i] = true;
+
+ { // Test move with aligned bytes
+ std::vector<bool> in{v};
+ std::vector<bool> out(N);
+ std::ranges::move(in, out.begin());
+ assert(out == v);
+ }
+ { // Test move with unaligned bytes
+ std::vector<bool> in{v};
+ std::vector<bool> out(N);
+ std::ranges::move(std::views::counted(in.begin() + 4, N - 4), out.begin());
+ assert(std::ranges::equal(v | std::views::drop(4), out | std::views::take(N - 4)));
+ }
+
+ return true;
+}
+#endif
+
// cpp17_intput_iterator has a defaulted template argument
template <class Iter>
using Cpp17InIter = cpp17_input_iterator<Iter>;
@@ -267,13 +297,13 @@ constexpr bool test() {
{ // check that ranges::dangling is returned
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> decltype(auto) ret =
- std::ranges::move(std::array {1, 2, 3, 4}, out.data());
+ std::ranges::move(std::array{1, 2, 3, 4}, out.data());
assert(ret.out == out.data() + 4);
assert((out == std::array{1, 2, 3, 4}));
}
{ // check that an iterator is returned with a borrowing range
- std::array in {1, 2, 3, 4};
+ std::array in{1, 2, 3, 4};
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::array<int, 4>::iterator, int*>> decltype(auto) ret =
std::ranges::move(std::views::all(in), out.data());
@@ -284,8 +314,8 @@ constexpr bool test() {
{ // check that every element is moved exactly once
struct MoveOnce {
- bool moved = false;
- constexpr MoveOnce() = default;
+ bool moved = false;
+ constexpr MoveOnce() = default;
constexpr MoveOnce(const MoveOnce& other) = delete;
constexpr MoveOnce& operator=(MoveOnce&& other) {
assert(!other.moved);
@@ -294,16 +324,16 @@ constexpr bool test() {
}
};
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move(in.begin(), in.end(), out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
}
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move(in, out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
@@ -314,8 +344,8 @@ constexpr bool test() {
{ // check that the range is moved forwards
struct OnlyForwardsMovable {
OnlyForwardsMovable* next = nullptr;
- bool canMove = false;
- OnlyForwardsMovable() = default;
+ bool canMove = false;
+ OnlyForwardsMovable() = default;
constexpr OnlyForwardsMovable& operator=(OnlyForwardsMovable&&) {
assert(canMove);
if (next != nullptr)
@@ -324,12 +354,12 @@ constexpr bool test() {
}
};
{
- std::array<OnlyForwardsMovable, 3> in {};
- std::array<OnlyForwardsMovable, 3> out {};
- out[0].next = &out[1];
- out[1].next = &out[2];
+ std::array<OnlyForwardsMovable, 3> in{};
+ std::array<OnlyForwardsMovable, 3> out{};
+ out[0].next = &out[1];
+ out[1].next = &out[2];
out[0].canMove = true;
- auto ret = std::ranges::move(in.begin(), in.end(), out.begin());
+ auto ret = std::ranges::move(in.begin(), in.end(), out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
assert(out[0].canMove);
@@ -337,12 +367,12 @@ constexpr bool test() {
assert(out[2].canMove);
}
{
- std::array<OnlyForwardsMovable, 3> in {};
- std::array<OnlyForwardsMovable, 3> out {};
- out[0].next = &out[1];
- out[1].next = &out[2];
+ std::array<OnlyForwardsMovable, 3> in{};
+ std::array<OnlyForwardsMovable, 3> out{};
+ out[0].next = &out[1];
+ out[1].next = &out[2];
out[0].canMove = true;
- auto ret = std::ranges::move(in, out.begin());
+ auto ret = std::ranges::move(in, out.begin());
assert(ret.in == in.end());
assert(ret.out == out.end());
assert(out[0].canMove);
@@ -358,19 +388,31 @@ constexpr bool test() {
auto ret = std::ranges::move(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data());
assert(ret.in == a + 4);
assert(ret.out == b.data() + 4);
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
{
int a[] = {1, 2, 3, 4};
std::array<int, 4> b;
auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
- auto ret = std::ranges::move(range, b.data());
+ auto ret = std::ranges::move(range, b.data());
assert(ret.in == a + 4);
assert(ret.out == b.data() + 4);
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
}
+#if TEST_STD_VER >= 23
+ { // Test vector<bool>::iterator optimization
+ assert(test_vector_bool(8));
+ assert(test_vector_bool(19));
+ assert(test_vector_bool(32));
+ assert(test_vector_bool(49));
+ assert(test_vector_bool(64));
+ assert(test_vector_bool(199));
+ assert(test_vector_bool(256));
+ }
+#endif
+
return true;
}
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp
index 47cf178..923b4c7 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp
@@ -31,6 +31,7 @@
#include "almost_satisfies_types.h"
#include "MoveOnly.h"
#include "test_iterators.h"
+#include "test_macros.h"
template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
concept HasMoveBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::move_backward(in, sent, out); };
@@ -65,7 +66,7 @@ constexpr void test(std::array<int, N> in) {
{
std::array<int, N> out;
std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
- std::ranges::move_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
+ std::ranges::move_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
assert(in == out);
assert(base(ret.in) == in.data() + in.size());
assert(base(ret.out) == out.data());
@@ -92,16 +93,16 @@ constexpr void test_iterators() {
template <class InContainer, class OutContainer, class In, class Out, class Sent = In>
constexpr void test_containers() {
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
- std::ranges::move_backward(In(in.begin()), Sent(In(in.end())), Out(out.end()));
+ std::ranges::move_backward(In(in.begin()), Sent(In(in.end())), Out(out.end()));
assert(std::ranges::equal(in, out));
assert(base(ret.in) == in.end());
assert(base(ret.out) == out.begin());
}
{
- InContainer in {1, 2, 3, 4};
+ InContainer in{1, 2, 3, 4};
OutContainer out(4);
auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end())));
std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move_backward(range, Out(out.end()));
@@ -159,25 +160,62 @@ constexpr void test_proxy_in_iterators() {
}
struct IteratorWithMoveIter {
- using value_type = int;
- using difference_type = int;
+ using value_type = int;
+ using difference_type = int;
explicit IteratorWithMoveIter() = default;
int* ptr;
constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
- constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
- constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
+ constexpr IteratorWithMoveIter& operator++() {
+ ++ptr;
+ return *this;
+ }
+ constexpr IteratorWithMoveIter operator++(int) {
+ auto ret = *this;
+ ++*this;
+ return ret;
+ }
- constexpr IteratorWithMoveIter& operator--() { --ptr; return *this; }
- constexpr IteratorWithMoveIter operator--(int) { auto ret = *this; --*this; return ret; }
+ constexpr IteratorWithMoveIter& operator--() {
+ --ptr;
+ return *this;
+ }
+ constexpr IteratorWithMoveIter operator--(int) {
+ auto ret = *this;
+ --*this;
+ return ret;
+ }
friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
};
+#if TEST_STD_VER >= 23
+constexpr bool test_vector_bool(std::size_t N) {
+ std::vector<bool> v(N, false);
+ for (std::size_t i = 0; i < N; i += 2)
+ v[i] = true;
+
+ { // Test move_backward with aligned bytes
+ std::vector<bool> in{v};
+ std::vector<bool> out(N);
+ std::ranges::move_backward(in, out.end());
+ assert(out == v);
+ }
+ { // Test move_backward with unaligned bytes
+ std::vector<bool> in{v};
+ std::vector<bool> out(N);
+ std::ranges::move_backward(std::views::counted(in.begin(), N - 4), out.end());
+ assert(std::ranges::equal(v | std::views::take(N - 4), out | std::views::drop(4)));
+ }
+
+ return true;
+}
+#endif
+
constexpr bool test() {
test_in_iterators<bidirectional_iterator>();
test_in_iterators<random_access_iterator>();
@@ -243,7 +281,8 @@ constexpr bool test() {
MoveOnly b[3];
ProxyRange proxyA{a};
ProxyRange proxyB{b};
- std::ranges::move_backward(std::begin(proxyA), std::end(proxyA), std::ranges::next(proxyB.begin(), std::end(proxyB)));
+ std::ranges::move_backward(
+ std::begin(proxyA), std::end(proxyA), std::ranges::next(proxyB.begin(), std::end(proxyB)));
assert(b[0].get() == 1);
assert(b[1].get() == 2);
assert(b[2].get() == 3);
@@ -253,13 +292,13 @@ constexpr bool test() {
{ // check that ranges::dangling is returned
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
- std::ranges::move_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
+ std::ranges::move_backward(std::array{1, 2, 3, 4}, out.data() + out.size());
assert(ret.out == out.data());
assert((out == std::array{1, 2, 3, 4}));
}
{ // check that an iterator is returned with a borrowing range
- std::array in {1, 2, 3, 4};
+ std::array in{1, 2, 3, 4};
std::array<int, 4> out;
std::same_as<std::ranges::in_out_result<std::array<int, 4>::iterator, int*>> auto ret =
std::ranges::move_backward(std::views::all(in), out.data() + out.size());
@@ -270,8 +309,8 @@ constexpr bool test() {
{ // check that every element is moved exactly once
struct MoveOnce {
- bool moved = false;
- constexpr MoveOnce() = default;
+ bool moved = false;
+ constexpr MoveOnce() = default;
constexpr MoveOnce(const MoveOnce& other) = delete;
constexpr MoveOnce& operator=(const MoveOnce& other) {
assert(!other.moved);
@@ -280,16 +319,16 @@ constexpr bool test() {
}
};
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
}
{
- std::array<MoveOnce, 4> in {};
- std::array<MoveOnce, 4> out {};
+ std::array<MoveOnce, 4> in{};
+ std::array<MoveOnce, 4> out{};
auto ret = std::ranges::move_backward(in, out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
@@ -300,8 +339,8 @@ constexpr bool test() {
{ // check that the range is moved backwards
struct OnlyBackwardsMovable {
OnlyBackwardsMovable* next = nullptr;
- bool canMove = false;
- OnlyBackwardsMovable() = default;
+ bool canMove = false;
+ OnlyBackwardsMovable() = default;
constexpr OnlyBackwardsMovable& operator=(const OnlyBackwardsMovable&) {
assert(canMove);
if (next != nullptr)
@@ -310,12 +349,12 @@ constexpr bool test() {
}
};
{
- std::array<OnlyBackwardsMovable, 3> in {};
- std::array<OnlyBackwardsMovable, 3> out {};
- out[1].next = &out[0];
- out[2].next = &out[1];
+ std::array<OnlyBackwardsMovable, 3> in{};
+ std::array<OnlyBackwardsMovable, 3> out{};
+ out[1].next = &out[0];
+ out[2].next = &out[1];
out[2].canMove = true;
- auto ret = std::ranges::move_backward(in, out.end());
+ auto ret = std::ranges::move_backward(in, out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
assert(out[0].canMove);
@@ -323,12 +362,12 @@ constexpr bool test() {
assert(out[2].canMove);
}
{
- std::array<OnlyBackwardsMovable, 3> in {};
- std::array<OnlyBackwardsMovable, 3> out {};
- out[1].next = &out[0];
- out[2].next = &out[1];
+ std::array<OnlyBackwardsMovable, 3> in{};
+ std::array<OnlyBackwardsMovable, 3> out{};
+ out[1].next = &out[0];
+ out[2].next = &out[1];
out[2].canMove = true;
- auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
+ auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
assert(ret.in == in.end());
assert(ret.out == out.begin());
assert(out[0].canMove);
@@ -344,19 +383,31 @@ constexpr bool test() {
auto ret = std::ranges::move_backward(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data() + b.size());
assert(ret.in == a + 4);
assert(ret.out == b.data());
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
{
int a[] = {1, 2, 3, 4};
std::array<int, 4> b;
auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
- auto ret = std::ranges::move_backward(range, b.data() + b.size());
+ auto ret = std::ranges::move_backward(range, b.data() + b.size());
assert(ret.in == a + 4);
assert(ret.out == b.data());
- assert((b == std::array {42, 42, 42, 42}));
+ assert((b == std::array{42, 42, 42, 42}));
}
}
+#if TEST_STD_VER >= 23
+ { // Test vector<bool>::iterator optimization
+ assert(test_vector_bool(8));
+ assert(test_vector_bool(19));
+ assert(test_vector_bool(32));
+ assert(test_vector_bool(49));
+ assert(test_vector_bool(64));
+ assert(test_vector_bool(199));
+ assert(test_vector_bool(256));
+ }
+#endif
+
return true;
}