ksgi

Форк
0
/
test-datetime2epoch.c 
178 строк · 3.7 Кб
1
/*	$Id$ */
2
/*
3
 * Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
#include "../config.h"
18

19
#if HAVE_ERR
20
# include <err.h>
21
#endif
22

23
#include <inttypes.h>
24
#include <stdarg.h>
25
#include <stdint.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <time.h>
29
#include <unistd.h>
30

31
#include <curl/curl.h>
32

33
#include "../kcgi.h"
34
#include "regress.h"
35

36
int
37
main(int argc, char *argv[])
38
{
39
	size_t	 	 i;
40
	struct tm	 test;
41
	int64_t		 v, res;
42

43
	/* 
44
	 * Test a lot of positive and negative numbers.
45
	 * We want the full range of time_t to work.
46
	 */
47

48
	for (i = 0; i < 100000; i++) {
49
#if HAVE_ARC4RANDOM
50
		v = arc4random();
51
#else
52
		v = random();
53
#endif
54
		if (!KHTTP_EPOCH2TM(v, &test))
55
			errx(1, "KHTTP_EPOCH2TM");
56
		if (!khttp_datetime2epoch(&res,
57
		    test.tm_mday,
58
		    test.tm_mon + 1,
59
		    test.tm_year + 1900,
60
		    test.tm_hour,
61
		    test.tm_min,
62
		    test.tm_sec))
63
			errx(1, "khttp_datetime2epoch: %" PRId64, v);
64
		if (res != v) 
65
			errx(1, "date cross-check: have "
66
				"%" PRId64 ", want %" PRId64, res, v);
67
	}
68

69
	for (i = 0; i < 100000; i++) {
70
#if HAVE_ARC4RANDOM
71
		v = (int64_t)arc4random() * -1;
72
#else
73
		v = (int64_t)random() * -1;
74
#endif
75
		if (!KHTTP_EPOCH2TM(v, &test))
76
			errx(1, "KHTTP_EPOCH2TM");
77
		if (!khttp_datetime2epoch(&res,
78
		    test.tm_mday,
79
		    test.tm_mon + 1,
80
		    test.tm_year + 1900,
81
		    test.tm_hour,
82
		    test.tm_min,
83
		    test.tm_sec))
84
			errx(1, "khttp_datetime2epoch: %" PRId64, v);
85
		if (res != v) 
86
			errx(1, "date cross-check: have "
87
				"%" PRId64 ", want %" PRId64, res, v);
88
	}
89

90
	/* Test specifically for -1 and 0. */
91

92
	v = -1;
93
	if (!KHTTP_EPOCH2TM(v, &test))
94
		errx(1, "KHTTP_EPOCH2TM");
95
	if (!khttp_datetime2epoch(&res,
96
	    test.tm_mday,
97
	    test.tm_mon + 1,
98
	    test.tm_year + 1900,
99
	    test.tm_hour,
100
	    test.tm_min,
101
	    test.tm_sec))
102
		errx(1, "khttp_datetime2epoch: %" PRId64, v);
103
	if (res != v) 
104
		errx(1, "date cross-check: have "
105
			"%" PRId64 ", want %" PRId64, res, v);
106

107
	v = 0;
108
	if (!KHTTP_EPOCH2TM(v, &test))
109
		errx(1, "KHTTP_EPOCH2TM");
110
	if (!khttp_datetime2epoch(&res,
111
	    test.tm_mday,
112
	    test.tm_mon + 1,
113
	    test.tm_year + 1900,
114
	    test.tm_hour,
115
	    test.tm_min,
116
	    test.tm_sec))
117
		errx(1, "khttp_datetime2epoch: %" PRId64, v);
118
	if (res != v) 
119
		errx(1, "date cross-check: have "
120
			"%" PRId64 ", want %" PRId64, res, v);
121

122
	/* Leap year is not ok. */
123

124
	if (khttp_datetime2epoch(&res,
125
	    29,
126
	    2,
127
	    2019,
128
	    0,
129
	    0,
130
	    0))
131
		errx(1, "khttp_datetime2epoch should fail");
132

133
	/* Leap year is ok. */
134

135
	if (!khttp_datetime2epoch(&res,
136
	    29,
137
	    2,
138
	    2020,
139
	    0,
140
	    0,
141
	    0))
142
		errx(1, "khttp_datetime2epoch");
143

144
	/* Bad day of month. */
145

146
	if (khttp_datetime2epoch(&res,
147
	    31,
148
	    4,
149
	    2020,
150
	    0,
151
	    0,
152
	    0))
153
		errx(1, "khttp_datetime2epoch should fail");
154

155
	/* Zeroes everywhere fails (month, day). */
156

157
	if (khttp_datetime2epoch(&res,
158
	    0,
159
	    0,
160
	    0,
161
	    0,
162
	    0,
163
	    0))
164
		errx(1, "khttp_datetime2epoch should fail");
165

166
	/* Zero hour. */
167

168
	if (!khttp_datetime2epoch(&res,
169
	    1,
170
	    1,
171
	    0,
172
	    0,
173
	    0,
174
	    0))
175
		errx(1, "khttp_datetime2epoch");
176

177
	return 0;
178
}
179

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.