1
2#ifdef LIBREPLACE_CONFIGURE_TEST_STRPTIME
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <time.h>
7
8#define true 1
9#define false 0
10
11#ifndef __STRING
12#define __STRING(x)    #x
13#endif
14
15/* make printf a no-op */
16#define printf if(0) printf
17
18#else /* LIBREPLACE_CONFIGURE_TEST_STRPTIME */
19
20#include "replace.h"
21#include "system/time.h"
22
23#endif /* LIBREPLACE_CONFIGURE_TEST_STRPTIME */
24
25int libreplace_test_strptime(void)
26{
27	const char *s = "20070414101546Z";
28	char *ret;
29	struct tm t, t2;
30
31	memset(&t, 0, sizeof(t));
32	memset(&t2, 0, sizeof(t2));
33
34	printf("test: strptime\n");
35
36	ret = strptime(s, "%Y%m%d%H%M%S", &t);
37	if ( ret == NULL ) {
38		printf("failure: strptime [\n"
39		       "returned NULL\n"
40		       "]\n");
41		return false;
42	}
43
44	if ( *ret != 'Z' ) {
45		printf("failure: strptime [\n"
46		       "ret doesn't point to 'Z'\n"
47		       "]\n");
48		return false;
49	}
50
51	ret = strptime(s, "%Y%m%d%H%M%SZ", &t2);
52	if ( ret == NULL ) {
53		printf("failure: strptime [\n"
54		       "returned NULL with Z\n"
55		       "]\n");
56		return false;
57	}
58
59	if ( *ret != '\0' ) {
60		printf("failure: strptime [\n"
61		       "ret doesn't point to '\\0'\n"
62		       "]\n");
63		return false;
64	}
65
66#define CMP_TM_ELEMENT(t1,t2,elem) \
67	if (t1.elem != t2.elem) { \
68		printf("failure: strptime [\n" \
69		       "result differs if the format string has a 'Z' at the end\n" \
70		       "element: %s %d != %d\n" \
71		       "]\n", \
72		       __STRING(elen), t1.elem, t2.elem); \
73		return false; \
74	}
75
76	CMP_TM_ELEMENT(t,t2,tm_sec);
77	CMP_TM_ELEMENT(t,t2,tm_min);
78	CMP_TM_ELEMENT(t,t2,tm_hour);
79	CMP_TM_ELEMENT(t,t2,tm_mday);
80	CMP_TM_ELEMENT(t,t2,tm_mon);
81	CMP_TM_ELEMENT(t,t2,tm_year);
82	CMP_TM_ELEMENT(t,t2,tm_wday);
83	CMP_TM_ELEMENT(t,t2,tm_yday);
84	CMP_TM_ELEMENT(t,t2,tm_isdst);
85
86	if (t.tm_sec != 46) {
87		printf("failure: strptime [\n"
88		       "tm_sec: expected: 46, got: %d\n"
89		       "]\n",
90		       t.tm_sec);
91		return false;
92	}
93
94	if (t.tm_min != 15) {
95		printf("failure: strptime [\n"
96		       "tm_min: expected: 15, got: %d\n"
97		       "]\n",
98		       t.tm_min);
99		return false;
100	}
101
102	if (t.tm_hour != 10) {
103		printf("failure: strptime [\n"
104		       "tm_hour: expected: 10, got: %d\n"
105		       "]\n",
106		       t.tm_hour);
107		return false;
108	}
109
110	if (t.tm_mday != 14) {
111		printf("failure: strptime [\n"
112		       "tm_mday: expected: 14, got: %d\n"
113		       "]\n",
114		       t.tm_mday);
115		return false;
116	}
117
118	if (t.tm_mon != 3) {
119		printf("failure: strptime [\n"
120		       "tm_mon: expected: 3, got: %d\n"
121		       "]\n",
122		       t.tm_mon);
123		return false;
124	}
125
126	if (t.tm_year != 107) {
127		printf("failure: strptime [\n"
128		       "tm_year: expected: 107, got: %d\n"
129		       "]\n",
130		       t.tm_year);
131		return false;
132	}
133
134	if (t.tm_wday != 6) { /* saturday */
135		printf("failure: strptime [\n"
136		       "tm_wday: expected: 6, got: %d\n"
137		       "]\n",
138		       t.tm_wday);
139		return false;
140	}
141
142	if (t.tm_yday != 103) {
143		printf("failure: strptime [\n"
144		       "tm_yday: expected: 103, got: %d\n"
145		       "]\n",
146		       t.tm_yday);
147		return false;
148	}
149
150	/* we don't test this as it depends on the host configuration
151	if (t.tm_isdst != 0) {
152		printf("failure: strptime [\n"
153		       "tm_isdst: expected: 0, got: %d\n"
154		       "]\n",
155		       t.tm_isdst);
156		return false;
157	}*/
158
159	printf("success: strptime\n");
160
161	return true;
162}
163
164#ifdef LIBREPLACE_CONFIGURE_TEST_STRPTIME
165int main (void)
166{
167	int ret;
168	ret = libreplace_test_strptime();
169	if (ret == false) return 1;
170	return 0;
171}
172#endif
173