Fix ldbl-128ibm log1pl (sNaN) (bug 20234).
[glibc.git] / time / tst-strptime2.c
blob7fe7350024c2e2be1502723f476d9eff58925bd8
1 /* tst-strptime2 - Test strptime %z timezone offset specifier. */
3 #include <limits.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <time.h>
8 /* Dummy string is used to match strptime's %s specifier. */
10 static const char dummy_string[] = "1113472456";
12 /* buffer_size contains the maximum test string length, including
13 trailing NUL. */
15 enum
17 buffer_size = 20,
20 /* Verbose execution, set with --verbose command line option. */
22 static bool verbose;
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.
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.
34 Note that a valid timezone string is either "Z" or contains the
35 following fields:
36 Sign field consisting of a '+' or '-' sign,
37 Hours field in two decimal digits, and
38 optional Minutes field in two decimal digits. Optionally,
39 a ':' is used to seperate hours and minutes.
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.
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.
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. */
59 static long int
60 mkbuf (char *buf, bool neg, bool colon, unsigned int hhmm, size_t ndigits)
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;
69 i = sprintf (buf, "%s %c", dummy_string, sign);
70 if (colon)
71 snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm);
72 else
73 snprintf (buf + i, ndigits + 1, "%04u", hhmm);
75 if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
77 long int tm_gmtoff = hh * 3600 + mm * 60;
79 expect = neg ? -tm_gmtoff : tm_gmtoff;
82 return expect;
86 /* Write a description of expected or actual test result to stdout. */
88 static void
89 describe (bool string_valid, long int tm_gmtoff)
91 if (string_valid)
92 printf ("valid, tm.tm_gmtoff %ld", tm_gmtoff);
93 else
94 printf ("invalid, return value NULL");
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. */
102 static int
103 compare (const char *buf, long int expect, unsigned int *nresult)
105 struct tm tm;
106 char *p;
107 bool test_string_valid;
108 long int test_result;
109 bool fail;
110 int result;
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;
117 if (fail || verbose)
119 bool expect_string_valid = expect != LONG_MAX;
121 printf ("%s: input \"%s\", expected: ", fail ? "FAIL" : "PASS", buf);
122 describe (expect_string_valid, expect);
124 if (fail)
126 printf (", got: ");
127 describe (test_string_valid, test_result);
130 printf ("\n");
133 result = fail ? 1 : 0;
134 nresult[result]++;
136 return result;
140 static int
141 do_test (void)
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;
153 nresult[0] = 0;
154 nresult[1] = 0;
156 /* Create and test input string with no sign and four digits input
157 (invalid format). */
159 sprintf (buf, "%s 1030", dummy_string);
160 expect = LONG_MAX;
161 result |= compare (buf, expect, nresult);
163 /* Create and test input string with "Z" input (valid format).
164 Expect tm_gmtoff of 0. */
166 sprintf (buf, "%s Z", dummy_string);
167 expect = 0;
168 result |= compare (buf, expect, nresult);
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. */
179 for (ndigits = 0, step = 10000; ndigits <= 4; ndigits++, step /= 10)
180 for (hhmm = 0; hhmm <= 9999; hhmm += step)
182 /* Test both positive and negative signs. */
184 expect = mkbuf (buf, false, false, hhmm, ndigits);
185 result |= compare (buf, expect, nresult);
187 expect = mkbuf (buf, true, false, hhmm, ndigits);
188 result |= compare (buf, expect, nresult);
190 /* Test with colon as well. */
192 if (ndigits >= 3)
194 expect = mkbuf (buf, false, true, hhmm, ndigits);
195 result |= compare (buf, expect, nresult);
197 expect = mkbuf (buf, true, true, hhmm, ndigits);
198 result |= compare (buf, expect, nresult);
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]);
207 return result;
211 /* Add a "--verbose" command line option to test-skeleton.c. */
213 #define OPT_VERBOSE 10000
215 #define CMDLINE_OPTIONS \
216 { "verbose", no_argument, NULL, OPT_VERBOSE, },
218 #define CMDLINE_PROCESS \
219 case OPT_VERBOSE: \
220 verbose = true; \
221 break;
223 #define TEST_FUNCTION do_test ()
224 #include "../test-skeleton.c"