23 #include "gtest/gtest.h" 29 const uint64_t kInitialNumber{0x0123456789abcdef};
30 const uint64_t k64Value{kInitialNumber};
31 const uint32_t k32Value{0x01234567};
32 const uint16_t k16Value{0x0123};
33 const int kNumValuesToTest = 1000000;
34 const int kRandomSeed = 12345;
36 #if defined(ABSL_IS_BIG_ENDIAN) 37 const uint64_t kInitialInNetworkOrder{kInitialNumber};
38 const uint64_t k64ValueLE{0xefcdab8967452301};
39 const uint32_t k32ValueLE{0x67452301};
40 const uint16_t k16ValueLE{0x2301};
42 const uint64_t k64ValueBE{kInitialNumber};
43 const uint32_t k32ValueBE{k32Value};
44 const uint16_t k16ValueBE{k16Value};
45 #elif defined(ABSL_IS_LITTLE_ENDIAN) 46 const uint64_t kInitialInNetworkOrder{0xefcdab8967452301};
47 const uint64_t k64ValueLE{kInitialNumber};
48 const uint32_t k32ValueLE{k32Value};
49 const uint16_t k16ValueLE{k16Value};
51 const uint64_t k64ValueBE{0xefcdab8967452301};
52 const uint32_t k32ValueBE{0x67452301};
53 const uint16_t k16ValueBE{0x2301};
57 std::vector<T> GenerateAllValuesForType() {
58 std::vector<T> result;
59 T
next = std::numeric_limits<T>::min();
61 result.push_back(next);
62 if (next == std::numeric_limits<T>::max()) {
70 std::vector<T> GenerateRandomIntegers(
size_t numValuesToTest) {
71 std::vector<T> result;
72 std::mt19937_64 rng(kRandomSeed);
73 for (
size_t i = 0;
i < numValuesToTest; ++
i) {
74 result.push_back(rng());
79 void ManualByteSwap(
char* bytes,
int length) {
83 EXPECT_EQ(0, length % 2);
84 for (
int i = 0;
i < length / 2; ++
i) {
85 int j = (length - 1) -
i;
87 swap(bytes[
i], bytes[j]);
92 inline T UnalignedLoad(
const char* p) {
94 sizeof(T) == 1 ||
sizeof(T) == 2 ||
sizeof(T) == 4 ||
sizeof(T) == 8,
95 "Unexpected type size");
98 case 1:
return *
reinterpret_cast<const T*
>(p);
100 return ABSL_INTERNAL_UNALIGNED_LOAD16(p);
102 return ABSL_INTERNAL_UNALIGNED_LOAD32(p);
104 return ABSL_INTERNAL_UNALIGNED_LOAD64(p);
111 template <
typename T,
typename ByteSwapper>
112 static void GBSwapHelper(
const std::vector<T>& host_values_to_test,
113 const ByteSwapper& byte_swapper) {
115 for (
typename std::vector<T>::const_iterator it = host_values_to_test.begin();
116 it != host_values_to_test.end(); ++it) {
119 char actual_value[
sizeof(host_value)];
120 memcpy(actual_value, &host_value,
sizeof(host_value));
121 byte_swapper(actual_value);
123 char expected_value[
sizeof(host_value)];
124 memcpy(expected_value, &host_value,
sizeof(host_value));
125 ManualByteSwap(expected_value,
sizeof(host_value));
127 ASSERT_EQ(0, memcmp(actual_value, expected_value,
sizeof(host_value)))
128 <<
"Swap output for 0x" <<
std::hex << host_value <<
" does not match. " 129 <<
"Expected: 0x" << UnalignedLoad<T>(expected_value) <<
"; " 130 <<
"actual: 0x" << UnalignedLoad<T>(actual_value);
134 void Swap16(
char* bytes) {
135 ABSL_INTERNAL_UNALIGNED_STORE16(
136 bytes,
gbswap_16(ABSL_INTERNAL_UNALIGNED_LOAD16(bytes)));
139 void Swap32(
char* bytes) {
140 ABSL_INTERNAL_UNALIGNED_STORE32(
141 bytes,
gbswap_32(ABSL_INTERNAL_UNALIGNED_LOAD32(bytes)));
144 void Swap64(
char* bytes) {
145 ABSL_INTERNAL_UNALIGNED_STORE64(
146 bytes,
gbswap_64(ABSL_INTERNAL_UNALIGNED_LOAD64(bytes)));
149 TEST(EndianessTest, Uint16) {
150 GBSwapHelper(GenerateAllValuesForType<uint16_t>(), &Swap16);
153 TEST(EndianessTest, Uint32) {
154 GBSwapHelper(GenerateRandomIntegers<uint32_t>(kNumValuesToTest), &Swap32);
157 TEST(EndianessTest, Uint64) {
158 GBSwapHelper(GenerateRandomIntegers<uint64_t>(kNumValuesToTest), &Swap64);
161 TEST(EndianessTest, ghtonll_gntohll) {
163 uint32_t test = 0x01234567;
166 uint64_t comp = absl::ghtonll(kInitialNumber);
167 EXPECT_EQ(comp, kInitialInNetworkOrder);
169 EXPECT_EQ(comp, kInitialNumber);
175 for (
int i = 0;
i < 100; ++
i) {
177 EXPECT_EQ(value, comp);
179 EXPECT_EQ(value, comp);
184 TEST(EndianessTest, little_endian) {
186 uint64_t comp = little_endian::FromHost16(k16Value);
187 EXPECT_EQ(comp, k16ValueLE);
188 comp = little_endian::ToHost16(k16ValueLE);
189 EXPECT_EQ(comp, k16Value);
192 comp = little_endian::FromHost32(k32Value);
193 EXPECT_EQ(comp, k32ValueLE);
194 comp = little_endian::ToHost32(k32ValueLE);
195 EXPECT_EQ(comp, k32Value);
198 comp = little_endian::FromHost64(k64Value);
199 EXPECT_EQ(comp, k64ValueLE);
200 comp = little_endian::ToHost64(k64ValueLE);
201 EXPECT_EQ(comp, k64Value);
209 EXPECT_EQ(u16Buf, k16ValueLE);
211 EXPECT_EQ(comp, k16Value);
214 EXPECT_EQ(u32Buf, k32ValueLE);
216 EXPECT_EQ(comp, k32Value);
219 EXPECT_EQ(u64Buf, k64ValueLE);
221 EXPECT_EQ(comp, k64Value);
224 TEST(EndianessTest, big_endian) {
230 unsigned char buffer[10];
232 EXPECT_EQ(u16Buf, k16ValueBE);
234 EXPECT_EQ(comp, k16Value);
237 EXPECT_EQ(u32Buf, k32ValueBE);
239 EXPECT_EQ(comp, k32Value);
242 EXPECT_EQ(u64Buf, k64ValueBE);
244 EXPECT_EQ(comp, k64Value);
247 EXPECT_EQ(u16Buf, k16ValueBE);
249 EXPECT_EQ(comp, k16Value);
252 EXPECT_EQ(u32Buf, k32ValueBE);
254 EXPECT_EQ(comp, k32Value);
257 EXPECT_EQ(u64Buf, k64ValueBE);
259 EXPECT_EQ(comp, k64Value);
void Store32(void *p, uint32_t v)
void Store32(void *p, uint32_t v)
uint64_t Load64(const void *p)
uint64_t Load64(const void *p)
void Store64(void *p, uint64_t v)
uint64_t gntohll(uint64_t x)
TEST(NotificationTest, SanityTest)
uint64_t gbswap_64(uint64_t host_int)
uint16_t Load16(const void *p)
uint32_t Load32(const void *p)
void Store64(void *p, uint64_t v)
uint32_t gntohl(uint32_t x)
void Store16(void *p, uint16_t v)
uint32_t gbswap_32(uint32_t host_int)
uint16_t gbswap_16(uint16_t host_int)
AllocList * next[kMaxLevel]
uint32_t Load32(const void *p)
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
void Store16(void *p, uint16_t v)
uint16_t Load16(const void *p)