]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/tst-strptime2.c
Installed-header hygiene (BZ#20366): time.h types.
[thirdparty/glibc.git] / time / tst-strptime2.c
CommitLineData
be13f5a4
JP
1/* tst-strptime2 - Test strptime %z timezone offset specifier. */
2
935f3e67 3#include <limits.h>
be13f5a4 4#include <stdbool.h>
935f3e67
UD
5#include <stdio.h>
6#include <time.h>
7
be13f5a4
JP
8/* Dummy string is used to match strptime's %s specifier. */
9
10static const char dummy_string[] = "1113472456";
11
12/* buffer_size contains the maximum test string length, including
13 trailing NUL. */
14
15enum
16{
17 buffer_size = 20,
18};
19
20/* Verbose execution, set with --verbose command line option. */
21
22static bool verbose;
23
24
25/* mkbuf - Write a test string for strptime with the specified time
26 value and number of digits into the supplied buffer, and return
27 the expected strptime test result.
28
29 The test string, buf, is written with the following content:
30 a dummy string matching strptime "%s" format specifier,
31 whitespace matching strptime " " format specifier, and
32 timezone string matching strptime "%z" format specifier.
33
900f33e2
VB
34 Note that a valid timezone string is either "Z" or contains the
35 following fields:
be13f5a4
JP
36 Sign field consisting of a '+' or '-' sign,
37 Hours field in two decimal digits, and
e952e1df
VB
38 optional Minutes field in two decimal digits. Optionally,
39 a ':' is used to seperate hours and minutes.
be13f5a4
JP
40
41 This function may write test strings with minutes values outside
42 the valid range 00-59. These are invalid strings and useful for
43 testing strptime's rejection of invalid strings.
44
45 The ndigits parameter is used to limit the number of timezone
46 string digits to be written and may range from 0 to 4. Note that
47 only 2 and 4 digit strings are valid input to strptime; strings
48 with 0, 1 or 3 digits are invalid and useful for testing strptime's
49 rejection of invalid strings.
50
51 This function returns the behavior expected of strptime resulting
52 from parsing the the test string. For valid strings, the function
53 returns the expected tm_gmtoff value. For invalid strings,
54 LONG_MAX is returned. LONG_MAX indicates the expectation that
55 strptime will return NULL; for example, if the number of digits
56 are not correct, or minutes part of the time is outside the valid
57 range of 00 to 59. */
935f3e67 58
be13f5a4 59static long int
e952e1df 60mkbuf (char *buf, bool neg, bool colon, unsigned int hhmm, size_t ndigits)
935f3e67 61{
be13f5a4
JP
62 const int mm_max = 59;
63 char sign = neg ? '-' : '+';
64 int i;
65 unsigned int hh = hhmm / 100;
66 unsigned int mm = hhmm % 100;
67 long int expect = LONG_MAX;
935f3e67 68
be13f5a4 69 i = sprintf (buf, "%s %c", dummy_string, sign);
e952e1df
VB
70 if (colon)
71 snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm);
72 else
73 snprintf (buf + i, ndigits + 1, "%04u", hhmm);
be13f5a4
JP
74
75 if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
76 {
77 long int tm_gmtoff = hh * 3600 + mm * 60;
78
79 expect = neg ? -tm_gmtoff : tm_gmtoff;
80 }
81
82 return expect;
83}
84
85
86/* Write a description of expected or actual test result to stdout. */
87
88static void
89describe (bool string_valid, long int tm_gmtoff)
90{
91 if (string_valid)
92 printf ("valid, tm.tm_gmtoff %ld", tm_gmtoff);
93 else
94 printf ("invalid, return value NULL");
95}
96
97
98/* Using buffer buf, run strptime. Compare results against expect,
99 the expected result. Report failures and verbose results to stdout.
100 Update the result counts. Return 1 if test failed, 0 if passed. */
935f3e67 101
29955b5d 102static int
be13f5a4 103compare (const char *buf, long int expect, unsigned int *nresult)
935f3e67 104{
be13f5a4
JP
105 struct tm tm;
106 char *p;
107 bool test_string_valid;
108 long int test_result;
109 bool fail;
110 int result;
111
112 p = strptime (buf, "%s %z", &tm);
113 test_string_valid = p != NULL;
114 test_result = test_string_valid ? tm.tm_gmtoff : LONG_MAX;
115 fail = test_result != expect;
935f3e67 116
be13f5a4 117 if (fail || verbose)
935f3e67 118 {
be13f5a4 119 bool expect_string_valid = expect != LONG_MAX;
935f3e67 120
be13f5a4
JP
121 printf ("%s: input \"%s\", expected: ", fail ? "FAIL" : "PASS", buf);
122 describe (expect_string_valid, expect);
935f3e67 123
be13f5a4 124 if (fail)
935f3e67 125 {
be13f5a4
JP
126 printf (", got: ");
127 describe (test_string_valid, test_result);
935f3e67 128 }
be13f5a4
JP
129
130 printf ("\n");
935f3e67
UD
131 }
132
be13f5a4
JP
133 result = fail ? 1 : 0;
134 nresult[result]++;
135
136 return result;
137}
138
139
140static int
141do_test (void)
142{
143 char buf[buffer_size];
144 long int expect;
145 int result = 0;
146 /* Number of tests run with passing (index==0) and failing (index==1)
147 results. */
148 unsigned int nresult[2];
149 unsigned int ndigits;
150 unsigned int step;
151 unsigned int hhmm;
152
153 nresult[0] = 0;
154 nresult[1] = 0;
155
156 /* Create and test input string with no sign and four digits input
157 (invalid format). */
158
159 sprintf (buf, "%s 1030", dummy_string);
160 expect = LONG_MAX;
161 result |= compare (buf, expect, nresult);
162
900f33e2
VB
163 /* Create and test input string with "Z" input (valid format).
164 Expect tm_gmtoff of 0. */
165
166 sprintf (buf, "%s Z", dummy_string);
167 expect = 0;
168 result |= compare (buf, expect, nresult);
169
be13f5a4
JP
170 /* Create and test input strings with sign and digits:
171 0 digits (invalid format),
172 1 digit (invalid format),
173 2 digits (valid format),
174 3 digits (invalid format),
175 4 digits (valid format if and only if minutes is in range 00-59,
176 otherwise invalid).
177 If format is valid, the returned tm_gmtoff is checked. */
178
179 for (ndigits = 0, step = 10000; ndigits <= 4; ndigits++, step /= 10)
180 for (hhmm = 0; hhmm <= 9999; hhmm += step)
181 {
182 /* Test both positive and negative signs. */
183
e952e1df 184 expect = mkbuf (buf, false, false, hhmm, ndigits);
be13f5a4
JP
185 result |= compare (buf, expect, nresult);
186
e952e1df 187 expect = mkbuf (buf, true, false, hhmm, ndigits);
be13f5a4 188 result |= compare (buf, expect, nresult);
e952e1df
VB
189
190 /* Test with colon as well. */
191
192 if (ndigits >= 3)
193 {
194 expect = mkbuf (buf, false, true, hhmm, ndigits);
195 result |= compare (buf, expect, nresult);
196
197 expect = mkbuf (buf, true, true, hhmm, ndigits);
198 result |= compare (buf, expect, nresult);
199 }
be13f5a4
JP
200 }
201
202 if (result > 0 || verbose)
203 printf ("%s: %u input strings: %u fail, %u pass\n",
204 result > 0 ? "FAIL" : "PASS",
205 nresult[1] + nresult[0], nresult[1], nresult[0]);
206
5df56c7e 207 return result;
935f3e67 208}
29955b5d 209
be13f5a4
JP
210
211/* Add a "--verbose" command line option to test-skeleton.c. */
212
213#define OPT_VERBOSE 10000
214
215#define CMDLINE_OPTIONS \
216 { "verbose", no_argument, NULL, OPT_VERBOSE, },
217
218#define CMDLINE_PROCESS \
219 case OPT_VERBOSE: \
220 verbose = true; \
221 break;
222
29955b5d
AS
223#define TEST_FUNCTION do_test ()
224#include "../test-skeleton.c"