comparison tests/test-state.c @ 410:1bf7d6669f0a

tests: switch to libdt
author David Demelier <markand@malikania.fr>
date Fri, 09 Sep 2022 13:30:34 +0200
parents 0ea90751a62d
children 3edda1ce314c
comparison
equal deleted inserted replaced
409:6011ad866e99 410:1bf7d6669f0a
21 #include <core/event.h> 21 #include <core/event.h>
22 #include <core/game.h> 22 #include <core/game.h>
23 #include <core/state.h> 23 #include <core/state.h>
24 #include <core/util.h> 24 #include <core/util.h>
25 25
26 #include "test.h" 26 #include <dt.h>
27 27
28 struct invokes { 28 struct invokes {
29 unsigned int start; 29 unsigned int start;
30 unsigned int handle; 30 unsigned int handle;
31 unsigned int update; 31 unsigned int update;
34 unsigned int resume; 34 unsigned int resume;
35 unsigned int end; 35 unsigned int end;
36 unsigned int finish; 36 unsigned int finish;
37 }; 37 };
38 38
39 static struct state *states[16]; 39 static struct state *mainstates[16];
40
41 RX_SET_UP(basics_set_up)
42 {
43 game_init(states, UTIL_SIZE(states));
44
45 return RX_SUCCESS;
46 }
47
48 RX_TEAR_DOWN(basics_tear_down)
49 {
50 game_quit();
51 }
52 40
53 static void 41 static void
54 my_start(struct state *state) 42 my_start(struct state *state)
55 { 43 {
56 ((struct invokes *)state->data)->start++; 44 ((struct invokes *)state->data)->start++;
112 .resume = my_resume, \ 100 .resume = my_resume, \
113 .end = my_end, \ 101 .end = my_end, \
114 .finish = my_finish \ 102 .finish = my_finish \
115 } 103 }
116 104
117 RX_TEST_CASE(basics, start) 105 static void
106 test_basics_start(void)
118 { 107 {
119 struct invokes inv = {0}; 108 struct invokes inv = {0};
120 struct state state = INIT(&inv); 109 struct state state = INIT(&inv);
121 110
122 state_start(&state); 111 state_start(&state);
123 RX_UINT_REQUIRE_EQUAL(inv.start, 1U); 112 DT_EQ_UINT(inv.start, 1U);
124 RX_UINT_REQUIRE_EQUAL(inv.handle, 0U); 113 DT_EQ_UINT(inv.handle, 0U);
125 RX_UINT_REQUIRE_EQUAL(inv.update, 0U); 114 DT_EQ_UINT(inv.update, 0U);
126 RX_UINT_REQUIRE_EQUAL(inv.draw, 0U); 115 DT_EQ_UINT(inv.draw, 0U);
127 RX_UINT_REQUIRE_EQUAL(inv.end, 0U); 116 DT_EQ_UINT(inv.end, 0U);
128 RX_UINT_REQUIRE_EQUAL(inv.finish, 0U); 117 DT_EQ_UINT(inv.finish, 0U);
129 } 118 }
130 119
131 RX_TEST_CASE(basics, handle) 120 static void
121 test_basics_handle(void)
132 { 122 {
133 struct invokes inv = {0}; 123 struct invokes inv = {0};
134 struct state state = INIT(&inv); 124 struct state state = INIT(&inv);
135 125
136 state_handle(&state, &(const union event){0}); 126 state_handle(&state, &(const union event){0});
137 RX_UINT_REQUIRE_EQUAL(inv.start, 0U); 127 DT_EQ_UINT(inv.start, 0U);
138 RX_UINT_REQUIRE_EQUAL(inv.handle, 1U); 128 DT_EQ_UINT(inv.handle, 1U);
139 RX_UINT_REQUIRE_EQUAL(inv.update, 0U); 129 DT_EQ_UINT(inv.update, 0U);
140 RX_UINT_REQUIRE_EQUAL(inv.draw, 0U); 130 DT_EQ_UINT(inv.draw, 0U);
141 RX_UINT_REQUIRE_EQUAL(inv.end, 0U); 131 DT_EQ_UINT(inv.end, 0U);
142 RX_UINT_REQUIRE_EQUAL(inv.finish, 0U); 132 DT_EQ_UINT(inv.finish, 0U);
143 } 133 }
144 134
145 RX_TEST_CASE(basics, update) 135 static void
136 test_basics_update(void)
146 { 137 {
147 struct invokes inv = {0}; 138 struct invokes inv = {0};
148 struct state state = INIT(&inv); 139 struct state state = INIT(&inv);
149 140
150 state_update(&state, 0); 141 state_update(&state, 0);
151 RX_UINT_REQUIRE_EQUAL(inv.start, 0U); 142 DT_EQ_UINT(inv.start, 0U);
152 RX_UINT_REQUIRE_EQUAL(inv.handle, 0U); 143 DT_EQ_UINT(inv.handle, 0U);
153 RX_UINT_REQUIRE_EQUAL(inv.update, 1U); 144 DT_EQ_UINT(inv.update, 1U);
154 RX_UINT_REQUIRE_EQUAL(inv.draw, 0U); 145 DT_EQ_UINT(inv.draw, 0U);
155 RX_UINT_REQUIRE_EQUAL(inv.end, 0U); 146 DT_EQ_UINT(inv.end, 0U);
156 RX_UINT_REQUIRE_EQUAL(inv.finish, 0U); 147 DT_EQ_UINT(inv.finish, 0U);
157 } 148 }
158 149
159 RX_TEST_CASE(basics, draw) 150 static void
151 test_basics_draw(void)
160 { 152 {
161 struct invokes inv = {0}; 153 struct invokes inv = {0};
162 struct state state = INIT(&inv); 154 struct state state = INIT(&inv);
163 155
164 state_draw(&state); 156 state_draw(&state);
165 RX_UINT_REQUIRE_EQUAL(inv.start, 0U); 157 DT_EQ_UINT(inv.start, 0U);
166 RX_UINT_REQUIRE_EQUAL(inv.handle, 0U); 158 DT_EQ_UINT(inv.handle, 0U);
167 RX_UINT_REQUIRE_EQUAL(inv.update, 0U); 159 DT_EQ_UINT(inv.update, 0U);
168 RX_UINT_REQUIRE_EQUAL(inv.draw, 1U); 160 DT_EQ_UINT(inv.draw, 1U);
169 RX_UINT_REQUIRE_EQUAL(inv.end, 0U); 161 DT_EQ_UINT(inv.end, 0U);
170 RX_UINT_REQUIRE_EQUAL(inv.finish, 0U); 162 DT_EQ_UINT(inv.finish, 0U);
171 } 163 }
172 164
173 RX_TEST_CASE(basics, end) 165 static void
166 test_basics_end(void)
174 { 167 {
175 struct invokes inv = {0}; 168 struct invokes inv = {0};
176 struct state state = INIT(&inv); 169 struct state state = INIT(&inv);
177 170
178 state_end(&state); 171 state_end(&state);
179 RX_UINT_REQUIRE_EQUAL(inv.start, 0U); 172 DT_EQ_UINT(inv.start, 0U);
180 RX_UINT_REQUIRE_EQUAL(inv.handle, 0U); 173 DT_EQ_UINT(inv.handle, 0U);
181 RX_UINT_REQUIRE_EQUAL(inv.update, 0U); 174 DT_EQ_UINT(inv.update, 0U);
182 RX_UINT_REQUIRE_EQUAL(inv.draw, 0U); 175 DT_EQ_UINT(inv.draw, 0U);
183 RX_UINT_REQUIRE_EQUAL(inv.end, 1U); 176 DT_EQ_UINT(inv.end, 1U);
184 RX_UINT_REQUIRE_EQUAL(inv.finish, 0U); 177 DT_EQ_UINT(inv.finish, 0U);
185 } 178 }
186 179
187 RX_TEST_CASE(basics, finish) 180 static void
181 test_basics_finish(void)
188 { 182 {
189 struct invokes inv = {0}; 183 struct invokes inv = {0};
190 struct state state = INIT(&inv); 184 struct state state = INIT(&inv);
191 185
192 state_finish(&state); 186 state_finish(&state);
193 RX_UINT_REQUIRE_EQUAL(inv.start, 0U); 187 DT_EQ_UINT(inv.start, 0U);
194 RX_UINT_REQUIRE_EQUAL(inv.handle, 0U); 188 DT_EQ_UINT(inv.handle, 0U);
195 RX_UINT_REQUIRE_EQUAL(inv.update, 0U); 189 DT_EQ_UINT(inv.update, 0U);
196 RX_UINT_REQUIRE_EQUAL(inv.draw, 0U); 190 DT_EQ_UINT(inv.draw, 0U);
197 RX_UINT_REQUIRE_EQUAL(inv.end, 0U); 191 DT_EQ_UINT(inv.end, 0U);
198 RX_UINT_REQUIRE_EQUAL(inv.finish, 1U); 192 DT_EQ_UINT(inv.finish, 1U);
199 } 193 }
200 194
201 RX_TEST_CASE(basics, game) 195 static void
196 test_basics_game(void)
202 { 197 {
203 static struct { 198 static struct {
204 struct invokes inv; 199 struct invokes inv;
205 struct state state; 200 struct state state;
206 } states[] = { 201 } states[] = {
207 { .state = INIT(&states[0].inv) }, 202 { .state = INIT(&states[0].inv) },
208 { .state = INIT(&states[1].inv) } 203 { .state = INIT(&states[1].inv) }
209 }; 204 };
210 205
211 /* 0 becomes active and should start. */ 206 /* 0 becomes active and should start. */
207 game_init(mainstates, UTIL_SIZE(mainstates));
212 game_push(&states[0].state); 208 game_push(&states[0].state);
213 RX_UINT_REQUIRE_EQUAL(states[0].inv.start, 1U); 209
214 RX_UINT_REQUIRE_EQUAL(states[0].inv.handle, 0U); 210 DT_EQ_UINT(states[0].inv.start, 1U);
215 RX_UINT_REQUIRE_EQUAL(states[0].inv.update, 0U); 211 DT_EQ_UINT(states[0].inv.handle, 0U);
216 RX_UINT_REQUIRE_EQUAL(states[0].inv.draw, 0U); 212 DT_EQ_UINT(states[0].inv.update, 0U);
217 RX_UINT_REQUIRE_EQUAL(states[0].inv.suspend, 0U); 213 DT_EQ_UINT(states[0].inv.draw, 0U);
218 RX_UINT_REQUIRE_EQUAL(states[0].inv.resume, 0U); 214 DT_EQ_UINT(states[0].inv.suspend, 0U);
219 RX_UINT_REQUIRE_EQUAL(states[0].inv.end, 0U); 215 DT_EQ_UINT(states[0].inv.resume, 0U);
220 RX_UINT_REQUIRE_EQUAL(states[0].inv.finish, 0U); 216 DT_EQ_UINT(states[0].inv.end, 0U);
217 DT_EQ_UINT(states[0].inv.finish, 0U);
221 218
222 /* Put some event, update and drawing. */ 219 /* Put some event, update and drawing. */
223 game_handle(&(union event) { .type = EVENT_QUIT }); 220 game_handle(&(union event) { .type = EVENT_QUIT });
224 game_update(100); 221 game_update(100);
225 game_update(100); 222 game_update(100);
226 game_draw(); 223 game_draw();
227 game_draw(); 224 game_draw();
228 game_draw(); 225 game_draw();
229 RX_UINT_REQUIRE_EQUAL(states[0].inv.start, 1U); 226 DT_EQ_UINT(states[0].inv.start, 1U);
230 RX_UINT_REQUIRE_EQUAL(states[0].inv.handle, 1U); 227 DT_EQ_UINT(states[0].inv.handle, 1U);
231 RX_UINT_REQUIRE_EQUAL(states[0].inv.update, 2U); 228 DT_EQ_UINT(states[0].inv.update, 2U);
232 RX_UINT_REQUIRE_EQUAL(states[0].inv.draw, 3U); 229 DT_EQ_UINT(states[0].inv.draw, 3U);
233 RX_UINT_REQUIRE_EQUAL(states[0].inv.suspend, 0U); 230 DT_EQ_UINT(states[0].inv.suspend, 0U);
234 RX_UINT_REQUIRE_EQUAL(states[0].inv.resume, 0U); 231 DT_EQ_UINT(states[0].inv.resume, 0U);
235 RX_UINT_REQUIRE_EQUAL(states[0].inv.end, 0U); 232 DT_EQ_UINT(states[0].inv.end, 0U);
236 RX_UINT_REQUIRE_EQUAL(states[0].inv.finish, 0U); 233 DT_EQ_UINT(states[0].inv.finish, 0U);
237 234
238 /* Switch to state 1, 0 must be suspended. */ 235 /* Switch to state 1, 0 must be suspended. */
239 game_push(&states[1].state); 236 game_push(&states[1].state);
240 RX_UINT_REQUIRE_EQUAL(states[0].inv.start, 1U); 237 DT_EQ_UINT(states[0].inv.start, 1U);
241 RX_UINT_REQUIRE_EQUAL(states[0].inv.handle, 1U); 238 DT_EQ_UINT(states[0].inv.handle, 1U);
242 RX_UINT_REQUIRE_EQUAL(states[0].inv.update, 2U); 239 DT_EQ_UINT(states[0].inv.update, 2U);
243 RX_UINT_REQUIRE_EQUAL(states[0].inv.draw, 3U); 240 DT_EQ_UINT(states[0].inv.draw, 3U);
244 RX_UINT_REQUIRE_EQUAL(states[0].inv.suspend, 1U); 241 DT_EQ_UINT(states[0].inv.suspend, 1U);
245 RX_UINT_REQUIRE_EQUAL(states[0].inv.resume, 0U); 242 DT_EQ_UINT(states[0].inv.resume, 0U);
246 RX_UINT_REQUIRE_EQUAL(states[0].inv.end, 0U); 243 DT_EQ_UINT(states[0].inv.end, 0U);
247 RX_UINT_REQUIRE_EQUAL(states[0].inv.finish, 0U); 244 DT_EQ_UINT(states[0].inv.finish, 0U);
248 245
249 RX_UINT_REQUIRE_EQUAL(states[1].inv.start, 1U); 246 DT_EQ_UINT(states[1].inv.start, 1U);
250 RX_UINT_REQUIRE_EQUAL(states[1].inv.handle, 0U); 247 DT_EQ_UINT(states[1].inv.handle, 0U);
251 RX_UINT_REQUIRE_EQUAL(states[1].inv.update, 0U); 248 DT_EQ_UINT(states[1].inv.update, 0U);
252 RX_UINT_REQUIRE_EQUAL(states[1].inv.draw, 0U); 249 DT_EQ_UINT(states[1].inv.draw, 0U);
253 RX_UINT_REQUIRE_EQUAL(states[1].inv.suspend, 0U); 250 DT_EQ_UINT(states[1].inv.suspend, 0U);
254 RX_UINT_REQUIRE_EQUAL(states[1].inv.resume, 0U); 251 DT_EQ_UINT(states[1].inv.resume, 0U);
255 RX_UINT_REQUIRE_EQUAL(states[1].inv.end, 0U); 252 DT_EQ_UINT(states[1].inv.end, 0U);
256 RX_UINT_REQUIRE_EQUAL(states[1].inv.finish, 0U); 253 DT_EQ_UINT(states[1].inv.finish, 0U);
257 254
258 /* Update a little this state. */ 255 /* Update a little this state. */
259 game_update(10); 256 game_update(10);
260 game_update(10); 257 game_update(10);
261 game_update(10); 258 game_update(10);
262 game_update(10); 259 game_update(10);
263 RX_UINT_REQUIRE_EQUAL(states[0].inv.start, 1U); 260 DT_EQ_UINT(states[0].inv.start, 1U);
264 RX_UINT_REQUIRE_EQUAL(states[0].inv.handle, 1U); 261 DT_EQ_UINT(states[0].inv.handle, 1U);
265 RX_UINT_REQUIRE_EQUAL(states[0].inv.update, 2U); 262 DT_EQ_UINT(states[0].inv.update, 2U);
266 RX_UINT_REQUIRE_EQUAL(states[0].inv.draw, 3U); 263 DT_EQ_UINT(states[0].inv.draw, 3U);
267 RX_UINT_REQUIRE_EQUAL(states[0].inv.suspend, 1U); 264 DT_EQ_UINT(states[0].inv.suspend, 1U);
268 RX_UINT_REQUIRE_EQUAL(states[0].inv.resume, 0U); 265 DT_EQ_UINT(states[0].inv.resume, 0U);
269 RX_UINT_REQUIRE_EQUAL(states[0].inv.end, 0U); 266 DT_EQ_UINT(states[0].inv.end, 0U);
270 RX_UINT_REQUIRE_EQUAL(states[0].inv.finish, 0U); 267 DT_EQ_UINT(states[0].inv.finish, 0U);
271 268
272 RX_UINT_REQUIRE_EQUAL(states[1].inv.start, 1U); 269 DT_EQ_UINT(states[1].inv.start, 1U);
273 RX_UINT_REQUIRE_EQUAL(states[1].inv.handle, 0U); 270 DT_EQ_UINT(states[1].inv.handle, 0U);
274 RX_UINT_REQUIRE_EQUAL(states[1].inv.update, 4U); 271 DT_EQ_UINT(states[1].inv.update, 4U);
275 RX_UINT_REQUIRE_EQUAL(states[1].inv.draw, 0U); 272 DT_EQ_UINT(states[1].inv.draw, 0U);
276 RX_UINT_REQUIRE_EQUAL(states[1].inv.suspend, 0U); 273 DT_EQ_UINT(states[1].inv.suspend, 0U);
277 RX_UINT_REQUIRE_EQUAL(states[1].inv.resume, 0U); 274 DT_EQ_UINT(states[1].inv.resume, 0U);
278 RX_UINT_REQUIRE_EQUAL(states[1].inv.end, 0U); 275 DT_EQ_UINT(states[1].inv.end, 0U);
279 RX_UINT_REQUIRE_EQUAL(states[1].inv.finish, 0U); 276 DT_EQ_UINT(states[1].inv.finish, 0U);
280 277
281 /* Pop it, it should be finalized through end and finish. */ 278 /* Pop it, it should be finalized through end and finish. */
282 game_pop(); 279 game_pop();
283 280
284 RX_UINT_REQUIRE_EQUAL(states[0].inv.start, 1U); 281 DT_EQ_UINT(states[0].inv.start, 1U);
285 RX_UINT_REQUIRE_EQUAL(states[0].inv.handle, 1U); 282 DT_EQ_UINT(states[0].inv.handle, 1U);
286 RX_UINT_REQUIRE_EQUAL(states[0].inv.update, 2U); 283 DT_EQ_UINT(states[0].inv.update, 2U);
287 RX_UINT_REQUIRE_EQUAL(states[0].inv.draw, 3U); 284 DT_EQ_UINT(states[0].inv.draw, 3U);
288 RX_UINT_REQUIRE_EQUAL(states[0].inv.suspend, 1U); 285 DT_EQ_UINT(states[0].inv.suspend, 1U);
289 RX_UINT_REQUIRE_EQUAL(states[0].inv.resume, 1U); 286 DT_EQ_UINT(states[0].inv.resume, 1U);
290 RX_UINT_REQUIRE_EQUAL(states[0].inv.end, 0U); 287 DT_EQ_UINT(states[0].inv.end, 0U);
291 RX_UINT_REQUIRE_EQUAL(states[0].inv.finish, 0U); 288 DT_EQ_UINT(states[0].inv.finish, 0U);
292 289
293 RX_UINT_REQUIRE_EQUAL(states[1].inv.start, 1U); 290 DT_EQ_UINT(states[1].inv.start, 1U);
294 RX_UINT_REQUIRE_EQUAL(states[1].inv.handle, 0U); 291 DT_EQ_UINT(states[1].inv.handle, 0U);
295 RX_UINT_REQUIRE_EQUAL(states[1].inv.update, 4U); 292 DT_EQ_UINT(states[1].inv.update, 4U);
296 RX_UINT_REQUIRE_EQUAL(states[1].inv.draw, 0U); 293 DT_EQ_UINT(states[1].inv.draw, 0U);
297 RX_UINT_REQUIRE_EQUAL(states[1].inv.suspend, 0U); 294 DT_EQ_UINT(states[1].inv.suspend, 0U);
298 RX_UINT_REQUIRE_EQUAL(states[1].inv.resume, 0U); 295 DT_EQ_UINT(states[1].inv.resume, 0U);
299 RX_UINT_REQUIRE_EQUAL(states[1].inv.end, 1U); 296 DT_EQ_UINT(states[1].inv.end, 1U);
300 RX_UINT_REQUIRE_EQUAL(states[1].inv.finish, 1U); 297 DT_EQ_UINT(states[1].inv.finish, 1U);
301 298
302 /* Pop this state as well. */ 299 /* Pop this state as well. */
303 game_pop(); 300 game_pop();
304 301
305 RX_UINT_REQUIRE_EQUAL(states[0].inv.start, 1U); 302 DT_EQ_UINT(states[0].inv.start, 1U);
306 RX_UINT_REQUIRE_EQUAL(states[0].inv.handle, 1U); 303 DT_EQ_UINT(states[0].inv.handle, 1U);
307 RX_UINT_REQUIRE_EQUAL(states[0].inv.update, 2U); 304 DT_EQ_UINT(states[0].inv.update, 2U);
308 RX_UINT_REQUIRE_EQUAL(states[0].inv.draw, 3U); 305 DT_EQ_UINT(states[0].inv.draw, 3U);
309 RX_UINT_REQUIRE_EQUAL(states[0].inv.suspend, 1U); 306 DT_EQ_UINT(states[0].inv.suspend, 1U);
310 RX_UINT_REQUIRE_EQUAL(states[0].inv.resume, 1U); 307 DT_EQ_UINT(states[0].inv.resume, 1U);
311 RX_UINT_REQUIRE_EQUAL(states[0].inv.end, 1U); 308 DT_EQ_UINT(states[0].inv.end, 1U);
312 RX_UINT_REQUIRE_EQUAL(states[0].inv.finish, 1U); 309 DT_EQ_UINT(states[0].inv.finish, 1U);
313 310
314 RX_UINT_REQUIRE_EQUAL(states[1].inv.start, 1U); 311 DT_EQ_UINT(states[1].inv.start, 1U);
315 RX_UINT_REQUIRE_EQUAL(states[1].inv.handle, 0U); 312 DT_EQ_UINT(states[1].inv.handle, 0U);
316 RX_UINT_REQUIRE_EQUAL(states[1].inv.update, 4U); 313 DT_EQ_UINT(states[1].inv.update, 4U);
317 RX_UINT_REQUIRE_EQUAL(states[1].inv.draw, 0U); 314 DT_EQ_UINT(states[1].inv.draw, 0U);
318 RX_UINT_REQUIRE_EQUAL(states[1].inv.suspend, 0U); 315 DT_EQ_UINT(states[1].inv.suspend, 0U);
319 RX_UINT_REQUIRE_EQUAL(states[1].inv.resume, 0U); 316 DT_EQ_UINT(states[1].inv.resume, 0U);
320 RX_UINT_REQUIRE_EQUAL(states[1].inv.end, 1U); 317 DT_EQ_UINT(states[1].inv.end, 1U);
321 RX_UINT_REQUIRE_EQUAL(states[1].inv.finish, 1U); 318 DT_EQ_UINT(states[1].inv.finish, 1U);
322 } 319
323 320 game_quit();
324 static const struct rx_test_case tests[] = { 321 }
325 TEST(basics, start),
326 TEST(basics, handle),
327 TEST(basics, update),
328 TEST(basics, draw),
329 TEST(basics, end),
330 TEST(basics, finish),
331 TEST_FIXTURE(basics, game, void *)
332 };
333 322
334 int 323 int
335 main(int argc, char **argv) 324 main(int argc, char **argv)
336 { 325 {
337 return TEST_RUN_ALL(tests, argc, argv); 326 DT_RUN(test_basics_start);
338 } 327 DT_RUN(test_basics_handle);
328 DT_RUN(test_basics_update);
329 DT_RUN(test_basics_draw);
330 DT_RUN(test_basics_end);
331 DT_RUN(test_basics_finish);
332 DT_RUN(test_basics_game);
333 DT_SUMMARY();
334 }