blob: 0de4c338786a52e2f87a0ad3fa11186714af5c44 [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 IIIfca0fdf2022-10-11 12:21:29 -070033 EXPECT_THROW(copyFromStrict<char>(s), std::runtime_error);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070034}
35
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070036TEST(CopyFrom, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070037{
38 int a = 4;
39 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070040 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070041 EXPECT_EQ(a, copyFromStrict<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070042}
43
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070044TEST(CopyFrom, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070045{
46 const std::vector<char> s = {'a', 'b', 'c'};
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070047 EXPECT_EQ('a', copyFrom<char>(s));
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070048 EXPECT_THROW(copyFromStrict<char>(s), std::runtime_error);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070049 const char s2[] = "def";
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070050 EXPECT_EQ('d', copyFrom<char>(s2));
William A. Kennington IIIe0990382019-10-18 02:10:25 -070051}
52
William A. Kennington III5c99ff42020-07-19 02:29:26 -070053struct Int
54{
55 uint8_t data[sizeof(int)];
56
57 inline bool operator==(const Int& other) const
58 {
59 return memcmp(data, other.data, sizeof(data)) == 0;
60 }
61};
62
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070063TEST(RefFrom, Empty)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070064{
65 const std::string_view cs;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070066 EXPECT_THROW(refFrom<Int>(cs), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070067 std::string_view s;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070068 EXPECT_THROW(refFrom<Int>(s), std::runtime_error);
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070069 EXPECT_THROW(refFromStrict<Int>(s), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070070}
71
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070072TEST(RefFrom, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070073{
74 Int a = {4, 0, 0, 4};
75 const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070076 EXPECT_EQ(a, refFrom<Int>(s));
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070077 EXPECT_EQ(a, refFromStrict<Int>(s));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070078}
79
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070080TEST(RefFrom, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -070081{
82 const std::vector<char> s = {'a', 'b', 'c'};
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070083 EXPECT_EQ('a', refFrom<char>(s));
William A. Kennington IIIfca0fdf2022-10-11 12:21:29 -070084 EXPECT_THROW(refFromStrict<char>(s), std::runtime_error);
William A. Kennington III5c99ff42020-07-19 02:29:26 -070085 const char s2[] = "def";
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070086 EXPECT_EQ('d', refFrom<char>(s2));
William A. Kennington III5c99ff42020-07-19 02:29:26 -070087}
88
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070089TEST(Extract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070090{
William A. Kennington III8e056312022-11-18 19:42:45 -080091 std::string str = "aaaa";
92 str.resize(1);
93 std::string_view s(str);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070094 EXPECT_THROW(extract<int>(s), std::runtime_error);
95 EXPECT_EQ("a", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070096}
97
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070098TEST(Extract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070099{
100 int a = 4;
101 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700102 EXPECT_EQ(a, extract<int>(s));
103 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700104}
105
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700106TEST(Extract, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700107{
108 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700109 EXPECT_EQ('a', extract<char>(s));
110 EXPECT_EQ("bc", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700111}
112
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700113TEST(ExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700114{
115 std::string_view s("a");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700116 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
117 EXPECT_EQ("a", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700118}
119
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700120TEST(ExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700121{
122 Int a = {4, 0, 0, 4};
123 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700124 EXPECT_EQ(a, extractRef<Int>(s));
125 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700126}
127
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700128TEST(ExtractRef, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700129{
130 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700131 EXPECT_EQ('a', extractRef<char>(s));
132 EXPECT_EQ("bc", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700133}
134
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700135TEST(AsView, Byte)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700136{
137 int32_t a = 4;
138 auto s = asView<uint8_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700139 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700140}
141
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700142TEST(AsView, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700143{
144 int32_t a = 4;
145 auto s = asView<char16_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700146 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700147}
148
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700149using testing::ElementsAre;
150using testing::ElementsAreArray;
151
152TEST(AsView, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700153{
154 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
155 auto s = asView<char16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700156 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700157}
158
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700159TEST(AsView, StringView)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700160{
161 std::string_view sv = "ab";
162 auto s = asView<uint8_t>(sv);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700163 EXPECT_THAT(s, ElementsAreArray(sv));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700164}
165
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700166TEST(SpanExtract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700167{
168 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500169 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700170 EXPECT_THROW(extract<int>(s), std::runtime_error);
171 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700172}
173
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700174TEST(SpanExtract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700175{
176 const std::vector<int> v = {4};
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_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700180}
181
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700182TEST(SpanExtract, Larger)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700183{
184 const std::vector<int> v{3, 4, 5};
Patrick Williams68975b92022-04-27 16:00:26 -0500185 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700186 EXPECT_EQ(v[0], extract<int>(s));
187 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700188}
189
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700190TEST(SpanExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700191{
192 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500193 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700194 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
195 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700196}
197
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700198TEST(SpanExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700199{
200 const std::vector<Int> v = {{4, 0, 0, 4}};
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_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700204}
205
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700206TEST(SpanExtractRef, Larger)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700207{
208 const std::vector<Int> v{{3}, {4}, {5}};
Patrick Williams68975b92022-04-27 16:00:26 -0500209 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700210 EXPECT_EQ(v[0], extractRef<Int>(s));
211 EXPECT_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700212}
213
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700214TEST(AsSpan, ConstInt)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700215{
216 const uint64_t data = htole64(0xffff0000);
217 auto s = asSpan<uint32_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700218 EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700219}
220
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700221TEST(AsSpan, ConstArray)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700222{
223 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
224 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700225 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700226}
227
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700228TEST(AsSpan, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700229{
William A. Kennington IIIa9cf86f2021-08-03 11:42:11 -0700230 struct
231 {
232 uint64_t a;
233 uint64_t* data()
234 {
235 return &a;
236 }
237 } data = {htole64(0xffff0000)};
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700238 auto s = asSpan<uint16_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700239 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
240 htole16(0x0000), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700241 s[2] = 0xfefe;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700242 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
243 htole16(0xfefe), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700244}
245
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700246TEST(AsSpan, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700247{
248 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
249 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700250 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700251}
252
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700253TEST(AsSpan, Span)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700254{
255 std::array<char, 2> arr = {'a', 'b'};
Patrick Williams68975b92022-04-27 16:00:26 -0500256 auto sp1 = std::span<const char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700257 auto s1 = asSpan<uint8_t>(sp1);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700258 EXPECT_THAT(s1, ElementsAreArray(arr));
Patrick Williams68975b92022-04-27 16:00:26 -0500259 auto sp2 = std::span<char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700260 auto s2 = asSpan<uint8_t>(sp2);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700261 EXPECT_THAT(s2, ElementsAreArray(arr));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700262}
263
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700264} // namespace
265} // namespace raw
266} // namespace stdplus