15#include "testing/gtest/include/gtest/gtest.h"
19TEST(NumericsTest, FromNativeEndian) {
24 static_assert(std::endian::native == std::endian::little);
26 constexpr uint8_t bytes[] = {0x12u};
32 constexpr uint8_t bytes[] = {0x12u, 0x34u};
38 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
44 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
45 0x90u, 0x12u, 0x34u, 0x56u};
52 constexpr uint8_t bytes[] = {0x12u};
58 constexpr uint8_t bytes[] = {0x12u, 0x34u};
64 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
70 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
71 0x90u, 0x12u, 0x34u, 0x56u};
78 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
89 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
90 0x90u, 0x12u, 0x34u, 0x56u};
92 1.84145159269283616391989849435e107);
94 0x56'34'12'90'78'56'34'12u);
98 1.84145159269283616391989849435e107);
100 0x56'34'12'90'78'56'34'12u);
104TEST(NumericsTest, FromLittleEndian) {
109 static_assert(std::endian::native == std::endian::little);
111 constexpr uint8_t bytes[] = {0x12u};
117 constexpr uint8_t bytes[] = {0x12u, 0x34u};
123 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
129 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
130 0x90u, 0x12u, 0x34u, 0x56u};
137 constexpr uint8_t bytes[] = {0x12u};
143 constexpr uint8_t bytes[] = {0x12u, 0x34u};
149 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
155 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
156 0x90u, 0x12u, 0x34u, 0x56u};
163 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
174 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
175 0x90u, 0x12u, 0x34u, 0x56u};
177 1.84145159269283616391989849435e107);
179 0x56'34'12'90'78'56'34'12u);
183 1.84145159269283616391989849435e107);
185 0x56'34'12'90'78'56'34'12u);
189TEST(NumericsTest, FromBigEndian) {
194 static_assert(std::endian::native == std::endian::little);
196 constexpr uint8_t bytes[] = {0x12u};
199 static_assert(std::same_as<uint8_t,
decltype(
U8FromBigEndian(bytes))>);
202 constexpr uint8_t bytes[] = {0x12u, 0x34u};
208 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
214 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
215 0x90u, 0x12u, 0x34u, 0x56u};
222 constexpr uint8_t bytes[] = {0x12u};
228 constexpr uint8_t bytes[] = {0x12u, 0x34u};
234 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
240 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
241 0x90u, 0x12u, 0x34u, 0x56u};
248 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u};
258 constexpr uint8_t bytes[] = {0x12u, 0x34u, 0x56u, 0x78u,
259 0x90u, 0x12u, 0x34u, 0x56u};
262 0x12'34'56'78'90'12'34'56u);
265 5.62634909901491201382066931077e-221);
267 0x12'34'56'78'90'12'34'56u);
271TEST(NumericsTest, ToNativeEndian) {
276 static_assert(std::endian::native == std::endian::little);
278 constexpr std::array<uint8_t, 1u> bytes = {0x12u};
279 constexpr auto val = uint8_t{0x12u};
286 constexpr std::array<uint8_t, 2u> bytes = {0x12u, 0x34u};
287 constexpr auto val = uint16_t{0x34'12u};
289 static_assert(std::same_as<std::array<uint8_t, 2u>,
294 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
295 constexpr auto val = uint32_t{0x78'56'34'12u};
297 static_assert(std::same_as<std::array<uint8_t, 4u>,
302 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
303 0x90u, 0x12u, 0x34u, 0x56u};
304 constexpr auto val = uint64_t{0x56'34'12'90'78'56'34'12u};
306 static_assert(std::same_as<std::array<uint8_t, 8u>,
312 constexpr std::array<uint8_t, 1u> bytes = {0x12u};
313 constexpr auto val = int8_t{0x12};
320 constexpr std::array<uint8_t, 2u> bytes = {0x12u, 0x34u};
321 constexpr auto val = int16_t{0x34'12};
323 static_assert(std::same_as<std::array<uint8_t, 2u>,
328 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
329 constexpr auto val = int32_t{0x78'56'34'12};
331 static_assert(std::same_as<std::array<uint8_t, 4u>,
336 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
337 0x90u, 0x12u, 0x34u, 0x56u};
338 constexpr auto val = int64_t{0x56'34'12'90'78'56'34'12};
340 static_assert(std::same_as<std::array<uint8_t, 8u>,
346 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
347 constexpr float val = 1.73782443614e+34f;
349 static_assert(std::same_as<std::array<uint8_t, 4u>,
355 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
356 0x90u, 0x12u, 0x34u, 0x56u};
357 constexpr double val = 1.84145159269283616391989849435e107;
359 static_assert(std::same_as<std::array<uint8_t, 8u>,
365TEST(NumericsTest, ToLittleEndian) {
370 static_assert(std::endian::native == std::endian::little);
372 constexpr std::array<uint8_t, 1u> bytes = {0x12u};
373 constexpr auto val = uint8_t{0x12u};
380 constexpr std::array<uint8_t, 2u> bytes = {0x12u, 0x34u};
381 constexpr auto val = uint16_t{0x34'12u};
383 static_assert(std::same_as<std::array<uint8_t, 2u>,
388 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
389 constexpr auto val = uint32_t{0x78'56'34'12u};
391 static_assert(std::same_as<std::array<uint8_t, 4u>,
396 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
397 0x90u, 0x12u, 0x34u, 0x56u};
398 constexpr auto val = uint64_t{0x56'34'12'90'78'56'34'12u};
400 static_assert(std::same_as<std::array<uint8_t, 8u>,
406 constexpr std::array<uint8_t, 1u> bytes = {0x12u};
407 constexpr auto val = int8_t{0x12};
414 constexpr std::array<uint8_t, 2u> bytes = {0x12u, 0x34u};
415 constexpr auto val = int16_t{0x34'12};
417 static_assert(std::same_as<std::array<uint8_t, 2u>,
422 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
423 constexpr auto val = int32_t{0x78'56'34'12};
425 static_assert(std::same_as<std::array<uint8_t, 4u>,
430 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
431 0x90u, 0x12u, 0x34u, 0x56u};
432 constexpr auto val = int64_t{0x56'34'12'90'78'56'34'12};
434 static_assert(std::same_as<std::array<uint8_t, 8u>,
440 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
441 constexpr float val = 1.73782443614e+34f;
443 static_assert(std::same_as<std::array<uint8_t, 4u>,
449 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
450 0x90u, 0x12u, 0x34u, 0x56u};
451 constexpr double val = 1.84145159269283616391989849435e107;
453 static_assert(std::same_as<std::array<uint8_t, 8u>,
459TEST(NumericsTest, ToBigEndian) {
464 static_assert(std::endian::native == std::endian::little);
466 constexpr std::array<uint8_t, 1u> bytes = {0x12u};
467 constexpr auto val = uint8_t{0x12u};
470 std::same_as<std::array<uint8_t, 1u>,
decltype(
U8ToBigEndian(val))>);
474 constexpr std::array<uint8_t, 2u> bytes = {0x12u, 0x34u};
475 constexpr auto val = uint16_t{0x12'34u};
478 std::same_as<std::array<uint8_t, 2u>,
decltype(
U16ToBigEndian(val))>);
482 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
483 constexpr auto val = uint32_t{0x12'34'56'78u};
486 std::same_as<std::array<uint8_t, 4u>,
decltype(
U32ToBigEndian(val))>);
490 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
491 0x90u, 0x12u, 0x34u, 0x56u};
492 constexpr auto val = uint64_t{0x12'34'56'78'90'12'34'56u};
495 std::same_as<std::array<uint8_t, 8u>,
decltype(
U64ToBigEndian(val))>);
500 constexpr std::array<uint8_t, 1u> bytes = {0x12u};
501 constexpr auto val = int8_t{0x12u};
504 std::same_as<std::array<uint8_t, 1u>,
decltype(
I8ToBigEndian(val))>);
508 constexpr std::array<uint8_t, 2u> bytes = {0x12u, 0x34u};
509 constexpr auto val = int16_t{0x12'34u};
512 std::same_as<std::array<uint8_t, 2u>,
decltype(
I16ToBigEndian(val))>);
516 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
517 constexpr auto val = int32_t{0x12'34'56'78u};
520 std::same_as<std::array<uint8_t, 4u>,
decltype(
I32ToBigEndian(val))>);
524 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
525 0x90u, 0x12u, 0x34u, 0x56u};
526 constexpr auto val = int64_t{0x12'34'56'78'90'12'34'56u};
529 std::same_as<std::array<uint8_t, 8u>,
decltype(
I64ToBigEndian(val))>);
534 constexpr std::array<uint8_t, 4u> bytes = {0x12u, 0x34u, 0x56u, 0x78u};
535 constexpr float val = 5.6904566139e-28f;
543 constexpr std::array<uint8_t, 8u> bytes = {0x12u, 0x34u, 0x56u, 0x78u,
544 0x90u, 0x12u, 0x34u, 0x56u};
545 constexpr double val = 5.62634909901491201382066931077e-221;
547 static_assert(std::same_as<std::array<uint8_t, 8u>,
TEST(NumericsTest, FromNativeEndian)
constexpr uint8_t U8FromLittleEndian(std::span< const uint8_t, 1u > bytes)
constexpr float FloatFromLittleEndian(std::span< const uint8_t, 4u > bytes)
constexpr uint8_t U8FromNativeEndian(std::span< const uint8_t, 1u > bytes)
constexpr uint32_t U32FromBigEndian(std::span< const uint8_t, 4u > bytes)
constexpr double DoubleFromNativeEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 4u > FloatToLittleEndian(float val)
constexpr uint16_t U16FromLittleEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 4u > FloatToBigEndian(float val)
constexpr int32_t I32FromNativeEndian(std::span< const uint8_t, 4u > bytes)
constexpr std::array< uint8_t, 4u > U32ToBigEndian(uint32_t val)
constexpr uint64_t U64FromNativeEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 8u > DoubleToBigEndian(double val)
constexpr std::array< uint8_t, 2u > U16ToNativeEndian(uint16_t val)
constexpr std::array< uint8_t, 4u > U32ToNativeEndian(uint32_t val)
constexpr int32_t I32FromBigEndian(std::span< const uint8_t, 4u > bytes)
constexpr uint64_t U64FromLittleEndian(std::span< const uint8_t, 8u > bytes)
constexpr int64_t I64FromBigEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 8u > U64ToNativeEndian(uint64_t val)
constexpr std::array< uint8_t, 1u > I8ToBigEndian(int8_t val)
constexpr uint32_t U32FromLittleEndian(std::span< const uint8_t, 4u > bytes)
constexpr int16_t I16FromLittleEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 1u > U8ToNativeEndian(uint8_t val)
constexpr std::array< uint8_t, 2u > I16ToBigEndian(int16_t val)
constexpr std::array< uint8_t, 1u > I8ToNativeEndian(int8_t val)
constexpr int64_t I64FromLittleEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 1u > I8ToLittleEndian(int8_t val)
constexpr std::array< uint8_t, 8u > U64ToBigEndian(uint64_t val)
constexpr std::array< uint8_t, 4u > U32ToLittleEndian(uint32_t val)
constexpr std::array< uint8_t, 1u > U8ToBigEndian(uint8_t val)
constexpr int16_t I16FromBigEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 4u > I32ToBigEndian(int32_t val)
constexpr std::array< uint8_t, 4u > FloatToNativeEndian(float val)
constexpr std::array< uint8_t, 2u > I16ToNativeEndian(int16_t val)
constexpr std::array< uint8_t, 8u > DoubleToNativeEndian(double val)
constexpr std::array< uint8_t, 1u > U8ToLittleEndian(uint8_t val)
constexpr int8_t I8FromLittleEndian(std::span< const uint8_t, 1u > bytes)
constexpr std::array< uint8_t, 8u > I64ToBigEndian(int64_t val)
constexpr float FloatFromNativeEndian(std::span< const uint8_t, 4u > bytes)
constexpr int16_t I16FromNativeEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 8u > I64ToNativeEndian(int64_t val)
constexpr int8_t I8FromNativeEndian(std::span< const uint8_t, 1u > bytes)
constexpr uint64_t U64FromBigEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 8u > U64ToLittleEndian(uint64_t val)
constexpr double DoubleFromBigEndian(std::span< const uint8_t, 8u > bytes)
constexpr uint8_t U8FromBigEndian(std::span< const uint8_t, 1u > bytes)
constexpr float FloatFromBigEndian(std::span< const uint8_t, 4u > bytes)
constexpr int64_t I64FromNativeEndian(std::span< const uint8_t, 8u > bytes)
constexpr int8_t I8FromBigEndian(std::span< const uint8_t, 1u > bytes)
constexpr double DoubleFromLittleEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 4u > I32ToLittleEndian(int32_t val)
constexpr int32_t I32FromLittleEndian(std::span< const uint8_t, 4u > bytes)
constexpr std::array< uint8_t, 8u > DoubleToLittleEndian(double val)
constexpr std::array< uint8_t, 8u > I64ToLittleEndian(int64_t val)
constexpr uint16_t U16FromNativeEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 4u > I32ToNativeEndian(int32_t val)
constexpr std::array< uint8_t, 2u > U16ToLittleEndian(uint16_t val)
constexpr std::array< uint8_t, 2u > I16ToLittleEndian(int16_t val)
constexpr uint16_t U16FromBigEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 2u > U16ToBigEndian(uint16_t val)
constexpr uint32_t U32FromNativeEndian(std::span< const uint8_t, 4u > bytes)