Brian Silverman | a6f7ce0 | 2018-07-07 15:04:00 -0700 | [diff] [blame^] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 | // |
| 5 | // This code is licensed under the MIT License (MIT). |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | // THE SOFTWARE. |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHECK... |
| 18 | |
| 19 | #include <gsl/gsl_byte> // for byte |
| 20 | #include <gsl/gsl_util> // for narrow_cast |
| 21 | #include <gsl/multi_span> // for multi_span, contiguous_span_iterator, dim |
| 22 | |
| 23 | #include <algorithm> // for fill, for_each |
| 24 | #include <array> // for array |
| 25 | #include <iostream> // for ptrdiff_t, size_t |
| 26 | #include <iterator> // for reverse_iterator, begin, end, operator!= |
| 27 | #include <numeric> // for iota |
| 28 | #include <stddef.h> // for ptrdiff_t |
| 29 | #include <string> // for string |
| 30 | #include <vector> // for vector |
| 31 | |
| 32 | namespace gsl { |
| 33 | struct fail_fast; |
| 34 | } // namespace gsl |
| 35 | |
| 36 | using namespace std; |
| 37 | using namespace gsl; |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | struct BaseClass |
| 42 | { |
| 43 | }; |
| 44 | struct DerivedClass : BaseClass |
| 45 | { |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | TEST_CASE("default_constructor") |
| 50 | { |
| 51 | { |
| 52 | multi_span<int> s; |
| 53 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 54 | |
| 55 | multi_span<const int> cs; |
| 56 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | multi_span<int, 0> s; |
| 61 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 62 | |
| 63 | multi_span<const int, 0> cs; |
| 64 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 69 | multi_span<int, 1> s; |
| 70 | CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | { |
| 75 | multi_span<int> s{}; |
| 76 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 77 | |
| 78 | multi_span<const int> cs{}; |
| 79 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | TEST_CASE("from_nullptr_constructor") |
| 84 | { |
| 85 | { |
| 86 | multi_span<int> s = nullptr; |
| 87 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 88 | |
| 89 | multi_span<const int> cs = nullptr; |
| 90 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | multi_span<int, 0> s = nullptr; |
| 95 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 96 | |
| 97 | multi_span<const int, 0> cs = nullptr; |
| 98 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 99 | } |
| 100 | |
| 101 | { |
| 102 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 103 | multi_span<int, 1> s = nullptr; |
| 104 | CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | { |
| 109 | multi_span<int> s{nullptr}; |
| 110 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 111 | |
| 112 | multi_span<const int> cs{nullptr}; |
| 113 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 114 | } |
| 115 | |
| 116 | { |
| 117 | multi_span<int*> s{nullptr}; |
| 118 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 119 | |
| 120 | multi_span<const int*> cs{nullptr}; |
| 121 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | TEST_CASE("from_nullptr_length_constructor") |
| 126 | { |
| 127 | { |
| 128 | multi_span<int> s{nullptr, 0}; |
| 129 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 130 | |
| 131 | multi_span<const int> cs{nullptr, 0}; |
| 132 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | multi_span<int, 0> s{nullptr, 0}; |
| 137 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 138 | |
| 139 | multi_span<const int, 0> cs{nullptr, 0}; |
| 140 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 141 | } |
| 142 | |
| 143 | { |
| 144 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 145 | multi_span<int, 1> s{nullptr, 0}; |
| 146 | CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile |
| 147 | #endif |
| 148 | } |
| 149 | |
| 150 | { |
| 151 | auto workaround_macro = []() { multi_span<int> s{nullptr, 1}; }; |
| 152 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 153 | |
| 154 | auto const_workaround_macro = []() { multi_span<const int> cs{nullptr, 1}; }; |
| 155 | CHECK_THROWS_AS(const_workaround_macro(), fail_fast); |
| 156 | } |
| 157 | |
| 158 | { |
| 159 | auto workaround_macro = []() { multi_span<int, 0> s{nullptr, 1}; }; |
| 160 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 161 | |
| 162 | auto const_workaround_macro = []() { multi_span<const int, 0> s{nullptr, 1}; }; |
| 163 | CHECK_THROWS_AS(const_workaround_macro(), fail_fast); |
| 164 | } |
| 165 | |
| 166 | { |
| 167 | multi_span<int*> s{nullptr, 0}; |
| 168 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 169 | |
| 170 | multi_span<const int*> cs{nullptr, 0}; |
| 171 | CHECK((cs.length() == 0 && cs.data() == nullptr)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | TEST_CASE("from_element_constructor") |
| 176 | { |
| 177 | int i = 5; |
| 178 | |
| 179 | { |
| 180 | multi_span<int> s = i; |
| 181 | CHECK((s.length() == 1 && s.data() == &i)); |
| 182 | CHECK(s[0] == 5); |
| 183 | |
| 184 | multi_span<const int> cs = i; |
| 185 | CHECK((cs.length() == 1 && cs.data() == &i)); |
| 186 | CHECK(cs[0] == 5); |
| 187 | } |
| 188 | |
| 189 | { |
| 190 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 191 | const j = 1; |
| 192 | multi_span<int, 0> s = j; |
| 193 | #endif |
| 194 | } |
| 195 | |
| 196 | { |
| 197 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 198 | multi_span<int, 0> s = i; |
| 199 | CHECK((s.length() == 0 && s.data() == &i)); |
| 200 | #endif |
| 201 | } |
| 202 | |
| 203 | { |
| 204 | multi_span<int, 1> s = i; |
| 205 | CHECK((s.length() == 1 && s.data() == &i)); |
| 206 | CHECK(s[0] == 5); |
| 207 | } |
| 208 | |
| 209 | { |
| 210 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 211 | multi_span<int, 2> s = i; |
| 212 | CHECK((s.length() == 2 && s.data() == &i)); |
| 213 | #endif |
| 214 | } |
| 215 | |
| 216 | { |
| 217 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 218 | auto get_a_temp = []() -> int { return 4; }; |
| 219 | auto use_a_span = [](multi_span<int> s) { (void) s; }; |
| 220 | use_a_span(get_a_temp()); |
| 221 | #endif |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | TEST_CASE("from_pointer_length_constructor") |
| 226 | { |
| 227 | int arr[4] = {1, 2, 3, 4}; |
| 228 | |
| 229 | { |
| 230 | multi_span<int> s{&arr[0], 2}; |
| 231 | CHECK((s.length() == 2 && s.data() == &arr[0])); |
| 232 | CHECK((s[0] == 1 && s[1] == 2)); |
| 233 | } |
| 234 | |
| 235 | { |
| 236 | multi_span<int, 2> s{&arr[0], 2}; |
| 237 | CHECK((s.length() == 2 && s.data() == &arr[0])); |
| 238 | CHECK((s[0] == 1 && s[1] == 2)); |
| 239 | } |
| 240 | |
| 241 | { |
| 242 | int* p = nullptr; |
| 243 | multi_span<int> s{p, 0}; |
| 244 | CHECK((s.length() == 0 && s.data() == nullptr)); |
| 245 | } |
| 246 | |
| 247 | { |
| 248 | int* p = nullptr; |
| 249 | auto workaround_macro = [=]() { multi_span<int> s{p, 2}; }; |
| 250 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | TEST_CASE("from_pointer_pointer_constructor") |
| 255 | { |
| 256 | int arr[4] = {1, 2, 3, 4}; |
| 257 | |
| 258 | { |
| 259 | multi_span<int> s{&arr[0], &arr[2]}; |
| 260 | CHECK((s.length() == 2 && s.data() == &arr[0])); |
| 261 | CHECK((s[0] == 1 && s[1] == 2)); |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | multi_span<int, 2> s{&arr[0], &arr[2]}; |
| 266 | CHECK((s.length() == 2 && s.data() == &arr[0])); |
| 267 | CHECK((s[0] == 1 && s[1] == 2)); |
| 268 | } |
| 269 | |
| 270 | { |
| 271 | multi_span<int> s{&arr[0], &arr[0]}; |
| 272 | CHECK((s.length() == 0 && s.data() == &arr[0])); |
| 273 | } |
| 274 | |
| 275 | { |
| 276 | multi_span<int, 0> s{&arr[0], &arr[0]}; |
| 277 | CHECK((s.length() == 0 && s.data() == &arr[0])); |
| 278 | } |
| 279 | |
| 280 | { |
| 281 | auto workaround_macro = [&]() { multi_span<int> s{&arr[1], &arr[0]}; }; |
| 282 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 283 | } |
| 284 | |
| 285 | { |
| 286 | int* p = nullptr; |
| 287 | auto workaround_macro = [&]() { multi_span<int> s{&arr[0], p}; }; |
| 288 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 289 | } |
| 290 | |
| 291 | { |
| 292 | int* p = nullptr; |
| 293 | auto workaround_macro = [&]() { multi_span<int> s{p, p}; }; |
| 294 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 295 | } |
| 296 | |
| 297 | { |
| 298 | int* p = nullptr; |
| 299 | auto workaround_macro = [&]() { multi_span<int> s{&arr[0], p}; }; |
| 300 | CHECK_THROWS_AS(workaround_macro(), fail_fast); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | TEST_CASE("from_array_constructor") |
| 305 | { |
| 306 | int arr[5] = {1, 2, 3, 4, 5}; |
| 307 | |
| 308 | { |
| 309 | multi_span<int> s{arr}; |
| 310 | CHECK((s.length() == 5 && s.data() == &arr[0])); |
| 311 | } |
| 312 | |
| 313 | { |
| 314 | multi_span<int, 5> s{arr}; |
| 315 | CHECK((s.length() == 5 && s.data() == &arr[0])); |
| 316 | } |
| 317 | |
| 318 | { |
| 319 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 320 | multi_span<int, 6> s{arr}; |
| 321 | #endif |
| 322 | } |
| 323 | |
| 324 | { |
| 325 | multi_span<int, 0> s{arr}; |
| 326 | CHECK((s.length() == 0 && s.data() == &arr[0])); |
| 327 | } |
| 328 | |
| 329 | int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; |
| 330 | |
| 331 | { |
| 332 | multi_span<int> s{arr2d}; |
| 333 | CHECK((s.length() == 6 && s.data() == &arr2d[0][0])); |
| 334 | CHECK((s[0] == 1 && s[5] == 6)); |
| 335 | } |
| 336 | |
| 337 | { |
| 338 | multi_span<int, 0> s{arr2d}; |
| 339 | CHECK((s.length() == 0 && s.data() == &arr2d[0][0])); |
| 340 | } |
| 341 | |
| 342 | { |
| 343 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 344 | multi_span<int, 5> s{arr2d}; |
| 345 | #endif |
| 346 | } |
| 347 | |
| 348 | { |
| 349 | multi_span<int, 6> s{arr2d}; |
| 350 | CHECK((s.length() == 6 && s.data() == &arr2d[0][0])); |
| 351 | CHECK((s[0] == 1 && s[5] == 6)); |
| 352 | } |
| 353 | |
| 354 | { |
| 355 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 356 | multi_span<int, 7> s{arr2d}; |
| 357 | #endif |
| 358 | } |
| 359 | |
| 360 | { |
| 361 | multi_span<int[3]> s{arr2d[0]}; |
| 362 | CHECK((s.length() == 1 && s.data() == &arr2d[0])); |
| 363 | } |
| 364 | |
| 365 | { |
| 366 | multi_span<int, 2, 3> s{arr2d}; |
| 367 | CHECK((s.length() == 6 && s.data() == &arr2d[0][0])); |
| 368 | auto workaround_macro = [&]() { return s[{1, 2}] == 6; }; |
| 369 | CHECK(workaround_macro()); |
| 370 | } |
| 371 | |
| 372 | { |
| 373 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 374 | multi_span<int, 3, 3> s{arr2d}; |
| 375 | #endif |
| 376 | } |
| 377 | |
| 378 | int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
| 379 | |
| 380 | { |
| 381 | multi_span<int> s{arr3d}; |
| 382 | CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); |
| 383 | CHECK((s[0] == 1 && s[11] == 12)); |
| 384 | } |
| 385 | |
| 386 | { |
| 387 | multi_span<int, 0> s{arr3d}; |
| 388 | CHECK((s.length() == 0 && s.data() == &arr3d[0][0][0])); |
| 389 | } |
| 390 | |
| 391 | { |
| 392 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 393 | multi_span<int, 11> s{arr3d}; |
| 394 | #endif |
| 395 | } |
| 396 | |
| 397 | { |
| 398 | multi_span<int, 12> s{arr3d}; |
| 399 | CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); |
| 400 | CHECK((s[0] == 1 && s[5] == 6)); |
| 401 | } |
| 402 | |
| 403 | { |
| 404 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 405 | multi_span<int, 13> s{arr3d}; |
| 406 | #endif |
| 407 | } |
| 408 | |
| 409 | { |
| 410 | multi_span<int[3][2]> s{arr3d[0]}; |
| 411 | CHECK((s.length() == 1 && s.data() == &arr3d[0])); |
| 412 | } |
| 413 | |
| 414 | { |
| 415 | multi_span<int, 3, 2, 2> s{arr3d}; |
| 416 | CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); |
| 417 | auto workaround_macro = [&]() { return s[{2, 1, 0}] == 11; }; |
| 418 | CHECK(workaround_macro()); |
| 419 | } |
| 420 | |
| 421 | { |
| 422 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 423 | multi_span<int, 3, 3, 3> s{arr3d}; |
| 424 | #endif |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | TEST_CASE("from_dynamic_array_constructor") |
| 429 | { |
| 430 | double(*arr)[3][4] = new double[100][3][4]; |
| 431 | |
| 432 | { |
| 433 | multi_span<double, dynamic_range, 3, 4> s(arr, 10); |
| 434 | CHECK((s.length() == 120 && s.data() == &arr[0][0][0])); |
| 435 | CHECK_THROWS_AS(s[10][3][4], fail_fast); |
| 436 | } |
| 437 | |
| 438 | { |
| 439 | multi_span<double, dynamic_range, 4, 3> s(arr, 10); |
| 440 | CHECK((s.length() == 120 && s.data() == &arr[0][0][0])); |
| 441 | } |
| 442 | |
| 443 | { |
| 444 | multi_span<double> s(arr, 10); |
| 445 | CHECK((s.length() == 120 && s.data() == &arr[0][0][0])); |
| 446 | } |
| 447 | |
| 448 | { |
| 449 | multi_span<double, dynamic_range, 3, 4> s(arr, 0); |
| 450 | CHECK((s.length() == 0 && s.data() == &arr[0][0][0])); |
| 451 | } |
| 452 | |
| 453 | delete[] arr; |
| 454 | } |
| 455 | |
| 456 | TEST_CASE("from_std_array_constructor") |
| 457 | { |
| 458 | std::array<int, 4> arr = {1, 2, 3, 4}; |
| 459 | |
| 460 | { |
| 461 | multi_span<int> s{arr}; |
| 462 | CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data())); |
| 463 | |
| 464 | multi_span<const int> cs{arr}; |
| 465 | CHECK((cs.size() == narrow_cast<ptrdiff_t>(arr.size()) && cs.data() == arr.data())); |
| 466 | } |
| 467 | |
| 468 | { |
| 469 | multi_span<int, 4> s{arr}; |
| 470 | CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data())); |
| 471 | |
| 472 | multi_span<const int, 4> cs{arr}; |
| 473 | CHECK((cs.size() == narrow_cast<ptrdiff_t>(arr.size()) && cs.data() == arr.data())); |
| 474 | } |
| 475 | |
| 476 | { |
| 477 | multi_span<int, 2> s{arr}; |
| 478 | CHECK((s.size() == 2 && s.data() == arr.data())); |
| 479 | |
| 480 | multi_span<const int, 2> cs{arr}; |
| 481 | CHECK((cs.size() == 2 && cs.data() == arr.data())); |
| 482 | } |
| 483 | |
| 484 | { |
| 485 | multi_span<int, 0> s{arr}; |
| 486 | CHECK((s.size() == 0 && s.data() == arr.data())); |
| 487 | |
| 488 | multi_span<const int, 0> cs{arr}; |
| 489 | CHECK((cs.size() == 0 && cs.data() == arr.data())); |
| 490 | } |
| 491 | |
| 492 | // TODO This is currently an unsupported scenario. We will come back to it as we revise |
| 493 | // the multidimensional interface and what transformations between dimensionality look like |
| 494 | //{ |
| 495 | // multi_span<int, 2, 2> s{arr}; |
| 496 | // CHECK(s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()); |
| 497 | //} |
| 498 | |
| 499 | { |
| 500 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 501 | multi_span<int, 5> s{arr}; |
| 502 | #endif |
| 503 | } |
| 504 | |
| 505 | { |
| 506 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 507 | auto get_an_array = []() { return std::array<int, 4>{1, 2, 3, 4}; }; |
| 508 | auto take_a_span = [](multi_span<int> s) { (void) s; }; |
| 509 | // try to take a temporary std::array |
| 510 | take_a_span(get_an_array()); |
| 511 | #endif |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | TEST_CASE("from_const_std_array_constructor") |
| 516 | { |
| 517 | const std::array<int, 4> arr = {1, 2, 3, 4}; |
| 518 | |
| 519 | { |
| 520 | multi_span<const int> s{arr}; |
| 521 | CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data())); |
| 522 | } |
| 523 | |
| 524 | { |
| 525 | multi_span<const int, 4> s{arr}; |
| 526 | CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data())); |
| 527 | } |
| 528 | |
| 529 | { |
| 530 | multi_span<const int, 2> s{arr}; |
| 531 | CHECK((s.size() == 2 && s.data() == arr.data())); |
| 532 | } |
| 533 | |
| 534 | { |
| 535 | multi_span<const int, 0> s{arr}; |
| 536 | CHECK((s.size() == 0 && s.data() == arr.data())); |
| 537 | } |
| 538 | |
| 539 | // TODO This is currently an unsupported scenario. We will come back to it as we revise |
| 540 | // the multidimensional interface and what transformations between dimensionality look like |
| 541 | //{ |
| 542 | // multi_span<int, 2, 2> s{arr}; |
| 543 | // CHECK(s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()); |
| 544 | //} |
| 545 | |
| 546 | { |
| 547 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 548 | multi_span<const int, 5> s{arr}; |
| 549 | #endif |
| 550 | } |
| 551 | |
| 552 | { |
| 553 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 554 | auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; }; |
| 555 | auto take_a_span = [](multi_span<const int> s) { (void) s; }; |
| 556 | // try to take a temporary std::array |
| 557 | take_a_span(get_an_array()); |
| 558 | #endif |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | TEST_CASE("from_container_constructor") |
| 563 | { |
| 564 | std::vector<int> v = {1, 2, 3}; |
| 565 | const std::vector<int> cv = v; |
| 566 | |
| 567 | { |
| 568 | multi_span<int> s{v}; |
| 569 | CHECK((s.size() == narrow_cast<std::ptrdiff_t>(v.size()) && s.data() == v.data())); |
| 570 | |
| 571 | multi_span<const int> cs{v}; |
| 572 | CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(v.size()) && cs.data() == v.data())); |
| 573 | } |
| 574 | |
| 575 | std::string str = "hello"; |
| 576 | const std::string cstr = "hello"; |
| 577 | |
| 578 | { |
| 579 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 580 | multi_span<char> s{str}; |
| 581 | CHECK((s.size() == narrow_cast<std::ptrdiff_t>(str.size()) && s.data() == str.data())); |
| 582 | #endif |
| 583 | multi_span<const char> cs{str}; |
| 584 | CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(str.size()) && cs.data() == str.data())); |
| 585 | } |
| 586 | |
| 587 | { |
| 588 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 589 | multi_span<char> s{cstr}; |
| 590 | #endif |
| 591 | multi_span<const char> cs{cstr}; |
| 592 | CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(cstr.size()) && |
| 593 | cs.data() == cstr.data())); |
| 594 | } |
| 595 | |
| 596 | { |
| 597 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 598 | auto get_temp_vector = []() -> std::vector<int> { return {}; }; |
| 599 | auto use_span = [](multi_span<int> s) { (void) s; }; |
| 600 | use_span(get_temp_vector()); |
| 601 | #endif |
| 602 | } |
| 603 | |
| 604 | { |
| 605 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 606 | auto get_temp_string = []() -> std::string { return {}; }; |
| 607 | auto use_span = [](multi_span<char> s) { (void) s; }; |
| 608 | use_span(get_temp_string()); |
| 609 | #endif |
| 610 | } |
| 611 | |
| 612 | { |
| 613 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 614 | auto get_temp_vector = []() -> const std::vector<int> { return {}; }; |
| 615 | auto use_span = [](multi_span<const char> s) { (void) s; }; |
| 616 | use_span(get_temp_vector()); |
| 617 | #endif |
| 618 | } |
| 619 | |
| 620 | { |
| 621 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 622 | auto get_temp_string = []() -> const std::string { return {}; }; |
| 623 | auto use_span = [](multi_span<const char> s) { (void) s; }; |
| 624 | use_span(get_temp_string()); |
| 625 | #endif |
| 626 | } |
| 627 | |
| 628 | { |
| 629 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 630 | std::map<int, int> m; |
| 631 | multi_span<int> s{m}; |
| 632 | #endif |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | TEST_CASE("from_convertible_span_constructor") |
| 637 | { |
| 638 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 639 | multi_span<int, 7, 4, 2> av1(nullptr, b1); |
| 640 | |
| 641 | auto f = [&]() { multi_span<int, 7, 4, 2> av1(nullptr); }; |
| 642 | CHECK_THROWS_AS(f(), fail_fast); |
| 643 | #endif |
| 644 | |
| 645 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 646 | static_bounds<std::size_t, 7, dynamic_range, 2> b12(b11); |
| 647 | b12 = b11; |
| 648 | b11 = b12; |
| 649 | |
| 650 | multi_span<int, dynamic_range> av1 = nullptr; |
| 651 | multi_span<int, 7, dynamic_range, 2> av2(av1); |
| 652 | multi_span<int, 7, 4, 2> av2(av1); |
| 653 | #endif |
| 654 | |
| 655 | multi_span<DerivedClass> avd; |
| 656 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 657 | multi_span<BaseClass> avb = avd; |
| 658 | #endif |
| 659 | multi_span<const DerivedClass> avcd = avd; |
| 660 | (void) avcd; |
| 661 | } |
| 662 | |
| 663 | TEST_CASE("copy_move_and_assignment") |
| 664 | { |
| 665 | multi_span<int> s1; |
| 666 | CHECK(s1.empty()); |
| 667 | |
| 668 | int arr[] = {3, 4, 5}; |
| 669 | |
| 670 | multi_span<const int> s2 = arr; |
| 671 | CHECK((s2.length() == 3 && s2.data() == &arr[0])); |
| 672 | |
| 673 | s2 = s1; |
| 674 | CHECK(s2.empty()); |
| 675 | |
| 676 | auto get_temp_span = [&]() -> multi_span<int> { return {&arr[1], 2}; }; |
| 677 | auto use_span = [&](multi_span<const int> s) { |
| 678 | CHECK((s.length() == 2 && s.data() == &arr[1])); |
| 679 | }; |
| 680 | use_span(get_temp_span()); |
| 681 | |
| 682 | s1 = get_temp_span(); |
| 683 | CHECK((s1.length() == 2 && s1.data() == &arr[1])); |
| 684 | } |
| 685 | |
| 686 | template <class Bounds> |
| 687 | void fn(const Bounds&) |
| 688 | { |
| 689 | static_assert(Bounds::static_size == 60, "static bounds is wrong size"); |
| 690 | } |
| 691 | TEST_CASE("as_multi_span_reshape") |
| 692 | { |
| 693 | int a[3][4][5]; |
| 694 | auto av = as_multi_span(a); |
| 695 | fn(av.bounds()); |
| 696 | auto av2 = as_multi_span(av, dim<60>()); |
| 697 | auto av3 = as_multi_span(av2, dim<3>(), dim<4>(), dim<5>()); |
| 698 | auto av4 = as_multi_span(av3, dim<4>(), dim(3), dim<5>()); |
| 699 | auto av5 = as_multi_span(av4, dim<3>(), dim<4>(), dim<5>()); |
| 700 | auto av6 = as_multi_span(av5, dim<12>(), dim(5)); |
| 701 | |
| 702 | fill(av6.begin(), av6.end(), 1); |
| 703 | |
| 704 | auto av7 = as_bytes(av6); |
| 705 | |
| 706 | auto av8 = as_multi_span<int>(av7); |
| 707 | |
| 708 | CHECK(av8.size() == av6.size()); |
| 709 | for (auto i = 0; i < av8.size(); i++) { |
| 710 | CHECK(av8[i] == 1); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | TEST_CASE("first") |
| 715 | { |
| 716 | int arr[5] = {1, 2, 3, 4, 5}; |
| 717 | |
| 718 | { |
| 719 | multi_span<int, 5> av = arr; |
| 720 | CHECK((av.first<2>().bounds() == static_bounds<2>())); |
| 721 | CHECK(av.first<2>().length() == 2); |
| 722 | CHECK(av.first(2).length() == 2); |
| 723 | } |
| 724 | |
| 725 | { |
| 726 | multi_span<int, 5> av = arr; |
| 727 | CHECK((av.first<0>().bounds() == static_bounds<0>())); |
| 728 | CHECK(av.first<0>().length() == 0); |
| 729 | CHECK(av.first(0).length() == 0); |
| 730 | } |
| 731 | |
| 732 | { |
| 733 | multi_span<int, 5> av = arr; |
| 734 | CHECK((av.first<5>().bounds() == static_bounds<5>())); |
| 735 | CHECK(av.first<5>().length() == 5); |
| 736 | CHECK(av.first(5).length() == 5); |
| 737 | } |
| 738 | |
| 739 | { |
| 740 | multi_span<int, 5> av = arr; |
| 741 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 742 | CHECK(av.first<6>().bounds() == static_bounds<6>()); |
| 743 | CHECK(av.first<6>().length() == 6); |
| 744 | CHECK(av.first<-1>().length() == -1); |
| 745 | #endif |
| 746 | CHECK_THROWS_AS(av.first(6).length(), fail_fast); |
| 747 | } |
| 748 | |
| 749 | { |
| 750 | multi_span<int, dynamic_range> av; |
| 751 | CHECK((av.first<0>().bounds() == static_bounds<0>())); |
| 752 | CHECK(av.first<0>().length() == 0); |
| 753 | CHECK(av.first(0).length() == 0); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | TEST_CASE("last") |
| 758 | { |
| 759 | int arr[5] = {1, 2, 3, 4, 5}; |
| 760 | |
| 761 | { |
| 762 | multi_span<int, 5> av = arr; |
| 763 | CHECK((av.last<2>().bounds() == static_bounds<2>())); |
| 764 | CHECK(av.last<2>().length() == 2); |
| 765 | CHECK(av.last(2).length() == 2); |
| 766 | } |
| 767 | |
| 768 | { |
| 769 | multi_span<int, 5> av = arr; |
| 770 | CHECK((av.last<0>().bounds() == static_bounds<0>())); |
| 771 | CHECK(av.last<0>().length() == 0); |
| 772 | CHECK(av.last(0).length() == 0); |
| 773 | } |
| 774 | |
| 775 | { |
| 776 | multi_span<int, 5> av = arr; |
| 777 | CHECK((av.last<5>().bounds() == static_bounds<5>())); |
| 778 | CHECK(av.last<5>().length() == 5); |
| 779 | CHECK(av.last(5).length() == 5); |
| 780 | } |
| 781 | |
| 782 | { |
| 783 | multi_span<int, 5> av = arr; |
| 784 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 785 | CHECK((av.last<6>().bounds() == static_bounds<6>())); |
| 786 | CHECK(av.last<6>().length() == 6); |
| 787 | #endif |
| 788 | CHECK_THROWS_AS(av.last(6).length(), fail_fast); |
| 789 | } |
| 790 | |
| 791 | { |
| 792 | multi_span<int, dynamic_range> av; |
| 793 | CHECK((av.last<0>().bounds() == static_bounds<0>())); |
| 794 | CHECK(av.last<0>().length() == 0); |
| 795 | CHECK(av.last(0).length() == 0); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | TEST_CASE("subspan") |
| 800 | { |
| 801 | int arr[5] = {1, 2, 3, 4, 5}; |
| 802 | |
| 803 | { |
| 804 | multi_span<int, 5> av = arr; |
| 805 | CHECK((av.subspan<2, 2>().bounds() == static_bounds<2>())); |
| 806 | CHECK((av.subspan<2, 2>().length() == 2)); |
| 807 | CHECK(av.subspan(2, 2).length() == 2); |
| 808 | CHECK(av.subspan(2, 3).length() == 3); |
| 809 | } |
| 810 | |
| 811 | { |
| 812 | multi_span<int, 5> av = arr; |
| 813 | CHECK((av.subspan<0, 0>().bounds() == static_bounds<0>())); |
| 814 | CHECK((av.subspan<0, 0>().length() == 0)); |
| 815 | CHECK(av.subspan(0, 0).length() == 0); |
| 816 | } |
| 817 | |
| 818 | { |
| 819 | multi_span<int, 5> av = arr; |
| 820 | CHECK((av.subspan<0, 5>().bounds() == static_bounds<5>())); |
| 821 | CHECK((av.subspan<0, 5>().length() == 5)); |
| 822 | CHECK(av.subspan(0, 5).length() == 5); |
| 823 | CHECK_THROWS_AS(av.subspan(0, 6).length(), fail_fast); |
| 824 | CHECK_THROWS_AS(av.subspan(1, 5).length(), fail_fast); |
| 825 | } |
| 826 | |
| 827 | { |
| 828 | multi_span<int, 5> av = arr; |
| 829 | CHECK((av.subspan<5, 0>().bounds() == static_bounds<0>())); |
| 830 | CHECK((av.subspan<5, 0>().length() == 0)); |
| 831 | CHECK(av.subspan(5, 0).length() == 0); |
| 832 | CHECK_THROWS_AS(av.subspan(6, 0).length(), fail_fast); |
| 833 | } |
| 834 | |
| 835 | { |
| 836 | multi_span<int, dynamic_range> av; |
| 837 | CHECK((av.subspan<0, 0>().bounds() == static_bounds<0>())); |
| 838 | CHECK((av.subspan<0, 0>().length() == 0)); |
| 839 | CHECK(av.subspan(0, 0).length() == 0); |
| 840 | CHECK_THROWS_AS((av.subspan<1, 0>().length()), fail_fast); |
| 841 | } |
| 842 | |
| 843 | { |
| 844 | multi_span<int> av; |
| 845 | CHECK(av.subspan(0).length() == 0); |
| 846 | CHECK_THROWS_AS(av.subspan(1).length(), fail_fast); |
| 847 | } |
| 848 | |
| 849 | { |
| 850 | multi_span<int> av = arr; |
| 851 | CHECK(av.subspan(0).length() == 5); |
| 852 | CHECK(av.subspan(1).length() == 4); |
| 853 | CHECK(av.subspan(4).length() == 1); |
| 854 | CHECK(av.subspan(5).length() == 0); |
| 855 | CHECK_THROWS_AS(av.subspan(6).length(), fail_fast); |
| 856 | auto av2 = av.subspan(1); |
| 857 | for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); |
| 858 | } |
| 859 | |
| 860 | { |
| 861 | multi_span<int, 5> av = arr; |
| 862 | CHECK(av.subspan(0).length() == 5); |
| 863 | CHECK(av.subspan(1).length() == 4); |
| 864 | CHECK(av.subspan(4).length() == 1); |
| 865 | CHECK(av.subspan(5).length() == 0); |
| 866 | CHECK_THROWS_AS(av.subspan(6).length(), fail_fast); |
| 867 | auto av2 = av.subspan(1); |
| 868 | for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | TEST_CASE("rank") |
| 873 | { |
| 874 | int arr[2] = {1, 2}; |
| 875 | |
| 876 | { |
| 877 | multi_span<int> s; |
| 878 | CHECK(s.rank() == 1); |
| 879 | } |
| 880 | |
| 881 | { |
| 882 | multi_span<int, 2> s = arr; |
| 883 | CHECK(s.rank() == 1); |
| 884 | } |
| 885 | |
| 886 | int arr2d[1][1] = {}; |
| 887 | { |
| 888 | multi_span<int, 1, 1> s = arr2d; |
| 889 | CHECK(s.rank() == 2); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | TEST_CASE("extent") |
| 894 | { |
| 895 | { |
| 896 | multi_span<int> s; |
| 897 | CHECK(s.extent() == 0); |
| 898 | CHECK(s.extent(0) == 0); |
| 899 | CHECK_THROWS_AS(s.extent(1), fail_fast); |
| 900 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 901 | CHECK(s.extent<1>() == 0); |
| 902 | #endif |
| 903 | } |
| 904 | |
| 905 | { |
| 906 | multi_span<int, 0> s; |
| 907 | CHECK(s.extent() == 0); |
| 908 | CHECK(s.extent(0) == 0); |
| 909 | CHECK_THROWS_AS(s.extent(1), fail_fast); |
| 910 | } |
| 911 | |
| 912 | { |
| 913 | int arr2d[1][2] = {}; |
| 914 | |
| 915 | multi_span<int, 1, 2> s = arr2d; |
| 916 | CHECK(s.extent() == 1); |
| 917 | CHECK(s.extent<0>() == 1); |
| 918 | CHECK(s.extent<1>() == 2); |
| 919 | CHECK(s.extent(0) == 1); |
| 920 | CHECK(s.extent(1) == 2); |
| 921 | CHECK_THROWS_AS(s.extent(3), fail_fast); |
| 922 | } |
| 923 | |
| 924 | { |
| 925 | int arr2d[1][2] = {}; |
| 926 | |
| 927 | multi_span<int, 0, 2> s = arr2d; |
| 928 | CHECK(s.extent() == 0); |
| 929 | CHECK(s.extent<0>() == 0); |
| 930 | CHECK(s.extent<1>() == 2); |
| 931 | CHECK(s.extent(0) == 0); |
| 932 | CHECK(s.extent(1) == 2); |
| 933 | CHECK_THROWS_AS(s.extent(3), fail_fast); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | TEST_CASE("operator_function_call") |
| 938 | { |
| 939 | int arr[4] = {1, 2, 3, 4}; |
| 940 | |
| 941 | { |
| 942 | multi_span<int> s = arr; |
| 943 | CHECK(s(0) == 1); |
| 944 | CHECK_THROWS_AS(s(5), fail_fast); |
| 945 | } |
| 946 | |
| 947 | int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; |
| 948 | |
| 949 | { |
| 950 | multi_span<int, 2, 3> s = arr2d; |
| 951 | CHECK(s(0, 0) == 1); |
| 952 | CHECK(s(0, 1) == 2); |
| 953 | CHECK(s(1, 2) == 6); |
| 954 | } |
| 955 | |
| 956 | int arr3d[2][2][2] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 957 | |
| 958 | { |
| 959 | multi_span<int, 2, 2, 2> s = arr3d; |
| 960 | CHECK(s(0, 0, 0) == 1); |
| 961 | CHECK(s(1, 1, 1) == 8); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | TEST_CASE("comparison_operators") |
| 966 | { |
| 967 | { |
| 968 | int arr[10][2]; |
| 969 | auto s1 = as_multi_span(arr); |
| 970 | multi_span<const int, dynamic_range, 2> s2 = s1; |
| 971 | |
| 972 | CHECK(s1 == s2); |
| 973 | |
| 974 | multi_span<int, 20> s3 = as_multi_span(s1, dim(20)); |
| 975 | CHECK((s3 == s2 && s3 == s1)); |
| 976 | } |
| 977 | |
| 978 | { |
| 979 | multi_span<int> s1 = nullptr; |
| 980 | multi_span<int> s2 = nullptr; |
| 981 | CHECK(s1 == s2); |
| 982 | CHECK(!(s1 != s2)); |
| 983 | CHECK(!(s1 < s2)); |
| 984 | CHECK(s1 <= s2); |
| 985 | CHECK(!(s1 > s2)); |
| 986 | CHECK(s1 >= s2); |
| 987 | CHECK(s2 == s1); |
| 988 | CHECK(!(s2 != s1)); |
| 989 | CHECK(!(s2 < s1)); |
| 990 | CHECK(s2 <= s1); |
| 991 | CHECK(!(s2 > s1)); |
| 992 | CHECK(s2 >= s1); |
| 993 | } |
| 994 | |
| 995 | { |
| 996 | int arr[] = {2, 1}; // bigger |
| 997 | |
| 998 | multi_span<int> s1 = nullptr; |
| 999 | multi_span<int> s2 = arr; |
| 1000 | |
| 1001 | CHECK(s1 != s2); |
| 1002 | CHECK(s2 != s1); |
| 1003 | CHECK(!(s1 == s2)); |
| 1004 | CHECK(!(s2 == s1)); |
| 1005 | CHECK(s1 < s2); |
| 1006 | CHECK(!(s2 < s1)); |
| 1007 | CHECK(s1 <= s2); |
| 1008 | CHECK(!(s2 <= s1)); |
| 1009 | CHECK(s2 > s1); |
| 1010 | CHECK(!(s1 > s2)); |
| 1011 | CHECK(s2 >= s1); |
| 1012 | CHECK(!(s1 >= s2)); |
| 1013 | } |
| 1014 | |
| 1015 | { |
| 1016 | int arr1[] = {1, 2}; |
| 1017 | int arr2[] = {1, 2}; |
| 1018 | multi_span<int> s1 = arr1; |
| 1019 | multi_span<int> s2 = arr2; |
| 1020 | |
| 1021 | CHECK(s1 == s2); |
| 1022 | CHECK(!(s1 != s2)); |
| 1023 | CHECK(!(s1 < s2)); |
| 1024 | CHECK(s1 <= s2); |
| 1025 | CHECK(!(s1 > s2)); |
| 1026 | CHECK(s1 >= s2); |
| 1027 | CHECK(s2 == s1); |
| 1028 | CHECK(!(s2 != s1)); |
| 1029 | CHECK(!(s2 < s1)); |
| 1030 | CHECK(s2 <= s1); |
| 1031 | CHECK(!(s2 > s1)); |
| 1032 | CHECK(s2 >= s1); |
| 1033 | } |
| 1034 | |
| 1035 | { |
| 1036 | int arr[] = {1, 2, 3}; |
| 1037 | |
| 1038 | multi_span<int> s1 = {&arr[0], 2}; // shorter |
| 1039 | multi_span<int> s2 = arr; // longer |
| 1040 | |
| 1041 | CHECK(s1 != s2); |
| 1042 | CHECK(s2 != s1); |
| 1043 | CHECK(!(s1 == s2)); |
| 1044 | CHECK(!(s2 == s1)); |
| 1045 | CHECK(s1 < s2); |
| 1046 | CHECK(!(s2 < s1)); |
| 1047 | CHECK(s1 <= s2); |
| 1048 | CHECK(!(s2 <= s1)); |
| 1049 | CHECK(s2 > s1); |
| 1050 | CHECK(!(s1 > s2)); |
| 1051 | CHECK(s2 >= s1); |
| 1052 | CHECK(!(s1 >= s2)); |
| 1053 | } |
| 1054 | |
| 1055 | { |
| 1056 | int arr1[] = {1, 2}; // smaller |
| 1057 | int arr2[] = {2, 1}; // bigger |
| 1058 | |
| 1059 | multi_span<int> s1 = arr1; |
| 1060 | multi_span<int> s2 = arr2; |
| 1061 | |
| 1062 | CHECK(s1 != s2); |
| 1063 | CHECK(s2 != s1); |
| 1064 | CHECK(!(s1 == s2)); |
| 1065 | CHECK(!(s2 == s1)); |
| 1066 | CHECK(s1 < s2); |
| 1067 | CHECK(!(s2 < s1)); |
| 1068 | CHECK(s1 <= s2); |
| 1069 | CHECK(!(s2 <= s1)); |
| 1070 | CHECK(s2 > s1); |
| 1071 | CHECK(!(s1 > s2)); |
| 1072 | CHECK(s2 >= s1); |
| 1073 | CHECK(!(s1 >= s2)); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | TEST_CASE("basics") |
| 1078 | { |
| 1079 | auto ptr = as_multi_span(new int[10], 10); |
| 1080 | fill(ptr.begin(), ptr.end(), 99); |
| 1081 | for (int num : ptr) { |
| 1082 | CHECK(num == 99); |
| 1083 | } |
| 1084 | |
| 1085 | delete[] ptr.data(); |
| 1086 | } |
| 1087 | |
| 1088 | TEST_CASE("bounds_checks") |
| 1089 | { |
| 1090 | int arr[10][2]; |
| 1091 | auto av = as_multi_span(arr); |
| 1092 | |
| 1093 | fill(begin(av), end(av), 0); |
| 1094 | |
| 1095 | av[2][0] = 1; |
| 1096 | av[1][1] = 3; |
| 1097 | |
| 1098 | // out of bounds |
| 1099 | CHECK_THROWS_AS(av[1][3] = 3, fail_fast); |
| 1100 | CHECK_THROWS_AS((av[{1, 3}] = 3), fail_fast); |
| 1101 | |
| 1102 | CHECK_THROWS_AS(av[10][2], fail_fast); |
| 1103 | CHECK_THROWS_AS((av[{10, 2}]), fail_fast); |
| 1104 | |
| 1105 | CHECK_THROWS_AS(av[-1][0], fail_fast); |
| 1106 | CHECK_THROWS_AS((av[{-1, 0}]), fail_fast); |
| 1107 | |
| 1108 | CHECK_THROWS_AS(av[0][-1], fail_fast); |
| 1109 | CHECK_THROWS_AS((av[{0, -1}]), fail_fast); |
| 1110 | } |
| 1111 | |
| 1112 | void overloaded_func(multi_span<const int, dynamic_range, 3, 5> exp, int expected_value) |
| 1113 | { |
| 1114 | for (auto val : exp) { |
| 1115 | CHECK(val == expected_value); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | void overloaded_func(multi_span<const char, dynamic_range, 3, 5> exp, char expected_value) |
| 1120 | { |
| 1121 | for (auto val : exp) { |
| 1122 | CHECK(val == expected_value); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | void fixed_func(multi_span<int, 3, 3, 5> exp, int expected_value) |
| 1127 | { |
| 1128 | for (auto val : exp) { |
| 1129 | CHECK(val == expected_value); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | TEST_CASE("span_parameter_test") |
| 1134 | { |
| 1135 | auto data = new int[4][3][5]; |
| 1136 | |
| 1137 | auto av = as_multi_span(data, 4); |
| 1138 | |
| 1139 | CHECK(av.size() == 60); |
| 1140 | |
| 1141 | fill(av.begin(), av.end(), 34); |
| 1142 | |
| 1143 | int count = 0; |
| 1144 | for_each(av.rbegin(), av.rend(), [&](int val) { count += val; }); |
| 1145 | CHECK(count == 34 * 60); |
| 1146 | overloaded_func(av, 34); |
| 1147 | |
| 1148 | overloaded_func(as_multi_span(av, dim(4), dim(3), dim(5)), 34); |
| 1149 | |
| 1150 | // fixed_func(av, 34); |
| 1151 | delete[] data; |
| 1152 | } |
| 1153 | |
| 1154 | TEST_CASE("md_access") |
| 1155 | { |
| 1156 | auto width = 5, height = 20; |
| 1157 | |
| 1158 | auto imgSize = width * height; |
| 1159 | auto image_ptr = new int[static_cast<std::size_t>(imgSize)][3]; |
| 1160 | |
| 1161 | // size check will be done |
| 1162 | auto image_view = |
| 1163 | as_multi_span(as_multi_span(image_ptr, imgSize), dim(height), dim(width), dim<3>()); |
| 1164 | |
| 1165 | iota(image_view.begin(), image_view.end(), 1); |
| 1166 | |
| 1167 | int expected = 0; |
| 1168 | for (auto i = 0; i < height; i++) { |
| 1169 | for (auto j = 0; j < width; j++) { |
| 1170 | CHECK(expected + 1 == image_view[i][j][0]); |
| 1171 | CHECK(expected + 2 == image_view[i][j][1]); |
| 1172 | CHECK(expected + 3 == image_view[i][j][2]); |
| 1173 | |
| 1174 | auto val = image_view[{i, j, 0}]; |
| 1175 | CHECK(expected + 1 == val); |
| 1176 | val = image_view[{i, j, 1}]; |
| 1177 | CHECK(expected + 2 == val); |
| 1178 | val = image_view[{i, j, 2}]; |
| 1179 | CHECK(expected + 3 == val); |
| 1180 | |
| 1181 | expected += 3; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | TEST_CASE("as_multi_span") |
| 1187 | { |
| 1188 | { |
| 1189 | int* arr = new int[150]; |
| 1190 | |
| 1191 | auto av = as_multi_span(arr, dim<10>(), dim(3), dim<5>()); |
| 1192 | |
| 1193 | fill(av.begin(), av.end(), 24); |
| 1194 | overloaded_func(av, 24); |
| 1195 | |
| 1196 | delete[] arr; |
| 1197 | |
| 1198 | array<int, 15> stdarr{0}; |
| 1199 | auto av2 = as_multi_span(stdarr); |
| 1200 | overloaded_func(as_multi_span(av2, dim(1), dim<3>(), dim<5>()), 0); |
| 1201 | |
| 1202 | string str = "ttttttttttttttt"; // size = 15 |
| 1203 | auto t = str.data(); |
| 1204 | (void) t; |
| 1205 | auto av3 = as_multi_span(str); |
| 1206 | overloaded_func(as_multi_span(av3, dim(1), dim<3>(), dim<5>()), 't'); |
| 1207 | } |
| 1208 | |
| 1209 | { |
| 1210 | string str; |
| 1211 | multi_span<char> strspan = as_multi_span(str); |
| 1212 | (void) strspan; |
| 1213 | const string cstr; |
| 1214 | multi_span<const char> cstrspan = as_multi_span(cstr); |
| 1215 | (void) cstrspan; |
| 1216 | } |
| 1217 | |
| 1218 | { |
| 1219 | int a[3][4][5]; |
| 1220 | auto av = as_multi_span(a); |
| 1221 | const int(*b)[4][5]; |
| 1222 | b = a; |
| 1223 | auto bv = as_multi_span(b, 3); |
| 1224 | |
| 1225 | CHECK(av == bv); |
| 1226 | |
| 1227 | const std::array<double, 3> arr = {0.0, 0.0, 0.0}; |
| 1228 | auto cv = as_multi_span(arr); |
| 1229 | (void) cv; |
| 1230 | |
| 1231 | vector<float> vec(3); |
| 1232 | auto dv = as_multi_span(vec); |
| 1233 | (void) dv; |
| 1234 | |
| 1235 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 1236 | auto dv2 = as_multi_span(std::move(vec)); |
| 1237 | #endif |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | TEST_CASE("empty_spans") |
| 1242 | { |
| 1243 | { |
| 1244 | multi_span<int, 0> empty_av(nullptr); |
| 1245 | |
| 1246 | CHECK(empty_av.bounds().index_bounds() == multi_span_index<1>{0}); |
| 1247 | CHECK_THROWS_AS(empty_av[0], fail_fast); |
| 1248 | CHECK_THROWS_AS(empty_av.begin()[0], fail_fast); |
| 1249 | CHECK_THROWS_AS(empty_av.cbegin()[0], fail_fast); |
| 1250 | for (auto& v : empty_av) { |
| 1251 | (void) v; |
| 1252 | CHECK(false); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | { |
| 1257 | multi_span<int> empty_av = {}; |
| 1258 | CHECK(empty_av.bounds().index_bounds() == multi_span_index<1>{0}); |
| 1259 | CHECK_THROWS_AS(empty_av[0], fail_fast); |
| 1260 | CHECK_THROWS_AS(empty_av.begin()[0], fail_fast); |
| 1261 | CHECK_THROWS_AS(empty_av.cbegin()[0], fail_fast); |
| 1262 | for (auto& v : empty_av) { |
| 1263 | (void) v; |
| 1264 | CHECK(false); |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | TEST_CASE("index_constructor") |
| 1270 | { |
| 1271 | auto arr = new int[8]; |
| 1272 | for (int i = 0; i < 4; ++i) { |
| 1273 | arr[2 * i] = 4 + i; |
| 1274 | arr[2 * i + 1] = i; |
| 1275 | } |
| 1276 | |
| 1277 | multi_span<int, dynamic_range> av(arr, 8); |
| 1278 | |
| 1279 | ptrdiff_t a[1] = {0}; |
| 1280 | multi_span_index<1> i = a; |
| 1281 | |
| 1282 | CHECK(av[i] == 4); |
| 1283 | |
| 1284 | auto av2 = as_multi_span(av, dim<4>(), dim(2)); |
| 1285 | ptrdiff_t a2[2] = {0, 1}; |
| 1286 | multi_span_index<2> i2 = a2; |
| 1287 | |
| 1288 | CHECK(av2[i2] == 0); |
| 1289 | CHECK(av2[0][i] == 4); |
| 1290 | |
| 1291 | delete[] arr; |
| 1292 | } |
| 1293 | |
| 1294 | TEST_CASE("index_constructors") |
| 1295 | { |
| 1296 | { |
| 1297 | // components of the same type |
| 1298 | multi_span_index<3> i1(0, 1, 2); |
| 1299 | CHECK(i1[0] == 0); |
| 1300 | |
| 1301 | // components of different types |
| 1302 | std::size_t c0 = 0; |
| 1303 | std::size_t c1 = 1; |
| 1304 | multi_span_index<3> i2(c0, c1, 2); |
| 1305 | CHECK(i2[0] == 0); |
| 1306 | |
| 1307 | // from array |
| 1308 | multi_span_index<3> i3 = {0, 1, 2}; |
| 1309 | CHECK(i3[0] == 0); |
| 1310 | |
| 1311 | // from other index of the same size type |
| 1312 | multi_span_index<3> i4 = i3; |
| 1313 | CHECK(i4[0] == 0); |
| 1314 | |
| 1315 | // default |
| 1316 | multi_span_index<3> i7; |
| 1317 | CHECK(i7[0] == 0); |
| 1318 | |
| 1319 | // default |
| 1320 | multi_span_index<3> i9 = {}; |
| 1321 | CHECK(i9[0] == 0); |
| 1322 | } |
| 1323 | |
| 1324 | { |
| 1325 | // components of the same type |
| 1326 | multi_span_index<1> i1(0); |
| 1327 | CHECK(i1[0] == 0); |
| 1328 | |
| 1329 | // components of different types |
| 1330 | std::size_t c0 = 0; |
| 1331 | multi_span_index<1> i2(c0); |
| 1332 | CHECK(i2[0] == 0); |
| 1333 | |
| 1334 | // from array |
| 1335 | multi_span_index<1> i3 = {0}; |
| 1336 | CHECK(i3[0] == 0); |
| 1337 | |
| 1338 | // from int |
| 1339 | multi_span_index<1> i4 = 0; |
| 1340 | CHECK(i4[0] == 0); |
| 1341 | |
| 1342 | // from other index of the same size type |
| 1343 | multi_span_index<1> i5 = i3; |
| 1344 | CHECK(i5[0] == 0); |
| 1345 | |
| 1346 | // default |
| 1347 | multi_span_index<1> i8; |
| 1348 | CHECK(i8[0] == 0); |
| 1349 | |
| 1350 | // default |
| 1351 | multi_span_index<1> i9 = {}; |
| 1352 | CHECK(i9[0] == 0); |
| 1353 | } |
| 1354 | |
| 1355 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 1356 | { |
| 1357 | multi_span_index<3> i1(0, 1); |
| 1358 | multi_span_index<3> i2(0, 1, 2, 3); |
| 1359 | multi_span_index<3> i3 = {0}; |
| 1360 | multi_span_index<3> i4 = {0, 1, 2, 3}; |
| 1361 | multi_span_index<1> i5 = {0, 1}; |
| 1362 | } |
| 1363 | #endif |
| 1364 | } |
| 1365 | |
| 1366 | TEST_CASE("index_operations") |
| 1367 | { |
| 1368 | ptrdiff_t a[3] = {0, 1, 2}; |
| 1369 | ptrdiff_t b[3] = {3, 4, 5}; |
| 1370 | multi_span_index<3> i = a; |
| 1371 | multi_span_index<3> j = b; |
| 1372 | |
| 1373 | CHECK(i[0] == 0); |
| 1374 | CHECK(i[1] == 1); |
| 1375 | CHECK(i[2] == 2); |
| 1376 | |
| 1377 | { |
| 1378 | multi_span_index<3> k = i + j; |
| 1379 | |
| 1380 | CHECK(i[0] == 0); |
| 1381 | CHECK(i[1] == 1); |
| 1382 | CHECK(i[2] == 2); |
| 1383 | CHECK(k[0] == 3); |
| 1384 | CHECK(k[1] == 5); |
| 1385 | CHECK(k[2] == 7); |
| 1386 | } |
| 1387 | |
| 1388 | { |
| 1389 | multi_span_index<3> k = i * 3; |
| 1390 | |
| 1391 | CHECK(i[0] == 0); |
| 1392 | CHECK(i[1] == 1); |
| 1393 | CHECK(i[2] == 2); |
| 1394 | CHECK(k[0] == 0); |
| 1395 | CHECK(k[1] == 3); |
| 1396 | CHECK(k[2] == 6); |
| 1397 | } |
| 1398 | |
| 1399 | { |
| 1400 | multi_span_index<3> k = 3 * i; |
| 1401 | |
| 1402 | CHECK(i[0] == 0); |
| 1403 | CHECK(i[1] == 1); |
| 1404 | CHECK(i[2] == 2); |
| 1405 | CHECK(k[0] == 0); |
| 1406 | CHECK(k[1] == 3); |
| 1407 | CHECK(k[2] == 6); |
| 1408 | } |
| 1409 | |
| 1410 | { |
| 1411 | multi_span_index<2> k = details::shift_left(i); |
| 1412 | |
| 1413 | CHECK(i[0] == 0); |
| 1414 | CHECK(i[1] == 1); |
| 1415 | CHECK(i[2] == 2); |
| 1416 | CHECK(k[0] == 1); |
| 1417 | CHECK(k[1] == 2); |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | void iterate_second_column(multi_span<int, dynamic_range, dynamic_range> av) |
| 1422 | { |
| 1423 | auto length = av.size() / 2; |
| 1424 | |
| 1425 | // view to the second column |
| 1426 | auto section = av.section({0, 1}, {length, 1}); |
| 1427 | |
| 1428 | CHECK(section.size() == length); |
| 1429 | for (auto i = 0; i < section.size(); ++i) { |
| 1430 | CHECK(section[i][0] == av[i][1]); |
| 1431 | } |
| 1432 | |
| 1433 | for (auto i = 0; i < section.size(); ++i) { |
| 1434 | auto idx = multi_span_index<2>{i, 0}; // avoid braces inside the CHECK macro |
| 1435 | CHECK(section[idx] == av[i][1]); |
| 1436 | } |
| 1437 | |
| 1438 | CHECK(section.bounds().index_bounds()[0] == length); |
| 1439 | CHECK(section.bounds().index_bounds()[1] == 1); |
| 1440 | for (auto i = 0; i < section.bounds().index_bounds()[0]; ++i) { |
| 1441 | for (auto j = 0; j < section.bounds().index_bounds()[1]; ++j) { |
| 1442 | auto idx = multi_span_index<2>{i, j}; // avoid braces inside the CHECK macro |
| 1443 | CHECK(section[idx] == av[i][1]); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | auto check_sum = 0; |
| 1448 | for (auto i = 0; i < length; ++i) { |
| 1449 | check_sum += av[i][1]; |
| 1450 | } |
| 1451 | |
| 1452 | { |
| 1453 | auto idx = 0; |
| 1454 | auto sum = 0; |
| 1455 | for (auto num : section) { |
| 1456 | CHECK(num == av[idx][1]); |
| 1457 | sum += num; |
| 1458 | idx++; |
| 1459 | } |
| 1460 | |
| 1461 | CHECK(sum == check_sum); |
| 1462 | } |
| 1463 | { |
| 1464 | auto idx = length - 1; |
| 1465 | auto sum = 0; |
| 1466 | for (auto iter = section.rbegin(); iter != section.rend(); ++iter) { |
| 1467 | CHECK(*iter == av[idx][1]); |
| 1468 | sum += *iter; |
| 1469 | idx--; |
| 1470 | } |
| 1471 | |
| 1472 | CHECK(sum == check_sum); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | TEST_CASE("span_section_iteration") |
| 1477 | { |
| 1478 | int arr[4][2] = {{4, 0}, {5, 1}, {6, 2}, {7, 3}}; |
| 1479 | |
| 1480 | // static bounds |
| 1481 | { |
| 1482 | multi_span<int, 4, 2> av = arr; |
| 1483 | iterate_second_column(av); |
| 1484 | } |
| 1485 | // first bound is dynamic |
| 1486 | { |
| 1487 | multi_span<int, dynamic_range, 2> av = arr; |
| 1488 | iterate_second_column(av); |
| 1489 | } |
| 1490 | // second bound is dynamic |
| 1491 | { |
| 1492 | multi_span<int, 4, dynamic_range> av = arr; |
| 1493 | iterate_second_column(av); |
| 1494 | } |
| 1495 | // both bounds are dynamic |
| 1496 | { |
| 1497 | multi_span<int, dynamic_range, dynamic_range> av = arr; |
| 1498 | iterate_second_column(av); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | TEST_CASE("dynamic_span_section_iteration") |
| 1503 | { |
| 1504 | auto height = 4, width = 2; |
| 1505 | auto size = height * width; |
| 1506 | |
| 1507 | auto arr = new int[static_cast<std::size_t>(size)]; |
| 1508 | for (auto i = 0; i < size; ++i) { |
| 1509 | arr[i] = i; |
| 1510 | } |
| 1511 | |
| 1512 | auto av = as_multi_span(arr, size); |
| 1513 | |
| 1514 | // first bound is dynamic |
| 1515 | { |
| 1516 | multi_span<int, dynamic_range, 2> av2 = as_multi_span(av, dim(height), dim(width)); |
| 1517 | iterate_second_column(av2); |
| 1518 | } |
| 1519 | // second bound is dynamic |
| 1520 | { |
| 1521 | multi_span<int, 4, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width)); |
| 1522 | iterate_second_column(av2); |
| 1523 | } |
| 1524 | // both bounds are dynamic |
| 1525 | { |
| 1526 | multi_span<int, dynamic_range, dynamic_range> av2 = |
| 1527 | as_multi_span(av, dim(height), dim(width)); |
| 1528 | iterate_second_column(av2); |
| 1529 | } |
| 1530 | |
| 1531 | delete[] arr; |
| 1532 | } |
| 1533 | |
| 1534 | TEST_CASE("span_structure_size") |
| 1535 | { |
| 1536 | double(*arr)[3][4] = new double[100][3][4]; |
| 1537 | multi_span<double, dynamic_range, 3, 4> av1(arr, 10); |
| 1538 | |
| 1539 | struct EffectiveStructure |
| 1540 | { |
| 1541 | double* v1; |
| 1542 | ptrdiff_t v2; |
| 1543 | }; |
| 1544 | CHECK(sizeof(av1) == sizeof(EffectiveStructure)); |
| 1545 | |
| 1546 | CHECK_THROWS_AS(av1[10][3][4], fail_fast); |
| 1547 | |
| 1548 | multi_span<const double, dynamic_range, 6, 4> av2 = |
| 1549 | as_multi_span(av1, dim(5), dim<6>(), dim<4>()); |
| 1550 | (void) av2; |
| 1551 | } |
| 1552 | |
| 1553 | TEST_CASE("fixed_size_conversions") |
| 1554 | { |
| 1555 | int arr[] = {1, 2, 3, 4}; |
| 1556 | |
| 1557 | // converting to an multi_span from an equal size array is ok |
| 1558 | multi_span<int, 4> av4 = arr; |
| 1559 | CHECK(av4.length() == 4); |
| 1560 | |
| 1561 | // converting to dynamic_range a_v is always ok |
| 1562 | { |
| 1563 | multi_span<int, dynamic_range> av = av4; |
| 1564 | (void) av; |
| 1565 | } |
| 1566 | { |
| 1567 | multi_span<int, dynamic_range> av = arr; |
| 1568 | (void) av; |
| 1569 | } |
| 1570 | |
| 1571 | // initialization or assignment to static multi_span that REDUCES size is NOT ok |
| 1572 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 1573 | { |
| 1574 | multi_span<int, 2> av2 = arr; |
| 1575 | } |
| 1576 | { |
| 1577 | multi_span<int, 2> av2 = av4; |
| 1578 | } |
| 1579 | #endif |
| 1580 | |
| 1581 | { |
| 1582 | multi_span<int, dynamic_range> av = arr; |
| 1583 | multi_span<int, 2> av2 = av; |
| 1584 | (void) av2; |
| 1585 | } |
| 1586 | |
| 1587 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 1588 | { |
| 1589 | multi_span<int, dynamic_range> av = arr; |
| 1590 | multi_span<int, 2, 1> av2 = av.as_multi_span(dim<2>(), dim<2>()); |
| 1591 | } |
| 1592 | #endif |
| 1593 | |
| 1594 | { |
| 1595 | multi_span<int, dynamic_range> av = arr; |
| 1596 | multi_span<int, 2, 1> av2 = as_multi_span(av, dim(2), dim(2)); |
| 1597 | auto workaround_macro = [&]() { return av2[{1, 0}] == 2; }; |
| 1598 | CHECK(workaround_macro()); |
| 1599 | } |
| 1600 | |
| 1601 | // but doing so explicitly is ok |
| 1602 | |
| 1603 | // you can convert statically |
| 1604 | { |
| 1605 | multi_span<int, 2> av2 = {arr, 2}; |
| 1606 | (void) av2; |
| 1607 | } |
| 1608 | { |
| 1609 | multi_span<int, 1> av2 = av4.first<1>(); |
| 1610 | (void) av2; |
| 1611 | } |
| 1612 | |
| 1613 | // ...or dynamically |
| 1614 | { |
| 1615 | // NB: implicit conversion to multi_span<int,2> from multi_span<int,dynamic_range> |
| 1616 | multi_span<int, 1> av2 = av4.first(1); |
| 1617 | (void) av2; |
| 1618 | } |
| 1619 | |
| 1620 | // initialization or assignment to static multi_span that requires size INCREASE is not ok. |
| 1621 | int arr2[2] = {1, 2}; |
| 1622 | |
| 1623 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 1624 | { |
| 1625 | multi_span<int, 4> av4 = arr2; |
| 1626 | } |
| 1627 | { |
| 1628 | multi_span<int, 2> av2 = arr2; |
| 1629 | multi_span<int, 4> av4 = av2; |
| 1630 | } |
| 1631 | #endif |
| 1632 | { |
| 1633 | auto f = [&]() { |
| 1634 | multi_span<int, 4> av9 = {arr2, 2}; |
| 1635 | (void) av9; |
| 1636 | }; |
| 1637 | CHECK_THROWS_AS(f(), fail_fast); |
| 1638 | } |
| 1639 | |
| 1640 | // this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one |
| 1641 | multi_span<int, dynamic_range> av = arr2; |
| 1642 | auto f = [&]() { |
| 1643 | multi_span<int, 4> av2 = av; |
| 1644 | (void) av2; |
| 1645 | }; |
| 1646 | CHECK_THROWS_AS(f(), fail_fast); |
| 1647 | } |
| 1648 | |
| 1649 | TEST_CASE("as_writeable_bytes") |
| 1650 | { |
| 1651 | int a[] = {1, 2, 3, 4}; |
| 1652 | |
| 1653 | { |
| 1654 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 1655 | // you should not be able to get writeable bytes for const objects |
| 1656 | multi_span<const int, dynamic_range> av = a; |
| 1657 | auto wav = av.as_writeable_bytes(); |
| 1658 | #endif |
| 1659 | } |
| 1660 | |
| 1661 | { |
| 1662 | multi_span<int, dynamic_range> av; |
| 1663 | auto wav = as_writeable_bytes(av); |
| 1664 | CHECK(wav.length() == av.length()); |
| 1665 | CHECK(wav.length() == 0); |
| 1666 | CHECK(wav.size_bytes() == 0); |
| 1667 | } |
| 1668 | |
| 1669 | { |
| 1670 | multi_span<int, dynamic_range> av = a; |
| 1671 | auto wav = as_writeable_bytes(av); |
| 1672 | CHECK(wav.data() == reinterpret_cast<byte*>(&a[0])); |
| 1673 | CHECK(static_cast<std::size_t>(wav.length()) == sizeof(a)); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | TEST_CASE("iterator") |
| 1678 | { |
| 1679 | int a[] = {1, 2, 3, 4}; |
| 1680 | |
| 1681 | { |
| 1682 | multi_span<int, dynamic_range> av = a; |
| 1683 | auto wav = as_writeable_bytes(av); |
| 1684 | for (auto& b : wav) { |
| 1685 | b = byte(0); |
| 1686 | } |
| 1687 | for (std::size_t i = 0; i < 4; ++i) { |
| 1688 | CHECK(a[i] == 0); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | { |
| 1693 | multi_span<int, dynamic_range> av = a; |
| 1694 | for (auto& n : av) { |
| 1695 | n = 1; |
| 1696 | } |
| 1697 | for (std::size_t i = 0; i < 4; ++i) { |
| 1698 | CHECK(a[i] == 1); |
| 1699 | } |
| 1700 | } |
| 1701 | } |