|
16 | 16 | #include <cassert> |
17 | 17 | #include <vector> |
18 | 18 |
|
| 19 | +#include "sized_allocator.h" |
19 | 20 | #include "test_macros.h" |
20 | 21 | #include "test_iterators.h" |
21 | 22 | #include "type_algorithms.h" |
@@ -109,6 +110,133 @@ TEST_CONSTEXPR_CXX20 bool test() { |
109 | 110 | assert(test_vector_bool(256)); |
110 | 111 | } |
111 | 112 |
|
| 113 | + // Make sure std::copy behaves properly with std::vector<bool> iterators with custom size types. |
| 114 | + { |
| 115 | + //// Tests for std::copy with aligned bits |
| 116 | + |
| 117 | + { // Test the first (partial) word for uint8_t |
| 118 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 119 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 120 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 121 | + std::copy(in.begin() + 4, in.end(), out.begin() + 4); |
| 122 | + // assert(std::equal(in.begin() + 4, in.end(), out.begin() + 4)); |
| 123 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 124 | + assert(in[i] == out[i]); |
| 125 | + } |
| 126 | + { // Test the last word for uint8_t |
| 127 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 128 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 129 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 130 | + std::copy(in.begin(), in.end(), out.begin()); |
| 131 | + // assert(std::equal(in.begin(), in.end(), out.begin())); |
| 132 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 133 | + assert(in[i] == out[i]); |
| 134 | + } |
| 135 | + { // Test middle words for uint8_t |
| 136 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 137 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 138 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 139 | + in[i] = false; |
| 140 | + std::vector<bool, Alloc> out(29, false, Alloc(1)); |
| 141 | + std::copy(in.begin(), in.end(), out.begin()); |
| 142 | + // assert(std::equal(in.begin(), in.end(), out.begin())); |
| 143 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 144 | + assert(in[i] == out[i]); |
| 145 | + } |
| 146 | + |
| 147 | + { // Test the first (partial) word for uint16_t |
| 148 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 149 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 150 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 151 | + std::copy(in.begin() + 4, in.end(), out.begin() + 4); |
| 152 | + // assert(std::equal(in.begin() + 4, in.end(), out.begin() + 4)); |
| 153 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 154 | + assert(in[i] == out[i]); |
| 155 | + } |
| 156 | + { // Test the last word for uint16_t |
| 157 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 158 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 159 | + std::vector<bool, Alloc> out(32, false, Alloc(1)); |
| 160 | + std::copy(in.begin(), in.end(), out.begin()); |
| 161 | + // assert(std::equal(in.begin(), in.end(), out.begin())); |
| 162 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 163 | + assert(in[i] == out[i]); |
| 164 | + } |
| 165 | + { // Test middle words for uint16_t |
| 166 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 167 | + std::vector<bool, Alloc> in(48, true, Alloc(1)); |
| 168 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 169 | + in[i] = false; |
| 170 | + std::vector<bool, Alloc> out(55, false, Alloc(1)); |
| 171 | + std::copy(in.begin(), in.end(), out.begin()); |
| 172 | + // assert(std::equal(in.begin(), in.end(), out.begin())); |
| 173 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 174 | + assert(in[i] == out[i]); |
| 175 | + } |
| 176 | + |
| 177 | + //// Tests for std::copy with unaligned bits |
| 178 | + |
| 179 | + { // Test the first (partial) word for uint8_t |
| 180 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 181 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 182 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 183 | + std::copy(in.begin() + 4, in.end(), out.begin() + 4); |
| 184 | + // assert(std::equal(in.begin() + 4, in.end(), out.begin() + 4)); |
| 185 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 186 | + assert(in[i] == out[i]); |
| 187 | + } |
| 188 | + { // Test the last word for uint8_t |
| 189 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 190 | + std::vector<bool, Alloc> in(4, true, Alloc(1)); |
| 191 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 192 | + std::copy(in.begin(), in.end(), out.begin() + 3); |
| 193 | + // assert(std::equal(in.begin(), in.end(), out.begin() + 3)); |
| 194 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 195 | + assert(in[i] == out[i + 3]); |
| 196 | + } |
| 197 | + { // Test middle words for uint8_t |
| 198 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 199 | + std::vector<bool, Alloc> in(16, true, Alloc(1)); |
| 200 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 201 | + in[i] = false; |
| 202 | + std::vector<bool, Alloc> out(24, false, Alloc(1)); |
| 203 | + std::copy(in.begin(), in.end(), out.begin() + 4); |
| 204 | + // assert(std::equal(in.begin(), in.end(), out.begin() + 4)); |
| 205 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 206 | + assert(in[i] == out[i + 4]); |
| 207 | + } |
| 208 | + |
| 209 | + { // Test the first (partial) word for uint16_t |
| 210 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 211 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 212 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 213 | + std::copy(in.begin() + 4, in.end(), out.begin() + 4); |
| 214 | + // assert(std::equal(in.begin() + 4, in.end(), out.begin() + 4)); |
| 215 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 216 | + assert(in[i] == out[i]); |
| 217 | + } |
| 218 | + { // Test the last word for uint16_t |
| 219 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 220 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 221 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 222 | + std::copy(in.begin(), in.end(), out.begin() + 3); |
| 223 | + // assert(std::equal(in.begin(), in.end(), out.begin() + 3)); |
| 224 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 225 | + assert(in[i] == out[i + 3]); |
| 226 | + } |
| 227 | + { // Test the middle words for uint16_t |
| 228 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 229 | + std::vector<bool, Alloc> in(32, true, Alloc(1)); |
| 230 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 231 | + in[i] = false; |
| 232 | + std::vector<bool, Alloc> out(64, false, Alloc(1)); |
| 233 | + std::copy(in.begin(), in.end(), out.begin() + 4); |
| 234 | + // assert(std::equal(in.begin(), in.end(), out.begin() + 4)); |
| 235 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 236 | + assert(in[i] == out[i + 4]); |
| 237 | + } |
| 238 | + } |
| 239 | + |
112 | 240 | return true; |
113 | 241 | } |
114 | 242 |
|
|
0 commit comments