Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <ECS/ECS.hpp> | ||
2 | #include <iostream> | ||
3 | |||
4 | // Test components | ||
5 | class TestPosition : public ECS::Component<TestPosition> { | ||
6 | public: | ||
7 | float x, y; | ||
8 | 6 | TestPosition(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} | |
9 | }; | ||
10 | |||
11 | class TestVelocity : public ECS::Component<TestVelocity> { | ||
12 | public: | ||
13 | float vx, vy; | ||
14 | 2 | TestVelocity(float vx = 0.0f, float vy = 0.0f) : vx(vx), vy(vy) {} | |
15 | }; | ||
16 | |||
17 | class TestHealth : public ECS::Component<TestHealth> { | ||
18 | public: | ||
19 | int hp; | ||
20 | 2 | TestHealth(int hp = 100) : hp(hp) {} | |
21 | }; | ||
22 | |||
23 | 1 | int main() { | |
24 | 1 | std::cout << "Running ECS Library Tests..." << std::endl; | |
25 | |||
26 | 1 | bool allTestsPassed = true; | |
27 | |||
28 | // Test 1: Entity creation and management | ||
29 | { | ||
30 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ECS::World world; |
31 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity1 = world.CreateEntity(); |
32 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity2 = world.CreateEntity(); |
33 | |||
34 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (entity1 == ECS::INVALID_ENTITY || entity2 == ECS::INVALID_ENTITY) { |
35 | ✗ | std::cout << "FAIL: Entity creation test" << std::endl; | |
36 | ✗ | allTestsPassed = false; | |
37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } else if (entity1 == entity2) { |
38 | ✗ | std::cout << "FAIL: Entity uniqueness test" << std::endl; | |
39 | ✗ | allTestsPassed = false; | |
40 | } else { | ||
41 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Entity creation and uniqueness" << std::endl; |
42 | } | ||
43 | |||
44 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (world.GetAliveEntityCount() != 2) { |
45 | ✗ | std::cout << "FAIL: Entity count test" << std::endl; | |
46 | ✗ | allTestsPassed = false; | |
47 | } else { | ||
48 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Entity count" << std::endl; |
49 | } | ||
50 | 1 | } | |
51 | |||
52 | // Test 2: Component addition and retrieval | ||
53 | { | ||
54 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ECS::World world; |
55 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity = world.CreateEntity(); |
56 | |||
57 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto* pos = world.AddComponent<TestPosition>(entity, 10.0f, 20.0f); |
58 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto* vel = world.AddComponent<TestVelocity>(entity, 1.0f, 2.0f); |
59 | |||
60 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (!pos || pos->x != 10.0f || pos->y != 20.0f) { |
61 | ✗ | std::cout << "FAIL: Position component test" << std::endl; | |
62 | ✗ | allTestsPassed = false; | |
63 | } else { | ||
64 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Position component addition and retrieval" << std::endl; |
65 | } | ||
66 | |||
67 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (!vel || vel->vx != 1.0f || vel->vy != 2.0f) { |
68 | ✗ | std::cout << "FAIL: Velocity component test" << std::endl; | |
69 | ✗ | allTestsPassed = false; | |
70 | } else { | ||
71 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Velocity component addition and retrieval" << std::endl; |
72 | } | ||
73 | 1 | } | |
74 | |||
75 | // Test 3: Component queries (HasComponent) | ||
76 | { | ||
77 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ECS::World world; |
78 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity = world.CreateEntity(); |
79 | |||
80 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestPosition>(entity, 5.0f, 15.0f); |
81 | |||
82 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (!world.HasComponent<TestPosition>(entity)) { |
83 | ✗ | std::cout << "FAIL: HasComponent positive test" << std::endl; | |
84 | ✗ | allTestsPassed = false; | |
85 | } else { | ||
86 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: HasComponent positive test" << std::endl; |
87 | } | ||
88 | |||
89 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (world.HasComponent<TestVelocity>(entity)) { |
90 | ✗ | std::cout << "FAIL: HasComponent negative test" << std::endl; | |
91 | ✗ | allTestsPassed = false; | |
92 | } else { | ||
93 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: HasComponent negative test" << std::endl; |
94 | } | ||
95 | 1 | } | |
96 | |||
97 | // Test 4: Component removal | ||
98 | { | ||
99 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ECS::World world; |
100 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity = world.CreateEntity(); |
101 | |||
102 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestPosition>(entity, 0.0f, 0.0f); |
103 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestHealth>(entity, 50); |
104 | |||
105 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.RemoveComponent<TestPosition>(entity); |
106 | |||
107 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (world.HasComponent<TestPosition>(entity)) { |
108 | ✗ | std::cout << "FAIL: Component removal test" << std::endl; | |
109 | ✗ | allTestsPassed = false; | |
110 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | } else if (!world.HasComponent<TestHealth>(entity)) { |
111 | ✗ | std::cout << "FAIL: Component removal affecting other components" << std::endl; | |
112 | ✗ | allTestsPassed = false; | |
113 | } else { | ||
114 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Component removal" << std::endl; |
115 | } | ||
116 | 1 | } | |
117 | |||
118 | // Test 5: Entity destruction | ||
119 | { | ||
120 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ECS::World world; |
121 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity1 = world.CreateEntity(); |
122 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity2 = world.CreateEntity(); |
123 | |||
124 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestPosition>(entity1, 1.0f, 2.0f); |
125 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestPosition>(entity2, 3.0f, 4.0f); |
126 | |||
127 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.DestroyEntity(entity1); |
128 | |||
129 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (world.IsEntityAlive(entity1)) { |
130 | ✗ | std::cout << "FAIL: Entity destruction test" << std::endl; | |
131 | ✗ | allTestsPassed = false; | |
132 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | } else if (!world.IsEntityAlive(entity2)) { |
133 | ✗ | std::cout << "FAIL: Entity destruction affecting other entities" << std::endl; | |
134 | ✗ | allTestsPassed = false; | |
135 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | } else if (world.HasComponent<TestPosition>(entity1)) { |
136 | ✗ | std::cout << "FAIL: Component cleanup on entity destruction" << std::endl; | |
137 | ✗ | allTestsPassed = false; | |
138 | } else { | ||
139 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Entity destruction and component cleanup" << std::endl; |
140 | } | ||
141 | 1 | } | |
142 | |||
143 | // Test 6: Multiple component types on single entity | ||
144 | { | ||
145 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ECS::World world; |
146 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto entity = world.CreateEntity(); |
147 | |||
148 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestPosition>(entity, 100.0f, 200.0f); |
149 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestVelocity>(entity, 10.0f, 20.0f); |
150 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | world.AddComponent<TestHealth>(entity, 75); |
151 | |||
152 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto* pos = world.GetComponent<TestPosition>(entity); |
153 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto* vel = world.GetComponent<TestVelocity>(entity); |
154 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | auto* health = world.GetComponent<TestHealth>(entity); |
155 | |||
156 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (!pos || !vel || !health) { |
157 | ✗ | std::cout << "FAIL: Multiple components on single entity" << std::endl; | |
158 | ✗ | allTestsPassed = false; | |
159 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | } else if (pos->x != 100.0f || vel->vx != 10.0f || health->hp != 75) { |
160 | ✗ | std::cout << "FAIL: Multiple component values" << std::endl; | |
161 | ✗ | allTestsPassed = false; | |
162 | } else { | ||
163 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << "PASS: Multiple components on single entity" << std::endl; |
164 | } | ||
165 | 1 | } | |
166 | |||
167 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (allTestsPassed) { |
168 | 1 | std::cout << "\nAll ECS tests passed!" << std::endl; | |
169 | 1 | return 0; | |
170 | } else { | ||
171 | ✗ | std::cout << "\nSome ECS tests failed!" << std::endl; | |
172 | ✗ | return 1; | |
173 | } | ||
174 | } | ||
175 |