1 //===-- Unittests for sscanf ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/limits.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/__support/FPUtil/PlatformDefs.h"
13 #include "src/stdio/sscanf.h"
15 #include <stdio.h> // For EOF
17 #include "test/UnitTest/FPMatcher.h"
18 #include "test/UnitTest/Test.h"
20 TEST(LlvmLibcSScanfTest
, SimpleStringConv
) {
24 ret_val
= __llvm_libc::sscanf("abc123", "abc %s", buffer
);
25 ASSERT_EQ(ret_val
, 1);
26 ASSERT_STREQ(buffer
, "123");
28 ret_val
= __llvm_libc::sscanf("abc123", "%3s %3s", buffer
, buffer2
);
29 ASSERT_EQ(ret_val
, 2);
30 ASSERT_STREQ(buffer
, "abc");
31 ASSERT_STREQ(buffer2
, "123");
33 ret_val
= __llvm_libc::sscanf("abc 123", "%3s%3s", buffer
, buffer2
);
34 ASSERT_EQ(ret_val
, 2);
35 ASSERT_STREQ(buffer
, "abc");
36 ASSERT_STREQ(buffer2
, "123");
39 TEST(LlvmLibcSScanfTest
, IntConvSimple
) {
42 ret_val
= __llvm_libc::sscanf("123", "%d", &result
);
43 EXPECT_EQ(ret_val
, 1);
44 EXPECT_EQ(result
, 123);
46 ret_val
= __llvm_libc::sscanf("456", "%i", &result
);
47 EXPECT_EQ(ret_val
, 1);
48 EXPECT_EQ(result
, 456);
50 ret_val
= __llvm_libc::sscanf("789", "%x", &result
);
51 EXPECT_EQ(ret_val
, 1);
52 EXPECT_EQ(result
, 0x789);
54 ret_val
= __llvm_libc::sscanf("012", "%o", &result
);
55 EXPECT_EQ(ret_val
, 1);
56 EXPECT_EQ(result
, 012);
58 ret_val
= __llvm_libc::sscanf("345", "%u", &result
);
59 EXPECT_EQ(ret_val
, 1);
60 EXPECT_EQ(result
, 345);
63 ret_val
= __llvm_libc::sscanf("10000000000000000000000000000000"
64 "00000000000000000000000000000000"
65 "00000000000000000000000000000000"
66 "00000000000000000000000000000000"
67 "00000000000000000000000000000000"
68 "00000000000000000000000000000000"
69 "00000000000000000000000000000000"
70 "00000000000000000000000000000000"
71 "00000000000000000000000000000000",
73 EXPECT_EQ(ret_val
, 1);
74 EXPECT_EQ(result
, int(__llvm_libc::cpp::numeric_limits
<intmax_t>::max()));
76 ret_val
= __llvm_libc::sscanf("Not an integer", "%d", &result
);
77 EXPECT_EQ(ret_val
, 0);
80 TEST(LlvmLibcSScanfTest
, IntConvLengthModifier
) {
82 uintmax_t max_result
= 0;
86 ret_val
= __llvm_libc::sscanf("123", "%ju", &max_result
);
87 EXPECT_EQ(ret_val
, 1);
88 EXPECT_EQ(max_result
, uintmax_t(123));
90 // Check overflow handling
91 ret_val
= __llvm_libc::sscanf("999999999999999999999999999999999999", "%ju",
93 EXPECT_EQ(ret_val
, 1);
94 EXPECT_EQ(max_result
, __llvm_libc::cpp::numeric_limits
<uintmax_t>::max());
96 // Because this is unsigned, any out of range value should return the maximum,
97 // even with a negative sign.
98 ret_val
= __llvm_libc::sscanf("-999999999999999999999999999999999999", "%ju",
100 EXPECT_EQ(ret_val
, 1);
101 EXPECT_EQ(max_result
, __llvm_libc::cpp::numeric_limits
<uintmax_t>::max());
103 ret_val
= __llvm_libc::sscanf("-18446744073709551616", "%ju", &max_result
);
104 EXPECT_EQ(ret_val
, 1);
105 EXPECT_EQ(max_result
, __llvm_libc::cpp::numeric_limits
<uintmax_t>::max());
107 // But any number below the maximum should have the - sign applied.
108 ret_val
= __llvm_libc::sscanf("-1", "%ju", &max_result
);
109 EXPECT_EQ(ret_val
, 1);
110 EXPECT_EQ(max_result
, uintmax_t(-1));
112 ret_val
= __llvm_libc::sscanf("-1", "%u", &int_result
);
113 EXPECT_EQ(ret_val
, 1);
114 EXPECT_EQ(int_result
, -1);
116 max_result
= 0xff00ff00ff00ff00;
119 // Overflows for sizes larger than the maximum are handled by casting.
120 ret_val
= __llvm_libc::sscanf("8589967360", "%d", &int_result
);
121 EXPECT_EQ(ret_val
, 1);
122 EXPECT_EQ(int_result
, int(8589967360)); // 2^33 + 2^15
124 // Check that the adjacent values weren't touched by the overflow.
125 ASSERT_EQ(max_result
, uintmax_t(0xff00ff00ff00ff00));
126 ASSERT_EQ(char_result
, char(0x6f));
128 ret_val
= __llvm_libc::sscanf("-8589967360", "%d", &int_result
);
129 EXPECT_EQ(ret_val
, 1);
130 EXPECT_EQ(int_result
, int(-8589967360));
131 ASSERT_EQ(max_result
, uintmax_t(0xff00ff00ff00ff00));
132 ASSERT_EQ(char_result
, char(0x6f));
134 ret_val
= __llvm_libc::sscanf("25", "%hhd", &char_result
);
135 EXPECT_EQ(ret_val
, 1);
136 EXPECT_EQ(char_result
, char(25));
139 TEST(LlvmLibcSScanfTest
, IntConvBaseSelection
) {
142 ret_val
= __llvm_libc::sscanf("0xabc123", "%i", &result
);
143 EXPECT_EQ(ret_val
, 1);
144 EXPECT_EQ(result
, 0xabc123);
146 ret_val
= __llvm_libc::sscanf("0456", "%i", &result
);
147 EXPECT_EQ(ret_val
, 1);
148 EXPECT_EQ(result
, 0456);
150 ret_val
= __llvm_libc::sscanf("0999", "%i", &result
);
151 EXPECT_EQ(ret_val
, 1);
152 EXPECT_EQ(result
, 0);
154 ret_val
= __llvm_libc::sscanf("123abc456", "%i", &result
);
155 EXPECT_EQ(ret_val
, 1);
156 EXPECT_EQ(result
, 123);
159 TEST(LlvmLibcSScanfTest
, IntConvMaxLengthTests
) {
163 ret_val
= __llvm_libc::sscanf("12", "%1d", &result
);
164 EXPECT_EQ(ret_val
, 1);
165 EXPECT_EQ(result
, 1);
167 ret_val
= __llvm_libc::sscanf("-1", "%1d", &result
);
168 EXPECT_EQ(ret_val
, 0);
169 EXPECT_EQ(result
, 0);
171 ret_val
= __llvm_libc::sscanf("+1", "%1d", &result
);
172 EXPECT_EQ(ret_val
, 0);
173 EXPECT_EQ(result
, 0);
175 ret_val
= __llvm_libc::sscanf("01", "%1d", &result
);
176 EXPECT_EQ(ret_val
, 1);
177 EXPECT_EQ(result
, 0);
179 ret_val
= __llvm_libc::sscanf("01", "%1i", &result
);
180 EXPECT_EQ(ret_val
, 1);
181 EXPECT_EQ(result
, 0);
183 ret_val
= __llvm_libc::sscanf("0x1", "%2i", &result
);
184 EXPECT_EQ(ret_val
, 1);
185 EXPECT_EQ(result
, 0);
187 ret_val
= __llvm_libc::sscanf("-0x1", "%3i", &result
);
188 EXPECT_EQ(ret_val
, 1);
189 EXPECT_EQ(result
, 0);
191 ret_val
= __llvm_libc::sscanf("-0x123", "%4i", &result
);
192 EXPECT_EQ(ret_val
, 1);
193 EXPECT_EQ(result
, -1);
195 ret_val
= __llvm_libc::sscanf("123456789", "%5i", &result
);
196 EXPECT_EQ(ret_val
, 1);
197 EXPECT_EQ(result
, 12345);
199 ret_val
= __llvm_libc::sscanf("123456789", "%10i", &result
);
200 EXPECT_EQ(ret_val
, 1);
201 EXPECT_EQ(result
, 123456789);
204 TEST(LlvmLibcSScanfTest
, IntConvNoWriteTests
) {
206 // Result shouldn't be used by these tests, but it's safer to have it and
209 ret_val
= __llvm_libc::sscanf("-1", "%*1d", &result
);
210 EXPECT_EQ(ret_val
, 0);
211 EXPECT_EQ(result
, 0);
213 ret_val
= __llvm_libc::sscanf("01", "%*1i", &result
);
214 EXPECT_EQ(ret_val
, 1);
215 EXPECT_EQ(result
, 0);
217 ret_val
= __llvm_libc::sscanf("0x1", "%*2i", &result
);
218 EXPECT_EQ(ret_val
, 1);
219 EXPECT_EQ(result
, 0);
221 ret_val
= __llvm_libc::sscanf("a", "%*i", &result
);
222 EXPECT_EQ(ret_val
, 0);
223 EXPECT_EQ(result
, 0);
225 ret_val
= __llvm_libc::sscanf("123", "%*i", &result
);
226 EXPECT_EQ(ret_val
, 1);
227 EXPECT_EQ(result
, 0);
230 TEST(LlvmLibcSScanfTest
, FloatConvSimple
) {
234 float inf
= __llvm_libc::fputil::FPBits
<float>::inf().get_val();
235 float nan
= __llvm_libc::fputil::FPBits
<float>::build_nan(1);
237 ret_val
= __llvm_libc::sscanf("123", "%f", &result
);
238 EXPECT_EQ(ret_val
, 1);
239 EXPECT_FP_EQ(result
, 123.0);
241 ret_val
= __llvm_libc::sscanf("456.1", "%a", &result
);
242 EXPECT_EQ(ret_val
, 1);
243 EXPECT_FP_EQ(result
, 456.1);
245 ret_val
= __llvm_libc::sscanf("0x789.ap0", "%e", &result
);
246 EXPECT_EQ(ret_val
, 1);
247 EXPECT_FP_EQ(result
, 0x789.ap0
);
249 ret_val
= __llvm_libc::sscanf("0x.8", "%e", &result
);
250 EXPECT_EQ(ret_val
, 1);
251 EXPECT_FP_EQ(result
, 0x0.8p0
);
253 ret_val
= __llvm_libc::sscanf("0x8.", "%e", &result
);
254 EXPECT_EQ(ret_val
, 1);
255 EXPECT_FP_EQ(result
, 0x8.0p0
);
257 ret_val
= __llvm_libc::sscanf("+12.0e1", "%g", &result
);
258 EXPECT_EQ(ret_val
, 1);
259 EXPECT_FP_EQ(result
, 12.0e1
);
261 ret_val
= __llvm_libc::sscanf("inf", "%F", &result
);
262 EXPECT_EQ(ret_val
, 1);
263 EXPECT_FP_EQ(result
, inf
);
265 ret_val
= __llvm_libc::sscanf("NaN", "%A", &result
);
266 EXPECT_EQ(ret_val
, 1);
267 EXPECT_FP_EQ(result
, nan
);
269 ret_val
= __llvm_libc::sscanf("-InFiNiTy", "%E", &result
);
270 EXPECT_EQ(ret_val
, 1);
271 EXPECT_FP_EQ(result
, -inf
);
273 ret_val
= __llvm_libc::sscanf("1e10", "%G", &result
);
274 EXPECT_EQ(ret_val
, 1);
275 EXPECT_FP_EQ(result
, 1e10
);
277 ret_val
= __llvm_libc::sscanf(".1", "%G", &result
);
278 EXPECT_EQ(ret_val
, 1);
279 EXPECT_FP_EQ(result
, 0.1);
281 ret_val
= __llvm_libc::sscanf("1.", "%G", &result
);
282 EXPECT_EQ(ret_val
, 1);
283 EXPECT_FP_EQ(result
, 1.0);
285 ret_val
= __llvm_libc::sscanf("0", "%f", &result
);
286 EXPECT_EQ(ret_val
, 1);
287 EXPECT_FP_EQ(result
, 0.0);
289 ret_val
= __llvm_libc::sscanf("Not a float", "%f", &result
);
290 EXPECT_EQ(ret_val
, 0);
293 TEST(LlvmLibcSScanfTest
, FloatConvLengthModifier
) {
296 long double ld_result
= 0;
298 double d_inf
= __llvm_libc::fputil::FPBits
<double>::inf().get_val();
299 long double ld_nan
= __llvm_libc::fputil::FPBits
<long double>::build_nan(1);
301 ret_val
= __llvm_libc::sscanf("123", "%lf", &d_result
);
302 EXPECT_EQ(ret_val
, 1);
303 EXPECT_FP_EQ(d_result
, 123.0);
305 ret_val
= __llvm_libc::sscanf("456.1", "%La", &ld_result
);
306 EXPECT_EQ(ret_val
, 1);
307 EXPECT_FP_EQ(ld_result
, 456.1L);
309 ret_val
= __llvm_libc::sscanf("inf", "%le", &d_result
);
310 EXPECT_EQ(ret_val
, 1);
311 EXPECT_FP_EQ(d_result
, d_inf
);
313 ret_val
= __llvm_libc::sscanf("nan", "%Lg", &ld_result
);
314 EXPECT_EQ(ret_val
, 1);
315 EXPECT_FP_EQ(ld_result
, ld_nan
);
317 ret_val
= __llvm_libc::sscanf("1e-300", "%lF", &d_result
);
318 EXPECT_EQ(ret_val
, 1);
319 EXPECT_FP_EQ(d_result
, 1e-300);
321 ret_val
= __llvm_libc::sscanf("1.0e600", "%LA", &ld_result
);
322 EXPECT_EQ(ret_val
, 1);
323 // 1e600 may be larger than the maximum long double (if long double is double).
324 // In that case both of these should be evaluated as inf.
325 #ifdef LONG_DOUBLE_IS_DOUBLE
326 EXPECT_FP_EQ(ld_result
, d_inf
);
328 EXPECT_FP_EQ(ld_result
, 1.0e600L
);
332 TEST(LlvmLibcSScanfTest
, FloatConvLongNumber
) {
339 __llvm_libc::sscanf("123456789012345678901234567890.0", "%f", &result
);
340 EXPECT_EQ(ret_val
, 1);
341 EXPECT_FP_EQ(result
, 123456789012345678901234567890.0f
);
344 ret_val
= __llvm_libc::sscanf(
345 "123456789012345678901234567890123456789012345678901234567890.000", "%la",
347 EXPECT_EQ(ret_val
, 1);
350 123456789012345678901234567890123456789012345678901234567890.000);
353 ret_val
= __llvm_libc::sscanf(
354 "123456789012345678901234567890123456789012345678901234567890"
355 "123456789012345678901234567890123456789012345678901234567890.0000000",
357 EXPECT_EQ(ret_val
, 1);
360 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.0000000);
363 ret_val
= __llvm_libc::sscanf("10000000000000000000000000000000"
364 "00000000000000000000000000000000"
365 "00000000000000000000000000000000"
366 "00000000000000000000000000000000"
367 "00000000000000000000000000000000"
368 "00000000000000000000000000000000"
369 "00000000000000000000000000000000"
370 "00000000000000000000000000000000",
372 EXPECT_EQ(ret_val
, 1);
373 EXPECT_FP_EQ(d_result
, 1e255
);
376 ret_val
= __llvm_libc::sscanf("10000000000000000000000000000000"
377 "00000000000000000000000000000000"
378 "00000000000000000000000000000000"
379 "00000000000000000000000000000000"
380 "00000000000000000000000000000000"
381 "00000000000000000000000000000000"
382 "00000000000000000000000000000000"
383 "00000000000000000000000000000000"
384 "00000000000000000000000000000000",
386 EXPECT_EQ(ret_val
, 1);
387 EXPECT_FP_EQ(d_result
, 1e287
);
390 TEST(LlvmLibcSScanfTest
, FloatConvComplexParsing
) {
394 float inf
= __llvm_libc::fputil::FPBits
<float>::inf().get_val();
395 float nan
= __llvm_libc::fputil::FPBits
<float>::build_nan(1);
397 ret_val
= __llvm_libc::sscanf("0x1.0e3", "%f", &result
);
398 EXPECT_EQ(ret_val
, 1);
399 EXPECT_FP_EQ(result
, 0x1.0e3p0
);
401 ret_val
= __llvm_libc::sscanf("", "%a", &result
);
402 EXPECT_EQ(ret_val
, 0);
404 ret_val
= __llvm_libc::sscanf("+", "%a", &result
);
405 EXPECT_EQ(ret_val
, 0);
407 ret_val
= __llvm_libc::sscanf("-", "%a", &result
);
408 EXPECT_EQ(ret_val
, 0);
410 ret_val
= __llvm_libc::sscanf("+.", "%a", &result
);
411 EXPECT_EQ(ret_val
, 0);
413 ret_val
= __llvm_libc::sscanf("-.e+10", "%a", &result
);
414 EXPECT_EQ(ret_val
, 0);
416 // This is a specific example from the standard. Its behavior diverges from
417 // other implementations that accept "100e" as being the same as "100e0"
418 ret_val
= __llvm_libc::sscanf("100er", "%a", &result
);
419 EXPECT_EQ(ret_val
, 0);
421 ret_val
= __llvm_libc::sscanf("nah", "%a", &result
);
422 EXPECT_EQ(ret_val
, 0);
424 ret_val
= __llvm_libc::sscanf("indirection", "%a", &result
);
425 EXPECT_EQ(ret_val
, 0);
427 ret_val
= __llvm_libc::sscanf("infnan", "%a", &result
);
428 EXPECT_EQ(ret_val
, 1);
429 EXPECT_FP_EQ(result
, inf
);
431 ret_val
= __llvm_libc::sscanf("naninf", "%a", &result
);
432 EXPECT_EQ(ret_val
, 1);
433 EXPECT_FP_EQ(result
, nan
);
435 ret_val
= __llvm_libc::sscanf("infinityinfinity", "%a", &result
);
436 EXPECT_EQ(ret_val
, 1);
437 EXPECT_FP_EQ(result
, inf
);
439 // For %f to accept a string as representing it has to be either "inf" or
440 // "infinity" when it stops. It only stops when it encounters a character that
441 // isn't the next one in the string, so it accepts "infi" as the the longest
442 // prefix of a possibly valid floating-point number, but determines that it is
443 // not valid and returns a matching failure. This is because it can only unget
444 // one character so when it finds that the character after the second 'i' is
445 // not the next character in "infinity" it can't rewind to the point where it
447 ret_val
= __llvm_libc::sscanf("infi", "%a", &result
);
448 EXPECT_EQ(ret_val
, 0);
450 ret_val
= __llvm_libc::sscanf("infinite", "%a", &result
);
451 EXPECT_EQ(ret_val
, 0);
453 ret_val
= __llvm_libc::sscanf("-.1e1", "%f", &result
);
454 EXPECT_EQ(ret_val
, 1);
455 EXPECT_FP_EQ(result
, -.1e1
);
457 ret_val
= __llvm_libc::sscanf("1.2.e1", "%f", &result
);
458 EXPECT_EQ(ret_val
, 1);
459 EXPECT_FP_EQ(result
, 1.2);
462 TEST(LlvmLibcSScanfTest
, FloatConvMaxWidth
) {
466 float inf
= __llvm_libc::fputil::FPBits
<float>::inf().get_val();
468 ret_val
= __llvm_libc::sscanf("123", "%3f", &result
);
469 EXPECT_EQ(ret_val
, 1);
470 EXPECT_FP_EQ(result
, 123.0);
472 ret_val
= __llvm_libc::sscanf("123", "%5f", &result
);
473 EXPECT_EQ(ret_val
, 1);
474 EXPECT_FP_EQ(result
, 123.0);
476 ret_val
= __llvm_libc::sscanf("456", "%1f", &result
);
477 EXPECT_EQ(ret_val
, 1);
478 EXPECT_FP_EQ(result
, 4.0);
480 ret_val
= __llvm_libc::sscanf("-789", "%1f", &result
);
481 EXPECT_EQ(ret_val
, 0);
483 ret_val
= __llvm_libc::sscanf("-123", "%2f", &result
);
484 EXPECT_EQ(ret_val
, 1);
485 EXPECT_FP_EQ(result
, -1.0);
487 ret_val
= __llvm_libc::sscanf("inf", "%2f", &result
);
488 EXPECT_EQ(ret_val
, 0);
490 ret_val
= __llvm_libc::sscanf("nan", "%1f", &result
);
491 EXPECT_EQ(ret_val
, 0);
493 ret_val
= __llvm_libc::sscanf("-inf", "%3f", &result
);
494 EXPECT_EQ(ret_val
, 0);
496 ret_val
= __llvm_libc::sscanf("-nan", "%3f", &result
);
497 EXPECT_EQ(ret_val
, 0);
499 // If the max length were not here this would fail as discussed above, but
500 // since the max length limits it to the 3 it succeeds.
501 ret_val
= __llvm_libc::sscanf("infinite", "%3f", &result
);
502 EXPECT_EQ(ret_val
, 1);
503 EXPECT_FP_EQ(result
, inf
);
505 ret_val
= __llvm_libc::sscanf("-infinite", "%4f", &result
);
506 EXPECT_EQ(ret_val
, 1);
507 EXPECT_FP_EQ(result
, -inf
);
509 ret_val
= __llvm_libc::sscanf("01", "%1f", &result
);
510 EXPECT_EQ(ret_val
, 1);
511 EXPECT_FP_EQ(result
, 0.0);
513 ret_val
= __llvm_libc::sscanf("0x1", "%2f", &result
);
514 EXPECT_EQ(ret_val
, 1);
515 EXPECT_FP_EQ(result
, 0.0);
517 ret_val
= __llvm_libc::sscanf("100e", "%4f", &result
);
518 EXPECT_EQ(ret_val
, 0);
520 ret_val
= __llvm_libc::sscanf("100e+10", "%5f", &result
);
521 EXPECT_EQ(ret_val
, 0);
523 ret_val
= __llvm_libc::sscanf("100e10", "%5f", &result
);
524 EXPECT_EQ(ret_val
, 1);
525 EXPECT_FP_EQ(result
, 100e1
);
528 TEST(LlvmLibcSScanfTest
, FloatConvNoWrite
) {
532 ret_val
= __llvm_libc::sscanf("123", "%*f", &result
);
533 EXPECT_EQ(ret_val
, 1);
534 EXPECT_FP_EQ(result
, 0.0);
536 ret_val
= __llvm_libc::sscanf("456.1", "%*a", &result
);
537 EXPECT_EQ(ret_val
, 1);
538 EXPECT_FP_EQ(result
, 0.0);
540 ret_val
= __llvm_libc::sscanf("0x789.ap0", "%*e", &result
);
541 EXPECT_EQ(ret_val
, 1);
542 EXPECT_FP_EQ(result
, 0.0);
544 ret_val
= __llvm_libc::sscanf("+12.0e1", "%*g", &result
);
545 EXPECT_EQ(ret_val
, 1);
546 EXPECT_FP_EQ(result
, 0.0);
548 ret_val
= __llvm_libc::sscanf("inf", "%*F", &result
);
549 EXPECT_EQ(ret_val
, 1);
550 EXPECT_FP_EQ(result
, 0.0);
552 ret_val
= __llvm_libc::sscanf("NaN", "%*A", &result
);
553 EXPECT_EQ(ret_val
, 1);
554 EXPECT_FP_EQ(result
, 0.0);
556 ret_val
= __llvm_libc::sscanf("-InFiNiTy", "%*E", &result
);
557 EXPECT_EQ(ret_val
, 1);
558 EXPECT_FP_EQ(result
, 0.0);
560 ret_val
= __llvm_libc::sscanf("1e10", "%*G", &result
);
561 EXPECT_EQ(ret_val
, 1);
562 EXPECT_FP_EQ(result
, 0.0);
564 ret_val
= __llvm_libc::sscanf(".1", "%*G", &result
);
565 EXPECT_EQ(ret_val
, 1);
566 EXPECT_FP_EQ(result
, 0.0);
568 ret_val
= __llvm_libc::sscanf("123", "%*3f", &result
);
569 EXPECT_EQ(ret_val
, 1);
570 EXPECT_FP_EQ(result
, 0.0);
572 ret_val
= __llvm_libc::sscanf("123", "%*5f", &result
);
573 EXPECT_EQ(ret_val
, 1);
574 EXPECT_FP_EQ(result
, 0.0);
576 ret_val
= __llvm_libc::sscanf("456", "%*1f", &result
);
577 EXPECT_EQ(ret_val
, 1);
578 EXPECT_FP_EQ(result
, 0.0);
580 ret_val
= __llvm_libc::sscanf("Not a float", "%*f", &result
);
581 EXPECT_EQ(ret_val
, 0);
584 TEST(LlvmLibcSScanfTest
, CurPosCombined
) {
589 ret_val
= __llvm_libc::sscanf("some text", "%n", &result
);
590 // %n doesn't count as a conversion for the return value.
591 EXPECT_EQ(ret_val
, 0);
592 EXPECT_EQ(result
, 0);
594 ret_val
= __llvm_libc::sscanf("1234567890", "12345%n", &result
);
595 EXPECT_EQ(ret_val
, 0);
596 EXPECT_EQ(result
, 5);
598 ret_val
= __llvm_libc::sscanf("1234567890", "12345%n", &result
);
599 EXPECT_EQ(ret_val
, 0);
600 EXPECT_EQ(result
, 5);
603 ret_val
= __llvm_libc::sscanf("10000000000000000000000000000000"
604 "00000000000000000000000000000000"
605 "00000000000000000000000000000000"
606 "00000000000000000000000000000000"
607 "00000000000000000000000000000000"
608 "00000000000000000000000000000000"
609 "00000000000000000000000000000000"
610 "00000000000000000000000000000000"
611 "00000000000000000000000000000000",
612 "%*d%hhn", &c_result
);
613 EXPECT_EQ(ret_val
, 1);
614 EXPECT_EQ(c_result
, char(288)); // Overflow is handled by casting.
617 ret_val
= __llvm_libc::sscanf("10000000000000000000000000000000"
618 "00000000000000000000000000000000"
619 "00000000000000000000000000000000"
620 "00000000000000000000000000000000"
621 "00000000000000000000000000000000"
622 "00000000000000000000000000000000"
623 "00000000000000000000000000000000"
624 "00000000000000000000000000000000"
625 "00000000000000000000000000000000"
626 "00000000000000000000000000000000",
628 EXPECT_EQ(ret_val
, 1);
629 EXPECT_EQ(result
, 320);
632 TEST(LlvmLibcSScanfTest
, PointerConvCombined
) {
636 ret_val
= __llvm_libc::sscanf("(nullptr)", "%p", &result
);
637 EXPECT_EQ(ret_val
, 1);
638 EXPECT_EQ(result
, static_cast<void *>(nullptr));
640 ret_val
= __llvm_libc::sscanf("(NuLlPtR)", "%p", &result
);
641 EXPECT_EQ(ret_val
, 1);
642 EXPECT_EQ(result
, static_cast<void *>(nullptr));
644 ret_val
= __llvm_libc::sscanf("(NULLPTR)", "%p", &result
);
645 EXPECT_EQ(ret_val
, 1);
646 EXPECT_EQ(result
, static_cast<void *>(nullptr));
648 ret_val
= __llvm_libc::sscanf("(null)", "%p", &result
);
649 EXPECT_EQ(ret_val
, 0);
651 ret_val
= __llvm_libc::sscanf("(nullptr2", "%p", &result
);
652 EXPECT_EQ(ret_val
, 0);
654 ret_val
= __llvm_libc::sscanf("0", "%p", &result
);
655 EXPECT_EQ(ret_val
, 1);
656 EXPECT_EQ(result
, reinterpret_cast<void *>(0));
658 ret_val
= __llvm_libc::sscanf("100", "%p", &result
);
659 EXPECT_EQ(ret_val
, 1);
660 EXPECT_EQ(result
, reinterpret_cast<void *>(0x100));
662 ret_val
= __llvm_libc::sscanf("-1", "%p", &result
);
663 EXPECT_EQ(ret_val
, 1);
664 EXPECT_EQ(result
, reinterpret_cast<void *>(-1));
666 ret_val
= __llvm_libc::sscanf("0xabcDEFG", "%p", &result
);
667 EXPECT_EQ(ret_val
, 1);
668 EXPECT_EQ(result
, reinterpret_cast<void *>(0xabcdef));
671 TEST(LlvmLibcSScanfTest
, CombinedConv
) {
675 ret_val
= __llvm_libc::sscanf("123abc", "%i%s", &result
, buffer
);
676 EXPECT_EQ(ret_val
, 2);
677 EXPECT_EQ(result
, 123);
678 ASSERT_STREQ(buffer
, "abc");
680 ret_val
= __llvm_libc::sscanf("0xZZZ", "%i%s", &result
, buffer
);
681 EXPECT_EQ(ret_val
, 2);
682 EXPECT_EQ(result
, 0);
683 ASSERT_STREQ(buffer
, "ZZZ");
685 ret_val
= __llvm_libc::sscanf("0xZZZ", "%X%s", &result
, buffer
);
686 EXPECT_EQ(ret_val
, 2);
687 EXPECT_EQ(result
, 0);
688 ASSERT_STREQ(buffer
, "ZZZ");