summaryrefslogtreecommitdiff
path: root/src/lib/tests/time_test.c
blob: 2b75b873bef443198bb74e6d9bd761ace45f728a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * Test of the time utilities
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#define _POSIX_C_SOURCE 200809L

#include <ouroboros/test.h>
#include <ouroboros/time.h>

#include <stdio.h>

static int ts_check(struct timespec * s,
                    time_t            sec,
                    time_t            nsec)
{
        return s->tv_sec == sec && s->tv_nsec == nsec;
}

static int tv_check(struct timeval * v,
                    time_t           sec,
                    time_t           usec)
{
        return v->tv_sec == sec && v->tv_usec == usec;
}


static int test_time_ts_init(void)
{
        struct timespec s  = TIMESPEC_INIT_S (100);
        struct timespec ms = TIMESPEC_INIT_MS(100);
        struct timespec us = TIMESPEC_INIT_US(100);
        struct timespec ns = TIMESPEC_INIT_NS(100);

        TEST_START();

        if (!ts_check(&s, 100, 0)) {
                printf("timespec_init_s failed.\n");
                goto fail;
        }

        if (!ts_check(&ms, 0, 100 * MILLION)) {
                printf("timespec_init_ms failed.\n");
                goto fail;
        }

        if (!ts_check(&us, 0, 100* 1000L)) {
                printf("timespec_init_us failed.\n");
                goto fail;
        }

        if (!ts_check(&ns, 0, 100)) {
                printf("timespec_init_ns failed.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_time_tv_init(void)
{
        struct timeval s  = TIMEVAL_INIT_S (100);
        struct timeval ms = TIMEVAL_INIT_MS(100);
        struct timeval us = TIMEVAL_INIT_US(100);

        TEST_START();

        if (!tv_check(&s, 100, 0)) {
                printf("timeval_init_s failed.\n");
                goto fail;
        }

        if (!tv_check(&ms, 0, 100 * 1000L)) {
                printf("timeval_init_ms failed.\n");
                goto fail;
        }

        if (!tv_check(&us, 0, 100)) {
                printf("timeval_init_us failed.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ts_diff(void)
{
        struct timespec s0  = TIMESPEC_INIT_S (100);
        struct timespec s1  = TIMESPEC_INIT_S (200);
        struct timespec ms0 = TIMESPEC_INIT_MS(100);
        struct timespec ms1 = TIMESPEC_INIT_MS(200);
        struct timespec us0 = TIMESPEC_INIT_US(100);
        struct timespec us1 = TIMESPEC_INIT_US(200);
        struct timespec ns0 = TIMESPEC_INIT_NS(100);
        struct timespec ns1 = TIMESPEC_INIT_NS(200);
        struct timespec res;

        TEST_START();

        ts_diff(&s0, &s1, &res);
        if (!ts_check(&res, -100, 0)) {
                printf("timespec_diff failed at s0 - s1.\n");
                goto fail;
        }

        ts_diff(&s1, &s0, &res);
        if (!ts_check(&res, 100, 0)) {
                printf("timespec_diff failed at s1 - s0.\n");
                goto fail;
        }

        ts_diff(&ms0, &ms1, &res);
        if (!ts_check(&res, -1, 900 * MILLION)) {
                printf("timespec_diff failed at ms0 - ms1.\n");
                goto fail;
        }

        ts_diff(&ms1, &ms0, &res);
        if (!ts_check(&res, 0, 100 * MILLION)) {
                printf("timespec_diff failed at ms1 - ms0.\n");
                goto fail;
        }

        ts_diff(&us0, &us1, &res);
        if (!ts_check(&res, -1, 999900 * 1000L)) {
                printf("timespec_diff failed at us0 - us1.\n");
                goto fail;
        }

        ts_diff(&us1, &us0, &res);
        if (!ts_check(&res, 0, 100 * 1000L)) {
                printf("timespec_diff failed at us1 - us0.\n");
                goto fail;
        }

        ts_diff(&ns0, &ns1, &res);
        if (!ts_check(&res, -1, 999999900)) {
                printf("timespec_diff failed at ns0 - ns1.\n");
                goto fail;
        }

        ts_diff(&ns1, &ns0, &res);
        if (!ts_check(&res, 0, 100)) {
                printf("timespec_diff failed at ns1 - ns0.\n");
                goto fail;
        }

        ts_diff(&s0, &ms0, &res);
        if (!ts_check(&res, 99, 900 * MILLION)) {
                printf("timespec_diff failed at s0 - ms0.\n");
                goto fail;
        }

        ts_diff(&s0, &us0, &res);
        if (!ts_check(&res, 99, 999900 * 1000L)) {
                printf("timespec_diff failed at s0 - us0.\n");
                goto fail;
        }

        ts_diff(&s0, &ns0, &res);
        if (!ts_check(&res, 99, 999999900)) {
                printf("timespec_diff failed at s0 - ns0.\n");
                goto fail;
        }

        ts_diff(&ms0, &us0, &res);
        if (!ts_check(&res, 0, 99900 * 1000L)) {
                printf("timespec_diff failed at ms0 - us0.\n");
                goto fail;
        }

        ts_diff(&ms0, &ns0, &res);
        if (!ts_check(&res, 0, 99999900)) {
                printf("timespec_diff failed at ms0 - ns0.\n");
                goto fail;
        }

        ts_diff(&us0, &ns0, &res);
        if (!ts_check(&res, 0, 99900)) {
                printf("timespec_diff failed at us0 - ns0.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tv_diff(void)
{
        struct timeval s0  = TIMEVAL_INIT_S (100);
        struct timeval s1  = TIMEVAL_INIT_S (200);
        struct timeval ms0 = TIMEVAL_INIT_MS(100);
        struct timeval ms1 = TIMEVAL_INIT_MS(200);
        struct timeval us0 = TIMEVAL_INIT_US(100);
        struct timeval us1 = TIMEVAL_INIT_US(200);
        struct timeval res;

        TEST_START();

        tv_diff(&s0, &s1, &res);
        if (!tv_check(&res, -100, 0)) {
                printf("timeval_diff failed at s0 - s1.\n");
                goto fail;
        }

        tv_diff(&s1, &s0, &res);
        if (!tv_check(&res, 100, 0)) {
                printf("timeval_diff failed at s1 - s0.\n");
                goto fail;
        }

        tv_diff(&ms0, &ms1, &res);
        if (!tv_check(&res, -1, 900 * 1000L)) {
                printf("timeval_diff failed at ms0 - ms1.\n");
                goto fail;
        }

        tv_diff(&ms1, &ms0, &res);
        if (!tv_check(&res, 0, 100 * 1000L)) {
                printf("timeval_diff failed at ms1 - ms0.\n");
                goto fail;
        }

        tv_diff(&us0, &us1, &res);
        if (!tv_check(&res, -1, 999900)) {
                printf("timeval_diff failed at us0 - us1.\n");
                goto fail;
        }

        tv_diff(&us1, &us0, &res);
        if (!tv_check(&res, 0, 100)) {
                printf("timeval_diff failed at us1 - us0.\n");
                goto fail;
        }

        tv_diff(&s0, &ms0, &res);
        if (!tv_check(&res, 99, 900 * 1000L)) {
                printf("timeval_diff failed at s0 - ms0.\n");
                goto fail;
        }

        tv_diff(&s0, &us0, &res);
        if (!tv_check(&res, 99, 999900)) {
                printf("timeval_diff failed at s0 - us0.\n");
                goto fail;
        }

        tv_diff(&ms0, &us0, &res);
        if (!tv_check(&res, 0, 99900)) {
                printf("timeval_diff failed at ms0 - us0.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ts_diff_time(void)
{
        struct timespec s0  = TIMESPEC_INIT_S (100);
        struct timespec s1  = TIMESPEC_INIT_S (200);
        struct timespec ms0 = TIMESPEC_INIT_MS(100);
        struct timespec ms1 = TIMESPEC_INIT_MS(200);
        struct timespec us0 = TIMESPEC_INIT_US(100);
        struct timespec us1 = TIMESPEC_INIT_US(200);
        struct timespec ns0 = TIMESPEC_INIT_NS(100);
        struct timespec ns1 = TIMESPEC_INIT_NS(200);

        TEST_START();

        if (ts_diff_ms(&s0, &s1) != -100 * 1000L) {
                printf("timespec_diff_ms failed at s0 - s1.\n");
                goto fail;
        }

        if (ts_diff_ms(&s1, &s0) != 100 * 1000L) {
                printf("timespec_diff_ms failed at s1 - s0.\n");
                goto fail;
        }

        if (ts_diff_us(&s0, &s1) != -100 * MILLION) {
                printf("timespec_diff_us failed at s1 - s0.\n");
                goto fail;
        }

        if (ts_diff_us(&s1, &s0) != 100 * MILLION) {
                printf("timespec_diff_us failed at s0 - s1.\n");
                goto fail;
        }

        if (ts_diff_ns(&s0, &s1) != -100 * BILLION) {
                printf("timespec_diff_ns failed at s0 - s1.\n");
                goto fail;
        }

        if (ts_diff_ns(&s1, &s0) != 100 * BILLION) {
                printf("timespec_diff_ns failed at s1 - s0.\n");
                goto fail;
        }

        if (ts_diff_ms(&ms0, &ms1) != -100) {
                printf("timespec_diff_ms failed at ms0 - ms1.\n");
                goto fail;
        }

        if (ts_diff_ms(&ms1, &ms0) != 100) {
                printf("timespec_diff_ms failed at ms1 - ms0.\n");
                goto fail;
        }

        if (ts_diff_us(&ms0, &ms1) != -100 * 1000L) {
                printf("timespec_diff_us failed at ms0 - ms1.\n");
                goto fail;
        }

        if (ts_diff_us(&ms1, &ms0) != 100 * 1000L) {
                printf("timespec_diff_us failed at ms1 - ms0.\n");
                goto fail;
        }

        if (ts_diff_ns(&ms0, &ms1) != -100 * MILLION) {
                printf("timespec_diff_ns failed at ms0 - ms1.\n");
                goto fail;
        }

        if (ts_diff_ns(&ms1, &ms0) != 100 * MILLION) {
                printf("timespec_diff_ns failed at ms1 - ms0.\n");
                goto fail;
        }

        if (ts_diff_ms(&us0, &us1) != 0) {
                printf("timespec_diff_ms failed at us0 - us1.\n");
                goto fail;
        }

        if (ts_diff_ms(&us1, &us0) != 0) {
                printf("timespec_diff_ms failed at us1 - us0.\n");
                goto fail;
        }

        if (ts_diff_us(&us0, &us1) != -100) {
                printf("timespec_diff_us failed at us0 - us1.\n");
                goto fail;
        }

        if (ts_diff_us(&us1, &us0) != 100) {
                printf("timespec_diff_us failed at us1 - us0.\n");
                goto fail;
        }

        if (ts_diff_ns(&us0, &us1) != -100 * 1000L) {
                printf("timespec_diff_ns failed at us0 - us1.\n");
                goto fail;
        }

        if (ts_diff_ns(&us1, &us0) != 100 * 1000L) {
                printf("timespec_diff_ns failed at us1 - us0.\n");
                goto fail;
        }

        if (ts_diff_ms(&ns0, &ns1) != 0) {
                printf("timespec_diff_ms failed at ns0 - ns1.\n");
                goto fail;
        }

        if (ts_diff_ms(&ns1, &ns0) != 0) {
                printf("timespec_diff_ms failed at ns1 - ns0.\n");
                goto fail;
        }

        if (ts_diff_us(&ns0, &ns1) != 0) {
                printf("timespec_diff_us failed at ns0 - ns1.\n");
                goto fail;
        }

        if (ts_diff_us(&ns1, &ns0) != 0) {
                printf("timespec_diff_us failed at ns1 - ns0.\n");
                goto fail;
        }

        if (ts_diff_ns(&ns0, &ns1) != -100) {
                printf("timespec_diff_ns failed at ns0 - ns1.\n");
                goto fail;
        }

        if (ts_diff_ns(&ns1, &ns0) != 100) {
                printf("timespec_diff_ns failed at ns1 - ns0.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tv_diff_time(void)
{
        struct timeval s0  = TIMEVAL_INIT_S (100);
        struct timeval s1  = TIMEVAL_INIT_S (200);
        struct timeval ms0 = TIMEVAL_INIT_MS(100);
        struct timeval ms1 = TIMEVAL_INIT_MS(200);
        struct timeval us0 = TIMEVAL_INIT_US(100);
        struct timeval us1 = TIMEVAL_INIT_US(200);

        TEST_START();

        if (tv_diff_ms(&s0, &s1) != -100 * 1000L) {
                printf("timeval_diff_ms failed at s0 - s1.\n");
                goto fail;
        }

        if (tv_diff_ms(&s1, &s0) != 100 * 1000L) {
                printf("timeval_diff_ms failed at s1 - s0.\n");
                goto fail;
        }

        if (tv_diff_us(&s0, &s1) != -100 * MILLION) {
                printf("timeval_diff_us failed at s0 - s1.\n");
                goto fail;
        }

        if (tv_diff_us(&s1, &s0) != 100 * MILLION) {
                printf("timeval_diff_us failed at s1 - s0.\n");
                goto fail;
        }

        if (tv_diff_ms(&ms0, &ms1) != -100) {
                printf("timeval_diff_ms failed at ms0 - ms1.\n");
                goto fail;
        }

        if (tv_diff_ms(&ms1, &ms0) != 100) {
                printf("timeval_diff_ms failed at ms1 - ms0.\n");
                goto fail;
        }

        if (tv_diff_us(&ms0, &ms1) != -100 * 1000L) {
                printf("timeval_diff_us failed at ms0 - ms1.\n");
                goto fail;
        }

        if (tv_diff_us(&ms1, &ms0) != 100 * 1000L) {
                printf("timeval_diff_us failed at ms1 - ms0.\n");
                goto fail;
        }

        if (tv_diff_ms(&us0, &us1) != 0) {
                printf("timeval_diff_ms failed at us0 - us1.\n");
                goto fail;
        }

        if (tv_diff_ms(&us1, &us0) != 0) {
                printf("timeval_diff_ms failed at us1 - us0.\n");
                goto fail;
        }

        if (tv_diff_us(&us0, &us1) != -100) {
                printf("timeval_diff_us failed at us0 - us1.\n");
                goto fail;
        }

        if (tv_diff_us(&us1, &us0) != 100) {
                printf("timeval_diff_us failed at us1 - us0.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

int time_test(int argc,
              char ** argv)
{
        int ret = 0;

        (void) argc;
        (void) argv;

        ret |= test_time_ts_init();
        ret |= test_time_tv_init();
        ret |= test_ts_diff();
        ret |= test_tv_diff();
        ret |= test_ts_diff_time();
        ret |= test_tv_diff_time();

        return ret;
}