Skip to content

Commit fc0b7a0

Browse files
jmrcopybara-github
authored andcommitted
[bits] Add tests for return types
With gcc <= 12, std::bit_width<T>() returns T, not int, so make sure the absl:: equivalents return the correct type. #1890 PiperOrigin-RevId: 760612745 Change-Id: Ibbbe6eaa1aab677ecd747cf40765f6443eefe628
1 parent 41a1f43 commit fc0b7a0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

absl/numeric/bits_test.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,37 @@ namespace absl {
2626
ABSL_NAMESPACE_BEGIN
2727
namespace {
2828

29+
template <typename IntT>
30+
class UnsignedIntegerTypesTest : public ::testing::Test {};
2931
template <typename IntT>
3032
class IntegerTypesTest : public ::testing::Test {};
3133

34+
using UnsignedIntegerTypes =
35+
::testing::Types<uint8_t, uint16_t, uint32_t, uint64_t>;
3236
using OneByteIntegerTypes = ::testing::Types<
3337
unsigned char,
3438
uint8_t
3539
>;
3640

41+
TYPED_TEST_SUITE(UnsignedIntegerTypesTest, UnsignedIntegerTypes);
3742
TYPED_TEST_SUITE(IntegerTypesTest, OneByteIntegerTypes);
3843

44+
TYPED_TEST(UnsignedIntegerTypesTest, ReturnTypes) {
45+
using UIntType = TypeParam;
46+
47+
static_assert(std::is_same_v<decltype(byteswap(UIntType{0})), UIntType>);
48+
static_assert(std::is_same_v<decltype(rotl(UIntType{0}, 0)), UIntType>);
49+
static_assert(std::is_same_v<decltype(rotr(UIntType{0}, 0)), UIntType>);
50+
static_assert(std::is_same_v<decltype(countl_zero(UIntType{0})), int>);
51+
static_assert(std::is_same_v<decltype(countl_one(UIntType{0})), int>);
52+
static_assert(std::is_same_v<decltype(countr_zero(UIntType{0})), int>);
53+
static_assert(std::is_same_v<decltype(countr_one(UIntType{0})), int>);
54+
static_assert(std::is_same_v<decltype(popcount(UIntType{0})), int>);
55+
static_assert(std::is_same_v<decltype(bit_ceil(UIntType{0})), UIntType>);
56+
static_assert(std::is_same_v<decltype(bit_floor(UIntType{0})), UIntType>);
57+
static_assert(std::is_same_v<decltype(bit_width(UIntType{0})), int>);
58+
}
59+
3960
TYPED_TEST(IntegerTypesTest, HandlesTypes) {
4061
using UIntType = TypeParam;
4162

0 commit comments

Comments
 (0)