80391694cd94e04abb7552337869802986213b4f
[ccan] / ccan / time / test / run.c
1 #include <ccan/time/time.h>
2 #include <ccan/time/time.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         struct timeval t1, t2, t3, zero = { 0, 0 };
8
9         plan_tests(46);
10
11         /* Test time_now */
12         t1 = time_now();
13         t2 = time_now();
14
15         /* Test time_sub. */
16         t3 = time_sub(t2, t1);
17         ok1(t3.tv_sec > 0 || t3.tv_usec >= 0);
18         t3 = time_sub(t2, t2);
19         ok1(t3.tv_sec == 0 && t3.tv_usec == 0);
20         t3 = time_sub(t1, t1);
21         ok1(t3.tv_sec == 0 && t3.tv_usec == 0);
22
23         /* Test time_eq */
24         ok1(time_eq(t1, t1));
25         ok1(time_eq(t2, t2));
26         ok1(!time_eq(t1, t3));
27         ok1(!time_eq(t2, t3));
28
29         /* Make sure t2 > t1. */
30         t3.tv_sec = 0;
31         t3.tv_usec = 1;
32         t2 = time_add(t2, t3);
33
34         /* Test time_less and time_greater. */
35         ok1(!time_eq(t1, t2));
36         ok1(!time_greater(t1, t2));
37         ok1(time_less(t1, t2));
38         ok1(time_greater(t2, t1));
39         ok1(!time_less(t2, t1));
40         t3.tv_sec = 0;
41         t3.tv_usec = 999999;
42         t2 = time_add(t2, t3);
43         ok1(!time_eq(t1, t2));
44         ok1(!time_greater(t1, t2));
45         ok1(time_less(t1, t2));
46         ok1(time_greater(t2, t1));
47         ok1(!time_less(t2, t1));
48
49         t3 = time_sub(t2, zero);
50         ok1(time_eq(t3, t2));
51         t3 = time_sub(t2, t2);
52         ok1(time_eq(t3, zero));
53
54         /* time_from_msec / time_to_msec */
55         t3 = time_from_msec(500);
56         ok1(t3.tv_sec == 0);
57         ok1(t3.tv_usec == 500000);
58         ok1(time_to_msec(t3) == 500);
59
60         t3 = time_from_msec(1000);
61         ok1(t3.tv_sec == 1);
62         ok1(t3.tv_usec == 0);
63         ok1(time_to_msec(t3) == 1000);
64
65         t3 = time_from_msec(1500);
66         ok1(t3.tv_sec == 1);
67         ok1(t3.tv_usec == 500000);
68         ok1(time_to_msec(t3) == 1500);
69
70         /* time_from_usec */
71         t3 = time_from_usec(500000);
72         ok1(t3.tv_sec == 0);
73         ok1(t3.tv_usec == 500000);
74         ok1(time_to_usec(t3) == 500000);
75
76         t3 = time_from_usec(1000000);
77         ok1(t3.tv_sec == 1);
78         ok1(t3.tv_usec == 0);
79         ok1(time_to_usec(t3) == 1000000);
80
81         t3 = time_from_usec(1500000);
82         ok1(t3.tv_sec == 1);
83         ok1(t3.tv_usec == 500000);
84         ok1(time_to_usec(t3) == 1500000);
85
86         /* Test wrapunder */
87         t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500));
88         ok1(t3.tv_sec == t2.tv_sec - 1);
89         ok1(t3.tv_usec == t2.tv_usec);
90
91         /* time_divide and time_multiply */
92         t1.tv_usec = 100;
93         t1.tv_sec = 100;
94
95         t3 = time_divide(t1, 2);
96         ok1(t3.tv_sec == 50);
97         ok1(t3.tv_usec == 50);
98
99         t3 = time_divide(t1, 100);
100         ok1(t3.tv_sec == 1);
101         ok1(t3.tv_usec == 1);
102
103         t3 = time_multiply(t3, 100);
104         ok1(time_eq(t3, t1));
105
106         t3 = time_divide(t1, 200);
107         ok1(t3.tv_sec == 0);
108         ok1(t3.tv_usec == 500000);
109
110         return exit_status();
111 }