forked from microsoft/GSL
Reformat files to follow clang-format style (#492)
Project files were not following the clang-format style. For people using IDEs were clang-format is always run after a save this would cause unwanted changes. This commit only applies "clang-format -i" to files.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl_algorithm>
|
||||
|
||||
#include <array>
|
||||
@@ -179,8 +180,8 @@ SUITE(copy_tests)
|
||||
span<int*, 4> dst_span_static(dst);
|
||||
|
||||
// every line should produce a compilation error
|
||||
copy(src_span_dyn, dst_span_dyn);
|
||||
copy(src_span_dyn, dst_span_static);
|
||||
copy(src_span_dyn, dst_span_dyn);
|
||||
copy(src_span_dyn, dst_span_static);
|
||||
copy(src_span_static, dst_span_dyn);
|
||||
copy(src_span_static, dst_span_static);
|
||||
}
|
||||
@@ -196,9 +197,9 @@ SUITE(copy_tests)
|
||||
span<int> dst_span_dyn(dst);
|
||||
span<int, 4> dst_span_static(dst);
|
||||
|
||||
CHECK_THROW(copy(src_span_dyn, dst_span_dyn), fail_fast);
|
||||
CHECK_THROW(copy(src_span_dyn, dst_span_static), fail_fast);
|
||||
CHECK_THROW(copy(src_span_static, dst_span_dyn), fail_fast);
|
||||
CHECK_THROW(copy(src_span_dyn, dst_span_dyn), fail_fast);
|
||||
CHECK_THROW(copy(src_span_dyn, dst_span_static), fail_fast);
|
||||
CHECK_THROW(copy(src_span_static, dst_span_dyn), fail_fast);
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
copy(src_span_static, dst_span_static);
|
||||
|
||||
+18
-20
@@ -1,20 +1,21 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl>
|
||||
|
||||
using namespace gsl;
|
||||
@@ -34,7 +35,7 @@ SUITE(assertion_tests)
|
||||
}
|
||||
|
||||
int g(int i)
|
||||
{
|
||||
{
|
||||
i++;
|
||||
Ensures(i > 0 && i < 10);
|
||||
return i;
|
||||
@@ -47,7 +48,4 @@ SUITE(assertion_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+17
-18
@@ -15,9 +15,11 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl>
|
||||
#include <vector>
|
||||
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
|
||||
using gsl::fail_fast;
|
||||
|
||||
@@ -25,8 +27,8 @@ SUITE(at_tests)
|
||||
{
|
||||
TEST(static_array)
|
||||
{
|
||||
int a[4] = { 1, 2, 3, 4 };
|
||||
const int (&c_a)[4] = a;
|
||||
int a[4] = {1, 2, 3, 4};
|
||||
const int(&c_a)[4] = a;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
CHECK(&gsl::at(a, i) == &a[i]);
|
||||
@@ -41,7 +43,7 @@ SUITE(at_tests)
|
||||
|
||||
TEST(std_array)
|
||||
{
|
||||
std::array<int, 4> a = { 1, 2, 3, 4 };
|
||||
std::array<int, 4> a = {1, 2, 3, 4};
|
||||
const std::array<int, 4>& c_a = a;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
@@ -57,7 +59,7 @@ SUITE(at_tests)
|
||||
|
||||
TEST(StdVector)
|
||||
{
|
||||
std::vector<int> a = { 1, 2, 3, 4 };
|
||||
std::vector<int> a = {1, 2, 3, 4};
|
||||
const std::vector<int>& c_a = a;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
@@ -73,26 +75,26 @@ SUITE(at_tests)
|
||||
|
||||
TEST(InitializerList)
|
||||
{
|
||||
std::initializer_list<int> a = { 1, 2, 3, 4 };
|
||||
std::initializer_list<int> a = {1, 2, 3, 4};
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
CHECK(gsl::at(a, i) == i+1);
|
||||
CHECK(gsl::at({1,2,3,4}, i) == i+1);
|
||||
CHECK(gsl::at(a, i) == i + 1);
|
||||
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
|
||||
}
|
||||
|
||||
CHECK_THROW(gsl::at(a, -1), fail_fast);
|
||||
CHECK_THROW(gsl::at(a, 4), fail_fast);
|
||||
CHECK_THROW(gsl::at({1,2,3,4}, -1), fail_fast);
|
||||
CHECK_THROW(gsl::at({1,2,3,4}, 4), fail_fast);
|
||||
CHECK_THROW(gsl::at({1, 2, 3, 4}, -1), fail_fast);
|
||||
CHECK_THROW(gsl::at({1, 2, 3, 4}, 4), fail_fast);
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
|
||||
static constexpr bool test_constexpr()
|
||||
{
|
||||
int a1[4] = { 1, 2, 3, 4 };
|
||||
const int (&c_a1)[4] = a1;
|
||||
std::array<int,4> a2 = { 1, 2, 3, 4 };
|
||||
int a1[4] = {1, 2, 3, 4};
|
||||
const int(&c_a1)[4] = a1;
|
||||
std::array<int, 4> a2 = {1, 2, 3, 4};
|
||||
const std::array<int, 4>& c_a2 = a2;
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
@@ -101,7 +103,7 @@ static constexpr bool test_constexpr()
|
||||
// requires C++17:
|
||||
// if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false;
|
||||
if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false;
|
||||
if (gsl::at({1,2,3,4}, i) != i+1) return false;
|
||||
if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -110,7 +112,4 @@ static constexpr bool test_constexpr()
|
||||
static_assert(test_constexpr(), "FAIL");
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main() { return UnitTest::RunAllTests(); }
|
||||
|
||||
+54
-57
@@ -15,7 +15,9 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/multi_span>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
@@ -23,81 +25,76 @@ using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
void use(std::ptrdiff_t&) {}
|
||||
void use(std::ptrdiff_t&) {}
|
||||
}
|
||||
|
||||
SUITE(bounds_test)
|
||||
{
|
||||
TEST(basic_bounds)
|
||||
{
|
||||
for (auto point : static_bounds<dynamic_range, 3, 4 > { 2 })
|
||||
{
|
||||
for (decltype(point)::size_type j = 0;
|
||||
j < static_cast<decltype(point)::size_type>(decltype(point)::rank);
|
||||
j++)
|
||||
{
|
||||
use(j);
|
||||
use(point[static_cast<std::size_t>(j)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
TEST(basic_bounds)
|
||||
{
|
||||
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
|
||||
for (decltype(point)::size_type j = 0;
|
||||
j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++)
|
||||
{
|
||||
use(j);
|
||||
use(point[static_cast<std::size_t>(j)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(bounds_basic)
|
||||
{
|
||||
static_bounds<3, 4, 5> b;
|
||||
const auto a = b.slice();
|
||||
(void)a;
|
||||
static_bounds<4, dynamic_range, 2> x{ 4 };
|
||||
x.slice().slice();
|
||||
}
|
||||
TEST(bounds_basic)
|
||||
{
|
||||
static_bounds<3, 4, 5> b;
|
||||
const auto a = b.slice();
|
||||
(void) a;
|
||||
static_bounds<4, dynamic_range, 2> x{4};
|
||||
x.slice().slice();
|
||||
}
|
||||
|
||||
TEST (arrayview_iterator)
|
||||
{
|
||||
static_bounds<4, dynamic_range, 2> bounds{ 3 };
|
||||
TEST(arrayview_iterator)
|
||||
{
|
||||
static_bounds<4, dynamic_range, 2> bounds{3};
|
||||
|
||||
const auto itr = bounds.begin();
|
||||
(void)itr;
|
||||
const auto itr = bounds.begin();
|
||||
(void) itr;
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
multi_span<int, 4, dynamic_range, 2> av(nullptr, bounds);
|
||||
multi_span<int, 4, dynamic_range, 2> av(nullptr, bounds);
|
||||
|
||||
auto itr2 = av.cbegin();
|
||||
auto itr2 = av.cbegin();
|
||||
|
||||
for (auto& v : av) {
|
||||
v = 4;
|
||||
}
|
||||
fill(av.begin(), av.end(), 0);
|
||||
for (auto& v : av) {
|
||||
v = 4;
|
||||
}
|
||||
fill(av.begin(), av.end(), 0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
TEST (bounds_convertible)
|
||||
{
|
||||
static_bounds<7, 4, 2> b1;
|
||||
static_bounds<7, dynamic_range, 2> b2 = b1;
|
||||
(void)b2;
|
||||
TEST(bounds_convertible)
|
||||
{
|
||||
static_bounds<7, 4, 2> b1;
|
||||
static_bounds<7, dynamic_range, 2> b2 = b1;
|
||||
(void) b2;
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
static_bounds<7, dynamic_range, 1> b4 = b2;
|
||||
static_bounds<7, dynamic_range, 1> b4 = b2;
|
||||
#endif
|
||||
|
||||
static_bounds<dynamic_range, dynamic_range, dynamic_range> b3 = b1;
|
||||
static_bounds<7, 4, 2> b4 = b3;
|
||||
(void)b4;
|
||||
static_bounds<dynamic_range, dynamic_range, dynamic_range> b3 = b1;
|
||||
static_bounds<7, 4, 2> b4 = b3;
|
||||
(void) b4;
|
||||
|
||||
static_bounds<dynamic_range> b11;
|
||||
static_bounds<dynamic_range> b11;
|
||||
|
||||
static_bounds<dynamic_range> b5;
|
||||
static_bounds<34> b6;
|
||||
static_bounds<dynamic_range> b5;
|
||||
static_bounds<34> b6;
|
||||
|
||||
b5 = static_bounds<20>();
|
||||
CHECK_THROW(b6 = b5, fail_fast);
|
||||
b5 = static_bounds<34>();
|
||||
b6 = b5;
|
||||
b5 = static_bounds<20>();
|
||||
CHECK_THROW(b6 = b5, fail_fast);
|
||||
b5 = static_bounds<34>();
|
||||
b6 = b5;
|
||||
|
||||
CHECK(b5 == b6);
|
||||
CHECK(b5.size() == b6.size());
|
||||
}
|
||||
CHECK(b5 == b6);
|
||||
CHECK(b5.size() == b6.size());
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl_byte>
|
||||
|
||||
#include <iostream>
|
||||
@@ -111,11 +112,11 @@ SUITE(byte_tests)
|
||||
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
|
||||
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
|
||||
|
||||
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
|
||||
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
|
||||
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
|
||||
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
|
||||
}
|
||||
|
||||
int modify_both(gsl::byte& b, int& i)
|
||||
int modify_both(gsl::byte & b, int& i)
|
||||
{
|
||||
i = 10;
|
||||
b = to_byte<5>();
|
||||
@@ -124,12 +125,11 @@ SUITE(byte_tests)
|
||||
|
||||
TEST(aliasing)
|
||||
{
|
||||
int i{ 0 };
|
||||
int i{0};
|
||||
const int res = modify_both(reinterpret_cast<byte&>(i), i);
|
||||
CHECK(res == i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+335
-333
@@ -15,6 +15,7 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/multi_span>
|
||||
|
||||
#include <iostream>
|
||||
@@ -668,7 +669,9 @@ SUITE(multi_span_tests)
|
||||
CHECK(s2.empty());
|
||||
|
||||
auto get_temp_span = [&]() -> multi_span<int> { return {&arr[1], 2}; };
|
||||
auto use_span = [&](multi_span<const int> s) { CHECK(s.length() == 2 && s.data() == &arr[1]); };
|
||||
auto use_span = [&](multi_span<const int> s) {
|
||||
CHECK(s.length() == 2 && s.data() == &arr[1]);
|
||||
};
|
||||
use_span(get_temp_span());
|
||||
|
||||
s1 = get_temp_span();
|
||||
@@ -1028,7 +1031,7 @@ SUITE(multi_span_tests)
|
||||
int arr[] = {1, 2, 3};
|
||||
|
||||
multi_span<int> s1 = {&arr[0], 2}; // shorter
|
||||
multi_span<int> s2 = arr; // longer
|
||||
multi_span<int> s2 = arr; // longer
|
||||
|
||||
CHECK(s1 != s2);
|
||||
CHECK(s2 != s1);
|
||||
@@ -1283,412 +1286,411 @@ SUITE(multi_span_tests)
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
TEST(index_constructors)
|
||||
{
|
||||
{
|
||||
// components of the same type
|
||||
index<3> i1(0, 1, 2);
|
||||
CHECK(i1[0] == 0);
|
||||
TEST(index_constructors){{// components of the same type
|
||||
index<3> i1(0, 1, 2);
|
||||
CHECK(i1[0] == 0);
|
||||
|
||||
// components of different types
|
||||
std::size_t c0 = 0;
|
||||
std::size_t c1 = 1;
|
||||
index<3> i2(c0, c1, 2);
|
||||
CHECK(i2[0] == 0);
|
||||
// components of different types
|
||||
std::size_t c0 = 0;
|
||||
std::size_t c1 = 1;
|
||||
index<3> i2(c0, c1, 2);
|
||||
CHECK(i2[0] == 0);
|
||||
|
||||
// from array
|
||||
index<3> i3 = {0, 1, 2};
|
||||
CHECK(i3[0] == 0);
|
||||
// from array
|
||||
index<3> i3 = {0, 1, 2};
|
||||
CHECK(i3[0] == 0);
|
||||
|
||||
// from other index of the same size type
|
||||
index<3> i4 = i3;
|
||||
CHECK(i4[0] == 0);
|
||||
// from other index of the same size type
|
||||
index<3> i4 = i3;
|
||||
CHECK(i4[0] == 0);
|
||||
|
||||
// default
|
||||
index<3> i7;
|
||||
CHECK(i7[0] == 0);
|
||||
// default
|
||||
index<3> i7;
|
||||
CHECK(i7[0] == 0);
|
||||
|
||||
// default
|
||||
index<3> i9 = {};
|
||||
CHECK(i9[0] == 0);
|
||||
}
|
||||
// default
|
||||
index<3> i9 = {};
|
||||
CHECK(i9[0] == 0);
|
||||
}
|
||||
|
||||
{
|
||||
// components of the same type
|
||||
index<1> i1(0);
|
||||
CHECK(i1[0] == 0);
|
||||
{
|
||||
// components of the same type
|
||||
index<1> i1(0);
|
||||
CHECK(i1[0] == 0);
|
||||
|
||||
// components of different types
|
||||
std::size_t c0 = 0;
|
||||
index<1> i2(c0);
|
||||
CHECK(i2[0] == 0);
|
||||
// components of different types
|
||||
std::size_t c0 = 0;
|
||||
index<1> i2(c0);
|
||||
CHECK(i2[0] == 0);
|
||||
|
||||
// from array
|
||||
index<1> i3 = {0};
|
||||
CHECK(i3[0] == 0);
|
||||
// from array
|
||||
index<1> i3 = {0};
|
||||
CHECK(i3[0] == 0);
|
||||
|
||||
// from int
|
||||
index<1> i4 = 0;
|
||||
CHECK(i4[0] == 0);
|
||||
// from int
|
||||
index<1> i4 = 0;
|
||||
CHECK(i4[0] == 0);
|
||||
|
||||
// from other index of the same size type
|
||||
index<1> i5 = i3;
|
||||
CHECK(i5[0] == 0);
|
||||
// from other index of the same size type
|
||||
index<1> i5 = i3;
|
||||
CHECK(i5[0] == 0);
|
||||
|
||||
// default
|
||||
index<1> i8;
|
||||
CHECK(i8[0] == 0);
|
||||
// default
|
||||
index<1> i8;
|
||||
CHECK(i8[0] == 0);
|
||||
|
||||
// default
|
||||
index<1> i9 = {};
|
||||
CHECK(i9[0] == 0);
|
||||
}
|
||||
// default
|
||||
index<1> i9 = {};
|
||||
CHECK(i9[0] == 0);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
index<3> i1(0, 1);
|
||||
index<3> i2(0, 1, 2, 3);
|
||||
index<3> i3 = {0};
|
||||
index<3> i4 = {0, 1, 2, 3};
|
||||
index<1> i5 = {0, 1};
|
||||
}
|
||||
{
|
||||
index<3> i1(0, 1);
|
||||
index<3> i2(0, 1, 2, 3);
|
||||
index<3> i3 = {0};
|
||||
index<3> i4 = {0, 1, 2, 3};
|
||||
index<1> i5 = {0, 1};
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
TEST(index_operations)
|
||||
{
|
||||
ptrdiff_t a[3] = {0, 1, 2};
|
||||
ptrdiff_t b[3] = {3, 4, 5};
|
||||
index<3> i = a;
|
||||
index<3> j = b;
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
|
||||
TEST(index_operations)
|
||||
{
|
||||
ptrdiff_t a[3] = {0, 1, 2};
|
||||
ptrdiff_t b[3] = {3, 4, 5};
|
||||
index<3> i = a;
|
||||
index<3> j = b;
|
||||
index<3> k = i + j;
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
|
||||
{
|
||||
index<3> k = i + j;
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 3);
|
||||
CHECK(k[1] == 5);
|
||||
CHECK(k[2] == 7);
|
||||
}
|
||||
|
||||
{
|
||||
index<3> k = i * 3;
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 0);
|
||||
CHECK(k[1] == 3);
|
||||
CHECK(k[2] == 6);
|
||||
}
|
||||
|
||||
{
|
||||
index<3> k = 3 * i;
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 0);
|
||||
CHECK(k[1] == 3);
|
||||
CHECK(k[2] == 6);
|
||||
}
|
||||
|
||||
{
|
||||
index<2> k = details::shift_left(i);
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 1);
|
||||
CHECK(k[1] == 2);
|
||||
}
|
||||
CHECK(k[0] == 3);
|
||||
CHECK(k[1] == 5);
|
||||
CHECK(k[2] == 7);
|
||||
}
|
||||
|
||||
void iterate_second_column(multi_span<int, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
auto length = av.size() / 2;
|
||||
index<3> k = i * 3;
|
||||
|
||||
// view to the second column
|
||||
auto section = av.section({0, 1}, {length, 1});
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 0);
|
||||
CHECK(k[1] == 3);
|
||||
CHECK(k[2] == 6);
|
||||
}
|
||||
|
||||
CHECK(section.size() == length);
|
||||
for (auto i = 0; i < section.size(); ++i) {
|
||||
CHECK(section[i][0] == av[i][1]);
|
||||
}
|
||||
{
|
||||
index<3> k = 3 * i;
|
||||
|
||||
for (auto i = 0; i < section.size(); ++i) {
|
||||
auto idx = index<2>{i, 0}; // avoid braces inside the CHECK macro
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 0);
|
||||
CHECK(k[1] == 3);
|
||||
CHECK(k[2] == 6);
|
||||
}
|
||||
|
||||
{
|
||||
index<2> k = details::shift_left(i);
|
||||
|
||||
CHECK(i[0] == 0);
|
||||
CHECK(i[1] == 1);
|
||||
CHECK(i[2] == 2);
|
||||
CHECK(k[0] == 1);
|
||||
CHECK(k[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
void iterate_second_column(multi_span<int, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
auto length = av.size() / 2;
|
||||
|
||||
// view to the second column
|
||||
auto section = av.section({0, 1}, {length, 1});
|
||||
|
||||
CHECK(section.size() == length);
|
||||
for (auto i = 0; i < section.size(); ++i) {
|
||||
CHECK(section[i][0] == av[i][1]);
|
||||
}
|
||||
|
||||
for (auto i = 0; i < section.size(); ++i) {
|
||||
auto idx = index<2>{i, 0}; // avoid braces inside the CHECK macro
|
||||
CHECK(section[idx] == av[i][1]);
|
||||
}
|
||||
|
||||
CHECK(section.bounds().index_bounds()[0] == length);
|
||||
CHECK(section.bounds().index_bounds()[1] == 1);
|
||||
for (auto i = 0; i < section.bounds().index_bounds()[0]; ++i) {
|
||||
for (auto j = 0; j < section.bounds().index_bounds()[1]; ++j) {
|
||||
auto idx = index<2>{i, j}; // avoid braces inside the CHECK macro
|
||||
CHECK(section[idx] == av[i][1]);
|
||||
}
|
||||
|
||||
CHECK(section.bounds().index_bounds()[0] == length);
|
||||
CHECK(section.bounds().index_bounds()[1] == 1);
|
||||
for (auto i = 0; i < section.bounds().index_bounds()[0]; ++i) {
|
||||
for (auto j = 0; j < section.bounds().index_bounds()[1]; ++j) {
|
||||
auto idx = index<2>{i, j}; // avoid braces inside the CHECK macro
|
||||
CHECK(section[idx] == av[i][1]);
|
||||
}
|
||||
}
|
||||
|
||||
auto check_sum = 0;
|
||||
for (auto i = 0; i < length; ++i) {
|
||||
check_sum += av[i][1];
|
||||
}
|
||||
|
||||
{
|
||||
auto idx = 0;
|
||||
auto sum = 0;
|
||||
for (auto num : section) {
|
||||
CHECK(num == av[idx][1]);
|
||||
sum += num;
|
||||
idx++;
|
||||
}
|
||||
|
||||
CHECK(sum == check_sum);
|
||||
}
|
||||
{
|
||||
auto idx = length - 1;
|
||||
auto sum = 0;
|
||||
for (auto iter = section.rbegin(); iter != section.rend(); ++iter) {
|
||||
CHECK(*iter == av[idx][1]);
|
||||
sum += *iter;
|
||||
idx--;
|
||||
}
|
||||
|
||||
CHECK(sum == check_sum);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_section_iteration)
|
||||
{
|
||||
int arr[4][2] = {{4, 0}, {5, 1}, {6, 2}, {7, 3}};
|
||||
|
||||
// static bounds
|
||||
{
|
||||
multi_span<int, 4, 2> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
// first bound is dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, 2> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
// second bound is dynamic
|
||||
{
|
||||
multi_span<int, 4, dynamic_range> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
// both bounds are dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, dynamic_range> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
auto check_sum = 0;
|
||||
for (auto i = 0; i < length; ++i) {
|
||||
check_sum += av[i][1];
|
||||
}
|
||||
|
||||
TEST(dynamic_span_section_iteration)
|
||||
{
|
||||
auto height = 4, width = 2;
|
||||
auto size = height * width;
|
||||
|
||||
auto arr = new int[static_cast<std::size_t>(size)];
|
||||
for (auto i = 0; i < size; ++i) {
|
||||
arr[i] = i;
|
||||
auto idx = 0;
|
||||
auto sum = 0;
|
||||
for (auto num : section) {
|
||||
CHECK(num == av[idx][1]);
|
||||
sum += num;
|
||||
idx++;
|
||||
}
|
||||
|
||||
auto av = as_multi_span(arr, size);
|
||||
|
||||
// first bound is dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, 2> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
// second bound is dynamic
|
||||
{
|
||||
multi_span<int, 4, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
// both bounds are dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
CHECK(sum == check_sum);
|
||||
}
|
||||
{
|
||||
auto idx = length - 1;
|
||||
auto sum = 0;
|
||||
for (auto iter = section.rbegin(); iter != section.rend(); ++iter) {
|
||||
CHECK(*iter == av[idx][1]);
|
||||
sum += *iter;
|
||||
idx--;
|
||||
}
|
||||
|
||||
delete[] arr;
|
||||
CHECK(sum == check_sum);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_section_iteration)
|
||||
{
|
||||
int arr[4][2] = {{4, 0}, {5, 1}, {6, 2}, {7, 3}};
|
||||
|
||||
// static bounds
|
||||
{
|
||||
multi_span<int, 4, 2> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
// first bound is dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, 2> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
// second bound is dynamic
|
||||
{
|
||||
multi_span<int, 4, dynamic_range> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
// both bounds are dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, dynamic_range> av = arr;
|
||||
iterate_second_column(av);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(dynamic_span_section_iteration)
|
||||
{
|
||||
auto height = 4, width = 2;
|
||||
auto size = height * width;
|
||||
|
||||
auto arr = new int[static_cast<std::size_t>(size)];
|
||||
for (auto i = 0; i < size; ++i) {
|
||||
arr[i] = i;
|
||||
}
|
||||
|
||||
TEST(span_structure_size)
|
||||
auto av = as_multi_span(arr, size);
|
||||
|
||||
// first bound is dynamic
|
||||
{
|
||||
double(*arr)[3][4] = new double[100][3][4];
|
||||
multi_span<double, dynamic_range, 3, 4> av1(arr, 10);
|
||||
|
||||
struct EffectiveStructure
|
||||
{
|
||||
double* v1;
|
||||
ptrdiff_t v2;
|
||||
};
|
||||
CHECK(sizeof(av1) == sizeof(EffectiveStructure));
|
||||
|
||||
CHECK_THROW(av1[10][3][4], fail_fast);
|
||||
|
||||
multi_span<const double, dynamic_range, 6, 4> av2 = as_multi_span(av1, dim(5), dim<6>(), dim<4>());
|
||||
(void) av2;
|
||||
multi_span<int, dynamic_range, 2> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
// second bound is dynamic
|
||||
{
|
||||
multi_span<int, 4, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
// both bounds are dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, dynamic_range> av2 =
|
||||
as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
|
||||
TEST(fixed_size_conversions)
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
TEST(span_structure_size)
|
||||
{
|
||||
double(*arr)[3][4] = new double[100][3][4];
|
||||
multi_span<double, dynamic_range, 3, 4> av1(arr, 10);
|
||||
|
||||
struct EffectiveStructure
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4};
|
||||
double* v1;
|
||||
ptrdiff_t v2;
|
||||
};
|
||||
CHECK(sizeof(av1) == sizeof(EffectiveStructure));
|
||||
|
||||
// converting to an multi_span from an equal size array is ok
|
||||
multi_span<int, 4> av4 = arr;
|
||||
CHECK(av4.length() == 4);
|
||||
CHECK_THROW(av1[10][3][4], fail_fast);
|
||||
|
||||
// converting to dynamic_range a_v is always ok
|
||||
{
|
||||
multi_span<int, dynamic_range> av = av4;
|
||||
(void) av;
|
||||
}
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
(void) av;
|
||||
}
|
||||
multi_span<const double, dynamic_range, 6, 4> av2 =
|
||||
as_multi_span(av1, dim(5), dim<6>(), dim<4>());
|
||||
(void) av2;
|
||||
}
|
||||
|
||||
TEST(fixed_size_conversions)
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4};
|
||||
|
||||
// converting to an multi_span from an equal size array is ok
|
||||
multi_span<int, 4> av4 = arr;
|
||||
CHECK(av4.length() == 4);
|
||||
|
||||
// converting to dynamic_range a_v is always ok
|
||||
{
|
||||
multi_span<int, dynamic_range> av = av4;
|
||||
(void) av;
|
||||
}
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
(void) av;
|
||||
}
|
||||
|
||||
// initialization or assignment to static multi_span that REDUCES size is NOT ok
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
multi_span<int, 2> av2 = arr;
|
||||
}
|
||||
{
|
||||
multi_span<int, 2> av2 = av4;
|
||||
}
|
||||
{
|
||||
multi_span<int, 2> av2 = arr;
|
||||
}
|
||||
{
|
||||
multi_span<int, 2> av2 = av4;
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2> av2 = av;
|
||||
(void) av2;
|
||||
}
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2> av2 = av;
|
||||
(void) av2;
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2, 1> av2 = av.as_multi_span(dim<2>(), dim<2>());
|
||||
}
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2, 1> av2 = av.as_multi_span(dim<2>(), dim<2>());
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2, 1> av2 = as_multi_span(av, dim(2), dim(2));
|
||||
auto workaround_macro = [&]() { return av2[{1, 0}] == 2; };
|
||||
CHECK(workaround_macro());
|
||||
}
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2, 1> av2 = as_multi_span(av, dim(2), dim(2));
|
||||
auto workaround_macro = [&]() { return av2[{1, 0}] == 2; };
|
||||
CHECK(workaround_macro());
|
||||
}
|
||||
|
||||
// but doing so explicitly is ok
|
||||
// but doing so explicitly is ok
|
||||
|
||||
// you can convert statically
|
||||
{
|
||||
multi_span<int, 2> av2 = {arr, 2};
|
||||
(void) av2;
|
||||
}
|
||||
{
|
||||
multi_span<int, 1> av2 = av4.first<1>();
|
||||
(void) av2;
|
||||
}
|
||||
// you can convert statically
|
||||
{
|
||||
multi_span<int, 2> av2 = {arr, 2};
|
||||
(void) av2;
|
||||
}
|
||||
{
|
||||
multi_span<int, 1> av2 = av4.first<1>();
|
||||
(void) av2;
|
||||
}
|
||||
|
||||
// ...or dynamically
|
||||
{
|
||||
// NB: implicit conversion to multi_span<int,2> from multi_span<int,dynamic_range>
|
||||
multi_span<int, 1> av2 = av4.first(1);
|
||||
(void) av2;
|
||||
}
|
||||
// ...or dynamically
|
||||
{
|
||||
// NB: implicit conversion to multi_span<int,2> from multi_span<int,dynamic_range>
|
||||
multi_span<int, 1> av2 = av4.first(1);
|
||||
(void) av2;
|
||||
}
|
||||
|
||||
// initialization or assignment to static multi_span that requires size INCREASE is not ok.
|
||||
int arr2[2] = {1, 2};
|
||||
// initialization or assignment to static multi_span that requires size INCREASE is not ok.
|
||||
int arr2[2] = {1, 2};
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
multi_span<int, 4> av4 = arr2;
|
||||
}
|
||||
{
|
||||
multi_span<int, 2> av2 = arr2;
|
||||
multi_span<int, 4> av4 = av2;
|
||||
}
|
||||
{
|
||||
multi_span<int, 4> av4 = arr2;
|
||||
}
|
||||
{
|
||||
multi_span<int, 2> av2 = arr2;
|
||||
multi_span<int, 4> av4 = av2;
|
||||
}
|
||||
#endif
|
||||
{
|
||||
auto f = [&]() {
|
||||
multi_span<int, 4> av9 = {arr2, 2};
|
||||
(void) av9;
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
// this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one
|
||||
multi_span<int, dynamic_range> av = arr2;
|
||||
{
|
||||
auto f = [&]() {
|
||||
multi_span<int, 4> av2 = av;
|
||||
(void) av2;
|
||||
multi_span<int, 4> av9 = {arr2, 2};
|
||||
(void) av9;
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
TEST(as_writeable_bytes)
|
||||
// this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one
|
||||
multi_span<int, dynamic_range> av = arr2;
|
||||
auto f = [&]() {
|
||||
multi_span<int, 4> av2 = av;
|
||||
(void) av2;
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
TEST(as_writeable_bytes)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
// you should not be able to get writeable bytes for const objects
|
||||
multi_span<const int, dynamic_range> av = a;
|
||||
auto wav = av.as_writeable_bytes();
|
||||
// you should not be able to get writeable bytes for const objects
|
||||
multi_span<const int, dynamic_range> av = a;
|
||||
auto wav = av.as_writeable_bytes();
|
||||
#endif
|
||||
}
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av;
|
||||
auto wav = as_writeable_bytes(av);
|
||||
CHECK(wav.length() == av.length());
|
||||
CHECK(wav.length() == 0);
|
||||
CHECK(wav.size_bytes() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = a;
|
||||
auto wav = as_writeable_bytes(av);
|
||||
CHECK(wav.data() == reinterpret_cast<byte*>(&a[0]));
|
||||
CHECK(static_cast<std::size_t>(wav.length()) == sizeof(a));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(iterator)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
multi_span<int, dynamic_range> av;
|
||||
auto wav = as_writeable_bytes(av);
|
||||
CHECK(wav.length() == av.length());
|
||||
CHECK(wav.length() == 0);
|
||||
CHECK(wav.size_bytes() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = a;
|
||||
auto wav = as_writeable_bytes(av);
|
||||
for (auto& b : wav) {
|
||||
b = byte(0);
|
||||
}
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
CHECK(a[i] == 0);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = a;
|
||||
for (auto& n : av) {
|
||||
n = 1;
|
||||
}
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
CHECK(a[i] == 1);
|
||||
}
|
||||
}
|
||||
{
|
||||
multi_span<int, dynamic_range> av = a;
|
||||
auto wav = as_writeable_bytes(av);
|
||||
CHECK(wav.data() == reinterpret_cast<byte*>(&a[0]));
|
||||
CHECK(static_cast<std::size_t>(wav.length()) == sizeof(a));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(iterator)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = a;
|
||||
auto wav = as_writeable_bytes(av);
|
||||
for (auto& b : wav) {
|
||||
b = byte(0);
|
||||
}
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
CHECK(a[i] == 0);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = a;
|
||||
for (auto& n : av) {
|
||||
n = 1;
|
||||
}
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
CHECK(a[i] == 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+65
-58
@@ -1,33 +1,41 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl>
|
||||
#include <vector>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
struct MyBase {};
|
||||
struct MyDerived : public MyBase {};
|
||||
struct Unrelated {};
|
||||
struct MyBase
|
||||
{
|
||||
};
|
||||
struct MyDerived : public MyBase
|
||||
{
|
||||
};
|
||||
struct Unrelated
|
||||
{
|
||||
};
|
||||
|
||||
// stand-in for a user-defined ref-counted class
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
struct RefCounted
|
||||
{
|
||||
RefCounted(T* p) : p_(p) {}
|
||||
@@ -39,95 +47,97 @@ struct RefCounted
|
||||
template <typename T>
|
||||
struct CustomPtr
|
||||
{
|
||||
CustomPtr(T* p) : p_(p) {}
|
||||
operator T*() { return p_; }
|
||||
bool operator !=(std::nullptr_t)const { return p_ != nullptr; }
|
||||
T* p_ = nullptr;
|
||||
CustomPtr(T* p) : p_(p) {}
|
||||
operator T*() { return p_; }
|
||||
bool operator!=(std::nullptr_t) const { return p_ != nullptr; }
|
||||
T* p_ = nullptr;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
||||
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
||||
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
||||
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
||||
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
||||
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true" : "false";
|
||||
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
|
||||
|
||||
|
||||
SUITE(NotNullTests)
|
||||
{
|
||||
|
||||
bool helper(not_null<int*> p)
|
||||
{
|
||||
return *p == 12;
|
||||
}
|
||||
bool helper(not_null<int*> p) { return *p == 12; }
|
||||
|
||||
TEST(TestNotNullConstructors)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
not_null<int*> p = nullptr; // yay...does not compile!
|
||||
not_null<int*> p = nullptr; // yay...does not compile!
|
||||
not_null<std::vector<char>*> p = 0; // yay...does not compile!
|
||||
not_null<int*> p; // yay...does not compile!
|
||||
not_null<int*> p; // yay...does not compile!
|
||||
std::unique_ptr<int> up = std::make_unique<int>(120);
|
||||
not_null<int*> p = up;
|
||||
|
||||
// Forbid non-nullptr assignable types
|
||||
not_null<std::vector<int>> f(std::vector<int>{1});
|
||||
not_null<int> z(10);
|
||||
not_null<std::vector<int>> y({1,2});
|
||||
not_null<std::vector<int>> y({1, 2});
|
||||
#endif
|
||||
int i = 12;
|
||||
auto rp = RefCounted<int>(&i);
|
||||
not_null<int*> p(rp);
|
||||
CHECK(p.get() == &i);
|
||||
int i = 12;
|
||||
auto rp = RefCounted<int>(&i);
|
||||
not_null<int*> p(rp);
|
||||
CHECK(p.get() == &i);
|
||||
|
||||
not_null<std::shared_ptr<int>> x(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
not_null<std::shared_ptr<int>> x(
|
||||
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
||||
}
|
||||
|
||||
TEST(TestNotNullCasting)
|
||||
{
|
||||
MyBase base;
|
||||
MyDerived derived;
|
||||
Unrelated unrelated;
|
||||
not_null<Unrelated*> u = &unrelated;
|
||||
(void)u;
|
||||
not_null<MyDerived*> p = &derived;
|
||||
MyDerived derived;
|
||||
Unrelated unrelated;
|
||||
not_null<Unrelated*> u = &unrelated;
|
||||
(void) u;
|
||||
not_null<MyDerived*> p = &derived;
|
||||
not_null<MyBase*> q = &base;
|
||||
q = p; // allowed with heterogeneous copy ctor
|
||||
q = p; // allowed with heterogeneous copy ctor
|
||||
CHECK(q == p);
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
q = u; // no viable conversion possible between MyBase* and Unrelated*
|
||||
p = q; // not possible to implicitly convert MyBase* to MyDerived*
|
||||
q = u; // no viable conversion possible between MyBase* and Unrelated*
|
||||
p = q; // not possible to implicitly convert MyBase* to MyDerived*
|
||||
|
||||
not_null<Unrelated*> r = p;
|
||||
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
|
||||
@@ -139,7 +149,7 @@ SUITE(NotNullTests)
|
||||
TEST(TestNotNullAssignment)
|
||||
{
|
||||
int i = 12;
|
||||
not_null<int*> p = &i;
|
||||
not_null<int*> p = &i;
|
||||
CHECK(helper(p));
|
||||
|
||||
int* q = nullptr;
|
||||
@@ -211,7 +221,7 @@ SUITE(NotNullTests)
|
||||
|
||||
TEST(TestNotNullCustomPtrComparison)
|
||||
{
|
||||
int ints[2] = { 42, 43 };
|
||||
int ints[2] = {42, 43};
|
||||
CustomPtr<int> p1(&ints[0]);
|
||||
CustomPtr<const int> p2(&ints[1]);
|
||||
|
||||
@@ -242,7 +252,4 @@ SUITE(NotNullTests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+19
-23
@@ -1,31 +1,30 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl>
|
||||
|
||||
#include <functional>
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
SUITE(owner_tests)
|
||||
{
|
||||
void f(int* i)
|
||||
{
|
||||
*i += 1;
|
||||
}
|
||||
void f(int* i) { *i += 1; }
|
||||
|
||||
TEST(basic_test)
|
||||
{
|
||||
@@ -37,7 +36,4 @@ SUITE(owner_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+733
-740
File diff suppressed because it is too large
Load Diff
+228
-219
@@ -15,34 +15,39 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/multi_span>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct BaseClass {};
|
||||
struct DerivedClass : BaseClass {};
|
||||
struct BaseClass
|
||||
{
|
||||
};
|
||||
struct DerivedClass : BaseClass
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
SUITE(strided_span_tests)
|
||||
{
|
||||
TEST (span_section_test)
|
||||
TEST(span_section_test)
|
||||
{
|
||||
int a[30][4][5];
|
||||
|
||||
const auto av = as_multi_span(a);
|
||||
const auto sub = av.section({15, 0, 0}, gsl::index<3>{2, 2, 2});
|
||||
const auto subsub = sub.section({1, 0, 0}, gsl::index<3>{1, 1, 1});
|
||||
(void)subsub;
|
||||
(void) subsub;
|
||||
}
|
||||
|
||||
TEST(span_section)
|
||||
@@ -51,13 +56,13 @@ SUITE(strided_span_tests)
|
||||
std::iota(begin(data), end(data), 0);
|
||||
const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
||||
|
||||
const strided_span<int, 2> av_section_1 = av.section({ 1, 2 }, { 3, 4 });
|
||||
const strided_span<int, 2> av_section_1 = av.section({1, 2}, {3, 4});
|
||||
CHECK((av_section_1[{0, 0}] == 12));
|
||||
CHECK((av_section_1[{0, 1}] == 13));
|
||||
CHECK((av_section_1[{1, 0}] == 22));
|
||||
CHECK((av_section_1[{2, 3}] == 35));
|
||||
|
||||
const strided_span<int, 2> av_section_2 = av_section_1.section({ 1, 2 }, { 2,2 });
|
||||
const strided_span<int, 2> av_section_2 = av_section_1.section({1, 2}, {2, 2});
|
||||
CHECK((av_section_2[{0, 0}] == 24));
|
||||
CHECK((av_section_2[{0, 1}] == 25));
|
||||
CHECK((av_section_2[{1, 0}] == 34));
|
||||
@@ -67,188 +72,197 @@ SUITE(strided_span_tests)
|
||||
{
|
||||
// Check stride constructor
|
||||
{
|
||||
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const int carr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
const int carr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
|
||||
strided_span<int, 1> sav1{ arr, {{9}, {1}} }; // T -> T
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{ 9 });
|
||||
strided_span<int, 1> sav1{arr, {{9}, {1}}}; // T -> T
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{9});
|
||||
CHECK(sav1.bounds().stride() == 1);
|
||||
CHECK(sav1[0] == 1 && sav1[8] == 9);
|
||||
|
||||
|
||||
strided_span<const int, 1> sav2{ carr, {{ 4 }, { 2 }} }; // const T -> const T
|
||||
CHECK(sav2.bounds().index_bounds() == index<1>{ 4 });
|
||||
strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T
|
||||
CHECK(sav2.bounds().index_bounds() == index<1>{4});
|
||||
CHECK(sav2.bounds().strides() == index<1>{2});
|
||||
CHECK(sav2[0] == 1 && sav2[3] == 7);
|
||||
|
||||
strided_span<int, 2> sav3{ arr, {{ 2, 2 },{ 6, 2 }} }; // T -> const T
|
||||
CHECK((sav3.bounds().index_bounds() == index<2>{ 2, 2 }));
|
||||
CHECK((sav3.bounds().strides() == index<2>{ 6, 2 }));
|
||||
strided_span<int, 2> sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T
|
||||
CHECK((sav3.bounds().index_bounds() == index<2>{2, 2}));
|
||||
CHECK((sav3.bounds().strides() == index<2>{6, 2}));
|
||||
CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7));
|
||||
}
|
||||
|
||||
// Check multi_span constructor
|
||||
{
|
||||
int arr[] = { 1, 2 };
|
||||
int arr[] = {1, 2};
|
||||
|
||||
// From non-cv-qualified source
|
||||
{
|
||||
const multi_span<int> src = arr;
|
||||
|
||||
strided_span<int, 1> sav{ src, {2, 1} };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav.bounds().strides() == index<1>{ 1 });
|
||||
strided_span<int, 1> sav{src, {2, 1}};
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav.bounds().strides() == index<1>{1});
|
||||
CHECK(sav[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
//strided_span<const int, 1> sav_c{ {src}, {2, 1} };
|
||||
strided_span<const int, 1> sav_c{ multi_span<const int>{src}, strided_bounds<1>{2, 1} };
|
||||
// strided_span<const int, 1> sav_c{ {src}, {2, 1} };
|
||||
strided_span<const int, 1> sav_c{multi_span<const int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#else
|
||||
strided_span<const int, 1> sav_c{ multi_span<const int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_span<const int, 1> sav_c{multi_span<const int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_c.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_c[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_span<volatile int, 1> sav_v{ src, {2, 1} };
|
||||
strided_span<volatile int, 1> sav_v{src, {2, 1}};
|
||||
#else
|
||||
strided_span<volatile int, 1> sav_v{ multi_span<volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_span<volatile int, 1> sav_v{multi_span<volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_v.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_v[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_span<const volatile int, 1> sav_cv{ src, {2, 1} };
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
#else
|
||||
strided_span<const volatile int, 1> sav_cv{ multi_span<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
}
|
||||
|
||||
// From const-qualified source
|
||||
{
|
||||
const multi_span<const int> src{ arr };
|
||||
const multi_span<const int> src{arr};
|
||||
|
||||
strided_span<const int, 1> sav_c{ src, {2, 1} };
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
|
||||
strided_span<const int, 1> sav_c{src, {2, 1}};
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_c.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_c[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_span<const volatile int, 1> sav_cv{ src, {2, 1} };
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
#else
|
||||
strided_span<const volatile int, 1> sav_cv{ multi_span<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
}
|
||||
|
||||
// From volatile-qualified source
|
||||
{
|
||||
const multi_span<volatile int> src{ arr };
|
||||
const multi_span<volatile int> src{arr};
|
||||
|
||||
strided_span<volatile int, 1> sav_v{ src, {2, 1} };
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
|
||||
strided_span<volatile int, 1> sav_v{src, {2, 1}};
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_v.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_v[1] == 2);
|
||||
|
||||
#if _MSC_VER > 1800
|
||||
strided_span<const volatile int, 1> sav_cv{ src, {2, 1} };
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
#else
|
||||
strided_span<const volatile int, 1> sav_cv{ multi_span<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
|
||||
strided_bounds<1>{2, 1}};
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
}
|
||||
|
||||
// From cv-qualified source
|
||||
{
|
||||
const multi_span<const volatile int> src{ arr };
|
||||
const multi_span<const volatile int> src{arr};
|
||||
|
||||
strided_span<const volatile int, 1> sav_cv{ src, {2, 1} };
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{1});
|
||||
CHECK(sav_cv[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Check const-casting constructor
|
||||
{
|
||||
int arr[2] = { 4, 5 };
|
||||
int arr[2] = {4, 5};
|
||||
|
||||
const multi_span<int, 2> av(arr, 2);
|
||||
multi_span<const int, 2> av2{ av };
|
||||
multi_span<const int, 2> av2{av};
|
||||
CHECK(av2[1] == 5);
|
||||
|
||||
static_assert(std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value, "ctor is not implicit!");
|
||||
static_assert(
|
||||
std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value,
|
||||
"ctor is not implicit!");
|
||||
|
||||
const strided_span<int, 1> src{ arr, {2, 1} };
|
||||
strided_span<const int, 1> sav{ src };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
const strided_span<int, 1> src{arr, {2, 1}};
|
||||
strided_span<const int, 1> sav{src};
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav.bounds().stride() == 1);
|
||||
CHECK(sav[1] == 5);
|
||||
|
||||
static_assert(std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value, "ctor is not implicit!");
|
||||
static_assert(
|
||||
std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value,
|
||||
"ctor is not implicit!");
|
||||
}
|
||||
|
||||
// Check copy constructor
|
||||
{
|
||||
int arr1[2] = { 3, 4 };
|
||||
const strided_span<int, 1> src1{ arr1, {2, 1} };
|
||||
strided_span<int, 1> sav1{ src1 };
|
||||
int arr1[2] = {3, 4};
|
||||
const strided_span<int, 1> src1{arr1, {2, 1}};
|
||||
strided_span<int, 1> sav1{src1};
|
||||
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav1.bounds().stride() == 1);
|
||||
CHECK(sav1[0] == 3);
|
||||
|
||||
int arr2[6] = { 1, 2, 3, 4, 5, 6 };
|
||||
const strided_span<const int, 2> src2{ arr2, {{ 3, 2 }, { 2, 1 }} };
|
||||
strided_span<const int, 2> sav2{ src2 };
|
||||
CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 }));
|
||||
CHECK((sav2.bounds().strides() == index<2>{ 2, 1 }));
|
||||
int arr2[6] = {1, 2, 3, 4, 5, 6};
|
||||
const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}};
|
||||
strided_span<const int, 2> sav2{src2};
|
||||
CHECK((sav2.bounds().index_bounds() == index<2>{3, 2}));
|
||||
CHECK((sav2.bounds().strides() == index<2>{2, 1}));
|
||||
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
|
||||
}
|
||||
|
||||
// Check const-casting assignment operator
|
||||
{
|
||||
int arr1[2] = { 1, 2 };
|
||||
int arr2[6] = { 3, 4, 5, 6, 7, 8 };
|
||||
int arr1[2] = {1, 2};
|
||||
int arr2[6] = {3, 4, 5, 6, 7, 8};
|
||||
|
||||
const strided_span<int, 1> src{ arr1, {{2}, {1}} };
|
||||
strided_span<const int, 1> sav{ arr2, {{3}, {2}} };
|
||||
const strided_span<int, 1> src{arr1, {{2}, {1}}};
|
||||
strided_span<const int, 1> sav{arr2, {{3}, {2}}};
|
||||
strided_span<const int, 1>& sav_ref = (sav = src);
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav.bounds().strides() == index<1>{1});
|
||||
CHECK(sav[0] == 1);
|
||||
CHECK(&sav_ref == &sav);
|
||||
}
|
||||
|
||||
// Check copy assignment operator
|
||||
{
|
||||
int arr1[2] = { 3, 4 };
|
||||
int arr1b[1] = { 0 };
|
||||
const strided_span<int, 1> src1{ arr1, {2, 1} };
|
||||
strided_span<int, 1> sav1{ arr1b, {1, 1} };
|
||||
int arr1[2] = {3, 4};
|
||||
int arr1b[1] = {0};
|
||||
const strided_span<int, 1> src1{arr1, {2, 1}};
|
||||
strided_span<int, 1> sav1{arr1b, {1, 1}};
|
||||
strided_span<int, 1>& sav1_ref = (sav1 = src1);
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav1.bounds().strides() == index<1>{ 1 });
|
||||
CHECK(sav1.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav1.bounds().strides() == index<1>{1});
|
||||
CHECK(sav1[0] == 3);
|
||||
CHECK(&sav1_ref == &sav1);
|
||||
|
||||
const int arr2[6] = { 1, 2, 3, 4, 5, 6 };
|
||||
const int arr2b[1] = { 0 };
|
||||
const strided_span<const int, 2> src2{ arr2, {{ 3, 2 },{ 2, 1 }} };
|
||||
strided_span<const int, 2> sav2{ arr2b, {{ 1, 1 },{ 1, 1 }} };
|
||||
const int arr2[6] = {1, 2, 3, 4, 5, 6};
|
||||
const int arr2b[1] = {0};
|
||||
const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}};
|
||||
strided_span<const int, 2> sav2{arr2b, {{1, 1}, {1, 1}}};
|
||||
strided_span<const int, 2>& sav2_ref = (sav2 = src2);
|
||||
CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 }));
|
||||
CHECK((sav2.bounds().strides() == index<2>{ 2, 1 }));
|
||||
CHECK((sav2.bounds().index_bounds() == index<2>{3, 2}));
|
||||
CHECK((sav2.bounds().strides() == index<2>{2, 1}));
|
||||
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
|
||||
CHECK(&sav2_ref == &sav2);
|
||||
}
|
||||
@@ -258,13 +272,15 @@ SUITE(strided_span_tests)
|
||||
{
|
||||
std::vector<int> data(5 * 10);
|
||||
std::iota(begin(data), end(data), 0);
|
||||
const multi_span<int, 5, 10> src = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
||||
const multi_span<int, 5, 10> src =
|
||||
as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
|
||||
|
||||
const strided_span<int, 2> sav{ src, {{5, 10}, {10, 1}} };
|
||||
const strided_span<int, 2> sav{src, {{5, 10}, {10, 1}}};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const strided_span<const int, 2> csav{ {src},{ { 5, 10 },{ 10, 1 } } };
|
||||
const strided_span<const int, 2> csav{{src}, {{5, 10}, {10, 1}}};
|
||||
#endif
|
||||
const strided_span<const int, 2> csav{ multi_span<const int, 5, 10>{ src }, { { 5, 10 },{ 10, 1 } } };
|
||||
const strided_span<const int, 2> csav{multi_span<const int, 5, 10>{src},
|
||||
{{5, 10}, {10, 1}}};
|
||||
|
||||
strided_span<int, 1> sav_sl = sav[2];
|
||||
CHECK(sav_sl[0] == 20);
|
||||
@@ -284,12 +300,8 @@ SUITE(strided_span_tests)
|
||||
// use cases, such as column-major multidimensional array
|
||||
// (aka. "FORTRAN" layout).
|
||||
|
||||
int cm_array[3 * 5] = {
|
||||
1, 4, 7, 10, 13,
|
||||
2, 5, 8, 11, 14,
|
||||
3, 6, 9, 12, 15
|
||||
};
|
||||
strided_span<int, 2> cm_sav{ cm_array, {{ 5, 3 },{ 1, 5 }} };
|
||||
int cm_array[3 * 5] = {1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15};
|
||||
strided_span<int, 2> cm_sav{cm_array, {{5, 3}, {1, 5}}};
|
||||
|
||||
// Accessing elements
|
||||
CHECK((cm_sav[{0, 0}] == 1));
|
||||
@@ -305,7 +317,7 @@ SUITE(strided_span_tests)
|
||||
CHECK(cm_sl[2] == 12);
|
||||
|
||||
// Section
|
||||
strided_span<int, 2> cm_sec = cm_sav.section( { 2, 1 }, { 3, 2 });
|
||||
strided_span<int, 2> cm_sec = cm_sav.section({2, 1}, {3, 2});
|
||||
|
||||
CHECK((cm_sec.bounds().index_bounds() == index<2>{3, 2}));
|
||||
CHECK((cm_sec[{0, 0}] == 8));
|
||||
@@ -316,7 +328,7 @@ SUITE(strided_span_tests)
|
||||
|
||||
TEST(strided_span_bounds)
|
||||
{
|
||||
int arr[] = { 0, 1, 2, 3 };
|
||||
int arr[] = {0, 1, 2, 3};
|
||||
multi_span<int> av(arr);
|
||||
|
||||
{
|
||||
@@ -335,7 +347,7 @@ SUITE(strided_span_tests)
|
||||
|
||||
{
|
||||
// zero stride
|
||||
strided_span<int, 1> sav{ av,{ { 4 },{} } };
|
||||
strided_span<int, 1> sav{av, {{4}, {}}};
|
||||
CHECK(sav[0] == 0);
|
||||
CHECK(sav[3] == 0);
|
||||
CHECK_THROW(sav[4], fail_fast);
|
||||
@@ -343,36 +355,36 @@ SUITE(strided_span_tests)
|
||||
|
||||
{
|
||||
// zero extent
|
||||
strided_span<int, 1> sav{ av,{ {},{ 1 } } };
|
||||
strided_span<int, 1> sav{av, {{}, {1}}};
|
||||
CHECK_THROW(sav[0], fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// zero extent and stride
|
||||
strided_span<int, 1> sav{ av,{ {},{} } };
|
||||
strided_span<int, 1> sav{av, {{}, {}}};
|
||||
CHECK_THROW(sav[0], fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with matching strided bounds
|
||||
strided_span<int, 1> sav{ arr,{ 4, 1 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 4 });
|
||||
strided_span<int, 1> sav{arr, {4, 1}};
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{4});
|
||||
CHECK(sav[3] == 3);
|
||||
CHECK_THROW(sav[4], fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with smaller strided bounds
|
||||
strided_span<int, 1> sav{ arr,{ 2, 1 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
strided_span<int, 1> sav{arr, {2, 1}};
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav[1] == 1);
|
||||
CHECK_THROW(sav[2], fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// strided array ctor with fitting irregular bounds
|
||||
strided_span<int, 1> sav{ arr,{ 2, 3 } };
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
|
||||
strided_span<int, 1> sav{arr, {2, 3}};
|
||||
CHECK(sav.bounds().index_bounds() == index<1>{2});
|
||||
CHECK(sav[0] == 0);
|
||||
CHECK(sav[1] == 3);
|
||||
CHECK_THROW(sav[2], fail_fast);
|
||||
@@ -380,68 +392,69 @@ SUITE(strided_span_tests)
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from static arrays
|
||||
CHECK_THROW((strided_span<int, 1> { arr, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { arr, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { arr, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { arr, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { arr, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{arr, {3, 2}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{arr, {3, 3}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{arr, {4, 5}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{arr, {5, 1}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{arr, {5, 5}}), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from array view
|
||||
CHECK_THROW((strided_span<int, 1> { av, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av, {3, 2}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av, {3, 3}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av, {4, 5}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av, {5, 1}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av, {5, 5}}), fail_fast);
|
||||
}
|
||||
|
||||
{
|
||||
// bounds cross data boundaries - from dynamic arrays
|
||||
CHECK_THROW((strided_span<int, 1> { av.data(), 4, { 3, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av.data(), 4, { 3, 3 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av.data(), 4, { 4, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av.data(), 4, { 5, 1 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av.data(), 4, { 5, 5 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1> { av.data(), 2, { 2, 2 } }), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {3, 2}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {3, 3}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {4, 5}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {5, 1}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av.data(), 4, {5, 5}}), fail_fast);
|
||||
CHECK_THROW((strided_span<int, 1>{av.data(), 2, {2, 2}}), fail_fast);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
strided_span<int, 1> sav0{ av.data(), { 3, 2 } };
|
||||
strided_span<int, 1> sav1{ arr, { 1 } };
|
||||
strided_span<int, 1> sav2{ arr, { 1,1,1 } };
|
||||
strided_span<int, 1> sav3{ av, { 1 } };
|
||||
strided_span<int, 1> sav4{ av, { 1,1,1 } };
|
||||
strided_span<int, 2> sav5{ av.as_multi_span(dim<2>(), dim<2>()), { 1 } };
|
||||
strided_span<int, 2> sav6{ av.as_multi_span(dim<2>(), dim<2>()), { 1,1,1 } };
|
||||
strided_span<int, 2> sav7{ av.as_multi_span(dim<2>(), dim<2>()), { { 1,1 },{ 1,1 },{ 1,1 } } };
|
||||
strided_span<int, 1> sav0{av.data(), {3, 2}};
|
||||
strided_span<int, 1> sav1{arr, {1}};
|
||||
strided_span<int, 1> sav2{arr, {1, 1, 1}};
|
||||
strided_span<int, 1> sav3{av, {1}};
|
||||
strided_span<int, 1> sav4{av, {1, 1, 1}};
|
||||
strided_span<int, 2> sav5{av.as_multi_span(dim<2>(), dim<2>()), {1}};
|
||||
strided_span<int, 2> sav6{av.as_multi_span(dim<2>(), dim<2>()), {1, 1, 1}};
|
||||
strided_span<int, 2> sav7{av.as_multi_span(dim<2>(), dim<2>()),
|
||||
{{1, 1}, {1, 1}, {1, 1}}};
|
||||
|
||||
index<1> index{ 0, 1 };
|
||||
strided_span<int, 1> sav8{ arr,{ 1,{ 1,1 } } };
|
||||
strided_span<int, 1> sav9{ arr,{ { 1,1 },{ 1,1 } } };
|
||||
strided_span<int, 1> sav10{ av,{ 1,{ 1,1 } } };
|
||||
strided_span<int, 1> sav11{ av,{ { 1,1 },{ 1,1 } } };
|
||||
strided_span<int, 2> sav12{ av.as_multi_span(dim<2>(), dim<2>()),{ { 1 },{ 1 } } };
|
||||
strided_span<int, 2> sav13{ av.as_multi_span(dim<2>(), dim<2>()),{ { 1 },{ 1,1,1 } } };
|
||||
strided_span<int, 2> sav14{ av.as_multi_span(dim<2>(), dim<2>()),{ { 1,1,1 },{ 1 } } };
|
||||
index<1> index{0, 1};
|
||||
strided_span<int, 1> sav8{arr, {1, {1, 1}}};
|
||||
strided_span<int, 1> sav9{arr, {{1, 1}, {1, 1}}};
|
||||
strided_span<int, 1> sav10{av, {1, {1, 1}}};
|
||||
strided_span<int, 1> sav11{av, {{1, 1}, {1, 1}}};
|
||||
strided_span<int, 2> sav12{av.as_multi_span(dim<2>(), dim<2>()), {{1}, {1}}};
|
||||
strided_span<int, 2> sav13{av.as_multi_span(dim<2>(), dim<2>()), {{1}, {1, 1, 1}}};
|
||||
strided_span<int, 2> sav14{av.as_multi_span(dim<2>(), dim<2>()), {{1, 1, 1}, {1}}};
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(strided_span_type_conversion)
|
||||
{
|
||||
int arr[] = { 0, 1, 2, 3 };
|
||||
int arr[] = {0, 1, 2, 3};
|
||||
multi_span<int> av(arr);
|
||||
|
||||
{
|
||||
strided_span<int, 1> sav{ av.data(), av.size(), { av.size() / 2, 2 } };
|
||||
strided_span<int, 1> sav{av.data(), av.size(), {av.size() / 2, 2}};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strided_span<long, 1> lsav1 = sav.as_strided_span<long, 1>();
|
||||
#endif
|
||||
}
|
||||
{
|
||||
strided_span<int, 1> sav{ av, { av.size() / 2, 2 } };
|
||||
strided_span<int, 1> sav{av, {av.size() / 2, 2}};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strided_span<long, 1> lsav1 = sav.as_strided_span<long, 1>();
|
||||
#endif
|
||||
@@ -451,8 +464,8 @@ SUITE(strided_span_tests)
|
||||
|
||||
// retype strided array with regular strides - from raw data
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
|
||||
strided_span<const byte, 2> sav2{ bytes.data(), bytes.size(), bounds };
|
||||
strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}};
|
||||
strided_span<const byte, 2> sav2{bytes.data(), bytes.size(), bounds};
|
||||
strided_span<const int, 2> sav3 = sav2.as_strided_span<const int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
CHECK(sav3[1][0] == 2);
|
||||
@@ -462,9 +475,10 @@ SUITE(strided_span_tests)
|
||||
|
||||
// retype strided array with regular strides - from multi_span
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}};
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
CHECK(sav3[1][0] == 2);
|
||||
@@ -474,47 +488,53 @@ SUITE(strided_span_tests)
|
||||
|
||||
// retype strided array with not enough elements - last dimension of the array is too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4,2 },{ 4, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_bounds<2> bounds{{4, 2}, {4, 1}};
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides are too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4,2 },{ 2, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_bounds<2> bounds{{4, 2}, {2, 1}};
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new typesize
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new
|
||||
// typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2,6 },{ 4, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_bounds<2> bounds{{2, 6}, {4, 1}};
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides does not divide by the new typesize
|
||||
// retype strided array with not enough elements - strides does not divide by the new
|
||||
// typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, 1 },{ 6, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_bounds<2> bounds{{2, 1}, {6, 1}};
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 =
|
||||
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{bytes2, bounds};
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with irregular strides - from raw data
|
||||
{
|
||||
strided_bounds<1> bounds{ bytes.size() / 2, 2 };
|
||||
strided_span<const byte, 1> sav2{ bytes.data(), bytes.size(), bounds };
|
||||
strided_bounds<1> bounds{bytes.size() / 2, 2};
|
||||
strided_span<const byte, 1> sav2{bytes.data(), bytes.size(), bounds};
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with irregular strides - from multi_span
|
||||
{
|
||||
strided_bounds<1> bounds{ bytes.size() / 2, 2 };
|
||||
strided_span<const byte, 1> sav2{ bytes, bounds };
|
||||
strided_bounds<1> bounds{bytes.size() / 2, 2};
|
||||
strided_span<const byte, 1> sav2{bytes, bounds};
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
}
|
||||
@@ -523,31 +543,29 @@ SUITE(strided_span_tests)
|
||||
{
|
||||
{
|
||||
multi_span<int, 0> empty_av(nullptr);
|
||||
strided_span<int, 1> empty_sav{ empty_av, { 0, 1 } };
|
||||
strided_span<int, 1> empty_sav{empty_av, {0, 1}};
|
||||
|
||||
CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 });
|
||||
CHECK(empty_sav.bounds().index_bounds() == index<1>{0});
|
||||
CHECK_THROW(empty_sav[0], fail_fast);
|
||||
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
||||
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
||||
|
||||
for (const auto& v : empty_sav)
|
||||
{
|
||||
(void)v;
|
||||
for (const auto& v : empty_sav) {
|
||||
(void) v;
|
||||
CHECK(false);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
strided_span<int, 1> empty_sav{ nullptr, 0, { 0, 1 } };
|
||||
strided_span<int, 1> empty_sav{nullptr, 0, {0, 1}};
|
||||
|
||||
CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 });
|
||||
CHECK(empty_sav.bounds().index_bounds() == index<1>{0});
|
||||
CHECK_THROW(empty_sav[0], fail_fast);
|
||||
CHECK_THROW(empty_sav.begin()[0], fail_fast);
|
||||
CHECK_THROW(empty_sav.cbegin()[0], fail_fast);
|
||||
|
||||
for (const auto& v : empty_sav)
|
||||
{
|
||||
(void)v;
|
||||
for (const auto& v : empty_sav) {
|
||||
(void) v;
|
||||
CHECK(false);
|
||||
}
|
||||
}
|
||||
@@ -561,20 +579,18 @@ SUITE(strided_span_tests)
|
||||
#if _MSC_VER > 1800
|
||||
auto bounds = strided_bounds<1>({length}, {2});
|
||||
#else
|
||||
auto bounds = strided_bounds<1>(index<1>{ length }, index<1>{ 2 });
|
||||
auto bounds = strided_bounds<1>(index<1>{length}, index<1>{2});
|
||||
#endif
|
||||
strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
|
||||
|
||||
CHECK(strided.size() == length);
|
||||
CHECK(strided.bounds().index_bounds()[0] == length);
|
||||
for (auto i = 0; i < strided.size(); ++i)
|
||||
{
|
||||
for (auto i = 0; i < strided.size(); ++i) {
|
||||
CHECK(strided[i] == av[2 * i + 1]);
|
||||
}
|
||||
|
||||
int idx = 0;
|
||||
for (auto num : strided)
|
||||
{
|
||||
for (auto num : strided) {
|
||||
CHECK(num == av[2 * idx + 1]);
|
||||
idx++;
|
||||
}
|
||||
@@ -582,7 +598,7 @@ SUITE(strided_span_tests)
|
||||
|
||||
TEST(strided_span_section_iteration)
|
||||
{
|
||||
int arr[8] = {4,0,5,1,6,2,7,3};
|
||||
int arr[8] = {4, 0, 5, 1, 6, 2, 7, 3};
|
||||
|
||||
// static bounds
|
||||
{
|
||||
@@ -600,8 +616,7 @@ SUITE(strided_span_tests)
|
||||
TEST(dynamic_strided_span_section_iteration)
|
||||
{
|
||||
auto arr = new int[8];
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
arr[2 * i] = 4 + i;
|
||||
arr[2 * i + 1] = i;
|
||||
}
|
||||
@@ -614,29 +629,25 @@ SUITE(strided_span_tests)
|
||||
|
||||
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
|
||||
{
|
||||
const int expected[6] = {2,3,10,11,18,19};
|
||||
auto section = av.section({0,1,0}, {3,1,2});
|
||||
const int expected[6] = {2, 3, 10, 11, 18, 19};
|
||||
auto section = av.section({0, 1, 0}, {3, 1, 2});
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i)
|
||||
{
|
||||
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||
for (auto k = 0; k < section.extent<2>(); ++k)
|
||||
{
|
||||
auto idx = index<3>{i,j,k}; // avoid braces in the CHECK macro
|
||||
for (auto k = 0; k < section.extent<2>(); ++k) {
|
||||
auto idx = index<3>{i, j, k}; // avoid braces in the CHECK macro
|
||||
CHECK(section[idx] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = 0; i < section.extent<0>(); ++i)
|
||||
{
|
||||
for (auto i = 0; i < section.extent<0>(); ++i) {
|
||||
for (auto j = 0; j < section.extent<1>(); ++j)
|
||||
for (auto k = 0; k < section.extent<2>(); ++k)
|
||||
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (const auto num : section)
|
||||
{
|
||||
for (const auto num : section) {
|
||||
CHECK(num == expected[i]);
|
||||
i++;
|
||||
}
|
||||
@@ -645,11 +656,9 @@ SUITE(strided_span_tests)
|
||||
TEST(strided_span_section_iteration_3d)
|
||||
{
|
||||
int arr[3][4][2]{};
|
||||
for (auto i = 0; i < 3; ++i)
|
||||
{
|
||||
for (auto i = 0; i < 3; ++i) {
|
||||
for (auto j = 0; j < 4; ++j)
|
||||
for (auto k = 0; k < 2; ++k)
|
||||
arr[i][j][k] = 8 * i + 2 * j + k;
|
||||
for (auto k = 0; k < 2; ++k) arr[i][j][k] = 8 * i + 2 * j + k;
|
||||
}
|
||||
|
||||
{
|
||||
@@ -664,8 +673,7 @@ SUITE(strided_span_tests)
|
||||
const auto size = height * width;
|
||||
|
||||
auto arr = new int[static_cast<std::size_t>(size)];
|
||||
for (auto i = 0; i < size; ++i)
|
||||
{
|
||||
for (auto i = 0; i < size; ++i) {
|
||||
arr[i] = i;
|
||||
}
|
||||
|
||||
@@ -695,9 +703,14 @@ SUITE(strided_span_tests)
|
||||
{
|
||||
// get an multi_span of 'c' values from the list of X's
|
||||
|
||||
struct X { int a; int b; int c; };
|
||||
struct X
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
};
|
||||
|
||||
X arr[4] = {{0,1,2},{3,4,5},{6,7,8},{9,10,11}};
|
||||
X arr[4] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}};
|
||||
|
||||
int s = sizeof(int) / sizeof(byte);
|
||||
auto d2 = 3 * s;
|
||||
@@ -710,9 +723,11 @@ SUITE(strided_span_tests)
|
||||
CHECK(av.bounds().index_bounds()[1] == 12);
|
||||
|
||||
// get the last 4 columns
|
||||
auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], arr[0].c[3] } , { arr[1].c[0], ... } , ... }
|
||||
auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2],
|
||||
// arr[0].c[3] } , { arr[1].c[0], ... } , ...
|
||||
// }
|
||||
|
||||
// convert to array 4x1 array of integers
|
||||
// convert to array 4x1 array of integers
|
||||
auto cs = section.as_strided_span<int>(); // { { arr[0].c }, {arr[1].c } , ... }
|
||||
|
||||
CHECK(cs.bounds().index_bounds()[0] == 4);
|
||||
@@ -720,9 +735,8 @@ SUITE(strided_span_tests)
|
||||
|
||||
// transpose to 1x4 array
|
||||
strided_bounds<2> reverse_bounds{
|
||||
{cs.bounds().index_bounds()[1] , cs.bounds().index_bounds()[0]},
|
||||
{cs.bounds().strides()[1], cs.bounds().strides()[0]}
|
||||
};
|
||||
{cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0]},
|
||||
{cs.bounds().strides()[1], cs.bounds().strides()[0]}};
|
||||
|
||||
strided_span<int, 2> transposed{cs.data(), cs.bounds().total_size(), reverse_bounds};
|
||||
|
||||
@@ -733,16 +747,11 @@ SUITE(strided_span_tests)
|
||||
CHECK_THROW(result.bounds().index_bounds()[1], fail_fast);
|
||||
|
||||
int i = 0;
|
||||
for (auto& num : result)
|
||||
{
|
||||
for (auto& num : result) {
|
||||
CHECK(num == arr[i].c);
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+83
-86
@@ -15,16 +15,17 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
#include <cstdlib>
|
||||
#include <gsl/string_span>
|
||||
|
||||
#include <gsl/gsl> //owner
|
||||
#include <vector>
|
||||
#include <gsl/string_span>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
|
||||
SUITE(string_span_tests)
|
||||
{
|
||||
|
||||
@@ -48,7 +49,7 @@ SUITE(string_span_tests)
|
||||
TEST(TestConstructFromStdVector)
|
||||
{
|
||||
std::vector<char> vec(5, 'h');
|
||||
string_span<> v {vec};
|
||||
string_span<> v{vec};
|
||||
CHECK(v.length() == static_cast<string_span<>::index_type>(vec.size()));
|
||||
}
|
||||
|
||||
@@ -96,7 +97,7 @@ SUITE(string_span_tests)
|
||||
{
|
||||
char stack_string[] = "Hello";
|
||||
cstring_span<> v = ensure_z(stack_string);
|
||||
(void)v;
|
||||
(void) v;
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
string_span<> v2 = v;
|
||||
string_span<> v3 = "Hello";
|
||||
@@ -117,12 +118,13 @@ SUITE(string_span_tests)
|
||||
|
||||
TEST(TestToBasicString)
|
||||
{
|
||||
auto s = gsl::to_basic_string<char,std::char_traits<char>,::std::allocator<char>>(cstring_span<>{});
|
||||
auto s = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(
|
||||
cstring_span<>{});
|
||||
CHECK(s.length() == 0);
|
||||
|
||||
char stack_string[] = "Hello";
|
||||
cstring_span<> v = ensure_z(stack_string);
|
||||
auto s2 = gsl::to_basic_string<char,std::char_traits<char>,::std::allocator<char>>(v);
|
||||
auto s2 = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(v);
|
||||
CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
|
||||
CHECK(s2.length() == 5);
|
||||
}
|
||||
@@ -150,12 +152,12 @@ SUITE(string_span_tests)
|
||||
{
|
||||
cstring_span<> span = "Hello";
|
||||
|
||||
const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
const char ar1[] = "Hello";
|
||||
const char ar2[10] = "Hello";
|
||||
const char* ptr = "Hello";
|
||||
const std::string str = "Hello";
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
gsl::span<const char> sp = ensure_z("Hello");
|
||||
|
||||
// comparison to literal
|
||||
@@ -187,7 +189,7 @@ SUITE(string_span_tests)
|
||||
}
|
||||
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
|
||||
string_span<> span = ar;
|
||||
|
||||
@@ -195,7 +197,7 @@ SUITE(string_span_tests)
|
||||
char ar2[10] = "Hello";
|
||||
char* ptr = ar;
|
||||
std::string str = "Hello";
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
gsl::span<char> sp = ensure_z(ar1);
|
||||
|
||||
// comparison to static array with no null termination
|
||||
@@ -223,13 +225,12 @@ SUITE(string_span_tests)
|
||||
CHECK(span == span);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
const char ar1[] = "Hello";
|
||||
const char ar2[10] = "Hello";
|
||||
const std::string str = "Hello";
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const gsl::span<const char> sp = ensure_z("Hello");
|
||||
|
||||
cstring_span<> span = "Hello";
|
||||
@@ -261,13 +262,13 @@ SUITE(string_span_tests)
|
||||
|
||||
// const span, non-const other type
|
||||
|
||||
char _ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char _ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
char _ar1[] = "Hello";
|
||||
char _ar2[10] = "Hello";
|
||||
char* _ptr = _ar;
|
||||
std::string _str = "Hello";
|
||||
std::vector<char> _vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
gsl::span<char> _sp{ _ar, 5 };
|
||||
std::vector<char> _vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
gsl::span<char> _sp{_ar, 5};
|
||||
|
||||
CHECK(span == _ar);
|
||||
CHECK(span == _ar1);
|
||||
@@ -289,7 +290,7 @@ SUITE(string_span_tests)
|
||||
CHECK(_vec == span);
|
||||
CHECK(_sp == span);
|
||||
|
||||
string_span<> _span{ _ptr, 5 };
|
||||
string_span<> _span{_ptr, 5};
|
||||
|
||||
// non-const span, non-const other type
|
||||
|
||||
@@ -344,7 +345,7 @@ SUITE(string_span_tests)
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<char> str1 = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> str1 = {'H', 'e', 'l', 'l', 'o'};
|
||||
cstring_span<> span1 = str1;
|
||||
std::vector<char> str2 = std::move(str1);
|
||||
cstring_span<> span2 = str2;
|
||||
@@ -359,12 +360,12 @@ SUITE(string_span_tests)
|
||||
{
|
||||
cstring_span<> span = "Hello";
|
||||
|
||||
const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
const char ar1[] = "Hello";
|
||||
const char ar2[10] = "Hello";
|
||||
const char* ptr = "Hello";
|
||||
const std::string str = "Hello";
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
|
||||
// comparison to literal
|
||||
CHECK(span < cstring_span<>("Helloo"));
|
||||
@@ -390,7 +391,7 @@ SUITE(string_span_tests)
|
||||
}
|
||||
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
|
||||
string_span<> span = ar;
|
||||
|
||||
@@ -401,8 +402,7 @@ SUITE(string_span_tests)
|
||||
char ar2[10] = "Hello";
|
||||
char* ptr = ar;
|
||||
std::string str = "Hello";
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
|
||||
// comparison to static array with no null termination
|
||||
CHECK(span <= string_span<>(ar));
|
||||
@@ -466,7 +466,7 @@ SUITE(string_span_tests)
|
||||
CHECK(span.length() == 6);
|
||||
}
|
||||
|
||||
// from const span of a final extent to non-const string_span
|
||||
// from const span of a final extent to non-const string_span
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
span<const char, 6> sp = "Hello";
|
||||
@@ -475,7 +475,7 @@ SUITE(string_span_tests)
|
||||
}
|
||||
#endif
|
||||
|
||||
// from string temporary
|
||||
// from string temporary
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
cstring_span<> span = std::string("Hello");
|
||||
@@ -502,14 +502,14 @@ SUITE(string_span_tests)
|
||||
|
||||
// from const static array
|
||||
{
|
||||
const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
cstring_span<> span = ar;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const static array
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
cstring_span<> span = ar;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
@@ -517,37 +517,37 @@ SUITE(string_span_tests)
|
||||
// from const ptr and length
|
||||
{
|
||||
const char* ptr = "Hello";
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
cstring_span<> span{ptr, 5};
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from const ptr and length, include 0
|
||||
{
|
||||
const char* ptr = "Hello";
|
||||
cstring_span<> span{ ptr, 6 };
|
||||
cstring_span<> span{ptr, 6};
|
||||
CHECK(span.length() == 6);
|
||||
}
|
||||
|
||||
// from const ptr and length, 0 inside
|
||||
{
|
||||
const char* ptr = "He\0lo";
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
cstring_span<> span{ptr, 5};
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const ptr and length
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
char* ptr = ar;
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
cstring_span<> span{ptr, 5};
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const ptr and length, 0 inside
|
||||
{
|
||||
char ar[] = { 'H', 'e', '\0', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', '\0', 'l', 'o'};
|
||||
char* ptr = ar;
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
cstring_span<> span{ptr, 5};
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
@@ -567,21 +567,21 @@ SUITE(string_span_tests)
|
||||
|
||||
// from const vector
|
||||
{
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const cstring_span<> span = vec;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const vector
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const cstring_span<> span = vec;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from const span
|
||||
{
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const span<const char> inner = vec;
|
||||
const cstring_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -589,7 +589,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from non-const span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const span<char> inner = vec;
|
||||
const cstring_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -597,7 +597,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from const string_span
|
||||
{
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const cstring_span<> tmp = vec;
|
||||
const cstring_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -605,7 +605,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from non-const string_span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> tmp = vec;
|
||||
cstring_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -623,7 +623,7 @@ SUITE(string_span_tests)
|
||||
// from const static array
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> span = ar;
|
||||
CHECK(span.length() == 5);
|
||||
#endif
|
||||
@@ -631,7 +631,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from non-const static array
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> span = ar;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
@@ -640,16 +640,16 @@ SUITE(string_span_tests)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const char* ptr = "Hello";
|
||||
string_span<> span{ ptr, 5 };
|
||||
string_span<> span{ptr, 5};
|
||||
CHECK(span.length() == 5);
|
||||
#endif
|
||||
}
|
||||
|
||||
// from non-const ptr and length
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char ar[] = {'H', 'e', 'l', 'l', 'o'};
|
||||
char* ptr = ar;
|
||||
string_span<> span{ ptr, 5 };
|
||||
string_span<> span{ptr, 5};
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
@@ -672,7 +672,7 @@ SUITE(string_span_tests)
|
||||
// from const vector
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> span = vec;
|
||||
CHECK(span.length() == 5);
|
||||
#endif
|
||||
@@ -680,7 +680,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from non-const vector
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> span = vec;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
@@ -688,7 +688,7 @@ SUITE(string_span_tests)
|
||||
// from const span
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const span<const char> inner = vec;
|
||||
string_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -697,7 +697,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from non-const span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
span<char> inner = vec;
|
||||
string_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -706,7 +706,7 @@ SUITE(string_span_tests)
|
||||
// from non-const span of non-const data from const vector
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const span<char> inner = vec;
|
||||
string_span<> span = inner;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -716,7 +716,7 @@ SUITE(string_span_tests)
|
||||
// from const string_span
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
cstring_span<> tmp = vec;
|
||||
string_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -725,7 +725,7 @@ SUITE(string_span_tests)
|
||||
|
||||
// from non-const string_span
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const string_span<> tmp = vec;
|
||||
const string_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -734,7 +734,7 @@ SUITE(string_span_tests)
|
||||
// from non-const string_span from const vector
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> tmp = vec;
|
||||
string_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
@@ -743,24 +743,29 @@ SUITE(string_span_tests)
|
||||
|
||||
// from const string_span of non-const data
|
||||
{
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
const string_span<> tmp = vec;
|
||||
const string_span<> span = tmp;
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T move_wrapper(T&& t)
|
||||
template <typename T>
|
||||
T move_wrapper(T && t)
|
||||
{
|
||||
return std::move(t);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T create() { return T{}; }
|
||||
T create()
|
||||
{
|
||||
return T{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void use(basic_string_span<T, gsl::dynamic_extent>) {}
|
||||
void use(basic_string_span<T, gsl::dynamic_extent>)
|
||||
{
|
||||
}
|
||||
|
||||
TEST(MoveConstructors)
|
||||
{
|
||||
@@ -817,14 +822,14 @@ SUITE(string_span_tests)
|
||||
// move container
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> span = std::move(vec);
|
||||
CHECK(span.length() == 5);
|
||||
#endif
|
||||
}
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
|
||||
string_span<> span = move_wrapper<std::vector<char>>(std::move(vec));
|
||||
CHECK(span.length() == 5);
|
||||
#endif
|
||||
@@ -840,7 +845,7 @@ SUITE(string_span_tests)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
cstring_span<> span = "Hello";
|
||||
cwstring_span<> wspan{ span };
|
||||
cwstring_span<> wspan{span};
|
||||
CHECK(wspan.length() == 5);
|
||||
#endif
|
||||
}
|
||||
@@ -850,8 +855,7 @@ SUITE(string_span_tests)
|
||||
Expects(span.size() > 1);
|
||||
|
||||
int last = 0;
|
||||
if (span.size() > 4)
|
||||
{
|
||||
if (span.size() > 4) {
|
||||
span[0] = 't';
|
||||
span[1] = 'm';
|
||||
span[2] = 'p';
|
||||
@@ -860,7 +864,7 @@ SUITE(string_span_tests)
|
||||
span[last] = '\0';
|
||||
|
||||
auto ret = span.subspan(0, 4);
|
||||
return{ ret };
|
||||
return {ret};
|
||||
}
|
||||
|
||||
TEST(zstring)
|
||||
@@ -871,7 +875,7 @@ SUITE(string_span_tests)
|
||||
char buf[1];
|
||||
buf[0] = '\0';
|
||||
|
||||
zstring_span<> zspan({ buf, 1 });
|
||||
zstring_span<> zspan({buf, 1});
|
||||
|
||||
CHECK(strlen(zspan.assume_z()) == 0);
|
||||
CHECK(zspan.as_string_span().size() == 0);
|
||||
@@ -883,7 +887,7 @@ SUITE(string_span_tests)
|
||||
char buf[1];
|
||||
buf[0] = 'a';
|
||||
|
||||
auto workaround_macro = [&]() { zstring_span<> zspan({ buf, 1 }); };
|
||||
auto workaround_macro = [&]() { zstring_span<> zspan({buf, 1}); };
|
||||
CHECK_THROW(workaround_macro(), fail_fast);
|
||||
}
|
||||
|
||||
@@ -891,15 +895,13 @@ SUITE(string_span_tests)
|
||||
{
|
||||
char buf[10];
|
||||
|
||||
auto name = CreateTempName({ buf, 10 });
|
||||
if (!name.empty())
|
||||
{
|
||||
auto name = CreateTempName({buf, 10});
|
||||
if (!name.empty()) {
|
||||
czstring<> str = name.assume_z();
|
||||
CHECK(strlen(str) == 3);
|
||||
CHECK(*(str+3) == '\0');
|
||||
CHECK(*(str + 3) == '\0');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cwzstring_span<> CreateTempNameW(wstring_span<> span)
|
||||
@@ -907,8 +909,7 @@ SUITE(string_span_tests)
|
||||
Expects(span.size() > 1);
|
||||
|
||||
int last = 0;
|
||||
if (span.size() > 4)
|
||||
{
|
||||
if (span.size() > 4) {
|
||||
span[0] = L't';
|
||||
span[1] = L'm';
|
||||
span[2] = L'p';
|
||||
@@ -917,7 +918,7 @@ SUITE(string_span_tests)
|
||||
span[last] = L'\0';
|
||||
|
||||
auto ret = span.subspan(0, 4);
|
||||
return{ ret };
|
||||
return {ret};
|
||||
}
|
||||
|
||||
TEST(wzstring)
|
||||
@@ -928,7 +929,7 @@ SUITE(string_span_tests)
|
||||
wchar_t buf[1];
|
||||
buf[0] = L'\0';
|
||||
|
||||
wzstring_span<> zspan({ buf, 1 });
|
||||
wzstring_span<> zspan({buf, 1});
|
||||
|
||||
CHECK(wcsnlen(zspan.assume_z(), 1) == 0);
|
||||
CHECK(zspan.as_string_span().size() == 0);
|
||||
@@ -940,7 +941,7 @@ SUITE(string_span_tests)
|
||||
wchar_t buf[1];
|
||||
buf[0] = L'a';
|
||||
|
||||
const auto workaround_macro = [&]() { wzstring_span<> zspan({ buf, 1 }); };
|
||||
const auto workaround_macro = [&]() { wzstring_span<> zspan({buf, 1}); };
|
||||
CHECK_THROW(workaround_macro(), fail_fast);
|
||||
}
|
||||
|
||||
@@ -948,9 +949,8 @@ SUITE(string_span_tests)
|
||||
{
|
||||
wchar_t buf[10];
|
||||
|
||||
const auto name = CreateTempNameW({ buf, 10 });
|
||||
if (!name.empty())
|
||||
{
|
||||
const auto name = CreateTempNameW({buf, 10});
|
||||
if (!name.empty()) {
|
||||
cwzstring<> str = name.assume_z();
|
||||
CHECK(wcsnlen(str, 10) == 3);
|
||||
CHECK(*(str + 3) == L'\0');
|
||||
@@ -960,13 +960,10 @@ SUITE(string_span_tests)
|
||||
|
||||
TEST(Issue305)
|
||||
{
|
||||
std::map<gsl::cstring_span<>, int> foo = { { "foo", 0 },{ "bar", 1 } };
|
||||
std::map<gsl::cstring_span<>, int> foo = {{"foo", 0}, {"bar", 1}};
|
||||
CHECK(foo["foo"] == 0);
|
||||
CHECK(foo["bar"] == 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
+6
-10
@@ -15,23 +15,22 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
|
||||
#include <gsl/gsl>
|
||||
|
||||
#include <functional>
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
SUITE(utils_tests)
|
||||
{
|
||||
void f(int& i)
|
||||
{
|
||||
i += 1;
|
||||
}
|
||||
void f(int& i) { i += 1; }
|
||||
|
||||
TEST(finally_lambda)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _ = finally([&]() {f(i);});
|
||||
auto _ = finally([&]() { f(i); });
|
||||
CHECK(i == 0);
|
||||
}
|
||||
CHECK(i == 1);
|
||||
@@ -41,7 +40,7 @@ SUITE(utils_tests)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _1 = finally([&]() {f(i);});
|
||||
auto _1 = finally([&]() { f(i); });
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 0);
|
||||
@@ -113,7 +112,4 @@ SUITE(utils_tests)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|
||||
|
||||
Reference in New Issue
Block a user