1 #define CCAN_TIMER_DEBUG
2 /* Include the C files directly. */
3 #include <ccan/timer/timer.c>
4 #include <ccan/tap/tap.h>
6 struct timers_with_counters {
8 size_t num_alloc, num_free;
12 static void *test_alloc(struct timers *timers, size_t len)
14 ((struct timers_with_counters *)timers)->num_alloc++;
18 static void test_free(struct timers *timers, void *p)
21 ((struct timers_with_counters *)timers)->num_free++;
26 static struct timemono timemono_from_nsec(unsigned long long nsec)
28 struct timemono epoch = { { 0, 0 } };
29 return timemono_add(epoch, time_from_nsec(nsec));
34 struct timers_with_counters tc;
36 const struct timemono epoch = { { 0, 0 } };
38 tc.num_alloc = tc.num_free = 0;
41 timers_set_allocator(test_alloc, test_free);
42 timers_init(&tc.timers, epoch);
45 timer_addmono(&tc.timers, &t[0],
46 timemono_from_nsec(TIMER_GRANULARITY << TIMER_LEVEL_BITS));
47 timers_expire(&tc.timers, timemono_from_nsec(1));
48 ok1(tc.num_alloc == 1);
49 ok1(tc.num_free == 0);
50 timer_del(&tc.timers, &t[0]);
51 ok1(tc.num_alloc == 1);
52 ok1(tc.num_free == 0);
53 timers_cleanup(&tc.timers);
54 ok1(tc.num_alloc == 1);
55 ok1(tc.num_free == 1);
57 /* Should restore defaults */
58 timers_set_allocator(NULL, NULL);
59 ok1(timer_alloc == timer_default_alloc);
60 ok1(timer_free == timer_default_free);
62 timers_init(&tc.timers, epoch);
63 timer_addmono(&tc.timers, &t[0],
64 timemono_from_nsec(TIMER_GRANULARITY << TIMER_LEVEL_BITS));
65 ok1(tc.num_alloc == 1);
66 ok1(tc.num_free == 1);
67 timers_cleanup(&tc.timers);
68 ok1(tc.num_alloc == 1);
69 ok1(tc.num_free == 1);
71 /* This exits depending on whether all tests passed */