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.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <UnitTest++/UnitTest++.h>
|
|
|
|
#include <span.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace gsl;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct BaseClass
|
|
|
|
{
|
|
|
|
};
|
|
|
|
struct DerivedClass : BaseClass
|
|
|
|
{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
SUITE(span_tests)
|
|
|
|
{
|
|
|
|
TEST(default_constructor)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int> cs;
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s;
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int, 0> cs;
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
span<int, 1> s;
|
|
|
|
CHECK(s.length() == 1 && s.data() == nullptr); // explains why it can't compile
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{};
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int> cs{};
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 03:50:53 -05:00
|
|
|
TEST(size_optimization)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
CHECK(sizeof(s) == sizeof(int*) + sizeof(ptrdiff_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s;
|
|
|
|
CHECK(sizeof(s) == sizeof(int*));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(from_nullptr_constructor)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
span<int> s = nullptr;
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int> cs = nullptr;
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s = nullptr;
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int, 0> cs = nullptr;
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
span<int, 1> s = nullptr;
|
|
|
|
CHECK(s.length() == 1 && s.data() == nullptr); // explains why it can't compile
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{nullptr};
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int> cs{nullptr};
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int*> s{nullptr};
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
|
|
|
span<const int*> cs{nullptr};
|
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(from_nullptr_length_constructor)
|
|
|
|
{
|
|
|
|
{
|
2016-07-20 15:47:21 -04:00
|
|
|
span<int> s{nullptr, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
2016-07-20 15:47:21 -04:00
|
|
|
span<const int> cs{nullptr, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-07-20 15:47:21 -04:00
|
|
|
span<int, 0> s{nullptr, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
2016-07-20 15:47:21 -04:00
|
|
|
span<const int, 0> cs{nullptr, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-07-20 15:47:21 -04:00
|
|
|
auto workaround_macro = []() { span<int, 1> s{ nullptr, static_cast<span<int>::index_type>(0) }; };
|
2016-02-25 14:42:26 -05:00
|
|
|
CHECK_THROW(workaround_macro(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto workaround_macro = []() { span<int> s{nullptr, 1}; };
|
|
|
|
CHECK_THROW(workaround_macro(), fail_fast);
|
|
|
|
|
|
|
|
auto const_workaround_macro = []() { span<const int> cs{nullptr, 1}; };
|
|
|
|
CHECK_THROW(const_workaround_macro(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto workaround_macro = []() { span<int, 0> s{nullptr, 1}; };
|
|
|
|
CHECK_THROW(workaround_macro(), fail_fast);
|
|
|
|
|
|
|
|
auto const_workaround_macro = []() { span<const int, 0> s{nullptr, 1}; };
|
|
|
|
CHECK_THROW(const_workaround_macro(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-07-20 15:47:21 -04:00
|
|
|
span<int*> s{nullptr, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
|
2016-07-20 15:47:21 -04:00
|
|
|
span<const int*> cs{nullptr, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(cs.length() == 0 && cs.data() == nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(from_pointer_length_constructor)
|
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{&arr[0], 2};
|
|
|
|
CHECK(s.length() == 2 && s.data() == &arr[0]);
|
|
|
|
CHECK(s[0] == 1 && s[1] == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 2> s{&arr[0], 2};
|
|
|
|
CHECK(s.length() == 2 && s.data() == &arr[0]);
|
|
|
|
CHECK(s[0] == 1 && s[1] == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int* p = nullptr;
|
2016-07-20 15:47:21 -04:00
|
|
|
span<int> s{p, static_cast<span<int>::index_type>(0)};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int* p = nullptr;
|
|
|
|
auto workaround_macro = [=]() { span<int> s{p, 2}; };
|
|
|
|
CHECK_THROW(workaround_macro(), fail_fast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(from_pointer_pointer_constructor)
|
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{&arr[0], &arr[2]};
|
|
|
|
CHECK(s.length() == 2 && s.data() == &arr[0]);
|
|
|
|
CHECK(s[0] == 1 && s[1] == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 2> s{&arr[0], &arr[2]};
|
|
|
|
CHECK(s.length() == 2 && s.data() == &arr[0]);
|
|
|
|
CHECK(s[0] == 1 && s[1] == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{&arr[0], &arr[0]};
|
|
|
|
CHECK(s.length() == 0 && s.data() == &arr[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s{&arr[0], &arr[0]};
|
|
|
|
CHECK(s.length() == 0 && s.data() == &arr[0]);
|
|
|
|
}
|
|
|
|
|
2016-02-25 14:42:26 -05: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_THROW(workaround_macro(), fail_fast);
|
|
|
|
//}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-02-25 14:42:26 -05: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_THROW(workaround_macro(), fail_fast);
|
|
|
|
//}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
int* p = nullptr;
|
2016-02-25 14:42:26 -05:00
|
|
|
span<int> s{ p, p };
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int* p = nullptr;
|
2016-02-25 14:42:26 -05:00
|
|
|
span<int, 0> s{ p, p };
|
|
|
|
CHECK(s.length() == 0 && s.data() == nullptr);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
2016-02-25 14:42:26 -05: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_THROW(workaround_macro(), fail_fast);
|
|
|
|
//}
|
|
|
|
}
|
2016-02-28 03:50:53 -05:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(from_array_constructor)
|
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{arr};
|
|
|
|
CHECK(s.length() == 5 && s.data() == &arr[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> s{arr};
|
|
|
|
CHECK(s.length() == 5 && s.data() == &arr[0]);
|
|
|
|
}
|
|
|
|
|
2016-02-28 03:50:53 -05:00
|
|
|
int arr2d[2][3] = { 1, 2, 3, 4, 5, 6 };
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2016-02-28 03:50:53 -05:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
span<int, 6> s{arr};
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s{arr};
|
|
|
|
CHECK(s.length() == 0 && s.data() == &arr[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{arr2d};
|
|
|
|
CHECK(s.length() == 6 && s.data() == &arr2d[0][0]);
|
|
|
|
CHECK(s[0] == 1 && s[5] == 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s{arr2d};
|
|
|
|
CHECK(s.length() == 0 && s.data() == &arr2d[0][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-02-28 03:50:53 -05:00
|
|
|
span<int, 6> s{ arr2d };
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
2016-02-29 16:16:48 -05:00
|
|
|
span<int[3]> s{ &(arr2d[0]), 1 };
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(s.length() == 1 && s.data() == &arr2d[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
|
|
|
|
2016-02-28 03:50:53 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
|
|
|
span<int> s{arr3d};
|
|
|
|
CHECK(s.length() == 12 && s.data() == &arr3d[0][0][0]);
|
|
|
|
CHECK(s[0] == 1 && s[11] == 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s{arr3d};
|
|
|
|
CHECK(s.length() == 0 && s.data() == &arr3d[0][0][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 11> s{arr3d};
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 12> s{arr3d};
|
|
|
|
CHECK(s.length() == 12 && s.data() == &arr3d[0][0][0]);
|
|
|
|
CHECK(s[0] == 1 && s[5] == 6);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
2016-02-29 16:16:48 -05:00
|
|
|
span<int[3][2]> s{&arr3d[0], 1};
|
|
|
|
CHECK(s.length() == 1 && s.data() == &arr3d[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
|
|
|
TEST(from_dynamic_array_constructor)
|
|
|
|
{
|
|
|
|
double(*arr)[3][4] = new double[100][3][4];
|
|
|
|
|
|
|
|
{
|
2016-02-29 16:16:48 -05:00
|
|
|
span<double> s(&arr[0][0][0], 10);
|
|
|
|
CHECK(s.length() == 10 && s.data() == &arr[0][0][0]);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(from_std_array_constructor)
|
|
|
|
{
|
|
|
|
std::array<int, 4> arr = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{arr};
|
|
|
|
CHECK(s.size() == narrow_cast<ptrdiff_t>(arr.size()) && s.data() == arr.data());
|
|
|
|
|
|
|
|
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-29 16:16:48 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
|
|
|
span<int, 2> s{arr};
|
|
|
|
CHECK(s.size() == 2 && s.data() == arr.data());
|
|
|
|
|
|
|
|
span<const int, 2> cs{arr};
|
|
|
|
CHECK(cs.size() == 2 && cs.data() == arr.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 0> s{arr};
|
|
|
|
CHECK(s.size() == 0 && s.data() == arr.data());
|
|
|
|
|
|
|
|
span<const int, 0> cs{arr};
|
|
|
|
CHECK(cs.size() == 0 && cs.data() == arr.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> s{arr};
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-17 20:20:33 -04:00
|
|
|
auto get_an_array = []()->std::array<int, 4> { return{1, 2, 3, 4}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto take_a_span = [](span<int> s) { static_cast<void>(s); };
|
2016-02-24 14:26:28 -05:00
|
|
|
// 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
|
|
|
|
2016-03-17 20:20:33 -04:00
|
|
|
{
|
|
|
|
auto get_an_array = []() -> std::array<int, 4> { return { 1, 2, 3, 4 }; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
|
2016-03-17 20:20:33 -04:00
|
|
|
// try to take a temporary std::array
|
|
|
|
take_a_span(get_an_array());
|
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(from_const_std_array_constructor)
|
|
|
|
{
|
|
|
|
const std::array<int, 4> arr = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
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-02-29 16:16:48 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2016-02-24 14:26:28 -05: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};
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
|
2016-02-24 14:26:28 -05:00
|
|
|
// try to take a temporary std::array
|
|
|
|
take_a_span(get_an_array());
|
|
|
|
}
|
2016-02-29 16:16:48 -05:00
|
|
|
#endif
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-03-01 15:11:41 -05:00
|
|
|
|
2016-06-14 23:14:17 -04:00
|
|
|
TEST(from_std_array_const_constructor)
|
|
|
|
{
|
|
|
|
std::array<const int, 4> arr = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(from_container_constructor)
|
|
|
|
{
|
|
|
|
std::vector<int> v = {1, 2, 3};
|
|
|
|
const std::vector<int> cv = v;
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s{v};
|
|
|
|
CHECK(s.size() == narrow_cast<std::ptrdiff_t>(v.size()) && s.data() == v.data());
|
|
|
|
|
|
|
|
span<const int> cs{v};
|
|
|
|
CHECK(cs.size() == narrow_cast<std::ptrdiff_t>(v.size()) && cs.data() == v.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str = "hello";
|
|
|
|
const std::string cstr = "hello";
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
span<char> s{str};
|
|
|
|
CHECK(s.size() == narrow_cast<std::ptrdiff_t>(str.size()) && s.data() == str.data());
|
|
|
|
#endif
|
|
|
|
span<const char> cs{str};
|
|
|
|
CHECK(cs.size() == narrow_cast<std::ptrdiff_t>(str.size()) && cs.data() == str.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
span<char> s{cstr};
|
|
|
|
#endif
|
|
|
|
span<const char> cs{cstr};
|
|
|
|
CHECK(cs.size() == narrow_cast<std::ptrdiff_t>(cstr.size()) &&
|
|
|
|
cs.data() == cstr.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
auto get_temp_vector = []() -> std::vector<int> { return {}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto use_span = [](span<int> s) { static_cast<void>(s); };
|
2016-02-24 14:26:28 -05:00
|
|
|
use_span(get_temp_vector());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:20:33 -04:00
|
|
|
{
|
|
|
|
auto get_temp_vector = []() -> std::vector<int> { return{}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto use_span = [](span<const int> s) { static_cast<void>(s); };
|
2016-03-17 20:20:33 -04:00
|
|
|
use_span(get_temp_vector());
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2016-03-17 20:20:33 -04:00
|
|
|
auto get_temp_string = []() -> std::string { return{}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto use_span = [](span<char> s) { static_cast<void>(s); };
|
2016-02-24 14:26:28 -05:00
|
|
|
use_span(get_temp_string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:20:33 -04:00
|
|
|
{
|
|
|
|
auto get_temp_string = []() -> std::string { return {}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
2016-03-17 20:20:33 -04:00
|
|
|
use_span(get_temp_string());
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
auto get_temp_vector = []() -> const std::vector<int> { return {}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
2016-02-24 14:26:28 -05:00
|
|
|
use_span(get_temp_vector());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
auto get_temp_string = []() -> const std::string { return {}; };
|
2016-06-12 21:28:19 -04:00
|
|
|
auto use_span = [](span<const char> s) { static_cast<void>(s); };
|
2016-02-24 14:26:28 -05:00
|
|
|
use_span(get_temp_string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
std::map<int, int> m;
|
|
|
|
span<int> s{m};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2016-03-16 22:39:55 -04:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(from_convertible_span_constructor)
|
|
|
|
{
|
2016-03-16 22:39:55 -04:00
|
|
|
{
|
|
|
|
span<DerivedClass> avd;
|
|
|
|
span<const DerivedClass> avcd = avd;
|
2016-06-12 21:28:19 -04:00
|
|
|
static_cast<void>(avcd);
|
2016-03-16 22:39:55 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-03-16 22:39:55 -04:00
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
span<DerivedClass> avd;
|
|
|
|
span<BaseClass> avb = avd;
|
2016-06-12 21:28:19 -04:00
|
|
|
static_cast<void>(avb);
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2016-03-16 22:39:55 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-03-16 22:39:55 -04:00
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
span<unsigned int> s2 = s;
|
2016-06-12 21:28:19 -04:00
|
|
|
static_cast<void>(s2);
|
2016-03-16 22:39:55 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-03-16 22:39:55 -04:00
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
span<const unsigned int> s2 = s;
|
2016-06-12 21:28:19 -04:00
|
|
|
static_cast<void>(s2);
|
2016-03-16 22:39:55 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-03-16 22:39:55 -04:00
|
|
|
{
|
2016-02-24 14:26:28 -05:00
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
2016-03-16 22:39:55 -04:00
|
|
|
span<int> s;
|
|
|
|
span<short> s2 = s;
|
2016-06-12 21:28:19 -04:00
|
|
|
static_cast<void>(s2);
|
2016-02-24 14:26:28 -05:00
|
|
|
#endif
|
2016-03-16 22:39:55 -04:00
|
|
|
}
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
2016-03-17 20:20:33 -04:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(copy_move_and_assignment)
|
|
|
|
{
|
|
|
|
span<int> s1;
|
|
|
|
CHECK(s1.empty());
|
|
|
|
|
|
|
|
int arr[] = {3, 4, 5};
|
|
|
|
|
|
|
|
span<const int> s2 = arr;
|
|
|
|
CHECK(s2.length() == 3 && s2.data() == &arr[0]);
|
|
|
|
|
|
|
|
s2 = s1;
|
|
|
|
CHECK(s2.empty());
|
|
|
|
|
|
|
|
auto get_temp_span = [&]() -> span<int> { return {&arr[1], 2}; };
|
|
|
|
auto use_span = [&](span<const int> s) { CHECK(s.length() == 2 && s.data() == &arr[1]); };
|
|
|
|
use_span(get_temp_span());
|
|
|
|
|
|
|
|
s1 = get_temp_span();
|
|
|
|
CHECK(s1.length() == 2 && s1.data() == &arr[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(first)
|
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.first<2>().length() == 2);
|
|
|
|
CHECK(av.first(2).length() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.first<0>().length() == 0);
|
|
|
|
CHECK(av.first(0).length() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.first<5>().length() == 5);
|
|
|
|
CHECK(av.first(5).length() == 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
CHECK(av.first<6>().length() == 6);
|
|
|
|
CHECK(av.first<-1>().length() == -1);
|
|
|
|
#endif
|
|
|
|
CHECK_THROW(av.first(6).length(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-18 19:49:29 -04:00
|
|
|
span<int> av;
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(av.first<0>().length() == 0);
|
|
|
|
CHECK(av.first(0).length() == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(last)
|
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.last<2>().length() == 2);
|
|
|
|
CHECK(av.last(2).length() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.last<0>().length() == 0);
|
|
|
|
CHECK(av.last(0).length() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.last<5>().length() == 5);
|
|
|
|
CHECK(av.last(5).length() == 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
CHECK(av.last<6>().length() == 6);
|
|
|
|
#endif
|
|
|
|
CHECK_THROW(av.last(6).length(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-18 19:49:29 -04:00
|
|
|
span<int> av;
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(av.last<0>().length() == 0);
|
|
|
|
CHECK(av.last(0).length() == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(subspan)
|
|
|
|
{
|
|
|
|
int arr[5] = {1, 2, 3, 4, 5};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK((av.subspan<2, 2>().length() == 2));
|
|
|
|
CHECK(av.subspan(2, 2).length() == 2);
|
|
|
|
CHECK(av.subspan(2, 3).length() == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK((av.subspan<0, 0>().length() == 0));
|
|
|
|
CHECK(av.subspan(0, 0).length() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK((av.subspan<0, 5>().length() == 5));
|
|
|
|
CHECK(av.subspan(0, 5).length() == 5);
|
|
|
|
CHECK_THROW(av.subspan(0, 6).length(), fail_fast);
|
|
|
|
CHECK_THROW(av.subspan(1, 5).length(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
2016-03-18 19:49:29 -04:00
|
|
|
CHECK((av.subspan<4, 0>().length() == 0));
|
|
|
|
CHECK(av.subspan(4, 0).length() == 0);
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(av.subspan(5, 0).length() == 0);
|
|
|
|
CHECK_THROW(av.subspan(6, 0).length(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-18 19:49:29 -04:00
|
|
|
span<int> av;
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK((av.subspan<0, 0>().length() == 0));
|
|
|
|
CHECK(av.subspan(0, 0).length() == 0);
|
|
|
|
CHECK_THROW((av.subspan<1, 0>().length()), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> av;
|
|
|
|
CHECK(av.subspan(0).length() == 0);
|
|
|
|
CHECK_THROW(av.subspan(1).length(), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> av = arr;
|
|
|
|
CHECK(av.subspan(0).length() == 5);
|
|
|
|
CHECK(av.subspan(1).length() == 4);
|
|
|
|
CHECK(av.subspan(4).length() == 1);
|
|
|
|
CHECK(av.subspan(5).length() == 0);
|
|
|
|
CHECK_THROW(av.subspan(6).length(), fail_fast);
|
|
|
|
auto av2 = av.subspan(1);
|
|
|
|
for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int, 5> av = arr;
|
|
|
|
CHECK(av.subspan(0).length() == 5);
|
|
|
|
CHECK(av.subspan(1).length() == 4);
|
|
|
|
CHECK(av.subspan(4).length() == 1);
|
|
|
|
CHECK(av.subspan(5).length() == 0);
|
|
|
|
CHECK_THROW(av.subspan(6).length(), fail_fast);
|
|
|
|
auto av2 = av.subspan(1);
|
|
|
|
for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2);
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 19:53:16 -04:00
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(operator_function_call)
|
|
|
|
{
|
|
|
|
int arr[4] = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s = arr;
|
|
|
|
CHECK(s(0) == 1);
|
|
|
|
CHECK_THROW(s(5), fail_fast);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-03-18 19:53:16 -04:00
|
|
|
int arr2d[2] = {1, 6};
|
|
|
|
span<int, 2> s = arr2d;
|
|
|
|
CHECK(s(0) == 1);
|
|
|
|
CHECK(s(1) == 6);
|
|
|
|
CHECK_THROW(s(2) ,fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-29 16:52:28 -04:00
|
|
|
|
|
|
|
TEST(iterator)
|
|
|
|
{
|
|
|
|
span<int>::iterator it1;
|
|
|
|
span<int>::iterator it2;
|
|
|
|
CHECK(it1 == it2);
|
|
|
|
}
|
|
|
|
|
2016-07-15 20:31:40 -04:00
|
|
|
TEST(const_iterator)
|
|
|
|
{
|
|
|
|
span<int>::const_iterator it1;
|
|
|
|
span<int>::const_iterator it2;
|
|
|
|
CHECK(it1 == it2);
|
|
|
|
}
|
|
|
|
|
2016-05-29 16:52:28 -04:00
|
|
|
TEST(begin_end)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int a[] = { 1, 2, 3, 4 };
|
|
|
|
span<int> s = a;
|
|
|
|
|
2016-07-18 14:38:01 -04:00
|
|
|
auto it = s.begin();
|
2016-05-29 16:52:28 -04:00
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 1);
|
|
|
|
|
2016-07-18 14:38:01 -04:00
|
|
|
auto beyond = s.end();
|
2016-05-29 16:52:28 -04:00
|
|
|
CHECK(it != beyond);
|
|
|
|
CHECK_THROW(*beyond, fail_fast);
|
|
|
|
|
|
|
|
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;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
|
|
|
|
|
|
|
for (auto& n : s)
|
2016-07-29 14:16:06 -04:00
|
|
|
{
|
2016-05-29 16:52:28 -04:00
|
|
|
CHECK(n == 5);
|
2016-07-29 14:16:06 -04:00
|
|
|
}
|
2016-05-29 16:52:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 14:38:01 -04:00
|
|
|
TEST(cbegin_cend)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
|
|
|
|
|
|
|
auto it = s.cbegin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 1);
|
|
|
|
|
|
|
|
auto beyond = s.cend();
|
|
|
|
CHECK(it != beyond);
|
|
|
|
CHECK_THROW(*beyond, fail_fast);
|
|
|
|
|
|
|
|
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.cend())
|
|
|
|
{
|
|
|
|
*it = 5;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
|
|
|
|
|
|
|
for (auto& n : s)
|
2016-07-29 14:16:06 -04:00
|
|
|
{
|
2016-07-18 14:38:01 -04:00
|
|
|
CHECK(n == 5);
|
2016-07-29 14:16:06 -04:00
|
|
|
}
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(rbegin_rend)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
|
|
|
|
|
|
|
auto it = s.rbegin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 4);
|
|
|
|
|
|
|
|
auto beyond = s.rend();
|
|
|
|
CHECK(it != beyond);
|
|
|
|
CHECK_THROW(*beyond, fail_fast);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
|
|
|
|
|
|
|
for (auto& n : s)
|
2016-07-29 14:16:06 -04:00
|
|
|
{
|
2016-07-18 14:38:01 -04:00
|
|
|
CHECK(n == 5);
|
2016-07-29 14:16:06 -04:00
|
|
|
}
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(crbegin_crend)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
span<int> s = a;
|
|
|
|
|
|
|
|
auto it = s.crbegin();
|
|
|
|
auto first = it;
|
|
|
|
CHECK(it == first);
|
|
|
|
CHECK(*it == 4);
|
|
|
|
|
|
|
|
auto beyond = s.crend();
|
|
|
|
CHECK(it != beyond);
|
|
|
|
CHECK_THROW(*beyond, fail_fast);
|
|
|
|
|
|
|
|
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.crend())
|
|
|
|
{
|
|
|
|
*it = 5;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(it == beyond);
|
|
|
|
CHECK(it - beyond == 0);
|
|
|
|
|
|
|
|
for (auto& n : s)
|
2016-07-29 14:16:06 -04:00
|
|
|
{
|
2016-07-18 14:38:01 -04:00
|
|
|
CHECK(n == 5);
|
2016-07-29 14:16:06 -04:00
|
|
|
}
|
2016-07-18 14:38:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(comparison_operators)
|
|
|
|
{
|
|
|
|
{
|
2016-05-29 17:05:09 -04:00
|
|
|
span<int> s1 = nullptr;
|
|
|
|
span<int> s2 = nullptr;
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK(s1 == s2);
|
2016-05-29 17:05:09 -04:00
|
|
|
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-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-05-29 17:05:09 -04:00
|
|
|
int arr[] = {2, 1};
|
|
|
|
span<int> s1 = arr;
|
|
|
|
span<int> s2 = arr;
|
|
|
|
|
2016-02-24 14:26:28 -05: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int arr[] = {2, 1}; // bigger
|
|
|
|
|
|
|
|
span<int> s1 = nullptr;
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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
|
|
|
TEST(as_bytes)
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
span<const int> s = a;
|
|
|
|
CHECK(s.length() == 4);
|
|
|
|
span<const byte> bs = as_bytes(s);
|
|
|
|
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
|
|
|
CHECK(bs.length() == s.length_bytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
auto bs = as_bytes(s);
|
|
|
|
CHECK(bs.length() == s.length());
|
|
|
|
CHECK(bs.length() == 0);
|
|
|
|
CHECK(bs.size_bytes() == 0);
|
|
|
|
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
|
|
|
CHECK(bs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s = a;
|
|
|
|
auto bs = as_bytes(s);
|
|
|
|
CHECK(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
|
|
|
CHECK(bs.length() == s.length_bytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(as_writeable_bytes)
|
|
|
|
{
|
|
|
|
int a[] = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
// you should not be able to get writeable bytes for const objects
|
|
|
|
span<const int> s = a;
|
|
|
|
CHECK(s.length() == 4);
|
|
|
|
span<const byte> bs = as_writeable_bytes(s);
|
|
|
|
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
|
|
|
CHECK(bs.length() == s.length_bytes());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s;
|
|
|
|
auto bs = as_writeable_bytes(s);
|
|
|
|
CHECK(bs.length() == s.length());
|
|
|
|
CHECK(bs.length() == 0);
|
|
|
|
CHECK(bs.size_bytes() == 0);
|
|
|
|
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
|
|
|
CHECK(bs.data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
span<int> s = a;
|
|
|
|
auto bs = as_writeable_bytes(s);
|
|
|
|
CHECK(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
|
|
|
CHECK(bs.length() == s.length_bytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:26:28 -05:00
|
|
|
TEST(fixed_size_conversions)
|
|
|
|
{
|
|
|
|
int arr[] = {1, 2, 3, 4};
|
|
|
|
|
|
|
|
// converting to an span from an equal size array is ok
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 4> s4 = arr;
|
|
|
|
CHECK(s4.length() == 4);
|
2016-02-24 14:26:28 -05:00
|
|
|
|
2016-06-12 21:28:19 -04:00
|
|
|
// converting to dynamic_range is always ok
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int> s = s4;
|
|
|
|
CHECK(s.length() == s4.length());
|
|
|
|
static_cast<void>(s);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialization or assignment to static span that REDUCES size is NOT ok
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 2> s = arr;
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 2> s2 = s4;
|
|
|
|
static_cast<void>(s2);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-12 21:28:19 -04:00
|
|
|
// even when done dynamically
|
2016-02-24 14:26:28 -05:00
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int> s = arr;
|
|
|
|
auto f = [&]() {
|
|
|
|
span<int, 2> s2 = s;
|
|
|
|
static_cast<void>(s2);
|
|
|
|
};
|
|
|
|
CHECK_THROW(f(), fail_fast);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// but doing so explicitly is ok
|
|
|
|
|
|
|
|
// you can convert statically
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 2> s2 = {arr, 2};
|
|
|
|
static_cast<void>(s2);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 1> s1 = s4.first<1>();
|
|
|
|
static_cast<void>(s1);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ...or dynamically
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
// NB: implicit conversion to span<int,1> from span<int>
|
|
|
|
span<int, 1> s1 = s4.first(1);
|
|
|
|
static_cast<void>(s1);
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialization or assignment to static span that requires size INCREASE is not ok.
|
|
|
|
int arr2[2] = {1, 2};
|
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 4> s3 = arr2;
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
{
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 2> s2 = arr2;
|
|
|
|
span<int, 4> s4a = s2;
|
2016-02-24 14:26:28 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
auto f = [&]() {
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 4> s4 = {arr2, 2};
|
|
|
|
static_cast<void>(s4);
|
|
|
|
};
|
2016-02-24 14:26:28 -05:00
|
|
|
CHECK_THROW(f(), fail_fast);
|
|
|
|
}
|
|
|
|
|
2016-06-12 21:28:19 -04:00
|
|
|
// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
|
|
|
|
span<int> av = arr2;
|
2016-02-24 14:26:28 -05:00
|
|
|
auto f = [&]() {
|
2016-06-12 21:28:19 -04:00
|
|
|
span<int, 4> s4 = av;
|
|
|
|
static_cast<void>(s4);
|
2016-02-24 14:26:28 -05:00
|
|
|
};
|
|
|
|
CHECK_THROW(f(), fail_fast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|