blob: fa3c77e07cc7cd986107e8f80526c81df4c4159d [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 III6c15d9a2022-08-24 13:45:42 -070033 EXPECT_THROW(copyFrom<int>(cs), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070034 std::string_view s;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070035 EXPECT_THROW(copyFrom<int>(s), std::runtime_error);
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070036 EXPECT_THROW(copyFromStrict<char>(s), std::runtime_error);
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 IIIfca0fdf2022-10-11 12:21:29 -070051 EXPECT_THROW(copyFromStrict<char>(s), std::runtime_error);
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 III6c15d9a2022-08-24 13:45:42 -070069 EXPECT_THROW(refFrom<Int>(cs), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070070 std::string_view s;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070071 EXPECT_THROW(refFrom<Int>(s), std::runtime_error);
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070072 EXPECT_THROW(refFromStrict<Int>(s), std::runtime_error);
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 IIIfca0fdf2022-10-11 12:21:29 -070087 EXPECT_THROW(refFromStrict<char>(s), std::runtime_error);
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 III6c15d9a2022-08-24 13:45:42 -070097 EXPECT_THROW(extract<int>(s), std::runtime_error);
98 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{
118 std::string_view s("a");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700119 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
120 EXPECT_EQ("a", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700121}
122
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700123TEST(ExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700124{
125 Int a = {4, 0, 0, 4};
126 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700127 EXPECT_EQ(a, extractRef<Int>(s));
128 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700129}
130
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700131TEST(ExtractRef, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700132{
133 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700134 EXPECT_EQ('a', extractRef<char>(s));
135 EXPECT_EQ("bc", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700136}
137
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700138TEST(AsView, Byte)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700139{
140 int32_t a = 4;
141 auto s = asView<uint8_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700142 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700143}
144
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700145TEST(AsView, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700146{
147 int32_t a = 4;
148 auto s = asView<char16_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700149 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700150}
151
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700152using testing::ElementsAre;
153using testing::ElementsAreArray;
154
155TEST(AsView, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700156{
157 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
158 auto s = asView<char16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700159 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700160}
161
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700162TEST(AsView, StringView)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700163{
164 std::string_view sv = "ab";
165 auto s = asView<uint8_t>(sv);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700166 EXPECT_THAT(s, ElementsAreArray(sv));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700167}
168
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700169TEST(SpanExtract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700170{
171 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500172 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700173 EXPECT_THROW(extract<int>(s), std::runtime_error);
174 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700175}
176
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700177TEST(SpanExtract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700178{
179 const std::vector<int> v = {4};
Patrick Williams68975b92022-04-27 16:00:26 -0500180 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700181 EXPECT_EQ(v[0], extract<int>(s));
182 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700183}
184
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700185TEST(SpanExtract, Larger)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700186{
187 const std::vector<int> v{3, 4, 5};
Patrick Williams68975b92022-04-27 16:00:26 -0500188 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700189 EXPECT_EQ(v[0], extract<int>(s));
190 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700191}
192
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700193TEST(SpanExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700194{
195 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500196 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700197 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
198 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700199}
200
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700201TEST(SpanExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700202{
203 const std::vector<Int> v = {{4, 0, 0, 4}};
Patrick Williams68975b92022-04-27 16:00:26 -0500204 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700205 EXPECT_EQ(v[0], extractRef<Int>(s));
206 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700207}
208
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700209TEST(SpanExtractRef, Larger)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700210{
211 const std::vector<Int> v{{3}, {4}, {5}};
Patrick Williams68975b92022-04-27 16:00:26 -0500212 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700213 EXPECT_EQ(v[0], extractRef<Int>(s));
214 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700215}
216
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700217TEST(AsSpan, ConstInt)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700218{
219 const uint64_t data = htole64(0xffff0000);
220 auto s = asSpan<uint32_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700221 EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700222}
223
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700224TEST(AsSpan, ConstArray)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700225{
226 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
227 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700228 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700229}
230
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700231TEST(AsSpan, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700232{
William A. Kennington IIIa9cf86f2021-08-03 11:42:11 -0700233 struct
234 {
235 uint64_t a;
236 uint64_t* data()
237 {
238 return &a;
239 }
240 } data = {htole64(0xffff0000)};
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700241 auto s = asSpan<uint16_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700242 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
243 htole16(0x0000), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700244 s[2] = 0xfefe;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700245 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
246 htole16(0xfefe), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700247}
248
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700249TEST(AsSpan, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700250{
251 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
252 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700253 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700254}
255
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700256TEST(AsSpan, Span)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700257{
258 std::array<char, 2> arr = {'a', 'b'};
Patrick Williams68975b92022-04-27 16:00:26 -0500259 auto sp1 = std::span<const char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700260 auto s1 = asSpan<uint8_t>(sp1);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700261 EXPECT_THAT(s1, ElementsAreArray(arr));
Patrick Williams68975b92022-04-27 16:00:26 -0500262 auto sp2 = std::span<char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700263 auto s2 = asSpan<uint8_t>(sp2);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700264 EXPECT_THAT(s2, ElementsAreArray(arr));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700265}
266
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700267} // namespace
268} // namespace raw
269} // namespace stdplus