21c722134865a919175ed69c6eefd45fcb8ccb63
[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 timespec t1, t2, t3, zero = { 0, 0 };
8
9         plan_tests(61);
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_nsec >= 0);
18         t3 = time_sub(t2, t2);
19         ok1(t3.tv_sec == 0 && t3.tv_nsec == 0);
20         t3 = time_sub(t1, t1);
21         ok1(t3.tv_sec == 0 && t3.tv_nsec == 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_nsec = 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_nsec = 999999999;
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_sec / time_to_sec */
55         t3 = time_from_sec(500);
56         ok1(t3.tv_sec == 500);
57         ok1(t3.tv_nsec == 0);
58         ok1(time_to_sec(t3) == 500);
59
60         /* time_from_msec / time_to_msec */
61         t3 = time_from_msec(500);
62         ok1(t3.tv_sec == 0);
63         ok1(t3.tv_nsec == 500000000);
64         ok1(time_to_msec(t3) == 500);
65
66         t3 = time_from_msec(1000);
67         ok1(t3.tv_sec == 1);
68         ok1(t3.tv_nsec == 0);
69         ok1(time_to_msec(t3) == 1000);
70
71         t3 = time_from_msec(1500);
72         ok1(t3.tv_sec == 1);
73         ok1(t3.tv_nsec == 500000000);
74         ok1(time_to_msec(t3) == 1500);
75
76         /* time_from_usec */
77         t3 = time_from_usec(500000);
78         ok1(t3.tv_sec == 0);
79         ok1(t3.tv_nsec == 500000000);
80         ok1(time_to_usec(t3) == 500000);
81
82         t3 = time_from_usec(1000000);
83         ok1(t3.tv_sec == 1);
84         ok1(t3.tv_nsec == 0);
85         ok1(time_to_usec(t3) == 1000000);
86
87         t3 = time_from_usec(1500000);
88         ok1(t3.tv_sec == 1);
89         ok1(t3.tv_nsec == 500000000);
90         ok1(time_to_usec(t3) == 1500000);
91
92         /* time_from_nsec */
93         t3 = time_from_nsec(500000000);
94         ok1(t3.tv_sec == 0);
95         ok1(t3.tv_nsec == 500000000);
96         ok1(time_to_nsec(t3) == 500000000);
97
98         t3 = time_from_nsec(1000000000);
99         ok1(t3.tv_sec == 1);
100         ok1(t3.tv_nsec == 0);
101         ok1(time_to_nsec(t3) == 1000000000);
102
103         t3 = time_from_nsec(1500000000);
104         ok1(t3.tv_sec == 1);
105         ok1(t3.tv_nsec == 500000000);
106         ok1(time_to_nsec(t3) == 1500000000);
107
108         /* Test wrapunder */
109         t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500));
110         ok1(t3.tv_sec == t2.tv_sec - 1);
111         ok1(t3.tv_nsec == t2.tv_nsec);
112
113         /* time_divide and time_multiply */
114         t1.tv_nsec = 100;
115         t1.tv_sec = 100;
116
117         t3 = time_divide(t1, 2);
118         ok1(t3.tv_sec == 50);
119         ok1(t3.tv_nsec == 50);
120
121         t3 = time_divide(t1, 100);
122         ok1(t3.tv_sec == 1);
123         ok1(t3.tv_nsec == 1);
124
125         t3 = time_multiply(t3, 100);
126         ok1(time_eq(t3, t1));
127
128         t3 = time_divide(t1, 200);
129         ok1(t3.tv_sec == 0);
130         ok1(t3.tv_nsec == 500000000);
131
132         /* Divide by huge number. */
133         t1.tv_sec = (1U << 31) - 1;
134         t1.tv_nsec = 999999999;
135         t2 = time_divide(t1, 1 << 30);
136         /* Allow us to round either way. */
137         ok1((t2.tv_sec == 2 && t2.tv_nsec == 0)
138             || (t2.tv_sec == 1 && t2.tv_nsec == 999999999));
139
140         /* Multiply by huge number. */
141         t1.tv_sec = 0;
142         t1.tv_nsec = 1;
143         t2 = time_multiply(t1, 1UL << 31);
144         ok1(t2.tv_sec == 2);
145         ok1(t2.tv_nsec == 147483648);
146
147         return exit_status();
148 }