]>
Commit | Line | Data |
---|---|---|
5b262bb6 DO |
1 | /* |
2 | * QTest testcase for the ptimer | |
3 | * | |
4 | * Author: Dmitry Osipenko <digetx@gmail.com> | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | * | |
9 | */ | |
10 | ||
11 | #include <glib/gprintf.h> | |
12 | ||
13 | #include "qemu/osdep.h" | |
14 | #include "qemu/main-loop.h" | |
15 | #include "hw/ptimer.h" | |
16 | ||
17 | #include "libqtest.h" | |
18 | #include "ptimer-test.h" | |
19 | ||
20 | static bool triggered; | |
21 | ||
22 | static void ptimer_trigger(void *opaque) | |
23 | { | |
24 | triggered = true; | |
25 | } | |
26 | ||
27 | static void ptimer_test_expire_qemu_timers(int64_t expire_time, | |
28 | QEMUClockType type) | |
29 | { | |
30 | QEMUTimerList *timer_list = main_loop_tlg.tl[type]; | |
31 | QEMUTimer *t = timer_list->active_timers.next; | |
32 | ||
33 | while (t != NULL) { | |
34 | if (t->expire_time == expire_time) { | |
35 | timer_del(t); | |
36 | ||
37 | if (t->cb != NULL) { | |
38 | t->cb(t->opaque); | |
39 | } | |
40 | } | |
41 | ||
42 | t = t->next; | |
43 | } | |
44 | } | |
45 | ||
46 | static void ptimer_test_set_qemu_time_ns(int64_t ns) | |
47 | { | |
48 | ptimer_test_time_ns = ns; | |
49 | } | |
50 | ||
51 | static void qemu_clock_step(uint64_t ns) | |
52 | { | |
53 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); | |
54 | int64_t advanced_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns; | |
55 | ||
56 | while (deadline != -1 && deadline <= advanced_time) { | |
57 | ptimer_test_set_qemu_time_ns(deadline); | |
58 | ptimer_test_expire_qemu_timers(deadline, QEMU_CLOCK_VIRTUAL); | |
59 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); | |
60 | } | |
61 | ||
62 | ptimer_test_set_qemu_time_ns(advanced_time); | |
63 | } | |
64 | ||
65 | static void check_set_count(gconstpointer arg) | |
66 | { | |
67 | const uint8_t *policy = arg; | |
68 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
69 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
70 | ||
71 | triggered = false; | |
72 | ||
73 | ptimer_set_count(ptimer, 1000); | |
74 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 1000); | |
75 | g_assert_false(triggered); | |
76 | } | |
77 | ||
78 | static void check_set_limit(gconstpointer arg) | |
79 | { | |
80 | const uint8_t *policy = arg; | |
81 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
82 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
83 | ||
84 | triggered = false; | |
85 | ||
86 | ptimer_set_limit(ptimer, 1000, 0); | |
87 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
88 | g_assert_cmpuint(ptimer_get_limit(ptimer), ==, 1000); | |
89 | g_assert_false(triggered); | |
90 | ||
91 | ptimer_set_limit(ptimer, 2000, 1); | |
92 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 2000); | |
93 | g_assert_cmpuint(ptimer_get_limit(ptimer), ==, 2000); | |
94 | g_assert_false(triggered); | |
95 | } | |
96 | ||
97 | static void check_oneshot(gconstpointer arg) | |
98 | { | |
99 | const uint8_t *policy = arg; | |
100 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
101 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
102 | ||
103 | triggered = false; | |
104 | ||
105 | ptimer_set_period(ptimer, 2000000); | |
106 | ptimer_set_count(ptimer, 10); | |
107 | ptimer_run(ptimer, 1); | |
108 | ||
109 | qemu_clock_step(2000000 * 2 + 100000); | |
110 | ||
111 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 7); | |
112 | g_assert_false(triggered); | |
113 | ||
114 | ptimer_stop(ptimer); | |
115 | ||
116 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 7); | |
117 | g_assert_false(triggered); | |
118 | ||
119 | qemu_clock_step(2000000 * 11); | |
120 | ||
121 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 7); | |
122 | g_assert_false(triggered); | |
123 | ||
124 | ptimer_run(ptimer, 1); | |
125 | ||
126 | qemu_clock_step(2000000 * 7 + 100000); | |
127 | ||
128 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
129 | g_assert_true(triggered); | |
130 | ||
131 | triggered = false; | |
132 | ||
133 | qemu_clock_step(2000000); | |
134 | ||
135 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
136 | g_assert_false(triggered); | |
137 | ||
138 | qemu_clock_step(4000000); | |
139 | ||
140 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
141 | g_assert_false(triggered); | |
142 | ||
143 | ptimer_set_count(ptimer, 10); | |
144 | ||
145 | qemu_clock_step(20000000 + 100000); | |
146 | ||
147 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 10); | |
148 | g_assert_false(triggered); | |
149 | ||
150 | ptimer_set_limit(ptimer, 9, 1); | |
151 | ||
152 | qemu_clock_step(20000000 + 100000); | |
153 | ||
154 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 9); | |
155 | g_assert_false(triggered); | |
156 | ||
157 | ptimer_run(ptimer, 1); | |
158 | ||
159 | qemu_clock_step(2000000 + 100000); | |
160 | ||
161 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 7); | |
162 | g_assert_false(triggered); | |
163 | ||
164 | ptimer_set_count(ptimer, 20); | |
165 | ||
166 | qemu_clock_step(2000000 * 19 + 100000); | |
167 | ||
168 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
169 | g_assert_false(triggered); | |
170 | ||
171 | qemu_clock_step(2000000); | |
172 | ||
173 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
174 | g_assert_true(triggered); | |
175 | ||
176 | ptimer_stop(ptimer); | |
177 | ||
178 | triggered = false; | |
179 | ||
180 | qemu_clock_step(2000000 * 12 + 100000); | |
181 | ||
182 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
183 | g_assert_false(triggered); | |
184 | } | |
185 | ||
186 | static void check_periodic(gconstpointer arg) | |
187 | { | |
188 | const uint8_t *policy = arg; | |
189 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
190 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
293130aa | 191 | bool wrap_policy = (*policy & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD); |
5b262bb6 DO |
192 | |
193 | triggered = false; | |
194 | ||
195 | ptimer_set_period(ptimer, 2000000); | |
196 | ptimer_set_limit(ptimer, 10, 1); | |
197 | ptimer_run(ptimer, 0); | |
198 | ||
293130aa DO |
199 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 10); |
200 | g_assert_false(triggered); | |
201 | ||
202 | qemu_clock_step(100000); | |
5b262bb6 DO |
203 | |
204 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 9); | |
293130aa DO |
205 | g_assert_false(triggered); |
206 | ||
207 | qemu_clock_step(2000000 * 10 - 100000); | |
208 | ||
209 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 0 : 10); | |
210 | g_assert_true(triggered); | |
211 | ||
212 | qemu_clock_step(100000); | |
213 | ||
214 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 0 : 9); | |
5b262bb6 DO |
215 | g_assert_true(triggered); |
216 | ||
217 | triggered = false; | |
218 | ||
219 | qemu_clock_step(2000000); | |
220 | ||
293130aa | 221 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 9 : 8); |
5b262bb6 DO |
222 | g_assert_false(triggered); |
223 | ||
224 | ptimer_set_count(ptimer, 20); | |
225 | ||
293130aa DO |
226 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 20); |
227 | g_assert_false(triggered); | |
228 | ||
229 | qemu_clock_step(100000); | |
230 | ||
231 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 19); | |
232 | g_assert_false(triggered); | |
233 | ||
5b262bb6 DO |
234 | qemu_clock_step(2000000 * 11 + 100000); |
235 | ||
236 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 8); | |
237 | g_assert_false(triggered); | |
238 | ||
239 | qemu_clock_step(2000000 * 10); | |
240 | ||
293130aa DO |
241 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 9 : 8); |
242 | g_assert_true(triggered); | |
243 | ||
244 | triggered = false; | |
245 | ||
246 | ptimer_set_count(ptimer, 3); | |
247 | ||
248 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 3); | |
249 | g_assert_false(triggered); | |
250 | ||
251 | qemu_clock_step(100000); | |
252 | ||
253 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 2); | |
254 | g_assert_false(triggered); | |
255 | ||
256 | qemu_clock_step(2000000 * 4); | |
257 | ||
258 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 9 : 8); | |
5b262bb6 DO |
259 | g_assert_true(triggered); |
260 | ||
261 | ptimer_stop(ptimer); | |
262 | triggered = false; | |
263 | ||
264 | qemu_clock_step(2000000); | |
265 | ||
293130aa | 266 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 9 : 8); |
5b262bb6 DO |
267 | g_assert_false(triggered); |
268 | ||
269 | ptimer_set_count(ptimer, 3); | |
270 | ptimer_run(ptimer, 0); | |
271 | ||
272 | qemu_clock_step(2000000 * 3 + 100000); | |
273 | ||
293130aa | 274 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 0 : 9); |
5b262bb6 DO |
275 | g_assert_true(triggered); |
276 | ||
277 | triggered = false; | |
278 | ||
279 | qemu_clock_step(2000000); | |
280 | ||
293130aa | 281 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 9 : 8); |
5b262bb6 DO |
282 | g_assert_false(triggered); |
283 | ||
284 | ptimer_set_count(ptimer, 0); | |
285 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 10); | |
286 | g_assert_true(triggered); | |
287 | ||
288 | triggered = false; | |
289 | ||
293130aa | 290 | qemu_clock_step(100000); |
5b262bb6 | 291 | |
293130aa DO |
292 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 9); |
293 | g_assert_false(triggered); | |
294 | ||
295 | qemu_clock_step(2000000 * 12); | |
296 | ||
297 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 8 : 7); | |
5b262bb6 DO |
298 | g_assert_true(triggered); |
299 | ||
300 | ptimer_stop(ptimer); | |
301 | ||
302 | triggered = false; | |
303 | ||
293130aa | 304 | qemu_clock_step(2000000 * 10); |
5b262bb6 | 305 | |
293130aa | 306 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 8 : 7); |
5b262bb6 DO |
307 | g_assert_false(triggered); |
308 | ||
309 | ptimer_run(ptimer, 0); | |
310 | ptimer_set_period(ptimer, 0); | |
311 | ||
312 | qemu_clock_step(2000000 + 100000); | |
313 | ||
293130aa | 314 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 8 : 7); |
5b262bb6 DO |
315 | g_assert_false(triggered); |
316 | } | |
317 | ||
318 | static void check_on_the_fly_mode_change(gconstpointer arg) | |
319 | { | |
320 | const uint8_t *policy = arg; | |
321 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
322 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
293130aa | 323 | bool wrap_policy = (*policy & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD); |
5b262bb6 DO |
324 | |
325 | triggered = false; | |
326 | ||
327 | ptimer_set_period(ptimer, 2000000); | |
328 | ptimer_set_limit(ptimer, 10, 1); | |
329 | ptimer_run(ptimer, 1); | |
330 | ||
331 | qemu_clock_step(2000000 * 9 + 100000); | |
332 | ||
293130aa DO |
333 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); |
334 | g_assert_false(triggered); | |
335 | ||
5b262bb6 DO |
336 | ptimer_run(ptimer, 0); |
337 | ||
338 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
339 | g_assert_false(triggered); | |
340 | ||
341 | qemu_clock_step(2000000); | |
342 | ||
293130aa | 343 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 0 : 9); |
5b262bb6 DO |
344 | g_assert_true(triggered); |
345 | ||
346 | triggered = false; | |
347 | ||
348 | qemu_clock_step(2000000 * 9); | |
349 | ||
350 | ptimer_run(ptimer, 1); | |
351 | ||
293130aa | 352 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 1 : 0); |
5b262bb6 DO |
353 | g_assert_false(triggered); |
354 | ||
355 | qemu_clock_step(2000000 * 3); | |
356 | ||
357 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
358 | g_assert_true(triggered); | |
359 | } | |
360 | ||
361 | static void check_on_the_fly_period_change(gconstpointer arg) | |
362 | { | |
363 | const uint8_t *policy = arg; | |
364 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
365 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
366 | ||
367 | triggered = false; | |
368 | ||
369 | ptimer_set_period(ptimer, 2000000); | |
370 | ptimer_set_limit(ptimer, 8, 1); | |
371 | ptimer_run(ptimer, 1); | |
372 | ||
373 | qemu_clock_step(2000000 * 4 + 100000); | |
374 | ||
375 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 3); | |
376 | g_assert_false(triggered); | |
377 | ||
378 | ptimer_set_period(ptimer, 4000000); | |
379 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 3); | |
380 | ||
381 | qemu_clock_step(4000000 * 2 + 100000); | |
382 | ||
383 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
384 | g_assert_false(triggered); | |
385 | ||
386 | qemu_clock_step(4000000 * 2); | |
387 | ||
388 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
389 | g_assert_true(triggered); | |
390 | } | |
391 | ||
392 | static void check_on_the_fly_freq_change(gconstpointer arg) | |
393 | { | |
394 | const uint8_t *policy = arg; | |
395 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
396 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
397 | ||
398 | triggered = false; | |
399 | ||
400 | ptimer_set_freq(ptimer, 500); | |
401 | ptimer_set_limit(ptimer, 8, 1); | |
402 | ptimer_run(ptimer, 1); | |
403 | ||
404 | qemu_clock_step(2000000 * 4 + 100000); | |
405 | ||
406 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 3); | |
407 | g_assert_false(triggered); | |
408 | ||
409 | ptimer_set_freq(ptimer, 250); | |
410 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 3); | |
411 | ||
412 | qemu_clock_step(2000000 * 4 + 100000); | |
413 | ||
414 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
415 | g_assert_false(triggered); | |
416 | ||
417 | qemu_clock_step(2000000 * 4); | |
418 | ||
419 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
420 | g_assert_true(triggered); | |
421 | } | |
422 | ||
423 | static void check_run_with_period_0(gconstpointer arg) | |
424 | { | |
425 | const uint8_t *policy = arg; | |
426 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
427 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
428 | ||
429 | triggered = false; | |
430 | ||
431 | ptimer_set_count(ptimer, 99); | |
432 | ptimer_run(ptimer, 1); | |
433 | ||
434 | qemu_clock_step(10 * NANOSECONDS_PER_SECOND); | |
435 | ||
436 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 99); | |
437 | g_assert_false(triggered); | |
438 | } | |
439 | ||
440 | static void check_run_with_delta_0(gconstpointer arg) | |
441 | { | |
442 | const uint8_t *policy = arg; | |
443 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
444 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
293130aa | 445 | bool wrap_policy = (*policy & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD); |
5b262bb6 DO |
446 | |
447 | triggered = false; | |
448 | ||
449 | ptimer_set_period(ptimer, 2000000); | |
450 | ptimer_set_limit(ptimer, 99, 0); | |
451 | ptimer_run(ptimer, 1); | |
452 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 99); | |
453 | g_assert_true(triggered); | |
454 | ||
455 | triggered = false; | |
456 | ||
457 | qemu_clock_step(2000000 + 100000); | |
458 | ||
459 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 97); | |
460 | g_assert_false(triggered); | |
461 | ||
462 | qemu_clock_step(2000000 * 97); | |
463 | ||
464 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
465 | g_assert_false(triggered); | |
466 | ||
467 | qemu_clock_step(2000000 * 2); | |
468 | ||
469 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
470 | g_assert_true(triggered); | |
471 | ||
472 | triggered = false; | |
473 | ||
474 | ptimer_set_count(ptimer, 0); | |
475 | ptimer_run(ptimer, 0); | |
476 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 99); | |
477 | g_assert_true(triggered); | |
478 | ||
479 | triggered = false; | |
480 | ||
293130aa DO |
481 | qemu_clock_step(100000); |
482 | ||
483 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 98); | |
484 | g_assert_false(triggered); | |
485 | ||
486 | triggered = false; | |
487 | ||
488 | qemu_clock_step(2000000); | |
5b262bb6 DO |
489 | |
490 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 97); | |
491 | g_assert_false(triggered); | |
492 | ||
493 | qemu_clock_step(2000000 * 98); | |
494 | ||
293130aa | 495 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, wrap_policy ? 0 : 98); |
5b262bb6 DO |
496 | g_assert_true(triggered); |
497 | ||
498 | ptimer_stop(ptimer); | |
499 | } | |
500 | ||
501 | static void check_periodic_with_load_0(gconstpointer arg) | |
502 | { | |
503 | const uint8_t *policy = arg; | |
504 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
505 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
2e74583b | 506 | bool continuous_trigger = (*policy & PTIMER_POLICY_CONTINUOUS_TRIGGER); |
5b262bb6 DO |
507 | |
508 | triggered = false; | |
509 | ||
510 | ptimer_set_period(ptimer, 2000000); | |
511 | ptimer_run(ptimer, 0); | |
512 | ||
513 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
514 | g_assert_true(triggered); | |
515 | ||
516 | triggered = false; | |
517 | ||
518 | qemu_clock_step(2000000 + 100000); | |
519 | ||
520 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
2e74583b DO |
521 | |
522 | if (continuous_trigger) { | |
523 | g_assert_true(triggered); | |
524 | } else { | |
525 | g_assert_false(triggered); | |
526 | } | |
5b262bb6 | 527 | |
293130aa DO |
528 | triggered = false; |
529 | ||
530 | ptimer_set_count(ptimer, 10); | |
531 | ptimer_run(ptimer, 0); | |
532 | ||
533 | qemu_clock_step(2000000 * 10 + 100000); | |
534 | ||
535 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
536 | g_assert_true(triggered); | |
537 | ||
538 | triggered = false; | |
539 | ||
540 | qemu_clock_step(2000000 + 100000); | |
541 | ||
542 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
2e74583b DO |
543 | |
544 | if (continuous_trigger) { | |
545 | g_assert_true(triggered); | |
546 | } else { | |
547 | g_assert_false(triggered); | |
548 | } | |
293130aa | 549 | |
5b262bb6 DO |
550 | ptimer_stop(ptimer); |
551 | } | |
552 | ||
553 | static void check_oneshot_with_load_0(gconstpointer arg) | |
554 | { | |
555 | const uint8_t *policy = arg; | |
556 | QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL); | |
557 | ptimer_state *ptimer = ptimer_init(bh, *policy); | |
558 | ||
559 | triggered = false; | |
560 | ||
561 | ptimer_set_period(ptimer, 2000000); | |
562 | ptimer_run(ptimer, 1); | |
563 | ||
564 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
565 | g_assert_true(triggered); | |
566 | ||
567 | triggered = false; | |
568 | ||
569 | qemu_clock_step(2000000 + 100000); | |
570 | ||
571 | g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0); | |
572 | g_assert_false(triggered); | |
5b262bb6 DO |
573 | } |
574 | ||
575 | static void add_ptimer_tests(uint8_t policy) | |
576 | { | |
577 | uint8_t *ppolicy = g_malloc(1); | |
293130aa | 578 | char *policy_name = g_malloc0(256); |
5b262bb6 DO |
579 | |
580 | *ppolicy = policy; | |
581 | ||
582 | if (policy == PTIMER_POLICY_DEFAULT) { | |
583 | g_sprintf(policy_name, "default"); | |
584 | } | |
585 | ||
293130aa DO |
586 | if (policy & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) { |
587 | g_strlcat(policy_name, "wrap_after_one_period,", 256); | |
588 | } | |
589 | ||
2e74583b DO |
590 | if (policy & PTIMER_POLICY_CONTINUOUS_TRIGGER) { |
591 | g_strlcat(policy_name, "continuous_trigger,", 256); | |
592 | } | |
593 | ||
24b94625 | 594 | g_test_add_data_func( |
5b262bb6 DO |
595 | g_strdup_printf("/ptimer/set_count policy=%s", policy_name), |
596 | ppolicy, check_set_count); | |
597 | ||
24b94625 | 598 | g_test_add_data_func( |
5b262bb6 DO |
599 | g_strdup_printf("/ptimer/set_limit policy=%s", policy_name), |
600 | ppolicy, check_set_limit); | |
601 | ||
24b94625 | 602 | g_test_add_data_func( |
5b262bb6 DO |
603 | g_strdup_printf("/ptimer/oneshot policy=%s", policy_name), |
604 | ppolicy, check_oneshot); | |
605 | ||
24b94625 | 606 | g_test_add_data_func( |
5b262bb6 DO |
607 | g_strdup_printf("/ptimer/periodic policy=%s", policy_name), |
608 | ppolicy, check_periodic); | |
609 | ||
24b94625 | 610 | g_test_add_data_func( |
5b262bb6 DO |
611 | g_strdup_printf("/ptimer/on_the_fly_mode_change policy=%s", policy_name), |
612 | ppolicy, check_on_the_fly_mode_change); | |
613 | ||
24b94625 | 614 | g_test_add_data_func( |
5b262bb6 DO |
615 | g_strdup_printf("/ptimer/on_the_fly_period_change policy=%s", policy_name), |
616 | ppolicy, check_on_the_fly_period_change); | |
617 | ||
24b94625 | 618 | g_test_add_data_func( |
5b262bb6 DO |
619 | g_strdup_printf("/ptimer/on_the_fly_freq_change policy=%s", policy_name), |
620 | ppolicy, check_on_the_fly_freq_change); | |
621 | ||
24b94625 | 622 | g_test_add_data_func( |
5b262bb6 DO |
623 | g_strdup_printf("/ptimer/run_with_period_0 policy=%s", policy_name), |
624 | ppolicy, check_run_with_period_0); | |
625 | ||
24b94625 | 626 | g_test_add_data_func( |
5b262bb6 DO |
627 | g_strdup_printf("/ptimer/run_with_delta_0 policy=%s", policy_name), |
628 | ppolicy, check_run_with_delta_0); | |
629 | ||
24b94625 | 630 | g_test_add_data_func( |
5b262bb6 DO |
631 | g_strdup_printf("/ptimer/periodic_with_load_0 policy=%s", policy_name), |
632 | ppolicy, check_periodic_with_load_0); | |
633 | ||
24b94625 | 634 | g_test_add_data_func( |
5b262bb6 DO |
635 | g_strdup_printf("/ptimer/oneshot_with_load_0 policy=%s", policy_name), |
636 | ppolicy, check_oneshot_with_load_0); | |
637 | } | |
638 | ||
293130aa DO |
639 | static void add_all_ptimer_policies_comb_tests(void) |
640 | { | |
2e74583b | 641 | int last_policy = PTIMER_POLICY_CONTINUOUS_TRIGGER; |
293130aa DO |
642 | int policy = PTIMER_POLICY_DEFAULT; |
643 | ||
644 | for (; policy < (last_policy << 1); policy++) { | |
645 | add_ptimer_tests(policy); | |
646 | } | |
647 | } | |
648 | ||
5b262bb6 DO |
649 | int main(int argc, char **argv) |
650 | { | |
651 | int i; | |
652 | ||
653 | g_test_init(&argc, &argv, NULL); | |
654 | ||
655 | for (i = 0; i < QEMU_CLOCK_MAX; i++) { | |
656 | main_loop_tlg.tl[i] = g_new0(QEMUTimerList, 1); | |
657 | } | |
658 | ||
293130aa | 659 | add_all_ptimer_policies_comb_tests(); |
5b262bb6 DO |
660 | |
661 | qtest_allowed = true; | |
662 | ||
663 | return g_test_run(); | |
664 | } |