time: split absolute and relative times.
[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 timeabs t1, t2;
8         struct timerel t3, t4, zero = { { 0, 0 } };
9
10         plan_tests(66);
11
12         /* Test time_now */
13         t1 = time_now();
14         t2 = time_now();
15
16         /* Test time_between. */
17         t3 = time_between(t2, t1);
18         ok1(t3.ts.tv_sec > 0 || t3.ts.tv_nsec >= 0);
19         t3 = time_between(t2, t2);
20         ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 0);
21         t3 = time_between(t1, t1);
22         ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 0);
23
24         /* Test timeabs_eq / timerel_eq */
25         ok1(timeabs_eq(t1, t1));
26         ok1(timeabs_eq(t2, t2));
27         t3.ts.tv_sec = 0;
28         t3.ts.tv_nsec = 1;
29         ok1(!timerel_eq(t3, zero));
30         ok1(!timerel_eq(t3, zero));
31
32         /* Make sure t2 > t1. */
33         t2 = timeabs_add(t2, t3);
34
35         /* Test time_after and time_before. */
36         ok1(!timeabs_eq(t1, t2));
37         ok1(!time_after(t1, t2));
38         ok1(time_before(t1, t2));
39         ok1(time_after(t2, t1));
40         ok1(!time_before(t2, t1));
41         t3.ts.tv_sec = 0;
42         t3.ts.tv_nsec = 999999999;
43         t2 = timeabs_add(t2, t3);
44         ok1(!timeabs_eq(t1, t2));
45         ok1(!time_after(t1, t2));
46         ok1(time_before(t1, t2));
47         ok1(time_after(t2, t1));
48         ok1(!time_before(t2, t1));
49
50         /* Test time_less and time_greater. */
51         ok1(time_less(zero, t3));
52         ok1(!time_greater(zero, t3));
53         ok1(!time_less(t3, zero));
54         ok1(time_greater(t3, zero));
55
56         /* Test time_sub */
57         t3 = time_sub(t3, t3);
58         ok1(timerel_eq(t3, zero));
59
60         /* Test time_between */
61         t3 = time_between(t2, t2);
62         ok1(timerel_eq(t3, zero));
63         t3.ts.tv_sec = 0;
64         t3.ts.tv_nsec = 999999999;
65         t1 = timeabs_add(t2, t3);
66         t3 = time_between(t1, t2);
67         ok1(t3.ts.tv_sec == 0 && t3.ts.tv_nsec == 999999999);
68
69         /* time_from_sec / time_to_sec */
70         t3 = time_from_sec(500);
71         ok1(t3.ts.tv_sec == 500);
72         ok1(t3.ts.tv_nsec == 0);
73         ok1(time_to_sec(t3) == 500);
74
75         /* time_from_msec / time_to_msec */
76         t3 = time_from_msec(500);
77         ok1(t3.ts.tv_sec == 0);
78         ok1(t3.ts.tv_nsec == 500000000);
79         ok1(time_to_msec(t3) == 500);
80
81         t3 = time_from_msec(1000);
82         ok1(t3.ts.tv_sec == 1);
83         ok1(t3.ts.tv_nsec == 0);
84         ok1(time_to_msec(t3) == 1000);
85
86         t3 = time_from_msec(1500);
87         ok1(t3.ts.tv_sec == 1);
88         ok1(t3.ts.tv_nsec == 500000000);
89         ok1(time_to_msec(t3) == 1500);
90
91         /* time_from_usec */
92         t3 = time_from_usec(500000);
93         ok1(t3.ts.tv_sec == 0);
94         ok1(t3.ts.tv_nsec == 500000000);
95         ok1(time_to_usec(t3) == 500000);
96
97         t3 = time_from_usec(1000000);
98         ok1(t3.ts.tv_sec == 1);
99         ok1(t3.ts.tv_nsec == 0);
100         ok1(time_to_usec(t3) == 1000000);
101
102         t3 = time_from_usec(1500000);
103         ok1(t3.ts.tv_sec == 1);
104         ok1(t3.ts.tv_nsec == 500000000);
105         ok1(time_to_usec(t3) == 1500000);
106
107         /* time_from_nsec */
108         t3 = time_from_nsec(500000000);
109         ok1(t3.ts.tv_sec == 0);
110         ok1(t3.ts.tv_nsec == 500000000);
111         ok1(time_to_nsec(t3) == 500000000);
112
113         t3 = time_from_nsec(1000000000);
114         ok1(t3.ts.tv_sec == 1);
115         ok1(t3.ts.tv_nsec == 0);
116         ok1(time_to_nsec(t3) == 1000000000);
117
118         t3 = time_from_nsec(1500000000);
119         ok1(t3.ts.tv_sec == 1);
120         ok1(t3.ts.tv_nsec == 500000000);
121         ok1(time_to_nsec(t3) == 1500000000);
122
123         /* Test wrapunder */
124         t1 = timeabs_sub(timeabs_sub(t2, time_from_msec(500)), time_from_msec(500));
125         ok1(t1.ts.tv_sec == t2.ts.tv_sec - 1);
126         ok1(t1.ts.tv_nsec == t2.ts.tv_nsec);
127
128         /* time_divide and time_multiply */
129         t4.ts.tv_nsec = 100;
130         t4.ts.tv_sec = 100;
131
132         t3 = time_divide(t4, 2);
133         ok1(t3.ts.tv_sec == 50);
134         ok1(t3.ts.tv_nsec == 50);
135
136         t3 = time_divide(t4, 100);
137         ok1(t3.ts.tv_sec == 1);
138         ok1(t3.ts.tv_nsec == 1);
139
140         t3 = time_multiply(t3, 100);
141         ok1(timerel_eq(t3, t4));
142
143         t3 = time_divide(t4, 200);
144         ok1(t3.ts.tv_sec == 0);
145         ok1(t3.ts.tv_nsec == 500000000);
146
147         /* Divide by huge number. */
148         t4.ts.tv_sec = (1U << 31) - 1;
149         t4.ts.tv_nsec = 999999999;
150         t3 = time_divide(t4, 1 << 30);
151         /* Allow us to round either way. */
152         ok1((t3.ts.tv_sec == 2 && t3.ts.tv_nsec == 0)
153             || (t3.ts.tv_sec == 1 && t3.ts.tv_nsec == 999999999));
154
155         /* Multiply by huge number. */
156         t4.ts.tv_sec = 0;
157         t4.ts.tv_nsec = 1;
158         t3 = time_multiply(t4, 1UL << 31);
159         ok1(t3.ts.tv_sec == 2);
160         ok1(t3.ts.tv_nsec == 147483648);
161
162         return exit_status();
163 }