entity.cpp 6.2 KB

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