|
1 | | -/*Copyright (c) 2024 The DarkMatter Project |
2 | | -Licensed under the GNU General Public License 2.0. |
3 | | -
|
4 | | -test_g_clients_lifecycle.cpp implementation.*/ |
5 | | - |
6 | | -#include "../src/server/gameplay/g_clients.hpp" |
7 | | - |
8 | | -#include <cassert> |
9 | | -#include <new> |
10 | | -#include <type_traits> |
11 | | - |
12 | | -local_game_import_t gi{}; |
13 | | -game_export_t globals{}; |
14 | | -GameLocals game{}; |
| 1 | +#include "../src/server/gameplay/g_clients.cpp" |
| 2 | +#include <gtest/gtest.h> |
| 3 | + |
| 4 | +// Mocks |
| 5 | +local_game_import_t gi; |
| 6 | +GameLocals game; |
| 7 | +game_export_t globals; |
| 8 | +gentity_t* g_entities = nullptr; |
| 9 | + |
| 10 | +// Menu class implementation stubs for g_local.hpp |
| 11 | +void Menu::Next() {} |
| 12 | +void Menu::Prev() {} |
| 13 | +void Menu::Select(gentity_t* ent) {} |
| 14 | +void Menu::Render(gentity_t* ent) const {} |
| 15 | +void Menu::EnsureCurrentVisible() {} |
15 | 16 |
|
16 | 17 | namespace { |
17 | | -void* TestTagMalloc(size_t size, int /*tag*/) { |
18 | | - return ::operator new(size); |
19 | | -} |
20 | | - |
21 | | -void TestTagFree(void* ptr) { |
22 | | - ::operator delete(ptr); |
23 | | -} |
| 18 | + // Mock malloc/free |
| 19 | + void* MockTagMalloc(size_t size, int tag) { |
| 20 | + return malloc(size); |
| 21 | + } |
| 22 | + |
| 23 | + void MockTagFree(void* ptr) { |
| 24 | + free(ptr); |
| 25 | + } |
| 26 | + |
| 27 | + void MockComError(const char* msg) { |
| 28 | + fprintf(stderr, "Com_Error: %s\n", msg); |
| 29 | + exit(1); |
| 30 | + } |
24 | 31 | } |
25 | 32 |
|
26 | | -int main() { |
27 | | - gi.TagMalloc = TestTagMalloc; |
28 | | - gi.TagFree = TestTagFree; |
29 | | - gi.Com_Error = +[](const char*) {}; |
30 | | - gi.frameTimeSec = 0.05f; |
31 | | - globals.numEntities = 1; |
32 | | - |
33 | | - constexpr std::size_t kClientCount = 3; |
34 | | - using Storage = std::aligned_storage_t<sizeof(gclient_t), alignof(gclient_t)>; |
35 | | - Storage raw[kClientCount]; |
36 | | - auto* clients = reinterpret_cast<gclient_t*>(raw); |
37 | | - |
38 | | - ConstructClients(clients, kClientCount); |
39 | | - for (std::size_t i = 0; i < kClientCount; ++i) { |
40 | | - assert(!clients[i].showScores); |
41 | | - assert(!clients[i].showHelp); |
42 | | - } |
43 | | - |
44 | | - clients[0].showScores = true; |
45 | | - DestroyClients(clients, kClientCount); |
46 | | - |
47 | | - ConstructClients(clients, kClientCount); |
48 | | - for (std::size_t i = 0; i < kClientCount; ++i) { |
49 | | - assert(!clients[i].showScores); |
50 | | - } |
51 | | - DestroyClients(clients, kClientCount); |
52 | | - |
53 | | - ClientArrayLifetime lifetime; |
54 | | - lifetime.Reset(clients, kClientCount); |
55 | | - clients[1].showHelp = true; |
56 | | - lifetime.Reset(clients, kClientCount); |
57 | | - for (std::size_t i = 0; i < kClientCount; ++i) { |
58 | | - assert(!clients[i].showHelp); |
59 | | - } |
60 | | - lifetime.Reset(nullptr, 0); |
61 | | - |
62 | | - AllocateClientArray(4); |
63 | | - assert(game.maxClients == 4); |
64 | | - assert(globals.numEntities == 5); |
65 | | - assert(game.clients != nullptr); |
66 | | - assert(game.lagOrigins != nullptr); |
67 | | - |
68 | | - game.clients[0].showScores = true; |
69 | | - ReplaceClientArray(2); |
70 | | - assert(game.maxClients == 2); |
71 | | - assert(globals.numEntities == 3); |
72 | | - assert(!game.clients[0].showScores); |
73 | | - |
74 | | - FreeClientArray(); |
75 | | - assert(game.clients == nullptr); |
76 | | - assert(game.lagOrigins == nullptr); |
77 | | - assert(globals.numEntities == 1); |
78 | | - |
79 | | - return 0; |
| 33 | +class GClientsTest : public ::testing::Test { |
| 34 | +protected: |
| 35 | + void SetUp() override { |
| 36 | + // Initialize mocks |
| 37 | + gi.TagMalloc = MockTagMalloc; |
| 38 | + gi.TagFree = MockTagFree; |
| 39 | + gi.Com_Error = MockComError; |
| 40 | + |
| 41 | + // Allocate g_entities array mock |
| 42 | + // Assuming MAX_CLIENTS_KEX is 32, we need enough space. |
| 43 | + // g_entities needs to be large enough to hold maxClients + 1 entities. |
| 44 | + // Let's allocate 64 entities to be safe. |
| 45 | + g_entities = new gentity_t[64]; |
| 46 | + |
| 47 | + // Ensure g_entities are zeroed out initially |
| 48 | + memset(g_entities, 0, sizeof(gentity_t) * 64); |
| 49 | + |
| 50 | + game.clients = nullptr; |
| 51 | + game.maxClients = 0; |
| 52 | + globals.numEntities = 0; |
| 53 | + } |
| 54 | + |
| 55 | + void TearDown() override { |
| 56 | + if (game.clients) { |
| 57 | + FreeClientArray(); |
| 58 | + } |
| 59 | + delete[] g_entities; |
| 60 | + g_entities = nullptr; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +TEST_F(GClientsTest, AllocateAndFreeClientArray) { |
| 65 | + int maxClients = 4; |
| 66 | + |
| 67 | + // Test Allocation |
| 68 | + AllocateClientArray(maxClients); |
| 69 | + |
| 70 | + EXPECT_EQ(game.maxClients, maxClients); |
| 71 | + EXPECT_NE(game.clients, nullptr); |
| 72 | + EXPECT_EQ(globals.numEntities, maxClients + 1); |
| 73 | + |
| 74 | + // Verify linkage |
| 75 | + for (int i = 0; i < maxClients; i++) { |
| 76 | + EXPECT_EQ(g_entities[i + 1].client, &game.clients[i]); |
| 77 | + } |
| 78 | + |
| 79 | + // Verify FreeClientArray sets pointers to dummy client |
| 80 | + FreeClientArray(); |
| 81 | + |
| 82 | + EXPECT_EQ(game.clients, nullptr); |
| 83 | + EXPECT_EQ(game.maxClients, 0); |
| 84 | + EXPECT_EQ(globals.numEntities, 1); |
| 85 | + |
| 86 | + // The key verification: g_entities[i+1].client should NOT be nullptr |
| 87 | + // It should point to the static dummy client. |
| 88 | + |
| 89 | + gclient_t* dummyAddress = g_entities[1].client; |
| 90 | + EXPECT_NE(dummyAddress, nullptr); |
| 91 | + |
| 92 | + for (int i = 0; i < maxClients; i++) { |
| 93 | + EXPECT_EQ(g_entities[i + 1].client, dummyAddress); |
| 94 | + } |
80 | 95 | } |
0 commit comments