blob: f127bceeb1b86d967de6151d4a25a49cff5a39e8 [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>
35
36#include "timespec-util.h"
37
38#include "shared/helpers.h"
39#include "zunitc/zunitc.h"
40
41ZUC_TEST(timespec_test, timespec_sub)
42{
43 struct timespec a, b, r;
44
45 a.tv_sec = 1;
46 a.tv_nsec = 1;
47 b.tv_sec = 0;
48 b.tv_nsec = 2;
49 timespec_sub(&r, &a, &b);
50 ZUC_ASSERT_EQ(r.tv_sec, 0);
51 ZUC_ASSERT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
52}
53
54ZUC_TEST(timespec_test, timespec_to_nsec)
55{
56 struct timespec a;
57
58 a.tv_sec = 4;
59 a.tv_nsec = 4;
60 ZUC_ASSERT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4);
61}
62
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +020063ZUC_TEST(timespec_test, timespec_to_usec)
64{
65 struct timespec a;
66
67 a.tv_sec = 4;
68 a.tv_nsec = 4000;
69 ZUC_ASSERT_EQ(timespec_to_usec(&a), (4000000ULL) + 4);
70}
71
Daniel Stone37ad7e32017-03-01 11:34:02 +000072ZUC_TEST(timespec_test, timespec_to_msec)
73{
74 struct timespec a;
75
76 a.tv_sec = 4;
77 a.tv_nsec = 4000000;
78 ZUC_ASSERT_EQ(timespec_to_msec(&a), (4000ULL) + 4);
79}
80
Daniel Stone5ae7e842017-03-01 11:34:00 +000081ZUC_TEST(timespec_test, millihz_to_nsec)
82{
83 ZUC_ASSERT_EQ(millihz_to_nsec(60000), 16666666);
84}
85
86ZUC_TEST(timespec_test, timespec_add_nsec)
87{
88 struct timespec a, r;
89
90 a.tv_sec = 0;
91 a.tv_nsec = NSEC_PER_SEC - 1;
92 timespec_add_nsec(&r, &a, 1);
93 ZUC_ASSERT_EQ(1, r.tv_sec);
94 ZUC_ASSERT_EQ(0, r.tv_nsec);
95
96 timespec_add_nsec(&r, &a, 2);
97 ZUC_ASSERT_EQ(1, r.tv_sec);
98 ZUC_ASSERT_EQ(1, r.tv_nsec);
99
100 timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL));
101 ZUC_ASSERT_EQ(2, r.tv_sec);
102 ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, r.tv_nsec);
103
104 timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL) + 2);
105 ZUC_ASSERT_EQ(r.tv_sec, 3);
106 ZUC_ASSERT_EQ(r.tv_nsec, 1);
107
108 a.tv_sec = 1;
109 a.tv_nsec = 1;
110 timespec_add_nsec(&r, &a, -2);
111 ZUC_ASSERT_EQ(r.tv_sec, 0);
112 ZUC_ASSERT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
113
114 a.tv_nsec = 0;
115 timespec_add_nsec(&r, &a, -NSEC_PER_SEC);
116 ZUC_ASSERT_EQ(0, r.tv_sec);
117 ZUC_ASSERT_EQ(0, r.tv_nsec);
118
119 a.tv_nsec = 0;
120 timespec_add_nsec(&r, &a, -NSEC_PER_SEC + 1);
121 ZUC_ASSERT_EQ(0, r.tv_sec);
122 ZUC_ASSERT_EQ(1, r.tv_nsec);
123
124 a.tv_nsec = 50;
125 timespec_add_nsec(&r, &a, (-NSEC_PER_SEC * 10ULL));
126 ZUC_ASSERT_EQ(-9, r.tv_sec);
127 ZUC_ASSERT_EQ(50, r.tv_nsec);
128
129 r.tv_sec = 4;
130 r.tv_nsec = 0;
131 timespec_add_nsec(&r, &r, NSEC_PER_SEC + 10ULL);
132 ZUC_ASSERT_EQ(5, r.tv_sec);
133 ZUC_ASSERT_EQ(10, r.tv_nsec);
134
135 timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 3ULL) - 9ULL);
136 ZUC_ASSERT_EQ(8, r.tv_sec);
137 ZUC_ASSERT_EQ(1, r.tv_nsec);
138
139 timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 7ULL) + (NSEC_PER_SEC - 1ULL));
140 ZUC_ASSERT_EQ(16, r.tv_sec);
141 ZUC_ASSERT_EQ(0, r.tv_nsec);
142}
Daniel Stone61f6d7d2017-03-01 11:34:01 +0000143
144ZUC_TEST(timespec_test, timespec_add_msec)
145{
146 struct timespec a, r;
147
148 a.tv_sec = 1000;
149 a.tv_nsec = 1;
150 timespec_add_msec(&r, &a, 2002);
151 ZUC_ASSERT_EQ(1002, r.tv_sec);
152 ZUC_ASSERT_EQ(2000001, r.tv_nsec);
153}
Daniel Stone839b6352017-03-01 11:34:03 +0000154
155ZUC_TEST(timespec_test, timespec_sub_to_nsec)
156{
157 struct timespec a, b;
158
159 a.tv_sec = 1000;
160 a.tv_nsec = 1;
161 b.tv_sec = 1;
162 b.tv_nsec = 2;
163 ZUC_ASSERT_EQ((999L * NSEC_PER_SEC) - 1, timespec_sub_to_nsec(&a, &b));
164}
165
166ZUC_TEST(timespec_test, timespec_sub_to_msec)
167{
168 struct timespec a, b;
169
170 a.tv_sec = 1000;
171 a.tv_nsec = 2000000L;
172 b.tv_sec = 2;
173 b.tv_nsec = 1000000L;
174 ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
175}
Alexandros Frantzise2a5f9e2017-11-27 10:54:49 +0200176
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +0200177ZUC_TEST(timespec_test, timespec_from_nsec)
178{
179 struct timespec a;
180
181 timespec_from_nsec(&a, 0);
182 ZUC_ASSERT_EQ(0, a.tv_sec);
183 ZUC_ASSERT_EQ(0, a.tv_nsec);
184
185 timespec_from_nsec(&a, NSEC_PER_SEC - 1);
186 ZUC_ASSERT_EQ(0, a.tv_sec);
187 ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, a.tv_nsec);
188
189 timespec_from_nsec(&a, NSEC_PER_SEC);
190 ZUC_ASSERT_EQ(1, a.tv_sec);
191 ZUC_ASSERT_EQ(0, a.tv_nsec);
192
193 timespec_from_nsec(&a, (5L * NSEC_PER_SEC) + 1);
194 ZUC_ASSERT_EQ(5, a.tv_sec);
195 ZUC_ASSERT_EQ(1, a.tv_nsec);
196}
197
198ZUC_TEST(timespec_test, timespec_from_usec)
199{
200 struct timespec a;
201
202 timespec_from_usec(&a, 0);
203 ZUC_ASSERT_EQ(0, a.tv_sec);
204 ZUC_ASSERT_EQ(0, a.tv_nsec);
205
206 timespec_from_usec(&a, 999999);
207 ZUC_ASSERT_EQ(0, a.tv_sec);
208 ZUC_ASSERT_EQ(999999 * 1000, a.tv_nsec);
209
210 timespec_from_usec(&a, 1000000);
211 ZUC_ASSERT_EQ(1, a.tv_sec);
212 ZUC_ASSERT_EQ(0, a.tv_nsec);
213
214 timespec_from_usec(&a, 5000001);
215 ZUC_ASSERT_EQ(5, a.tv_sec);
216 ZUC_ASSERT_EQ(1000, a.tv_nsec);
217}
218
219ZUC_TEST(timespec_test, timespec_from_msec)
220{
221 struct timespec a;
222
223 timespec_from_msec(&a, 0);
224 ZUC_ASSERT_EQ(0, a.tv_sec);
225 ZUC_ASSERT_EQ(0, a.tv_nsec);
226
227 timespec_from_msec(&a, 999);
228 ZUC_ASSERT_EQ(0, a.tv_sec);
229 ZUC_ASSERT_EQ(999 * 1000000, a.tv_nsec);
230
231 timespec_from_msec(&a, 1000);
232 ZUC_ASSERT_EQ(1, a.tv_sec);
233 ZUC_ASSERT_EQ(0, a.tv_nsec);
234
235 timespec_from_msec(&a, 5001);
236 ZUC_ASSERT_EQ(5, a.tv_sec);
237 ZUC_ASSERT_EQ(1000000, a.tv_nsec);
238}
239
Alexandros Frantzise2a5f9e2017-11-27 10:54:49 +0200240ZUC_TEST(timespec_test, timespec_is_zero)
241{
242 struct timespec zero = { 0 };
243 struct timespec non_zero_sec = { .tv_sec = 1, .tv_nsec = 0 };
244 struct timespec non_zero_nsec = { .tv_sec = 0, .tv_nsec = 1 };
245
246 ZUC_ASSERT_TRUE(timespec_is_zero(&zero));
247 ZUC_ASSERT_FALSE(timespec_is_zero(&non_zero_nsec));
248 ZUC_ASSERT_FALSE(timespec_is_zero(&non_zero_sec));
249}