From 5a1e4f39537330c579662a76a9a9b5c8580ad708 Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Tue, 4 Feb 2020 15:31:33 -0800 Subject: [PATCH] update index_type to size_type, fix a couple Expects --- include/gsl/span | 10 ++++++---- tests/span_tests.cpp | 14 +++++++------- tests/string_span_tests.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/gsl/span b/include/gsl/span index 737ef80..5049b97 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -143,14 +143,14 @@ namespace details constexpr reference operator*() const noexcept { Expects(begin_ && current_ && end_); - Expects(current_ < end_); + Expects(begin_ <= current_ && current_ < end_); return *current_; } constexpr pointer operator->() const noexcept { Expects(begin_ && current_ && end_); - Expects(current_ < end_); + Expects(begin_ <= current_ && current_ < end_); return current_; } constexpr span_iterator& operator++() noexcept @@ -171,7 +171,7 @@ namespace details constexpr span_iterator& operator--() noexcept { Expects(begin_ && current_ && end_); - Expects(current_ > begin_); + Expects(begin_ < current_); --current_; return *this; } @@ -295,7 +295,9 @@ namespace details constexpr void _Verify_offset(const difference_type n) const noexcept { // test that *this + n is within the range of this call - Expects( n <= (current_ - begin_) && n <= (end_ - current_)); + if (n != 0) Expects(begin_ && current_ && end_); + if (n > 0) Expects(end_ - current_ >= n); + if (n < 0) Expects(current_ - begin_ >= -n); } GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 8d9fadd..99ee0d8 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -105,17 +105,17 @@ TEST(span_test, from_nullptr_size_constructor) std::abort(); }); { - span s{nullptr, narrow_cast::index_type>(0)}; + span s{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); - span cs{nullptr, narrow_cast::index_type>(0)}; + span cs{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(cs.size() == 0); EXPECT_TRUE(cs.data() == nullptr); } { auto workaround_macro = []() { - const span s{nullptr, narrow_cast::index_type>(0)}; + const span s{nullptr, narrow_cast::size_type>(0)}; }; EXPECT_DEATH(workaround_macro(), deathstring); } @@ -134,11 +134,11 @@ TEST(span_test, from_nullptr_size_constructor) EXPECT_DEATH(const_workaround_macro(), deathstring); } { - span s{nullptr, narrow_cast::index_type>(0)}; + span s{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); - span cs{nullptr, narrow_cast::index_type>(0)}; + span cs{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(cs.size() == 0); EXPECT_TRUE(cs.data() == nullptr); } @@ -193,7 +193,7 @@ TEST(span_test, from_pointer_length_constructor) { int* p = nullptr; - span s{p, narrow_cast::index_type>(0)}; + span s{p, narrow_cast::size_type>(0)}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); } @@ -214,7 +214,7 @@ TEST(span_test, from_pointer_length_constructor) { int* p = nullptr; - auto s = make_span(p, narrow_cast::index_type>(0)); + auto s = make_span(p, narrow_cast::size_type>(0)); EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); } diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp index dd5a0de..c1f8cf4 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -163,14 +163,14 @@ TEST(string_span_tests, TestConstructFromStdString) { std::string s = "Hello there world"; cstring_span<> v = s; - EXPECT_TRUE(v.length() == static_cast::index_type>(s.length())); + EXPECT_TRUE(v.length() == static_cast::size_type>(s.length())); } TEST(string_span_tests, TestConstructFromStdVector) { std::vector vec(5, 'h'); string_span<> v{vec}; - EXPECT_TRUE(v.length() == static_cast::index_type>(vec.size())); + EXPECT_TRUE(v.length() == static_cast::size_type>(vec.size())); } TEST(string_span_tests, TestStackArrayConstruction) @@ -232,7 +232,7 @@ TEST(string_span_tests, TestToString) char stack_string[] = "Hello"; cstring_span<> v = ensure_z(stack_string); auto s2 = gsl::to_string(v); - EXPECT_TRUE(static_cast::index_type>(s2.length()) == v.length()); + EXPECT_TRUE(static_cast::size_type>(s2.length()) == v.length()); EXPECT_TRUE(s2.length() == static_cast(5)); } @@ -245,7 +245,7 @@ TEST(string_span_tests, TestToBasicString) char stack_string[] = "Hello"; cstring_span<> v = ensure_z(stack_string); auto s2 = gsl::to_basic_string, ::std::allocator>(v); - EXPECT_TRUE(static_cast::index_type>(s2.length()) == v.length()); + EXPECT_TRUE(static_cast::size_type>(s2.length()) == v.length()); EXPECT_TRUE(s2.length() == static_cast(5)); }