1 /* tst-strptime2 - Test strptime %z timezone offset specifier. */
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
20 /* Verbose execution, set with --verbose command line option. */
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
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
60 mkbuf (char *buf
, bool neg
, bool colon
, unsigned int hhmm
, size_t ndigits
)
62 const int mm_max
= 59;
63 char sign
= neg
? '-' : '+';
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
);
71 snprintf (buf
+ i
, ndigits
+ 2, "%02u:%02u", hh
, mm
);
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
;
86 /* Write a description of expected or actual test result to stdout. */
89 describe (bool string_valid
, long int tm_gmtoff
)
92 printf ("valid, tm.tm_gmtoff %ld", tm_gmtoff
);
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. */
103 compare (const char *buf
, long int expect
, unsigned int *nresult
)
107 bool test_string_valid
;
108 long int test_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
;
119 bool expect_string_valid
= expect
!= LONG_MAX
;
121 printf ("%s: input \"%s\", expected: ", fail
? "FAIL" : "PASS", buf
);
122 describe (expect_string_valid
, expect
);
127 describe (test_string_valid
, test_result
);
133 result
= fail
? 1 : 0;
143 char buf
[buffer_size
];
146 /* Number of tests run with passing (index==0) and failing (index==1)
148 unsigned int nresult
[2];
149 unsigned int ndigits
;
156 /* Create and test input string with no sign and four digits input
159 sprintf (buf
, "%s 1030", dummy_string
);
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
);
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,
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. */
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]);
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 \
223 #define TEST_FUNCTION do_test ()
224 #include "../test-skeleton.c"