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