Mercurial > molko
comparison tests/test-action.c @ 480:c1f64d451230
core: mlk_action has a start callback
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 27 Feb 2023 17:26:00 +0100 |
parents | 02c1481b7dbb |
children | daf085bf8a8c |
comparison
equal
deleted
inserted
replaced
479:8e41ed5474cf | 480:c1f64d451230 |
---|---|
22 #include <mlk/core/event.h> | 22 #include <mlk/core/event.h> |
23 | 23 |
24 #include <dt.h> | 24 #include <dt.h> |
25 | 25 |
26 struct invokes { | 26 struct invokes { |
27 int start; | |
27 int handle; | 28 int handle; |
28 int update; | 29 int update; |
29 int draw; | 30 int draw; |
30 int end; | 31 int end; |
31 int finish; | 32 int finish; |
33 | 34 |
34 static union mlk_event dummy; | 35 static union mlk_event dummy; |
35 | 36 |
36 #define INIT(dat, up) { \ | 37 #define INIT(dat, up) { \ |
37 .data = (dat), \ | 38 .data = (dat), \ |
39 .start = my_start, \ | |
38 .handle = my_handle, \ | 40 .handle = my_handle, \ |
39 .update = (up), \ | 41 .update = (up), \ |
40 .draw = my_draw, \ | 42 .draw = my_draw, \ |
41 .end = my_end, \ | 43 .end = my_end, \ |
42 .finish = my_finish \ | 44 .finish = my_finish \ |
43 } | 45 } |
44 | 46 |
45 static void | 47 static void |
48 my_start(struct mlk_action *act) | |
49 { | |
50 ((struct invokes *)act->data)->start = 1; | |
51 } | |
52 | |
53 static void | |
46 my_handle(struct mlk_action *act, const union mlk_event *ev) | 54 my_handle(struct mlk_action *act, const union mlk_event *ev) |
47 { | 55 { |
48 (void)ev; | 56 (void)ev; |
49 | 57 |
50 ((struct invokes *)act->data)->handle = 1; | 58 ((struct invokes *)act->data)->handle = 1; |
87 { | 95 { |
88 ((struct invokes *)act->data)->finish = 1; | 96 ((struct invokes *)act->data)->finish = 1; |
89 } | 97 } |
90 | 98 |
91 static void | 99 static void |
92 test_basics_handle(void) | 100 test_basics_start(void) |
93 { | 101 { |
94 struct invokes inv = {0}; | 102 struct invokes inv = {0}; |
95 struct mlk_action act = INIT(&inv, my_update_true); | 103 struct mlk_action act = INIT(&inv, my_update_true); |
96 | 104 |
105 mlk_action_start(&act); | |
106 | |
107 DT_ASSERT(inv.start); | |
108 DT_ASSERT(!inv.handle); | |
109 DT_ASSERT(!inv.update); | |
110 DT_ASSERT(!inv.draw); | |
111 DT_ASSERT(!inv.end); | |
112 DT_ASSERT(!inv.finish); | |
113 } | |
114 | |
115 static void | |
116 test_basics_handle(void) | |
117 { | |
118 struct invokes inv = {0}; | |
119 struct mlk_action act = INIT(&inv, my_update_true); | |
120 | |
97 mlk_action_handle(&act, &dummy); | 121 mlk_action_handle(&act, &dummy); |
98 | 122 |
123 DT_ASSERT(!inv.start); | |
99 DT_ASSERT(inv.handle); | 124 DT_ASSERT(inv.handle); |
100 DT_ASSERT(!inv.update); | 125 DT_ASSERT(!inv.update); |
101 DT_ASSERT(!inv.draw); | 126 DT_ASSERT(!inv.draw); |
102 DT_ASSERT(!inv.end); | 127 DT_ASSERT(!inv.end); |
103 DT_ASSERT(!inv.finish); | 128 DT_ASSERT(!inv.finish); |
114 { .act = INIT(&table[1], my_update_false) } | 139 { .act = INIT(&table[1], my_update_false) } |
115 }; | 140 }; |
116 | 141 |
117 /* True version. */ | 142 /* True version. */ |
118 DT_ASSERT(mlk_action_update(&table[0].act, 0)); | 143 DT_ASSERT(mlk_action_update(&table[0].act, 0)); |
144 DT_ASSERT(!table[0].inv.start); | |
119 DT_ASSERT(!table[0].inv.handle); | 145 DT_ASSERT(!table[0].inv.handle); |
120 DT_ASSERT(table[0].inv.update); | 146 DT_ASSERT(table[0].inv.update); |
121 DT_ASSERT(!table[0].inv.draw); | 147 DT_ASSERT(!table[0].inv.draw); |
122 DT_ASSERT(!table[0].inv.end); | 148 DT_ASSERT(!table[0].inv.end); |
123 DT_ASSERT(!table[0].inv.finish); | 149 DT_ASSERT(!table[0].inv.finish); |
124 | 150 |
125 /* False version. */ | 151 /* False version. */ |
126 DT_ASSERT(!mlk_action_update(&table[1].act, 0)); | 152 DT_ASSERT(!mlk_action_update(&table[1].act, 0)); |
153 DT_ASSERT(!table[1].inv.start); | |
127 DT_ASSERT(!table[1].inv.handle); | 154 DT_ASSERT(!table[1].inv.handle); |
128 DT_ASSERT(table[1].inv.update); | 155 DT_ASSERT(table[1].inv.update); |
129 DT_ASSERT(!table[1].inv.draw); | 156 DT_ASSERT(!table[1].inv.draw); |
130 DT_ASSERT(!table[1].inv.end); | 157 DT_ASSERT(!table[1].inv.end); |
131 DT_ASSERT(!table[1].inv.finish); | 158 DT_ASSERT(!table[1].inv.finish); |
137 struct invokes inv = {0}; | 164 struct invokes inv = {0}; |
138 struct mlk_action act = INIT(&inv, my_update_true); | 165 struct mlk_action act = INIT(&inv, my_update_true); |
139 | 166 |
140 mlk_action_draw(&act); | 167 mlk_action_draw(&act); |
141 | 168 |
169 DT_ASSERT(!inv.start); | |
142 DT_ASSERT(!inv.handle); | 170 DT_ASSERT(!inv.handle); |
143 DT_ASSERT(!inv.update); | 171 DT_ASSERT(!inv.update); |
144 DT_ASSERT(inv.draw); | 172 DT_ASSERT(inv.draw); |
145 DT_ASSERT(!inv.end); | 173 DT_ASSERT(!inv.end); |
146 DT_ASSERT(!inv.finish); | 174 DT_ASSERT(!inv.finish); |
152 struct invokes inv = {0}; | 180 struct invokes inv = {0}; |
153 struct mlk_action act = INIT(&inv, my_update_true); | 181 struct mlk_action act = INIT(&inv, my_update_true); |
154 | 182 |
155 mlk_action_end(&act); | 183 mlk_action_end(&act); |
156 | 184 |
185 DT_ASSERT(!inv.start); | |
157 DT_ASSERT(!inv.handle); | 186 DT_ASSERT(!inv.handle); |
158 DT_ASSERT(!inv.update); | 187 DT_ASSERT(!inv.update); |
159 DT_ASSERT(!inv.draw); | 188 DT_ASSERT(!inv.draw); |
160 DT_ASSERT(inv.end); | 189 DT_ASSERT(inv.end); |
161 DT_ASSERT(!inv.finish); | 190 DT_ASSERT(!inv.finish); |
167 struct invokes inv = {0}; | 196 struct invokes inv = {0}; |
168 struct mlk_action act = INIT(&inv, my_update_true); | 197 struct mlk_action act = INIT(&inv, my_update_true); |
169 | 198 |
170 mlk_action_finish(&act); | 199 mlk_action_finish(&act); |
171 | 200 |
201 DT_ASSERT(!inv.start); | |
172 DT_ASSERT(!inv.handle); | 202 DT_ASSERT(!inv.handle); |
173 DT_ASSERT(!inv.update); | 203 DT_ASSERT(!inv.update); |
174 DT_ASSERT(!inv.draw); | 204 DT_ASSERT(!inv.draw); |
175 DT_ASSERT(!inv.end); | 205 DT_ASSERT(!inv.end); |
176 DT_ASSERT(inv.finish); | 206 DT_ASSERT(inv.finish); |
194 /* This one should not fit in. */ | 224 /* This one should not fit in. */ |
195 DT_EQ_INT(mlk_action_stack_add(&st, &act), MLK_ERR_NO_MEM); | 225 DT_EQ_INT(mlk_action_stack_add(&st, &act), MLK_ERR_NO_MEM); |
196 } | 226 } |
197 | 227 |
198 static void | 228 static void |
199 test_stack_handle(void) | 229 test_stack_start(void) |
200 { | 230 { |
201 struct { | 231 struct { |
202 int called; | 232 struct invokes inv; |
203 struct mlk_action act; | 233 struct mlk_action act; |
204 } table[] = { | 234 } table[] = { |
205 { 0, { .data = &table[0].called, .handle = my_handle } }, | 235 { .act = INIT(&table[0], my_update_true) }, |
206 { 0, { .data = &table[1].called, .handle = my_handle } }, | 236 { .act = INIT(&table[1], my_update_true) }, |
207 { 0, { .data = &table[2].called, .handle = my_handle } }, | |
208 }; | 237 }; |
209 | 238 |
210 struct mlk_action *actions[10]; | 239 struct mlk_action *actions[10]; |
211 struct mlk_action_stack st = {0}; | 240 struct mlk_action_stack st = {0}; |
212 | 241 |
213 mlk_action_stack_init(&st, actions, 10); | 242 mlk_action_stack_init(&st, actions, 10); |
214 mlk_action_stack_add(&st, &table[0].act); | 243 mlk_action_stack_add(&st, &table[0].act); |
215 mlk_action_stack_add(&st, &table[1].act); | 244 mlk_action_stack_add(&st, &table[1].act); |
216 mlk_action_stack_add(&st, &table[2].act); | 245 mlk_action_stack_start(&st); |
246 | |
247 DT_ASSERT(table[0].inv.start); | |
248 DT_ASSERT(!table[0].inv.handle); | |
249 DT_ASSERT(!table[0].inv.update); | |
250 DT_ASSERT(!table[0].inv.draw); | |
251 DT_ASSERT(!table[0].inv.end); | |
252 DT_ASSERT(!table[0].inv.finish); | |
253 | |
254 DT_ASSERT(table[1].inv.start); | |
255 DT_ASSERT(!table[1].inv.handle); | |
256 DT_ASSERT(!table[1].inv.update); | |
257 DT_ASSERT(!table[1].inv.draw); | |
258 DT_ASSERT(!table[1].inv.end); | |
259 DT_ASSERT(!table[1].inv.finish); | |
260 } | |
261 | |
262 static void | |
263 test_stack_handle(void) | |
264 { | |
265 struct { | |
266 struct invokes inv; | |
267 struct mlk_action act; | |
268 } table[] = { | |
269 { .act = INIT(&table[0], my_update_true) }, | |
270 { .act = INIT(&table[1], my_update_true) }, | |
271 }; | |
272 | |
273 struct mlk_action *actions[10]; | |
274 struct mlk_action_stack st = {0}; | |
275 | |
276 mlk_action_stack_init(&st, actions, 10); | |
277 mlk_action_stack_add(&st, &table[0].act); | |
278 mlk_action_stack_add(&st, &table[1].act); | |
217 mlk_action_stack_handle(&st, &dummy); | 279 mlk_action_stack_handle(&st, &dummy); |
218 | 280 |
219 DT_ASSERT(table[0].called); | 281 DT_ASSERT(!table[0].inv.start); |
220 DT_ASSERT(table[1].called); | 282 DT_ASSERT(table[0].inv.handle); |
221 DT_ASSERT(table[2].called); | 283 DT_ASSERT(!table[0].inv.update); |
284 DT_ASSERT(!table[0].inv.draw); | |
285 DT_ASSERT(!table[0].inv.end); | |
286 DT_ASSERT(!table[0].inv.finish); | |
287 | |
288 DT_ASSERT(!table[1].inv.start); | |
289 DT_ASSERT(table[1].inv.handle); | |
290 DT_ASSERT(!table[1].inv.update); | |
291 DT_ASSERT(!table[1].inv.draw); | |
292 DT_ASSERT(!table[1].inv.end); | |
293 DT_ASSERT(!table[1].inv.finish); | |
222 } | 294 } |
223 | 295 |
224 static void | 296 static void |
225 test_stack_update(void) | 297 test_stack_update(void) |
226 { | 298 { |
396 struct { | 468 struct { |
397 struct invokes inv; | 469 struct invokes inv; |
398 struct mlk_action act; | 470 struct mlk_action act; |
399 } table[] = { | 471 } table[] = { |
400 { .act = INIT(&table[0], my_update_true) }, | 472 { .act = INIT(&table[0], my_update_true) }, |
401 { .act = INIT(&table[0], my_update_false) }, | 473 { .act = INIT(&table[1], my_update_false) }, |
402 }; | 474 }; |
403 | 475 |
404 struct mlk_action *actions[10]; | 476 struct mlk_action *actions[10]; |
405 struct mlk_action_stack st = {0}; | 477 struct mlk_action_stack st = {0}; |
406 | 478 |
413 DT_ASSERT(!table[0].inv.update); | 485 DT_ASSERT(!table[0].inv.update); |
414 DT_ASSERT(!table[0].inv.draw); | 486 DT_ASSERT(!table[0].inv.draw); |
415 DT_ASSERT(table[0].inv.end); | 487 DT_ASSERT(table[0].inv.end); |
416 DT_ASSERT(table[0].inv.finish); | 488 DT_ASSERT(table[0].inv.finish); |
417 | 489 |
418 DT_ASSERT(!table[0].inv.handle); | 490 DT_ASSERT(!table[1].inv.handle); |
419 DT_ASSERT(!table[0].inv.update); | 491 DT_ASSERT(!table[1].inv.update); |
420 DT_ASSERT(!table[0].inv.draw); | 492 DT_ASSERT(!table[1].inv.draw); |
421 DT_ASSERT(table[0].inv.end); | 493 DT_ASSERT(table[1].inv.end); |
422 DT_ASSERT(table[0].inv.finish); | 494 DT_ASSERT(table[1].inv.finish); |
423 } | 495 } |
424 | 496 |
425 int | 497 int |
426 main(void) | 498 main(void) |
427 { | 499 { |
500 DT_RUN(test_basics_start); | |
428 DT_RUN(test_basics_handle); | 501 DT_RUN(test_basics_handle); |
429 DT_RUN(test_basics_update); | 502 DT_RUN(test_basics_update); |
430 DT_RUN(test_basics_draw); | 503 DT_RUN(test_basics_draw); |
431 DT_RUN(test_basics_end); | 504 DT_RUN(test_basics_end); |
432 DT_RUN(test_basics_finish); | 505 DT_RUN(test_basics_finish); |
433 DT_RUN(test_stack_add); | 506 DT_RUN(test_stack_add); |
507 DT_RUN(test_stack_start); | |
434 DT_RUN(test_stack_handle); | 508 DT_RUN(test_stack_handle); |
435 DT_RUN(test_stack_update); | 509 DT_RUN(test_stack_update); |
436 DT_RUN(test_stack_draw); | 510 DT_RUN(test_stack_draw); |
437 DT_RUN(test_stack_finish); | 511 DT_RUN(test_stack_finish); |
438 DT_SUMMARY(); | 512 DT_SUMMARY(); |