reinterpret_bytes_test.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2017 The Crashpad Authors. All rights reserved.
  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. // http://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 "util/misc/reinterpret_bytes.h"
  15. #include <stdint.h>
  16. #include <limits>
  17. #include "base/bit_cast.h"
  18. #include "gtest/gtest.h"
  19. #include "util/numeric/int128.h"
  20. namespace crashpad {
  21. namespace test {
  22. namespace {
  23. template <typename From, typename To>
  24. void ExpectReinterpret(From from, To* to, To expected) {
  25. ASSERT_TRUE(ReinterpretBytes(from, to));
  26. EXPECT_EQ(*to, expected);
  27. }
  28. template <typename From, typename To>
  29. void ExpectUnsignedEqual(From from, To* to) {
  30. To expected = static_cast<To>(from);
  31. ExpectReinterpret(from, to, expected);
  32. }
  33. TEST(ReinterpretBytes, ToUnsigned) {
  34. uint64_t from64, to64;
  35. uint32_t from32, to32;
  36. from32 = 0;
  37. ExpectUnsignedEqual(from32, &to32);
  38. ExpectUnsignedEqual(from32, &to64);
  39. from32 = std::numeric_limits<uint32_t>::max();
  40. ExpectUnsignedEqual(from32, &to32);
  41. ExpectUnsignedEqual(from32, &to64);
  42. from64 = 0;
  43. ExpectUnsignedEqual(from64, &to32);
  44. ExpectUnsignedEqual(from64, &to64);
  45. from64 = std::numeric_limits<uint64_t>::max();
  46. ExpectUnsignedEqual(from64, &to64);
  47. EXPECT_FALSE(ReinterpretBytes(from64, &to32));
  48. uint8_t to8 = std::numeric_limits<uint8_t>::max();
  49. uint128_struct from128;
  50. from128.lo = to8;
  51. from128.hi = 0;
  52. ExpectReinterpret(from128, &to8, to8);
  53. }
  54. TEST(ReinterpretBytes, ToSigned) {
  55. uint64_t from64;
  56. int64_t to64;
  57. int32_t to32;
  58. from64 = 0;
  59. ExpectReinterpret(from64, &to32, static_cast<int32_t>(0));
  60. ExpectReinterpret(from64, &to64, static_cast<int64_t>(0));
  61. to32 = -1;
  62. from64 = bit_cast<uint32_t>(to32);
  63. ExpectReinterpret(from64, &to32, to32);
  64. to32 = std::numeric_limits<int32_t>::max();
  65. from64 = bit_cast<uint32_t>(to32);
  66. ExpectReinterpret(from64, &to32, to32);
  67. to32 = std::numeric_limits<int32_t>::min();
  68. from64 = bit_cast<uint32_t>(to32);
  69. ExpectReinterpret(from64, &to32, to32);
  70. to64 = -1;
  71. from64 = bit_cast<uint64_t>(to64);
  72. ExpectReinterpret(from64, &to64, to64);
  73. to64 = std::numeric_limits<int64_t>::max();
  74. from64 = bit_cast<uint64_t>(to64);
  75. ExpectReinterpret(from64, &to64, to64);
  76. to64 = std::numeric_limits<int64_t>::min();
  77. from64 = bit_cast<uint64_t>(to64);
  78. ExpectReinterpret(from64, &to64, to64);
  79. }
  80. } // namespace
  81. } // namespace test
  82. } // namespace crashpad