2016-02-24 14:26:28 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// blanket turn off warnings from CppCoreCheck from catch
|
|
|
|
// so people aren't annoyed by them when running the tool.
|
|
|
|
#pragma warning(disable : 26440 26426 26497) // from catch
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <gsl/gsl_byte> // for byte
|
|
|
|
#include <gsl/gsl_util> // for narrow_cast, at
|
|
|
|
#include <gsl/span> // for span, span_iterator, operator==, operator!=
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-11-28 10:13:49 -05:00
|
|
|
#include <array> // for array
|
|
|
|
#include <iostream> // for ptrdiff_t
|
|
|
|
#include <iterator> // for reverse_iterator, operator-, operator==
|
|
|
|
#include <memory> // for unique_ptr, shared_ptr, make_unique, allo...
|
|
|
|
#include <regex> // for match_results, sub_match, match_results<>...
|
|
|
|
#include <stddef.h> // for ptrdiff_t
|
|
|
|
#include <string> // for string
|
|
|
|
#include <type_traits> // for integral_constant<>::value, is_default_co...
|
|
|
|
#include <vector> // for vector
|
|
|
|
|
|
|
|
namespace gsl {
|
|
|
|
struct fail_fast;
|
|
|
|
} // namespace gsl
|
2016-02-24 14:26:28 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace gsl;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct BaseClass
|
|
|
|
{
|
|
|
|
};
|
|
|
|
struct DerivedClass : BaseClass
|
|
|
|
{
|
|
|
|
};
|
2018-08-19 20:10:53 -04:00
|
|
|
struct AddressOverloaded
|
|
|
|
{
|
|
|
|
AddressOverloaded operator&() const { return {}; }
|
|
|
|
};
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("default_constructor")
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
span<int> s;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int> cs;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((cs.size() == 0 && cs.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int, 0> cs;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((cs.size() == 0 && cs.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
span<int, 1> s;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 1 && s.data() == nullptr)); // explains why it can't compile
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int> cs{};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((cs.size() == 0 && cs.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("size_optimization")
|
|
|
|
{
|
2016-02-28 03:50:53 -05:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
span<int> s;
|
|
|
|
CHECK(sizeof(s) == sizeof(int*) + sizeof(ptrdiff_t));
|
|
|
|
}
|
2016-02-28 03:50:53 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s;
|
|
|
|
CHECK(sizeof(s) == sizeof(int*));
|
2016-02-28 03:50:53 -05:00
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-28 03:50:53 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2018-02-10 21:58:28 -05:00
|
|
|
TEST_CASE("from_nullptr_size_constructor")
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
span<int> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
span<const int> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((cs.size() == 0 && cs.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
span<int, 0> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
span<const int, 0> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((cs.size() == 0 && cs.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto workaround_macro = []() {
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 1> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2017-07-13 16:53:56 -04:00
|
|
|
};
|
|
|
|
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
auto workaround_macro = []() { const span<int> s{nullptr, 1}; };
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
auto const_workaround_macro = []() { const span<const int> cs{nullptr, 1}; };
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(const_workaround_macro(), fail_fast);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
auto workaround_macro = []() { const span<int, 0> s{nullptr, 1}; };
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
auto const_workaround_macro = []() { const span<const int, 0> s{nullptr, 1}; };
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(const_workaround_macro(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
span<int*> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
span<const int*> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((cs.size() == 0 && cs.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_pointer_length_constructor")
|
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2017-10-31 13:11:45 -04:00
|
|
|
for(int i = 0; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
span<int> s = { &arr[0], i };
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(s.size() == i);
|
2017-10-31 13:11:45 -04:00
|
|
|
CHECK(s.data() == &arr[0]);
|
|
|
|
CHECK(s.empty() == (i == 0));
|
|
|
|
for (int j = 0; j < i; ++j)
|
|
|
|
{
|
|
|
|
CHECK(arr[j] == s[j]);
|
|
|
|
CHECK(arr[j] == s.at(j));
|
|
|
|
CHECK(arr[j] == s(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
span<int> s = { &arr[i], 4-narrow_cast<ptrdiff_t>(i) };
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(s.size() == 4-i);
|
2017-10-31 13:11:45 -04:00
|
|
|
CHECK(s.data() == &arr[i]);
|
|
|
|
CHECK(s.empty() == (4-i == 0));
|
|
|
|
for (int j = 0; j < 4-i; ++j)
|
|
|
|
{
|
|
|
|
CHECK(arr[j+i] == s[j]);
|
|
|
|
CHECK(arr[j+i] == s.at(j));
|
|
|
|
CHECK(arr[j+i] == s(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 2> s{&arr[0], 2};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 2 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[1] == 2));
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int* p = nullptr;
|
2018-08-13 00:44:17 -04:00
|
|
|
span<int> s{p, narrow_cast<span<int>::index_type>(0)};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int* p = nullptr;
|
2018-08-13 00:44:17 -04:00
|
|
|
auto workaround_macro = [=]() { const span<int> s{p, 2}; };
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto s = make_span(&arr[0], 2);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 2 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[1] == 2));
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
int* p = nullptr;
|
2018-08-13 00:44:17 -04:00
|
|
|
auto s = make_span(p, narrow_cast<span<int>::index_type>(0));
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
auto workaround_macro = [=]() { make_span(p, 2); };
|
|
|
|
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
|
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_pointer_pointer_constructor")
|
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{&arr[0], &arr[2]};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 2 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[1] == 2));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 2> s{&arr[0], &arr[2]};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 2 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[1] == 2));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{&arr[0], &arr[0]};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s{&arr[0], &arr[0]};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
|
|
|
|
//{
|
|
|
|
// auto workaround_macro = [&]() { span<int> s{&arr[1], &arr[0]}; };
|
|
|
|
// CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
|
|
|
//}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
|
|
|
|
//{
|
|
|
|
// int* p = nullptr;
|
|
|
|
// auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
|
|
|
|
// CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
|
|
|
//}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
span<int> s{p, p};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
span<int, 0> s{p, p};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
|
|
|
|
//{
|
|
|
|
// int* p = nullptr;
|
|
|
|
// auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
|
|
|
|
// CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
|
|
|
//}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto s = make_span(&arr[0], &arr[2]);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 2 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[1] == 2));
|
2016-02-25 14:42:26 -05:00
|
|
|
}
|
2016-02-28 03:50:53 -05:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
auto s = make_span(&arr[0], &arr[0]);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
auto s = make_span(p, p);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == nullptr));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_array_constructor")
|
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
|
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const span<int> s{arr};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 5 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const span<int, 5> s{arr};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 5 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
|
2016-02-28 03:50:53 -05:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 6> s{arr};
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s{arr};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == &arr[0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{arr2d};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 6 && s.data() == &arr2d[0][0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[5] == 6));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s{arr2d};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == &arr2d[0][0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 6> s{arr2d};
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const span<int[3]> s{std::addressof(arr2d[0]), 1};
|
|
|
|
CHECK((s.size() == 1 && s.data() == std::addressof(arr2d[0])));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-02-28 03:50:53 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{arr3d};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 12 && s.data() == &arr3d[0][0][0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[11] == 12));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s{arr3d};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 0 && s.data() == &arr3d[0][0][0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 11> s{arr3d};
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 12> s{arr3d};
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 12 && s.data() == &arr3d[0][0][0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((s[0] == 1 && s[5] == 6));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const span<int[3][2]> s{std::addressof(arr3d[0]), 1};
|
|
|
|
CHECK((s.size() == 1 && s.data() == std::addressof(arr3d[0])));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const auto s = make_span(arr);
|
|
|
|
CHECK((s.size() == 5 && s.data() == std::addressof(arr[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto s = make_span(std::addressof(arr2d[0]), 1);
|
|
|
|
CHECK((s.size() == 1 && s.data() == std::addressof(arr2d[0])));
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-02-29 16:16:48 -05:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const auto s = make_span(std::addressof(arr3d[0]), 1);
|
|
|
|
CHECK((s.size() == 1 && s.data() == std::addressof(arr3d[0])));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-19 20:10:53 -04:00
|
|
|
AddressOverloaded ao_arr[5] = {};
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2018-08-19 20:10:53 -04:00
|
|
|
const span<AddressOverloaded, 5> s{ao_arr};
|
|
|
|
CHECK((s.size() == 5 && s.data() == std::addressof(ao_arr[0])));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2018-08-19 20:10:53 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(i.11) // NO-FORMAT: attribute
|
|
|
|
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_dynamic_array_constructor")
|
|
|
|
{
|
|
|
|
double(*arr)[3][4] = new double[100][3][4];
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<double> s(&arr[0][0][0], 10);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 10 && s.data() == &arr[0][0][0]));
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
auto s = make_span(&arr[0][0][0], 10);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s.size() == 10 && s.data() == &arr[0][0][0]));
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
delete[] arr;
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
|
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_std_array_constructor")
|
|
|
|
{
|
|
|
|
std::array<int, 4> arr = {1, 2, 3, 4};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int> cs{arr};
|
|
|
|
CHECK((cs.size() == narrow_cast<ptrdiff_t>(arr.size()) && cs.data() == arr.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 4> s{arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
|
|
|
|
span<const int, 4> cs{arr};
|
|
|
|
CHECK((cs.size() == narrow_cast<ptrdiff_t>(arr.size()) && cs.data() == arr.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-11-05 18:39:41 -05:00
|
|
|
{
|
|
|
|
std::array<int, 0> empty_arr{};
|
|
|
|
span<int> s{empty_arr};
|
|
|
|
CHECK((s.size() == 0 && s.empty()));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<AddressOverloaded, 4> ao_arr{};
|
2018-08-19 20:10:53 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
span<AddressOverloaded, 4> fs{ao_arr};
|
|
|
|
CHECK((fs.size() == narrow_cast<ptrdiff_t>(ao_arr.size()) && ao_arr.data() == fs.data()));
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:16:48 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 2> s{arr};
|
|
|
|
CHECK((s.size() == 2 && s.data() == arr.data()));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int, 2> cs{arr};
|
|
|
|
CHECK((cs.size() == 2 && cs.data() == arr.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 0> s{arr};
|
|
|
|
CHECK((s.size() == 0 && s.data() == arr.data()));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int, 0> cs{arr};
|
|
|
|
CHECK((cs.size() == 0 && cs.data() == arr.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int, 5> s{arr};
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
|
|
|
|
auto take_a_span = [](span<int> s) { static_cast<void>(s); };
|
|
|
|
// try to take a temporary std::array
|
|
|
|
take_a_span(get_an_array());
|
|
|
|
}
|
2016-02-29 16:16:48 -05:00
|
|
|
#endif
|
2016-06-14 23:14:17 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
|
|
|
|
auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
|
|
|
|
// try to take a temporary std::array
|
|
|
|
take_a_span(get_an_array());
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
auto s = make_span(arr);
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
2018-07-13 20:19:09 -04:00
|
|
|
|
2018-11-27 20:28:57 -05:00
|
|
|
// This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590
|
2018-07-13 20:19:09 -04:00
|
|
|
{
|
|
|
|
span<int> s1 = make_span(arr);
|
|
|
|
|
|
|
|
static span<int> s2;
|
|
|
|
s2 = s1;
|
|
|
|
|
2018-11-27 20:28:57 -05:00
|
|
|
#if __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && __GNUC_PATCHLEVEL__ == 0 && \
|
|
|
|
defined(__OPTIMIZE__)
|
|
|
|
// Known to be broken in gcc 6.4 and 6.5 with optimizations
|
2018-07-13 20:19:09 -04:00
|
|
|
// Issue in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116
|
|
|
|
CHECK(s1.size() == 4);
|
|
|
|
CHECK(s2.size() == 0);
|
|
|
|
#else
|
|
|
|
CHECK(s1.size() == s2.size());
|
|
|
|
#endif
|
|
|
|
}
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_const_std_array_constructor")
|
|
|
|
{
|
|
|
|
const std::array<int, 4> arr = {1, 2, 3, 4};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<const int> s{arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<const int, 4> s{arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
2016-09-12 21:51:23 -04:00
|
|
|
|
2018-08-19 20:10:53 -04:00
|
|
|
const std::array<AddressOverloaded, 4> ao_arr{};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<const AddressOverloaded, 4> s{ao_arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(ao_arr.size()) && s.data() == ao_arr.data()));
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:16:48 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<const int, 2> s{arr};
|
|
|
|
CHECK((s.size() == 2 && s.data() == arr.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<const int, 0> s{arr};
|
|
|
|
CHECK((s.size() == 0 && s.data() == arr.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<const int, 5> s{arr};
|
|
|
|
}
|
2016-09-12 21:51:23 -04:00
|
|
|
#endif
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; };
|
|
|
|
auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
|
|
|
|
// try to take a temporary std::array
|
|
|
|
take_a_span(get_an_array());
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-03-01 15:11:41 -05:00
|
|
|
|
2016-06-14 23:14:17 -04:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
auto s = make_span(arr);
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 23:14:17 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_std_array_const_constructor")
|
|
|
|
{
|
|
|
|
std::array<const int, 4> arr = {1, 2, 3, 4};
|
2016-06-14 23:14:17 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<const int> s{arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<const int, 4> s{arr};
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
2016-09-12 21:51:23 -04:00
|
|
|
|
2016-06-14 23:14:17 -04:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<const int, 2> s{arr};
|
|
|
|
CHECK((s.size() == 2 && s.data() == arr.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<const int, 0> s{arr};
|
|
|
|
CHECK((s.size() == 0 && s.data() == arr.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<const int, 5> s{arr};
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 4> s{arr};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
auto s = make_span(arr);
|
|
|
|
CHECK((s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_container_constructor")
|
|
|
|
{
|
|
|
|
std::vector<int> v = {1, 2, 3};
|
|
|
|
const std::vector<int> cv = v;
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s{v};
|
|
|
|
CHECK((s.size() == narrow_cast<std::ptrdiff_t>(v.size()) && s.data() == v.data()));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const int> cs{v};
|
|
|
|
CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(v.size()) && cs.data() == v.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
std::string str = "hello";
|
|
|
|
const std::string cstr = "hello";
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
span<char> s{str};
|
|
|
|
CHECK((s.size() == narrow_cast<std::ptrdiff_t>(str.size()) && s.data() == str.data()));
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const char> cs{str};
|
|
|
|
CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(str.size()) && cs.data() == str.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
span<char> s{cstr};
|
2017-01-28 03:08:48 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
span<const char> cs{cstr};
|
|
|
|
CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(cstr.size()) &&
|
|
|
|
cs.data() == cstr.data()));
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
auto get_temp_vector = []() -> std::vector<int> { return {}; };
|
|
|
|
auto use_span = [](span<int> s) { static_cast<void>(s); };
|
|
|
|
use_span(get_temp_vector());
|
2017-01-28 03:08:48 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto get_temp_vector = []() -> std::vector<int> { return {}; };
|
|
|
|
auto use_span = [](span<const int> s) { static_cast<void>(s); };
|
|
|
|
use_span(get_temp_vector());
|
|
|
|
}
|
2016-03-17 20:20:33 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
auto get_temp_string = []() -> std::string { return {}; };
|
|
|
|
auto use_span = [](span<char> s) { static_cast<void>(s); };
|
|
|
|
use_span(get_temp_string());
|
2017-01-28 03:08:48 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto get_temp_string = []() -> std::string { return {}; };
|
|
|
|
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
|
|
|
use_span(get_temp_string());
|
|
|
|
}
|
2016-03-17 20:20:33 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
auto get_temp_vector = []() -> const std::vector<int> { return {}; };
|
|
|
|
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
|
|
|
use_span(get_temp_vector());
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto get_temp_string = []() -> const std::string { return {}; };
|
|
|
|
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
|
|
|
use_span(get_temp_string());
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-07-13 16:53:56 -04:00
|
|
|
std::map<int, int> m;
|
|
|
|
span<int> s{m};
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
auto s = make_span(v);
|
|
|
|
CHECK((s.size() == narrow_cast<std::ptrdiff_t>(v.size()) && s.data() == v.data()));
|
2016-11-16 13:17:04 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
auto cs = make_span(cv);
|
|
|
|
CHECK((cs.size() == narrow_cast<std::ptrdiff_t>(cv.size()) && cs.data() == cv.data()));
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("from_convertible_span_constructor")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<DerivedClass> avd;
|
|
|
|
span<const DerivedClass> avcd = avd;
|
|
|
|
static_cast<void>(avcd);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
span<DerivedClass> avd;
|
|
|
|
span<BaseClass> avb = avd;
|
|
|
|
static_cast<void>(avb);
|
|
|
|
#endif
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
span<unsigned int> s2 = s;
|
|
|
|
static_cast<void>(s2);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
span<const unsigned int> s2 = s;
|
|
|
|
static_cast<void>(s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
span<short> s2 = s;
|
|
|
|
static_cast<void>(s2);
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-03-17 20:20:33 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("copy_move_and_assignment")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> s1;
|
|
|
|
CHECK(s1.empty());
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
int arr[] = {3, 4, 5};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
span<const int> s2 = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s2.size() == 3 && s2.data() == &arr[0]));
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
s2 = s1;
|
|
|
|
CHECK(s2.empty());
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto get_temp_span = [&]() -> span<int> { return {&arr[1], 2}; };
|
2018-02-10 21:58:28 -05:00
|
|
|
auto use_span = [&](span<const int> s) { CHECK((s.size() == 2 && s.data() == &arr[1])); };
|
2017-04-20 10:51:37 -04:00
|
|
|
use_span(get_temp_span());
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
s1 = get_temp_span();
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((s1.size() == 2 && s1.data() == &arr[1]));
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("first")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.first<2>().size() == 2);
|
|
|
|
CHECK(av.first(2).size() == 2);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.first<0>().size() == 0);
|
|
|
|
CHECK(av.first(0).size() == 0);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.first<5>().size() == 5);
|
|
|
|
CHECK(av.first(5).size() == 5);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.first<6>().size() == 6);
|
|
|
|
CHECK(av.first<-1>().size() == -1);
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK_THROWS_AS(av.first(6).size(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> av;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.first<0>().size() == 0);
|
|
|
|
CHECK(av.first(0).size() == 0);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("last")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.last<2>().size() == 2);
|
|
|
|
CHECK(av.last(2).size() == 2);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.last<0>().size() == 0);
|
|
|
|
CHECK(av.last(0).size() == 0);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.last<5>().size() == 5);
|
|
|
|
CHECK(av.last(5).size() == 5);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.last<6>().size() == 6);
|
2017-01-28 03:08:48 -05:00
|
|
|
#endif
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK_THROWS_AS(av.last(6).size(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> av;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.last<0>().size() == 0);
|
|
|
|
CHECK(av.last(0).size() == 0);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("subspan")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((av.subspan<2, 2>().size() == 2));
|
2018-03-03 22:12:45 -05:00
|
|
|
CHECK(decltype(av.subspan<2, 2>())::extent == 2);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(2, 2).size() == 2);
|
|
|
|
CHECK(av.subspan(2, 3).size() == 3);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-03-18 19:53:16 -04:00
|
|
|
|
2016-10-26 17:11:24 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((av.subspan<0, 0>().size() == 0));
|
2018-03-03 22:12:45 -05:00
|
|
|
CHECK(decltype(av.subspan<0,0>())::extent == 0);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(0, 0).size() == 0);
|
2016-10-26 17:11:24 -04:00
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((av.subspan<0, 5>().size() == 5));
|
2018-03-03 22:12:45 -05:00
|
|
|
CHECK(decltype(av.subspan<0, 5>())::extent == 5);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(0, 5).size() == 5);
|
2018-03-03 22:12:45 -05:00
|
|
|
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK_THROWS_AS(av.subspan(0, 6).size(), fail_fast);
|
|
|
|
CHECK_THROWS_AS(av.subspan(1, 5).size(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-05-29 16:52:28 -04:00
|
|
|
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((av.subspan<4, 0>().size() == 0));
|
2018-03-03 22:12:45 -05:00
|
|
|
CHECK(decltype(av.subspan<4, 0>())::extent == 0);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(4, 0).size() == 0);
|
|
|
|
CHECK(av.subspan(5, 0).size() == 0);
|
|
|
|
CHECK_THROWS_AS(av.subspan(6, 0).size(), fail_fast);
|
2016-05-29 16:52:28 -04:00
|
|
|
}
|
|
|
|
|
2018-03-03 22:12:45 -05:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK((av.subspan<1>().size() == 4));
|
|
|
|
CHECK(decltype(av.subspan<1>())::extent == 4);
|
|
|
|
}
|
|
|
|
|
2016-07-15 20:31:40 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> av;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK((av.subspan<0, 0>().size() == 0));
|
2018-03-03 22:12:45 -05:00
|
|
|
CHECK((decltype(av.subspan<0, 0>())::extent == 0));
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(0, 0).size() == 0);
|
|
|
|
CHECK_THROWS_AS((av.subspan<1, 0>().size()), fail_fast);
|
2016-07-15 20:31:40 -04:00
|
|
|
}
|
|
|
|
|
2016-08-08 15:06:47 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> av;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(0).size() == 0);
|
|
|
|
CHECK_THROWS_AS(av.subspan(1).size(), fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(0).size() == 5);
|
|
|
|
CHECK(av.subspan(1).size() == 4);
|
|
|
|
CHECK(av.subspan(4).size() == 1);
|
|
|
|
CHECK(av.subspan(5).size() == 0);
|
|
|
|
CHECK_THROWS_AS(av.subspan(6).size(), fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
const auto av2 = av.subspan(1);
|
|
|
|
for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2);
|
|
|
|
}
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(av.subspan(0).size() == 5);
|
|
|
|
CHECK(av.subspan(1).size() == 4);
|
|
|
|
CHECK(av.subspan(4).size() == 1);
|
|
|
|
CHECK(av.subspan(5).size() == 0);
|
|
|
|
CHECK_THROWS_AS(av.subspan(6).size(), fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
const auto av2 = av.subspan(1);
|
|
|
|
for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2);
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("at_call")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> s = arr;
|
|
|
|
CHECK(s.at(0) == 1);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(s.at(5), fail_fast);
|
2016-08-08 15:06:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int arr2d[2] = {1, 6};
|
|
|
|
span<int, 2> s = arr2d;
|
|
|
|
CHECK(s.at(0) == 1);
|
|
|
|
CHECK(s.at(1) == 6);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(s.at(2), fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("operator_function_call")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> s = arr;
|
|
|
|
CHECK(s(0) == 1);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(s(5), fail_fast);
|
2016-08-08 15:06:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-29 16:52:28 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int arr2d[2] = {1, 6};
|
|
|
|
span<int, 2> s = arr2d;
|
|
|
|
CHECK(s(0) == 1);
|
|
|
|
CHECK(s(1) == 6);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(s(2), fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-08 16:48:12 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("iterator_default_init")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int>::iterator it1;
|
|
|
|
span<int>::iterator it2;
|
|
|
|
CHECK(it1 == it2);
|
|
|
|
}
|
2016-08-08 16:48:12 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("const_iterator_default_init")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int>::const_iterator it1;
|
|
|
|
span<int>::const_iterator it2;
|
|
|
|
CHECK(it1 == it2);
|
|
|
|
}
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("iterator_conversions")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int>::iterator badIt;
|
|
|
|
span<int>::const_iterator badConstIt;
|
|
|
|
CHECK(badIt == badConstIt);
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto it = s.begin();
|
|
|
|
auto cit = s.cbegin();
|
2017-01-28 03:08:48 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(it == cit);
|
|
|
|
CHECK(cit == it);
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int>::const_iterator cit2 = it;
|
|
|
|
CHECK(cit2 == cit);
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int>::const_iterator cit3 = it + 4;
|
|
|
|
CHECK(cit3 == s.cend());
|
|
|
|
}
|
2016-05-29 16:52:28 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("iterator_comparisons")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
2016-07-18 14:38:01 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> s = a;
|
|
|
|
span<int>::iterator it = s.begin();
|
|
|
|
auto it2 = it + 1;
|
|
|
|
span<int>::const_iterator cit = s.cbegin();
|
2016-08-08 16:48:12 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(it == cit);
|
|
|
|
CHECK(cit == it);
|
|
|
|
CHECK(it == it);
|
|
|
|
CHECK(cit == cit);
|
|
|
|
CHECK(cit == s.begin());
|
|
|
|
CHECK(s.begin() == cit);
|
|
|
|
CHECK(s.cbegin() == cit);
|
|
|
|
CHECK(it == s.begin());
|
|
|
|
CHECK(s.begin() == it);
|
|
|
|
|
|
|
|
CHECK(it != it2);
|
|
|
|
CHECK(it2 != it);
|
|
|
|
CHECK(it != s.end());
|
|
|
|
CHECK(it2 != s.end());
|
|
|
|
CHECK(s.end() != it);
|
|
|
|
CHECK(it2 != cit);
|
|
|
|
CHECK(cit != it2);
|
|
|
|
|
|
|
|
CHECK(it < it2);
|
|
|
|
CHECK(it <= it2);
|
|
|
|
CHECK(it2 <= s.end());
|
|
|
|
CHECK(it < s.end());
|
|
|
|
CHECK(it <= cit);
|
|
|
|
CHECK(cit <= it);
|
|
|
|
CHECK(cit < it2);
|
|
|
|
CHECK(cit <= it2);
|
|
|
|
CHECK(cit < s.end());
|
|
|
|
CHECK(cit <= s.end());
|
|
|
|
|
|
|
|
CHECK(it2 > it);
|
|
|
|
CHECK(it2 >= it);
|
|
|
|
CHECK(s.end() > it2);
|
|
|
|
CHECK(s.end() >= it2);
|
|
|
|
CHECK(it2 > cit);
|
|
|
|
CHECK(it2 >= cit);
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 16:48:12 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("begin_end")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int>::iterator it = s.begin();
|
|
|
|
span<int>::iterator it2 = std::begin(s);
|
|
|
|
CHECK(it == it2);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
it = s.end();
|
|
|
|
it2 = std::end(s);
|
|
|
|
CHECK(it == it2);
|
|
|
|
}
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto it = s.begin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 1);
|
|
|
|
|
|
|
|
auto beyond = s.end();
|
|
|
|
CHECK(it != beyond);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(*beyond, fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
|
|
|
|
CHECK(beyond - first == 4);
|
|
|
|
CHECK(first - first == 0);
|
|
|
|
CHECK(beyond - beyond == 0);
|
|
|
|
|
|
|
|
++it;
|
|
|
|
CHECK(it - first == 1);
|
|
|
|
CHECK(*it == 2);
|
|
|
|
*it = 22;
|
|
|
|
CHECK(*it == 22);
|
|
|
|
CHECK(beyond - it == 3);
|
|
|
|
|
|
|
|
it = first;
|
|
|
|
CHECK(it == first);
|
|
|
|
while (it != s.end()) {
|
|
|
|
*it = 5;
|
2016-07-18 14:38:01 -04:00
|
|
|
++it;
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
for (const auto& n : s) {
|
|
|
|
CHECK(n == 5);
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("cbegin_cend")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
2016-07-18 14:38:01 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int>::const_iterator cit = s.cbegin();
|
|
|
|
span<int>::const_iterator cit2 = std::cbegin(s);
|
|
|
|
CHECK(cit == cit2);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
cit = s.cend();
|
|
|
|
cit2 = std::cend(s);
|
|
|
|
CHECK(cit == cit2);
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto it = s.cbegin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 1);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto beyond = s.cend();
|
|
|
|
CHECK(it != beyond);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(*beyond, fail_fast);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(beyond - first == 4);
|
|
|
|
CHECK(first - first == 0);
|
|
|
|
CHECK(beyond - beyond == 0);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
++it;
|
|
|
|
CHECK(it - first == 1);
|
|
|
|
CHECK(*it == 2);
|
|
|
|
CHECK(beyond - it == 3);
|
2016-08-08 15:06:47 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
int last = 0;
|
|
|
|
it = first;
|
|
|
|
CHECK(it == first);
|
|
|
|
while (it != s.cend()) {
|
|
|
|
CHECK(*it == last + 1);
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
last = *it;
|
|
|
|
++it;
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
|
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-07-18 14:38:01 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("rbegin_rend")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-05-29 17:05:09 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto it = s.rbegin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 4);
|
|
|
|
|
|
|
|
auto beyond = s.rend();
|
|
|
|
CHECK(it != beyond);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(*beyond, fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
|
|
|
|
CHECK(beyond - first == 4);
|
|
|
|
CHECK(first - first == 0);
|
|
|
|
CHECK(beyond - beyond == 0);
|
|
|
|
|
|
|
|
++it;
|
|
|
|
CHECK(it - first == 1);
|
|
|
|
CHECK(*it == 3);
|
|
|
|
*it = 22;
|
|
|
|
CHECK(*it == 22);
|
|
|
|
CHECK(beyond - it == 3);
|
|
|
|
|
|
|
|
it = first;
|
|
|
|
CHECK(it == first);
|
|
|
|
while (it != s.rend()) {
|
|
|
|
*it = 5;
|
|
|
|
++it;
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
for (const auto& n : s) {
|
|
|
|
CHECK(n == 5);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("crbegin_crend")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto it = s.crbegin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 4);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
auto beyond = s.crend();
|
|
|
|
CHECK(it != beyond);
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(*beyond, fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(beyond - first == 4);
|
|
|
|
CHECK(first - first == 0);
|
|
|
|
CHECK(beyond - beyond == 0);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
++it;
|
|
|
|
CHECK(it - first == 1);
|
|
|
|
CHECK(*it == 3);
|
|
|
|
CHECK(beyond - it == 3);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
it = first;
|
|
|
|
CHECK(it == first);
|
|
|
|
int last = 5;
|
|
|
|
while (it != s.crend()) {
|
|
|
|
CHECK(*it == last - 1);
|
|
|
|
last = *it;
|
|
|
|
|
|
|
|
++it;
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
|
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("comparison_operators")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
2016-05-29 20:06:29 -04:00
|
|
|
{
|
2018-02-10 22:21:22 -05:00
|
|
|
span<int> s1;
|
|
|
|
span<int> s2;
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(s1 == s2);
|
|
|
|
CHECK(!(s1 != s2));
|
|
|
|
CHECK(!(s1 < s2));
|
|
|
|
CHECK(s1 <= s2);
|
|
|
|
CHECK(!(s1 > s2));
|
|
|
|
CHECK(s1 >= s2);
|
|
|
|
CHECK(s2 == s1);
|
|
|
|
CHECK(!(s2 != s1));
|
|
|
|
CHECK(!(s2 < s1));
|
|
|
|
CHECK(s2 <= s1);
|
|
|
|
CHECK(!(s2 > s1));
|
|
|
|
CHECK(s2 >= s1);
|
|
|
|
}
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[] = {2, 1};
|
|
|
|
span<int> s1 = arr;
|
|
|
|
span<int> s2 = arr;
|
|
|
|
|
|
|
|
CHECK(s1 == s2);
|
|
|
|
CHECK(!(s1 != s2));
|
|
|
|
CHECK(!(s1 < s2));
|
|
|
|
CHECK(s1 <= s2);
|
|
|
|
CHECK(!(s1 > s2));
|
|
|
|
CHECK(s1 >= s2);
|
|
|
|
CHECK(s2 == s1);
|
|
|
|
CHECK(!(s2 != s1));
|
|
|
|
CHECK(!(s2 < s1));
|
|
|
|
CHECK(s2 <= s1);
|
|
|
|
CHECK(!(s2 > s1));
|
|
|
|
CHECK(s2 >= s1);
|
|
|
|
}
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[] = {2, 1}; // bigger
|
|
|
|
|
2018-02-10 22:21:22 -05:00
|
|
|
span<int> s1;
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> s2 = arr;
|
|
|
|
|
|
|
|
CHECK(s1 != s2);
|
|
|
|
CHECK(s2 != s1);
|
|
|
|
CHECK(!(s1 == s2));
|
|
|
|
CHECK(!(s2 == s1));
|
|
|
|
CHECK(s1 < s2);
|
|
|
|
CHECK(!(s2 < s1));
|
|
|
|
CHECK(s1 <= s2);
|
|
|
|
CHECK(!(s2 <= s1));
|
|
|
|
CHECK(s2 > s1);
|
|
|
|
CHECK(!(s1 > s2));
|
|
|
|
CHECK(s2 >= s1);
|
|
|
|
CHECK(!(s1 >= s2));
|
|
|
|
}
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr1[] = {1, 2};
|
|
|
|
int arr2[] = {1, 2};
|
|
|
|
span<int> s1 = arr1;
|
|
|
|
span<int> s2 = arr2;
|
|
|
|
|
|
|
|
CHECK(s1 == s2);
|
|
|
|
CHECK(!(s1 != s2));
|
|
|
|
CHECK(!(s1 < s2));
|
|
|
|
CHECK(s1 <= s2);
|
|
|
|
CHECK(!(s1 > s2));
|
|
|
|
CHECK(s1 >= s2);
|
|
|
|
CHECK(s2 == s1);
|
|
|
|
CHECK(!(s2 != s1));
|
|
|
|
CHECK(!(s2 < s1));
|
|
|
|
CHECK(s2 <= s1);
|
|
|
|
CHECK(!(s2 > s1));
|
|
|
|
CHECK(s2 >= s1);
|
2016-05-29 20:06:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
int arr[] = {1, 2, 3};
|
|
|
|
|
|
|
|
span<int> s1 = {&arr[0], 2}; // shorter
|
|
|
|
span<int> s2 = arr; // longer
|
|
|
|
|
|
|
|
CHECK(s1 != s2);
|
|
|
|
CHECK(s2 != s1);
|
|
|
|
CHECK(!(s1 == s2));
|
|
|
|
CHECK(!(s2 == s1));
|
|
|
|
CHECK(s1 < s2);
|
|
|
|
CHECK(!(s2 < s1));
|
|
|
|
CHECK(s1 <= s2);
|
|
|
|
CHECK(!(s2 <= s1));
|
|
|
|
CHECK(s2 > s1);
|
|
|
|
CHECK(!(s1 > s2));
|
|
|
|
CHECK(s2 >= s1);
|
|
|
|
CHECK(!(s1 >= s2));
|
|
|
|
}
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr1[] = {1, 2}; // smaller
|
|
|
|
int arr2[] = {2, 1}; // bigger
|
|
|
|
|
|
|
|
span<int> s1 = arr1;
|
|
|
|
span<int> s2 = arr2;
|
|
|
|
|
|
|
|
CHECK(s1 != s2);
|
|
|
|
CHECK(s2 != s1);
|
|
|
|
CHECK(!(s1 == s2));
|
|
|
|
CHECK(!(s2 == s1));
|
|
|
|
CHECK(s1 < s2);
|
|
|
|
CHECK(!(s2 < s1));
|
|
|
|
CHECK(s1 <= s2);
|
|
|
|
CHECK(!(s2 <= s1));
|
|
|
|
CHECK(s2 > s1);
|
|
|
|
CHECK(!(s1 > s2));
|
|
|
|
CHECK(s2 >= s1);
|
|
|
|
CHECK(!(s1 >= s2));
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("as_bytes")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
2016-05-29 20:06:29 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
const span<const int> s = a;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(s.size() == 4);
|
2017-04-20 10:51:37 -04:00
|
|
|
const span<const byte> bs = as_bytes(s);
|
|
|
|
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(bs.size() == s.size_bytes());
|
2016-05-29 20:06:29 -04:00
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int> s;
|
|
|
|
const auto bs = as_bytes(s);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(bs.size() == s.size());
|
|
|
|
CHECK(bs.size() == 0);
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(bs.size_bytes() == 0);
|
|
|
|
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
|
|
|
CHECK(bs.data() == nullptr);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> s = a;
|
|
|
|
const auto bs = as_bytes(s);
|
|
|
|
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(bs.size() == s.size_bytes());
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("as_writeable_bytes")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-04-20 10:51:37 -04:00
|
|
|
// you should not be able to get writeable bytes for const objects
|
|
|
|
span<const int> s = a;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(s.size() == 4);
|
2017-04-20 10:51:37 -04:00
|
|
|
span<const byte> bs = as_writeable_bytes(s);
|
|
|
|
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(bs.size() == s.size_bytes());
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
const auto bs = as_writeable_bytes(s);
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(bs.size() == s.size());
|
|
|
|
CHECK(bs.size() == 0);
|
2017-04-20 10:51:37 -04:00
|
|
|
CHECK(bs.size_bytes() == 0);
|
|
|
|
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
|
|
|
CHECK(bs.data() == nullptr);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int> s = a;
|
|
|
|
const auto bs = as_writeable_bytes(s);
|
|
|
|
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(bs.size() == s.size_bytes());
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("fixed_size_conversions")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[] = {1, 2, 3, 4};
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// converting to an span from an equal size array is ok
|
|
|
|
span<int, 4> s4 = arr;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(s4.size() == 4);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// converting to dynamic_range is always ok
|
|
|
|
{
|
|
|
|
span<int> s = s4;
|
2018-02-10 21:58:28 -05:00
|
|
|
CHECK(s.size() == s4.size());
|
2017-04-20 10:51:37 -04:00
|
|
|
static_cast<void>(s);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// initialization or assignment to static span that REDUCES size is NOT ok
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
span<int, 2> s = arr;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
span<int, 2> s2 = s4;
|
|
|
|
static_cast<void>(s2);
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// even when done dynamically
|
|
|
|
{
|
|
|
|
span<int> s = arr;
|
2016-02-24 14:26:28 -05:00
|
|
|
auto f = [&]() {
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 2> s2 = s;
|
2017-04-20 10:51:37 -04:00
|
|
|
static_cast<void>(s2);
|
2016-02-24 14:26:28 -05:00
|
|
|
};
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(f(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-08-10 22:32:00 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// but doing so explicitly is ok
|
|
|
|
|
|
|
|
// you can convert statically
|
2016-08-10 22:32:00 -04:00
|
|
|
{
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 2> s2 = {&arr[0], 2};
|
2017-04-20 10:51:37 -04:00
|
|
|
static_cast<void>(s2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const span<int, 1> s1 = s4.first<1>();
|
|
|
|
static_cast<void>(s1);
|
|
|
|
}
|
2016-08-10 22:32:00 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// ...or dynamically
|
|
|
|
{
|
|
|
|
// NB: implicit conversion to span<int,1> from span<int>
|
|
|
|
span<int, 1> s1 = s4.first(1);
|
|
|
|
static_cast<void>(s1);
|
|
|
|
}
|
2016-08-10 22:32:00 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
// initialization or assignment to static span that requires size INCREASE is not ok.
|
|
|
|
int arr2[2] = {1, 2};
|
2016-08-10 22:32:00 -04:00
|
|
|
|
2017-04-20 10:51:37 -04:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
|
|
|
span<int, 4> s3 = arr2;
|
2016-08-10 22:32:00 -04:00
|
|
|
}
|
2016-08-23 04:30:06 -04:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
span<int, 2> s2 = arr2;
|
|
|
|
span<int, 4> s4a = s2;
|
2016-08-23 04:30:06 -04:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
#endif
|
2017-01-28 03:08:48 -05:00
|
|
|
{
|
2017-04-20 10:51:37 -04:00
|
|
|
auto f = [&]() {
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 4> _s4 = {arr2, 2};
|
2017-04-20 10:51:37 -04:00
|
|
|
static_cast<void>(_s4);
|
|
|
|
};
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(f(), fail_fast);
|
2017-01-28 03:08:48 -05:00
|
|
|
}
|
2017-04-20 10:51:37 -04:00
|
|
|
|
|
|
|
// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
|
|
|
|
span<int> av = arr2;
|
|
|
|
auto f = [&]() {
|
2018-08-13 00:44:17 -04:00
|
|
|
const span<int, 4> _s4 = av;
|
2017-04-20 10:51:37 -04:00
|
|
|
static_cast<void>(_s4);
|
|
|
|
};
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK_THROWS_AS(f(), fail_fast);
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("interop_with_std_regex")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
char lat[] = {'1', '2', '3', '4', '5', '6', 'E', 'F', 'G'};
|
|
|
|
span<char> s = lat;
|
|
|
|
const auto f_it = s.begin() + 7;
|
|
|
|
|
|
|
|
std::match_results<span<char>::iterator> match;
|
|
|
|
|
|
|
|
std::regex_match(s.begin(), s.end(), match, std::regex(".*"));
|
|
|
|
CHECK(match.ready());
|
|
|
|
CHECK(!match.empty());
|
|
|
|
CHECK(match[0].matched);
|
|
|
|
CHECK(match[0].first == s.begin());
|
|
|
|
CHECK(match[0].second == s.end());
|
|
|
|
|
|
|
|
std::regex_search(s.begin(), s.end(), match, std::regex("F"));
|
|
|
|
CHECK(match.ready());
|
|
|
|
CHECK(!match.empty());
|
|
|
|
CHECK(match[0].matched);
|
|
|
|
CHECK(match[0].first == f_it);
|
|
|
|
CHECK(match[0].second == (f_it + 1));
|
|
|
|
}
|
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("interop_with_gsl_at")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
span<int> s{arr};
|
2017-07-13 16:53:56 -04:00
|
|
|
CHECK((at(s, 0) == 1 && at(s, 1) == 2));
|
2017-04-20 10:51:37 -04:00
|
|
|
}
|
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
TEST_CASE("default_constructible")
|
2017-04-20 10:51:37 -04:00
|
|
|
{
|
|
|
|
CHECK((std::is_default_constructible<span<int>>::value));
|
|
|
|
CHECK((std::is_default_constructible<span<int, 0>>::value));
|
|
|
|
CHECK((!std::is_default_constructible<span<int, 42>>::value));
|
|
|
|
}
|
2018-08-13 00:44:17 -04:00
|
|
|
|