entity.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <cstddef>
  2. #include <cstdint>
  3. #include <gtest/gtest.h>
  4. #include "common/entity.h"
  5. #include <entt/config/config.h>
  6. #include <entt/entity/entity.hpp>
  7. struct entity_traits {
  8. using value_type = test::entity;
  9. using entity_type = std::uint32_t;
  10. using version_type = std::uint16_t;
  11. static constexpr entity_type entity_mask = 0x3FFFF; // 18b
  12. static constexpr entity_type version_mask = 0x0FFF; // 12b
  13. };
  14. struct other_entity_traits {
  15. using value_type = test::other_entity;
  16. using entity_type = std::uint32_t;
  17. using version_type = std::uint16_t;
  18. static constexpr entity_type entity_mask = 0xFFFFFFFF; // 32b
  19. static constexpr entity_type version_mask = 0x00; // 0b
  20. };
  21. template<>
  22. struct entt::entt_traits<test::entity>: entt::basic_entt_traits<entity_traits> {
  23. static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
  24. };
  25. template<>
  26. struct entt::entt_traits<test::other_entity>: entt::basic_entt_traits<other_entity_traits> {
  27. static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
  28. };
  29. template<typename Type>
  30. struct Entity: testing::Test {
  31. using type = Type;
  32. };
  33. using EntityTypes = ::testing::Types<entt::entity, test::entity, test::other_entity>;
  34. TYPED_TEST_SUITE(Entity, EntityTypes, );
  35. TYPED_TEST(Entity, Traits) {
  36. using entity_type = typename TestFixture::type;
  37. using traits_type = entt::entt_traits<entity_type>;
  38. constexpr entity_type tombstone{entt::tombstone};
  39. constexpr entity_type null{entt::null};
  40. const entity_type entity = traits_type::construct(4u, 1u);
  41. const entity_type other = traits_type::construct(3u, 0u);
  42. ASSERT_EQ(entt::to_integral(entity), entt::to_integral(entity));
  43. ASSERT_NE(entt::to_integral(entity), entt::to_integral<entity_type>(entt::null));
  44. ASSERT_NE(entt::to_integral(entity), entt::to_integral(entity_type{}));
  45. ASSERT_EQ(entt::to_entity(entity), 4u);
  46. ASSERT_EQ(entt::to_version(entity), !!traits_type::version_mask);
  47. ASSERT_EQ(entt::to_entity(other), 3u);
  48. ASSERT_EQ(entt::to_version(other), 0u);
  49. ASSERT_EQ(traits_type::construct(entt::to_entity(entity), entt::to_version(entity)), entity);
  50. ASSERT_EQ(traits_type::construct(entt::to_entity(other), entt::to_version(other)), other);
  51. if constexpr(traits_type::version_mask == 0u) {
  52. ASSERT_EQ(traits_type::construct(entt::to_entity(entity), entt::to_version(other)), entity);
  53. } else {
  54. ASSERT_NE(traits_type::construct(entt::to_entity(entity), entt::to_version(other)), entity);
  55. }
  56. ASSERT_EQ(traits_type::construct(entt::to_entity(other), entt::to_version(entity)), traits_type::combine(entt::to_integral(other), entt::to_integral(entity)));
  57. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
  58. ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
  59. ASSERT_EQ(traits_type::next(entity), traits_type::construct(entt::to_integral(entity), entt::to_version(entity) + 1u));
  60. ASSERT_EQ(traits_type::next(other), traits_type::construct(entt::to_integral(other), entt::to_version(other) + 1u));
  61. ASSERT_EQ(traits_type::next(entt::tombstone), traits_type::construct(entt::null, {}));
  62. ASSERT_EQ(traits_type::next(entt::null), traits_type::construct(entt::null, {}));
  63. if constexpr(traits_type::to_integral(tombstone) != ~typename traits_type::entity_type{}) {
  64. // test reserved bits, if any
  65. constexpr entity_type reserved{traits_type::to_integral(entity) | (traits_type::to_integral(tombstone) + 1u)};
  66. ASSERT_NE(reserved, entity);
  67. ASSERT_NE(traits_type::to_integral(null), ~typename traits_type::entity_type{});
  68. ASSERT_NE(traits_type::to_integral(tombstone), ~typename traits_type::entity_type{});
  69. ASSERT_EQ(traits_type::to_entity(reserved), traits_type::to_entity(entity));
  70. ASSERT_EQ(traits_type::to_version(reserved), traits_type::to_version(entity));
  71. ASSERT_EQ(traits_type::to_version(null), traits_type::version_mask);
  72. ASSERT_EQ(traits_type::to_version(tombstone), traits_type::version_mask);
  73. ASSERT_EQ(traits_type::to_version(traits_type::next(null)), 0u);
  74. ASSERT_EQ(traits_type::to_version(traits_type::next(tombstone)), 0u);
  75. ASSERT_EQ(traits_type::construct(traits_type::to_integral(entity), traits_type::version_mask + 1u), entity_type{traits_type::to_entity(entity)});
  76. ASSERT_EQ(traits_type::construct(traits_type::to_integral(null), traits_type::to_version(null) + 1u), entity_type{traits_type::to_entity(null)});
  77. ASSERT_EQ(traits_type::construct(traits_type::to_integral(tombstone), traits_type::to_version(tombstone) + 1u), entity_type{traits_type::to_entity(tombstone)});
  78. ASSERT_EQ(traits_type::next(reserved), traits_type::next(entity));
  79. ASSERT_EQ(traits_type::next(null), traits_type::combine(null, entity_type{}));
  80. ASSERT_EQ(traits_type::next(tombstone), traits_type::combine(tombstone, entity_type{}));
  81. ASSERT_EQ(traits_type::combine(entity, reserved), entity);
  82. ASSERT_NE(traits_type::combine(entity, reserved), reserved);
  83. ASSERT_EQ(traits_type::combine(reserved, entity), entity);
  84. ASSERT_NE(traits_type::combine(reserved, entity), reserved);
  85. }
  86. }
  87. TYPED_TEST(Entity, Null) {
  88. using entity_type = typename TestFixture::type;
  89. using traits_type = entt::entt_traits<entity_type>;
  90. constexpr entity_type null{entt::null};
  91. ASSERT_FALSE(entity_type{} == entt::null);
  92. ASSERT_TRUE(entt::null == entt::null);
  93. ASSERT_FALSE(entt::null != entt::null);
  94. const entity_type entity{4u};
  95. ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(entity)), (traits_type::construct(entt::to_entity(null), entt::to_version(entity))));
  96. ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(null)), null);
  97. ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
  98. ASSERT_FALSE(entity == entt::null);
  99. ASSERT_FALSE(entt::null == entity);
  100. ASSERT_TRUE(entity != entt::null);
  101. ASSERT_TRUE(entt::null != entity);
  102. }
  103. TYPED_TEST(Entity, Tombstone) {
  104. using entity_type = typename TestFixture::type;
  105. using traits_type = entt::entt_traits<entity_type>;
  106. constexpr entity_type tombstone{entt::tombstone};
  107. ASSERT_FALSE(entity_type{} == entt::tombstone);
  108. ASSERT_TRUE(entt::tombstone == entt::tombstone);
  109. ASSERT_FALSE(entt::tombstone != entt::tombstone);
  110. const entity_type entity{4u};
  111. ASSERT_EQ(traits_type::combine(entt::to_integral(entity), entt::tombstone), (traits_type::construct(entt::to_entity(entity), entt::to_version(tombstone))));
  112. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::to_integral(tombstone)), tombstone);
  113. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
  114. ASSERT_FALSE(entity == entt::tombstone);
  115. ASSERT_FALSE(entt::tombstone == entity);
  116. ASSERT_TRUE(entity != entt::tombstone);
  117. ASSERT_TRUE(entt::tombstone != entity);
  118. }