numeric/endian: Add constexpr endian conversion functions
These are as fast as the c library versions but they are also constexpr.
Change-Id: I95bb1562a5a15a7f9c57a3483afe364a0286da48
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/meson.build b/test/meson.build
index 1e15a0e..51bc288 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -6,6 +6,7 @@
'hash': [stdplus_dep, gtest_main_dep],
'hash/array': [stdplus_dep, gtest_main_dep],
'hash/tuple': [stdplus_dep, gtest_main_dep],
+ 'numeric/endian': [stdplus_dep, gtest_main_dep],
'pinned': [stdplus_dep, gtest_main_dep],
'raw': [stdplus_dep, gmock_dep, gtest_main_dep],
'signal': [stdplus_dep, gtest_main_dep],
diff --git a/test/numeric/endian.cpp b/test/numeric/endian.cpp
new file mode 100644
index 0000000..d8f2ae8
--- /dev/null
+++ b/test/numeric/endian.cpp
@@ -0,0 +1,24 @@
+#include <stdplus/numeric/endian.hpp>
+
+#include <array>
+
+#include <gtest/gtest.h>
+
+namespace stdplus
+{
+
+TEST(Byteswap, Swap)
+{
+ EXPECT_EQ(38, bswap(uint8_t{38}));
+ EXPECT_EQ(38 << 8, bswap(uint16_t{38}));
+ EXPECT_EQ(0x240082fe, bswap(uint32_t{0xfe820024}));
+ EXPECT_EQ(0x240082fe00000000, bswap(uint64_t{0xfe820024}));
+ struct
+ {
+ std::array<char, 4> a = {1, 2, 3, 4};
+ } s;
+ EXPECT_EQ((std::array<char, 4>{4, 3, 2, 1}), bswap(s).a);
+ EXPECT_EQ(40, ntoh(hton(40)));
+}
+
+} // namespace stdplus