timer: change to use time_mono (api break!)
[ccan] / ccan / timer / test / run-corrupt.c
1 #define CCAN_TIMER_DEBUG 1
2 #include <ccan/timer/timer.h>
3 /* Include the C files directly. */
4 #include <ccan/timer/timer.c>
5 #include <ccan/tap/tap.h>
6
7 static void new_timer(struct timers *timers, unsigned long nsec)
8 {
9         struct timer *timer;
10         struct timemono when;
11
12         timer = malloc(sizeof(*timer));
13         timer_init(timer);
14         when.ts.tv_sec = 0; when.ts.tv_nsec = nsec;
15         timer_addmono(timers, timer, when);
16 }
17
18 static void update_and_expire(struct timers *timers)
19 {
20         struct timemono when;
21
22         timer_earliest(timers, &when);
23         free(timers_expire(timers, when));
24 }
25
26 int main(int argc, char *argv[])
27 {
28         struct timemono when;
29         struct timers timers;
30
31         plan_tests(7);
32         
33         when.ts.tv_sec = 0; when.ts.tv_nsec = 0;
34         timers_init(&timers, when);
35
36         /* Add these */
37         new_timer(&timers, 35000000);
38         new_timer(&timers, 38000000);
39         new_timer(&timers, 59000000);
40         new_timer(&timers, 65000000);
41         new_timer(&timers, 88000000);
42         new_timer(&timers, 125000000);
43         new_timer(&timers, 130000000);
44         new_timer(&timers, 152000000);
45         new_timer(&timers, 168000000);
46         /* Expire all but the last one. */
47         update_and_expire(&timers);
48         update_and_expire(&timers);
49         update_and_expire(&timers);
50         update_and_expire(&timers);
51         update_and_expire(&timers);
52         update_and_expire(&timers);
53         update_and_expire(&timers);
54         update_and_expire(&timers);
55         /* Add a new one. */
56         new_timer(&timers, 169000000);
57         ok1(timers_check(&timers, NULL));
58
59         /* Used to get the wrong one... */
60         timers_dump(&timers, stdout);
61         ok1(timer_earliest(&timers, &when));
62         ok1(when.ts.tv_nsec == 168000000);
63         free(timers_expire(&timers, when));
64
65         ok1(timer_earliest(&timers, &when));
66         ok1(when.ts.tv_nsec == 169000000);
67         free(timers_expire(&timers, when));
68
69         ok1(timers_check(&timers, NULL));
70         ok1(!timer_earliest(&timers, &when));
71         timers_cleanup(&timers);
72
73         return exit_status();
74 }