blob: fa1e6a163813c3617cb10f797f4fe92b165529fb [file] [log] [blame]
Daniel Stone5ae7e842017-03-01 11:34:00 +00001/*
2 * Copyright © 2016 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include "config.h"
27
28#include <stdlib.h>
29#include <stdint.h>
30#include <stdio.h>
31#include <string.h>
32#include <assert.h>
33#include <errno.h>
34#include <unistd.h>
Valery Karteld4512f62017-08-09 14:27:29 +030035#include <time.h>
Daniel Stone5ae7e842017-03-01 11:34:00 +000036
Pekka Paalanenc232f8d2019-04-05 16:09:45 +030037#include "shared/timespec-util.h"
Daniel Stone5ae7e842017-03-01 11:34:00 +000038
39#include "shared/helpers.h"
40#include "zunitc/zunitc.h"
41
42ZUC_TEST(timespec_test, timespec_sub)
43{
44 struct timespec a, b, r;
45
46 a.tv_sec = 1;
47 a.tv_nsec = 1;
48 b.tv_sec = 0;
49 b.tv_nsec = 2;
50 timespec_sub(&r, &a, &b);
51 ZUC_ASSERT_EQ(r.tv_sec, 0);
52 ZUC_ASSERT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
53}
54
55ZUC_TEST(timespec_test, timespec_to_nsec)
56{
57 struct timespec a;
58
59 a.tv_sec = 4;
60 a.tv_nsec = 4;
61 ZUC_ASSERT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4);
62}
63
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +020064ZUC_TEST(timespec_test, timespec_to_usec)
65{
66 struct timespec a;
67
68 a.tv_sec = 4;
69 a.tv_nsec = 4000;
70 ZUC_ASSERT_EQ(timespec_to_usec(&a), (4000000ULL) + 4);
71}
72
Daniel Stone37ad7e32017-03-01 11:34:02 +000073ZUC_TEST(timespec_test, timespec_to_msec)
74{
75 struct timespec a;
76
77 a.tv_sec = 4;
78 a.tv_nsec = 4000000;
79 ZUC_ASSERT_EQ(timespec_to_msec(&a), (4000ULL) + 4);
80}
81
Alexandros Frantzis10d708d2017-12-13 13:27:54 +020082ZUC_TEST(timespec_test, timespec_to_proto)
83{
84 struct timespec a;
85 uint32_t tv_sec_hi;
86 uint32_t tv_sec_lo;
87 uint32_t tv_nsec;
88
89 a.tv_sec = 0;
90 a.tv_nsec = 0;
91 timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
92 ZUC_ASSERT_EQ(0, tv_sec_hi);
93 ZUC_ASSERT_EQ(0, tv_sec_lo);
94 ZUC_ASSERT_EQ(0, tv_nsec);
95
96 a.tv_sec = 1234;
97 a.tv_nsec = NSEC_PER_SEC - 1;
98 timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
99 ZUC_ASSERT_EQ(0, tv_sec_hi);
100 ZUC_ASSERT_EQ(1234, tv_sec_lo);
101 ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, tv_nsec);
102
103 a.tv_sec = (time_t)0x7000123470005678LL;
104 a.tv_nsec = 1;
105 timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
106 ZUC_ASSERT_EQ((uint64_t)a.tv_sec >> 32, tv_sec_hi);
107 ZUC_ASSERT_EQ(0x70005678, tv_sec_lo);
108 ZUC_ASSERT_EQ(1, tv_nsec);
109}
110
Daniel Stone5ae7e842017-03-01 11:34:00 +0000111ZUC_TEST(timespec_test, millihz_to_nsec)
112{
113 ZUC_ASSERT_EQ(millihz_to_nsec(60000), 16666666);
114}
115
116ZUC_TEST(timespec_test, timespec_add_nsec)
117{
118 struct timespec a, r;
119
120 a.tv_sec = 0;
121 a.tv_nsec = NSEC_PER_SEC - 1;
122 timespec_add_nsec(&r, &a, 1);
123 ZUC_ASSERT_EQ(1, r.tv_sec);
124 ZUC_ASSERT_EQ(0, r.tv_nsec);
125
126 timespec_add_nsec(&r, &a, 2);
127 ZUC_ASSERT_EQ(1, r.tv_sec);
128 ZUC_ASSERT_EQ(1, r.tv_nsec);
129
130 timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL));
131 ZUC_ASSERT_EQ(2, r.tv_sec);
132 ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, r.tv_nsec);
133
134 timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL) + 2);
135 ZUC_ASSERT_EQ(r.tv_sec, 3);
136 ZUC_ASSERT_EQ(r.tv_nsec, 1);
137
138 a.tv_sec = 1;
139 a.tv_nsec = 1;
140 timespec_add_nsec(&r, &a, -2);
141 ZUC_ASSERT_EQ(r.tv_sec, 0);
142 ZUC_ASSERT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
143
144 a.tv_nsec = 0;
145 timespec_add_nsec(&r, &a, -NSEC_PER_SEC);
146 ZUC_ASSERT_EQ(0, r.tv_sec);
147 ZUC_ASSERT_EQ(0, r.tv_nsec);
148
149 a.tv_nsec = 0;
150 timespec_add_nsec(&r, &a, -NSEC_PER_SEC + 1);
151 ZUC_ASSERT_EQ(0, r.tv_sec);
152 ZUC_ASSERT_EQ(1, r.tv_nsec);
153
154 a.tv_nsec = 50;
155 timespec_add_nsec(&r, &a, (-NSEC_PER_SEC * 10ULL));
156 ZUC_ASSERT_EQ(-9, r.tv_sec);
157 ZUC_ASSERT_EQ(50, r.tv_nsec);
158
159 r.tv_sec = 4;
160 r.tv_nsec = 0;
161 timespec_add_nsec(&r, &r, NSEC_PER_SEC + 10ULL);
162 ZUC_ASSERT_EQ(5, r.tv_sec);
163 ZUC_ASSERT_EQ(10, r.tv_nsec);
164
165 timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 3ULL) - 9ULL);
166 ZUC_ASSERT_EQ(8, r.tv_sec);
167 ZUC_ASSERT_EQ(1, r.tv_nsec);
168
169 timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 7ULL) + (NSEC_PER_SEC - 1ULL));
170 ZUC_ASSERT_EQ(16, r.tv_sec);
171 ZUC_ASSERT_EQ(0, r.tv_nsec);
172}
Daniel Stone61f6d7d2017-03-01 11:34:01 +0000173
174ZUC_TEST(timespec_test, timespec_add_msec)
175{
176 struct timespec a, r;
177
178 a.tv_sec = 1000;
179 a.tv_nsec = 1;
180 timespec_add_msec(&r, &a, 2002);
181 ZUC_ASSERT_EQ(1002, r.tv_sec);
182 ZUC_ASSERT_EQ(2000001, r.tv_nsec);
183}
Daniel Stone839b6352017-03-01 11:34:03 +0000184
185ZUC_TEST(timespec_test, timespec_sub_to_nsec)
186{
187 struct timespec a, b;
188
189 a.tv_sec = 1000;
190 a.tv_nsec = 1;
191 b.tv_sec = 1;
192 b.tv_nsec = 2;
Alexandros Frantzis8c919b42017-12-01 19:28:52 +0200193 ZUC_ASSERT_EQ((999LL * NSEC_PER_SEC) - 1, timespec_sub_to_nsec(&a, &b));
Daniel Stone839b6352017-03-01 11:34:03 +0000194}
195
196ZUC_TEST(timespec_test, timespec_sub_to_msec)
197{
198 struct timespec a, b;
199
200 a.tv_sec = 1000;
201 a.tv_nsec = 2000000L;
202 b.tv_sec = 2;
203 b.tv_nsec = 1000000L;
204 ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
205}
Alexandros Frantzise2a5f9e2017-11-27 10:54:49 +0200206
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +0200207ZUC_TEST(timespec_test, timespec_from_nsec)
208{
209 struct timespec a;
210
211 timespec_from_nsec(&a, 0);
212 ZUC_ASSERT_EQ(0, a.tv_sec);
213 ZUC_ASSERT_EQ(0, a.tv_nsec);
214
215 timespec_from_nsec(&a, NSEC_PER_SEC - 1);
216 ZUC_ASSERT_EQ(0, a.tv_sec);
217 ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, a.tv_nsec);
218
219 timespec_from_nsec(&a, NSEC_PER_SEC);
220 ZUC_ASSERT_EQ(1, a.tv_sec);
221 ZUC_ASSERT_EQ(0, a.tv_nsec);
222
Alexandros Frantzis8c919b42017-12-01 19:28:52 +0200223 timespec_from_nsec(&a, (5LL * NSEC_PER_SEC) + 1);
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +0200224 ZUC_ASSERT_EQ(5, a.tv_sec);
225 ZUC_ASSERT_EQ(1, a.tv_nsec);
226}
227
228ZUC_TEST(timespec_test, timespec_from_usec)
229{
230 struct timespec a;
231
232 timespec_from_usec(&a, 0);
233 ZUC_ASSERT_EQ(0, a.tv_sec);
234 ZUC_ASSERT_EQ(0, a.tv_nsec);
235
236 timespec_from_usec(&a, 999999);
237 ZUC_ASSERT_EQ(0, a.tv_sec);
238 ZUC_ASSERT_EQ(999999 * 1000, a.tv_nsec);
239
240 timespec_from_usec(&a, 1000000);
241 ZUC_ASSERT_EQ(1, a.tv_sec);
242 ZUC_ASSERT_EQ(0, a.tv_nsec);
243
244 timespec_from_usec(&a, 5000001);
245 ZUC_ASSERT_EQ(5, a.tv_sec);
246 ZUC_ASSERT_EQ(1000, a.tv_nsec);
247}
248
249ZUC_TEST(timespec_test, timespec_from_msec)
250{
251 struct timespec a;
252
253 timespec_from_msec(&a, 0);
254 ZUC_ASSERT_EQ(0, a.tv_sec);
255 ZUC_ASSERT_EQ(0, a.tv_nsec);
256
257 timespec_from_msec(&a, 999);
258 ZUC_ASSERT_EQ(0, a.tv_sec);
259 ZUC_ASSERT_EQ(999 * 1000000, a.tv_nsec);
260
261 timespec_from_msec(&a, 1000);
262 ZUC_ASSERT_EQ(1, a.tv_sec);
263 ZUC_ASSERT_EQ(0, a.tv_nsec);
264
265 timespec_from_msec(&a, 5001);
266 ZUC_ASSERT_EQ(5, a.tv_sec);
267 ZUC_ASSERT_EQ(1000000, a.tv_nsec);
268}
269
Alexandros Frantzis787fa612017-12-13 13:27:53 +0200270ZUC_TEST(timespec_test, timespec_from_proto)
271{
272 struct timespec a;
273
274 timespec_from_proto(&a, 0, 0, 0);
275 ZUC_ASSERT_EQ(0, a.tv_sec);
276 ZUC_ASSERT_EQ(0, a.tv_nsec);
277
278 timespec_from_proto(&a, 0, 1234, 9999);
279 ZUC_ASSERT_EQ(1234, a.tv_sec);
280 ZUC_ASSERT_EQ(9999, a.tv_nsec);
281
282 timespec_from_proto(&a, 0x1234, 0x5678, 1);
283 ZUC_ASSERT_EQ((time_t)0x0000123400005678LL, a.tv_sec);
284 ZUC_ASSERT_EQ(1, a.tv_nsec);
285}
286
Alexandros Frantzise2a5f9e2017-11-27 10:54:49 +0200287ZUC_TEST(timespec_test, timespec_is_zero)
288{
289 struct timespec zero = { 0 };
290 struct timespec non_zero_sec = { .tv_sec = 1, .tv_nsec = 0 };
291 struct timespec non_zero_nsec = { .tv_sec = 0, .tv_nsec = 1 };
292
293 ZUC_ASSERT_TRUE(timespec_is_zero(&zero));
294 ZUC_ASSERT_FALSE(timespec_is_zero(&non_zero_nsec));
295 ZUC_ASSERT_FALSE(timespec_is_zero(&non_zero_sec));
296}
Alexandros Frantzis7a93bb22018-02-16 18:44:14 +0200297
298ZUC_TEST(timespec_test, timespec_eq)
299{
300 struct timespec a = { .tv_sec = 2, .tv_nsec = 1 };
301 struct timespec b = { .tv_sec = -1, .tv_nsec = 2 };
302
303 ZUC_ASSERT_TRUE(timespec_eq(&a, &a));
304 ZUC_ASSERT_TRUE(timespec_eq(&b, &b));
305
306 ZUC_ASSERT_FALSE(timespec_eq(&a, &b));
307 ZUC_ASSERT_FALSE(timespec_eq(&b, &a));
308}