]> git.ozlabs.org Git - ccan/blob - ccan/timer/test/run-add.c
timers: implementation of lazily-ordered timers.
[ccan] / ccan / timer / test / run-add.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 /* More than 32 bits */
7 #define MAX_ORD 34
8
9 /* 0...17, 63, 64, 65, 127, 128, 129, 255, 256, 257, ... */
10 static uint64_t next(uint64_t base)
11 {
12         if (base > 16 && ((base - 1) & ((base - 1) >> 1)) == 0)
13                 return base * 2 - 3;
14         return base+1;
15 }
16
17 int main(void)
18 {
19         struct timers timers;
20         struct timer t;
21         uint64_t diff;
22         unsigned int i;
23
24         /* This is how many tests you plan to run */
25         plan_tests(2 + (18 + (MAX_ORD - 4) * 3) * (18 + (MAX_ORD - 4) * 3));
26
27         timers_init(&timers, time_from_nsec(0));
28         ok1(timers_check(&timers, NULL));
29
30         for (i = 0; i < 4; i++)
31                 add_level(&timers, i);
32
33         i = 0;
34         for (diff = 0; diff < (1ULL << MAX_ORD)+2; diff = next(diff)) {
35                 i++;
36                 for (timers.base = 0;
37                      timers.base < (1ULL << MAX_ORD)+2;
38                      timers.base = next(timers.base)) {
39                         t.time = timers.base + diff;
40                         timer_add_raw(&timers, &t);
41                         ok1(timers_check(&timers, NULL));
42                         timer_del(&timers, &t);
43                 }
44         }
45
46         ok1(timers_check(&timers, NULL));
47
48         timers_cleanup(&timers);
49
50         /* This exits depending on whether all tests passed */
51         return exit_status();
52 }