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 strided_span, index, multi_span, strided_... |
| 22 | |
| 23 | #include <iostream> // for size_t |
| 24 | #include <iterator> // for begin, end |
| 25 | #include <numeric> // for iota |
| 26 | #include <type_traits> // for integral_constant<>::value, is_convertible |
| 27 | #include <vector> // for vector |
| 28 | |
| 29 | namespace gsl { |
| 30 | struct fail_fast; |
| 31 | } // namespace gsl |
| 32 | |
| 33 | using namespace std; |
| 34 | using namespace gsl; |
| 35 | |
| 36 | namespace |
| 37 | { |
| 38 | struct BaseClass |
| 39 | { |
| 40 | }; |
| 41 | struct DerivedClass : BaseClass |
| 42 | { |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | TEST_CASE("span_section_test") |
| 47 | { |
| 48 | int a[30][4][5]; |
| 49 | |
| 50 | const auto av = as_multi_span(a); |
| 51 | const auto sub = av.section({15, 0, 0}, gsl::multi_span_index<3>{2, 2, 2}); |
| 52 | const auto subsub = sub.section({1, 0, 0}, gsl::multi_span_index<3>{1, 1, 1}); |
| 53 | (void) subsub; |
| 54 | } |
| 55 | |
| 56 | TEST_CASE("span_section") |
| 57 | { |
| 58 | std::vector<int> data(5 * 10); |
| 59 | std::iota(begin(data), end(data), 0); |
| 60 | const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>()); |
| 61 | |
| 62 | const strided_span<int, 2> av_section_1 = av.section({1, 2}, {3, 4}); |
| 63 | CHECK((av_section_1[{0, 0}] == 12)); |
| 64 | CHECK((av_section_1[{0, 1}] == 13)); |
| 65 | CHECK((av_section_1[{1, 0}] == 22)); |
| 66 | CHECK((av_section_1[{2, 3}] == 35)); |
| 67 | |
| 68 | const strided_span<int, 2> av_section_2 = av_section_1.section({1, 2}, {2, 2}); |
| 69 | CHECK((av_section_2[{0, 0}] == 24)); |
| 70 | CHECK((av_section_2[{0, 1}] == 25)); |
| 71 | CHECK((av_section_2[{1, 0}] == 34)); |
| 72 | } |
| 73 | |
| 74 | TEST_CASE("strided_span_constructors") |
| 75 | { |
| 76 | // Check stride constructor |
| 77 | { |
| 78 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 79 | const int carr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 80 | |
| 81 | strided_span<int, 1> sav1{arr, {{9}, {1}}}; // T -> T |
| 82 | CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{9}); |
| 83 | CHECK(sav1.bounds().stride() == 1); |
| 84 | CHECK((sav1[0] == 1 && sav1[8] == 9)); |
| 85 | |
| 86 | strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T |
| 87 | CHECK(sav2.bounds().index_bounds() == multi_span_index<1>{4}); |
| 88 | CHECK(sav2.bounds().strides() == multi_span_index<1>{2}); |
| 89 | CHECK((sav2[0] == 1 && sav2[3] == 7)); |
| 90 | |
| 91 | strided_span<int, 2> sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T |
| 92 | CHECK((sav3.bounds().index_bounds() == multi_span_index<2>{2, 2})); |
| 93 | CHECK((sav3.bounds().strides() == multi_span_index<2>{6, 2})); |
| 94 | CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7)); |
| 95 | } |
| 96 | |
| 97 | // Check multi_span constructor |
| 98 | { |
| 99 | int arr[] = {1, 2}; |
| 100 | |
| 101 | // From non-cv-qualified source |
| 102 | { |
| 103 | const multi_span<int> src = arr; |
| 104 | |
| 105 | strided_span<int, 1> sav{src, {2, 1}}; |
| 106 | CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); |
| 107 | CHECK(sav.bounds().strides() == multi_span_index<1>{1}); |
| 108 | CHECK(sav[1] == 2); |
| 109 | |
| 110 | #if _MSC_VER > 1800 |
| 111 | // strided_span<const int, 1> sav_c{ {src}, {2, 1} }; |
| 112 | strided_span<const int, 1> sav_c{multi_span<const int>{src}, |
| 113 | strided_bounds<1>{2, 1}}; |
| 114 | #else |
| 115 | strided_span<const int, 1> sav_c{multi_span<const int>{src}, |
| 116 | strided_bounds<1>{2, 1}}; |
| 117 | #endif |
| 118 | CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2}); |
| 119 | CHECK(sav_c.bounds().strides() == multi_span_index<1>{1}); |
| 120 | CHECK(sav_c[1] == 2); |
| 121 | |
| 122 | #if _MSC_VER > 1800 |
| 123 | strided_span<volatile int, 1> sav_v{src, {2, 1}}; |
| 124 | #else |
| 125 | strided_span<volatile int, 1> sav_v{multi_span<volatile int>{src}, |
| 126 | strided_bounds<1>{2, 1}}; |
| 127 | #endif |
| 128 | CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2}); |
| 129 | CHECK(sav_v.bounds().strides() == multi_span_index<1>{1}); |
| 130 | CHECK(sav_v[1] == 2); |
| 131 | |
| 132 | #if _MSC_VER > 1800 |
| 133 | strided_span<const volatile int, 1> sav_cv{src, {2, 1}}; |
| 134 | #else |
| 135 | strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src}, |
| 136 | strided_bounds<1>{2, 1}}; |
| 137 | #endif |
| 138 | CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); |
| 139 | CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); |
| 140 | CHECK(sav_cv[1] == 2); |
| 141 | } |
| 142 | |
| 143 | // From const-qualified source |
| 144 | { |
| 145 | const multi_span<const int> src{arr}; |
| 146 | |
| 147 | strided_span<const int, 1> sav_c{src, {2, 1}}; |
| 148 | CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2}); |
| 149 | CHECK(sav_c.bounds().strides() == multi_span_index<1>{1}); |
| 150 | CHECK(sav_c[1] == 2); |
| 151 | |
| 152 | #if _MSC_VER > 1800 |
| 153 | strided_span<const volatile int, 1> sav_cv{src, {2, 1}}; |
| 154 | #else |
| 155 | strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src}, |
| 156 | strided_bounds<1>{2, 1}}; |
| 157 | #endif |
| 158 | |
| 159 | CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); |
| 160 | CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); |
| 161 | CHECK(sav_cv[1] == 2); |
| 162 | } |
| 163 | |
| 164 | // From volatile-qualified source |
| 165 | { |
| 166 | const multi_span<volatile int> src{arr}; |
| 167 | |
| 168 | strided_span<volatile int, 1> sav_v{src, {2, 1}}; |
| 169 | CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2}); |
| 170 | CHECK(sav_v.bounds().strides() == multi_span_index<1>{1}); |
| 171 | CHECK(sav_v[1] == 2); |
| 172 | |
| 173 | #if _MSC_VER > 1800 |
| 174 | strided_span<const volatile int, 1> sav_cv{src, {2, 1}}; |
| 175 | #else |
| 176 | strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src}, |
| 177 | strided_bounds<1>{2, 1}}; |
| 178 | #endif |
| 179 | CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); |
| 180 | CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); |
| 181 | CHECK(sav_cv[1] == 2); |
| 182 | } |
| 183 | |
| 184 | // From cv-qualified source |
| 185 | { |
| 186 | const multi_span<const volatile int> src{arr}; |
| 187 | |
| 188 | strided_span<const volatile int, 1> sav_cv{src, {2, 1}}; |
| 189 | CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); |
| 190 | CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); |
| 191 | CHECK(sav_cv[1] == 2); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Check const-casting constructor |
| 196 | { |
| 197 | int arr[2] = {4, 5}; |
| 198 | |
| 199 | const multi_span<int, 2> av(arr, 2); |
| 200 | multi_span<const int, 2> av2{av}; |
| 201 | CHECK(av2[1] == 5); |
| 202 | |
| 203 | static_assert( |
| 204 | std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value, |
| 205 | "ctor is not implicit!"); |
| 206 | |
| 207 | const strided_span<int, 1> src{arr, {2, 1}}; |
| 208 | strided_span<const int, 1> sav{src}; |
| 209 | CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); |
| 210 | CHECK(sav.bounds().stride() == 1); |
| 211 | CHECK(sav[1] == 5); |
| 212 | |
| 213 | static_assert( |
| 214 | std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value, |
| 215 | "ctor is not implicit!"); |
| 216 | } |
| 217 | |
| 218 | // Check copy constructor |
| 219 | { |
| 220 | int arr1[2] = {3, 4}; |
| 221 | const strided_span<int, 1> src1{arr1, {2, 1}}; |
| 222 | strided_span<int, 1> sav1{src1}; |
| 223 | |
| 224 | CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2}); |
| 225 | CHECK(sav1.bounds().stride() == 1); |
| 226 | CHECK(sav1[0] == 3); |
| 227 | |
| 228 | int arr2[6] = {1, 2, 3, 4, 5, 6}; |
| 229 | const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}}; |
| 230 | strided_span<const int, 2> sav2{src2}; |
| 231 | CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2})); |
| 232 | CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1})); |
| 233 | CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); |
| 234 | } |
| 235 | |
| 236 | // Check const-casting assignment operator |
| 237 | { |
| 238 | int arr1[2] = {1, 2}; |
| 239 | int arr2[6] = {3, 4, 5, 6, 7, 8}; |
| 240 | |
| 241 | const strided_span<int, 1> src{arr1, {{2}, {1}}}; |
| 242 | strided_span<const int, 1> sav{arr2, {{3}, {2}}}; |
| 243 | strided_span<const int, 1>& sav_ref = (sav = src); |
| 244 | CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); |
| 245 | CHECK(sav.bounds().strides() == multi_span_index<1>{1}); |
| 246 | CHECK(sav[0] == 1); |
| 247 | CHECK(&sav_ref == &sav); |
| 248 | } |
| 249 | |
| 250 | // Check copy assignment operator |
| 251 | { |
| 252 | int arr1[2] = {3, 4}; |
| 253 | int arr1b[1] = {0}; |
| 254 | const strided_span<int, 1> src1{arr1, {2, 1}}; |
| 255 | strided_span<int, 1> sav1{arr1b, {1, 1}}; |
| 256 | strided_span<int, 1>& sav1_ref = (sav1 = src1); |
| 257 | CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2}); |
| 258 | CHECK(sav1.bounds().strides() == multi_span_index<1>{1}); |
| 259 | CHECK(sav1[0] == 3); |
| 260 | CHECK(&sav1_ref == &sav1); |
| 261 | |
| 262 | const int arr2[6] = {1, 2, 3, 4, 5, 6}; |
| 263 | const int arr2b[1] = {0}; |
| 264 | const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}}; |
| 265 | strided_span<const int, 2> sav2{arr2b, {{1, 1}, {1, 1}}}; |
| 266 | strided_span<const int, 2>& sav2_ref = (sav2 = src2); |
| 267 | CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2})); |
| 268 | CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1})); |
| 269 | CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); |
| 270 | CHECK(&sav2_ref == &sav2); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | TEST_CASE("strided_span_slice") |
| 275 | { |
| 276 | std::vector<int> data(5 * 10); |
| 277 | std::iota(begin(data), end(data), 0); |
| 278 | const multi_span<int, 5, 10> src = |
| 279 | as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>()); |
| 280 | |
| 281 | const strided_span<int, 2> sav{src, {{5, 10}, {10, 1}}}; |
| 282 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 283 | const strided_span<const int, 2> csav{{src}, {{5, 10}, {10, 1}}}; |
| 284 | #endif |
| 285 | const strided_span<const int, 2> csav{multi_span<const int, 5, 10>{src}, |
| 286 | {{5, 10}, {10, 1}}}; |
| 287 | |
| 288 | strided_span<int, 1> sav_sl = sav[2]; |
| 289 | CHECK(sav_sl[0] == 20); |
| 290 | CHECK(sav_sl[9] == 29); |
| 291 | |
| 292 | strided_span<const int, 1> csav_sl = sav[3]; |
| 293 | CHECK(csav_sl[0] == 30); |
| 294 | CHECK(csav_sl[9] == 39); |
| 295 | |
| 296 | CHECK(sav[4][0] == 40); |
| 297 | CHECK(sav[4][9] == 49); |
| 298 | } |
| 299 | |
| 300 | TEST_CASE("strided_span_column_major") |
| 301 | { |
| 302 | // strided_span may be used to accommodate more peculiar |
| 303 | // use cases, such as column-major multidimensional array |
| 304 | // (aka. "FORTRAN" layout). |
| 305 | |
| 306 | int cm_array[3 * 5] = {1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15}; |
| 307 | strided_span<int, 2> cm_sav{cm_array, {{5, 3}, {1, 5}}}; |
| 308 | |
| 309 | // Accessing elements |
| 310 | CHECK((cm_sav[{0, 0}] == 1)); |
| 311 | CHECK((cm_sav[{0, 1}] == 2)); |
| 312 | CHECK((cm_sav[{1, 0}] == 4)); |
| 313 | CHECK((cm_sav[{4, 2}] == 15)); |
| 314 | |
| 315 | // Slice |
| 316 | strided_span<int, 1> cm_sl = cm_sav[3]; |
| 317 | |
| 318 | CHECK(cm_sl[0] == 10); |
| 319 | CHECK(cm_sl[1] == 11); |
| 320 | CHECK(cm_sl[2] == 12); |
| 321 | |
| 322 | // Section |
| 323 | strided_span<int, 2> cm_sec = cm_sav.section({2, 1}, {3, 2}); |
| 324 | |
| 325 | CHECK((cm_sec.bounds().index_bounds() == multi_span_index<2>{3, 2})); |
| 326 | CHECK((cm_sec[{0, 0}] == 8)); |
| 327 | CHECK((cm_sec[{0, 1}] == 9)); |
| 328 | CHECK((cm_sec[{1, 0}] == 11)); |
| 329 | CHECK((cm_sec[{2, 1}] == 15)); |
| 330 | } |
| 331 | |
| 332 | TEST_CASE("strided_span_bounds") |
| 333 | { |
| 334 | int arr[] = {0, 1, 2, 3}; |
| 335 | multi_span<int> av(arr); |
| 336 | |
| 337 | { |
| 338 | // incorrect sections |
| 339 | |
| 340 | CHECK_THROWS_AS(av.section(0, 0)[0], fail_fast); |
| 341 | CHECK_THROWS_AS(av.section(1, 0)[0], fail_fast); |
| 342 | CHECK_THROWS_AS(av.section(1, 1)[1], fail_fast); |
| 343 | |
| 344 | CHECK_THROWS_AS(av.section(2, 5), fail_fast); |
| 345 | CHECK_THROWS_AS(av.section(5, 2), fail_fast); |
| 346 | CHECK_THROWS_AS(av.section(5, 0), fail_fast); |
| 347 | CHECK_THROWS_AS(av.section(0, 5), fail_fast); |
| 348 | CHECK_THROWS_AS(av.section(5, 5), fail_fast); |
| 349 | } |
| 350 | |
| 351 | { |
| 352 | // zero stride |
| 353 | strided_span<int, 1> sav{av, {{4}, {}}}; |
| 354 | CHECK(sav[0] == 0); |
| 355 | CHECK(sav[3] == 0); |
| 356 | CHECK_THROWS_AS(sav[4], fail_fast); |
| 357 | } |
| 358 | |
| 359 | { |
| 360 | // zero extent |
| 361 | strided_span<int, 1> sav{av, {{}, {1}}}; |
| 362 | CHECK_THROWS_AS(sav[0], fail_fast); |
| 363 | } |
| 364 | |
| 365 | { |
| 366 | // zero extent and stride |
| 367 | strided_span<int, 1> sav{av, {{}, {}}}; |
| 368 | CHECK_THROWS_AS(sav[0], fail_fast); |
| 369 | } |
| 370 | |
| 371 | { |
| 372 | // strided array ctor with matching strided bounds |
| 373 | strided_span<int, 1> sav{arr, {4, 1}}; |
| 374 | CHECK(sav.bounds().index_bounds() == multi_span_index<1>{4}); |
| 375 | CHECK(sav[3] == 3); |
| 376 | CHECK_THROWS_AS(sav[4], fail_fast); |
| 377 | } |
| 378 | |
| 379 | { |
| 380 | // strided array ctor with smaller strided bounds |
| 381 | strided_span<int, 1> sav{arr, {2, 1}}; |
| 382 | CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); |
| 383 | CHECK(sav[1] == 1); |
| 384 | CHECK_THROWS_AS(sav[2], fail_fast); |
| 385 | } |
| 386 | |
| 387 | { |
| 388 | // strided array ctor with fitting irregular bounds |
| 389 | strided_span<int, 1> sav{arr, {2, 3}}; |
| 390 | CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); |
| 391 | CHECK(sav[0] == 0); |
| 392 | CHECK(sav[1] == 3); |
| 393 | CHECK_THROWS_AS(sav[2], fail_fast); |
| 394 | } |
| 395 | |
| 396 | { |
| 397 | // bounds cross data boundaries - from static arrays |
| 398 | CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 2}}), fail_fast); |
| 399 | CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 3}}), fail_fast); |
| 400 | CHECK_THROWS_AS((strided_span<int, 1>{arr, {4, 5}}), fail_fast); |
| 401 | CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 1}}), fail_fast); |
| 402 | CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 5}}), fail_fast); |
| 403 | } |
| 404 | |
| 405 | { |
| 406 | // bounds cross data boundaries - from array view |
| 407 | CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 2}}), fail_fast); |
| 408 | CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 3}}), fail_fast); |
| 409 | CHECK_THROWS_AS((strided_span<int, 1>{av, {4, 5}}), fail_fast); |
| 410 | CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 1}}), fail_fast); |
| 411 | CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 5}}), fail_fast); |
| 412 | } |
| 413 | |
| 414 | { |
| 415 | // bounds cross data boundaries - from dynamic arrays |
| 416 | CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 2}}), fail_fast); |
| 417 | CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 3}}), fail_fast); |
| 418 | CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {4, 5}}), fail_fast); |
| 419 | CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 1}}), fail_fast); |
| 420 | CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 5}}), fail_fast); |
| 421 | CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 2, {2, 2}}), fail_fast); |
| 422 | } |
| 423 | |
| 424 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 425 | { |
| 426 | strided_span<int, 1> sav0{av.data(), {3, 2}}; |
| 427 | strided_span<int, 1> sav1{arr, {1}}; |
| 428 | strided_span<int, 1> sav2{arr, {1, 1, 1}}; |
| 429 | strided_span<int, 1> sav3{av, {1}}; |
| 430 | strided_span<int, 1> sav4{av, {1, 1, 1}}; |
| 431 | strided_span<int, 2> sav5{av.as_multi_span(dim<2>(), dim<2>()), {1}}; |
| 432 | strided_span<int, 2> sav6{av.as_multi_span(dim<2>(), dim<2>()), {1, 1, 1}}; |
| 433 | strided_span<int, 2> sav7{av.as_multi_span(dim<2>(), dim<2>()), |
| 434 | {{1, 1}, {1, 1}, {1, 1}}}; |
| 435 | |
| 436 | multi_span_index<1> index{0, 1}; |
| 437 | strided_span<int, 1> sav8{arr, {1, {1, 1}}}; |
| 438 | strided_span<int, 1> sav9{arr, {{1, 1}, {1, 1}}}; |
| 439 | strided_span<int, 1> sav10{av, {1, {1, 1}}}; |
| 440 | strided_span<int, 1> sav11{av, {{1, 1}, {1, 1}}}; |
| 441 | strided_span<int, 2> sav12{av.as_multi_span(dim<2>(), dim<2>()), {{1}, {1}}}; |
| 442 | strided_span<int, 2> sav13{av.as_multi_span(dim<2>(), dim<2>()), {{1}, {1, 1, 1}}}; |
| 443 | strided_span<int, 2> sav14{av.as_multi_span(dim<2>(), dim<2>()), {{1, 1, 1}, {1}}}; |
| 444 | } |
| 445 | #endif |
| 446 | } |
| 447 | |
| 448 | TEST_CASE("strided_span_type_conversion") |
| 449 | { |
| 450 | int arr[] = {0, 1, 2, 3}; |
| 451 | multi_span<int> av(arr); |
| 452 | |
| 453 | { |
| 454 | strided_span<int, 1> sav{av.data(), av.size(), {av.size() / 2, 2}}; |
| 455 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 456 | strided_span<long, 1> lsav1 = sav.as_strided_span<long, 1>(); |
| 457 | #endif |
| 458 | } |
| 459 | { |
| 460 | strided_span<int, 1> sav{av, {av.size() / 2, 2}}; |
| 461 | #ifdef CONFIRM_COMPILATION_ERRORS |
| 462 | strided_span<long, 1> lsav1 = sav.as_strided_span<long, 1>(); |
| 463 | #endif |
| 464 | } |
| 465 | |
| 466 | multi_span<const byte, dynamic_range> bytes = as_bytes(av); |
| 467 | |
| 468 | // retype strided array with regular strides - from raw data |
| 469 | { |
| 470 | strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}}; |
| 471 | strided_span<const byte, 2> sav2{bytes.data(), bytes.size(), bounds}; |
| 472 | strided_span<const int, 2> sav3 = sav2.as_strided_span<const int>(); |
| 473 | CHECK(sav3[0][0] == 0); |
| 474 | CHECK(sav3[1][0] == 2); |
| 475 | CHECK_THROWS_AS(sav3[1][1], fail_fast); |
| 476 | CHECK_THROWS_AS(sav3[0][1], fail_fast); |
| 477 | } |
| 478 | |
| 479 | // retype strided array with regular strides - from multi_span |
| 480 | { |
| 481 | strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}}; |
| 482 | multi_span<const byte, 2, dynamic_range> bytes2 = |
| 483 | as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); |
| 484 | strided_span<const byte, 2> sav2{bytes2, bounds}; |
| 485 | strided_span<int, 2> sav3 = sav2.as_strided_span<int>(); |
| 486 | CHECK(sav3[0][0] == 0); |
| 487 | CHECK(sav3[1][0] == 2); |
| 488 | CHECK_THROWS_AS(sav3[1][1], fail_fast); |
| 489 | CHECK_THROWS_AS(sav3[0][1], fail_fast); |
| 490 | } |
| 491 | |
| 492 | // retype strided array with not enough elements - last dimension of the array is too small |
| 493 | { |
| 494 | strided_bounds<2> bounds{{4, 2}, {4, 1}}; |
| 495 | multi_span<const byte, 2, dynamic_range> bytes2 = |
| 496 | as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); |
| 497 | strided_span<const byte, 2> sav2{bytes2, bounds}; |
| 498 | CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast); |
| 499 | } |
| 500 | |
| 501 | // retype strided array with not enough elements - strides are too small |
| 502 | { |
| 503 | strided_bounds<2> bounds{{4, 2}, {2, 1}}; |
| 504 | multi_span<const byte, 2, dynamic_range> bytes2 = |
| 505 | as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); |
| 506 | strided_span<const byte, 2> sav2{bytes2, bounds}; |
| 507 | CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast); |
| 508 | } |
| 509 | |
| 510 | // retype strided array with not enough elements - last dimension does not divide by the new |
| 511 | // typesize |
| 512 | { |
| 513 | strided_bounds<2> bounds{{2, 6}, {4, 1}}; |
| 514 | multi_span<const byte, 2, dynamic_range> bytes2 = |
| 515 | as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); |
| 516 | strided_span<const byte, 2> sav2{bytes2, bounds}; |
| 517 | CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast); |
| 518 | } |
| 519 | |
| 520 | // retype strided array with not enough elements - strides does not divide by the new |
| 521 | // typesize |
| 522 | { |
| 523 | strided_bounds<2> bounds{{2, 1}, {6, 1}}; |
| 524 | multi_span<const byte, 2, dynamic_range> bytes2 = |
| 525 | as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2)); |
| 526 | strided_span<const byte, 2> sav2{bytes2, bounds}; |
| 527 | CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast); |
| 528 | } |
| 529 | |
| 530 | // retype strided array with irregular strides - from raw data |
| 531 | { |
| 532 | strided_bounds<1> bounds{bytes.size() / 2, 2}; |
| 533 | strided_span<const byte, 1> sav2{bytes.data(), bytes.size(), bounds}; |
| 534 | CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast); |
| 535 | } |
| 536 | |
| 537 | // retype strided array with irregular strides - from multi_span |
| 538 | { |
| 539 | strided_bounds<1> bounds{bytes.size() / 2, 2}; |
| 540 | strided_span<const byte, 1> sav2{bytes, bounds}; |
| 541 | CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | TEST_CASE("empty_strided_spans") |
| 546 | { |
| 547 | { |
| 548 | multi_span<int, 0> empty_av(nullptr); |
| 549 | strided_span<int, 1> empty_sav{empty_av, {0, 1}}; |
| 550 | |
| 551 | CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); |
| 552 | CHECK_THROWS_AS(empty_sav[0], fail_fast); |
| 553 | CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast); |
| 554 | CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast); |
| 555 | |
| 556 | for (const auto& v : empty_sav) { |
| 557 | (void) v; |
| 558 | CHECK(false); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | { |
| 563 | strided_span<int, 1> empty_sav{nullptr, 0, {0, 1}}; |
| 564 | |
| 565 | CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); |
| 566 | CHECK_THROWS_AS(empty_sav[0], fail_fast); |
| 567 | CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast); |
| 568 | CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast); |
| 569 | |
| 570 | for (const auto& v : empty_sav) { |
| 571 | (void) v; |
| 572 | CHECK(false); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void iterate_every_other_element(multi_span<int, dynamic_range> av) |
| 578 | { |
| 579 | // pick every other element |
| 580 | |
| 581 | auto length = av.size() / 2; |
| 582 | #if _MSC_VER > 1800 |
| 583 | auto bounds = strided_bounds<1>({length}, {2}); |
| 584 | #else |
| 585 | auto bounds = strided_bounds<1>(multi_span_index<1>{length}, multi_span_index<1>{2}); |
| 586 | #endif |
| 587 | strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds); |
| 588 | |
| 589 | CHECK(strided.size() == length); |
| 590 | CHECK(strided.bounds().index_bounds()[0] == length); |
| 591 | for (auto i = 0; i < strided.size(); ++i) { |
| 592 | CHECK(strided[i] == av[2 * i + 1]); |
| 593 | } |
| 594 | |
| 595 | int idx = 0; |
| 596 | for (auto num : strided) { |
| 597 | CHECK(num == av[2 * idx + 1]); |
| 598 | idx++; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | TEST_CASE("strided_span_section_iteration") |
| 603 | { |
| 604 | int arr[8] = {4, 0, 5, 1, 6, 2, 7, 3}; |
| 605 | |
| 606 | // static bounds |
| 607 | { |
| 608 | multi_span<int, 8> av(arr, 8); |
| 609 | iterate_every_other_element(av); |
| 610 | } |
| 611 | |
| 612 | // dynamic bounds |
| 613 | { |
| 614 | multi_span<int, dynamic_range> av(arr, 8); |
| 615 | iterate_every_other_element(av); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | TEST_CASE("dynamic_strided_span_section_iteration") |
| 620 | { |
| 621 | auto arr = new int[8]; |
| 622 | for (int i = 0; i < 4; ++i) { |
| 623 | arr[2 * i] = 4 + i; |
| 624 | arr[2 * i + 1] = i; |
| 625 | } |
| 626 | |
| 627 | auto av = as_multi_span(arr, 8); |
| 628 | iterate_every_other_element(av); |
| 629 | |
| 630 | delete[] arr; |
| 631 | } |
| 632 | |
| 633 | void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av) |
| 634 | { |
| 635 | const int expected[6] = {2, 3, 10, 11, 18, 19}; |
| 636 | auto section = av.section({0, 1, 0}, {3, 1, 2}); |
| 637 | |
| 638 | for (auto i = 0; i < section.extent<0>(); ++i) { |
| 639 | for (auto j = 0; j < section.extent<1>(); ++j) |
| 640 | for (auto k = 0; k < section.extent<2>(); ++k) { |
| 641 | auto idx = multi_span_index<3>{i, j, k}; // avoid braces in the CHECK macro |
| 642 | CHECK(section[idx] == expected[2 * i + 2 * j + k]); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | for (auto i = 0; i < section.extent<0>(); ++i) { |
| 647 | for (auto j = 0; j < section.extent<1>(); ++j) |
| 648 | for (auto k = 0; k < section.extent<2>(); ++k) |
| 649 | CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]); |
| 650 | } |
| 651 | |
| 652 | int i = 0; |
| 653 | for (const auto num : section) { |
| 654 | CHECK(num == expected[i]); |
| 655 | i++; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | TEST_CASE("strided_span_section_iteration_3d") |
| 660 | { |
| 661 | int arr[3][4][2]{}; |
| 662 | for (auto i = 0; i < 3; ++i) { |
| 663 | for (auto j = 0; j < 4; ++j) |
| 664 | for (auto k = 0; k < 2; ++k) arr[i][j][k] = 8 * i + 2 * j + k; |
| 665 | } |
| 666 | |
| 667 | { |
| 668 | multi_span<int, 3, 4, 2> av = arr; |
| 669 | iterate_second_slice(av); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | TEST_CASE("dynamic_strided_span_section_iteration_3d") |
| 674 | { |
| 675 | const auto height = 12, width = 2; |
| 676 | const auto size = height * width; |
| 677 | |
| 678 | auto arr = new int[static_cast<std::size_t>(size)]; |
| 679 | for (auto i = 0; i < size; ++i) { |
| 680 | arr[i] = i; |
| 681 | } |
| 682 | |
| 683 | { |
| 684 | auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim<4>(), dim<2>()); |
| 685 | iterate_second_slice(av); |
| 686 | } |
| 687 | |
| 688 | { |
| 689 | auto av = as_multi_span(as_multi_span(arr, 24), dim(3), dim<4>(), dim<2>()); |
| 690 | iterate_second_slice(av); |
| 691 | } |
| 692 | |
| 693 | { |
| 694 | auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim(4), dim<2>()); |
| 695 | iterate_second_slice(av); |
| 696 | } |
| 697 | |
| 698 | { |
| 699 | auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim<4>(), dim(2)); |
| 700 | iterate_second_slice(av); |
| 701 | } |
| 702 | delete[] arr; |
| 703 | } |
| 704 | |
| 705 | TEST_CASE("strided_span_conversion") |
| 706 | { |
| 707 | // get an multi_span of 'c' values from the list of X's |
| 708 | |
| 709 | struct X |
| 710 | { |
| 711 | int a; |
| 712 | int b; |
| 713 | int c; |
| 714 | }; |
| 715 | |
| 716 | X arr[4] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}}; |
| 717 | |
| 718 | int s = sizeof(int) / sizeof(byte); |
| 719 | auto d2 = 3 * s; |
| 720 | auto d1 = narrow_cast<int>(sizeof(int)) * 12 / d2; |
| 721 | |
| 722 | // convert to 4x12 array of bytes |
| 723 | auto av = as_multi_span(as_bytes(as_multi_span(arr, 4)), dim(d1), dim(d2)); |
| 724 | |
| 725 | CHECK(av.bounds().index_bounds()[0] == 4); |
| 726 | CHECK(av.bounds().index_bounds()[1] == 12); |
| 727 | |
| 728 | // get the last 4 columns |
| 729 | auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], |
| 730 | // arr[0].c[3] } , { arr[1].c[0], ... } , ... |
| 731 | // } |
| 732 | |
| 733 | // convert to array 4x1 array of integers |
| 734 | auto cs = section.as_strided_span<int>(); // { { arr[0].c }, {arr[1].c } , ... } |
| 735 | |
| 736 | CHECK(cs.bounds().index_bounds()[0] == 4); |
| 737 | CHECK(cs.bounds().index_bounds()[1] == 1); |
| 738 | |
| 739 | // transpose to 1x4 array |
| 740 | strided_bounds<2> reverse_bounds{ |
| 741 | {cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0]}, |
| 742 | {cs.bounds().strides()[1], cs.bounds().strides()[0]}}; |
| 743 | |
| 744 | strided_span<int, 2> transposed{cs.data(), cs.bounds().total_size(), reverse_bounds}; |
| 745 | |
| 746 | // slice to get a one-dimensional array of c's |
| 747 | strided_span<int, 1> result = transposed[0]; |
| 748 | |
| 749 | CHECK(result.bounds().index_bounds()[0] == 4); |
| 750 | CHECK_THROWS_AS(result.bounds().index_bounds()[1], fail_fast); |
| 751 | |
| 752 | int i = 0; |
| 753 | for (auto& num : result) { |
| 754 | CHECK(num == arr[i].c); |
| 755 | i++; |
| 756 | } |
| 757 | } |