File: test_time_funcs.c

package info (click to toggle)
htslib 1.16%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,804 kB
  • sloc: ansic: 59,004; perl: 1,727; makefile: 732; sh: 359
file content (125 lines) | stat: -rw-r--r-- 4,581 bytes parent folder | download | duplicates (6)
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
/*  test_time_compat.c -- Test time functions

    Copyright (C) 2022 Genome Research Ltd.

    Author: Rob Davies <rmd@sanger.ac.uk>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.  */

#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <errno.h>
#include <time.h>

#include "../hts_time_funcs.h"

int test_normalised(time_t start, time_t end, time_t incr) {
    time_t i, j;
    struct tm *utc;

    for (i = start; i < end; i += incr) {
        utc = gmtime(&i);
        j = hts_time_gm(utc);
        if (i != j) {
            fprintf(stderr,
                    "hts_time_gm() failed, got %"PRId64" expected %"PRId64"\n",
                    (int64_t) j, (int64_t) i);
            return 1;
        }
    }
    return 0;
}

int test_specific(int year, int mon, int mday, int hour, int min, int sec,
                  time_t expected) {
    struct tm utc = { sec, min, hour, mday, mon - 1, year - 1900, 0, 0, 0 };
    time_t res = hts_time_gm(&utc);
    if (res != expected) {
        fprintf(stderr,
                "hts_time_gm() failed for %4d/%02d/%02d %02d:%02d:%02d :"
                " got %"PRId64" expected %"PRId64"\n",
                year, mon, mday, hour, min, sec,
                (int64_t) res, (int64_t) expected);
        return 1;
    }
    return 0;
}

int main(int argc, char **argv) {
    int res = 0;

    if (test_normalised(0, INT_MAX - 1000, 1000) != 0)
        return EXIT_FAILURE;
    if (sizeof(time_t) >= 8) {
        if (test_normalised(INT_MAX - 1000,
                            (time_t)((int64_t) INT_MAX * 2), 1000) != 0)
            return EXIT_FAILURE;
    }

    // 2022-06-14 12:32:10
    res |= test_specific(2022, 6, 14, 12, 32, 10, 1655209930);
    // 2022-06-14 12:32:10
    res |= test_specific(1993, 9, 10514, 12, 32, 10, 1655209930);
    // 2022-02-28 12:00:00
    res |= test_specific(2020, 2, 28, 12, 0, 0, 1582891200);
    // 2022-02-29 12:00:00
    res |= test_specific(2020, 2, 29, 12, 0, 0, 1582977600);
    // 2022-03-01 12:00:00
    res |= test_specific(2020, 2, 30, 12, 0, 0, 1583064000);
    // 2022-02-29 12:00:00
    res |= test_specific(2020, 3, 0, 12, 0, 0, 1582977600);
    // 2020-02-01 12:00:00
    res |= test_specific(2019, 14, 1, 12, 0, 0, 1580558400);
    // 2020-03-01 12:00:00
    res |= test_specific(2019, 15, 1, 12, 0, 0, 1583064000);
    // 2021-03-01 12:00:00
    res |= test_specific(2019, 27, 1, 12, 0, 0, 1614600000);
    // 2024-02-01 12:00:00
    res |= test_specific(2019, 62, 1, 12, 0, 0, 1706788800);
    // 2024-03-01 12:00:00
    res |= test_specific(2019, 63, 1, 12, 0, 0, 1709294400);
    // 2020-12-31 23:59:59
    res |= test_specific(2021, 0, 31, 23, 59, 59, 1609459199);
    // 2020-03-01 12:00:00
    res |= test_specific(2021, -9, 1, 12, 0, 0, 1583064000);
    // 2020-02-01 12:00:00
    res |= test_specific(2021, -10, 1, 12, 0, 0, 1580558400);
    // 2019-02-01 12:00:00
    res |= test_specific(2021, -22, 1, 12, 0, 0, 1549022400);
    // 1970-01-01 00:00:00
    res |= test_specific(1970, 1, 1, 0, 0, 0, 0);
    // 2038-01-19 03:14:07
    res |= test_specific(1970, 1, 1, 0, 0, INT_MAX, INT_MAX);
    // 2038-01-19 03:14:07
    res |= test_specific(2038, 1, 19, 3, 14, 7, INT_MAX);
    if (sizeof(time_t) < 8) {
        // 2038-01-19 03:14:08
        res |= test_specific(2038, 1, 19, 3, 14, 8, (time_t) -1);
    } else {
        // 2038-01-19 03:14:08
        res |= test_specific(2038, 1, 19, 3, 14, 8,
                             (time_t)((int64_t) INT_MAX + 1));
    }

    return res == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}