blob: bcfa73e6eae245c5aa56f7d9abef5c7a28931cec [file] [log] [blame]
William A. Kennington IIIe0990382019-10-18 02:10:25 -07001#include <endian.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
3#include <stdplus/raw.hpp>
4
5#include <array>
William A. Kennington III615b82a2022-08-24 13:47:17 -07006#include <span>
William A. Kennington IIIe0990382019-10-18 02:10:25 -07007#include <stdexcept>
William A. Kennington IIIe0990382019-10-18 02:10:25 -07008#include <string_view>
9#include <vector>
10
Patrick Williamsd1984dd2023-05-10 16:12:44 -050011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
William A. Kennington IIIe0990382019-10-18 02:10:25 -070014namespace stdplus
15{
16namespace raw
17{
18namespace
19{
20
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070021TEST(Equal, Equal)
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070022{
23 int a = 4;
24 unsigned b = 4;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070025 EXPECT_TRUE(equal(a, b));
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070026 b = 5;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070027 EXPECT_FALSE(equal(a, b));
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070028}
29
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070030TEST(CopyFrom, Empty)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070031{
William A. Kennington III5c99ff42020-07-19 02:29:26 -070032 const std::string_view cs;
William A. Kennington III2f6affd2024-01-28 17:49:41 -080033 EXPECT_THROW(copyFrom<int>(cs), exception::Incomplete);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070034 std::string_view s;
William A. Kennington III2f6affd2024-01-28 17:49:41 -080035 EXPECT_THROW(copyFrom<int>(s), exception::Incomplete);
36 EXPECT_THROW(copyFromStrict<char>(s), exception::Incomplete);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070037}
38
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070039TEST(CopyFrom, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070040{
41 int a = 4;
42 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070043 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070044 EXPECT_EQ(a, copyFromStrict<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070045}
46
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070047TEST(CopyFrom, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070048{
49 const std::vector<char> s = {'a', 'b', 'c'};
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070050 EXPECT_EQ('a', copyFrom<char>(s));
William A. Kennington III2f6affd2024-01-28 17:49:41 -080051 EXPECT_THROW(copyFromStrict<char>(s), exception::Incomplete);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070052 const char s2[] = "def";
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070053 EXPECT_EQ('d', copyFrom<char>(s2));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070054}
55
William A. Kennington III5c99ff42020-07-19 02:29:26 -070056struct Int
57{
58 uint8_t data[sizeof(int)];
59
60 inline bool operator==(const Int& other) const
61 {
62 return memcmp(data, other.data, sizeof(data)) == 0;
63 }
64};
65
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070066TEST(RefFrom, Empty)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070067{
68 const std::string_view cs;
William A. Kennington III2f6affd2024-01-28 17:49:41 -080069 EXPECT_THROW(refFrom<Int>(cs), exception::Incomplete);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070070 std::string_view s;
William A. Kennington III2f6affd2024-01-28 17:49:41 -080071 EXPECT_THROW(refFrom<Int>(s), exception::Incomplete);
72 EXPECT_THROW(refFromStrict<Int>(s), exception::Incomplete);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070073}
74
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070075TEST(RefFrom, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070076{
77 Int a = {4, 0, 0, 4};
78 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070079 EXPECT_EQ(a, refFrom<Int>(s));
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070080 EXPECT_EQ(a, refFromStrict<Int>(s));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070081}
82
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070083TEST(RefFrom, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070084{
85 const std::vector<char> s = {'a', 'b', 'c'};
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070086 EXPECT_EQ('a', refFrom<char>(s));
William A. Kennington III2f6affd2024-01-28 17:49:41 -080087 EXPECT_THROW(refFromStrict<char>(s), exception::Incomplete);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070088 const char s2[] = "def";
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070089 EXPECT_EQ('d', refFrom<char>(s2));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070090}
91
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070092TEST(Extract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070093{
William A. Kennington III8e056312022-11-18 19:42:45 -080094 std::string str = "aaaa";
95 str.resize(1);
96 std::string_view s(str);
William A. Kennington III2f6affd2024-01-28 17:49:41 -080097 EXPECT_THROW(extract<int>(s), exception::Incomplete);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070098 EXPECT_EQ("a", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070099}
100
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700101TEST(Extract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700102{
103 int a = 4;
104 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700105 EXPECT_EQ(a, extract<int>(s));
106 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700107}
108
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700109TEST(Extract, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700110{
111 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700112 EXPECT_EQ('a', extract<char>(s));
113 EXPECT_EQ("bc", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700114}
115
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700116TEST(ExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700117{
William A. Kennington III1df92762023-01-12 10:34:13 -0800118 // Trick the compiler with a bigger buffer so it doesn't complain
119 std::array<char, sizeof(Int)> buf{};
120 buf[0] = 'a';
121 std::string_view s(buf.data(), 1);
William A. Kennington III2f6affd2024-01-28 17:49:41 -0800122 EXPECT_THROW(extractRef<Int>(s), exception::Incomplete);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700123 EXPECT_EQ("a", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700124}
125
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700126TEST(ExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700127{
128 Int a = {4, 0, 0, 4};
129 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700130 EXPECT_EQ(a, extractRef<Int>(s));
131 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700132}
133
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700134TEST(ExtractRef, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700135{
136 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700137 EXPECT_EQ('a', extractRef<char>(s));
138 EXPECT_EQ("bc", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700139}
140
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700141TEST(AsView, Byte)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700142{
143 int32_t a = 4;
144 auto s = asView<uint8_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700145 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700146}
147
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700148TEST(AsView, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700149{
150 int32_t a = 4;
151 auto s = asView<char16_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700152 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700153}
154
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700155using testing::ElementsAre;
156using testing::ElementsAreArray;
157
158TEST(AsView, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700159{
160 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
161 auto s = asView<char16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700162 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700163}
164
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700165TEST(AsView, StringView)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700166{
167 std::string_view sv = "ab";
168 auto s = asView<uint8_t>(sv);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700169 EXPECT_THAT(s, ElementsAreArray(sv));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700170}
171
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700172TEST(SpanExtract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700173{
174 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500175 std::span<const char> s = v;
William A. Kennington III2f6affd2024-01-28 17:49:41 -0800176 EXPECT_THROW(extract<int>(s), exception::Incomplete);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700177 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700178}
179
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700180TEST(SpanExtract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700181{
182 const std::vector<int> v = {4};
Patrick Williams68975b92022-04-27 16:00:26 -0500183 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700184 EXPECT_EQ(v[0], extract<int>(s));
185 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700186}
187
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700188TEST(SpanExtract, Larger)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700189{
190 const std::vector<int> v{3, 4, 5};
Patrick Williams68975b92022-04-27 16:00:26 -0500191 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700192 EXPECT_EQ(v[0], extract<int>(s));
193 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700194}
195
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700196TEST(SpanExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700197{
198 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500199 std::span<const char> s = v;
William A. Kennington III2f6affd2024-01-28 17:49:41 -0800200 EXPECT_THROW(extractRef<Int>(s), exception::Incomplete);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700201 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700202}
203
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700204TEST(SpanExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700205{
206 const std::vector<Int> v = {{4, 0, 0, 4}};
Patrick Williams68975b92022-04-27 16:00:26 -0500207 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700208 EXPECT_EQ(v[0], extractRef<Int>(s));
209 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700210}
211
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700212TEST(SpanExtractRef, Larger)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700213{
214 const std::vector<Int> v{{3}, {4}, {5}};
Patrick Williams68975b92022-04-27 16:00:26 -0500215 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700216 EXPECT_EQ(v[0], extractRef<Int>(s));
217 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700218}
219
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700220TEST(AsSpan, ConstInt)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700221{
222 const uint64_t data = htole64(0xffff0000);
223 auto s = asSpan<uint32_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700224 EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700225}
226
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700227TEST(AsSpan, ConstArray)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700228{
229 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
230 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700231 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700232}
233
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700234TEST(AsSpan, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700235{
William A. Kennington IIIa9cf86f2021-08-03 11:42:11 -0700236 struct
237 {
238 uint64_t a;
239 uint64_t* data()
240 {
241 return &a;
242 }
243 } data = {htole64(0xffff0000)};
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700244 auto s = asSpan<uint16_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700245 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
246 htole16(0x0000), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700247 s[2] = 0xfefe;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700248 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
249 htole16(0xfefe), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700250}
251
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700252TEST(AsSpan, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700253{
254 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
255 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700256 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700257}
258
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700259TEST(AsSpan, Span)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700260{
261 std::array<char, 2> arr = {'a', 'b'};
Patrick Williams68975b92022-04-27 16:00:26 -0500262 auto sp1 = std::span<const char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700263 auto s1 = asSpan<uint8_t>(sp1);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700264 EXPECT_THAT(s1, ElementsAreArray(arr));
Patrick Williams68975b92022-04-27 16:00:26 -0500265 auto sp2 = std::span<char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700266 auto s2 = asSpan<uint8_t>(sp2);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700267 EXPECT_THAT(s2, ElementsAreArray(arr));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700268}
269
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700270} // namespace
271} // namespace raw
272} // namespace stdplus