blob: 8be8b7b0f8cef55fc0f3f17f6cb6129f991f3332 [file] [log] [blame]
William A. Kennington IIIe0990382019-10-18 02:10:25 -07001#include <catch2/catch.hpp>
2#include <endian.h>
3#include <stdexcept>
4#include <stdplus/raw.hpp>
5#include <string_view>
6#include <vector>
7
8namespace stdplus
9{
10namespace raw
11{
12namespace
13{
14
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070015TEST_CASE("Equal", "[Equal]")
16{
17 int a = 4;
18 unsigned b = 4;
19 CHECK(equal(a, b));
20 b = 5;
21 CHECK(!equal(a, b));
22}
23
William A. Kennington IIIe0990382019-10-18 02:10:25 -070024TEST_CASE("Copy From Empty", "[CopyFrom]")
25{
William A. Kennington III5c99ff42020-07-19 02:29:26 -070026 const std::string_view cs;
27 CHECK_THROWS_AS(copyFrom<int>(cs), std::runtime_error);
28 std::string_view s;
William A. Kennington IIIe0990382019-10-18 02:10:25 -070029 CHECK_THROWS_AS(copyFrom<int>(s), std::runtime_error);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070030}
31
32TEST_CASE("Copy From Basic", "[CopyFrom]")
33{
34 int a = 4;
35 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
36 CHECK(a == copyFrom<int>(s));
37}
38
39TEST_CASE("Copy From Partial", "[CopyFrom]")
40{
41 const std::vector<char> s = {'a', 'b', 'c'};
42 CHECK('a' == copyFrom<char>(s));
43 const char s2[] = "def";
44 CHECK('d' == copyFrom<char>(s2));
45}
46
William A. Kennington III5c99ff42020-07-19 02:29:26 -070047struct Int
48{
49 uint8_t data[sizeof(int)];
50
51 inline bool operator==(const Int& other) const
52 {
53 return memcmp(data, other.data, sizeof(data)) == 0;
54 }
55};
56
57TEST_CASE("Ref From Empty", "[RefFrom]")
58{
59 const std::string_view cs;
60 CHECK_THROWS_AS(refFrom<Int>(cs), std::runtime_error);
61 std::string_view s;
62 CHECK_THROWS_AS(refFrom<Int>(s), std::runtime_error);
63}
64
65TEST_CASE("Ref From Basic", "[RefFrom]")
66{
67 Int a = {4, 0, 0, 4};
68 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
69 CHECK(a == refFrom<Int>(s));
70}
71
72TEST_CASE("Ref From Partial", "[RefFrom]")
73{
74 const std::vector<char> s = {'a', 'b', 'c'};
75 CHECK('a' == refFrom<char>(s));
76 const char s2[] = "def";
77 CHECK('d' == refFrom<char>(s2));
78}
79
William A. Kennington IIIe0990382019-10-18 02:10:25 -070080TEST_CASE("Extract Too Small", "[Extract]")
81{
82 std::string_view s("a");
83 CHECK_THROWS_AS(extract<int>(s), std::runtime_error);
84 CHECK(1 == s.size());
85}
86
87TEST_CASE("Extract Basic", "[Extract]")
88{
89 int a = 4;
90 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
91 CHECK(a == extract<int>(s));
92 CHECK(s.empty());
93}
94
95TEST_CASE("Extract Partial", "[Extract]")
96{
97 std::string_view s("abc");
98 CHECK('a' == extract<char>(s));
99 CHECK(2 == s.size());
100}
101
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700102TEST_CASE("Extract Ref Too Small", "[ExtractRef]")
103{
104 std::string_view s("a");
105 CHECK_THROWS_AS(extractRef<Int>(s), std::runtime_error);
106 CHECK(1 == s.size());
107}
108
109TEST_CASE("Extract Ref Basic", "[ExtractRef]")
110{
111 Int a = {4, 0, 0, 4};
112 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
113 CHECK(a == extractRef<Int>(s));
114 CHECK(s.empty());
115}
116
117TEST_CASE("Extract Ref Partial", "[ExtractRef]")
118{
119 std::string_view s("abc");
120 CHECK('a' == extractRef<char>(s));
121 CHECK(2 == s.size());
122}
123
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700124TEST_CASE("As View Byte", "[AsView]")
125{
126 int32_t a = 4;
127 auto s = asView<uint8_t>(a);
128 CHECK(a == copyFrom<int>(s));
129}
130
131TEST_CASE("As View Int", "[AsView]")
132{
133 int32_t a = 4;
134 auto s = asView<char16_t>(a);
135 CHECK(a == copyFrom<int>(s));
136}
137
138TEST_CASE("As View Arr", "[AsView]")
139{
140 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
141 auto s = asView<char16_t>(arr);
142 REQUIRE(4 == s.size());
143 CHECK(htole16(1) == s[0]);
144 CHECK(htole16(0) == s[1]);
145 CHECK(htole16(2) == s[2]);
146 CHECK(htole16(0) == s[3]);
147}
148
149#ifdef STDPLUS_SPAN_TYPE
150TEST_CASE("Span Extract TooSmall", "[Extract]")
151{
152 const std::vector<char> v = {'c'};
153 span<const char> s = v;
154 CHECK_THROWS_AS(extract<int>(s), std::runtime_error);
155 CHECK(1 == s.size());
156}
157
158TEST_CASE("Span Extract Basic", "[Extract]")
159{
160 const std::vector<int> v = {4};
161 span<const int> s = v;
162 CHECK(v[0] == extract<int>(s));
163 CHECK(s.empty());
164}
165
166TEST_CASE("Span Extract Larger", "[Extract]")
167{
168 const std::vector<int> v{3, 4, 5};
169 span<const int> s = v;
170 CHECK(v[0] == extract<int>(s));
171 CHECK(v.size() - 1 == s.size());
172}
173
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700174TEST_CASE("Span Extract Ref TooSmall", "[ExtractRef]")
175{
176 const std::vector<char> v = {'c'};
177 span<const char> s = v;
178 CHECK_THROWS_AS(extractRef<Int>(s), std::runtime_error);
179 CHECK(1 == s.size());
180}
181
182TEST_CASE("Span Extract Ref Basic", "[ExtractRef]")
183{
184 const std::vector<Int> v = {{4, 0, 0, 4}};
185 span<const Int> s = v;
186 CHECK(v[0] == extractRef<Int>(s));
187 CHECK(s.empty());
188}
189
190TEST_CASE("Span Extract Ref Larger", "[ExtractRef]")
191{
192 const std::vector<Int> v{{3}, {4}, {5}};
193 span<const Int> s = v;
194 CHECK(v[0] == extractRef<Int>(s));
195 CHECK(v.size() - 1 == s.size());
196}
197
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700198TEST_CASE("As Span const", "[AsSpan]")
199{
200 const uint64_t data = htole64(0xffff0000);
201 auto s = asSpan<uint32_t>(data);
202 CHECK(s.size() == 2);
203 CHECK(s[0] == htole32(0xffff0000));
204 CHECK(s[1] == htole32(0x00000000));
205}
206
207TEST_CASE("As Span Arr const", "[AsSpan]")
208{
209 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
210 auto s = asSpan<uint16_t>(arr);
211 REQUIRE(4 == s.size());
212 CHECK(htole16(1) == s[0]);
213 CHECK(htole16(0) == s[1]);
214 CHECK(htole16(2) == s[2]);
215 CHECK(htole16(0) == s[3]);
216}
217
218TEST_CASE("As Span", "[AsSpan]")
219{
220 uint64_t data = htole64(0xffff0000);
221 auto s = asSpan<uint16_t>(data);
222 CHECK(s.size() == 4);
223 s[2] = 0xfefe;
224 CHECK(s[0] == htole16(0x0000));
225 CHECK(s[1] == htole16(0xffff));
226 CHECK(s[2] == htole16(0xfefe));
227 CHECK(s[3] == htole16(0x0000));
228}
229
230TEST_CASE("As Span Arr", "[AsSpan]")
231{
232 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
233 auto s = asSpan<uint16_t>(arr);
234 REQUIRE(4 == s.size());
235 CHECK(htole16(1) == s[0]);
236 CHECK(htole16(0) == s[1]);
237 CHECK(htole16(2) == s[2]);
238 CHECK(htole16(0) == s[3]);
239}
240
241#endif
242
243} // namespace
244} // namespace raw
245} // namespace stdplus