blob: 5e48419ac2a9f47eeb5f1cc105df80a41498ab0d [file] [log] [blame]
William A. Kennington IIIbe79c1e2021-01-25 16:06:31 -08001#include <array>
William A. Kennington III01db6622021-01-31 15:24:13 -08002#if __has_include(<catch2/catch.hpp>)
William A. Kennington IIIe0990382019-10-18 02:10:25 -07003#include <catch2/catch.hpp>
William A. Kennington III01db6622021-01-31 15:24:13 -08004#else
5#include <catch2/catch_test_macros.hpp>
6#endif
William A. Kennington IIIe0990382019-10-18 02:10:25 -07007#include <endian.h>
William A. Kennington III615b82a2022-08-24 13:47:17 -07008#include <span>
William A. Kennington IIIe0990382019-10-18 02:10:25 -07009#include <stdexcept>
10#include <stdplus/raw.hpp>
11#include <string_view>
12#include <vector>
13
14namespace stdplus
15{
16namespace raw
17{
18namespace
19{
20
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070021TEST_CASE("Equal", "[Equal]")
22{
23 int a = 4;
24 unsigned b = 4;
25 CHECK(equal(a, b));
26 b = 5;
27 CHECK(!equal(a, b));
28}
29
William A. Kennington IIIe0990382019-10-18 02:10:25 -070030TEST_CASE("Copy From Empty", "[CopyFrom]")
31{
William A. Kennington III5c99ff42020-07-19 02:29:26 -070032 const std::string_view cs;
33 CHECK_THROWS_AS(copyFrom<int>(cs), std::runtime_error);
34 std::string_view s;
William A. Kennington IIIe0990382019-10-18 02:10:25 -070035 CHECK_THROWS_AS(copyFrom<int>(s), std::runtime_error);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070036}
37
38TEST_CASE("Copy From Basic", "[CopyFrom]")
39{
40 int a = 4;
41 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
42 CHECK(a == copyFrom<int>(s));
43}
44
45TEST_CASE("Copy From Partial", "[CopyFrom]")
46{
47 const std::vector<char> s = {'a', 'b', 'c'};
48 CHECK('a' == copyFrom<char>(s));
49 const char s2[] = "def";
50 CHECK('d' == copyFrom<char>(s2));
51}
52
William A. Kennington III5c99ff42020-07-19 02:29:26 -070053struct Int
54{
55 uint8_t data[sizeof(int)];
56
57 inline bool operator==(const Int& other) const
58 {
59 return memcmp(data, other.data, sizeof(data)) == 0;
60 }
61};
62
63TEST_CASE("Ref From Empty", "[RefFrom]")
64{
65 const std::string_view cs;
66 CHECK_THROWS_AS(refFrom<Int>(cs), std::runtime_error);
67 std::string_view s;
68 CHECK_THROWS_AS(refFrom<Int>(s), std::runtime_error);
69}
70
71TEST_CASE("Ref From Basic", "[RefFrom]")
72{
73 Int a = {4, 0, 0, 4};
74 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
75 CHECK(a == refFrom<Int>(s));
76}
77
78TEST_CASE("Ref From Partial", "[RefFrom]")
79{
80 const std::vector<char> s = {'a', 'b', 'c'};
81 CHECK('a' == refFrom<char>(s));
82 const char s2[] = "def";
83 CHECK('d' == refFrom<char>(s2));
84}
85
William A. Kennington IIIe0990382019-10-18 02:10:25 -070086TEST_CASE("Extract Too Small", "[Extract]")
87{
88 std::string_view s("a");
89 CHECK_THROWS_AS(extract<int>(s), std::runtime_error);
90 CHECK(1 == s.size());
91}
92
93TEST_CASE("Extract Basic", "[Extract]")
94{
95 int a = 4;
96 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
97 CHECK(a == extract<int>(s));
98 CHECK(s.empty());
99}
100
101TEST_CASE("Extract Partial", "[Extract]")
102{
103 std::string_view s("abc");
104 CHECK('a' == extract<char>(s));
105 CHECK(2 == s.size());
106}
107
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700108TEST_CASE("Extract Ref Too Small", "[ExtractRef]")
109{
110 std::string_view s("a");
111 CHECK_THROWS_AS(extractRef<Int>(s), std::runtime_error);
112 CHECK(1 == s.size());
113}
114
115TEST_CASE("Extract Ref Basic", "[ExtractRef]")
116{
117 Int a = {4, 0, 0, 4};
118 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
119 CHECK(a == extractRef<Int>(s));
120 CHECK(s.empty());
121}
122
123TEST_CASE("Extract Ref Partial", "[ExtractRef]")
124{
125 std::string_view s("abc");
126 CHECK('a' == extractRef<char>(s));
127 CHECK(2 == s.size());
128}
129
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700130TEST_CASE("As View Byte", "[AsView]")
131{
132 int32_t a = 4;
133 auto s = asView<uint8_t>(a);
134 CHECK(a == copyFrom<int>(s));
135}
136
137TEST_CASE("As View Int", "[AsView]")
138{
139 int32_t a = 4;
140 auto s = asView<char16_t>(a);
141 CHECK(a == copyFrom<int>(s));
142}
143
144TEST_CASE("As View Arr", "[AsView]")
145{
146 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
147 auto s = asView<char16_t>(arr);
148 REQUIRE(4 == s.size());
149 CHECK(htole16(1) == s[0]);
150 CHECK(htole16(0) == s[1]);
151 CHECK(htole16(2) == s[2]);
152 CHECK(htole16(0) == s[3]);
153}
154
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700155TEST_CASE("As View View", "[AsView]")
156{
157 std::string_view sv = "ab";
158 auto s = asView<uint8_t>(sv);
159 REQUIRE(s.size() == 2);
160 CHECK(s[0] == sv[0]);
161 CHECK(s[1] == sv[1]);
162}
163
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700164TEST_CASE("Span Extract TooSmall", "[Extract]")
165{
166 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500167 std::span<const char> s = v;
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700168 CHECK_THROWS_AS(extract<int>(s), std::runtime_error);
169 CHECK(1 == s.size());
170}
171
172TEST_CASE("Span Extract Basic", "[Extract]")
173{
174 const std::vector<int> v = {4};
Patrick Williams68975b92022-04-27 16:00:26 -0500175 std::span<const int> s = v;
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700176 CHECK(v[0] == extract<int>(s));
177 CHECK(s.empty());
178}
179
180TEST_CASE("Span Extract Larger", "[Extract]")
181{
182 const std::vector<int> v{3, 4, 5};
Patrick Williams68975b92022-04-27 16:00:26 -0500183 std::span<const int> s = v;
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700184 CHECK(v[0] == extract<int>(s));
185 CHECK(v.size() - 1 == s.size());
186}
187
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700188TEST_CASE("Span Extract Ref TooSmall", "[ExtractRef]")
189{
190 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500191 std::span<const char> s = v;
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700192 CHECK_THROWS_AS(extractRef<Int>(s), std::runtime_error);
193 CHECK(1 == s.size());
194}
195
196TEST_CASE("Span Extract Ref Basic", "[ExtractRef]")
197{
198 const std::vector<Int> v = {{4, 0, 0, 4}};
Patrick Williams68975b92022-04-27 16:00:26 -0500199 std::span<const Int> s = v;
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700200 CHECK(v[0] == extractRef<Int>(s));
201 CHECK(s.empty());
202}
203
204TEST_CASE("Span Extract Ref Larger", "[ExtractRef]")
205{
206 const std::vector<Int> v{{3}, {4}, {5}};
Patrick Williams68975b92022-04-27 16:00:26 -0500207 std::span<const Int> s = v;
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700208 CHECK(v[0] == extractRef<Int>(s));
209 CHECK(v.size() - 1 == s.size());
210}
211
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700212TEST_CASE("As Span const", "[AsSpan]")
213{
214 const uint64_t data = htole64(0xffff0000);
215 auto s = asSpan<uint32_t>(data);
216 CHECK(s.size() == 2);
217 CHECK(s[0] == htole32(0xffff0000));
218 CHECK(s[1] == htole32(0x00000000));
219}
220
221TEST_CASE("As Span Arr const", "[AsSpan]")
222{
223 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
224 auto s = asSpan<uint16_t>(arr);
225 REQUIRE(4 == s.size());
226 CHECK(htole16(1) == s[0]);
227 CHECK(htole16(0) == s[1]);
228 CHECK(htole16(2) == s[2]);
229 CHECK(htole16(0) == s[3]);
230}
231
232TEST_CASE("As Span", "[AsSpan]")
233{
William A. Kennington IIIa9cf86f2021-08-03 11:42:11 -0700234 struct
235 {
236 uint64_t a;
237 uint64_t* data()
238 {
239 return &a;
240 }
241 } data = {htole64(0xffff0000)};
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700242 auto s = asSpan<uint16_t>(data);
243 CHECK(s.size() == 4);
244 s[2] = 0xfefe;
245 CHECK(s[0] == htole16(0x0000));
246 CHECK(s[1] == htole16(0xffff));
247 CHECK(s[2] == htole16(0xfefe));
248 CHECK(s[3] == htole16(0x0000));
249}
250
251TEST_CASE("As Span Arr", "[AsSpan]")
252{
253 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
254 auto s = asSpan<uint16_t>(arr);
255 REQUIRE(4 == s.size());
256 CHECK(htole16(1) == s[0]);
257 CHECK(htole16(0) == s[1]);
258 CHECK(htole16(2) == s[2]);
259 CHECK(htole16(0) == s[3]);
260}
261
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700262TEST_CASE("As Span Span", "[AsSpan]")
263{
264 std::array<char, 2> arr = {'a', 'b'};
Patrick Williams68975b92022-04-27 16:00:26 -0500265 auto sp1 = std::span<const char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700266 auto s1 = asSpan<uint8_t>(sp1);
267 REQUIRE(s1.size() == 2);
268 CHECK(s1[0] == arr[0]);
269 CHECK(s1[1] == arr[1]);
Patrick Williams68975b92022-04-27 16:00:26 -0500270 auto sp2 = std::span<char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700271 auto s2 = asSpan<uint8_t>(sp2);
272 REQUIRE(s2.size() == 2);
273 CHECK(s2[0] == arr[0]);
274 CHECK(s2[1] == arr[1]);
275}
276
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700277} // namespace
278} // namespace raw
279} // namespace stdplus