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 TEST(Entity, Traits) {
8     using traits_type = entt::entt_traits<entt::entity>;
9     entt::registry registry{};
10 
11     registry.destroy(registry.create());
12     const auto entity = registry.create();
13     const auto other = registry.create();
14 
15     ASSERT_EQ(entt::to_integral(entity), traits_type::to_integral(entity));
16     ASSERT_NE(entt::to_integral(entity), entt::to_integral<entt::entity>(entt::null));
17     ASSERT_NE(entt::to_integral(entity), entt::to_integral(entt::entity{}));
18 
19     ASSERT_EQ(traits_type::to_entity(entity), 0u);
20     ASSERT_EQ(traits_type::to_version(entity), 1u);
21     ASSERT_EQ(traits_type::to_entity(other), 1u);
22     ASSERT_EQ(traits_type::to_version(other), 0u);
23 
24     ASSERT_EQ(traits_type::construct(traits_type::to_entity(entity), traits_type::to_version(entity)), entity);
25     ASSERT_EQ(traits_type::construct(traits_type::to_entity(other), traits_type::to_version(other)), other);
26     ASSERT_NE(traits_type::construct(traits_type::to_entity(entity), {}), entity);
27 
28     ASSERT_EQ(traits_type::construct(), entt::tombstone | static_cast<entt::entity>(entt::null));
29     ASSERT_EQ(traits_type::construct(), entt::null | static_cast<entt::entity>(entt::tombstone));
30 
31     ASSERT_EQ(traits_type::construct(), static_cast<entt::entity>(entt::null));
32     ASSERT_EQ(traits_type::construct(), static_cast<entt::entity>(entt::tombstone));
33     ASSERT_EQ(traits_type::construct(), entt::entity{~entt::id_type{}});
34 }
35 
TEST(Entity,Null)36 TEST(Entity, Null) {
37     using traits_type = entt::entt_traits<entt::entity>;
38     constexpr entt::entity tombstone = entt::tombstone;
39     constexpr entt::entity null = entt::null;
40 
41     ASSERT_FALSE(entt::entity{} == entt::null);
42     ASSERT_TRUE(entt::entity{traits_type::construct()} == entt::null);
43 
44     ASSERT_TRUE(entt::null == entt::null);
45     ASSERT_FALSE(entt::null != entt::null);
46 
47     entt::registry registry{};
48     const auto entity = registry.create();
49 
50     ASSERT_EQ((entt::null | entity), (traits_type::construct(traits_type::to_entity(null), traits_type::to_version(entity))));
51     ASSERT_EQ((entt::null | null), null);
52     ASSERT_EQ((entt::null | tombstone), null);
53 
54     registry.emplace<int>(entity, 42);
55 
56     ASSERT_FALSE(entity == entt::null);
57     ASSERT_FALSE(entt::null == entity);
58 
59     ASSERT_TRUE(entity != entt::null);
60     ASSERT_TRUE(entt::null != entity);
61 
62     const entt::entity other = entt::null;
63 
64     ASSERT_FALSE(registry.valid(other));
65     ASSERT_NE(registry.create(other), other);
66 }
67 
TEST(Entity,Tombstone)68 TEST(Entity, Tombstone) {
69     using traits_type = entt::entt_traits<entt::entity>;
70     constexpr entt::entity tombstone = entt::tombstone;
71     constexpr entt::entity null = entt::null;
72 
73     ASSERT_FALSE(entt::entity{} == entt::tombstone);
74     ASSERT_TRUE(entt::entity{traits_type::construct()} == entt::tombstone);
75 
76     ASSERT_TRUE(entt::tombstone == entt::tombstone);
77     ASSERT_FALSE(entt::tombstone != entt::tombstone);
78 
79     entt::registry registry{};
80     const auto entity = registry.create();
81 
82     ASSERT_EQ((entt::tombstone | entity), (traits_type::construct(traits_type::to_entity(entity), traits_type::to_version(tombstone))));
83     ASSERT_EQ((entt::tombstone | tombstone), tombstone);
84     ASSERT_EQ((entt::tombstone | null), tombstone);
85 
86     registry.emplace<int>(entity, 42);
87 
88     ASSERT_FALSE(entity == entt::tombstone);
89     ASSERT_FALSE(entt::tombstone == entity);
90 
91     ASSERT_TRUE(entity != entt::tombstone);
92     ASSERT_TRUE(entt::tombstone != entity);
93 
94     const auto vers = traits_type::to_version(entt::tombstone);
95     const auto other = traits_type::construct(traits_type::to_entity(entity), vers);
96 
97     ASSERT_FALSE(registry.valid(entt::tombstone));
98     ASSERT_NE(registry.destroy(entity, vers), vers);
99     ASSERT_NE(registry.create(other), other);
100 }
101