blob: 81de96d3f14d85c5fe4bdb7fc557def150f7e249 [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
15TEST_CASE("Copy From Empty", "[CopyFrom]")
16{
17 const std::string_view s;
18 CHECK_THROWS_AS(copyFrom<int>(s), std::runtime_error);
19 CHECK(s.empty());
20}
21
22TEST_CASE("Copy From Basic", "[CopyFrom]")
23{
24 int a = 4;
25 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
26 CHECK(a == copyFrom<int>(s));
27}
28
29TEST_CASE("Copy From Partial", "[CopyFrom]")
30{
31 const std::vector<char> s = {'a', 'b', 'c'};
32 CHECK('a' == copyFrom<char>(s));
33 const char s2[] = "def";
34 CHECK('d' == copyFrom<char>(s2));
35}
36
37TEST_CASE("Extract Too Small", "[Extract]")
38{
39 std::string_view s("a");
40 CHECK_THROWS_AS(extract<int>(s), std::runtime_error);
41 CHECK(1 == s.size());
42}
43
44TEST_CASE("Extract Basic", "[Extract]")
45{
46 int a = 4;
47 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
48 CHECK(a == extract<int>(s));
49 CHECK(s.empty());
50}
51
52TEST_CASE("Extract Partial", "[Extract]")
53{
54 std::string_view s("abc");
55 CHECK('a' == extract<char>(s));
56 CHECK(2 == s.size());
57}
58
59TEST_CASE("As View Byte", "[AsView]")
60{
61 int32_t a = 4;
62 auto s = asView<uint8_t>(a);
63 CHECK(a == copyFrom<int>(s));
64}
65
66TEST_CASE("As View Int", "[AsView]")
67{
68 int32_t a = 4;
69 auto s = asView<char16_t>(a);
70 CHECK(a == copyFrom<int>(s));
71}
72
73TEST_CASE("As View Arr", "[AsView]")
74{
75 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
76 auto s = asView<char16_t>(arr);
77 REQUIRE(4 == s.size());
78 CHECK(htole16(1) == s[0]);
79 CHECK(htole16(0) == s[1]);
80 CHECK(htole16(2) == s[2]);
81 CHECK(htole16(0) == s[3]);
82}
83
84#ifdef STDPLUS_SPAN_TYPE
85TEST_CASE("Span Extract TooSmall", "[Extract]")
86{
87 const std::vector<char> v = {'c'};
88 span<const char> s = v;
89 CHECK_THROWS_AS(extract<int>(s), std::runtime_error);
90 CHECK(1 == s.size());
91}
92
93TEST_CASE("Span Extract Basic", "[Extract]")
94{
95 const std::vector<int> v = {4};
96 span<const int> s = v;
97 CHECK(v[0] == extract<int>(s));
98 CHECK(s.empty());
99}
100
101TEST_CASE("Span Extract Larger", "[Extract]")
102{
103 const std::vector<int> v{3, 4, 5};
104 span<const int> s = v;
105 CHECK(v[0] == extract<int>(s));
106 CHECK(v.size() - 1 == s.size());
107}
108
109TEST_CASE("As Span const", "[AsSpan]")
110{
111 const uint64_t data = htole64(0xffff0000);
112 auto s = asSpan<uint32_t>(data);
113 CHECK(s.size() == 2);
114 CHECK(s[0] == htole32(0xffff0000));
115 CHECK(s[1] == htole32(0x00000000));
116}
117
118TEST_CASE("As Span Arr const", "[AsSpan]")
119{
120 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
121 auto s = asSpan<uint16_t>(arr);
122 REQUIRE(4 == s.size());
123 CHECK(htole16(1) == s[0]);
124 CHECK(htole16(0) == s[1]);
125 CHECK(htole16(2) == s[2]);
126 CHECK(htole16(0) == s[3]);
127}
128
129TEST_CASE("As Span", "[AsSpan]")
130{
131 uint64_t data = htole64(0xffff0000);
132 auto s = asSpan<uint16_t>(data);
133 CHECK(s.size() == 4);
134 s[2] = 0xfefe;
135 CHECK(s[0] == htole16(0x0000));
136 CHECK(s[1] == htole16(0xffff));
137 CHECK(s[2] == htole16(0xfefe));
138 CHECK(s[3] == htole16(0x0000));
139}
140
141TEST_CASE("As Span Arr", "[AsSpan]")
142{
143 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
144 auto s = asSpan<uint16_t>(arr);
145 REQUIRE(4 == s.size());
146 CHECK(htole16(1) == s[0]);
147 CHECK(htole16(0) == s[1]);
148 CHECK(htole16(2) == s[2]);
149 CHECK(htole16(0) == s[3]);
150}
151
152#endif
153
154} // namespace
155} // namespace raw
156} // namespace stdplus