R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Tests.cpp
Go to the documentation of this file.
1#include "Tests.hpp"
2
3EventDispatcher *ev = nullptr;
4Registry *r = nullptr;
5Game *g = nullptr;
6Queue *q = nullptr;
7
8inline void printMessage(GameMessage &msg)
9{
10 switch (msg.type)
11 {
12 case P_MOVE:
13 std::cout << "MOVE " << msg.id << " " << msg.msg.coords.x << " " << msg.msg.coords.y;
14 break;
15 case P_SHOOT:
16 std::cout << "SHOOT " << msg.id;
17 break;
18 case P_SPAWN:
19 std::cout << "SPAWN " << msg.id << " " << msg.msg.asset_id << " " << msg.msg.coords.x << " " << msg.msg.coords.y;
20 break;
21 case P_KILL:
22 std::cout << "KILL " << msg.id;
23 break;
24 case P_DAMAGE:
25 std::cout << "MOVE " << msg.id;
26 break;
27 case P_STATUS:
28 std::cout << "STATUS ";
29 switch (msg.msg.status)
30 {
31 case 0x00:
32 std::cout << "ON GOING";
33 break;
34 case 0x01:
35 std::cout << "VICTORY";
36 break;
37 default:
38 std::cout << "DEFEAT";
39 break;
40 }
41 break;
42 default:
43 break;
44 }
45 std::cout << std::endl;
46}
47
48void cleanup(void)
49{
50 if (ev)
51 delete ev;
52 if (r)
53 delete r;
54 if (g)
55 delete g;
56 if (q)
57 delete q;
58}
59
60int test_zipper(void)
61{
62 std::vector<std::optional<int>> v1 = {97, 98, std::nullopt, 99, 100};
63 std::vector<std::optional<char>> v2 = {'a', 'b', 'o', 'c', std::nullopt};
64 try
65 {
66 Zipper z = Zipper(v1, v2);
67 for (auto &&[num, letter] : z) {
68 if (num != letter)
69 throw std::runtime_error("Zipper is not zipping.");
70 }
71 }
72 catch(std::exception& e)
73 {
74 std::cout << "Test Failed" << std::endl;
75 std::cerr << "Exception caught: " << e.what() << std::endl;
76 return -1;
77 }
78 std::cout << "Test OK" << std::endl;
79 return 0;
80}
81
83{
84 std::vector<std::optional<int>> v1 = {97, 98, std::nullopt, 99, 100};
85 std::vector<std::optional<char>> v2 = {'a', 'b', 'o', 'c', std::nullopt};
86 try
87 {
88 IndexedZipper z = IndexedZipper(v1, v2);
89 for (auto &&[idx, num, letter] : z) {
90 if (num != letter)
91 throw std::runtime_error("IndexedZipper is not zipping.");
92 }
93 }
94 catch(const std::exception& e)
95 {
96 std::cout << "Test Failed" << std::endl;
97 std::cerr << "Exception caught: " << e.what() << std::endl;
98 return -1;
99 }
100 std::cout << "Test OK" << std::endl;
101 return 0;
102}
103
105{
106 try {
107 ev = new EventDispatcher();
108 q = new Queue();
109 r = new Registry(ev, q);
110 } catch (std::exception &e) {
111 std::cout << "Test Failed" << std::endl;
112 std::cerr << "Exception caught: " << e.what() << std::endl;
113 return -1;
114 }
115 std::cout << "Test OK" << std::endl;
116 return 0;
117}
118
120{
121 try {
122 if (!r)
123 throw std::runtime_error("Registry not initialized");
124 r->run_systems();
125 } catch (std::exception &e) {
126 std::cout << "Test Failed" << std::endl;
127 std::cerr << "Exception caught: " << e.what() << std::endl;
128 return -1;
129 }
130 std::cout << "Test OK" << std::endl;
131 return 0;
132}
133
135{
136 try {
137 g = new Game();
138 } catch (std::exception &e) {
139 std::cout << "Test Failed" << std::endl;
140 std::cerr << "Exception caught: " << e.what() << std::endl;
141 return -1;
142 }
143 std::cout << "Test OK" << std::endl;
144 return 0;
145}
146
148{
149 try {
150 if (!g)
151 throw std::runtime_error("Game not initialized");
152 g->update(6);
153 } catch (std::exception &e) {
154 std::cout << "Test Failed" << std::endl;
155 std::cerr << "Exception caught: " << e.what() << std::endl;
156 return -1;
157 }
158 std::cout << "Test OK" << std::endl;
159 return 0;
160}
161
163{
164 try {
165 if (!g)
166 throw std::runtime_error("Game not initialized");
167 g->update(6);
168 auto list = g->getGameEvents();
169 for (auto a : list) {
170 std::istringstream iss = std::istringstream(a);
171 GameMessage msg = deserialize(iss);
172 //printMessage(msg);
173 }
174 } catch (std::exception &e) {
175 std::cout << "Test Failed" << std::endl;
176 std::cerr << "Exception caught: " << e.what() << std::endl;
177 return -1;
178 }
179 std::cout << "Test OK" << std::endl;
180 return 0;
181}
182
184{
185 GameMessage msg = {messageType::P_CONNECT, 0, {0, 0, 0, "Player1", {0, 0}}};
186 std::ostringstream oss;
187 std::string s;
188 try {
189 if (!g)
190 throw std::runtime_error("Game not initialized");
191 serialize(msg, oss);
192 s = oss.str();
194 g->update(1);
195 auto list = g->getGameEvents();
196 for (auto a : list) {
197 std::istringstream iss = std::istringstream(a);
198 GameMessage msg = deserialize(iss);
199 //printMessage(msg);
200 }
201 } catch (std::exception &e) {
202 std::cout << "Test Failed" << std::endl;
203 std::cerr << "Exception caught: " << e.what() << std::endl;
204 return -1;
205 }
206 std::cout << "Test OK" << std::endl;
207 return 0;
208}
209
210int test_stress(void)
211{
212 GameMessage msg1 = {messageType::P_CONNECT, 0, {0, 0, 0, "Player1", {0, 0}}};
213 GameMessage msg2 = {messageType::P_CONNECT, 0, {0, 0, 1, "Player2", {0, 0}}};
214 try {
215 Game newGame;
216 std::ostringstream oss;
217 std::string s;
218 serialize(msg1, oss);
219 s = oss.str();
220 newGame.onServerEventReceived(s);
221 serialize(msg2, oss);
222 s = oss.str();
223 newGame.onServerEventReceived(s);
224 for (int i = 0; i <= 60; i++) {
225 newGame.update(1);
226 auto events = newGame.getGameEvents();
227 for (auto event : events) {
228 std::cout << "frame " << i << ": ";
229 std::istringstream iss(event);
230 GameMessage msg = deserialize(iss);
231 printMessage(msg);
232 }
233 }
234 } catch (std::exception &e) {
235 std::cout << "Test Failed" << std::endl;
236 std::cerr << "Exception caught: " << e.what() << std::endl;
237 return -1;
238 }
239 std::cout << "Test OK" << std::endl;
240 return 0;
241}
GameMessage deserialize(std::istringstream &is)
Deserializes a GameMessage object from a binary stream.
void serialize(const GameMessage &message, std::ostringstream &os)
Serializes a GameMessage object into a binary stream.
@ P_SHOOT
Represents a player shooting event.
@ P_CONNECT
Represents a connecting player event.
@ P_DAMAGE
Represents a damage event.
@ P_KILL
Represents a kill event.
@ P_STATUS
Represents a status event.
@ P_SPAWN
Represents a spawn event.
@ P_MOVE
Represents a movement event.
Registry * r
Definition Tests.cpp:4
EventDispatcher * ev
Definition Tests.cpp:3
int test_get_game_event(void)
Test retrieving game events from the Game instance.
Definition Tests.cpp:162
int test_stress(void)
Test for stress and performance.
Definition Tests.cpp:210
int test_indexed_zipper(void)
Test the functionality of the IndexedZipper class.
Definition Tests.cpp:82
int test_create_registry(void)
Test function for creating a registry.
Definition Tests.cpp:104
int test_zipper(void)
Test the functionality of the Zipper class.
Definition Tests.cpp:60
int test_send_msg(void)
Test sending a message to the Game instance.
Definition Tests.cpp:183
Queue * q
Definition Tests.cpp:6
int test_run_systems(void)
Test function for running systems within the registry.
Definition Tests.cpp:119
void printMessage(GameMessage &msg)
Definition Tests.cpp:8
int test_create_game(void)
Test function for creating a game.
Definition Tests.cpp:134
int test_run_update(void)
Test function for running the game's update cycle.
Definition Tests.cpp:147
void cleanup(void)
Cleans up any resources or states after tests.
Definition Tests.cpp:48
Game * g
Definition Tests.cpp:5
A utility class for dispatching and handling game events.
Represents the main game logic and state management.
Definition Game.hpp:20
std::forward_list< std::string > getGameEvents(void)
Retrieves game messages.
Definition Game.cpp:30
void update(float _deltaTime)
Updates the game state for the current frame.
Definition Game.cpp:24
void onServerEventReceived(std::string &event)
Handles an event received from server.
Definition Game.cpp:58
Combines multiple containers into a single iterable unit, iterating over corresponding elements from ...
A thread-safe queue for storing and managing GameMessage objects.
Definition Queue.hpp:17
Manages entities and their associated components, enabling the creation, deletion,...
Definition Registry.hpp:23
void run_systems()
Executes all registered systems.
Definition Registry.cpp:11
Combines multiple containers into a single iterable unit, iterating over corresponding elements from ...
Definition Zipper.hpp:15
A structure representing a game message.
messageType type
The type of message (e.g., SPAWN, MOVE, etc.).
messageInfo msg
Additional information about the event (status, asset ID, and coordinates).
std::size_t id
The unique identifier for the entity or object involved in the event.
float x
The X coordinate.
float y
The Y coordinate.
uint8_t status
Status of the event (e.g., success or failure).
coord coords
Coordinates associated with the event, such as the position of an entity.
int asset_id
ID representing the game asset related to the event (e.g., entity or object).