io failtest timer tools: fallout from time changes.
[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 static struct timeabs timeabs_from_nsec(unsigned long long nsec)
7 {
8         struct timeabs epoch = { { 0, 0 } };
9         return timeabs_add(epoch, time_from_nsec(nsec));
10 }
11
12 int main(void)
13 {
14         struct timers timers;
15         struct timer t[64];
16         struct list_head expired;
17         struct timeabs earliest;
18         uint64_t i;
19         struct timeabs epoch = { { 0, 0 } };
20
21         /* This is how many tests you plan to run */
22         plan_tests(488);
23
24         timers_init(&timers, epoch);
25         ok1(timers_check(&timers, NULL));
26         ok1(!timer_earliest(&timers, &earliest));
27
28         timer_add(&timers, &t[0], timeabs_from_nsec(1));
29         ok1(timers_check(&timers, NULL));
30         ok1(timer_earliest(&timers, &earliest));
31         ok1(timeabs_eq(earliest, grains_to_time(t[0].time)));
32         timer_del(&timers, &t[0]);
33         ok1(timers_check(&timers, NULL));
34         ok1(!timer_earliest(&timers, &earliest));
35
36         /* Check timer ordering. */
37         for (i = 0; i < 32; i++) {
38                 timer_add(&timers, &t[i*2], timeabs_from_nsec(1ULL << i));
39                 ok1(timers_check(&timers, NULL));
40                 timer_add(&timers, &t[i*2+1], timeabs_from_nsec((1ULL << i) + 1));
41                 ok1(timers_check(&timers, NULL));
42         }
43
44         for (i = 0; i < 32; i++) {
45                 const struct timer *t1, *t2;
46
47                 t1 = get_first(&timers);
48                 ok1(t1 == &t[i*2] || t1 == &t[i*2+1]);
49                 timer_del(&timers, (struct timer *)t1);
50                 ok1(timers_check(&timers, NULL));
51
52                 t2 = get_first(&timers);
53                 ok1(t2 != t1 && (t2 == &t[i*2] || t2 == &t[i*2+1]));
54                 timer_del(&timers, (struct timer *)t2);
55                 ok1(timers_check(&timers, NULL));
56         }
57
58         /* Check expiry. */
59         for (i = 0; i < 32; i++) {
60                 uint64_t exp = (uint64_t)TIMER_GRANULARITY << i;
61
62                 timer_add(&timers, &t[i*2], timeabs_from_nsec(exp));
63                 ok1(timers_check(&timers, NULL));
64                 timer_add(&timers, &t[i*2+1], timeabs_from_nsec(exp + 1));
65                 ok1(timers_check(&timers, NULL));
66         }
67
68         for (i = 0; i < 32; i++) {
69                 struct timer *t1, *t2;
70
71                 ok1(timer_earliest(&timers, &earliest));
72                 timers_expire(&timers, earliest, &expired);
73
74                 t1 = list_pop(&expired, struct timer, list);
75                 ok1(t1);
76                 t2 = list_pop(&expired, struct timer, list);
77                 ok1(t2);
78                 ok1(list_empty(&expired));
79
80                 ok1(t1 == &t[i*2] || t1 == &t[i*2+1]);
81                 ok1(t2 != t1 && (t2 == &t[i*2] || t2 == &t[i*2+1]));
82                 ok1(timers_check(&timers, NULL));
83         }
84
85         ok1(!timer_earliest(&timers, &earliest));
86
87         timers_cleanup(&timers);
88
89         /* This exits depending on whether all tests passed */
90         return exit_status();
91 }