William A. Kennington III | be79c1e | 2021-01-25 16:06:31 -0800 | [diff] [blame] | 1 | #include <array> |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 2 | #include <endian.h> |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 3 | #include <gmock/gmock.h> |
| 4 | #include <gtest/gtest.h> |
William A. Kennington III | 615b82a | 2022-08-24 13:47:17 -0700 | [diff] [blame] | 5 | #include <span> |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 6 | #include <stdexcept> |
| 7 | #include <stdplus/raw.hpp> |
| 8 | #include <string_view> |
| 9 | #include <vector> |
| 10 | |
| 11 | namespace stdplus |
| 12 | { |
| 13 | namespace raw |
| 14 | { |
| 15 | namespace |
| 16 | { |
| 17 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 18 | TEST(Equal, Equal) |
William A. Kennington III | 3b35fcf | 2020-06-13 18:42:39 -0700 | [diff] [blame] | 19 | { |
| 20 | int a = 4; |
| 21 | unsigned b = 4; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 22 | EXPECT_TRUE(equal(a, b)); |
William A. Kennington III | 3b35fcf | 2020-06-13 18:42:39 -0700 | [diff] [blame] | 23 | b = 5; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 24 | EXPECT_FALSE(equal(a, b)); |
William A. Kennington III | 3b35fcf | 2020-06-13 18:42:39 -0700 | [diff] [blame] | 25 | } |
| 26 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 27 | TEST(CopyFrom, Empty) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 28 | { |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 29 | const std::string_view cs; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 30 | EXPECT_THROW(copyFrom<int>(cs), std::runtime_error); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 31 | std::string_view s; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 32 | EXPECT_THROW(copyFrom<int>(s), std::runtime_error); |
William A. Kennington III | fca0fdf | 2022-10-11 12:21:29 -0700 | [diff] [blame^] | 33 | EXPECT_THROW(copyFromStrict<char>(s), std::runtime_error); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 36 | TEST(CopyFrom, Basic) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 37 | { |
| 38 | int a = 4; |
| 39 | const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 40 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | fca0fdf | 2022-10-11 12:21:29 -0700 | [diff] [blame^] | 41 | EXPECT_EQ(a, copyFromStrict<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 42 | } |
| 43 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 44 | TEST(CopyFrom, Partial) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 45 | { |
| 46 | const std::vector<char> s = {'a', 'b', 'c'}; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 47 | EXPECT_EQ('a', copyFrom<char>(s)); |
William A. Kennington III | fca0fdf | 2022-10-11 12:21:29 -0700 | [diff] [blame^] | 48 | EXPECT_THROW(copyFromStrict<char>(s), std::runtime_error); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 49 | const char s2[] = "def"; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 50 | EXPECT_EQ('d', copyFrom<char>(s2)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 53 | struct 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 III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 63 | TEST(RefFrom, Empty) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 64 | { |
| 65 | const std::string_view cs; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 66 | EXPECT_THROW(refFrom<Int>(cs), std::runtime_error); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 67 | std::string_view s; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 68 | EXPECT_THROW(refFrom<Int>(s), std::runtime_error); |
William A. Kennington III | fca0fdf | 2022-10-11 12:21:29 -0700 | [diff] [blame^] | 69 | EXPECT_THROW(refFromStrict<Int>(s), std::runtime_error); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 72 | TEST(RefFrom, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 73 | { |
| 74 | Int a = {4, 0, 0, 4}; |
| 75 | const std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 76 | EXPECT_EQ(a, refFrom<Int>(s)); |
William A. Kennington III | fca0fdf | 2022-10-11 12:21:29 -0700 | [diff] [blame^] | 77 | EXPECT_EQ(a, refFromStrict<Int>(s)); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 78 | } |
| 79 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 80 | TEST(RefFrom, Partial) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 81 | { |
| 82 | const std::vector<char> s = {'a', 'b', 'c'}; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 83 | EXPECT_EQ('a', refFrom<char>(s)); |
William A. Kennington III | fca0fdf | 2022-10-11 12:21:29 -0700 | [diff] [blame^] | 84 | EXPECT_THROW(refFromStrict<char>(s), std::runtime_error); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 85 | const char s2[] = "def"; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 86 | EXPECT_EQ('d', refFrom<char>(s2)); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 89 | TEST(Extract, TooSmall) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 90 | { |
| 91 | std::string_view s("a"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 92 | EXPECT_THROW(extract<int>(s), std::runtime_error); |
| 93 | EXPECT_EQ("a", s); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 94 | } |
| 95 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 96 | TEST(Extract, Basic) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 97 | { |
| 98 | int a = 4; |
| 99 | std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 100 | EXPECT_EQ(a, extract<int>(s)); |
| 101 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 104 | TEST(Extract, Partial) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 105 | { |
| 106 | std::string_view s("abc"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 107 | EXPECT_EQ('a', extract<char>(s)); |
| 108 | EXPECT_EQ("bc", s); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 109 | } |
| 110 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 111 | TEST(ExtractRef, TooSmall) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 112 | { |
| 113 | std::string_view s("a"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 114 | EXPECT_THROW(extractRef<Int>(s), std::runtime_error); |
| 115 | EXPECT_EQ("a", s); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 118 | TEST(ExtractRef, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 119 | { |
| 120 | Int a = {4, 0, 0, 4}; |
| 121 | std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 122 | EXPECT_EQ(a, extractRef<Int>(s)); |
| 123 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 124 | } |
| 125 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 126 | TEST(ExtractRef, Partial) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 127 | { |
| 128 | std::string_view s("abc"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 129 | EXPECT_EQ('a', extractRef<char>(s)); |
| 130 | EXPECT_EQ("bc", s); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 131 | } |
| 132 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 133 | TEST(AsView, Byte) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 134 | { |
| 135 | int32_t a = 4; |
| 136 | auto s = asView<uint8_t>(a); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 137 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 138 | } |
| 139 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 140 | TEST(AsView, Int) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 141 | { |
| 142 | int32_t a = 4; |
| 143 | auto s = asView<char16_t>(a); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 144 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 145 | } |
| 146 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 147 | using testing::ElementsAre; |
| 148 | using testing::ElementsAreArray; |
| 149 | |
| 150 | TEST(AsView, Array) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 151 | { |
| 152 | std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 153 | auto s = asView<char16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 154 | EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 155 | } |
| 156 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 157 | TEST(AsView, StringView) |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 158 | { |
| 159 | std::string_view sv = "ab"; |
| 160 | auto s = asView<uint8_t>(sv); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 161 | EXPECT_THAT(s, ElementsAreArray(sv)); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 162 | } |
| 163 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 164 | TEST(SpanExtract, TooSmall) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 165 | { |
| 166 | const std::vector<char> v = {'c'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 167 | std::span<const char> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 168 | EXPECT_THROW(extract<int>(s), std::runtime_error); |
| 169 | EXPECT_THAT(s, ElementsAreArray(v)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 170 | } |
| 171 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 172 | TEST(SpanExtract, Basic) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 173 | { |
| 174 | const std::vector<int> v = {4}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 175 | std::span<const int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 176 | EXPECT_EQ(v[0], extract<int>(s)); |
| 177 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 178 | } |
| 179 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 180 | TEST(SpanExtract, Larger) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 181 | { |
| 182 | const std::vector<int> v{3, 4, 5}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 183 | std::span<const int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 184 | EXPECT_EQ(v[0], extract<int>(s)); |
| 185 | EXPECT_THAT(s, ElementsAre(v[1], v[2])); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 186 | } |
| 187 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 188 | TEST(SpanExtractRef, TooSmall) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 189 | { |
| 190 | const std::vector<char> v = {'c'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 191 | std::span<const char> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 192 | EXPECT_THROW(extractRef<Int>(s), std::runtime_error); |
| 193 | EXPECT_THAT(s, ElementsAreArray(v)); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 194 | } |
| 195 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 196 | TEST(SpanExtractRef, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 197 | { |
| 198 | const std::vector<Int> v = {{4, 0, 0, 4}}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 199 | std::span<const Int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 200 | EXPECT_EQ(v[0], extractRef<Int>(s)); |
| 201 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 202 | } |
| 203 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 204 | TEST(SpanExtractRef, Larger) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 205 | { |
| 206 | const std::vector<Int> v{{3}, {4}, {5}}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 207 | std::span<const Int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 208 | EXPECT_EQ(v[0], extractRef<Int>(s)); |
| 209 | EXPECT_THAT(s, ElementsAre(v[1], v[2])); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 212 | TEST(AsSpan, ConstInt) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 213 | { |
| 214 | const uint64_t data = htole64(0xffff0000); |
| 215 | auto s = asSpan<uint32_t>(data); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 216 | EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 217 | } |
| 218 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 219 | TEST(AsSpan, ConstArray) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 220 | { |
| 221 | const std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 222 | auto s = asSpan<uint16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 223 | EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 224 | } |
| 225 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 226 | TEST(AsSpan, Int) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 227 | { |
William A. Kennington III | a9cf86f | 2021-08-03 11:42:11 -0700 | [diff] [blame] | 228 | struct |
| 229 | { |
| 230 | uint64_t a; |
| 231 | uint64_t* data() |
| 232 | { |
| 233 | return &a; |
| 234 | } |
| 235 | } data = {htole64(0xffff0000)}; |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 236 | auto s = asSpan<uint16_t>(data); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 237 | EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff), |
| 238 | htole16(0x0000), htole16(0x0000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 239 | s[2] = 0xfefe; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 240 | EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff), |
| 241 | htole16(0xfefe), htole16(0x0000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 242 | } |
| 243 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 244 | TEST(AsSpan, Array) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 245 | { |
| 246 | std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 247 | auto s = asSpan<uint16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 248 | EXPECT_THAT(s, ElementsAre(htole16(1), htole16(0), htole16(2), htole16(0))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 249 | } |
| 250 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 251 | TEST(AsSpan, Span) |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 252 | { |
| 253 | std::array<char, 2> arr = {'a', 'b'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 254 | auto sp1 = std::span<const char>(arr); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 255 | auto s1 = asSpan<uint8_t>(sp1); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 256 | EXPECT_THAT(s1, ElementsAreArray(arr)); |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 257 | auto sp2 = std::span<char>(arr); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 258 | auto s2 = asSpan<uint8_t>(sp2); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 259 | EXPECT_THAT(s2, ElementsAreArray(arr)); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 260 | } |
| 261 | |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 262 | } // namespace |
| 263 | } // namespace raw |
| 264 | } // namespace stdplus |