blob: 5248b9fb558dfda8b0702541ccd6d0f94ee1f26a [file] [log] [blame]
William A. Kennington IIIbe79c1e2021-01-25 16:06:31 -08001#include <array>
William A. Kennington IIIe0990382019-10-18 02:10:25 -07002#include <endian.h>
William A. Kennington III6c15d9a2022-08-24 13:45:42 -07003#include <gmock/gmock.h>
4#include <gtest/gtest.h>
William A. Kennington III615b82a2022-08-24 13:47:17 -07005#include <span>
William A. Kennington IIIe0990382019-10-18 02:10:25 -07006#include <stdexcept>
7#include <stdplus/raw.hpp>
8#include <string_view>
9#include <vector>
10
11namespace stdplus
12{
13namespace raw
14{
15namespace
16{
17
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070018TEST(Equal, Equal)
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070019{
20 int a = 4;
21 unsigned b = 4;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070022 EXPECT_TRUE(equal(a, b));
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070023 b = 5;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070024 EXPECT_FALSE(equal(a, b));
William A. Kennington III3b35fcf2020-06-13 18:42:39 -070025}
26
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070027TEST(CopyFrom, Empty)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070028{
William A. Kennington III5c99ff42020-07-19 02:29:26 -070029 const std::string_view cs;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070030 EXPECT_THROW(copyFrom<int>(cs), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070031 std::string_view s;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070032 EXPECT_THROW(copyFrom<int>(s), std::runtime_error);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070033}
34
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070035TEST(CopyFrom, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070036{
37 int a = 4;
38 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070039 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070040}
41
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070042TEST(CopyFrom, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070043{
44 const std::vector<char> s = {'a', 'b', 'c'};
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070045 EXPECT_EQ('a', copyFrom<char>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070046 const char s2[] = "def";
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070047 EXPECT_EQ('d', copyFrom<char>(s2));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070048}
49
William A. Kennington III5c99ff42020-07-19 02:29:26 -070050struct Int
51{
52 uint8_t data[sizeof(int)];
53
54 inline bool operator==(const Int& other) const
55 {
56 return memcmp(data, other.data, sizeof(data)) == 0;
57 }
58};
59
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070060TEST(RefFrom, Empty)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070061{
62 const std::string_view cs;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070063 EXPECT_THROW(refFrom<Int>(cs), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070064 std::string_view s;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070065 EXPECT_THROW(refFrom<Int>(s), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070066}
67
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070068TEST(RefFrom, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070069{
70 Int a = {4, 0, 0, 4};
71 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070072 EXPECT_EQ(a, refFrom<Int>(s));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070073}
74
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070075TEST(RefFrom, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070076{
77 const std::vector<char> s = {'a', 'b', 'c'};
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070078 EXPECT_EQ('a', refFrom<char>(s));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070079 const char s2[] = "def";
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070080 EXPECT_EQ('d', refFrom<char>(s2));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070081}
82
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070083TEST(Extract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070084{
85 std::string_view s("a");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070086 EXPECT_THROW(extract<int>(s), std::runtime_error);
87 EXPECT_EQ("a", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070088}
89
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070090TEST(Extract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070091{
92 int a = 4;
93 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070094 EXPECT_EQ(a, extract<int>(s));
95 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -070096}
97
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070098TEST(Extract, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070099{
100 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700101 EXPECT_EQ('a', extract<char>(s));
102 EXPECT_EQ("bc", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700103}
104
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700105TEST(ExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700106{
107 std::string_view s("a");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700108 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
109 EXPECT_EQ("a", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700110}
111
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700112TEST(ExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700113{
114 Int a = {4, 0, 0, 4};
115 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700116 EXPECT_EQ(a, extractRef<Int>(s));
117 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700118}
119
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700120TEST(ExtractRef, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700121{
122 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700123 EXPECT_EQ('a', extractRef<char>(s));
124 EXPECT_EQ("bc", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700125}
126
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700127TEST(AsView, Byte)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700128{
129 int32_t a = 4;
130 auto s = asView<uint8_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700131 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700132}
133
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700134TEST(AsView, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700135{
136 int32_t a = 4;
137 auto s = asView<char16_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700138 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700139}
140
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700141using testing::ElementsAre;
142using testing::ElementsAreArray;
143
144TEST(AsView, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700145{
146 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
147 auto s = asView<char16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700148 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700149}
150
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700151TEST(AsView, StringView)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700152{
153 std::string_view sv = "ab";
154 auto s = asView<uint8_t>(sv);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700155 EXPECT_THAT(s, ElementsAreArray(sv));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700156}
157
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700158TEST(SpanExtract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700159{
160 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500161 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700162 EXPECT_THROW(extract<int>(s), std::runtime_error);
163 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700164}
165
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700166TEST(SpanExtract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700167{
168 const std::vector<int> v = {4};
Patrick Williams68975b92022-04-27 16:00:26 -0500169 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700170 EXPECT_EQ(v[0], extract<int>(s));
171 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700172}
173
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700174TEST(SpanExtract, Larger)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700175{
176 const std::vector<int> v{3, 4, 5};
Patrick Williams68975b92022-04-27 16:00:26 -0500177 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700178 EXPECT_EQ(v[0], extract<int>(s));
179 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700180}
181
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700182TEST(SpanExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700183{
184 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500185 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700186 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
187 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700188}
189
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700190TEST(SpanExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700191{
192 const std::vector<Int> v = {{4, 0, 0, 4}};
Patrick Williams68975b92022-04-27 16:00:26 -0500193 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700194 EXPECT_EQ(v[0], extractRef<Int>(s));
195 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700196}
197
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700198TEST(SpanExtractRef, Larger)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700199{
200 const std::vector<Int> v{{3}, {4}, {5}};
Patrick Williams68975b92022-04-27 16:00:26 -0500201 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700202 EXPECT_EQ(v[0], extractRef<Int>(s));
203 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700204}
205
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700206TEST(AsSpan, ConstInt)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700207{
208 const uint64_t data = htole64(0xffff0000);
209 auto s = asSpan<uint32_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700210 EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700211}
212
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700213TEST(AsSpan, ConstArray)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700214{
215 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
216 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700217 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700218}
219
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700220TEST(AsSpan, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700221{
William A. Kennington IIIa9cf86f2021-08-03 11:42:11 -0700222 struct
223 {
224 uint64_t a;
225 uint64_t* data()
226 {
227 return &a;
228 }
229 } data = {htole64(0xffff0000)};
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700230 auto s = asSpan<uint16_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700231 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
232 htole16(0x0000), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700233 s[2] = 0xfefe;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700234 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
235 htole16(0xfefe), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700236}
237
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700238TEST(AsSpan, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700239{
240 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
241 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700242 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700243}
244
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700245TEST(AsSpan, Span)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700246{
247 std::array<char, 2> arr = {'a', 'b'};
Patrick Williams68975b92022-04-27 16:00:26 -0500248 auto sp1 = std::span<const char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700249 auto s1 = asSpan<uint8_t>(sp1);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700250 EXPECT_THAT(s1, ElementsAreArray(arr));
Patrick Williams68975b92022-04-27 16:00:26 -0500251 auto sp2 = std::span<char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700252 auto s2 = asSpan<uint8_t>(sp2);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700253 EXPECT_THAT(s2, ElementsAreArray(arr));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700254}
255
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700256} // namespace
257} // namespace raw
258} // namespace stdplus