timer: change to use time_mono (api break!)
[ccan] / ccan / timer / test / run.c
1 #define CCAN_TIMER_DEBUG
2 #include <ccan/timer/timer.h>
3 #include <ccan/time/time.h>
4
5 #define time_mono() fake_mono_time
6
7 static struct timemono fake_mono_time;
8
9 /* Include the C files directly. */
10 #include <ccan/timer/timer.c>
11 #include <ccan/tap/tap.h>
12
13 static struct timemono timemono_from_nsec(unsigned long long nsec)
14 {
15         struct timemono epoch = { { 0, 0 } };
16         return timemono_add(epoch, time_from_nsec(nsec));
17 }
18
19 int main(void)
20 {
21         struct timers timers;
22         struct timer t[64];
23         struct timemono earliest;
24         uint64_t i;
25         const struct timemono epoch = { { 0, 0 } };
26
27         /* This is how many tests you plan to run */
28         plan_tests(495);
29
30         timers_init(&timers, epoch);
31         ok1(timers_check(&timers, NULL));
32         ok1(!timer_earliest(&timers, &earliest));
33
34         timer_init(&t[0]);
35         /* timer_del can be called immediately after init. */
36         timer_del(&timers, &t[0]);
37
38         timer_addmono(&timers, &t[0], timemono_from_nsec(1));
39         ok1(timers_check(&timers, NULL));
40         ok1(timer_earliest(&timers, &earliest));
41         ok1(timemono_eq(earliest, grains_to_time(t[0].time)));
42         timer_del(&timers, &t[0]);
43         ok1(timers_check(&timers, NULL));
44         ok1(!timer_earliest(&timers, &earliest));
45
46         /* timer_del can be called twice, no problems. */
47         timer_del(&timers, &t[0]);
48
49         /* Check timer ordering. */
50         for (i = 0; i < 32; i++) {
51                 timer_init(&t[i*2]);
52                 timer_addmono(&timers, &t[i*2], timemono_from_nsec(1ULL << i));
53                 ok1(timers_check(&timers, NULL));
54                 timer_init(&t[i*2+1]);
55                 timer_addmono(&timers, &t[i*2+1], timemono_from_nsec((1ULL << i) + 1));
56                 ok1(timers_check(&timers, NULL));
57         }
58
59         for (i = 0; i < 32; i++) {
60                 const struct timer *t1, *t2;
61
62                 t1 = get_first(&timers);
63                 ok1(t1 == &t[i*2] || t1 == &t[i*2+1]);
64                 timer_del(&timers, (struct timer *)t1);
65                 ok1(timers_check(&timers, NULL));
66
67                 t2 = get_first(&timers);
68                 ok1(t2 != t1 && (t2 == &t[i*2] || t2 == &t[i*2+1]));
69                 timer_del(&timers, (struct timer *)t2);
70                 ok1(timers_check(&timers, NULL));
71         }
72
73         /* Check expiry. */
74         for (i = 0; i < 32; i++) {
75                 uint64_t exp = (uint64_t)TIMER_GRANULARITY << i;
76
77                 timer_addmono(&timers, &t[i*2], timemono_from_nsec(exp));
78                 ok1(timers_check(&timers, NULL));
79                 timer_addmono(&timers, &t[i*2+1], timemono_from_nsec(exp + 1));
80                 ok1(timers_check(&timers, NULL));
81         }
82
83         for (i = 0; i < 32; i++) {
84                 struct timer *t1, *t2;
85
86                 ok1(timer_earliest(&timers, &earliest));
87                 t1 = timers_expire(&timers, earliest);
88                 ok1(t1);
89                 t2 = timers_expire(&timers, earliest);
90                 ok1(t2);
91                 ok1(!timers_expire(&timers, earliest));
92
93                 ok1(t1 == &t[i*2] || t1 == &t[i*2+1]);
94                 ok1(t2 != t1 && (t2 == &t[i*2] || t2 == &t[i*2+1]));
95                 ok1(timers_check(&timers, NULL));
96         }
97
98         ok1(!timer_earliest(&timers, &earliest));
99         ok1(timers_check(&timers, NULL));
100         timers_cleanup(&timers);
101         
102         /* Relative timers. */
103         timers_init(&timers, epoch);
104         fake_mono_time = timemono_from_nsec(TIMER_GRANULARITY);
105         timer_addrel(&timers, &t[0], time_from_sec(1));
106         ok1(timer_earliest(&timers, &earliest));
107         ok1(timers_check(&timers, NULL));
108         ok1(earliest.ts.tv_sec == 1 && earliest.ts.tv_nsec == TIMER_GRANULARITY);
109         ok1(timers_expire(&timers, earliest) == &t[0]);
110         ok1(!timer_earliest(&timers, &earliest));
111         ok1(timers_check(&timers, NULL));
112         timers_cleanup(&timers);
113
114         /* This exits depending on whether all tests passed */
115         return exit_status();
116 }