2946ba10d653b8a1b9731d6bbed04dc8ace61d56
[ccan] / ccan / timer / test / run.c
1 #include <ccan/timer/timer.h>
2 /* Include the C files directly. */
3 #include <ccan/timer/timer.c>
4 #include <ccan/tap/tap.h>
5
6 int main(void)
7 {
8         struct timers timers;
9         struct timer t[64];
10         struct list_head expired;
11         struct timespec earliest;
12         uint64_t i;
13
14         /* This is how many tests you plan to run */
15         plan_tests(488);
16
17         timers_init(&timers, time_from_nsec(0));
18         ok1(timers_check(&timers, NULL));
19         ok1(!timer_earliest(&timers, &earliest));
20
21         timer_add(&timers, &t[0], time_from_nsec(1));
22         ok1(timers_check(&timers, NULL));
23         ok1(timer_earliest(&timers, &earliest));
24         ok1(time_eq(earliest, grains_to_time(t[0].time)));
25         timer_del(&timers, &t[0]);
26         ok1(timers_check(&timers, NULL));
27         ok1(!timer_earliest(&timers, &earliest));
28
29         /* Check timer ordering. */
30         for (i = 0; i < 32; i++) {
31                 timer_add(&timers, &t[i*2], time_from_nsec(1ULL << i));
32                 ok1(timers_check(&timers, NULL));
33                 timer_add(&timers, &t[i*2+1], time_from_nsec((1ULL << i) + 1));
34                 ok1(timers_check(&timers, NULL));
35         }
36
37         for (i = 0; i < 32; i++) {
38                 const struct timer *t1, *t2;
39
40                 t1 = get_first(&timers);
41                 ok1(t1 == &t[i*2] || t1 == &t[i*2+1]);
42                 timer_del(&timers, (struct timer *)t1);
43                 ok1(timers_check(&timers, NULL));
44
45                 t2 = get_first(&timers);
46                 ok1(t2 != t1 && (t2 == &t[i*2] || t2 == &t[i*2+1]));
47                 timer_del(&timers, (struct timer *)t2);
48                 ok1(timers_check(&timers, NULL));
49         }
50
51         /* Check expiry. */
52         for (i = 0; i < 32; i++) {
53                 uint64_t exp = (uint64_t)TIMER_GRANULARITY << i;
54
55                 timer_add(&timers, &t[i*2], time_from_nsec(exp));
56                 ok1(timers_check(&timers, NULL));
57                 timer_add(&timers, &t[i*2+1], time_from_nsec(exp + 1));
58                 ok1(timers_check(&timers, NULL));
59         }
60
61         for (i = 0; i < 32; i++) {
62                 struct timer *t1, *t2;
63
64                 ok1(timer_earliest(&timers, &earliest));
65                 timers_expire(&timers, earliest, &expired);
66
67                 t1 = list_pop(&expired, struct timer, list);
68                 ok1(t1);
69                 t2 = list_pop(&expired, struct timer, list);
70                 ok1(t2);
71                 ok1(list_empty(&expired));
72
73                 ok1(t1 == &t[i*2] || t1 == &t[i*2+1]);
74                 ok1(t2 != t1 && (t2 == &t[i*2] || t2 == &t[i*2+1]));
75                 ok1(timers_check(&timers, NULL));
76         }
77
78         ok1(!timer_earliest(&timers, &earliest));
79
80         timers_cleanup(&timers);
81
82         /* This exits depending on whether all tests passed */
83         return exit_status();
84 }