|
13 | 13 | // constexpr OutIter // constexpr after C++17
|
14 | 14 | // copy_backward(InIter first, InIter last, OutIter result);
|
15 | 15 |
|
| 16 | +// XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 17 | + |
16 | 18 | #include <algorithm>
|
17 | 19 | #include <cassert>
|
18 | 20 | #include <vector>
|
19 | 21 |
|
| 22 | +#include "sized_allocator.h" |
20 | 23 | #include "test_macros.h"
|
21 | 24 | #include "test_iterators.h"
|
22 | 25 | #include "type_algorithms.h"
|
@@ -111,6 +114,146 @@ TEST_CONSTEXPR_CXX20 bool test() {
|
111 | 114 | assert(test_vector_bool(256));
|
112 | 115 | }
|
113 | 116 |
|
| 117 | + // Validate std::copy_backward with std::vector<bool> iterators and custom storage types. |
| 118 | + // Ensure that assigned bits hold the intended values, while unassigned bits stay unchanged. |
| 119 | + // Related issue: https://github.com/llvm/llvm-project/issues/131718. |
| 120 | + { |
| 121 | + //// Tests for std::copy_backward with aligned bits |
| 122 | + |
| 123 | + { // Test the first (partial) word for uint8_t |
| 124 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 125 | + std::vector<bool, Alloc> in(7, false, Alloc(1)); |
| 126 | + std::vector<bool, Alloc> out(8, true, Alloc(1)); |
| 127 | + std::copy_backward(in.begin(), in.begin() + 1, out.begin() + 1); |
| 128 | + assert(out[0] == false); |
| 129 | + for (std::size_t i = 1; i < out.size(); ++i) |
| 130 | + assert(out[i] == true); |
| 131 | + } |
| 132 | + { // Test the last (partial) word for uint8_t |
| 133 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 134 | + std::vector<bool, Alloc> in(8, false, Alloc(1)); |
| 135 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 136 | + in[i] = true; |
| 137 | + std::vector<bool, Alloc> out(8, true, Alloc(1)); |
| 138 | + std::copy_backward(in.end() - 4, in.end(), out.end()); |
| 139 | + for (std::size_t i = 0; i < static_cast<std::size_t>(in.size() - 4); ++i) |
| 140 | + assert(out[i] == true); |
| 141 | + for (std::size_t i = in.size() + 4; i < out.size(); ++i) |
| 142 | + assert(in[i] == out[i]); |
| 143 | + } |
| 144 | + { // Test the middle (whole) words for uint8_t |
| 145 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 146 | + std::vector<bool, Alloc> in(17, false, Alloc(1)); |
| 147 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 148 | + in[i] = true; |
| 149 | + std::vector<bool, Alloc> out(24, true, Alloc(1)); |
| 150 | + std::copy_backward(in.begin(), in.end(), out.begin() + in.size()); |
| 151 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 152 | + assert(in[i] == out[i]); |
| 153 | + for (std::size_t i = in.size(); i < out.size(); ++i) |
| 154 | + assert(out[i] == true); |
| 155 | + } |
| 156 | + |
| 157 | + { // Test the first (partial) word for uint16_t |
| 158 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 159 | + std::vector<bool, Alloc> in(14, false, Alloc(1)); |
| 160 | + std::vector<bool, Alloc> out(16, true, Alloc(1)); |
| 161 | + std::copy_backward(in.begin(), in.begin() + 2, out.begin() + 2); |
| 162 | + assert(out[0] == false); |
| 163 | + assert(out[1] == false); |
| 164 | + for (std::size_t i = 2; i < out.size(); ++i) |
| 165 | + assert(out[i] == true); |
| 166 | + } |
| 167 | + { // Test the last (partial) word for uint16_t |
| 168 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 169 | + std::vector<bool, Alloc> in(16, false, Alloc(1)); |
| 170 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 171 | + in[i] = true; |
| 172 | + std::vector<bool, Alloc> out(16, true, Alloc(1)); |
| 173 | + std::copy_backward(in.end() - 8, in.end(), out.end()); |
| 174 | + for (std::size_t i = 0; i < static_cast<std::size_t>(in.size() - 8); ++i) |
| 175 | + assert(out[i] == true); |
| 176 | + for (std::size_t i = in.size() + 8; i < out.size(); ++i) |
| 177 | + assert(in[i] == out[i]); |
| 178 | + } |
| 179 | + { // Test the middle (whole) words for uint16_t |
| 180 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 181 | + std::vector<bool, Alloc> in(34, false, Alloc(1)); |
| 182 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 183 | + in[i] = true; |
| 184 | + std::vector<bool, Alloc> out(48, true, Alloc(1)); |
| 185 | + std::copy_backward(in.begin(), in.end(), out.begin() + in.size()); |
| 186 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 187 | + assert(in[i] == out[i]); |
| 188 | + for (std::size_t i = in.size(); i < out.size(); ++i) |
| 189 | + assert(out[i] == true); |
| 190 | + } |
| 191 | + |
| 192 | + //// Tests for std::copy_backward with unaligned bits |
| 193 | + |
| 194 | + { // Test the first (partial) word for uint8_t |
| 195 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 196 | + std::vector<bool, Alloc> in(8, false, Alloc(1)); |
| 197 | + std::vector<bool, Alloc> out(8, true, Alloc(1)); |
| 198 | + std::copy_backward(in.begin(), in.begin() + 1, out.begin() + 1); |
| 199 | + assert(out[0] == false); |
| 200 | + for (std::size_t i = 1; i < out.size(); ++i) |
| 201 | + assert(out[i] == true); |
| 202 | + } |
| 203 | + { // Test the last (partial) word for uint8_t |
| 204 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 205 | + std::vector<bool, Alloc> in(8, false, Alloc(1)); |
| 206 | + std::vector<bool, Alloc> out(8, true, Alloc(1)); |
| 207 | + std::copy_backward(in.end() - 1, in.end(), out.begin() + 1); |
| 208 | + assert(out[0] == false); |
| 209 | + for (std::size_t i = 1; i < out.size(); ++i) |
| 210 | + assert(out[i] == true); |
| 211 | + } |
| 212 | + { // Test the middle (whole) words for uint8_t |
| 213 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 214 | + std::vector<bool, Alloc> in(16, false, Alloc(1)); |
| 215 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 216 | + in[i] = true; |
| 217 | + std::vector<bool, Alloc> out(17, true, Alloc(1)); |
| 218 | + std::copy_backward(in.begin(), in.end(), out.end()); |
| 219 | + assert(out[0] == true); |
| 220 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 221 | + assert(in[i] == out[i + 1]); |
| 222 | + } |
| 223 | + |
| 224 | + { // Test the first (partial) word for uint16_t |
| 225 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 226 | + std::vector<bool, Alloc> in(16, false, Alloc(1)); |
| 227 | + std::vector<bool, Alloc> out(16, true, Alloc(1)); |
| 228 | + std::copy_backward(in.begin(), in.begin() + 2, out.begin() + 2); |
| 229 | + assert(out[0] == false); |
| 230 | + assert(out[1] == false); |
| 231 | + for (std::size_t i = 2; i < out.size(); ++i) |
| 232 | + assert(out[i] == true); |
| 233 | + } |
| 234 | + { // Test the last (partial) word for uint16_t |
| 235 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 236 | + std::vector<bool, Alloc> in(16, false, Alloc(1)); |
| 237 | + std::vector<bool, Alloc> out(16, true, Alloc(1)); |
| 238 | + std::copy_backward(in.end() - 2, in.end(), out.begin() + 2); |
| 239 | + assert(out[0] == false); |
| 240 | + assert(out[1] == false); |
| 241 | + for (std::size_t i = 2; i < out.size(); ++i) |
| 242 | + assert(out[i] == true); |
| 243 | + } |
| 244 | + { // Test the middle (whole) words for uint16_t |
| 245 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 246 | + std::vector<bool, Alloc> in(32, false, Alloc(1)); |
| 247 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 248 | + in[i] = true; |
| 249 | + std::vector<bool, Alloc> out(33, true, Alloc(1)); |
| 250 | + std::copy_backward(in.begin(), in.end(), out.end()); |
| 251 | + assert(out[0] == true); |
| 252 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 253 | + assert(in[i] == out[i + 1]); |
| 254 | + } |
| 255 | + } |
| 256 | + |
114 | 257 | return true;
|
115 | 258 | }
|
116 | 259 |
|
|
0 commit comments