entity.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <gtest/gtest.h>
  2. #include <entt/entity/entity.hpp>
  3. #include <entt/entity/registry.hpp>
  4. #include "../common/custom_entity.h"
  5. struct custom_entity_traits {
  6. using value_type = test::custom_entity;
  7. using entity_type = std::uint32_t;
  8. using version_type = std::uint16_t;
  9. static constexpr entity_type entity_mask = 0x3FFFF; // 18b
  10. static constexpr entity_type version_mask = 0x3FFF; // 14b
  11. };
  12. template<>
  13. struct entt::entt_traits<test::custom_entity>: entt::basic_entt_traits<custom_entity_traits> {
  14. static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
  15. };
  16. template<typename Type>
  17. struct Entity: testing::Test {
  18. using type = Type;
  19. };
  20. using EntityTypes = ::testing::Types<entt::entity, test::custom_entity>;
  21. TYPED_TEST_SUITE(Entity, EntityTypes, );
  22. TYPED_TEST(Entity, Traits) {
  23. using entity_type = typename TestFixture::type;
  24. using traits_type = entt::entt_traits<entity_type>;
  25. constexpr entity_type tombstone = entt::tombstone;
  26. constexpr entity_type null = entt::null;
  27. entt::basic_registry<entity_type> registry{};
  28. registry.destroy(registry.create());
  29. const auto entity = registry.create();
  30. const auto other = registry.create();
  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), 0u);
  35. ASSERT_EQ(entt::to_version(entity), 1u);
  36. ASSERT_EQ(entt::to_entity(other), 1u);
  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. }
  49. TYPED_TEST(Entity, Null) {
  50. using entity_type = typename TestFixture::type;
  51. using traits_type = entt::entt_traits<entity_type>;
  52. constexpr entity_type null = entt::null;
  53. ASSERT_FALSE(entity_type{} == entt::null);
  54. ASSERT_TRUE(entt::null == entt::null);
  55. ASSERT_FALSE(entt::null != entt::null);
  56. entt::basic_registry<entity_type> registry{};
  57. const auto entity = registry.create();
  58. ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(entity)), (traits_type::construct(entt::to_entity(null), entt::to_version(entity))));
  59. ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(null)), null);
  60. ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
  61. registry.emplace<int>(entity, 42);
  62. ASSERT_FALSE(entity == entt::null);
  63. ASSERT_FALSE(entt::null == entity);
  64. ASSERT_TRUE(entity != entt::null);
  65. ASSERT_TRUE(entt::null != entity);
  66. const entity_type other = entt::null;
  67. ASSERT_FALSE(registry.valid(other));
  68. ASSERT_NE(registry.create(other), other);
  69. }
  70. TYPED_TEST(Entity, Tombstone) {
  71. using entity_type = typename TestFixture::type;
  72. using traits_type = entt::entt_traits<entity_type>;
  73. constexpr entity_type tombstone = entt::tombstone;
  74. ASSERT_FALSE(entity_type{} == entt::tombstone);
  75. ASSERT_TRUE(entt::tombstone == entt::tombstone);
  76. ASSERT_FALSE(entt::tombstone != entt::tombstone);
  77. entt::basic_registry<entity_type> registry{};
  78. const auto entity = registry.create();
  79. ASSERT_EQ(traits_type::combine(entt::to_integral(entity), entt::tombstone), (traits_type::construct(entt::to_entity(entity), entt::to_version(tombstone))));
  80. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::to_integral(tombstone)), tombstone);
  81. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
  82. registry.emplace<int>(entity, 42);
  83. ASSERT_FALSE(entity == entt::tombstone);
  84. ASSERT_FALSE(entt::tombstone == entity);
  85. ASSERT_TRUE(entity != entt::tombstone);
  86. ASSERT_TRUE(entt::tombstone != entity);
  87. constexpr auto vers = entt::to_version(tombstone);
  88. const auto other = traits_type::construct(entt::to_entity(entity), vers);
  89. ASSERT_FALSE(registry.valid(entt::tombstone));
  90. ASSERT_NE(registry.destroy(entity, vers), vers);
  91. ASSERT_NE(registry.create(other), other);
  92. }