/* * Copyright (c) 2018-2020, Nick Johnson * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include TEST_CASE(wrapped_popcount) { EXPECT_EQ(popcount(NumericLimits::max()), 8); EXPECT_EQ(popcount(NumericLimits::max()), 16); EXPECT_EQ(popcount(NumericLimits::max()), 32); EXPECT_EQ(popcount(NumericLimits::max()), 64); EXPECT_EQ(popcount(NumericLimits::max()), static_cast(8 * sizeof(size_t))); EXPECT_EQ(popcount(0u), 0); EXPECT_EQ(popcount(0b01010101ULL), 4); } TEST_CASE(wrapped_count_leading_zeroes) { EXPECT_EQ(count_leading_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_leading_zeroes(static_cast(0x20)), 2); EXPECT_EQ(count_leading_zeroes_safe(static_cast(0)), 8); EXPECT_EQ(count_leading_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_leading_zeroes(static_cast(0x20)), 10); EXPECT_EQ(count_leading_zeroes_safe(static_cast(0)), 16); EXPECT_EQ(count_leading_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_leading_zeroes(static_cast(0x20)), 26); EXPECT_EQ(count_leading_zeroes_safe(static_cast(0)), 32); EXPECT_EQ(count_leading_zeroes(NumericLimits::max()), 0); } TEST_CASE(wrapped_count_trailing_zeroes) { EXPECT_EQ(count_trailing_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(1)), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(2)), 1); EXPECT_EQ(count_trailing_zeroes_safe(static_cast(0)), 8); EXPECT_EQ(count_trailing_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(1)), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(2)), 1); EXPECT_EQ(count_trailing_zeroes_safe(static_cast(0)), 16); EXPECT_EQ(count_trailing_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(1)), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(2)), 1); EXPECT_EQ(count_trailing_zeroes_safe(static_cast(0)), 32); EXPECT_EQ(count_trailing_zeroes(NumericLimits::max()), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(1)), 0); EXPECT_EQ(count_trailing_zeroes(static_cast(2)), 1); }