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 | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 33 | EXPECT_THROW(copyFrom<int>(cs), exception::Incomplete); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 34 | std::string_view s; |
William A. Kennington III | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 35 | EXPECT_THROW(copyFrom<int>(s), exception::Incomplete); |
| 36 | EXPECT_THROW(copyFromStrict<char>(s), exception::Incomplete); |
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 | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 51 | EXPECT_THROW(copyFromStrict<char>(s), exception::Incomplete); |
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 | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 69 | EXPECT_THROW(refFrom<Int>(cs), exception::Incomplete); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 70 | std::string_view s; |
William A. Kennington III | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 71 | EXPECT_THROW(refFrom<Int>(s), exception::Incomplete); |
| 72 | EXPECT_THROW(refFromStrict<Int>(s), exception::Incomplete); |
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 | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 87 | EXPECT_THROW(refFromStrict<char>(s), exception::Incomplete); |
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 | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 97 | EXPECT_THROW(extract<int>(s), exception::Incomplete); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 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 | { |
William A. Kennington III | 1df9276 | 2023-01-12 10:34:13 -0800 | [diff] [blame] | 118 | // Trick the compiler with a bigger buffer so it doesn't complain |
| 119 | std::array<char, sizeof(Int)> buf{}; |
| 120 | buf[0] = 'a'; |
| 121 | std::string_view s(buf.data(), 1); |
William A. Kennington III | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 122 | EXPECT_THROW(extractRef<Int>(s), exception::Incomplete); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 123 | EXPECT_EQ("a", s); |
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, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 127 | { |
| 128 | Int a = {4, 0, 0, 4}; |
| 129 | std::string_view s(reinterpret_cast<char*>(&a), sizeof(a)); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 130 | EXPECT_EQ(a, extractRef<Int>(s)); |
| 131 | EXPECT_TRUE(s.empty()); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 134 | TEST(ExtractRef, Partial) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 135 | { |
| 136 | std::string_view s("abc"); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 137 | EXPECT_EQ('a', extractRef<char>(s)); |
| 138 | EXPECT_EQ("bc", s); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 141 | TEST(AsView, Byte) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 142 | { |
| 143 | int32_t a = 4; |
| 144 | auto s = asView<uint8_t>(a); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 145 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 146 | } |
| 147 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 148 | TEST(AsView, Int) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 149 | { |
| 150 | int32_t a = 4; |
| 151 | auto s = asView<char16_t>(a); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 152 | EXPECT_EQ(a, copyFrom<int>(s)); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 155 | using testing::ElementsAre; |
| 156 | using testing::ElementsAreArray; |
| 157 | |
| 158 | TEST(AsView, Array) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 159 | { |
| 160 | std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 161 | auto s = asView<char16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 162 | 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] | 163 | } |
| 164 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 165 | TEST(AsView, StringView) |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 166 | { |
| 167 | std::string_view sv = "ab"; |
| 168 | auto s = asView<uint8_t>(sv); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 169 | EXPECT_THAT(s, ElementsAreArray(sv)); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 170 | } |
| 171 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 172 | TEST(SpanExtract, TooSmall) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 173 | { |
| 174 | const std::vector<char> v = {'c'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 175 | std::span<const char> s = v; |
William A. Kennington III | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 176 | EXPECT_THROW(extract<int>(s), exception::Incomplete); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 177 | EXPECT_THAT(s, ElementsAreArray(v)); |
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, Basic) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 181 | { |
| 182 | const std::vector<int> v = {4}; |
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_TRUE(s.empty()); |
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(SpanExtract, Larger) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 189 | { |
| 190 | const std::vector<int> v{3, 4, 5}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 191 | std::span<const int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 192 | EXPECT_EQ(v[0], extract<int>(s)); |
| 193 | EXPECT_THAT(s, ElementsAre(v[1], v[2])); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 194 | } |
| 195 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 196 | TEST(SpanExtractRef, TooSmall) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 197 | { |
| 198 | const std::vector<char> v = {'c'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 199 | std::span<const char> s = v; |
William A. Kennington III | 2f6affd | 2024-01-28 17:49:41 -0800 | [diff] [blame^] | 200 | EXPECT_THROW(extractRef<Int>(s), exception::Incomplete); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 201 | EXPECT_THAT(s, ElementsAreArray(v)); |
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, Basic) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 205 | { |
| 206 | const std::vector<Int> v = {{4, 0, 0, 4}}; |
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_TRUE(s.empty()); |
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(SpanExtractRef, Larger) |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 213 | { |
| 214 | const std::vector<Int> v{{3}, {4}, {5}}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 215 | std::span<const Int> s = v; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 216 | EXPECT_EQ(v[0], extractRef<Int>(s)); |
| 217 | EXPECT_THAT(s, ElementsAre(v[1], v[2])); |
William A. Kennington III | 5c99ff4 | 2020-07-19 02:29:26 -0700 | [diff] [blame] | 218 | } |
| 219 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 220 | TEST(AsSpan, ConstInt) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 221 | { |
| 222 | const uint64_t data = htole64(0xffff0000); |
| 223 | auto s = asSpan<uint32_t>(data); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 224 | EXPECT_THAT(s, ElementsAre(htole32(0xffff0000), htole32(0x00000000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 225 | } |
| 226 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 227 | TEST(AsSpan, ConstArray) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 228 | { |
| 229 | const std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 230 | auto s = asSpan<uint16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 231 | 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] | 232 | } |
| 233 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 234 | TEST(AsSpan, Int) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 235 | { |
William A. Kennington III | a9cf86f | 2021-08-03 11:42:11 -0700 | [diff] [blame] | 236 | struct |
| 237 | { |
| 238 | uint64_t a; |
| 239 | uint64_t* data() |
| 240 | { |
| 241 | return &a; |
| 242 | } |
| 243 | } data = {htole64(0xffff0000)}; |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 244 | auto s = asSpan<uint16_t>(data); |
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(0x0000), htole16(0x0000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 247 | s[2] = 0xfefe; |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 248 | EXPECT_THAT(s, ElementsAre(htole16(0x0000), htole16(0xffff), |
| 249 | htole16(0xfefe), htole16(0x0000))); |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 250 | } |
| 251 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 252 | TEST(AsSpan, Array) |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 253 | { |
| 254 | std::vector<uint32_t> arr = {htole32(1), htole32(2)}; |
| 255 | auto s = asSpan<uint16_t>(arr); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 256 | 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] | 257 | } |
| 258 | |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 259 | TEST(AsSpan, Span) |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 260 | { |
| 261 | std::array<char, 2> arr = {'a', 'b'}; |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 262 | auto sp1 = std::span<const char>(arr); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 263 | auto s1 = asSpan<uint8_t>(sp1); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 264 | EXPECT_THAT(s1, ElementsAreArray(arr)); |
Patrick Williams | 68975b9 | 2022-04-27 16:00:26 -0500 | [diff] [blame] | 265 | auto sp2 = std::span<char>(arr); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 266 | auto s2 = asSpan<uint8_t>(sp2); |
William A. Kennington III | 6c15d9a | 2022-08-24 13:45:42 -0700 | [diff] [blame] | 267 | EXPECT_THAT(s2, ElementsAreArray(arr)); |
William A. Kennington III | ba7d754 | 2020-07-19 20:04:44 -0700 | [diff] [blame] | 268 | } |
| 269 | |
William A. Kennington III | e099038 | 2019-10-18 02:10:25 -0700 | [diff] [blame] | 270 | } // namespace |
| 271 | } // namespace raw |
| 272 | } // namespace stdplus |