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 | { |
William A. Kennington III | 8e05631 | 2022-11-18 19:42:45 -0800 | [diff] [blame^] | 91 | std::string str = "aaaa"; |
| 92 | str.resize(1); |
| 93 | std::string_view s(str); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 94 | EXPECT_THROW(extract<int>(s), std::runtime_error); |
| 95 | EXPECT_EQ("a", s); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 96 | } |
| 97 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 98 | TEST(Extract, Basic) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 99 | { |
| 100 | int a = 4; |
| 101 | std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 102 | EXPECT_EQ(a, extract<int>(s)); |
| 103 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 106 | TEST(Extract, Partial) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 107 | { |
| 108 | std::string_view s("abc"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 109 | EXPECT_EQ('a', extract<char>(s)); |
| 110 | EXPECT_EQ("bc", s); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 113 | TEST(ExtractRef, TooSmall) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 114 | { |
| 115 | std::string_view s("a"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 116 | EXPECT_THROW(extractRef<Int>(s), std::runtime_error); |
| 117 | EXPECT_EQ("a", s); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 118 | } |
| 119 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 120 | TEST(ExtractRef, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 121 | { |
| 122 | Int a = {4, 0, 0, 4}; |
| 123 | std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 124 | EXPECT_EQ(a, extractRef<Int>(s)); |
| 125 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 126 | } |
| 127 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 128 | TEST(ExtractRef, Partial) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 129 | { |
| 130 | std::string_view s("abc"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 131 | EXPECT_EQ('a', extractRef<char>(s)); |
| 132 | EXPECT_EQ("bc", s); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 135 | TEST(AsView, Byte) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 136 | { |
| 137 | int32_t a = 4; |
| 138 | auto s = asView<uint8_t>(a); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 139 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 140 | } |
| 141 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 142 | TEST(AsView, Int) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 143 | { |
| 144 | int32_t a = 4; |
| 145 | auto s = asView<char16_t>(a); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 146 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 147 | } |
| 148 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 149 | using testing::ElementsAre; |
| 150 | using testing::ElementsAreArray; |
| 151 | |
| 152 | TEST(AsView, Array) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 153 | { |
| 154 | std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 155 | auto s = asView<char16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 156 | 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] | 157 | } |
| 158 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 159 | TEST(AsView, StringView) |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 160 | { |
| 161 | std::string_view sv = "ab"; |
| 162 | auto s = asView<uint8_t>(sv); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 163 | EXPECT_THAT(s, ElementsAreArray(sv)); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 164 | } |
| 165 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 166 | TEST(SpanExtract, TooSmall) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 167 | { |
| 168 | const std::vector<char> v = {'c'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 169 | std::span<const char> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 170 | EXPECT_THROW(extract<int>(s), std::runtime_error); |
| 171 | EXPECT_THAT(s, ElementsAreArray(v)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 172 | } |
| 173 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 174 | TEST(SpanExtract, Basic) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 175 | { |
| 176 | const std::vector<int> v = {4}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 177 | std::span<const int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 178 | EXPECT_EQ(v[0], extract<int>(s)); |
| 179 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 180 | } |
| 181 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 182 | TEST(SpanExtract, Larger) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 183 | { |
| 184 | const std::vector<int> v{3, 4, 5}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 185 | std::span<const int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 186 | EXPECT_EQ(v[0], extract<int>(s)); |
| 187 | EXPECT_THAT(s, ElementsAre(v[1], v[2])); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 188 | } |
| 189 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 190 | TEST(SpanExtractRef, TooSmall) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 191 | { |
| 192 | const std::vector<char> v = {'c'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 193 | std::span<const char> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 194 | EXPECT_THROW(extractRef<Int>(s), std::runtime_error); |
| 195 | EXPECT_THAT(s, ElementsAreArray(v)); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 196 | } |
| 197 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 198 | TEST(SpanExtractRef, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 199 | { |
| 200 | const std::vector<Int> v = {{4, 0, 0, 4}}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 201 | std::span<const Int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 202 | EXPECT_EQ(v[0], extractRef<Int>(s)); |
| 203 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 204 | } |
| 205 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 206 | TEST(SpanExtractRef, Larger) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 207 | { |
| 208 | const std::vector<Int> v{{3}, {4}, {5}}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 209 | std::span<const Int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 210 | EXPECT_EQ(v[0], extractRef<Int>(s)); |
| 211 | EXPECT_THAT(s, ElementsAre(v[1], v[2])); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 212 | } |
| 213 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 214 | TEST(AsSpan, ConstInt) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 215 | { |
| 216 | const uint64_t data = htole64(0xffff0000); |
| 217 | auto s = asSpan<uint32_t>(data); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 218 | EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 219 | } |
| 220 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 221 | TEST(AsSpan, ConstArray) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 222 | { |
| 223 | const std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 224 | auto s = asSpan<uint16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 225 | 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] | 226 | } |
| 227 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 228 | TEST(AsSpan, Int) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 229 | { |
William A. Kennington III | a9cf86f | 2021-08-03 11:42:11 -0700 | [diff] [blame] | 230 | struct |
| 231 | { |
| 232 | uint64_t a; |
| 233 | uint64_t* data() |
| 234 | { |
| 235 | return &a; |
| 236 | } |
| 237 | } data = {htole64(0xffff0000)}; |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 238 | auto s = asSpan<uint16_t>(data); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 239 | EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff), |
| 240 | htole16(0x0000), htole16(0x0000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 241 | s[2] = 0xfefe; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 242 | EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff), |
| 243 | htole16(0xfefe), htole16(0x0000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 244 | } |
| 245 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 246 | TEST(AsSpan, Array) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 247 | { |
| 248 | std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 249 | auto s = asSpan<uint16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 250 | 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] | 251 | } |
| 252 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 253 | TEST(AsSpan, Span) |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 254 | { |
| 255 | std::array<char, 2> arr = {'a', 'b'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 256 | auto sp1 = std::span<const char>(arr); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 257 | auto s1 = asSpan<uint8_t>(sp1); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 258 | EXPECT_THAT(s1, ElementsAreArray(arr)); |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 259 | auto sp2 = std::span<char>(arr); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 260 | auto s2 = asSpan<uint8_t>(sp2); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 261 | EXPECT_THAT(s2, ElementsAreArray(arr)); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 262 | } |
| 263 | |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 264 | } // namespace |
| 265 | } // namespace raw |
| 266 | } // namespace stdplus |