HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Pie
|
9.0.0_r8
下载
查看原文件
收藏
根目录
external
Microsoft-GSL
tests
span_tests.cpp
/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// #include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; using namespace gsl; namespace { struct BaseClass { }; struct DerivedClass : BaseClass { }; } TEST_CASE("default_constructor") { { span
s; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { span
s; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { #ifdef CONFIRM_COMPILATION_ERRORS span
s; CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile #endif } { span
s{}; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs{}; CHECK((cs.length() == 0 && cs.data() == nullptr)); } } TEST_CASE("size_optimization") { { span
s; CHECK(sizeof(s) == sizeof(int*) + sizeof(ptrdiff_t)); } { span
s; CHECK(sizeof(s) == sizeof(int*)); } } TEST_CASE("from_nullptr_constructor") { { span
s = nullptr; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs = nullptr; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { span
s = nullptr; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs = nullptr; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { #ifdef CONFIRM_COMPILATION_ERRORS span
s = nullptr; CHECK((s.length() == 1 && s.data() == nullptr)); // explains why it can't compile #endif } { span
s{nullptr}; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs{nullptr}; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { span
s{nullptr}; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs{nullptr}; CHECK((cs.length() == 0 && cs.data() == nullptr)); } } TEST_CASE("from_nullptr_length_constructor") { { span
s{nullptr, static_cast
::index_type>(0)}; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs{nullptr, static_cast
::index_type>(0)}; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { span
s{nullptr, static_cast
::index_type>(0)}; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs{nullptr, static_cast
::index_type>(0)}; CHECK((cs.length() == 0 && cs.data() == nullptr)); } { auto workaround_macro = []() { span
s{nullptr, static_cast
::index_type>(0)}; }; CHECK_THROWS_AS(workaround_macro(), fail_fast); } { auto workaround_macro = []() { span
s{nullptr, 1}; }; CHECK_THROWS_AS(workaround_macro(), fail_fast); auto const_workaround_macro = []() { span
cs{nullptr, 1}; }; CHECK_THROWS_AS(const_workaround_macro(), fail_fast); } { auto workaround_macro = []() { span
s{nullptr, 1}; }; CHECK_THROWS_AS(workaround_macro(), fail_fast); auto const_workaround_macro = []() { span
s{nullptr, 1}; }; CHECK_THROWS_AS(const_workaround_macro(), fail_fast); } { span
s{nullptr, static_cast
::index_type>(0)}; CHECK((s.length() == 0 && s.data() == nullptr)); span
cs{nullptr, static_cast
::index_type>(0)}; CHECK((cs.length() == 0 && cs.data() == nullptr)); } } TEST_CASE("from_pointer_length_constructor") { int arr[4] = {1, 2, 3, 4}; { span
s{&arr[0], 2}; CHECK((s.length() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span
s{&arr[0], 2}; CHECK((s.length() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { int* p = nullptr; span
s{p, static_cast
::index_type>(0)}; CHECK((s.length() == 0 && s.data() == nullptr)); } { int* p = nullptr; auto workaround_macro = [=]() { span
s{p, 2}; }; CHECK_THROWS_AS(workaround_macro(), fail_fast); } { auto s = make_span(&arr[0], 2); CHECK((s.length() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { int* p = nullptr; auto s = make_span(p, static_cast
::index_type>(0)); CHECK((s.length() == 0 && s.data() == nullptr)); } { int* p = nullptr; auto workaround_macro = [=]() { make_span(p, 2); }; CHECK_THROWS_AS(workaround_macro(), fail_fast); } } TEST_CASE("from_pointer_pointer_constructor") { int arr[4] = {1, 2, 3, 4}; { span
s{&arr[0], &arr[2]}; CHECK((s.length() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span
s{&arr[0], &arr[2]}; CHECK((s.length() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { span
s{&arr[0], &arr[0]}; CHECK((s.length() == 0 && s.data() == &arr[0])); } { span
s{&arr[0], &arr[0]}; CHECK((s.length() == 0 && s.data() == &arr[0])); } // this will fail the std::distance() precondition, which asserts on MSVC debug builds //{ // auto workaround_macro = [&]() { span
s{&arr[1], &arr[0]}; }; // CHECK_THROWS_AS(workaround_macro(), fail_fast); //} // this will fail the std::distance() precondition, which asserts on MSVC debug builds //{ // int* p = nullptr; // auto workaround_macro = [&]() { span
s{&arr[0], p}; }; // CHECK_THROWS_AS(workaround_macro(), fail_fast); //} { int* p = nullptr; span
s{p, p}; CHECK((s.length() == 0 && s.data() == nullptr)); } { int* p = nullptr; span
s{p, p}; CHECK((s.length() == 0 && s.data() == nullptr)); } // this will fail the std::distance() precondition, which asserts on MSVC debug builds //{ // int* p = nullptr; // auto workaround_macro = [&]() { span
s{&arr[0], p}; }; // CHECK_THROWS_AS(workaround_macro(), fail_fast); //} { auto s = make_span(&arr[0], &arr[2]); CHECK((s.length() == 2 && s.data() == &arr[0])); CHECK((s[0] == 1 && s[1] == 2)); } { auto s = make_span(&arr[0], &arr[0]); CHECK((s.length() == 0 && s.data() == &arr[0])); } { int* p = nullptr; auto s = make_span(p, p); CHECK((s.length() == 0 && s.data() == nullptr)); } } TEST_CASE("from_array_constructor") { int arr[5] = {1, 2, 3, 4, 5}; { span
s{arr}; CHECK((s.length() == 5 && s.data() == &arr[0])); } { span
s{arr}; CHECK((s.length() == 5 && s.data() == &arr[0])); } int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; #ifdef CONFIRM_COMPILATION_ERRORS { span
s{arr}; } { span
s{arr}; CHECK((s.length() == 0 && s.data() == &arr[0])); } { span
s{arr2d}; CHECK((s.length() == 6 && s.data() == &arr2d[0][0])); CHECK((s[0] == 1 && s[5] == 6)); } { span
s{arr2d}; CHECK((s.length() == 0 && s.data() == &arr2d[0][0])); } { span
s{arr2d}; } #endif { span
s{&(arr2d[0]), 1}; 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}; #ifdef CONFIRM_COMPILATION_ERRORS { span
s{arr3d}; CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); CHECK((s[0] == 1 && s[11] == 12)); } { span
s{arr3d}; CHECK((s.length() == 0 && s.data() == &arr3d[0][0][0])); } { span
s{arr3d}; } { span
s{arr3d}; CHECK((s.length() == 12 && s.data() == &arr3d[0][0][0])); CHECK((s[0] == 1 && s[5] == 6)); } #endif { span
s{&arr3d[0], 1}; CHECK((s.length() == 1 && s.data() == &arr3d[0])); } { auto s = make_span(arr); CHECK((s.length() == 5 && s.data() == &arr[0])); } { auto s = make_span(&(arr2d[0]), 1); CHECK((s.length() == 1 && s.data() == &arr2d[0])); } { auto s = make_span(&arr3d[0], 1); CHECK((s.length() == 1 && s.data() == &arr3d[0])); } } TEST_CASE("from_dynamic_array_constructor") { double(*arr)[3][4] = new double[100][3][4]; { span
s(&arr[0][0][0], 10); CHECK((s.length() == 10 && s.data() == &arr[0][0][0])); } { auto s = make_span(&arr[0][0][0], 10); CHECK((s.length() == 10 && s.data() == &arr[0][0][0])); } delete[] arr; } TEST_CASE("from_std_array_constructor") { std::array
arr = {1, 2, 3, 4}; { span
s{arr}; CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); span
cs{arr}; CHECK((cs.size() == narrow_cast
(arr.size()) && cs.data() == arr.data())); } { span
s{arr}; CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); span
cs{arr}; CHECK((cs.size() == narrow_cast
(arr.size()) && cs.data() == arr.data())); } #ifdef CONFIRM_COMPILATION_ERRORS { span
s{arr}; CHECK((s.size() == 2 && s.data() == arr.data())); span
cs{arr}; CHECK((cs.size() == 2 && cs.data() == arr.data())); } { span
s{arr}; CHECK((s.size() == 0 && s.data() == arr.data())); span
cs{arr}; CHECK((cs.size() == 0 && cs.data() == arr.data())); } { span
s{arr}; } { auto get_an_array = []() -> std::array
{ return {1, 2, 3, 4}; }; auto take_a_span = [](span
s) { static_cast
(s); }; // try to take a temporary std::array take_a_span(get_an_array()); } #endif { auto get_an_array = []() -> std::array
{ return {1, 2, 3, 4}; }; auto take_a_span = [](span
s) { static_cast
(s); }; // try to take a temporary std::array take_a_span(get_an_array()); } { auto s = make_span(arr); CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } } TEST_CASE("from_const_std_array_constructor") { const std::array
arr = {1, 2, 3, 4}; { span
s{arr}; CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } { span
s{arr}; CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } #ifdef CONFIRM_COMPILATION_ERRORS { span
s{arr}; CHECK((s.size() == 2 && s.data() == arr.data())); } { span
s{arr}; CHECK((s.size() == 0 && s.data() == arr.data())); } { span
s{arr}; } #endif { auto get_an_array = []() -> const std::array
{ return {1, 2, 3, 4}; }; auto take_a_span = [](span
s) { static_cast
(s); }; // try to take a temporary std::array take_a_span(get_an_array()); } { auto s = make_span(arr); CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } } TEST_CASE("from_std_array_const_constructor") { std::array
arr = {1, 2, 3, 4}; { span
s{arr}; CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } { span
s{arr}; CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } #ifdef CONFIRM_COMPILATION_ERRORS { span
s{arr}; CHECK((s.size() == 2 && s.data() == arr.data())); } { span
s{arr}; CHECK((s.size() == 0 && s.data() == arr.data())); } { span
s{arr}; } { span
s{arr}; } #endif { auto s = make_span(arr); CHECK((s.size() == narrow_cast
(arr.size()) && s.data() == arr.data())); } } TEST_CASE("from_unique_pointer_construction") { { auto ptr = std::make_unique
(4); { span
s{ptr}; CHECK((s.length() == 1 && s.data() == ptr.get())); CHECK(s[0] == 4); } { auto s = make_span(ptr); CHECK((s.length() == 1 && s.data() == ptr.get())); CHECK(s[0] == 4); } } { auto ptr = std::unique_ptr
{nullptr}; { span
s{ptr}; CHECK((s.length() == 0 && s.data() == nullptr)); } { auto s = make_span(ptr); CHECK((s.length() == 0 && s.data() == nullptr)); } } { auto arr = std::make_unique
(4); for (auto i = 0U; i < 4; i++) arr[i] = gsl::narrow_cast
(i + 1); { span
s{arr, 4}; CHECK((s.length() == 4 && s.data() == arr.get())); CHECK((s[0] == 1 && s[1] == 2)); } { auto s = make_span(arr, 4); CHECK((s.length() == 4 && s.data() == arr.get())); CHECK((s[0] == 1 && s[1] == 2)); } } { auto arr = std::unique_ptr
{nullptr}; { span
s{arr, 0}; CHECK((s.length() == 0 && s.data() == nullptr)); } { auto s = make_span(arr, 0); CHECK((s.length() == 0 && s.data() == nullptr)); } } } TEST_CASE("from_shared_pointer_construction") { { auto ptr = std::make_shared
(4); { span
s{ptr}; CHECK((s.length() == 1 && s.data() == ptr.get())); CHECK((s[0] == 4)); } { auto s = make_span(ptr); CHECK((s.length() == 1 && s.data() == ptr.get())); CHECK((s[0] == 4)); } } { auto ptr = std::shared_ptr
{nullptr}; { span
s{ptr}; CHECK((s.length() == 0 && s.data() == nullptr)); } { auto s = make_span(ptr); CHECK((s.length() == 0 && s.data() == nullptr)); } } } TEST_CASE("from_container_constructor") { std::vector
v = {1, 2, 3}; const std::vector
cv = v; { span
s{v}; CHECK((s.size() == narrow_cast
(v.size()) && s.data() == v.data())); span
cs{v}; CHECK((cs.size() == narrow_cast
(v.size()) && cs.data() == v.data())); } std::string str = "hello"; const std::string cstr = "hello"; { #ifdef CONFIRM_COMPILATION_ERRORS span
s{str}; CHECK((s.size() == narrow_cast
(str.size()) && s.data() == str.data())); #endif span
cs{str}; CHECK((cs.size() == narrow_cast
(str.size()) && cs.data() == str.data())); } { #ifdef CONFIRM_COMPILATION_ERRORS span
s{cstr}; #endif span
cs{cstr}; CHECK((cs.size() == narrow_cast
(cstr.size()) && cs.data() == cstr.data())); } { #ifdef CONFIRM_COMPILATION_ERRORS auto get_temp_vector = []() -> std::vector
{ return {}; }; auto use_span = [](span
s) { static_cast
(s); }; use_span(get_temp_vector()); #endif } { auto get_temp_vector = []() -> std::vector
{ return {}; }; auto use_span = [](span
s) { static_cast
(s); }; use_span(get_temp_vector()); } { #ifdef CONFIRM_COMPILATION_ERRORS auto get_temp_string = []() -> std::string { return {}; }; auto use_span = [](span
s) { static_cast
(s); }; use_span(get_temp_string()); #endif } { auto get_temp_string = []() -> std::string { return {}; }; auto use_span = [](span
s) { static_cast
(s); }; use_span(get_temp_string()); } { #ifdef CONFIRM_COMPILATION_ERRORS auto get_temp_vector = []() -> const std::vector
{ return {}; }; auto use_span = [](span
s) { static_cast
(s); }; use_span(get_temp_vector()); #endif } { auto get_temp_string = []() -> const std::string { return {}; }; auto use_span = [](span
s) { static_cast
(s); }; use_span(get_temp_string()); } { #ifdef CONFIRM_COMPILATION_ERRORS std::map
m; span
s{m}; #endif } { auto s = make_span(v); CHECK((s.size() == narrow_cast
(v.size()) && s.data() == v.data())); auto cs = make_span(cv); CHECK((cs.size() == narrow_cast
(cv.size()) && cs.data() == cv.data())); } } TEST_CASE("from_convertible_span_constructor") { { span
avd; span
avcd = avd; static_cast
(avcd); } { #ifdef CONFIRM_COMPILATION_ERRORS span
avd; span
avb = avd; static_cast
(avb); #endif } #ifdef CONFIRM_COMPILATION_ERRORS { span
s; span
s2 = s; static_cast
(s2); } { span
s; span
s2 = s; static_cast
(s2); } { span
s; span
s2 = s; static_cast
(s2); } #endif } TEST_CASE("copy_move_and_assignment") { span
s1; CHECK(s1.empty()); int arr[] = {3, 4, 5}; span
s2 = arr; CHECK((s2.length() == 3 && s2.data() == &arr[0])); s2 = s1; CHECK(s2.empty()); auto get_temp_span = [&]() -> span
{ return {&arr[1], 2}; }; auto use_span = [&](span
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_CASE("first") { int arr[5] = {1, 2, 3, 4, 5}; { span
av = arr; CHECK(av.first<2>().length() == 2); CHECK(av.first(2).length() == 2); } { span
av = arr; CHECK(av.first<0>().length() == 0); CHECK(av.first(0).length() == 0); } { span
av = arr; CHECK(av.first<5>().length() == 5); CHECK(av.first(5).length() == 5); } { span
av = arr; #ifdef CONFIRM_COMPILATION_ERRORS CHECK(av.first<6>().length() == 6); CHECK(av.first<-1>().length() == -1); #endif CHECK_THROWS_AS(av.first(6).length(), fail_fast); } { span
av; CHECK(av.first<0>().length() == 0); CHECK(av.first(0).length() == 0); } } TEST_CASE("last") { int arr[5] = {1, 2, 3, 4, 5}; { span
av = arr; CHECK(av.last<2>().length() == 2); CHECK(av.last(2).length() == 2); } { span
av = arr; CHECK(av.last<0>().length() == 0); CHECK(av.last(0).length() == 0); } { span
av = arr; CHECK(av.last<5>().length() == 5); CHECK(av.last(5).length() == 5); } { span
av = arr; #ifdef CONFIRM_COMPILATION_ERRORS CHECK(av.last<6>().length() == 6); #endif CHECK_THROWS_AS(av.last(6).length(), fail_fast); } { span
av; CHECK(av.last<0>().length() == 0); CHECK(av.last(0).length() == 0); } } TEST_CASE("subspan") { int arr[5] = {1, 2, 3, 4, 5}; { span
av = arr; CHECK((av.subspan<2, 2>().length() == 2)); CHECK(av.subspan(2, 2).length() == 2); CHECK(av.subspan(2, 3).length() == 3); } { span
av = arr; CHECK((av.subspan<0, 0>().length() == 0)); CHECK(av.subspan(0, 0).length() == 0); } { span
av = arr; CHECK((av.subspan<0, 5>().length() == 5)); CHECK(av.subspan(0, 5).length() == 5); CHECK_THROWS_AS(av.subspan(0, 6).length(), fail_fast); CHECK_THROWS_AS(av.subspan(1, 5).length(), fail_fast); } { span
av = arr; CHECK((av.subspan<4, 0>().length() == 0)); CHECK(av.subspan(4, 0).length() == 0); CHECK(av.subspan(5, 0).length() == 0); CHECK_THROWS_AS(av.subspan(6, 0).length(), fail_fast); } { span
av; CHECK((av.subspan<0, 0>().length() == 0)); CHECK(av.subspan(0, 0).length() == 0); CHECK_THROWS_AS((av.subspan<1, 0>().length()), fail_fast); } { span
av; CHECK(av.subspan(0).length() == 0); CHECK_THROWS_AS(av.subspan(1).length(), fail_fast); } { span
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_THROWS_AS(av.subspan(6).length(), fail_fast); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); } { span
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_THROWS_AS(av.subspan(6).length(), fail_fast); const auto av2 = av.subspan(1); for (int i = 0; i < 4; ++i) CHECK(av2[i] == i + 2); } } TEST_CASE("at_call") { int arr[4] = {1, 2, 3, 4}; { span
s = arr; CHECK(s.at(0) == 1); CHECK_THROWS_AS(s.at(5), fail_fast); } { int arr2d[2] = {1, 6}; span
s = arr2d; CHECK(s.at(0) == 1); CHECK(s.at(1) == 6); CHECK_THROWS_AS(s.at(2), fail_fast); } } TEST_CASE("operator_function_call") { int arr[4] = {1, 2, 3, 4}; { span
s = arr; CHECK(s(0) == 1); CHECK_THROWS_AS(s(5), fail_fast); } { int arr2d[2] = {1, 6}; span
s = arr2d; CHECK(s(0) == 1); CHECK(s(1) == 6); CHECK_THROWS_AS(s(2), fail_fast); } } TEST_CASE("iterator_default_init") { span
::iterator it1; span
::iterator it2; CHECK(it1 == it2); } TEST_CASE("const_iterator_default_init") { span
::const_iterator it1; span
::const_iterator it2; CHECK(it1 == it2); } TEST_CASE("iterator_conversions") { span
::iterator badIt; span
::const_iterator badConstIt; CHECK(badIt == badConstIt); int a[] = {1, 2, 3, 4}; span
s = a; auto it = s.begin(); auto cit = s.cbegin(); CHECK(it == cit); CHECK(cit == it); span
::const_iterator cit2 = it; CHECK(cit2 == cit); span
::const_iterator cit3 = it + 4; CHECK(cit3 == s.cend()); } TEST_CASE("iterator_comparisons") { int a[] = {1, 2, 3, 4}; { span
s = a; span
::iterator it = s.begin(); auto it2 = it + 1; span
::const_iterator cit = s.cbegin(); 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); } } TEST_CASE("begin_end") { { int a[] = {1, 2, 3, 4}; span
s = a; span
::iterator it = s.begin(); span
::iterator it2 = std::begin(s); CHECK(it == it2); it = s.end(); it2 = std::end(s); CHECK(it == it2); } { int a[] = {1, 2, 3, 4}; span
s = a; auto it = s.begin(); auto first = it; CHECK(it == first); CHECK(*it == 1); auto beyond = s.end(); CHECK(it != beyond); CHECK_THROWS_AS(*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 (const auto& n : s) { CHECK(n == 5); } } } TEST_CASE("cbegin_cend") { { int a[] = {1, 2, 3, 4}; span
s = a; span
::const_iterator cit = s.cbegin(); span
::const_iterator cit2 = std::cbegin(s); CHECK(cit == cit2); cit = s.cend(); cit2 = std::cend(s); CHECK(cit == cit2); } { int a[] = {1, 2, 3, 4}; span
s = a; auto it = s.cbegin(); auto first = it; CHECK(it == first); CHECK(*it == 1); auto beyond = s.cend(); CHECK(it != beyond); CHECK_THROWS_AS(*beyond, fail_fast); CHECK(beyond - first == 4); CHECK(first - first == 0); CHECK(beyond - beyond == 0); ++it; CHECK(it - first == 1); CHECK(*it == 2); CHECK(beyond - it == 3); int last = 0; it = first; CHECK(it == first); while (it != s.cend()) { CHECK(*it == last + 1); last = *it; ++it; } CHECK(it == beyond); CHECK(it - beyond == 0); } } TEST_CASE("rbegin_rend") { { int a[] = {1, 2, 3, 4}; span
s = a; auto it = s.rbegin(); auto first = it; CHECK(it == first); CHECK(*it == 4); auto beyond = s.rend(); CHECK(it != beyond); CHECK_THROWS_AS(*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 (const auto& n : s) { CHECK(n == 5); } } } TEST_CASE("crbegin_crend") { { int a[] = {1, 2, 3, 4}; span
s = a; auto it = s.crbegin(); auto first = it; CHECK(it == first); CHECK(*it == 4); auto beyond = s.crend(); CHECK(it != beyond); CHECK_THROWS_AS(*beyond, fail_fast); CHECK(beyond - first == 4); CHECK(first - first == 0); CHECK(beyond - beyond == 0); ++it; CHECK(it - first == 1); CHECK(*it == 3); CHECK(beyond - it == 3); it = first; CHECK(it == first); int last = 5; while (it != s.crend()) { CHECK(*it == last - 1); last = *it; ++it; } CHECK(it == beyond); CHECK(it - beyond == 0); } } TEST_CASE("comparison_operators") { { span
s1 = nullptr; span
s2 = nullptr; 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}; span
s1 = arr; span
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); } { int arr[] = {2, 1}; // bigger span
s1 = nullptr; span
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
s1 = arr1; span
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
s1 = {&arr[0], 2}; // shorter span
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
s1 = arr1; span
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)); } } TEST_CASE("as_bytes") { int a[] = {1, 2, 3, 4}; { const span
s = a; CHECK(s.length() == 4); const span
bs = as_bytes(s); CHECK(static_cast
(bs.data()) == static_cast
(s.data())); CHECK(bs.length() == s.length_bytes()); } { span
s; const auto bs = as_bytes(s); CHECK(bs.length() == s.length()); CHECK(bs.length() == 0); CHECK(bs.size_bytes() == 0); CHECK(static_cast
(bs.data()) == static_cast
(s.data())); CHECK(bs.data() == nullptr); } { span
s = a; const auto bs = as_bytes(s); CHECK(static_cast
(bs.data()) == static_cast
(s.data())); CHECK(bs.length() == s.length_bytes()); } } TEST_CASE("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
s = a; CHECK(s.length() == 4); span
bs = as_writeable_bytes(s); CHECK(static_cast
(bs.data()) == static_cast
(s.data())); CHECK(bs.length() == s.length_bytes()); #endif } { span
s; const auto bs = as_writeable_bytes(s); CHECK(bs.length() == s.length()); CHECK(bs.length() == 0); CHECK(bs.size_bytes() == 0); CHECK(static_cast
(bs.data()) == static_cast