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