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