preCICE
v3.1.2
Loading...
Searching...
No Matches
src
precice
tests
SpanTests.cpp
Go to the documentation of this file.
1
#include <
array
>
2
#include <
string
>
3
#include <
string_view
>
4
#include <
vector
>
5
6
#include "
precice/span.hpp
"
7
#include "
testing/Testing.hpp
"
8
9
BOOST_AUTO_TEST_SUITE
(PreciceTests)
10
BOOST_AUTO_TEST_SUITE
(Span)
11
12
using
precice::span
;
13
14
BOOST_AUTO_TEST_CASE
(FromDoubleVector)
15
{
16
PRECICE_TEST
(1_rank);
17
std::vector<double>
v{1.0, 2.0, 3.0, 4.0};
18
span<const double>
const_span{v};
19
BOOST_TEST(v == const_span, boost::test_tools::per_element());
20
span<double>
mut_span{v};
21
BOOST_TEST(v == mut_span, boost::test_tools::per_element());
22
}
23
24
BOOST_AUTO_TEST_CASE
(FromIntVector)
25
{
26
PRECICE_TEST
(1_rank);
27
std::vector<int>
v{1, 2, 3, 4};
28
span<const int>
const_span{v};
29
BOOST_TEST(v == const_span, boost::test_tools::per_element());
30
span<int>
mut_span{v};
31
BOOST_TEST(v == mut_span, boost::test_tools::per_element());
32
}
33
34
BOOST_AUTO_TEST_CASE
(FromString)
35
{
36
PRECICE_TEST
(1_rank);
37
std::string
s =
"hello there"
;
38
span<const char>
const_span{s};
39
BOOST_TEST(s ==
std::string
(const_span.data(), const_span.size()));
40
}
41
42
BOOST_AUTO_TEST_CASE
(FromCharVec)
43
{
44
PRECICE_TEST
(1_rank);
45
std::vector<char>
s{
'h'
,
'e'
,
'l'
,
'l'
,
'o'
,
' '
,
't'
,
'h'
,
'e'
,
'r'
,
'e'
};
46
span<const char>
const_span{s};
47
BOOST_CHECK_EQUAL_COLLECTIONS(
48
s.begin(), s.end(),
49
const_span.begin(), const_span.end());
50
}
51
52
BOOST_AUTO_TEST_CASE
(FromLiteral)
53
{
54
PRECICE_TEST
(1_rank);
55
std::string
s =
"hello there"
;
56
span<const char>
const_span{
"hello there"
};
// Includes the NULL byte
57
58
BOOST_CHECK_EQUAL_COLLECTIONS(s.
begin
(), ++s.
end
(),
59
const_span.begin(), const_span.end());
60
}
61
62
BOOST_AUTO_TEST_CASE
(FromStdArray)
63
{
64
PRECICE_TEST
(1_rank);
65
std::array<char, 11>
s{
'h'
,
'e'
,
'l'
,
'l'
,
'o'
,
' '
,
't'
,
'h'
,
'e'
,
'r'
,
'e'
};
66
span<const char>
const_span{s};
67
68
BOOST_CHECK_EQUAL_COLLECTIONS(
69
s.begin(), s.end(),
70
const_span.begin(), const_span.end());
71
}
72
73
BOOST_AUTO_TEST_CASE
(FromCArray)
74
{
75
PRECICE_TEST
(1_rank);
76
char
s[] =
"hello there"
;
77
span<const char>
const_span{s};
// Includes the NULL byte
78
79
BOOST_CHECK_EQUAL_COLLECTIONS(
80
s, s + 12,
81
const_span.begin(), const_span.end());
82
}
83
84
BOOST_AUTO_TEST_CASE
(FromStringRef)
85
{
86
PRECICE_TEST
(1_rank);
87
std::string
s =
"hello there"
;
88
std::string
& sr = s;
89
span<const char>
const_span{sr};
90
BOOST_TEST(s ==
std::string
(const_span.data(), const_span.size()));
91
}
92
93
BOOST_AUTO_TEST_CASE
(FromConstStringRef)
94
{
95
PRECICE_TEST
(1_rank);
96
std::string
s =
"hello there"
;
97
const
std::string
&scr = s;
98
span<const char>
const_span{scr};
99
BOOST_TEST(s ==
std::string
(const_span.data(), const_span.size()));
100
}
101
102
BOOST_AUTO_TEST_CASE
(FromCString)
103
{
104
PRECICE_TEST
(1_rank);
105
std::string
s =
"hello there"
;
106
char
* sp = s.
data
();
107
span<const char>
const_span{sp};
108
BOOST_TEST(s ==
std::string
(const_span.data(), const_span.size()));
109
}
110
111
BOOST_AUTO_TEST_CASE
(FromConstCString)
112
{
113
PRECICE_TEST
(1_rank);
114
const
char
* s =
"hello there"
;
115
span<const char>
const_span{s};
116
BOOST_TEST(s ==
std::string
(const_span.data(), const_span.size()));
117
}
118
119
BOOST_AUTO_TEST_CASE
(ToStringView)
120
{
121
PRECICE_TEST
(1_rank);
122
const
char
* s =
"hello there"
;
123
span<const char>
const_span{s};
124
std::string_view
sv{const_span.
data
(), const_span.size()};
125
126
BOOST_TEST(s ==
std::string
(const_span.data(), const_span.size()));
127
BOOST_TEST(s == sv);
128
}
129
130
BOOST_AUTO_TEST_SUITE_END
()
131
BOOST_AUTO_TEST_SUITE_END
()
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(FromDoubleVector)
Definition
SpanTests.cpp:14
Testing.hpp
PRECICE_TEST
#define PRECICE_TEST(...)
Definition
Testing.hpp:27
array
std::string
std::string_view
std::string::begin
T begin(T... args)
precice::span
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition
span.hpp:284
std::string::data
T data(T... args)
std::string::end
T end(T... args)
span.hpp
string
string_view
vector