memory.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <cmath>
  2. #include <cstddef>
  3. #include <limits>
  4. #include <memory>
  5. #include <type_traits>
  6. #include <utility>
  7. #include <gtest/gtest.h>
  8. #include <entt/core/memory.hpp>
  9. #include "../common/basic_test_allocator.hpp"
  10. #include "../common/throwing_allocator.hpp"
  11. TEST(Memory, ToAddress) {
  12. std::shared_ptr<int> shared = std::make_shared<int>();
  13. auto *plain = std::addressof(*shared);
  14. ASSERT_EQ(entt::to_address(shared), plain);
  15. ASSERT_EQ(entt::to_address(plain), plain);
  16. }
  17. TEST(Memory, PoccaPocmaAndPocs) {
  18. test::basic_test_allocator<int> lhs, rhs;
  19. // honestly, I don't even know how one is supposed to test such a thing :)
  20. entt::propagate_on_container_copy_assignment(lhs, rhs);
  21. entt::propagate_on_container_move_assignment(lhs, rhs);
  22. entt::propagate_on_container_swap(lhs, rhs);
  23. }
  24. TEST(Memory, IsPowerOfTwo) {
  25. // constexpr-ness guaranteed
  26. constexpr auto zero_is_power_of_two = entt::is_power_of_two(0u);
  27. ASSERT_FALSE(zero_is_power_of_two);
  28. ASSERT_TRUE(entt::is_power_of_two(1u));
  29. ASSERT_TRUE(entt::is_power_of_two(2u));
  30. ASSERT_TRUE(entt::is_power_of_two(4u));
  31. ASSERT_FALSE(entt::is_power_of_two(7u));
  32. ASSERT_TRUE(entt::is_power_of_two(128u));
  33. ASSERT_FALSE(entt::is_power_of_two(200u));
  34. }
  35. TEST(Memory, NextPowerOfTwo) {
  36. // constexpr-ness guaranteed
  37. constexpr auto next_power_of_two_of_zero = entt::next_power_of_two(0u);
  38. ASSERT_EQ(next_power_of_two_of_zero, 1u);
  39. ASSERT_EQ(entt::next_power_of_two(1u), 1u);
  40. ASSERT_EQ(entt::next_power_of_two(2u), 2u);
  41. ASSERT_EQ(entt::next_power_of_two(3u), 4u);
  42. ASSERT_EQ(entt::next_power_of_two(17u), 32u);
  43. ASSERT_EQ(entt::next_power_of_two(32u), 32u);
  44. ASSERT_EQ(entt::next_power_of_two(33u), 64u);
  45. ASSERT_EQ(entt::next_power_of_two(std::pow(2, 16)), std::pow(2, 16));
  46. ASSERT_EQ(entt::next_power_of_two(std::pow(2, 16) + 1u), std::pow(2, 17));
  47. }
  48. TEST(MemoryDeathTest, NextPowerOfTwo) {
  49. ASSERT_DEATH(static_cast<void>(entt::next_power_of_two((std::size_t{1u} << (std::numeric_limits<std::size_t>::digits - 1)) + 1)), "");
  50. }
  51. TEST(Memory, FastMod) {
  52. // constexpr-ness guaranteed
  53. constexpr auto fast_mod_of_zero = entt::fast_mod(0u, 8u);
  54. ASSERT_EQ(fast_mod_of_zero, 0u);
  55. ASSERT_EQ(entt::fast_mod(7u, 8u), 7u);
  56. ASSERT_EQ(entt::fast_mod(8u, 8u), 0u);
  57. }
  58. TEST(Memory, AllocateUnique) {
  59. test::throwing_allocator<double> allocator{};
  60. test::throwing_allocator<int>::trigger_on_allocate = true;
  61. ASSERT_THROW((entt::allocate_unique<int>(allocator, 0)), test::throwing_allocator<int>::exception_type);
  62. std::unique_ptr<int, entt::allocation_deleter<test::throwing_allocator<int>>> ptr = entt::allocate_unique<int>(allocator, 42);
  63. ASSERT_TRUE(ptr);
  64. ASSERT_EQ(*ptr, 42);
  65. ptr.reset();
  66. ASSERT_FALSE(ptr);
  67. }