entity.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <functional>
  2. #include <type_traits>
  3. #include <gtest/gtest.h>
  4. #include <entt/entity/entity.hpp>
  5. #include <entt/entity/registry.hpp>
  6. TEST(Entity, Traits) {
  7. using traits_type = entt::entt_traits<entt::entity>;
  8. constexpr entt::entity tombstone = entt::tombstone;
  9. constexpr entt::entity null = entt::null;
  10. entt::registry registry{};
  11. registry.destroy(registry.create());
  12. const auto entity = registry.create();
  13. const auto other = registry.create();
  14. ASSERT_EQ(entt::to_integral(entity), entt::to_integral(entity));
  15. ASSERT_NE(entt::to_integral(entity), entt::to_integral<entt::entity>(entt::null));
  16. ASSERT_NE(entt::to_integral(entity), entt::to_integral(entt::entity{}));
  17. ASSERT_EQ(entt::to_entity(entity), 0u);
  18. ASSERT_EQ(entt::to_version(entity), 1u);
  19. ASSERT_EQ(entt::to_entity(other), 1u);
  20. ASSERT_EQ(entt::to_version(other), 0u);
  21. ASSERT_EQ(traits_type::construct(entt::to_entity(entity), entt::to_version(entity)), entity);
  22. ASSERT_EQ(traits_type::construct(entt::to_entity(other), entt::to_version(other)), other);
  23. ASSERT_NE(traits_type::construct(entt::to_entity(entity), {}), entity);
  24. ASSERT_EQ(traits_type::construct(entt::to_entity(other), entt::to_version(entity)), traits_type::combine(entt::to_integral(other), entt::to_integral(entity)));
  25. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
  26. ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
  27. }
  28. TEST(Entity, Null) {
  29. using traits_type = entt::entt_traits<entt::entity>;
  30. constexpr entt::entity null = entt::null;
  31. ASSERT_FALSE(entt::entity{} == entt::null);
  32. ASSERT_TRUE(entt::null == entt::null);
  33. ASSERT_FALSE(entt::null != entt::null);
  34. entt::registry registry{};
  35. const auto entity = registry.create();
  36. ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(entity)), (traits_type::construct(entt::to_entity(null), entt::to_version(entity))));
  37. ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(null)), null);
  38. ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
  39. registry.emplace<int>(entity, 42);
  40. ASSERT_FALSE(entity == entt::null);
  41. ASSERT_FALSE(entt::null == entity);
  42. ASSERT_TRUE(entity != entt::null);
  43. ASSERT_TRUE(entt::null != entity);
  44. const entt::entity other = entt::null;
  45. ASSERT_FALSE(registry.valid(other));
  46. ASSERT_NE(registry.create(other), other);
  47. }
  48. TEST(Entity, Tombstone) {
  49. using traits_type = entt::entt_traits<entt::entity>;
  50. constexpr entt::entity tombstone = entt::tombstone;
  51. ASSERT_FALSE(entt::entity{} == entt::tombstone);
  52. ASSERT_TRUE(entt::tombstone == entt::tombstone);
  53. ASSERT_FALSE(entt::tombstone != entt::tombstone);
  54. entt::registry registry{};
  55. const auto entity = registry.create();
  56. ASSERT_EQ(traits_type::combine(entt::to_integral(entity), entt::tombstone), (traits_type::construct(entt::to_entity(entity), entt::to_version(tombstone))));
  57. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::to_integral(tombstone)), tombstone);
  58. ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
  59. registry.emplace<int>(entity, 42);
  60. ASSERT_FALSE(entity == entt::tombstone);
  61. ASSERT_FALSE(entt::tombstone == entity);
  62. ASSERT_TRUE(entity != entt::tombstone);
  63. ASSERT_TRUE(entt::tombstone != entity);
  64. constexpr auto vers = entt::to_version(tombstone);
  65. const auto other = traits_type::construct(entt::to_entity(entity), vers);
  66. ASSERT_FALSE(registry.valid(entt::tombstone));
  67. ASSERT_NE(registry.destroy(entity, vers), vers);
  68. ASSERT_NE(registry.create(other), other);
  69. }