diff options
author | Benjamin Barenblat <bbaren@google.com> | 2023-09-07 13:16:09 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2023-09-07 13:16:09 -0400 |
commit | 6fdbff8bbce2a1debdc060df381f39e3dcfb65af (patch) | |
tree | 71f1ef38477a65d5cce472fc042c90087c2bb351 /absl/numeric/bits_test.cc | |
parent | 8d4a80fe37176b1170d7dce0772dea9584ec3e32 (diff) | |
parent | 29bf8085f3bf17b84d30e34b3d7ff8248fda404e (diff) | |
download | abseil-6fdbff8bbce2a1debdc060df381f39e3dcfb65af.tar.gz abseil-6fdbff8bbce2a1debdc060df381f39e3dcfb65af.tar.bz2 abseil-6fdbff8bbce2a1debdc060df381f39e3dcfb65af.zip |
Merge new upstream LTS 20230802.0
Diffstat (limited to 'absl/numeric/bits_test.cc')
-rw-r--r-- | absl/numeric/bits_test.cc | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/absl/numeric/bits_test.cc b/absl/numeric/bits_test.cc index 7c942aae..14955eb3 100644 --- a/absl/numeric/bits_test.cc +++ b/absl/numeric/bits_test.cc @@ -15,6 +15,7 @@ #include "absl/numeric/bits.h" #include <limits> +#include <type_traits> #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -24,6 +25,73 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace { +template <typename IntT> +class IntegerTypesTest : public ::testing::Test {}; + +using OneByteIntegerTypes = ::testing::Types< + unsigned char, + uint8_t + >; + +TYPED_TEST_SUITE(IntegerTypesTest, OneByteIntegerTypes); + +TYPED_TEST(IntegerTypesTest, HandlesTypes) { + using UIntType = TypeParam; + + EXPECT_EQ(rotl(UIntType{0x12}, 0), uint8_t{0x12}); + EXPECT_EQ(rotr(UIntType{0x12}, -4), uint8_t{0x21}); + static_assert(rotl(UIntType{0x12}, 0) == uint8_t{0x12}, ""); + + static_assert(rotr(UIntType{0x12}, 0) == uint8_t{0x12}, ""); + EXPECT_EQ(rotr(UIntType{0x12}, 0), uint8_t{0x12}); + +#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ + static_assert(countl_zero(UIntType{}) == 8, ""); + static_assert(countl_zero(static_cast<UIntType>(-1)) == 0, ""); + + static_assert(countl_one(UIntType{}) == 0, ""); + static_assert(countl_one(static_cast<UIntType>(-1)) == 8, ""); + + static_assert(countr_zero(UIntType{}) == 8, ""); + static_assert(countr_zero(static_cast<UIntType>(-1)) == 0, ""); + + static_assert(countr_one(UIntType{}) == 0, ""); + static_assert(countr_one(static_cast<UIntType>(-1)) == 8, ""); + + static_assert(popcount(UIntType{}) == 0, ""); + static_assert(popcount(UIntType{1}) == 1, ""); + static_assert(popcount(static_cast<UIntType>(-1)) == 8, ""); + + static_assert(bit_width(UIntType{}) == 0, ""); + static_assert(bit_width(UIntType{1}) == 1, ""); + static_assert(bit_width(UIntType{3}) == 2, ""); + static_assert(bit_width(static_cast<UIntType>(-1)) == 8, ""); +#endif + + EXPECT_EQ(countl_zero(UIntType{}), 8); + EXPECT_EQ(countl_zero(static_cast<UIntType>(-1)), 0); + + EXPECT_EQ(countl_one(UIntType{}), 0); + EXPECT_EQ(countl_one(static_cast<UIntType>(-1)), 8); + + EXPECT_EQ(countr_zero(UIntType{}), 8); + EXPECT_EQ(countr_zero(static_cast<UIntType>(-1)), 0); + + EXPECT_EQ(countr_one(UIntType{}), 0); + EXPECT_EQ(countr_one(static_cast<UIntType>(-1)), 8); + + EXPECT_EQ(popcount(UIntType{}), 0); + EXPECT_EQ(popcount(UIntType{1}), 1); + + EXPECT_FALSE(has_single_bit(UIntType{})); + EXPECT_FALSE(has_single_bit(static_cast<UIntType>(-1))); + + EXPECT_EQ(bit_width(UIntType{}), 0); + EXPECT_EQ(bit_width(UIntType{1}), 1); + EXPECT_EQ(bit_width(UIntType{3}), 2); + EXPECT_EQ(bit_width(static_cast<UIntType>(-1)), 8); +} + TEST(Rotate, Left) { static_assert(rotl(uint8_t{0x12}, 0) == uint8_t{0x12}, ""); static_assert(rotl(uint16_t{0x1234}, 0) == uint16_t{0x1234}, ""); |