bit_cast_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // Unit test for bit_cast template.
00016 
00017 #include <cstdint>
00018 #include <cstring>
00019 
00020 #include "gtest/gtest.h"
00021 #include "absl/base/casts.h"
00022 #include "absl/base/macros.h"
00023 
00024 namespace absl {
00025 namespace {
00026 
00027 template <int N>
00028 struct marshall { char buf[N]; };
00029 
00030 template <typename T>
00031 void TestMarshall(const T values[], int num_values) {
00032   for (int i = 0; i < num_values; ++i) {
00033     T t0 = values[i];
00034     marshall<sizeof(T)> m0 = absl::bit_cast<marshall<sizeof(T)> >(t0);
00035     T t1 = absl::bit_cast<T>(m0);
00036     marshall<sizeof(T)> m1 = absl::bit_cast<marshall<sizeof(T)> >(t1);
00037     ASSERT_EQ(0, memcmp(&t0, &t1, sizeof(T)));
00038     ASSERT_EQ(0, memcmp(&m0, &m1, sizeof(T)));
00039   }
00040 }
00041 
00042 // Convert back and forth to an integral type.  The C++ standard does
00043 // not guarantee this will work, but we test that this works on all the
00044 // platforms we support.
00045 //
00046 // Likewise, we below make assumptions about sizeof(float) and
00047 // sizeof(double) which the standard does not guarantee, but which hold on the
00048 // platforms we support.
00049 
00050 template <typename T, typename I>
00051 void TestIntegral(const T values[], int num_values) {
00052   for (int i = 0; i < num_values; ++i) {
00053     T t0 = values[i];
00054     I i0 = absl::bit_cast<I>(t0);
00055     T t1 = absl::bit_cast<T>(i0);
00056     I i1 = absl::bit_cast<I>(t1);
00057     ASSERT_EQ(0, memcmp(&t0, &t1, sizeof(T)));
00058     ASSERT_EQ(i0, i1);
00059   }
00060 }
00061 
00062 TEST(BitCast, Bool) {
00063   static const bool bool_list[] = { false, true };
00064   TestMarshall<bool>(bool_list, ABSL_ARRAYSIZE(bool_list));
00065 }
00066 
00067 TEST(BitCast, Int32) {
00068   static const int32_t int_list[] =
00069     { 0, 1, 100, 2147483647, -1, -100, -2147483647, -2147483647-1 };
00070   TestMarshall<int32_t>(int_list, ABSL_ARRAYSIZE(int_list));
00071 }
00072 
00073 TEST(BitCast, Int64) {
00074   static const int64_t int64_list[] =
00075     { 0, 1, 1LL << 40, -1, -(1LL<<40) };
00076   TestMarshall<int64_t>(int64_list, ABSL_ARRAYSIZE(int64_list));
00077 }
00078 
00079 TEST(BitCast, Uint64) {
00080   static const uint64_t uint64_list[] =
00081     { 0, 1, 1LLU << 40, 1LLU << 63 };
00082   TestMarshall<uint64_t>(uint64_list, ABSL_ARRAYSIZE(uint64_list));
00083 }
00084 
00085 TEST(BitCast, Float) {
00086   static const float float_list[] =
00087     { 0.0f, 1.0f, -1.0f, 10.0f, -10.0f,
00088       1e10f, 1e20f, 1e-10f, 1e-20f,
00089       2.71828f, 3.14159f };
00090   TestMarshall<float>(float_list, ABSL_ARRAYSIZE(float_list));
00091   TestIntegral<float, int>(float_list, ABSL_ARRAYSIZE(float_list));
00092   TestIntegral<float, unsigned>(float_list, ABSL_ARRAYSIZE(float_list));
00093 }
00094 
00095 TEST(BitCast, Double) {
00096   static const double double_list[] =
00097     { 0.0, 1.0, -1.0, 10.0, -10.0,
00098       1e10, 1e100, 1e-10, 1e-100,
00099       2.718281828459045,
00100       3.141592653589793238462643383279502884197169399375105820974944 };
00101   TestMarshall<double>(double_list, ABSL_ARRAYSIZE(double_list));
00102   TestIntegral<double, int64_t>(double_list, ABSL_ARRAYSIZE(double_list));
00103   TestIntegral<double, uint64_t>(double_list, ABSL_ARRAYSIZE(double_list));
00104 }
00105 
00106 }  // namespace
00107 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14