[libcxx-commits] [libcxx] [libc++] Fix copy_backward for vector<bool> with small storage types (PR #131560)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Mar 16 21:55:40 PDT 2025
https://github.com/winner245 created https://github.com/llvm/llvm-project/pull/131560
None
>From a5995e551b0eda9abec646338551c04ff0d1a19b Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Sun, 16 Mar 2025 21:37:27 -0400
Subject: [PATCH] Fix copy_backward for vector<bool> with small storage types
---
libcxx/include/__algorithm/copy_backward.h | 18 +--
libcxx/include/__bit_reference | 11 +-
libcxx/include/__fwd/bit_reference.h | 3 +
.../alg.copy/copy_backward.pass.cpp | 128 ++++++++++++++++++
.../alg.copy/ranges.copy_backward.pass.cpp | 128 ++++++++++++++++++
5 files changed, 277 insertions(+), 11 deletions(-)
diff --git a/libcxx/include/__algorithm/copy_backward.h b/libcxx/include/__algorithm/copy_backward.h
index 02ffc14361e6a..9f890645a41d3 100644
--- a/libcxx/include/__algorithm/copy_backward.h
+++ b/libcxx/include/__algorithm/copy_backward.h
@@ -52,7 +52,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
difference_type __dn = std::min(static_cast<difference_type>(__last.__ctz_), __n);
__n -= __dn;
unsigned __clz = __bits_per_word - __last.__ctz_;
- __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz);
+ __storage_type __m = std::__middle_mask<__storage_type>(__clz, __last.__ctz_ - __dn);
__storage_type __b = *__last.__seg_ & __m;
*__result.__seg_ &= ~__m;
*__result.__seg_ |= __b;
@@ -69,7 +69,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
__n -= __nw * __bits_per_word;
// do last word
if (__n > 0) {
- __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n);
+ __storage_type __m = std::__leading_mask<__storage_type>(__bits_per_word - __n);
__storage_type __b = *--__last.__seg_ & __m;
*--__result.__seg_ &= ~__m;
*__result.__seg_ |= __b;
@@ -94,12 +94,12 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
difference_type __dn = std::min(static_cast<difference_type>(__last.__ctz_), __n);
__n -= __dn;
unsigned __clz_l = __bits_per_word - __last.__ctz_;
- __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l);
+ __storage_type __m = std::__middle_mask<__storage_type>(__clz_l, __last.__ctz_ - __dn);
__storage_type __b = *__last.__seg_ & __m;
unsigned __clz_r = __bits_per_word - __result.__ctz_;
__storage_type __ddn = std::min(__dn, static_cast<difference_type>(__result.__ctz_));
if (__ddn > 0) {
- __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r);
+ __m = std::__middle_mask<__storage_type>(__clz_r, __result.__ctz_ - __ddn);
*__result.__seg_ &= ~__m;
if (__result.__ctz_ > __last.__ctz_)
*__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
@@ -112,7 +112,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
// __result.__ctz_ == 0
--__result.__seg_;
__result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1));
- __m = ~__storage_type(0) << __result.__ctz_;
+ __m = std::__leading_mask<__storage_type>(__result.__ctz_);
*__result.__seg_ &= ~__m;
__last.__ctz_ -= __dn + __ddn;
*__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
@@ -123,7 +123,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
// __result.__ctz_ != 0 || __n == 0
// do middle words
unsigned __clz_r = __bits_per_word - __result.__ctz_;
- __storage_type __m = ~__storage_type(0) >> __clz_r;
+ __storage_type __m = std::__trailing_mask<__storage_type>(__clz_r);
for (; __n >= __bits_per_word; __n -= __bits_per_word) {
__storage_type __b = *--__last.__seg_;
*__result.__seg_ &= ~__m;
@@ -133,11 +133,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
}
// do last word
if (__n > 0) {
- __m = ~__storage_type(0) << (__bits_per_word - __n);
+ __m = std::__leading_mask<__storage_type>(__bits_per_word - __n);
__storage_type __b = *--__last.__seg_ & __m;
__clz_r = __bits_per_word - __result.__ctz_;
__storage_type __dn = std::min(__n, static_cast<difference_type>(__result.__ctz_));
- __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r);
+ __m = std::__middle_mask<__storage_type>(__clz_r, __result.__ctz_ - __dn);
*__result.__seg_ &= ~__m;
*__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_);
__result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + __result.__ctz_) % __bits_per_word);
@@ -146,7 +146,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false> _
// __result.__ctz_ == 0
--__result.__seg_;
__result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
- __m = ~__storage_type(0) << __result.__ctz_;
+ __m = std::__leading_mask<__storage_type>(__result.__ctz_);
*__result.__seg_ &= ~__m;
*__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn));
}
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index 58cbd4d51c88e..f5c22fc0a3ade 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -82,13 +82,20 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz;
}
+// Creates a mask of type `_StorageType` with a specified number of trailing zeros (__ctz) and sets all remaining
+// bits to one.
+template <class _StorageType>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __leading_mask(unsigned __ctz) {
+ static_assert(is_unsigned<_StorageType>::value, "__leading_mask only works with unsigned types");
+ return static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz;
+}
+
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
// trailing zeros (__ctz), and sets all bits in between to one.
template <class _StorageType>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
- return (static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz) &
- std::__trailing_mask<_StorageType>(__clz);
+ return std::__leading_mask<_StorageType>(__ctz) & std::__trailing_mask<_StorageType>(__clz);
}
// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
diff --git a/libcxx/include/__fwd/bit_reference.h b/libcxx/include/__fwd/bit_reference.h
index e212667f3de1f..99d1fdbc41959 100644
--- a/libcxx/include/__fwd/bit_reference.h
+++ b/libcxx/include/__fwd/bit_reference.h
@@ -33,6 +33,9 @@ __fill_masked_range(_StoragePointer __word, unsigned __ctz, unsigned __clz, bool
template <class _StorageType>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz);
+template <class _StorageType>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __leading_mask(unsigned __ctz);
+
template <class _StorageType>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz);
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp
index 8a528a96f5294..d296f303856a1 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp
@@ -17,6 +17,7 @@
#include <cassert>
#include <vector>
+#include "sized_allocator.h"
#include "test_macros.h"
#include "test_iterators.h"
#include "type_algorithms.h"
@@ -111,6 +112,133 @@ TEST_CONSTEXPR_CXX20 bool test() {
assert(test_vector_bool(256));
}
+ // Make sure std::copy_backward behaves properly with std::vector<bool> iterators with custom size types.
+ {
+ //// Tests for std::copy_backward with aligned bits
+
+ { // Test the first (partial) word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(8, true, Alloc(1));
+ std::vector<bool, Alloc> out(8, false, Alloc(1));
+ std::copy_backward(in.begin() + 2, in.end() - 2, out.end() - 2);
+ // assert(std::equal(in.begin() + 2, in.end() - 2, out.begin() + 2));
+ for (std::size_t i = 2; i < in.size() - 2; ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test the last word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(12, true, Alloc(1));
+ std::vector<bool, Alloc> out(15, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end() - 3);
+ // assert(std::equal(in.begin(), in.end(), out.begin()));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test middle words for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(24, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(29, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end() - 5);
+ // assert(std::equal(in.begin(), in.end(), out.begin()));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+
+ { // Test the first (partial) word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(16, true, Alloc(1));
+ std::vector<bool, Alloc> out(16, false, Alloc(1));
+ std::copy_backward(in.begin() + 3, in.end() - 2, out.end() - 2);
+ // assert(std::equal(in.begin() + 3, in.end() - 2, out.begin() + 3));
+ for (std::size_t i = 3; i < in.size() - 5; ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test the last word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(24, true, Alloc(1));
+ std::vector<bool, Alloc> out(32, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end() - 8);
+ // assert(std::equal(in.begin(), in.end(), out.begin()));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test middle words for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(48, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(55, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end() - 7);
+ // assert(std::equal(in.begin(), in.end(), out.begin()));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+
+ //// Tests for std::copy_backward with unaligned bits
+
+ { // Test the first (partial) word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(6, true, Alloc(1));
+ std::vector<bool, Alloc> out(8, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end() - 2, out.end() - 1);
+ // assert(std::equal(in.begin(), in.end() - 2, out.begin() + 3));
+ for (std::size_t i = 0; i < in.size() - 2; ++i)
+ assert(in[i] == out[i + 3]);
+ }
+ { // Test the last word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(5, true, Alloc(1));
+ std::vector<bool, Alloc> out(8, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end());
+ // assert(std::equal(in.begin(), in.end(), out.begin() + 3));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i + 3]);
+ }
+ { // Test middle words for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(16, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(24, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end() - 3);
+ // assert(std::equal(in.begin(), in.end(), out.begin() + 5));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i + 5]);
+ }
+
+ { // Test the first (partial) word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(12, true, Alloc(1));
+ std::vector<bool, Alloc> out(16, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end() - 3, out.end() - 2);
+ // assert(std::equal(in.begin(), in.end() - 3, out.begin() + 5));
+ for (std::size_t i = 0; i < in.size() - 3; ++i)
+ assert(in[i] == out[i + 5]);
+ }
+ { // Test the last word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(16, true, Alloc(1));
+ std::vector<bool, Alloc> out(16, false, Alloc(1));
+ std::copy_backward(in.begin() + 3, in.end(), out.end() - 3);
+ // assert(std::equal(in.begin() + 3, in.end(), out.begin()));
+ for (std::size_t i = 3; i < in.size(); ++i)
+ assert(in[i] == out[i - 3]);
+ }
+ { // Test the middle words for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(32, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(64, false, Alloc(1));
+ std::copy_backward(in.begin(), in.end(), out.end() - 4);
+ // assert(std::equal(in.begin(), in.end(), out.begin() + 28));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i + 28]);
+ }
+ }
+
return true;
}
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy_backward.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy_backward.pass.cpp
index a7fa3db23e6ba..20139ea392b7f 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy_backward.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy_backward.pass.cpp
@@ -28,6 +28,7 @@
#include <vector>
#include "almost_satisfies_types.h"
+#include "sized_allocator.h"
#include "test_iterators.h"
#include "test_macros.h"
@@ -355,6 +356,133 @@ constexpr bool test() {
assert(test_vector_bool(199));
assert(test_vector_bool(256));
}
+
+ // Make sure std::ranges::copy_backward behaves properly with std::vector<bool> iterators with custom size types.
+ {
+ //// Tests for std::ranges::copy_backward with aligned bits
+
+ { // Test the first (partial) word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(8, true, Alloc(1));
+ std::vector<bool, Alloc> out(8, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin() + 2, in.end() - 2), out.end() - 2);
+ // assert(std::ranges::equal(in.begin() + 2, in.end() - 2, out.begin() + 2, out.end() - 2));
+ for (std::size_t i = 2; i < in.size() - 2; ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test the last word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(12, true, Alloc(1));
+ std::vector<bool, Alloc> out(15, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 3);
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 3));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test middle words for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(24, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(29, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 5);
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 5));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+
+ { // Test the first (partial) word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(16, true, Alloc(1));
+ std::vector<bool, Alloc> out(16, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin() + 3, in.end() - 2), out.end() - 2);
+ // assert(std::ranges::equal(in.begin() + 3, in.end() - 2, out.begin() + 3, out.end() - 2));
+ for (std::size_t i = 3; i < in.size() - 5; ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test the last word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(24, true, Alloc(1));
+ std::vector<bool, Alloc> out(32, false, Alloc(1));
+ std::ranges::copy_backward(in.begin(), in.end(), out.end() - 8);
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 8));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+ { // Test middle words for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(48, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(55, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 7);
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 7));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i]);
+ }
+
+ // //// Tests for std::ranges::copy_backward with unaligned bits
+
+ { // Test the first (partial) word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(6, true, Alloc(1));
+ std::vector<bool, Alloc> out(8, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end() - 2), out.end() - 1);
+ // assert(std::ranges::equal(in.begin(), in.end() - 2, out.begin() + 3, out.end() - 1));
+ for (std::size_t i = 0; i < in.size() - 2; ++i)
+ assert(in[i] == out[i + 3]);
+ }
+ { // Test the last word for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(5, true, Alloc(1));
+ std::vector<bool, Alloc> out(8, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end());
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 3, out.end()));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i + 3]);
+ }
+ { // Test middle words for uint8_t
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> in(16, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(24, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 3);
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 5, out.end() - 3));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i + 5]);
+ }
+
+ { // Test the first (partial) word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(12, true, Alloc(1));
+ std::vector<bool, Alloc> out(16, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end() - 3), out.end() - 2);
+ // assert(std::ranges::equal(in.begin(), in.end() - 3, out.begin() + 5, out.end() - 2));
+ for (std::size_t i = 0; i < in.size() - 3; ++i)
+ assert(in[i] == out[i + 5]);
+ }
+ { // Test the last word for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(16, true, Alloc(1));
+ std::vector<bool, Alloc> out(16, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin() + 3, in.end()), out.end() - 3);
+ // assert(std::ranges::equal(in.begin() + 3, in.end(), out.begin(), out.end() - 1));
+ for (std::size_t i = 3; i < in.size(); ++i)
+ assert(in[i] == out[i - 3]);
+ }
+ { // Test the middle words for uint16_t
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> in(32, true, Alloc(1));
+ for (std::size_t i = 0; i < in.size(); i += 2)
+ in[i] = false;
+ std::vector<bool, Alloc> out(64, false, Alloc(1));
+ std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 4);
+ // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 28, out.end() - 4));
+ for (std::size_t i = 0; i < in.size(); ++i)
+ assert(in[i] == out[i + 28]);
+ }
+ }
#endif
return true;
More information about the libcxx-commits
mailing list