summaryrefslogtreecommitdiffstats
path: root/lib/replace/tests/strptime.c
blob: 5bf03f5b35348d28cd420c13ac2ce99b7679b936 (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
#ifdef LIBREPLACE_CONFIGURE_TEST_STRPTIME

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define true 1
#define false 0

#ifndef __STRING
#define __STRING(x)    #x
#endif

/* make printf a no-op */
#define printf if(0) printf

#else /* LIBREPLACE_CONFIGURE_TEST_STRPTIME */

#include "replace.h"
#include "system/time.h"
#include "replace-test.h"

#endif /* LIBREPLACE_CONFIGURE_TEST_STRPTIME */

int libreplace_test_strptime(void)
{
	const char *s = "20070414101546Z";
	char *ret;
	struct tm t, t2;

	memset(&t, 0, sizeof(t));
	memset(&t2, 0, sizeof(t2));

	printf("test: strptime\n");

	ret = strptime(s, "%Y%m%d%H%M%S", &t);
	if ( ret == NULL ) {
		printf("failure: strptime [\n"
		       "returned NULL\n"
		       "]\n");
		return false;
	}

	if ( *ret != 'Z' ) {
		printf("failure: strptime [\n"
		       "ret doesn't point to 'Z'\n"
		       "]\n");
		return false;
	}

	ret = strptime(s, "%Y%m%d%H%M%SZ", &t2);
	if ( ret == NULL ) {
		printf("failure: strptime [\n"
		       "returned NULL with Z\n"
		       "]\n");
		return false;
	}

	if ( *ret != '\0' ) {
		printf("failure: strptime [\n"
		       "ret doesn't point to '\\0'\n"
		       "]\n");
		return false;
	}

#define CMP_TM_ELEMENT(t1,t2,elem) \
	if (t1.elem != t2.elem) { \
		printf("failure: strptime [\n" \
		       "result differs if the format string has a 'Z' at the end\n" \
		       "element: %s %d != %d\n" \
		       "]\n", \
		       __STRING(elen), t1.elem, t2.elem); \
		return false; \
	}

	CMP_TM_ELEMENT(t,t2,tm_sec);
	CMP_TM_ELEMENT(t,t2,tm_min);
	CMP_TM_ELEMENT(t,t2,tm_hour);
	CMP_TM_ELEMENT(t,t2,tm_mday);
	CMP_TM_ELEMENT(t,t2,tm_mon);
	CMP_TM_ELEMENT(t,t2,tm_year);
	CMP_TM_ELEMENT(t,t2,tm_wday);
	CMP_TM_ELEMENT(t,t2,tm_yday);
	CMP_TM_ELEMENT(t,t2,tm_isdst);

	if (t.tm_sec != 46) {
		printf("failure: strptime [\n"
		       "tm_sec: expected: 46, got: %d\n"
		       "]\n",
		       t.tm_sec);
		return false;
	}

	if (t.tm_min != 15) {
		printf("failure: strptime [\n"
		       "tm_min: expected: 15, got: %d\n"
		       "]\n",
		       t.tm_min);
		return false;
	}

	if (t.tm_hour != 10) {
		printf("failure: strptime [\n"
		       "tm_hour: expected: 10, got: %d\n"
		       "]\n",
		       t.tm_hour);
		return false;
	}

	if (t.tm_mday != 14) {
		printf("failure: strptime [\n"
		       "tm_mday: expected: 14, got: %d\n"
		       "]\n",
		       t.tm_mday);
		return false;
	}

	if (t.tm_mon != 3) {
		printf("failure: strptime [\n"
		       "tm_mon: expected: 3, got: %d\n"
		       "]\n",
		       t.tm_mon);
		return false;
	}

	if (t.tm_year != 107) {
		printf("failure: strptime [\n"
		       "tm_year: expected: 107, got: %d\n"
		       "]\n",
		       t.tm_year);
		return false;
	}

	if (t.tm_wday != 6) { /* saturday */
		printf("failure: strptime [\n"
		       "tm_wday: expected: 6, got: %d\n"
		       "]\n",
		       t.tm_wday);
		return false;
	}

	if (t.tm_yday != 103) {
		printf("failure: strptime [\n"
		       "tm_yday: expected: 103, got: %d\n"
		       "]\n",
		       t.tm_yday);
		return false;
	}

	/* we don't test this as it depends on the host configuration
	if (t.tm_isdst != 0) {
		printf("failure: strptime [\n"
		       "tm_isdst: expected: 0, got: %d\n"
		       "]\n",
		       t.tm_isdst);
		return false;
	}*/

	printf("success: strptime\n");

	return true;
}

#ifdef LIBREPLACE_CONFIGURE_TEST_STRPTIME
int main (void)
{
	int ret;
	ret = libreplace_test_strptime();
	if (ret == false) return 1;
	return 0;
}
#endif