-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathECSTest.cpp
More file actions
518 lines (406 loc) · 20.1 KB
/
Copy pathECSTest.cpp
File metadata and controls
518 lines (406 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <chrono>
#include <iomanip>
#include <algorithm>
#include <random>
#include <array>
#include <tuple>
#include <functional>
#include "Common.h"
#include "Systems.h"
using namespace engine;
// Cache optimized system for testing
namespace engine {
template<typename... Components>
class CacheOptimizedSystem {
public:
using UpdateFunction = std::function<void(float, size_t, Components*...)>;
CacheOptimizedSystem(EntityManager* entityManager, UpdateFunction updateFunction)
: m_EntityManager(entityManager), m_UpdateFunction(updateFunction) {}
void Update(float deltaTime) {
auto componentArrays = GetComponentArrays();
size_t minSize = GetMinArraySize(componentArrays);
if (minSize == 0) return;
auto rawArrays = GetRawArrays(componentArrays);
m_UpdateFunction(deltaTime, minSize, std::get<Components*>(rawArrays)...);
}
private:
EntityManager* m_EntityManager;
UpdateFunction m_UpdateFunction;
std::tuple<ComponentArray<Components>*...> GetComponentArrays() {
return std::make_tuple(m_EntityManager->GetComponentArray<Components>()...);
}
template<size_t... Is>
size_t GetMinArraySizeImpl(const std::tuple<ComponentArray<Components>*...>& arrays, std::index_sequence<Is...>) {
std::array<size_t, sizeof...(Components)> sizes = {std::get<Is>(arrays)->GetSize()...};
return *std::min_element(sizes.begin(), sizes.end());
}
size_t GetMinArraySize(const std::tuple<ComponentArray<Components>*...>& arrays) {
return GetMinArraySizeImpl(arrays, std::make_index_sequence<sizeof...(Components)>{});
}
std::tuple<Components*...> GetRawArrays(const std::tuple<ComponentArray<Components>*...>& arrays) {
return GetRawArraysImpl(arrays, std::make_index_sequence<sizeof...(Components)>{});
}
template<size_t... Is>
std::tuple<Components*...> GetRawArraysImpl(
const std::tuple<ComponentArray<Components>*...>& arrays, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is>(arrays)->GetArray()...);
}
};
// SIMD optimized movement system for testing
class SIMDOptimizedMovementSystem {
public:
SIMDOptimizedMovementSystem(EntityManager* entityManager) : m_EntityManager(entityManager) {}
void Update(float deltaTime) {
auto* positionArray = m_EntityManager->GetComponentArray<PositionComponent>();
auto* velocityArray = m_EntityManager->GetComponentArray<VelocityComponent>();
if (!positionArray || !velocityArray) return;
size_t size = std::min(positionArray->GetSize(), velocityArray->GetSize());
PositionComponent* positions = positionArray->GetArray();
VelocityComponent* velocities = velocityArray->GetArray();
// Simple SIMD-like optimization (manual loop unrolling)
size_t i = 0;
for (; i + 3 < size; i += 4) {
// Process 4 components at once
positions[i].x += velocities[i].vx * deltaTime;
positions[i].y += velocities[i].vy * deltaTime;
positions[i].z += velocities[i].vz * deltaTime;
velocities[i].vx *= 0.95f;
velocities[i].vy *= 0.95f;
velocities[i].vz *= 0.95f;
positions[i+1].x += velocities[i+1].vx * deltaTime;
positions[i+1].y += velocities[i+1].vy * deltaTime;
positions[i+1].z += velocities[i+1].vz * deltaTime;
velocities[i+1].vx *= 0.95f;
velocities[i+1].vy *= 0.95f;
velocities[i+1].vz *= 0.95f;
positions[i+2].x += velocities[i+2].vx * deltaTime;
positions[i+2].y += velocities[i+2].vy * deltaTime;
positions[i+2].z += velocities[i+2].vz * deltaTime;
velocities[i+2].vx *= 0.95f;
velocities[i+2].vy *= 0.95f;
velocities[i+2].vz *= 0.95f;
positions[i+3].x += velocities[i+3].vx * deltaTime;
positions[i+3].y += velocities[i+3].vy * deltaTime;
positions[i+3].z += velocities[i+3].vz * deltaTime;
velocities[i+3].vx *= 0.95f;
velocities[i+3].vy *= 0.95f;
velocities[i+3].vz *= 0.95f;
}
// Handle remaining components
for (; i < size; ++i) {
positions[i].x += velocities[i].vx * deltaTime;
positions[i].y += velocities[i].vy * deltaTime;
positions[i].z += velocities[i].vz * deltaTime;
velocities[i].vx *= 0.95f;
velocities[i].vy *= 0.95f;
velocities[i].vz *= 0.95f;
}
}
private:
EntityManager* m_EntityManager;
};
}
// ECS test class
class ECSTest : public ::testing::Test {
public:
void SetUp() override {
// Setup before each test
systemManager = std::make_unique<SystemManager>();
auto& entityManager = systemManager->GetEntityManager();
// Register all component types
entityManager.RegisterComponent<PositionComponent>();
entityManager.RegisterComponent<VelocityComponent>();
entityManager.RegisterComponent<HealthComponent>();
entityManager.RegisterComponent<PhysicsComponent>();
entityManager.RegisterComponent<AIComponent>();
entityManager.RegisterComponent<CollisionComponent>();
entityManager.RegisterComponent<InputComponent>();
entityManager.RegisterComponent<AnimationComponent>();
entityManager.RegisterComponent<TimerComponent>();
entityManager.RegisterComponent<ParticleComponent>();
entityManager.RegisterComponent<NetworkComponent>();
}
void TearDown() override {
// Cleanup after each test
systemManager.reset();
}
std::unique_ptr<SystemManager> systemManager;
};
// Basic ECS constructor test
TEST_F(ECSTest, Constructor) {
EXPECT_NO_THROW({
SystemManager sm;
});
}
// Entity creation test
TEST_F(ECSTest, CreateEntity) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity1 = entityManager.CreateEntity();
EntityID entity2 = entityManager.CreateEntity();
EXPECT_NE(entity1, entity2);
EXPECT_GT(entity2, entity1);
}
// Component addition test
TEST_F(ECSTest, AddComponent) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
PositionComponent pos{10.0f, 20.0f, 30.0f};
entityManager.AddComponent(entity, pos);
EXPECT_TRUE(entityManager.HasComponent<PositionComponent>(entity));
auto& retrievedPos = entityManager.GetComponent<PositionComponent>(entity);
EXPECT_FLOAT_EQ(retrievedPos.x, 10.0f);
EXPECT_FLOAT_EQ(retrievedPos.y, 20.0f);
EXPECT_FLOAT_EQ(retrievedPos.z, 30.0f);
}
// Component removal test
TEST_F(ECSTest, RemoveComponent) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
PositionComponent pos{10.0f, 20.0f, 30.0f};
entityManager.AddComponent(entity, pos);
EXPECT_TRUE(entityManager.HasComponent<PositionComponent>(entity));
entityManager.RemoveComponent<PositionComponent>(entity);
EXPECT_FALSE(entityManager.HasComponent<PositionComponent>(entity));
}
// Entity destruction test
TEST_F(ECSTest, DestroyEntity) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
PositionComponent pos{10.0f, 20.0f, 30.0f};
VelocityComponent vel{1.0f, 2.0f, 3.0f};
entityManager.AddComponent(entity, pos);
entityManager.AddComponent(entity, vel);
EXPECT_TRUE(entityManager.HasComponent<PositionComponent>(entity));
EXPECT_TRUE(entityManager.HasComponent<VelocityComponent>(entity));
entityManager.DestroyEntity(entity);
EXPECT_FALSE(entityManager.HasComponent<PositionComponent>(entity));
EXPECT_FALSE(entityManager.HasComponent<VelocityComponent>(entity));
}
// Component array access test
TEST_F(ECSTest, ComponentArrayAccess) {
auto& entityManager = systemManager->GetEntityManager();
// Create multiple entities
std::vector<EntityID> entities;
for (int i = 0; i < 5; ++i) {
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, PositionComponent{static_cast<float>(i), 0.0f, 0.0f});
entityManager.AddComponent(entity, VelocityComponent{static_cast<float>(i), 0.0f, 0.0f});
entities.push_back(entity);
}
auto* positionArray = entityManager.GetComponentArray<PositionComponent>();
auto* velocityArray = entityManager.GetComponentArray<VelocityComponent>();
EXPECT_EQ(positionArray->GetSize(), 5);
EXPECT_EQ(velocityArray->GetSize(), 5);
// Direct array access test
PositionComponent* positions = positionArray->GetArray();
VelocityComponent* velocities = velocityArray->GetArray();
for (size_t i = 0; i < 5; ++i) {
EXPECT_FLOAT_EQ(positions[i].x, static_cast<float>(i));
EXPECT_FLOAT_EQ(velocities[i].vx, static_cast<float>(i));
}
}
// Memory layout optimization verification test
TEST_F(ECSTest, MemoryLayoutOptimization) {
auto& entityManager = systemManager->GetEntityManager();
const int entityCount = 10000;
std::cout << "\n=== Memory Layout Analysis ===" << std::endl;
// Component size information
std::cout << "Component sizes:" << std::endl;
std::cout << " PositionComponent: " << sizeof(PositionComponent) << " bytes" << std::endl;
std::cout << " VelocityComponent: " << sizeof(VelocityComponent) << " bytes" << std::endl;
std::cout << " HealthComponent: " << sizeof(HealthComponent) << " bytes" << std::endl;
// Cache line size (typically 64 bytes)
const size_t cacheLineSize = 64;
size_t positionsPerCacheLine = cacheLineSize / sizeof(PositionComponent);
size_t velocitiesPerCacheLine = cacheLineSize / sizeof(VelocityComponent);
std::cout << "Cache line optimization:" << std::endl;
std::cout << " Cache line size: " << cacheLineSize << " bytes" << std::endl;
std::cout << " Positions per cache line: " << positionsPerCacheLine << std::endl;
std::cout << " Velocities per cache line: " << velocitiesPerCacheLine << std::endl;
// Create entities and analyze memory addresses
std::vector<EntityID> entities;
for (int i = 0; i < entityCount; ++i) {
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, PositionComponent{0.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity, VelocityComponent{0.0f, 0.0f, 0.0f});
entities.push_back(entity);
}
auto* positionArray = entityManager.GetComponentArray<PositionComponent>();
auto* velocityArray = entityManager.GetComponentArray<VelocityComponent>();
PositionComponent* positions = positionArray->GetArray();
VelocityComponent* velocities = velocityArray->GetArray();
// Memory contiguity verification
bool isPositionContiguous = true;
bool isVelocityContiguous = true;
for (size_t i = 1; i < entityCount && i < 100; ++i) {
ptrdiff_t positionDiff = reinterpret_cast<char*>(&positions[i]) - reinterpret_cast<char*>(&positions[i-1]);
ptrdiff_t velocityDiff = reinterpret_cast<char*>(&velocities[i]) - reinterpret_cast<char*>(&velocities[i-1]);
if (positionDiff != sizeof(PositionComponent)) {
isPositionContiguous = false;
}
if (velocityDiff != sizeof(VelocityComponent)) {
isVelocityContiguous = false;
}
}
std::cout << "Memory contiguity:" << std::endl;
std::cout << " Position components contiguous: " << (isPositionContiguous ? "Yes" : "No") << std::endl;
std::cout << " Velocity components contiguous: " << (isVelocityContiguous ? "Yes" : "No") << std::endl;
// Memory alignment verification
uintptr_t positionAlignment = reinterpret_cast<uintptr_t>(positions) % alignof(PositionComponent);
uintptr_t velocityAlignment = reinterpret_cast<uintptr_t>(velocities) % alignof(VelocityComponent);
std::cout << "Memory alignment:" << std::endl;
std::cout << " Position array aligned: " << (positionAlignment == 0 ? "Yes" : "No") << std::endl;
std::cout << " Velocity array aligned: " << (velocityAlignment == 0 ? "Yes" : "No") << std::endl;
// Memory usage
size_t totalMemory = entityCount * (sizeof(PositionComponent) + sizeof(VelocityComponent));
size_t cacheLines = (totalMemory + cacheLineSize - 1) / cacheLineSize;
std::cout << "Memory usage:" << std::endl;
std::cout << " Total memory: " << totalMemory << " bytes (" << (totalMemory / 1024) << " KB)" << std::endl;
std::cout << " Cache lines used: " << cacheLines << std::endl;
// Verification
EXPECT_TRUE(isPositionContiguous);
EXPECT_TRUE(isVelocityContiguous);
EXPECT_EQ(positionAlignment, 0);
EXPECT_EQ(velocityAlignment, 0);
// Cleanup
for (auto entity : entities) {
entityManager.DestroyEntity(entity);
}
}
// System registration and execution test
TEST_F(ECSTest, SystemRegistrationAndExecution) {
auto& entityManager = systemManager->GetEntityManager();
// Create test entities
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, PositionComponent{0.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity, VelocityComponent{1.0f, 2.0f, 3.0f});
// Register system
bool systemExecuted = false;
systemManager->RegisterSystem<PositionComponent, VelocityComponent>(
[&systemExecuted](float deltaTime, PositionComponent& position, VelocityComponent& velocity) {
systemExecuted = true;
// Simple movement update
position.x += velocity.vx * deltaTime;
position.y += velocity.vy * deltaTime;
position.z += velocity.vz * deltaTime;
});
// Execute system
systemManager->Update(1.0f);
EXPECT_TRUE(systemExecuted);
// Check if position was updated
auto& position = entityManager.GetComponent<PositionComponent>(entity);
EXPECT_FLOAT_EQ(position.x, 1.0f);
EXPECT_FLOAT_EQ(position.y, 2.0f);
EXPECT_FLOAT_EQ(position.z, 3.0f);
}
// MovementSystem test
TEST_F(ECSTest, MovementSystem) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, PositionComponent{0.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity, VelocityComponent{5.0f, 10.0f, 15.0f});
systemManager->RegisterSystem<PositionComponent, VelocityComponent>(
[](float deltaTime, PositionComponent& position, VelocityComponent& velocity) {
MovementSystem::Update(deltaTime, position, velocity);
});
systemManager->Update(1.0f);
auto& position = entityManager.GetComponent<PositionComponent>(entity);
auto& velocity = entityManager.GetComponent<VelocityComponent>(entity);
// Check position update
EXPECT_FLOAT_EQ(position.x, 5.0f);
EXPECT_FLOAT_EQ(position.y, 10.0f);
EXPECT_FLOAT_EQ(position.z, 15.0f);
// Check friction application
EXPECT_FLOAT_EQ(velocity.vx, 5.0f * 0.95f);
EXPECT_FLOAT_EQ(velocity.vy, 10.0f * 0.95f);
EXPECT_FLOAT_EQ(velocity.vz, 15.0f * 0.95f);
}
// PhysicsSystem test
TEST_F(ECSTest, PhysicsSystem) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, PositionComponent{0.0f, 10.0f, 0.0f});
entityManager.AddComponent(entity, VelocityComponent{0.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity, PhysicsComponent{1.0f, 0.1f, 0.5f, false, true});
systemManager->RegisterSystem<PositionComponent, VelocityComponent, PhysicsComponent>(
[](float deltaTime, PositionComponent& position, VelocityComponent& velocity, PhysicsComponent& physics) {
PhysicsSystem::Update(deltaTime, position, velocity, physics);
});
// Test gravity application
systemManager->Update(1.0f);
auto& position = entityManager.GetComponent<PositionComponent>(entity);
auto& velocity = entityManager.GetComponent<VelocityComponent>(entity);
// Check gravity application (g = -9.81)
EXPECT_FLOAT_EQ(velocity.vy, -9.81f);
EXPECT_FLOAT_EQ(position.y, 10.0f - 9.81f);
}
// HealthSystem test
TEST_F(ECSTest, HealthSystem) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, HealthComponent{50.0f, 100.0f, true});
systemManager->RegisterSystem<HealthComponent>(
[](float deltaTime, HealthComponent& health) {
HealthSystem::Update(deltaTime, health);
});
systemManager->Update(1.0f);
auto& health = entityManager.GetComponent<HealthComponent>(entity);
// Check health regeneration (5 HP per second)
EXPECT_FLOAT_EQ(health.currentHealth, 55.0f);
EXPECT_TRUE(health.isAlive);
}
// AISystem test
TEST_F(ECSTest, AISystem) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, AIComponent{0, 0.0f, 1.0f});
systemManager->RegisterSystem<AIComponent>(
[](float deltaTime, AIComponent& ai) {
AISystem::Update(deltaTime, ai);
});
auto& ai = entityManager.GetComponent<AIComponent>(entity);
uint32_t initialState = ai.aiState;
systemManager->Update(1.0f);
// Check if AI state changed
EXPECT_NE(ai.aiState, initialState);
EXPECT_FLOAT_EQ(ai.aiTimer, 0.0f);
}
// CollisionSystem test
TEST_F(ECSTest, CollisionSystem) {
auto& entityManager = systemManager->GetEntityManager();
EntityID entity = entityManager.CreateEntity();
entityManager.AddComponent(entity, PositionComponent{0.0f, -5.0f, 0.0f});
entityManager.AddComponent(entity, CollisionComponent{2.0f, true, 1});
systemManager->RegisterSystem<PositionComponent, CollisionComponent>(
[](float deltaTime, PositionComponent& position, CollisionComponent& collision) {
CollisionSystem::Update(deltaTime, position, collision);
});
systemManager->Update(1.0f);
auto& position = entityManager.GetComponent<PositionComponent>(entity);
// Check ground collision handling
EXPECT_FLOAT_EQ(position.y, 2.0f); // Adjusted to radius value
}
// Component combination query test
TEST_F(ECSTest, ComponentQuery) {
auto& entityManager = systemManager->GetEntityManager();
// Create entities with different component combinations
EntityID entity1 = entityManager.CreateEntity();
entityManager.AddComponent(entity1, PositionComponent{0.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity1, VelocityComponent{1.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity1, HealthComponent{100.0f, 100.0f, true});
EntityID entity2 = entityManager.CreateEntity();
entityManager.AddComponent(entity2, PositionComponent{10.0f, 0.0f, 0.0f});
entityManager.AddComponent(entity2, VelocityComponent{0.0f, 1.0f, 0.0f});
EntityID entity3 = entityManager.CreateEntity();
entityManager.AddComponent(entity3, HealthComponent{50.0f, 100.0f, true});
// Query entities with both Position and Velocity
auto movingEntities = entityManager.GetEntitiesWithComponents<PositionComponent, VelocityComponent>();
EXPECT_EQ(movingEntities.size(), 2);
// Query entities with Position, Velocity, and Health
auto fullEntities = entityManager.GetEntitiesWithComponents<PositionComponent, VelocityComponent, HealthComponent>();
EXPECT_EQ(fullEntities.size(), 1);
EXPECT_EQ(fullEntities[0], entity1);
}