]>
git.ipfire.org Git - thirdparty/glibc.git/blob - time/tst-strptime2.c
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 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
58 mkbuf (char *buf
, bool neg
, unsigned int hhmm
, size_t ndigits
)
60 const int mm_max
= 59;
61 char sign
= neg
? '-' : '+';
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
;
81 /* Write a description of expected or actual test result to stdout. */
84 describe (bool string_valid
, long int tm_gmtoff
)
87 printf ("valid, tm.tm_gmtoff %ld", tm_gmtoff
);
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. */
98 compare (const char *buf
, long int expect
, unsigned int *nresult
)
102 bool test_string_valid
;
103 long int test_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
;
114 bool expect_string_valid
= expect
!= LONG_MAX
;
116 printf ("%s: input \"%s\", expected: ", fail
? "FAIL" : "PASS", buf
);
117 describe (expect_string_valid
, expect
);
122 describe (test_string_valid
, test_result
);
128 result
= fail
? 1 : 0;
138 char buf
[buffer_size
];
141 /* Number of tests run with passing (index==0) and failing (index==1)
143 unsigned int nresult
[2];
144 unsigned int ndigits
;
151 /* Create and test input string with no sign and four digits input
154 sprintf (buf
, "%s 1030", dummy_string
);
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,
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]);
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 \
200 #define TEST_FUNCTION do_test ()
201 #include "../test-skeleton.c"