Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
span_tests.cpp
Go to the documentation of this file.
1#include <sysio/vm/span.hpp>
2
3#include <catch2/catch.hpp>
4
6
7TEST_CASE("span static_extent tests", "[span_static_extent]") {
8 // currently we only support a static extent of 1
9 int a = 0;
10 span<int, 1> s(&a, 1);
11
12 CHECK(s.data() == std::addressof(a));
13 CHECK(s.front() == a);
14 s.front() += 3;
15 CHECK(s.front() == a);
16 CHECK(3 == a);
17
18 int* lta = &a;
19 CHECK_THROWS_AS(span<int>(&a, lta-1), sysio::vm::span_exception);
20}
21
22TEST_CASE("default constructor", "[span_dynamic_extent]") {
23 //static_assert(std::is_constructible_v<span<int, 0>>);
24 static_assert(!std::is_constructible_v<span<int, 1>>);
25 static_assert(std::is_constructible_v<span<int>>);
27 CHECK(s.size() == 0);
28 CHECK(s.data() == nullptr);
29}
30
31TEST_CASE("array constructor", "[span]") {
32 static_assert(std::is_constructible_v<span<int>, int(&)[1]>);
33 static_assert(std::is_constructible_v<span<const int>, int(&)[1]>);
34 static_assert(std::is_constructible_v<span<const int>, const int(&)[1]>);
35 static_assert(std::is_constructible_v<span<int, 1>, int(&)[1]>);
36 static_assert(std::is_constructible_v<span<const int, 1>, int(&)[1]>);
37 static_assert(std::is_constructible_v<span<const int, 1>, const int(&)[1]>);
38 static_assert(!std::is_constructible_v<span<int>, long(&)[1]>);
39 static_assert(!std::is_constructible_v<span<int, 1>, int(&)[2]>);
40 static_assert(!std::is_constructible_v<span<int>, const int(&)[1]>);
41 static_assert(!std::is_constructible_v<span<int, 1>, const int(&)[1]>);
42 int a1[1];
43 span<int> s1(a1);
44 CHECK(s1.data() == a1);
45 CHECK(s1.size() == 1);
46}
47
48TEST_CASE("std::array constructor", "[span]") {
49 static_assert(std::is_constructible_v<span<int>, std::array<int, 1>&>);
50 static_assert(std::is_constructible_v<span<const int>, std::array<int, 1>&>);
51 static_assert(std::is_constructible_v<span<const int>, const std::array<int, 1>&>);
52 static_assert(std::is_constructible_v<span<int, 1>, std::array<int, 1>&>);
53 static_assert(std::is_constructible_v<span<const int, 1>, std::array<int, 1>&>);
54 static_assert(std::is_constructible_v<span<const int, 1>, const std::array<int, 1>&>);
55 static_assert(!std::is_constructible_v<span<int>, std::array<long, 1>&>);
56 static_assert(!std::is_constructible_v<span<int, 1>, std::array<int, 2>&>);
57 static_assert(!std::is_constructible_v<span<int>, const std::array<int, 1>&>);
58 static_assert(!std::is_constructible_v<span<int, 1>, const std::array<int, 1>&>);
59 std::array<int, 1> a1;
60 span<int> s1(a1);
61 CHECK(s1.data() == a1.data());
62 CHECK(s1.size() == 1);
63}
64
65namespace sysio::vm {
66template<typename T, std::size_t Extent>
67bool operator==(const span<T, Extent>& lhs, const span<T, Extent>& rhs) {
68 return lhs.data() == rhs.data() && lhs.size() == rhs.size();
69}
70}
71
72TEST_CASE("span dynamic_extent tests", "[span_dynamic_extent]") {
73 // currently we only support a static extent of 1
74 int a[10] = {1};
75 span<int> s(a, 10);
76
77 CHECK(s.data() == a);
78 CHECK(s.front() == a[0]);
79 s.front() += 3;
80 CHECK(s.front() == a[0]);
81 CHECK(4 == a[0]);
82
83 span<int> test_a(a, 3);
84 span<int> test_b(a + 7, 3);
85
86 for ( auto& elem : s ) {
87 elem = 7;
88 }
89
90 for ( int i=0; i < 10; i++ ) {
91 CHECK(a[i] == s[i]);
92 }
93
94 CHECK(s.first(3) == span<int>(a, 3));
95 CHECK(s.last(3) == span<int>(a+7, 3));
96 CHECK(s.subspan(3, 3) == span<int>(a+3, 3));
97 CHECK(s.subspan(3, sysio::vm::dynamic_extent) == span<int>(a+3, 7));
98}
constexpr std::size_t size() const noexcept
Definition span.hpp:73
constexpr T * data() const noexcept
Definition span.hpp:72
#define CHECK(cond)
Definition util.h:80
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
#define TEST_CASE(...)
Definition catch.hpp:222
bool operator==(const host_function &lhs, const func_type &rhs)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
char * s