endian_test.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/base/internal/endian.h"
  15. #include <algorithm>
  16. #include <cstdint>
  17. #include <limits>
  18. #include <random>
  19. #include <vector>
  20. #include "gtest/gtest.h"
  21. #include "absl/base/config.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace {
  25. const uint64_t kInitialNumber{0x0123456789abcdef};
  26. const uint64_t k64Value{kInitialNumber};
  27. const uint32_t k32Value{0x01234567};
  28. const uint16_t k16Value{0x0123};
  29. const int kNumValuesToTest = 1000000;
  30. const int kRandomSeed = 12345;
  31. #if defined(ABSL_IS_BIG_ENDIAN)
  32. const uint64_t kInitialInNetworkOrder{kInitialNumber};
  33. const uint64_t k64ValueLE{0xefcdab8967452301};
  34. const uint32_t k32ValueLE{0x67452301};
  35. const uint16_t k16ValueLE{0x2301};
  36. const uint64_t k64ValueBE{kInitialNumber};
  37. const uint32_t k32ValueBE{k32Value};
  38. const uint16_t k16ValueBE{k16Value};
  39. #elif defined(ABSL_IS_LITTLE_ENDIAN)
  40. const uint64_t kInitialInNetworkOrder{0xefcdab8967452301};
  41. const uint64_t k64ValueLE{kInitialNumber};
  42. const uint32_t k32ValueLE{k32Value};
  43. const uint16_t k16ValueLE{k16Value};
  44. const uint64_t k64ValueBE{0xefcdab8967452301};
  45. const uint32_t k32ValueBE{0x67452301};
  46. const uint16_t k16ValueBE{0x2301};
  47. #endif
  48. std::vector<uint16_t> GenerateAllUint16Values() {
  49. std::vector<uint16_t> result;
  50. result.reserve(size_t{1} << (sizeof(uint16_t) * 8));
  51. for (uint32_t i = std::numeric_limits<uint16_t>::min();
  52. i <= std::numeric_limits<uint16_t>::max(); ++i) {
  53. result.push_back(static_cast<uint16_t>(i));
  54. }
  55. return result;
  56. }
  57. template<typename T>
  58. std::vector<T> GenerateRandomIntegers(size_t num_values_to_test) {
  59. std::vector<T> result;
  60. result.reserve(num_values_to_test);
  61. std::mt19937_64 rng(kRandomSeed);
  62. for (size_t i = 0; i < num_values_to_test; ++i) {
  63. result.push_back(rng());
  64. }
  65. return result;
  66. }
  67. void ManualByteSwap(char* bytes, int length) {
  68. if (length == 1)
  69. return;
  70. EXPECT_EQ(0, length % 2);
  71. for (int i = 0; i < length / 2; ++i) {
  72. int j = (length - 1) - i;
  73. using std::swap;
  74. swap(bytes[i], bytes[j]);
  75. }
  76. }
  77. template<typename T>
  78. inline T UnalignedLoad(const char* p) {
  79. static_assert(
  80. sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
  81. "Unexpected type size");
  82. switch (sizeof(T)) {
  83. case 1: return *reinterpret_cast<const T*>(p);
  84. case 2:
  85. return ABSL_INTERNAL_UNALIGNED_LOAD16(p);
  86. case 4:
  87. return ABSL_INTERNAL_UNALIGNED_LOAD32(p);
  88. case 8:
  89. return ABSL_INTERNAL_UNALIGNED_LOAD64(p);
  90. default:
  91. // Suppresses invalid "not all control paths return a value" on MSVC
  92. return {};
  93. }
  94. }
  95. template <typename T, typename ByteSwapper>
  96. static void GBSwapHelper(const std::vector<T>& host_values_to_test,
  97. const ByteSwapper& byte_swapper) {
  98. // Test byte_swapper against a manual byte swap.
  99. for (typename std::vector<T>::const_iterator it = host_values_to_test.begin();
  100. it != host_values_to_test.end(); ++it) {
  101. T host_value = *it;
  102. char actual_value[sizeof(host_value)];
  103. memcpy(actual_value, &host_value, sizeof(host_value));
  104. byte_swapper(actual_value);
  105. char expected_value[sizeof(host_value)];
  106. memcpy(expected_value, &host_value, sizeof(host_value));
  107. ManualByteSwap(expected_value, sizeof(host_value));
  108. ASSERT_EQ(0, memcmp(actual_value, expected_value, sizeof(host_value)))
  109. << "Swap output for 0x" << std::hex << host_value << " does not match. "
  110. << "Expected: 0x" << UnalignedLoad<T>(expected_value) << "; "
  111. << "actual: 0x" << UnalignedLoad<T>(actual_value);
  112. }
  113. }
  114. void Swap16(char* bytes) {
  115. ABSL_INTERNAL_UNALIGNED_STORE16(
  116. bytes, gbswap_16(ABSL_INTERNAL_UNALIGNED_LOAD16(bytes)));
  117. }
  118. void Swap32(char* bytes) {
  119. ABSL_INTERNAL_UNALIGNED_STORE32(
  120. bytes, gbswap_32(ABSL_INTERNAL_UNALIGNED_LOAD32(bytes)));
  121. }
  122. void Swap64(char* bytes) {
  123. ABSL_INTERNAL_UNALIGNED_STORE64(
  124. bytes, gbswap_64(ABSL_INTERNAL_UNALIGNED_LOAD64(bytes)));
  125. }
  126. TEST(EndianessTest, Uint16) {
  127. GBSwapHelper(GenerateAllUint16Values(), &Swap16);
  128. }
  129. TEST(EndianessTest, Uint32) {
  130. GBSwapHelper(GenerateRandomIntegers<uint32_t>(kNumValuesToTest), &Swap32);
  131. }
  132. TEST(EndianessTest, Uint64) {
  133. GBSwapHelper(GenerateRandomIntegers<uint64_t>(kNumValuesToTest), &Swap64);
  134. }
  135. TEST(EndianessTest, ghtonll_gntohll) {
  136. // Test that absl::ghtonl compiles correctly
  137. uint32_t test = 0x01234567;
  138. EXPECT_EQ(absl::gntohl(absl::ghtonl(test)), test);
  139. uint64_t comp = absl::ghtonll(kInitialNumber);
  140. EXPECT_EQ(comp, kInitialInNetworkOrder);
  141. comp = absl::gntohll(kInitialInNetworkOrder);
  142. EXPECT_EQ(comp, kInitialNumber);
  143. // Test that htonll and ntohll are each others' inverse functions on a
  144. // somewhat assorted batch of numbers. 37 is chosen to not be anything
  145. // particularly nice base 2.
  146. uint64_t value = 1;
  147. for (int i = 0; i < 100; ++i) {
  148. comp = absl::ghtonll(absl::gntohll(value));
  149. EXPECT_EQ(value, comp);
  150. comp = absl::gntohll(absl::ghtonll(value));
  151. EXPECT_EQ(value, comp);
  152. value *= 37;
  153. }
  154. }
  155. TEST(EndianessTest, little_endian) {
  156. // Check little_endian uint16_t.
  157. uint64_t comp = little_endian::FromHost16(k16Value);
  158. EXPECT_EQ(comp, k16ValueLE);
  159. comp = little_endian::ToHost16(k16ValueLE);
  160. EXPECT_EQ(comp, k16Value);
  161. // Check little_endian uint32_t.
  162. comp = little_endian::FromHost32(k32Value);
  163. EXPECT_EQ(comp, k32ValueLE);
  164. comp = little_endian::ToHost32(k32ValueLE);
  165. EXPECT_EQ(comp, k32Value);
  166. // Check little_endian uint64_t.
  167. comp = little_endian::FromHost64(k64Value);
  168. EXPECT_EQ(comp, k64ValueLE);
  169. comp = little_endian::ToHost64(k64ValueLE);
  170. EXPECT_EQ(comp, k64Value);
  171. // Check little-endian Load and store functions.
  172. uint16_t u16Buf;
  173. uint32_t u32Buf;
  174. uint64_t u64Buf;
  175. little_endian::Store16(&u16Buf, k16Value);
  176. EXPECT_EQ(u16Buf, k16ValueLE);
  177. comp = little_endian::Load16(&u16Buf);
  178. EXPECT_EQ(comp, k16Value);
  179. little_endian::Store32(&u32Buf, k32Value);
  180. EXPECT_EQ(u32Buf, k32ValueLE);
  181. comp = little_endian::Load32(&u32Buf);
  182. EXPECT_EQ(comp, k32Value);
  183. little_endian::Store64(&u64Buf, k64Value);
  184. EXPECT_EQ(u64Buf, k64ValueLE);
  185. comp = little_endian::Load64(&u64Buf);
  186. EXPECT_EQ(comp, k64Value);
  187. }
  188. TEST(EndianessTest, big_endian) {
  189. // Check big-endian Load and store functions.
  190. uint16_t u16Buf;
  191. uint32_t u32Buf;
  192. uint64_t u64Buf;
  193. unsigned char buffer[10];
  194. big_endian::Store16(&u16Buf, k16Value);
  195. EXPECT_EQ(u16Buf, k16ValueBE);
  196. uint64_t comp = big_endian::Load16(&u16Buf);
  197. EXPECT_EQ(comp, k16Value);
  198. big_endian::Store32(&u32Buf, k32Value);
  199. EXPECT_EQ(u32Buf, k32ValueBE);
  200. comp = big_endian::Load32(&u32Buf);
  201. EXPECT_EQ(comp, k32Value);
  202. big_endian::Store64(&u64Buf, k64Value);
  203. EXPECT_EQ(u64Buf, k64ValueBE);
  204. comp = big_endian::Load64(&u64Buf);
  205. EXPECT_EQ(comp, k64Value);
  206. big_endian::Store16(buffer + 1, k16Value);
  207. EXPECT_EQ(u16Buf, k16ValueBE);
  208. comp = big_endian::Load16(buffer + 1);
  209. EXPECT_EQ(comp, k16Value);
  210. big_endian::Store32(buffer + 1, k32Value);
  211. EXPECT_EQ(u32Buf, k32ValueBE);
  212. comp = big_endian::Load32(buffer + 1);
  213. EXPECT_EQ(comp, k32Value);
  214. big_endian::Store64(buffer + 1, k64Value);
  215. EXPECT_EQ(u64Buf, k64ValueBE);
  216. comp = big_endian::Load64(buffer + 1);
  217. EXPECT_EQ(comp, k64Value);
  218. }
  219. } // namespace
  220. ABSL_NAMESPACE_END
  221. } // namespace absl