time/tst-strptime2.c: test full input range +/- 0-9999
[glibc.git] / time / tst-strptime2.c
blob5b06a63ba4c3c0212438bde9d81c0b5e42337e1f
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 contains the following fields:
35 Sign field consisting of a '+' or '-' sign,
36 Hours field in two decimal digits, and
37 optional Minutes field in two decimal digits.
39 This function may write test strings with minutes values outside
40 the valid range 00-59. These are invalid strings and useful for
41 testing strptime's rejection of invalid strings.
43 The ndigits parameter is used to limit the number of timezone
44 string digits to be written and may range from 0 to 4. Note that
45 only 2 and 4 digit strings are valid input to strptime; strings
46 with 0, 1 or 3 digits are invalid and useful for testing strptime's
47 rejection of invalid strings.
49 This function returns the behavior expected of strptime resulting
50 from parsing the the test string. For valid strings, the function
51 returns the expected tm_gmtoff value. For invalid strings,
52 LONG_MAX is returned. LONG_MAX indicates the expectation that
53 strptime will return NULL; for example, if the number of digits
54 are not correct, or minutes part of the time is outside the valid
55 range of 00 to 59. */
57 static long int
58 mkbuf (char *buf, bool neg, unsigned int hhmm, size_t ndigits)
60 const int mm_max = 59;
61 char sign = neg ? '-' : '+';
62 int i;
63 unsigned int hh = hhmm / 100;
64 unsigned int mm = hhmm % 100;
65 long int expect = LONG_MAX;
67 i = sprintf (buf, "%s %c", dummy_string, sign);
68 snprintf (buf + i, ndigits + 1, "%04u", hhmm);
70 if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
72 long int tm_gmtoff = hh * 3600 + mm * 60;
74 expect = neg ? -tm_gmtoff : tm_gmtoff;
77 return expect;
81 /* Write a description of expected or actual test result to stdout. */
83 static void
84 describe (bool string_valid, long int tm_gmtoff)
86 if (string_valid)
87 printf ("valid, tm.tm_gmtoff %ld", tm_gmtoff);
88 else
89 printf ("invalid, return value NULL");
93 /* Using buffer buf, run strptime. Compare results against expect,
94 the expected result. Report failures and verbose results to stdout.
95 Update the result counts. Return 1 if test failed, 0 if passed. */
97 static int
98 compare (const char *buf, long int expect, unsigned int *nresult)
100 struct tm tm;
101 char *p;
102 bool test_string_valid;
103 long int test_result;
104 bool fail;
105 int result;
107 p = strptime (buf, "%s %z", &tm);
108 test_string_valid = p != NULL;
109 test_result = test_string_valid ? tm.tm_gmtoff : LONG_MAX;
110 fail = test_result != expect;
112 if (fail || verbose)
114 bool expect_string_valid = expect != LONG_MAX;
116 printf ("%s: input \"%s\", expected: ", fail ? "FAIL" : "PASS", buf);
117 describe (expect_string_valid, expect);
119 if (fail)
121 printf (", got: ");
122 describe (test_string_valid, test_result);
125 printf ("\n");
128 result = fail ? 1 : 0;
129 nresult[result]++;
131 return result;
135 static int
136 do_test (void)
138 char buf[buffer_size];
139 long int expect;
140 int result = 0;
141 /* Number of tests run with passing (index==0) and failing (index==1)
142 results. */
143 unsigned int nresult[2];
144 unsigned int ndigits;
145 unsigned int step;
146 unsigned int hhmm;
148 nresult[0] = 0;
149 nresult[1] = 0;
151 /* Create and test input string with no sign and four digits input
152 (invalid format). */
154 sprintf (buf, "%s 1030", dummy_string);
155 expect = LONG_MAX;
156 result |= compare (buf, expect, nresult);
158 /* Create and test input strings with sign and digits:
159 0 digits (invalid format),
160 1 digit (invalid format),
161 2 digits (valid format),
162 3 digits (invalid format),
163 4 digits (valid format if and only if minutes is in range 00-59,
164 otherwise invalid).
165 If format is valid, the returned tm_gmtoff is checked. */
167 for (ndigits = 0, step = 10000; ndigits <= 4; ndigits++, step /= 10)
168 for (hhmm = 0; hhmm <= 9999; hhmm += step)
170 /* Test both positive and negative signs. */
172 expect = mkbuf (buf, false, hhmm, ndigits);
173 result |= compare (buf, expect, nresult);
175 expect = mkbuf (buf, true, hhmm, ndigits);
176 result |= compare (buf, expect, nresult);
179 if (result > 0 || verbose)
180 printf ("%s: %u input strings: %u fail, %u pass\n",
181 result > 0 ? "FAIL" : "PASS",
182 nresult[1] + nresult[0], nresult[1], nresult[0]);
184 return result;
188 /* Add a "--verbose" command line option to test-skeleton.c. */
190 #define OPT_VERBOSE 10000
192 #define CMDLINE_OPTIONS \
193 { "verbose", no_argument, NULL, OPT_VERBOSE, },
195 #define CMDLINE_PROCESS \
196 case OPT_VERBOSE: \
197 verbose = true; \
198 break;
200 #define TEST_FUNCTION do_test ()
201 #include "../test-skeleton.c"