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