/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// #include #include // for byte #include // for span, span_iterator, operator==, operator!= #include // for narrow_cast, at #include // for array #include // for ptrdiff_t #include // for ptrdiff_t #include // for reverse_iterator, operator-, operator== #include // for unique_ptr, shared_ptr, make_unique, allo... #include // for match_results, sub_match, match_results<>... #include // for string #include // for integral_constant<>::value, is_default_co... #include #include // for vector // the string_view include and macro are used in the deduction guide verification #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L)) #ifdef __has_include #if __has_include() #include #define HAS_STRING_VIEW #endif // __has_include() #endif // __has_include #endif // (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L)) #if defined(__cplusplus) && __cplusplus >= 202002L #include #endif // __cplusplus >= 202002L #include "deathTestCommon.h" using namespace gsl; #if __cplusplus >= 201703l using std::void_t; #else // __cplusplus >= 201703l template using void_t = void; #endif // __cplusplus < 201703l namespace { struct BaseClass { }; struct DerivedClass : BaseClass { }; struct AddressOverloaded { #if (__cplusplus > 201402L) [[maybe_unused]] #endif AddressOverloaded operator&() const { return {}; } }; } // namespace TEST(span_test, constructors) { span s; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); span cs; EXPECT_TRUE(cs.size() == 0); EXPECT_TRUE(cs.data() == nullptr); } TEST(span_test, constructors_with_extent) { span s; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); span cs; EXPECT_TRUE(cs.size() == 0); EXPECT_TRUE(cs.data() == nullptr); } TEST(span_test, constructors_with_bracket_init) { span s{}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); span cs{}; EXPECT_TRUE(cs.size() == 0); EXPECT_TRUE(cs.data() == nullptr); } TEST(span_test, size_optimization) { span s; EXPECT_TRUE(sizeof(s) == sizeof(int*) + sizeof(ptrdiff_t)); span se; EXPECT_TRUE(sizeof(se) == sizeof(int*)); } TEST(span_test, from_nullptr_size_constructor) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. from_nullptr_size_constructor"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { span s{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); 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::size_type>(0)}; }; EXPECT_DEATH(workaround_macro(), expected); } { auto workaround_macro = []() { const span s{nullptr, 1}; }; EXPECT_DEATH(workaround_macro(), expected); auto const_workaround_macro = []() { const span s{nullptr, 1}; }; EXPECT_DEATH(const_workaround_macro(), expected); } { auto workaround_macro = []() { const span s{nullptr, 1}; }; EXPECT_DEATH(workaround_macro(), expected); auto const_workaround_macro = []() { const span s{nullptr, 1}; }; EXPECT_DEATH(const_workaround_macro(), expected); } { span s{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); span cs{nullptr, narrow_cast::size_type>(0)}; EXPECT_TRUE(cs.size() == 0); EXPECT_TRUE(cs.data() == nullptr); } } TEST(span_test, from_pointer_length_constructor) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. from_pointer_length_constructor"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int arr[4] = {1, 2, 3, 4}; { for (int i = 0; i < 4; ++i) { { span s = {&arr[0], narrow_cast(i)}; EXPECT_TRUE(s.size() == narrow_cast(i)); EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s.empty() == (i == 0)); for (int j = 0; j < i; ++j) EXPECT_TRUE(arr[j] == s[narrow_cast(j)]); } { span s = {&arr[i], 4 - narrow_cast(i)}; EXPECT_TRUE(s.size() == 4 - narrow_cast(i)); EXPECT_TRUE(s.data() == &arr[i]); EXPECT_TRUE(s.empty() == ((4 - i) == 0)); for (int j = 0; j < 4 - i; ++j) EXPECT_TRUE(arr[j + i] == s[narrow_cast(j)]); } } } { span s{&arr[0], 2}; EXPECT_TRUE(s.size() == 2); EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s[0] == 1); EXPECT_TRUE(s[1] == 2); } { int* p = nullptr; span s{p, narrow_cast::size_type>(0)}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); } { int* p = nullptr; auto workaround_macro = [=]() { const span s{p, 2}; }; EXPECT_DEATH(workaround_macro(), expected); } } TEST(span_test, from_pointer_pointer_construction) { // const auto terminateHandler = std::set_terminate([] { // std::cerr << "Expected Death. from_pointer_pointer_construction"; // std::abort(); // }); // const auto expected = GetExpectedDeathString(terminateHandler); int arr[4] = {1, 2, 3, 4}; { span s{&arr[0], &arr[2]}; EXPECT_TRUE(s.size() == 2); EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s[0] == 1); EXPECT_TRUE(s[1] == 2); } { span s{&arr[0], &arr[2]}; EXPECT_TRUE(s.size() == 2); EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s[0] == 1); EXPECT_TRUE(s[1] == 2); } { span s{&arr[0], &arr[0]}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == &arr[0]); } { span s{&arr[0], &arr[0]}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == &arr[0]); } //{ // this test succeeds on all platforms, gsl::span is more relaxed than std::span where this would be UB // auto workaround_macro = [&]() { span s{&arr[1], &arr[0]}; }; // EXPECT_DEATH(workaround_macro(), expected); //} { int* p = nullptr; span s{p, p}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); } { int* p = nullptr; span s{p, p}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.data() == nullptr); } } template static constexpr bool CtorCompilesFor = false; template static constexpr bool CtorCompilesFor()})>> = true; TEST(span_test, from_array_constructor) { int arr[5] = {1, 2, 3, 4, 5}; static_assert(!CtorCompilesFor, int[5]>, "!CtorCompilesFor, int[5]>"); static_assert(!CtorCompilesFor, int[5]>, "!CtorCompilesFor, int[5]>"); static_assert(!CtorCompilesFor, int[2][3]>, "!CtorCompilesFor, int[2][3]>"); { const span s{arr}; EXPECT_TRUE(s.size() == 5); EXPECT_TRUE(s.data() == &arr[0]); } { const span s{arr}; EXPECT_TRUE(s.size() == 5); EXPECT_TRUE(s.data() == &arr[0]); } int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; static_assert(!CtorCompilesFor, int[2][3]>, "!CtorCompilesFor, int[2][3]>"); static_assert(!CtorCompilesFor, int[2][3]>, "!CtorCompilesFor, int[2][3]>"); { const span s{std::addressof(arr2d[0]), 1}; EXPECT_TRUE(s.size() == 1); EXPECT_TRUE(s.data() == std::addressof(arr2d[0])); } int arr3d[2][3][2] = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}}; static_assert(!CtorCompilesFor, int[2][3][2]>, "!CtorCompilesFor, int[2][3][2]>"); static_assert(!CtorCompilesFor, int[2][3][2]>, "!CtorCompilesFor, int[2][3][2]>"); static_assert(!CtorCompilesFor, int[2][3][2]>, "!CtorCompilesFor, int[2][3][2]>"); static_assert(!CtorCompilesFor, int[2][3][2]>, "!CtorCompilesFor, int[2][3][2]>"); { const span s{std::addressof(arr3d[0]), 1}; EXPECT_TRUE(s.size() == 1); } AddressOverloaded ao_arr[5] = {}; { const span s{ao_arr}; EXPECT_TRUE(s.size() == 5); EXPECT_TRUE(s.data() == std::addressof(ao_arr[0])); } } TEST(span_test, from_dynamic_array_constructor) { double(*arr)[3][4] = new double[100][3][4]; { span s(&arr[0][0][0], 10); EXPECT_TRUE(s.size() == 10); EXPECT_TRUE(s.data() == &arr[0][0][0]); } delete[] arr; } template static constexpr bool ConversionCompilesFor = false; template static constexpr bool ConversionCompilesFor()(std::declval()))>> = true; TEST(span_test, from_std_array_constructor) { std::array arr = {1, 2, 3, 4}; { span s{arr}; EXPECT_TRUE(s.size() == arr.size()); EXPECT_TRUE(s.data() == arr.data()); span cs{arr}; EXPECT_TRUE(cs.size() == arr.size()); EXPECT_TRUE(cs.data() == arr.data()); } { span s{arr}; EXPECT_TRUE(s.size() == arr.size()); EXPECT_TRUE(s.data() == arr.data()); span cs{arr}; EXPECT_TRUE(cs.size() == arr.size()); EXPECT_TRUE(cs.data() == arr.data()); } { std::array empty_arr{}; span s{empty_arr}; EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.empty()); } std::array ao_arr{}; { span fs{ao_arr}; EXPECT_TRUE(fs.size() == ao_arr.size()); EXPECT_TRUE(ao_arr.data() == fs.data()); } static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); #if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L) // Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14". static_assert(!ConversionCompilesFor, std::array>, "!ConversionCompilesFor, std::array>"); #endif { auto get_an_array = []() -> std::array { return {1, 2, 3, 4}; }; auto take_a_span = [](span) {}; // try to take a temporary std::array static_assert(ConversionCompilesFor, std::array>, "ConversionCompilesFor, std::array>"); take_a_span(get_an_array()); } } TEST(span_test, from_const_std_array_constructor) { const std::array arr = {1, 2, 3, 4}; { span s{arr}; EXPECT_TRUE(s.size() == arr.size()); EXPECT_TRUE(s.data() == arr.data()); } { span s{arr}; EXPECT_TRUE(s.size() == arr.size()); EXPECT_TRUE(s.data() == arr.data()); } const std::array ao_arr{}; { span s{ao_arr}; EXPECT_TRUE(s.size() == ao_arr.size()); EXPECT_TRUE(s.data() == ao_arr.data()); } static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); static_assert(!CtorCompilesFor, std::array&>, "!CtorCompilesFor, std::array&>"); { auto get_an_array = []() -> const std::array { return {1, 2, 3, 4}; }; auto take_a_span = [](span s) { static_cast(s); }; // try to take a temporary std::array take_a_span(get_an_array()); } } TEST(span_test, from_std_array_const_constructor) { std::array arr = {1, 2, 3, 4}; { span s{arr}; EXPECT_TRUE(s.size() == arr.size()); EXPECT_TRUE(s.data() == arr.data()); } { span s{arr}; EXPECT_TRUE(s.size() == arr.size()); EXPECT_TRUE(s.data() == arr.data()); } static_assert(!CtorCompilesFor, const std::array&>, "!CtorCompilesFor, const std::array&>"); static_assert(!CtorCompilesFor, const std::array&>, "!CtorCompilesFor, const std::array&>"); static_assert(!CtorCompilesFor, const std::array&>, "!CtorCompilesFor, const std::array&>"); static_assert(!CtorCompilesFor, const std::array&>, "!CtorCompilesFor, const std::array&>"); } TEST(span_test, from_container_constructor) { std::vector v = {1, 2, 3}; const std::vector cv = v; { span s{v}; EXPECT_TRUE(s.size() == v.size()); EXPECT_TRUE(s.data() == v.data()); span cs{v}; EXPECT_TRUE(cs.size() == v.size()); EXPECT_TRUE(cs.data() == v.data()); } std::string str = "hello"; const std::string cstr = "hello"; { static_assert(CtorCompilesFor, std::string&> == (__cplusplus >= 201703L), "CtorCompilesFor, std::string&> == (__cplusplus >= 201703L)"); span cs{str}; EXPECT_TRUE(cs.size() == str.size()); EXPECT_TRUE(cs.data() == str.data()); } { static_assert(!CtorCompilesFor, const std::string&>, "!CtorCompilesFor, const std::string&>"); span cs{cstr}; EXPECT_TRUE(cs.size() == cstr.size()); EXPECT_TRUE(cs.data() == cstr.data()); } #if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L) // Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14". static_assert(!ConversionCompilesFor, std::vector>, "!ConversionCompilesFor, std::vector>"); #endif // !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L) { auto get_temp_vector = []() -> std::vector { return {}; }; auto use_span = [](span s) { static_cast(s); }; use_span(get_temp_vector()); } static_assert(!ConversionCompilesFor, std::string>, "!ConversionCompilesFor, std::string>"); { auto get_temp_string = []() -> std::string { return {}; }; auto use_span = [](span s) { static_cast(s); }; use_span(get_temp_string()); } static_assert(!ConversionCompilesFor, const std::vector>, "!ConversionCompilesFor, const std::vector>"); static_assert(!ConversionCompilesFor, const std::string>, "!ConversionCompilesFor, const std::string>"); { auto get_temp_string = []() -> const std::string { return {}; }; auto use_span = [](span s) { static_cast(s); }; use_span(get_temp_string()); } static_assert(!CtorCompilesFor, std::map&>, "!CtorCompilesFor, std::map&>"); } TEST(span_test, from_convertible_span_constructor) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. from_convertible_span_constructor"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { span avd; span avcd = avd; static_cast(avcd); } { std::array arr{}; span avd{arr}; span avcd = avd; static_cast(avcd); } { std::array arr{}; span avd{arr}; span avcd = avd; static_cast(avcd); } { std::array arr{}; span avd{arr}; span avcd{avd}; static_cast(avcd); } { std::array arr{}; span avd{arr}; using T = span; EXPECT_DEATH(T{avd}, expected); } { std::array arr{}; span avd{arr}; using T = span; EXPECT_DEATH(T{avd}, expected); } static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); static_assert(!ConversionCompilesFor, span&>, "!ConversionCompilesFor, span&>"); } TEST(span_test, copy_move_and_assignment) { span s1; EXPECT_TRUE(s1.empty()); int arr[] = {3, 4, 5}; span s2 = arr; EXPECT_TRUE(s2.size() == 3); EXPECT_TRUE(s2.data() == &arr[0]); s2 = s1; EXPECT_TRUE(s2.empty()); auto get_temp_span = [&]() -> span { return {&arr[1], 2}; }; auto use_span = [&](span s) { EXPECT_TRUE(s.size() == 2); EXPECT_TRUE(s.data() == &arr[1]); }; use_span(get_temp_span()); s1 = get_temp_span(); EXPECT_TRUE(s1.size() == 2); EXPECT_TRUE(s1.data() == &arr[1]); } TEST(span_test, first) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. first"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int arr[5] = {1, 2, 3, 4, 5}; { span av = arr; EXPECT_TRUE(av.first<2>().size() == 2); EXPECT_TRUE(av.first(2).size() == 2); } { span av = arr; EXPECT_TRUE(av.first<0>().size() == 0); EXPECT_TRUE(av.first(0).size() == 0); } { span av = arr; EXPECT_TRUE(av.first<5>().size() == 5); EXPECT_TRUE(av.first(5).size() == 5); } { span av = arr; #ifdef CONFIRM_COMPILATION_ERRORS (void) av.first<6>(); #endif EXPECT_DEATH(av.first(6), expected); } { span av; EXPECT_TRUE(av.first<0>().size() == 0); EXPECT_TRUE(av.first(0).size() == 0); } } TEST(span_test, last) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. last"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int arr[5] = {1, 2, 3, 4, 5}; { span av = arr; EXPECT_TRUE(av.last<2>().size() == 2); EXPECT_TRUE(av.last(2).size() == 2); } { span av = arr; EXPECT_TRUE(av.last<0>().size() == 0); EXPECT_TRUE(av.last(0).size() == 0); } { span av = arr; EXPECT_TRUE(av.last<5>().size() == 5); EXPECT_TRUE(av.last(5).size() == 5); } { span av = arr; #ifdef CONFIRM_COMPILATION_ERRORS (void) av.last<6>(); #endif EXPECT_DEATH(av.last(6), expected); } { span av; EXPECT_TRUE(av.last<0>().size() == 0); EXPECT_TRUE(av.last(0).size() == 0); } } TEST(span_test, subspan) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. subspan"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int arr[5] = {1, 2, 3, 4, 5}; { span av = arr; EXPECT_TRUE((av.subspan<2, 2>().size()) == 2); EXPECT_TRUE(decltype(av.subspan<2, 2>())::extent == 2); EXPECT_TRUE(av.subspan(2, 2).size() == 2); EXPECT_TRUE((av.subspan<2, 3>().size()) == 3); EXPECT_TRUE(decltype(av.subspan<2, 3>())::extent == 3); EXPECT_TRUE(av.subspan(2, 3).size() == 3); } { span av = arr; EXPECT_TRUE((av.subspan<0, 0>().size()) == 0); EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0); EXPECT_TRUE(av.subspan(0, 0).size() == 0); } { span av = arr; EXPECT_TRUE((av.subspan<0, 5>().size()) == 5); EXPECT_TRUE(decltype(av.subspan<0, 5>())::extent == 5); EXPECT_TRUE(av.subspan(0, 5).size() == 5); #ifdef CONFIRM_COMPILATION_ERRORS (void) av.subspan<0, 6>(); (void) av.subspan<1, 5>(); #endif EXPECT_DEATH(av.subspan(0, 6), expected); EXPECT_DEATH(av.subspan(1, 5), expected); } { span av = arr; EXPECT_TRUE((av.subspan<4, 0>().size()) == 0); EXPECT_TRUE(decltype(av.subspan<4, 0>())::extent == 0); EXPECT_TRUE(av.subspan(4, 0).size() == 0); EXPECT_TRUE((av.subspan<5, 0>().size()) == 0); EXPECT_TRUE(decltype(av.subspan<5, 0>())::extent == 0); EXPECT_TRUE(av.subspan(5, 0).size() == 0); #ifdef CONFIRM_COMPILATION_ERRORS (void) av.subspan<6, 0>(); #endif EXPECT_DEATH(av.subspan(6, 0), expected); } { span av = arr; EXPECT_TRUE(av.subspan<1>().size() == 4); EXPECT_TRUE(decltype(av.subspan<1>())::extent == 4); EXPECT_TRUE(av.subspan(1).size() == 4); } { span av; EXPECT_TRUE((av.subspan<0, 0>().size()) == 0); EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0); EXPECT_TRUE(av.subspan(0, 0).size() == 0); EXPECT_DEATH((av.subspan<1, 0>()), expected); EXPECT_DEATH((av.subspan(1, 0)), expected); } { span av; EXPECT_TRUE((av.subspan<0>().size()) == 0); EXPECT_TRUE(decltype(av.subspan<0>())::extent == dynamic_extent); EXPECT_TRUE(av.subspan(0).size() == 0); EXPECT_DEATH(av.subspan<1>(), expected); EXPECT_TRUE(decltype(av.subspan<1>())::extent == dynamic_extent); EXPECT_DEATH(av.subspan(1), expected); } { span av = arr; EXPECT_TRUE(av.subspan(0).size() == 5); EXPECT_TRUE(av.subspan<0>().size() == 5); EXPECT_TRUE(av.subspan(1).size() == 4); EXPECT_TRUE(av.subspan<1>().size() == 4); EXPECT_TRUE(av.subspan(4).size() == 1); EXPECT_TRUE(av.subspan<4>().size() == 1); EXPECT_TRUE(av.subspan(5).size() == 0); EXPECT_TRUE(av.subspan<5>().size() == 0); EXPECT_DEATH(av.subspan(6), expected); EXPECT_DEATH(av.subspan<6>(), expected); const auto av2 = av.subspan(1); for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast(i) + 2); const auto av3 = av.subspan<1>(); for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av3[i] == static_cast(i) + 2); } { span av = arr; EXPECT_TRUE(av.subspan(0).size() == 5); EXPECT_TRUE(av.subspan<0>().size() == 5); EXPECT_TRUE(av.subspan(1).size() == 4); EXPECT_TRUE(av.subspan<1>().size() == 4); EXPECT_TRUE(av.subspan(4).size() == 1); EXPECT_TRUE(av.subspan<4>().size() == 1); EXPECT_TRUE(av.subspan(5).size() == 0); EXPECT_TRUE(av.subspan<5>().size() == 0); EXPECT_DEATH(av.subspan(6), expected); #ifdef CONFIRM_COMPILATION_ERRORS EXPECT_DEATH(av.subspan<6>(), expected); #endif const auto av2 = av.subspan(1); for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast(i) + 2); const auto av3 = av.subspan<1>(); for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av3[i] == static_cast(i) + 2); } } TEST(span_test, iterator_default_init) { span::iterator it1; span::iterator it2; EXPECT_TRUE(it1 == it2); } TEST(span_test, iterator_comparisons) { int a[] = {1, 2, 3, 4}; { span s = a; span::iterator it = s.begin(); auto it2 = it + 1; EXPECT_TRUE(it == it); EXPECT_TRUE(it == s.begin()); EXPECT_TRUE(s.begin() == it); EXPECT_TRUE(it != it2); EXPECT_TRUE(it2 != it); EXPECT_TRUE(it != s.end()); EXPECT_TRUE(it2 != s.end()); EXPECT_TRUE(s.end() != it); EXPECT_TRUE(it < it2); EXPECT_TRUE(it <= it2); EXPECT_TRUE(it2 <= s.end()); EXPECT_TRUE(it < s.end()); EXPECT_TRUE(it2 > it); EXPECT_TRUE(it2 >= it); EXPECT_TRUE(s.end() > it2); EXPECT_TRUE(s.end() >= it2); } } TEST(span_test, incomparable_iterators) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. incomparable_iterators"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int a[] = {1, 2, 3, 4}; int b[] = {1, 2, 3, 4}; { span s = a; span s2 = b; #if (__cplusplus > 201402L) EXPECT_DEATH([[maybe_unused]] bool _ = (s.begin() == s2.begin()), expected); EXPECT_DEATH([[maybe_unused]] bool _ = (s.begin() <= s2.begin()), expected); #else EXPECT_DEATH(bool _ = (s.begin() == s2.begin()), expected); EXPECT_DEATH(bool _ = (s.begin() <= s2.begin()), expected); #endif } } TEST(span_test, begin_end) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. begin_end"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { int a[] = {1, 2, 3, 4}; span s = a; span::iterator it = s.begin(); span::iterator it2 = std::begin(s); EXPECT_TRUE(it == it2); it = s.end(); it2 = std::end(s); EXPECT_TRUE(it == it2); } { int a[] = {1, 2, 3, 4}; span s = a; auto it = s.begin(); auto first = it; EXPECT_TRUE(it == first); EXPECT_TRUE(*it == 1); auto beyond = s.end(); EXPECT_TRUE(it != beyond); EXPECT_DEATH(*beyond, expected); EXPECT_TRUE(beyond - first == 4); EXPECT_TRUE(first - first == 0); EXPECT_TRUE(beyond - beyond == 0); ++it; EXPECT_TRUE(it - first == 1); EXPECT_TRUE(*it == 2); *it = 22; EXPECT_TRUE(*it == 22); EXPECT_TRUE(beyond - it == 3); it = first; EXPECT_TRUE(it == first); while (it != s.end()) { *it = 5; ++it; } EXPECT_TRUE(it == beyond); EXPECT_TRUE(it - beyond == 0); for (const auto& n : s) { EXPECT_TRUE(n == 5); } } } TEST(span_test, rbegin_rend) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. rbegin_rend"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); { int a[] = {1, 2, 3, 4}; span s = a; auto it = s.rbegin(); auto first = it; EXPECT_TRUE(it == first); EXPECT_TRUE(*it == 4); auto beyond = s.rend(); EXPECT_TRUE(it != beyond); #if (__cplusplus > 201402L) EXPECT_DEATH([[maybe_unused]] auto _ = *beyond, expected); #else EXPECT_DEATH(auto _ = *beyond, expected); #endif EXPECT_TRUE(beyond - first == 4); EXPECT_TRUE(first - first == 0); EXPECT_TRUE(beyond - beyond == 0); ++it; EXPECT_TRUE(it - s.rbegin() == 1); EXPECT_TRUE(*it == 3); *it = 22; EXPECT_TRUE(*it == 22); EXPECT_TRUE(beyond - it == 3); it = first; EXPECT_TRUE(it == first); while (it != s.rend()) { *it = 5; ++it; } EXPECT_TRUE(it == beyond); EXPECT_TRUE(it - beyond == 0); for (const auto& n : s) { EXPECT_TRUE(n == 5); } } } template static constexpr bool AsWritableBytesCompilesFor = false; template static constexpr bool AsWritableBytesCompilesFor()))>> = true; TEST(span_test, as_bytes) { int a[] = {1, 2, 3, 4}; static_assert(AsWritableBytesCompilesFor>, "AsWriteableBytesCompilesFor>"); // you should not be able to get writeable bytes for const objects static_assert(!AsWritableBytesCompilesFor>, "!AsWriteableBytesCompilesFor>"); { const span s = a; EXPECT_TRUE(s.size() == 4); const span bs = as_bytes(s); EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); EXPECT_TRUE(bs.size() == s.size_bytes()); } { span s; const auto bs = as_bytes(s); EXPECT_TRUE(bs.size() == s.size()); EXPECT_TRUE(bs.size() == 0); EXPECT_TRUE(bs.size_bytes() == 0); EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); EXPECT_TRUE(bs.data() == nullptr); } { span s = a; const auto bs = as_bytes(s); EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); EXPECT_TRUE(bs.size() == s.size_bytes()); } } TEST(span_test, as_writable_bytes) { int a[] = {1, 2, 3, 4}; { span s; const auto bs = as_writable_bytes(s); EXPECT_TRUE(bs.size() == s.size()); EXPECT_TRUE(bs.size() == 0); EXPECT_TRUE(bs.size_bytes() == 0); EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); EXPECT_TRUE(bs.data() == nullptr); } { span s = a; const auto bs = as_writable_bytes(s); EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); EXPECT_TRUE(bs.size() == s.size_bytes()); } } TEST(span_test, fixed_size_conversions) { const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. fixed_size_conversions"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); int arr[] = {1, 2, 3, 4}; // converting to an span from an equal size array is ok span s4 = arr; EXPECT_TRUE(s4.size() == 4); // converting to dynamic_range is always ok { span s = s4; EXPECT_TRUE(s.size() == s4.size()); static_cast(s); } // initialization or assignment to static span that REDUCES size is NOT ok static_assert(!ConversionCompilesFor, int[4]>, "!ConversionCompilesFor, int[4]>"); static_assert(!ConversionCompilesFor, span>, "!ConversionCompilesFor, span>"); // even when done dynamically static_assert(!ConversionCompilesFor, span>, "!ConversionCompilesFor, span>"); // but doing so explicitly is ok // you can convert statically { const span s2{&arr[0], 2}; static_cast(s2); } { const span s1 = s4.first<1>(); static_cast(s1); } // this is not a legal operation in std::span, so we are no longer supporting it // conversion from span to span via call to `first` // then convert from span to span // The dynamic to fixed extents are not supported in the standard // to make this work, span would need to be span. static_assert(!ConversionCompilesFor, span>, "!ConversionCompilesFor, span>"); // initialization or assignment to static span that requires size INCREASE is not ok. int arr2[2] = {1, 2}; static_assert(!ConversionCompilesFor, int[2]>, "!ConversionCompilesFor, int[2]>"); static_assert(!ConversionCompilesFor, int[2]>, "!ConversionCompilesFor, int[2]>"); static_assert(!ConversionCompilesFor, span>, "!ConversionCompilesFor, span>"); { auto f = [&]() { const span _s4{arr2, 2}; static_cast(_s4); }; EXPECT_DEATH(f(), expected); } // This no longer compiles. There is no suitable conversion from dynamic span to a fixed size // span. // this should fail - we are trying to assign a small dynamic span to a fixed_size larger one static_assert(!ConversionCompilesFor, span>, "!ConversionCompilesFor, span>"); } TEST(span_test, interop_with_std_regex) { char lat[] = {'1', '2', '3', '4', '5', '6', 'E', 'F', 'G'}; span s = lat; const auto f_it = s.begin() + 7; std::match_results::iterator> match; std::regex_match(s.begin(), s.end(), match, std::regex(".*")); EXPECT_TRUE(match.ready()); EXPECT_FALSE(match.empty()); EXPECT_TRUE(match[0].matched); EXPECT_TRUE(match[0].first == s.begin()); EXPECT_TRUE(match[0].second == s.end()); std::regex_search(s.begin(), s.end(), match, std::regex("F")); EXPECT_TRUE(match.ready()); EXPECT_FALSE(match.empty()); EXPECT_TRUE(match[0].matched); EXPECT_TRUE(match[0].first == f_it); EXPECT_TRUE(match[0].second == (f_it + 1)); } TEST(span_test, default_constructible) { EXPECT_TRUE((std::is_default_constructible>::value)); EXPECT_TRUE((std::is_default_constructible>::value)); EXPECT_FALSE((std::is_default_constructible>::value)); } TEST(span_test, std_container_ctad) { #if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L)) // this test is just to verify that these compile { std::vector v{1, 2, 3, 4}; gsl::span sp{v}; static_assert(std::is_same>::value); } { std::string str{"foo"}; gsl::span sp{str}; static_assert(std::is_same>::value); } #ifdef HAS_STRING_VIEW { std::string_view sv{"foo"}; gsl::span sp{sv}; static_assert(std::is_same>::value); } #endif #endif } TEST(span_test, front_back) { int arr[5] = {1, 2, 3, 4, 5}; span s{arr}; EXPECT_TRUE(s.front() == 1); EXPECT_TRUE(s.back() == 5); const auto terminateHandler = std::set_terminate([] { std::cerr << "Expected Death. front_back"; std::abort(); }); const auto expected = GetExpectedDeathString(terminateHandler); span s2; EXPECT_DEATH(s2.front(), expected); EXPECT_DEATH(s2.back(), expected); } #if defined(FORCE_STD_SPAN_TESTS) || defined(__cpp_lib_span) && __cpp_lib_span >= 202002L TEST(span_test, std_span) { // make sure std::span can be constructed from gsl::span int arr[5] = {1, 2, 3, 4, 5}; gsl::span gsl_span{arr}; #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts)) EXPECT_TRUE(std::to_address(gsl_span.begin()) == gsl_span.data()); EXPECT_TRUE(std::to_address(gsl_span.end()) == gsl_span.data() + gsl_span.size()); #endif // __cpp_lib_ranges std::span std_span = gsl_span; EXPECT_TRUE(std_span.data() == gsl_span.data()); EXPECT_TRUE(std_span.size() == gsl_span.size()); } #endif // defined(FORCE_STD_SPAN_TESTS) || defined(__cpp_lib_span) && __cpp_lib_span >= 202002L #if defined(__cpp_lib_span) && defined(__cpp_lib_ranges) // This test covers the changes in PR #1100 TEST(span_test, msvc_compile_error_PR1100) { int arr[]{1, 7, 2, 9}; gsl::span sp{arr, std::size(arr)}; std::ranges::sort(sp); for (const auto& e : sp) { (void) e; } } #endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges)