15 #include "absl/base/internal/endian.h"
23 #include "gtest/gtest.h"
24 #include "absl/base/config.h"
37 #if defined(ABSL_IS_BIG_ENDIAN)
39 const uint64_t k64ValueLE{0xefcdab8967452301};
40 const uint32_t k32ValueLE{0x67452301};
46 #elif defined(ABSL_IS_LITTLE_ENDIAN)
47 const uint64_t kInitialInNetworkOrder{0xefcdab8967452301};
52 const uint64_t k64ValueBE{0xefcdab8967452301};
53 const uint32_t k32ValueBE{0x67452301};
58 std::vector<uint16_t>
result;
70 result.reserve(num_values_to_test);
72 for (
size_t i = 0;
i < num_values_to_test; ++
i) {
93 sizeof(
T) == 1 ||
sizeof(
T) == 2 ||
sizeof(
T) == 4 ||
sizeof(
T) == 8,
94 "Unexpected type size");
97 case 1:
return *
reinterpret_cast<const T*
>(p);
99 return ABSL_INTERNAL_UNALIGNED_LOAD16(p);
101 return ABSL_INTERNAL_UNALIGNED_LOAD32(p);
103 return ABSL_INTERNAL_UNALIGNED_LOAD64(p);
110 template <
typename T,
typename ByteSwapper>
112 const ByteSwapper& byte_swapper) {
114 for (
typename std::vector<T>::const_iterator
it = host_values_to_test.begin();
115 it != host_values_to_test.end(); ++
it) {
118 char actual_value[
sizeof(host_value)];
119 memcpy(actual_value, &host_value,
sizeof(host_value));
120 byte_swapper(actual_value);
122 char expected_value[
sizeof(host_value)];
123 memcpy(expected_value, &host_value,
sizeof(host_value));
126 ASSERT_EQ(0, memcmp(actual_value, expected_value,
sizeof(host_value)))
127 <<
"Swap output for 0x" << std::hex << host_value <<
" does not match. "
128 <<
"Expected: 0x" << UnalignedLoad<T>(expected_value) <<
"; "
129 <<
"actual: 0x" << UnalignedLoad<T>(actual_value);
134 ABSL_INTERNAL_UNALIGNED_STORE16(
139 ABSL_INTERNAL_UNALIGNED_STORE32(
144 ABSL_INTERNAL_UNALIGNED_STORE64(
160 TEST(EndianessTest, ghtonll_gntohll) {
174 for (
int i = 0;
i < 100; ++
i) {
183 TEST(EndianessTest, little_endian) {
187 comp = little_endian::ToHost16(k16ValueLE);
191 comp = little_endian::FromHost32(
k32Value);
193 comp = little_endian::ToHost32(k32ValueLE);
197 comp = little_endian::FromHost64(
k64Value);
199 comp = little_endian::ToHost64(k64ValueLE);
223 TEST(EndianessTest, big_endian) {