traits_test.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/random/internal/traits.h"
  15. #include <cstdint>
  16. #include <type_traits>
  17. #include "gtest/gtest.h"
  18. namespace {
  19. using absl::random_internal::is_widening_convertible;
  20. // CheckWideningConvertsToSelf<T1, T2, ...>()
  21. //
  22. // For each type T, checks:
  23. // - T IS widening-convertible to itself.
  24. //
  25. template <typename T>
  26. void CheckWideningConvertsToSelf() {
  27. static_assert(is_widening_convertible<T, T>::value,
  28. "Type is not convertible to self!");
  29. }
  30. template <typename T, typename Next, typename... Args>
  31. void CheckWideningConvertsToSelf() {
  32. CheckWideningConvertsToSelf<T>();
  33. CheckWideningConvertsToSelf<Next, Args...>();
  34. }
  35. // CheckNotWideningConvertibleWithSigned<T1, T2, ...>()
  36. //
  37. // For each unsigned-type T, checks that:
  38. // - T is NOT widening-convertible to Signed(T)
  39. // - Signed(T) is NOT widening-convertible to T
  40. //
  41. template <typename T>
  42. void CheckNotWideningConvertibleWithSigned() {
  43. using signed_t = typename std::make_signed<T>::type;
  44. static_assert(!is_widening_convertible<T, signed_t>::value,
  45. "Unsigned type is convertible to same-sized signed-type!");
  46. static_assert(!is_widening_convertible<signed_t, T>::value,
  47. "Signed type is convertible to same-sized unsigned-type!");
  48. }
  49. template <typename T, typename Next, typename... Args>
  50. void CheckNotWideningConvertibleWithSigned() {
  51. CheckNotWideningConvertibleWithSigned<T>();
  52. CheckWideningConvertsToSelf<Next, Args...>();
  53. }
  54. // CheckWideningConvertsToLargerType<T1, T2, ...>()
  55. //
  56. // For each successive unsigned-types {Ti, Ti+1}, checks that:
  57. // - Ti IS widening-convertible to Ti+1
  58. // - Ti IS widening-convertible to Signed(Ti+1)
  59. // - Signed(Ti) is NOT widening-convertible to Ti
  60. // - Signed(Ti) IS widening-convertible to Ti+1
  61. template <typename T, typename Higher>
  62. void CheckWideningConvertsToLargerTypes() {
  63. using signed_t = typename std::make_signed<T>::type;
  64. using higher_t = Higher;
  65. using signed_higher_t = typename std::make_signed<Higher>::type;
  66. static_assert(is_widening_convertible<T, higher_t>::value,
  67. "Type not embeddable into larger type!");
  68. static_assert(is_widening_convertible<T, signed_higher_t>::value,
  69. "Type not embeddable into larger signed type!");
  70. static_assert(!is_widening_convertible<signed_t, higher_t>::value,
  71. "Signed type is embeddable into larger unsigned type!");
  72. static_assert(is_widening_convertible<signed_t, signed_higher_t>::value,
  73. "Signed type not embeddable into larger signed type!");
  74. }
  75. template <typename T, typename Higher, typename Next, typename... Args>
  76. void CheckWideningConvertsToLargerTypes() {
  77. CheckWideningConvertsToLargerTypes<T, Higher>();
  78. CheckWideningConvertsToLargerTypes<Higher, Next, Args...>();
  79. }
  80. // CheckWideningConvertsTo<T, U, [expect]>
  81. //
  82. // Checks that T DOES widening-convert to U.
  83. // If "expect" is false, then asserts that T does NOT widening-convert to U.
  84. template <typename T, typename U, bool expect = true>
  85. void CheckWideningConvertsTo() {
  86. static_assert(is_widening_convertible<T, U>::value == expect,
  87. "Unexpected result for is_widening_convertible<T, U>!");
  88. }
  89. TEST(TraitsTest, IsWideningConvertibleTest) {
  90. constexpr bool kInvalid = false;
  91. CheckWideningConvertsToSelf<
  92. uint8_t, uint16_t, uint32_t, uint64_t,
  93. int8_t, int16_t, int32_t, int64_t,
  94. float, double>();
  95. CheckNotWideningConvertibleWithSigned<
  96. uint8_t, uint16_t, uint32_t, uint64_t>();
  97. CheckWideningConvertsToLargerTypes<
  98. uint8_t, uint16_t, uint32_t, uint64_t>();
  99. CheckWideningConvertsTo<float, double>();
  100. CheckWideningConvertsTo<uint16_t, float>();
  101. CheckWideningConvertsTo<uint32_t, double>();
  102. CheckWideningConvertsTo<uint64_t, double, kInvalid>();
  103. CheckWideningConvertsTo<double, float, kInvalid>();
  104. CheckWideningConvertsTo<bool, int>();
  105. CheckWideningConvertsTo<bool, float>();
  106. }
  107. } // namespace