7f4366005b693ab66318613ae5c6537fde0ac1a2
[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(58);
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_msec / time_to_msec */
55         t3 = time_from_msec(500);
56         ok1(t3.tv_sec == 0);
57         ok1(t3.tv_nsec == 500000000);
58         ok1(time_to_msec(t3) == 500);
59
60         t3 = time_from_msec(1000);
61         ok1(t3.tv_sec == 1);
62         ok1(t3.tv_nsec == 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_nsec == 500000000);
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_nsec == 500000000);
74         ok1(time_to_usec(t3) == 500000);
75
76         t3 = time_from_usec(1000000);
77         ok1(t3.tv_sec == 1);
78         ok1(t3.tv_nsec == 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_nsec == 500000000);
84         ok1(time_to_usec(t3) == 1500000);
85
86         /* time_from_nsec */
87         t3 = time_from_nsec(500000000);
88         ok1(t3.tv_sec == 0);
89         ok1(t3.tv_nsec == 500000000);
90         ok1(time_to_nsec(t3) == 500000000);
91
92         t3 = time_from_nsec(1000000000);
93         ok1(t3.tv_sec == 1);
94         ok1(t3.tv_nsec == 0);
95         ok1(time_to_nsec(t3) == 1000000000);
96
97         t3 = time_from_nsec(1500000000);
98         ok1(t3.tv_sec == 1);
99         ok1(t3.tv_nsec == 500000000);
100         ok1(time_to_nsec(t3) == 1500000000);
101
102         /* Test wrapunder */
103         t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500));
104         ok1(t3.tv_sec == t2.tv_sec - 1);
105         ok1(t3.tv_nsec == t2.tv_nsec);
106
107         /* time_divide and time_multiply */
108         t1.tv_nsec = 100;
109         t1.tv_sec = 100;
110
111         t3 = time_divide(t1, 2);
112         ok1(t3.tv_sec == 50);
113         ok1(t3.tv_nsec == 50);
114
115         t3 = time_divide(t1, 100);
116         ok1(t3.tv_sec == 1);
117         ok1(t3.tv_nsec == 1);
118
119         t3 = time_multiply(t3, 100);
120         ok1(time_eq(t3, t1));
121
122         t3 = time_divide(t1, 200);
123         ok1(t3.tv_sec == 0);
124         ok1(t3.tv_nsec == 500000000);
125
126         /* Divide by huge number. */
127         t1.tv_sec = (1U << 31) - 1;
128         t1.tv_nsec = 999999999;
129         t2 = time_divide(t1, 1 << 30);
130         /* Allow us to round either way. */
131         ok1((t2.tv_sec == 2 && t2.tv_nsec == 0)
132             || (t2.tv_sec == 1 && t2.tv_nsec == 999999999));
133
134         /* Multiply by huge number. */
135         t1.tv_sec = 0;
136         t1.tv_nsec = 1;
137         t2 = time_multiply(t1, 1UL << 31);
138         ok1(t2.tv_sec == 2);
139         ok1(t2.tv_nsec == 147483648);
140
141         return exit_status();
142 }