File: testtime.c

package info (click to toggle)
nextepc 0.3.10+nods-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,304 kB
  • sloc: ansic: 164,982; yacc: 2,767; python: 2,567; makefile: 2,408; lex: 1,732; xml: 795; php: 486; sh: 349; sql: 337; perl: 75; awk: 35; cpp: 6
file content (289 lines) | stat: -rw-r--r-- 7,465 bytes parent folder | download
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
#include "core_time.h"
#include "testutil.h"

#define STR_SIZE 100

/* The time value is used throughout the tests, so just make this a global.
 * Also, we need a single value that we can test for the positive tests, so
 * I chose the number below, it corresponds to:
 *           2002-08-14 12:05:36.186711 -25200 [257 Sat].
 * Which happens to be when I wrote the new tests.
 */
static c_time_t now = C_INT64_C(1032030336186711);

static void test_now(abts_case *tc, void *data)
{
    c_time_t timediff;
    c_time_t current;
    time_t os_now;

    current = time_now();
    time(&os_now);

    timediff = os_now - (current / USEC_PER_SEC);
    /* Even though these are called so close together, there is the chance
     * that the time will be slightly off, so accept anything between -1 and
     * 1 second.
     */
    ABTS_ASSERT(tc, "core_time and OS time do not agree",
             (timediff > -2) && (timediff < 2));
}

static void test_gmtstr(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    char str[STR_SIZE+1];

    rv = time_exp_gmt(&xt, now);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "time_exp_gmt");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    snprintf(str, STR_SIZE, "%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
                 xt.tm_year + 1900,
                 xt.tm_mon,
                 xt.tm_mday,
                 xt.tm_hour,
                 xt.tm_min,
                 xt.tm_sec,
                 xt.tm_usec,
                 xt.tm_gmtoff,
                 xt.tm_yday + 1,
                 day_snames[xt.tm_wday],
                 (xt.tm_isdst ? " DST" : ""));
    ABTS_STR_EQUAL(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]", str);
}

static void test_exp_lt(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    time_t posix_secs = (time_t)time_sec(now);
    struct tm *posix_exp = localtime(&posix_secs);

    rv = time_exp_lt(&xt, now);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "time_exp_lt");
    }
    ABTS_TRUE(tc, rv == CORE_OK);

#define CHK_FIELD(f) \
    ABTS_ASSERT(tc, "Mismatch in " #f, posix_exp->f == xt.f)

    CHK_FIELD(tm_sec);
    CHK_FIELD(tm_min);
    CHK_FIELD(tm_hour);
    CHK_FIELD(tm_mday);
    CHK_FIELD(tm_mon);
    CHK_FIELD(tm_year);
    CHK_FIELD(tm_wday);
    CHK_FIELD(tm_yday);
    CHK_FIELD(tm_isdst);
#undef CHK_FIELD
}

static void test_exp_get_gmt(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    c_time_t imp;
    c_int64_t hr_off_64;

    rv = time_exp_gmt(&xt, now);
    ABTS_TRUE(tc, rv == CORE_OK);
    rv = time_exp_get(&imp, &xt);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "time_exp_get");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    hr_off_64 = (c_int64_t) xt.tm_gmtoff * USEC_PER_SEC;
    ABTS_TRUE(tc, now + hr_off_64 == imp);
}

static void test_exp_get_lt(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    c_time_t imp;
    c_int64_t hr_off_64;

    rv = time_exp_lt(&xt, now);
    ABTS_TRUE(tc, rv == CORE_OK);
    rv = time_exp_get(&imp, &xt);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "time_exp_get");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    hr_off_64 = (c_int64_t) xt.tm_gmtoff * USEC_PER_SEC;
    ABTS_TRUE(tc, now + hr_off_64 == imp);
}

static void test_imp_gmt(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    c_time_t imp;

    rv = time_exp_gmt(&xt, now);
    ABTS_TRUE(tc, rv == CORE_OK);
    rv = time_exp_gmt_get(&imp, &xt);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "time_exp_gmt_get");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    ABTS_TRUE(tc, now == imp);
}

static void test_rfcstr(abts_case *tc, void *data)
{
    status_t rv;
    char str[STR_SIZE];

    rv = rfc822_date(str, now);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "rfc822_date");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    ABTS_STR_EQUAL(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
}

static void test_ctime(abts_case *tc, void *data)
{
    status_t rv;
    char core_str[STR_SIZE];
    char libc_str[STR_SIZE];
    c_time_t now_sec = time_sec(now);
    time_t posix_sec = (time_t) now_sec;

    rv = core_ctime(core_str, now);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "core_ctime");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    strcpy(libc_str, ctime(&posix_sec));
    *strchr(libc_str, '\n') = '\0';

    ABTS_STR_EQUAL(tc, libc_str, core_str);
}

static void test_strftime(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    char str[STR_SIZE+1];
    size_t sz;

    rv = time_exp_gmt(&xt, now);
    rv = core_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "core_strftime");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    ABTS_STR_EQUAL(tc, "19:05 Saturday 14 September 2002", str);
}

static void test_strftimesmall(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    char str[STR_SIZE];
    size_t sz;

    rv = time_exp_gmt(&xt, now);
    rv = core_strftime(str, &sz, STR_SIZE, "%T", &xt);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "core_strftime");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    ABTS_STR_EQUAL(tc, "19:05:36", str);
}

static void test_exp_tz(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    c_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */

    rv = time_exp_tz(&xt, now, hr_off);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "time_exp_tz");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
    ABTS_TRUE(tc, (xt.tm_usec == 186711) &&
                     (xt.tm_sec == 36) &&
                     (xt.tm_min == 5) &&
                     (xt.tm_hour == 14) &&
                     (xt.tm_mday == 14) &&
                     (xt.tm_mon == 8) &&
                     (xt.tm_year == 102) &&
                     (xt.tm_wday == 6) &&
                     (xt.tm_yday == 256));
}

static void test_strftimeoffset(abts_case *tc, void *data)
{
    status_t rv;
    time_exp_t xt;
    char str[STR_SIZE];
    size_t sz;
    c_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */

    time_exp_tz(&xt, now, hr_off);
    rv = core_strftime(str, &sz, STR_SIZE, "%T", &xt);
    if (rv == CORE_ENOTIMPL)
    {
        ABTS_NOT_IMPL(tc, "core_strftime");
    }
    ABTS_TRUE(tc, rv == CORE_OK);
}

/* 0.9.4 and earlier rejected valid dates in 2038 */
static void test_2038(abts_case *tc, void *data)
{
    time_exp_t xt;
    c_time_t t;

    /* 2038-01-19T03:14:07.000000Z */
    xt.tm_year = 138;
    xt.tm_mon = 0;
    xt.tm_mday = 19;
    xt.tm_hour = 3;
    xt.tm_min = 14;
    xt.tm_sec = 7;

    CORE_ASSERT_OK(tc, "explode January 19th, 2038",
                       time_exp_get(&t, &xt));
}

abts_suite *testtime(abts_suite *suite)
{
    suite = ADD_SUITE(suite)

    abts_run_test(suite, test_now, NULL);
    abts_run_test(suite, test_gmtstr, NULL);
    abts_run_test(suite, test_exp_lt, NULL);
    abts_run_test(suite, test_exp_get_gmt, NULL);
    abts_run_test(suite, test_exp_get_lt, NULL);
    abts_run_test(suite, test_imp_gmt, NULL);
    abts_run_test(suite, test_rfcstr, NULL);
    abts_run_test(suite, test_ctime, NULL);
    abts_run_test(suite, test_strftime, NULL);
    abts_run_test(suite, test_strftimesmall, NULL);
    abts_run_test(suite, test_exp_tz, NULL);
    abts_run_test(suite, test_strftimeoffset, NULL);
    abts_run_test(suite, test_2038, NULL);

    return suite;
}