blob: cb013b63255957474f3b50d6d4984ff4b8b345b0 [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{
91 std::string_view s("a");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070092 EXPECT_THROW(extract<int>(s), std::runtime_error);
93 EXPECT_EQ("a", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -070094}
95
William A. Kennington III6c15d9a2022-08-24 13:45:42 -070096TEST(Extract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -070097{
98 int a = 4;
99 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700100 EXPECT_EQ(a, extract<int>(s));
101 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700102}
103
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700104TEST(Extract, Partial)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700105{
106 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700107 EXPECT_EQ('a', extract<char>(s));
108 EXPECT_EQ("bc", s);
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700109}
110
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700111TEST(ExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700112{
113 std::string_view s("a");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700114 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
115 EXPECT_EQ("a", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700116}
117
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700118TEST(ExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700119{
120 Int a = {4, 0, 0, 4};
121 std::string_view s(reinterpret_cast<char*>(&a), sizeof(a));
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700122 EXPECT_EQ(a, extractRef<Int>(s));
123 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700124}
125
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700126TEST(ExtractRef, Partial)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700127{
128 std::string_view s("abc");
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700129 EXPECT_EQ('a', extractRef<char>(s));
130 EXPECT_EQ("bc", s);
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700131}
132
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700133TEST(AsView, Byte)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700134{
135 int32_t a = 4;
136 auto s = asView<uint8_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700137 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700138}
139
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700140TEST(AsView, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700141{
142 int32_t a = 4;
143 auto s = asView<char16_t>(a);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700144 EXPECT_EQ(a, copyFrom<int>(s));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700145}
146
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700147using testing::ElementsAre;
148using testing::ElementsAreArray;
149
150TEST(AsView, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700151{
152 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
153 auto s = asView<char16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700154 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700155}
156
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700157TEST(AsView, StringView)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700158{
159 std::string_view sv = "ab";
160 auto s = asView<uint8_t>(sv);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700161 EXPECT_THAT(s, ElementsAreArray(sv));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700162}
163
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700164TEST(SpanExtract, TooSmall)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700165{
166 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500167 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700168 EXPECT_THROW(extract<int>(s), std::runtime_error);
169 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700170}
171
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700172TEST(SpanExtract, Basic)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700173{
174 const std::vector<int> v = {4};
Patrick Williams68975b92022-04-27 16:00:26 -0500175 std::span<const int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700176 EXPECT_EQ(v[0], extract<int>(s));
177 EXPECT_TRUE(s.empty());
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700178}
179
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700180TEST(SpanExtract, Larger)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700181{
182 const std::vector<int> v{3, 4, 5};
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_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700186}
187
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700188TEST(SpanExtractRef, TooSmall)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700189{
190 const std::vector<char> v = {'c'};
Patrick Williams68975b92022-04-27 16:00:26 -0500191 std::span<const char> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700192 EXPECT_THROW(extractRef<Int>(s), std::runtime_error);
193 EXPECT_THAT(s, ElementsAreArray(v));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700194}
195
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700196TEST(SpanExtractRef, Basic)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700197{
198 const std::vector<Int> v = {{4, 0, 0, 4}};
Patrick Williams68975b92022-04-27 16:00:26 -0500199 std::span<const Int> s = v;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700200 EXPECT_EQ(v[0], extractRef<Int>(s));
201 EXPECT_TRUE(s.empty());
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700202}
203
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700204TEST(SpanExtractRef, Larger)
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700205{
206 const std::vector<Int> v{{3}, {4}, {5}};
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_THAT(s, ElementsAre(v[1], v[2]));
William A. Kennington III5c99ff42020-07-19 02:29:26 -0700210}
211
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700212TEST(AsSpan, ConstInt)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700213{
214 const uint64_t data = htole64(0xffff0000);
215 auto s = asSpan<uint32_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700216 EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700217}
218
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700219TEST(AsSpan, ConstArray)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700220{
221 const std::vector<uint32_t> arr = {htole32(1), htole32(2)};
222 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700223 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700224}
225
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700226TEST(AsSpan, Int)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700227{
William A. Kennington IIIa9cf86f2021-08-03 11:42:11 -0700228 struct
229 {
230 uint64_t a;
231 uint64_t* data()
232 {
233 return &a;
234 }
235 } data = {htole64(0xffff0000)};
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700236 auto s = asSpan<uint16_t>(data);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700237 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
238 htole16(0x0000), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700239 s[2] = 0xfefe;
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700240 EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff),
241 htole16(0xfefe), htole16(0x0000)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700242}
243
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700244TEST(AsSpan, Array)
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700245{
246 std::vector<uint32_t> arr = {htole32(1), htole32(2)};
247 auto s = asSpan<uint16_t>(arr);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700248 EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0)));
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700249}
250
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700251TEST(AsSpan, Span)
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700252{
253 std::array<char, 2> arr = {'a', 'b'};
Patrick Williams68975b92022-04-27 16:00:26 -0500254 auto sp1 = std::span<const char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700255 auto s1 = asSpan<uint8_t>(sp1);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700256 EXPECT_THAT(s1, ElementsAreArray(arr));
Patrick Williams68975b92022-04-27 16:00:26 -0500257 auto sp2 = std::span<char>(arr);
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700258 auto s2 = asSpan<uint8_t>(sp2);
William A. Kennington III6c15d9a2022-08-24 13:45:42 -0700259 EXPECT_THAT(s2, ElementsAreArray(arr));
William A. Kennington IIIba7d7542020-07-19 20:04:44 -0700260}
261
William A. Kennington IIIe0990382019-10-18 02:10:25 -0700262} // namespace
263} // namespace raw
264} // namespace stdplus