bit_cast_test.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // Unit test for bit_cast template.
  15. #include <cstdint>
  16. #include <cstring>
  17. #include "gtest/gtest.h"
  18. #include "absl/base/casts.h"
  19. #include "absl/base/macros.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace {
  23. template <int N>
  24. struct marshall { char buf[N]; };
  25. template <typename T>
  26. void TestMarshall(const T values[], int num_values) {
  27. for (int i = 0; i < num_values; ++i) {
  28. T t0 = values[i];
  29. marshall<sizeof(T)> m0 = absl::bit_cast<marshall<sizeof(T)> >(t0);
  30. T t1 = absl::bit_cast<T>(m0);
  31. marshall<sizeof(T)> m1 = absl::bit_cast<marshall<sizeof(T)> >(t1);
  32. ASSERT_EQ(0, memcmp(&t0, &t1, sizeof(T)));
  33. ASSERT_EQ(0, memcmp(&m0, &m1, sizeof(T)));
  34. }
  35. }
  36. // Convert back and forth to an integral type. The C++ standard does
  37. // not guarantee this will work, but we test that this works on all the
  38. // platforms we support.
  39. //
  40. // Likewise, we below make assumptions about sizeof(float) and
  41. // sizeof(double) which the standard does not guarantee, but which hold on the
  42. // platforms we support.
  43. template <typename T, typename I>
  44. void TestIntegral(const T values[], int num_values) {
  45. for (int i = 0; i < num_values; ++i) {
  46. T t0 = values[i];
  47. I i0 = absl::bit_cast<I>(t0);
  48. T t1 = absl::bit_cast<T>(i0);
  49. I i1 = absl::bit_cast<I>(t1);
  50. ASSERT_EQ(0, memcmp(&t0, &t1, sizeof(T)));
  51. ASSERT_EQ(i0, i1);
  52. }
  53. }
  54. TEST(BitCast, Bool) {
  55. static const bool bool_list[] = { false, true };
  56. TestMarshall<bool>(bool_list, ABSL_ARRAYSIZE(bool_list));
  57. }
  58. TEST(BitCast, Int32) {
  59. static const int32_t int_list[] =
  60. { 0, 1, 100, 2147483647, -1, -100, -2147483647, -2147483647-1 };
  61. TestMarshall<int32_t>(int_list, ABSL_ARRAYSIZE(int_list));
  62. }
  63. TEST(BitCast, Int64) {
  64. static const int64_t int64_list[] =
  65. { 0, 1, 1LL << 40, -1, -(1LL<<40) };
  66. TestMarshall<int64_t>(int64_list, ABSL_ARRAYSIZE(int64_list));
  67. }
  68. TEST(BitCast, Uint64) {
  69. static const uint64_t uint64_list[] =
  70. { 0, 1, 1LLU << 40, 1LLU << 63 };
  71. TestMarshall<uint64_t>(uint64_list, ABSL_ARRAYSIZE(uint64_list));
  72. }
  73. TEST(BitCast, Float) {
  74. static const float float_list[] =
  75. { 0.0f, 1.0f, -1.0f, 10.0f, -10.0f,
  76. 1e10f, 1e20f, 1e-10f, 1e-20f,
  77. 2.71828f, 3.14159f };
  78. TestMarshall<float>(float_list, ABSL_ARRAYSIZE(float_list));
  79. TestIntegral<float, int>(float_list, ABSL_ARRAYSIZE(float_list));
  80. TestIntegral<float, unsigned>(float_list, ABSL_ARRAYSIZE(float_list));
  81. }
  82. TEST(BitCast, Double) {
  83. static const double double_list[] =
  84. { 0.0, 1.0, -1.0, 10.0, -10.0,
  85. 1e10, 1e100, 1e-10, 1e-100,
  86. 2.718281828459045,
  87. 3.141592653589793238462643383279502884197169399375105820974944 };
  88. TestMarshall<double>(double_list, ABSL_ARRAYSIZE(double_list));
  89. TestIntegral<double, int64_t>(double_list, ABSL_ARRAYSIZE(double_list));
  90. TestIntegral<double, uint64_t>(double_list, ABSL_ARRAYSIZE(double_list));
  91. }
  92. } // namespace
  93. ABSL_NAMESPACE_END
  94. } // namespace absl