From b3870ca02077ac39a306478ae6259124e46b8d9f Mon Sep 17 00:00:00 2001 From: paweldac Date: Wed, 21 Feb 2018 22:33:07 +0100 Subject: [PATCH] add gsl::index typedef (#620) * rename index in multi_span to span_index gsl::index is name reserved for different type * add gsl::index typedef cppcoreguidelines referece: ES.107: Don't use unsigned for subscripts, prefer gsl::index https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-subscripts * rename span_index to multi_span_index --- include/gsl/gsl_util | 21 +++++---- include/gsl/multi_span | 70 ++++++++++++++-------------- include/gsl/span | 10 ++-- tests/multi_span_tests.cpp | 60 ++++++++++++------------ tests/strided_span_tests.cpp | 88 ++++++++++++++++++------------------ tests/utils_tests.cpp | 15 ++++-- 6 files changed, 137 insertions(+), 127 deletions(-) diff --git a/include/gsl/gsl_util b/include/gsl/gsl_util index b048a43..1aa8ba6 100644 --- a/include/gsl/gsl_util +++ b/include/gsl/gsl_util @@ -43,6 +43,9 @@ namespace gsl // GSL.util: utilities // +// index type for all container indexes/subscripts/sizes +using index = std::ptrdiff_t; + // final_action allows you to ensure something gets run at the end of a scope template class final_action @@ -117,25 +120,25 @@ T narrow(U u) // at() - Bounds-checked way of accessing builtin arrays, std::array, std::vector // template -constexpr T& at(T (&arr)[N], const std::ptrdiff_t index) +constexpr T& at(T (&arr)[N], const index i) { - Expects(index >= 0 && index < narrow_cast(N)); - return arr[static_cast(index)]; + Expects(i >= 0 && i < narrow_cast(N)); + return arr[static_cast(i)]; } template -constexpr auto at(Cont& cont, const std::ptrdiff_t index) -> decltype(cont[cont.size()]) +constexpr auto at(Cont& cont, const index i) -> decltype(cont[cont.size()]) { - Expects(index >= 0 && index < narrow_cast(cont.size())); + Expects(i >= 0 && i < narrow_cast(cont.size())); using size_type = decltype(cont.size()); - return cont[static_cast(index)]; + return cont[static_cast(i)]; } template -constexpr T at(const std::initializer_list cont, const std::ptrdiff_t index) +constexpr T at(const std::initializer_list cont, const index i) { - Expects(index >= 0 && index < narrow_cast(cont.size())); - return *(cont.begin() + index); + Expects(i >= 0 && i < narrow_cast(cont.size())); + return *(cont.begin() + i); } } // namespace gsl diff --git a/include/gsl/multi_span b/include/gsl/multi_span index cf82144..65e65a1 100644 --- a/include/gsl/multi_span +++ b/include/gsl/multi_span @@ -85,12 +85,12 @@ namespace details } template -class index final +class multi_span_index final { static_assert(Rank > 0, "Rank must be greater than 0!"); template - friend class index; + friend class multi_span_index; public: static const std::size_t rank = Rank; @@ -99,22 +99,22 @@ public: using reference = std::add_lvalue_reference_t; using const_reference = std::add_lvalue_reference_t>; - constexpr index() GSL_NOEXCEPT {} + constexpr multi_span_index() GSL_NOEXCEPT {} - constexpr index(const value_type (&values)[Rank]) GSL_NOEXCEPT + constexpr multi_span_index(const value_type (&values)[Rank]) GSL_NOEXCEPT { std::copy(values, values + Rank, elems); } template ::value>> - constexpr index(Ts... ds) GSL_NOEXCEPT : elems{narrow_cast(ds)...} + constexpr multi_span_index(Ts... ds) GSL_NOEXCEPT : elems{narrow_cast(ds)...} { } - constexpr index(const index& other) GSL_NOEXCEPT = default; + constexpr multi_span_index(const multi_span_index& other) GSL_NOEXCEPT = default; - constexpr index& operator=(const index& rhs) GSL_NOEXCEPT = default; + constexpr multi_span_index& operator=(const multi_span_index& rhs) GSL_NOEXCEPT = default; // Preconditions: component_idx < rank constexpr reference operator[](std::size_t component_idx) @@ -130,75 +130,75 @@ public: return elems[component_idx]; } - constexpr bool operator==(const index& rhs) const GSL_NOEXCEPT + constexpr bool operator==(const multi_span_index& rhs) const GSL_NOEXCEPT { return std::equal(elems, elems + rank, rhs.elems); } - constexpr bool operator!=(const index& rhs) const GSL_NOEXCEPT { return !(*this == rhs); } + constexpr bool operator!=(const multi_span_index& rhs) const GSL_NOEXCEPT { return !(*this == rhs); } - constexpr index operator+() const GSL_NOEXCEPT { return *this; } + constexpr multi_span_index operator+() const GSL_NOEXCEPT { return *this; } - constexpr index operator-() const GSL_NOEXCEPT + constexpr multi_span_index operator-() const GSL_NOEXCEPT { - index ret = *this; + multi_span_index ret = *this; std::transform(ret, ret + rank, ret, std::negate{}); return ret; } - constexpr index operator+(const index& rhs) const GSL_NOEXCEPT + constexpr multi_span_index operator+(const multi_span_index& rhs) const GSL_NOEXCEPT { - index ret = *this; + multi_span_index ret = *this; ret += rhs; return ret; } - constexpr index operator-(const index& rhs) const GSL_NOEXCEPT + constexpr multi_span_index operator-(const multi_span_index& rhs) const GSL_NOEXCEPT { - index ret = *this; + multi_span_index ret = *this; ret -= rhs; return ret; } - constexpr index& operator+=(const index& rhs) GSL_NOEXCEPT + constexpr multi_span_index& operator+=(const multi_span_index& rhs) GSL_NOEXCEPT { std::transform(elems, elems + rank, rhs.elems, elems, std::plus{}); return *this; } - constexpr index& operator-=(const index& rhs) GSL_NOEXCEPT + constexpr multi_span_index& operator-=(const multi_span_index& rhs) GSL_NOEXCEPT { std::transform(elems, elems + rank, rhs.elems, elems, std::minus{}); return *this; } - constexpr index operator*(value_type v) const GSL_NOEXCEPT + constexpr multi_span_index operator*(value_type v) const GSL_NOEXCEPT { - index ret = *this; + multi_span_index ret = *this; ret *= v; return ret; } - constexpr index operator/(value_type v) const GSL_NOEXCEPT + constexpr multi_span_index operator/(value_type v) const GSL_NOEXCEPT { - index ret = *this; + multi_span_index ret = *this; ret /= v; return ret; } - friend constexpr index operator*(value_type v, const index& rhs) GSL_NOEXCEPT + friend constexpr multi_span_index operator*(value_type v, const multi_span_index& rhs) GSL_NOEXCEPT { return rhs * v; } - constexpr index& operator*=(value_type v) GSL_NOEXCEPT + constexpr multi_span_index& operator*=(value_type v) GSL_NOEXCEPT { std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies{}(x, v); }); return *this; } - constexpr index& operator/=(value_type v) GSL_NOEXCEPT + constexpr multi_span_index& operator/=(value_type v) GSL_NOEXCEPT { std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides{}(x, v); }); @@ -497,8 +497,8 @@ namespace details } template 1), - typename Ret = std::enable_if_t>> - constexpr Ret shift_left(const index& other) GSL_NOEXCEPT + typename Ret = std::enable_if_t>> + constexpr Ret shift_left(const multi_span_index& other) GSL_NOEXCEPT { Ret ret{}; for (std::size_t i = 0; i < Rank - 1; ++i) { @@ -535,7 +535,7 @@ public: static const std::ptrdiff_t static_size = MyRanges::TotalSize; using size_type = std::ptrdiff_t; - using index_type = index; + using index_type = multi_span_index; using const_index_type = std::add_const_t; using iterator = bounds_iterator; using const_iterator = bounds_iterator; @@ -631,9 +631,9 @@ public: return m_ranges.contains(idx) != -1; } - constexpr size_type operator[](std::size_t index) const GSL_NOEXCEPT + constexpr size_type operator[](std::size_t idx) const GSL_NOEXCEPT { - return m_ranges.elementNum(index); + return m_ranges.elementNum(idx); } template @@ -698,7 +698,7 @@ public: using const_reference = std::add_const_t; using size_type = value_type; using difference_type = value_type; - using index_type = index; + using index_type = multi_span_index; using const_index_type = std::add_const_t; using iterator = bounds_iterator; using const_iterator = bounds_iterator; @@ -1478,15 +1478,15 @@ public: constexpr pointer data() const GSL_NOEXCEPT { return data_; } template - constexpr reference operator()(FirstIndex index) + constexpr reference operator()(FirstIndex idx) { - return this->operator[](narrow_cast(index)); + return this->operator[](narrow_cast(idx)); } template - constexpr reference operator()(FirstIndex index, OtherIndices... indices) + constexpr reference operator()(FirstIndex firstIndex, OtherIndices... indices) { - index_type idx = {narrow_cast(index), + index_type idx = {narrow_cast(firstIndex), narrow_cast(indices)...}; return this->operator[](idx); } diff --git a/include/gsl/span b/include/gsl/span index 6f87633..9d67fb6 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -125,10 +125,10 @@ namespace details span_iterator() = default; - constexpr span_iterator(const Span* span, typename Span::index_type index) GSL_NOEXCEPT - : span_(span), index_(index) + constexpr span_iterator(const Span* span, typename Span::index_type idx) GSL_NOEXCEPT + : span_(span), index_(idx) { - Expects(span == nullptr || (0 <= index_ && index <= span_->size())); + Expects(span == nullptr || (0 <= index_ && index_ <= span_->size())); } friend span_iterator; @@ -662,10 +662,10 @@ span make_span(Ptr& cont) // Specialization of gsl::at for span template -constexpr ElementType& at(span s, std::ptrdiff_t index) +constexpr ElementType& at(span s, index i) { // No bounds checking here because it is done in span::operator[] called below - return s[index]; + return s[i]; } } // namespace gsl diff --git a/tests/multi_span_tests.cpp b/tests/multi_span_tests.cpp index 52df759..549dcbe 100644 --- a/tests/multi_span_tests.cpp +++ b/tests/multi_span_tests.cpp @@ -1243,7 +1243,7 @@ TEST_CASE("empty_spans") { multi_span empty_av(nullptr); - CHECK(empty_av.bounds().index_bounds() == index<1>{0}); + CHECK(empty_av.bounds().index_bounds() == multi_span_index<1>{0}); CHECK_THROWS_AS(empty_av[0], fail_fast); CHECK_THROWS_AS(empty_av.begin()[0], fail_fast); CHECK_THROWS_AS(empty_av.cbegin()[0], fail_fast); @@ -1255,7 +1255,7 @@ TEST_CASE("empty_spans") { multi_span empty_av = {}; - CHECK(empty_av.bounds().index_bounds() == index<1>{0}); + CHECK(empty_av.bounds().index_bounds() == multi_span_index<1>{0}); CHECK_THROWS_AS(empty_av[0], fail_fast); CHECK_THROWS_AS(empty_av.begin()[0], fail_fast); CHECK_THROWS_AS(empty_av.cbegin()[0], fail_fast); @@ -1277,13 +1277,13 @@ TEST_CASE("index_constructor") multi_span av(arr, 8); ptrdiff_t a[1] = {0}; - index<1> i = a; + multi_span_index<1> i = a; CHECK(av[i] == 4); auto av2 = as_multi_span(av, dim<4>(), dim(2)); ptrdiff_t a2[2] = {0, 1}; - index<2> i2 = a2; + multi_span_index<2> i2 = a2; CHECK(av2[i2] == 0); CHECK(av2[0][i] == 4); @@ -1295,70 +1295,70 @@ TEST_CASE("index_constructors") { { // components of the same type - index<3> i1(0, 1, 2); + multi_span_index<3> i1(0, 1, 2); CHECK(i1[0] == 0); // components of different types std::size_t c0 = 0; std::size_t c1 = 1; - index<3> i2(c0, c1, 2); + multi_span_index<3> i2(c0, c1, 2); CHECK(i2[0] == 0); // from array - index<3> i3 = {0, 1, 2}; + multi_span_index<3> i3 = {0, 1, 2}; CHECK(i3[0] == 0); // from other index of the same size type - index<3> i4 = i3; + multi_span_index<3> i4 = i3; CHECK(i4[0] == 0); // default - index<3> i7; + multi_span_index<3> i7; CHECK(i7[0] == 0); // default - index<3> i9 = {}; + multi_span_index<3> i9 = {}; CHECK(i9[0] == 0); } { // components of the same type - index<1> i1(0); + multi_span_index<1> i1(0); CHECK(i1[0] == 0); // components of different types std::size_t c0 = 0; - index<1> i2(c0); + multi_span_index<1> i2(c0); CHECK(i2[0] == 0); // from array - index<1> i3 = {0}; + multi_span_index<1> i3 = {0}; CHECK(i3[0] == 0); // from int - index<1> i4 = 0; + multi_span_index<1> i4 = 0; CHECK(i4[0] == 0); // from other index of the same size type - index<1> i5 = i3; + multi_span_index<1> i5 = i3; CHECK(i5[0] == 0); // default - index<1> i8; + multi_span_index<1> i8; CHECK(i8[0] == 0); // default - index<1> i9 = {}; + multi_span_index<1> i9 = {}; CHECK(i9[0] == 0); } #ifdef CONFIRM_COMPILATION_ERRORS { - index<3> i1(0, 1); - index<3> i2(0, 1, 2, 3); - index<3> i3 = {0}; - index<3> i4 = {0, 1, 2, 3}; - index<1> i5 = {0, 1}; + multi_span_index<3> i1(0, 1); + multi_span_index<3> i2(0, 1, 2, 3); + multi_span_index<3> i3 = {0}; + multi_span_index<3> i4 = {0, 1, 2, 3}; + multi_span_index<1> i5 = {0, 1}; } #endif } @@ -1367,15 +1367,15 @@ TEST_CASE("index_operations") { ptrdiff_t a[3] = {0, 1, 2}; ptrdiff_t b[3] = {3, 4, 5}; - index<3> i = a; - index<3> j = b; + multi_span_index<3> i = a; + multi_span_index<3> j = b; CHECK(i[0] == 0); CHECK(i[1] == 1); CHECK(i[2] == 2); { - index<3> k = i + j; + multi_span_index<3> k = i + j; CHECK(i[0] == 0); CHECK(i[1] == 1); @@ -1386,7 +1386,7 @@ TEST_CASE("index_operations") } { - index<3> k = i * 3; + multi_span_index<3> k = i * 3; CHECK(i[0] == 0); CHECK(i[1] == 1); @@ -1397,7 +1397,7 @@ TEST_CASE("index_operations") } { - index<3> k = 3 * i; + multi_span_index<3> k = 3 * i; CHECK(i[0] == 0); CHECK(i[1] == 1); @@ -1408,7 +1408,7 @@ TEST_CASE("index_operations") } { - index<2> k = details::shift_left(i); + multi_span_index<2> k = details::shift_left(i); CHECK(i[0] == 0); CHECK(i[1] == 1); @@ -1431,7 +1431,7 @@ void iterate_second_column(multi_span av) } for (auto i = 0; i < section.size(); ++i) { - auto idx = index<2>{i, 0}; // avoid braces inside the CHECK macro + auto idx = multi_span_index<2>{i, 0}; // avoid braces inside the CHECK macro CHECK(section[idx] == av[i][1]); } @@ -1439,7 +1439,7 @@ void iterate_second_column(multi_span av) CHECK(section.bounds().index_bounds()[1] == 1); for (auto i = 0; i < section.bounds().index_bounds()[0]; ++i) { for (auto j = 0; j < section.bounds().index_bounds()[1]; ++j) { - auto idx = index<2>{i, j}; // avoid braces inside the CHECK macro + auto idx = multi_span_index<2>{i, j}; // avoid braces inside the CHECK macro CHECK(section[idx] == av[i][1]); } } diff --git a/tests/strided_span_tests.cpp b/tests/strided_span_tests.cpp index 2c1c047..8719336 100644 --- a/tests/strided_span_tests.cpp +++ b/tests/strided_span_tests.cpp @@ -48,8 +48,8 @@ TEST_CASE("span_section_test") int a[30][4][5]; const auto av = as_multi_span(a); - const auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2}); - const auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1}); + const auto sub = av.section({15, 0, 0}, gsl::multi_span_index<3>{2, 2, 2}); + const auto subsub = sub.section({1, 0, 0}, gsl::multi_span_index<3>{1, 1, 1}); (void) subsub; } @@ -79,18 +79,18 @@ TEST_CASE("strided_span_constructors") const int carr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; strided_span sav1{arr, {{9}, {1}}}; // T -> T - CHECK(sav1.bounds().index_bounds() == index<1>{9}); + CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{9}); CHECK(sav1.bounds().stride() == 1); CHECK((sav1[0] == 1 && sav1[8] == 9)); strided_span sav2{carr, {{4}, {2}}}; // const T -> const T - CHECK(sav2.bounds().index_bounds() == index<1>{4}); - CHECK(sav2.bounds().strides() == index<1>{2}); + CHECK(sav2.bounds().index_bounds() == multi_span_index<1>{4}); + CHECK(sav2.bounds().strides() == multi_span_index<1>{2}); CHECK((sav2[0] == 1 && sav2[3] == 7)); strided_span sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T - CHECK((sav3.bounds().index_bounds() == index<2>{2, 2})); - CHECK((sav3.bounds().strides() == index<2>{6, 2})); + CHECK((sav3.bounds().index_bounds() == multi_span_index<2>{2, 2})); + CHECK((sav3.bounds().strides() == multi_span_index<2>{6, 2})); CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7)); } @@ -103,8 +103,8 @@ TEST_CASE("strided_span_constructors") const multi_span src = arr; strided_span sav{src, {2, 1}}; - CHECK(sav.bounds().index_bounds() == index<1>{2}); - CHECK(sav.bounds().strides() == index<1>{1}); + CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav.bounds().strides() == multi_span_index<1>{1}); CHECK(sav[1] == 2); #if _MSC_VER > 1800 @@ -115,8 +115,8 @@ TEST_CASE("strided_span_constructors") strided_span sav_c{multi_span{src}, strided_bounds<1>{2, 1}}; #endif - CHECK(sav_c.bounds().index_bounds() == index<1>{2}); - CHECK(sav_c.bounds().strides() == index<1>{1}); + CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_c.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_c[1] == 2); #if _MSC_VER > 1800 @@ -125,8 +125,8 @@ TEST_CASE("strided_span_constructors") strided_span sav_v{multi_span{src}, strided_bounds<1>{2, 1}}; #endif - CHECK(sav_v.bounds().index_bounds() == index<1>{2}); - CHECK(sav_v.bounds().strides() == index<1>{1}); + CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_v.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_v[1] == 2); #if _MSC_VER > 1800 @@ -135,8 +135,8 @@ TEST_CASE("strided_span_constructors") strided_span sav_cv{multi_span{src}, strided_bounds<1>{2, 1}}; #endif - CHECK(sav_cv.bounds().index_bounds() == index<1>{2}); - CHECK(sav_cv.bounds().strides() == index<1>{1}); + CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_cv[1] == 2); } @@ -145,8 +145,8 @@ TEST_CASE("strided_span_constructors") const multi_span src{arr}; strided_span sav_c{src, {2, 1}}; - CHECK(sav_c.bounds().index_bounds() == index<1>{2}); - CHECK(sav_c.bounds().strides() == index<1>{1}); + CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_c.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_c[1] == 2); #if _MSC_VER > 1800 @@ -156,8 +156,8 @@ TEST_CASE("strided_span_constructors") strided_bounds<1>{2, 1}}; #endif - CHECK(sav_cv.bounds().index_bounds() == index<1>{2}); - CHECK(sav_cv.bounds().strides() == index<1>{1}); + CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_cv[1] == 2); } @@ -166,8 +166,8 @@ TEST_CASE("strided_span_constructors") const multi_span src{arr}; strided_span sav_v{src, {2, 1}}; - CHECK(sav_v.bounds().index_bounds() == index<1>{2}); - CHECK(sav_v.bounds().strides() == index<1>{1}); + CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_v.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_v[1] == 2); #if _MSC_VER > 1800 @@ -176,8 +176,8 @@ TEST_CASE("strided_span_constructors") strided_span sav_cv{multi_span{src}, strided_bounds<1>{2, 1}}; #endif - CHECK(sav_cv.bounds().index_bounds() == index<1>{2}); - CHECK(sav_cv.bounds().strides() == index<1>{1}); + CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_cv[1] == 2); } @@ -186,8 +186,8 @@ TEST_CASE("strided_span_constructors") const multi_span src{arr}; strided_span sav_cv{src, {2, 1}}; - CHECK(sav_cv.bounds().index_bounds() == index<1>{2}); - CHECK(sav_cv.bounds().strides() == index<1>{1}); + CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1}); CHECK(sav_cv[1] == 2); } } @@ -206,7 +206,7 @@ TEST_CASE("strided_span_constructors") const strided_span src{arr, {2, 1}}; strided_span sav{src}; - CHECK(sav.bounds().index_bounds() == index<1>{2}); + CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); CHECK(sav.bounds().stride() == 1); CHECK(sav[1] == 5); @@ -221,15 +221,15 @@ TEST_CASE("strided_span_constructors") const strided_span src1{arr1, {2, 1}}; strided_span sav1{src1}; - CHECK(sav1.bounds().index_bounds() == index<1>{2}); + CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2}); CHECK(sav1.bounds().stride() == 1); CHECK(sav1[0] == 3); int arr2[6] = {1, 2, 3, 4, 5, 6}; const strided_span src2{arr2, {{3, 2}, {2, 1}}}; strided_span sav2{src2}; - CHECK((sav2.bounds().index_bounds() == index<2>{3, 2})); - CHECK((sav2.bounds().strides() == index<2>{2, 1})); + CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2})); + CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1})); CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); } @@ -241,8 +241,8 @@ TEST_CASE("strided_span_constructors") const strided_span src{arr1, {{2}, {1}}}; strided_span sav{arr2, {{3}, {2}}}; strided_span& sav_ref = (sav = src); - CHECK(sav.bounds().index_bounds() == index<1>{2}); - CHECK(sav.bounds().strides() == index<1>{1}); + CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav.bounds().strides() == multi_span_index<1>{1}); CHECK(sav[0] == 1); CHECK(&sav_ref == &sav); } @@ -254,8 +254,8 @@ TEST_CASE("strided_span_constructors") const strided_span src1{arr1, {2, 1}}; strided_span sav1{arr1b, {1, 1}}; strided_span& sav1_ref = (sav1 = src1); - CHECK(sav1.bounds().index_bounds() == index<1>{2}); - CHECK(sav1.bounds().strides() == index<1>{1}); + CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2}); + CHECK(sav1.bounds().strides() == multi_span_index<1>{1}); CHECK(sav1[0] == 3); CHECK(&sav1_ref == &sav1); @@ -264,8 +264,8 @@ TEST_CASE("strided_span_constructors") const strided_span src2{arr2, {{3, 2}, {2, 1}}}; strided_span sav2{arr2b, {{1, 1}, {1, 1}}}; strided_span& sav2_ref = (sav2 = src2); - CHECK((sav2.bounds().index_bounds() == index<2>{3, 2})); - CHECK((sav2.bounds().strides() == index<2>{2, 1})); + CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2})); + CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1})); CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5)); CHECK(&sav2_ref == &sav2); } @@ -322,7 +322,7 @@ TEST_CASE("strided_span_column_major") // Section strided_span cm_sec = cm_sav.section({2, 1}, {3, 2}); - CHECK((cm_sec.bounds().index_bounds() == index<2>{3, 2})); + CHECK((cm_sec.bounds().index_bounds() == multi_span_index<2>{3, 2})); CHECK((cm_sec[{0, 0}] == 8)); CHECK((cm_sec[{0, 1}] == 9)); CHECK((cm_sec[{1, 0}] == 11)); @@ -371,7 +371,7 @@ TEST_CASE("strided_span_bounds") { // strided array ctor with matching strided bounds strided_span sav{arr, {4, 1}}; - CHECK(sav.bounds().index_bounds() == index<1>{4}); + CHECK(sav.bounds().index_bounds() == multi_span_index<1>{4}); CHECK(sav[3] == 3); CHECK_THROWS_AS(sav[4], fail_fast); } @@ -379,7 +379,7 @@ TEST_CASE("strided_span_bounds") { // strided array ctor with smaller strided bounds strided_span sav{arr, {2, 1}}; - CHECK(sav.bounds().index_bounds() == index<1>{2}); + CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); CHECK(sav[1] == 1); CHECK_THROWS_AS(sav[2], fail_fast); } @@ -387,7 +387,7 @@ TEST_CASE("strided_span_bounds") { // strided array ctor with fitting irregular bounds strided_span sav{arr, {2, 3}}; - CHECK(sav.bounds().index_bounds() == index<1>{2}); + CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2}); CHECK(sav[0] == 0); CHECK(sav[1] == 3); CHECK_THROWS_AS(sav[2], fail_fast); @@ -433,7 +433,7 @@ TEST_CASE("strided_span_bounds") strided_span sav7{av.as_multi_span(dim<2>(), dim<2>()), {{1, 1}, {1, 1}, {1, 1}}}; - index<1> index{0, 1}; + multi_span_index<1> index{0, 1}; strided_span sav8{arr, {1, {1, 1}}}; strided_span sav9{arr, {{1, 1}, {1, 1}}}; strided_span sav10{av, {1, {1, 1}}}; @@ -548,7 +548,7 @@ TEST_CASE("empty_strided_spans") multi_span empty_av(nullptr); strided_span empty_sav{empty_av, {0, 1}}; - CHECK(empty_sav.bounds().index_bounds() == index<1>{0}); + CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); CHECK_THROWS_AS(empty_sav[0], fail_fast); CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast); @@ -562,7 +562,7 @@ TEST_CASE("empty_strided_spans") { strided_span empty_sav{nullptr, 0, {0, 1}}; - CHECK(empty_sav.bounds().index_bounds() == index<1>{0}); + CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0}); CHECK_THROWS_AS(empty_sav[0], fail_fast); CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast); CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast); @@ -582,7 +582,7 @@ void iterate_every_other_element(multi_span av) #if _MSC_VER > 1800 auto bounds = strided_bounds<1>({length}, {2}); #else - auto bounds = strided_bounds<1>(index<1>{length}, index<1>{2}); + auto bounds = strided_bounds<1>(multi_span_index<1>{length}, multi_span_index<1>{2}); #endif strided_span strided(&av.data()[1], av.size() - 1, bounds); @@ -638,7 +638,7 @@ void iterate_second_slice(multi_span(); ++i) { for (auto j = 0; j < section.extent<1>(); ++j) for (auto k = 0; k < section.extent<2>(); ++k) { - auto idx = index<3>{i, j, k}; // avoid braces in the CHECK macro + auto idx = multi_span_index<3>{i, j, k}; // avoid braces in the CHECK macro CHECK(section[idx] == expected[2 * i + 2 * j + k]); } } diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 610efbf..587b0a1 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -18,13 +18,20 @@ #include // for narrow, finally, narrow_cast, narrowing_e... -#include // for move -#include // for reference_wrapper, _Bind_helper<>::type -#include // for numeric_limits -#include // for uint32_t, int32_t +#include // for move +#include // for reference_wrapper, _Bind_helper<>::type +#include // for numeric_limits +#include // for uint32_t, int32_t +#include // for is_same using namespace gsl; +TEST_CASE("sanity check for gsl::index typedef") +{ + static_assert(std::is_same::value, + "gsl::index represents wrong arithmetic type"); +} + void f(int& i) { i += 1; } TEST_CASE("finally_lambda")