bit.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <cmath>
  2. #include <cstddef>
  3. #include <limits>
  4. #include <gtest/gtest.h>
  5. #include <entt/core/bit.hpp>
  6. #include "../../common/config.h"
  7. TEST(PopCount, Functionalities) {
  8. // constexpr-ness guaranteed
  9. constexpr auto zero_popcount = entt::popcount(0u);
  10. ASSERT_EQ(zero_popcount, 0u);
  11. ASSERT_EQ(entt::popcount(1u), 1u);
  12. ASSERT_EQ(entt::popcount(2u), 1u);
  13. ASSERT_EQ(entt::popcount(3u), 2u);
  14. ASSERT_EQ(entt::popcount(7u), 3u);
  15. ASSERT_EQ(entt::popcount(128u), 1u);
  16. ASSERT_EQ(entt::popcount(201u), 4u);
  17. }
  18. TEST(HasSingleBit, Functionalities) {
  19. // constexpr-ness guaranteed
  20. constexpr auto zero_is_power_of_two = entt::has_single_bit(0u);
  21. ASSERT_FALSE(zero_is_power_of_two);
  22. ASSERT_TRUE(entt::has_single_bit(1u));
  23. ASSERT_TRUE(entt::has_single_bit(2u));
  24. ASSERT_TRUE(entt::has_single_bit(4u));
  25. ASSERT_FALSE(entt::has_single_bit(7u));
  26. ASSERT_TRUE(entt::has_single_bit(128u));
  27. ASSERT_FALSE(entt::has_single_bit(200u));
  28. }
  29. TEST(NextPowerOfTwo, Functionalities) {
  30. // constexpr-ness guaranteed
  31. constexpr auto next_power_of_two_of_zero = entt::next_power_of_two(0u);
  32. ASSERT_EQ(next_power_of_two_of_zero, 1u);
  33. ASSERT_EQ(entt::next_power_of_two(1u), 1u);
  34. ASSERT_EQ(entt::next_power_of_two(2u), 2u);
  35. ASSERT_EQ(entt::next_power_of_two(3u), 4u);
  36. ASSERT_EQ(entt::next_power_of_two(17u), 32u);
  37. ASSERT_EQ(entt::next_power_of_two(32u), 32u);
  38. ASSERT_EQ(entt::next_power_of_two(33u), 64u);
  39. ASSERT_EQ(entt::next_power_of_two(static_cast<std::size_t>(std::pow(2, 16))), static_cast<std::size_t>(std::pow(2, 16)));
  40. ASSERT_EQ(entt::next_power_of_two(static_cast<std::size_t>(std::pow(2, 16) + 1u)), static_cast<std::size_t>(std::pow(2, 17)));
  41. }
  42. ENTT_DEBUG_TEST(NextPowerOfTwoDeathTest, Functionalities) {
  43. ASSERT_DEATH(static_cast<void>(entt::next_power_of_two((std::size_t{1u} << (std::numeric_limits<std::size_t>::digits - 1)) + 1)), "");
  44. }
  45. TEST(FastMod, Functionalities) {
  46. // constexpr-ness guaranteed
  47. constexpr auto fast_mod_of_zero = entt::fast_mod(0u, 8u);
  48. ASSERT_EQ(fast_mod_of_zero, 0u);
  49. ASSERT_EQ(entt::fast_mod(7u, 8u), 7u);
  50. ASSERT_EQ(entt::fast_mod(8u, 8u), 0u);
  51. }