1 //===-- Unittests for sprintf ---------------------------------------------===//
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/macros/config.h"
10 #include "src/stdio/sprintf.h"
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "src/errno/libc_errno.h"
14 #include "test/UnitTest/RoundingModeUtils.h"
15 #include "test/UnitTest/Test.h"
18 // TODO: Add a comment here explaining the printf format string.
21 // namespace LIBC_NAMESPACE_DECL {
25 using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode
;
26 using LIBC_NAMESPACE::fputil::testing::RoundingMode
;
28 // Subtract 1 from sizeof(expected_str) to account for the null byte.
29 #define ASSERT_STREQ_LEN(actual_written, actual_str, expected_str) \
30 EXPECT_EQ(actual_written, static_cast<int>(sizeof(expected_str) - 1)); \
31 EXPECT_STREQ(actual_str, expected_str);
33 #define macro_test(FMT, X, expected) \
35 for (char &c : buff) { \
38 written = LIBC_NAMESPACE::sprintf(buff, "%" FMT, X); \
39 ASSERT_STREQ_LEN(written, buff, expected); \
42 TEST(LlvmLibcSPrintfTest
, Macros
) {
45 macro_test(PRIu8
, 1, "1");
46 macro_test(PRIX16
, 0xAA, "AA");
47 macro_test(PRId32
, -123, "-123");
48 macro_test(PRIX32
, 0xFFFFFF85, "FFFFFF85");
49 macro_test(PRIo8
, 0xFF, "377");
50 macro_test(PRIo64
, 0123456712345671234567ll, "123456712345671234567");
53 TEST(LlvmLibcSPrintfTest
, SimpleNoConv
) {
58 LIBC_NAMESPACE::sprintf(buff
, "A simple string with no conversions.");
59 ASSERT_STREQ_LEN(written
, buff
, "A simple string with no conversions.");
62 TEST(LlvmLibcSPrintfTest
, PercentConv
) {
66 written
= LIBC_NAMESPACE::sprintf(buff
, "%%");
67 ASSERT_STREQ_LEN(written
, buff
, "%");
69 written
= LIBC_NAMESPACE::sprintf(buff
, "abc %% def");
70 ASSERT_STREQ_LEN(written
, buff
, "abc % def");
72 written
= LIBC_NAMESPACE::sprintf(buff
, "%%%%%%");
73 ASSERT_STREQ_LEN(written
, buff
, "%%%");
76 TEST(LlvmLibcSPrintfTest
, CharConv
) {
80 written
= LIBC_NAMESPACE::sprintf(buff
, "%c", 'a');
81 ASSERT_STREQ_LEN(written
, buff
, "a");
83 written
= LIBC_NAMESPACE::sprintf(buff
, "%3c %-3c", '1', '2');
84 ASSERT_STREQ_LEN(written
, buff
, " 1 2 ");
86 written
= LIBC_NAMESPACE::sprintf(buff
, "%*c", 2, '3');
87 ASSERT_STREQ_LEN(written
, buff
, " 3");
90 TEST(LlvmLibcSPrintfTest
, StringConv
) {
94 written
= LIBC_NAMESPACE::sprintf(buff
, "%s", "abcDEF123");
95 ASSERT_STREQ_LEN(written
, buff
, "abcDEF123");
97 written
= LIBC_NAMESPACE::sprintf(buff
, "%10s %-10s", "centered", "title");
98 ASSERT_STREQ_LEN(written
, buff
, " centered title ");
100 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.4s%-4.4s", "words can describe",
101 "soups most delicious");
102 ASSERT_STREQ_LEN(written
, buff
, "word soup");
104 written
= LIBC_NAMESPACE::sprintf(buff
, "%*s %.*s %*.*s", 10, "beginning", 2,
105 "isn't", 12, 10, "important. Ever.");
106 ASSERT_STREQ_LEN(written
, buff
, " beginning is important.");
108 #ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
109 written
= LIBC_NAMESPACE::sprintf(buff
, "%s", nullptr);
110 ASSERT_STREQ_LEN(written
, buff
, "(null)");
111 #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
114 TEST(LlvmLibcSPrintfTest
, IntConv
) {
120 written
= LIBC_NAMESPACE::sprintf(buff
, "%d", 123);
121 ASSERT_STREQ_LEN(written
, buff
, "123");
123 written
= LIBC_NAMESPACE::sprintf(buff
, "%i", -456);
124 ASSERT_STREQ_LEN(written
, buff
, "-456");
126 // Length Modifier Tests.
128 written
= LIBC_NAMESPACE::sprintf(buff
, "%hhu", 257); // 0x101
129 ASSERT_STREQ_LEN(written
, buff
, "1");
131 written
= LIBC_NAMESPACE::sprintf(buff
, "%llu", 18446744073709551615ull);
132 ASSERT_STREQ_LEN(written
, buff
, "18446744073709551615"); // ull max
134 written
= LIBC_NAMESPACE::sprintf(buff
, "%u", ~0);
135 if (sizeof(int) == 4) {
136 ASSERT_STREQ_LEN(written
, buff
, "4294967295");
139 written
= LIBC_NAMESPACE::sprintf(buff
, "%tu", ~ptrdiff_t(0));
140 if (sizeof(ptrdiff_t) == 8) {
141 ASSERT_STREQ_LEN(written
, buff
, "18446744073709551615");
142 } else if (sizeof(ptrdiff_t) == 4) {
143 ASSERT_STREQ_LEN(written
, buff
, "4294967295");
146 written
= LIBC_NAMESPACE::sprintf(buff
, "%lld", -9223372036854775807ll - 1ll);
147 ASSERT_STREQ_LEN(written
, buff
, "-9223372036854775808"); // ll min
149 written
= LIBC_NAMESPACE::sprintf(buff
, "%w3d", 5807);
150 ASSERT_STREQ_LEN(written
, buff
, "7");
152 written
= LIBC_NAMESPACE::sprintf(buff
, "%w3d", 1);
153 ASSERT_STREQ_LEN(written
, buff
, "1");
155 written
= LIBC_NAMESPACE::sprintf(buff
, "%w64d", 9223372036854775807ll);
156 ASSERT_STREQ_LEN(written
, buff
, "9223372036854775807");
158 written
= LIBC_NAMESPACE::sprintf(buff
, "%w-1d", 5807);
159 ASSERT_STREQ_LEN(written
, buff
, "%w-1d");
161 written
= LIBC_NAMESPACE::sprintf(buff
, "%w0d", 5807);
162 ASSERT_STREQ_LEN(written
, buff
, "%w0d");
164 written
= LIBC_NAMESPACE::sprintf(buff
, "%w999d", 9223372036854775807ll);
165 ASSERT_STREQ_LEN(written
, buff
, "9223372036854775807");
167 written
= LIBC_NAMESPACE::sprintf(buff
, "%winvalid%w1d", 5807, 5807);
168 ASSERT_STREQ_LEN(written
, buff
, "%winvalid1");
170 written
= LIBC_NAMESPACE::sprintf(buff
, "%w-1d%w1d", 5807, 5807);
171 ASSERT_STREQ_LEN(written
, buff
, "%w-1d1");
175 LIBC_NAMESPACE::sprintf(format
, "%%w%du", sizeof(uintmax_t) * CHAR_BIT
);
176 const int uintmax_len
=
177 LIBC_NAMESPACE::sprintf(uintmax
, "%ju", sizeof(uintmax_t) * CHAR_BIT
);
178 written
= LIBC_NAMESPACE::sprintf(buff
, format
, sizeof(uintmax_t) * CHAR_BIT
);
179 EXPECT_EQ(written
, uintmax_len
);
180 ASSERT_STREQ(buff
, uintmax
);
182 written
= LIBC_NAMESPACE::sprintf(buff
, "%w64u", 18446744073709551615ull);
183 ASSERT_STREQ_LEN(written
, buff
, "18446744073709551615"); // ull max
186 LIBC_NAMESPACE::sprintf(buff
, "%w64d", -9223372036854775807ll - 1ll);
187 ASSERT_STREQ_LEN(written
, buff
, "-9223372036854775808"); // ll min
189 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf3d", 5807);
190 ASSERT_STREQ_LEN(written
, buff
, "7");
192 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf3d", 1);
193 ASSERT_STREQ_LEN(written
, buff
, "1");
195 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf64u", 18446744073709551615ull);
196 ASSERT_STREQ_LEN(written
, buff
, "18446744073709551615"); // ull max
199 LIBC_NAMESPACE::sprintf(buff
, "%wf64d", -9223372036854775807ll - 1ll);
200 ASSERT_STREQ_LEN(written
, buff
, "-9223372036854775808"); // ll min
202 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf0d", 5807);
203 ASSERT_STREQ_LEN(written
, buff
, "%wf0d");
205 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf-1d", 5807);
206 ASSERT_STREQ_LEN(written
, buff
, "%wf-1d");
208 written
= LIBC_NAMESPACE::sprintf(buff
, "%wfinvalid%wf1d", 5807, 5807);
209 ASSERT_STREQ_LEN(written
, buff
, "%wfinvalid1");
211 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf-1d%wf1d", 5807, 5807);
212 ASSERT_STREQ_LEN(written
, buff
, "%wf-1d1");
214 written
= LIBC_NAMESPACE::sprintf(buff
, "%wf999d", 9223372036854775807ll);
215 ASSERT_STREQ_LEN(written
, buff
, "9223372036854775807");
219 written
= LIBC_NAMESPACE::sprintf(buff
, "%4d", 789);
220 ASSERT_STREQ_LEN(written
, buff
, " 789");
222 written
= LIBC_NAMESPACE::sprintf(buff
, "%2d", 987);
223 ASSERT_STREQ_LEN(written
, buff
, "987");
227 written
= LIBC_NAMESPACE::sprintf(buff
, "%d", 0);
228 ASSERT_STREQ_LEN(written
, buff
, "0");
230 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0d", 0);
231 ASSERT_STREQ_LEN(written
, buff
, "");
233 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5d", 654);
234 ASSERT_STREQ_LEN(written
, buff
, "00654");
236 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5d", -321);
237 ASSERT_STREQ_LEN(written
, buff
, "-00321");
239 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2d", 135);
240 ASSERT_STREQ_LEN(written
, buff
, "135");
244 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5d", -321);
245 ASSERT_STREQ_LEN(written
, buff
, "-00321");
247 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5d", 246);
248 ASSERT_STREQ_LEN(written
, buff
, "246 ");
250 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5d", -147);
251 ASSERT_STREQ_LEN(written
, buff
, "-147 ");
253 written
= LIBC_NAMESPACE::sprintf(buff
, "%+d", 258);
254 ASSERT_STREQ_LEN(written
, buff
, "+258");
256 written
= LIBC_NAMESPACE::sprintf(buff
, "% d", 369);
257 ASSERT_STREQ_LEN(written
, buff
, " 369");
259 written
= LIBC_NAMESPACE::sprintf(buff
, "%05d", 470);
260 ASSERT_STREQ_LEN(written
, buff
, "00470");
262 written
= LIBC_NAMESPACE::sprintf(buff
, "%05d", -581);
263 ASSERT_STREQ_LEN(written
, buff
, "-0581");
267 written
= LIBC_NAMESPACE::sprintf(buff
, "%+ u", 692);
268 ASSERT_STREQ_LEN(written
, buff
, "692");
270 written
= LIBC_NAMESPACE::sprintf(buff
, "%+ -05d", 703);
271 ASSERT_STREQ_LEN(written
, buff
, "+703 ");
273 written
= LIBC_NAMESPACE::sprintf(buff
, "%7.5d", 814);
274 ASSERT_STREQ_LEN(written
, buff
, " 00814");
276 written
= LIBC_NAMESPACE::sprintf(buff
, "%7.5d", -925);
277 ASSERT_STREQ_LEN(written
, buff
, " -00925");
279 written
= LIBC_NAMESPACE::sprintf(buff
, "%7.5d", 159);
280 ASSERT_STREQ_LEN(written
, buff
, " 00159");
282 written
= LIBC_NAMESPACE::sprintf(buff
, "% -7.5d", 260);
283 ASSERT_STREQ_LEN(written
, buff
, " 00260 ");
285 written
= LIBC_NAMESPACE::sprintf(buff
, "%5.4d", 10000);
286 ASSERT_STREQ_LEN(written
, buff
, "10000");
288 // Multiple Conversion Tests.
290 written
= LIBC_NAMESPACE::sprintf(buff
, "%10d %-10d", 456, -789);
291 ASSERT_STREQ_LEN(written
, buff
, " 456 -789 ");
293 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.4d%+.4u", 75, 25);
294 ASSERT_STREQ_LEN(written
, buff
, "0075 0025");
296 written
= LIBC_NAMESPACE::sprintf(buff
, "% 05hhi %+-0.5llu %-+ 06.3zd",
297 256 + 127, 68719476736ll, size_t(2));
298 ASSERT_STREQ_LEN(written
, buff
, " 0127 68719476736 +002 ");
301 TEST(LlvmLibcSPrintfTest
, HexConv
) {
307 written
= LIBC_NAMESPACE::sprintf(buff
, "%x", 0x123a);
308 ASSERT_STREQ_LEN(written
, buff
, "123a");
310 written
= LIBC_NAMESPACE::sprintf(buff
, "%X", 0x456b);
311 ASSERT_STREQ_LEN(written
, buff
, "456B");
313 // Length Modifier Tests.
315 written
= LIBC_NAMESPACE::sprintf(buff
, "%hhx", 0x10001);
316 ASSERT_STREQ_LEN(written
, buff
, "1");
318 written
= LIBC_NAMESPACE::sprintf(buff
, "%llx", 0xffffffffffffffffull
);
319 ASSERT_STREQ_LEN(written
, buff
, "ffffffffffffffff"); // ull max
321 written
= LIBC_NAMESPACE::sprintf(buff
, "%tX", ~ptrdiff_t(0));
322 if (sizeof(ptrdiff_t) == 8) {
323 ASSERT_STREQ_LEN(written
, buff
, "FFFFFFFFFFFFFFFF");
324 } else if (sizeof(ptrdiff_t) == 4) {
325 ASSERT_STREQ_LEN(written
, buff
, "FFFFFFFF");
330 written
= LIBC_NAMESPACE::sprintf(buff
, "%4x", 0x789);
331 ASSERT_STREQ_LEN(written
, buff
, " 789");
333 written
= LIBC_NAMESPACE::sprintf(buff
, "%2X", 0x987);
334 ASSERT_STREQ_LEN(written
, buff
, "987");
338 written
= LIBC_NAMESPACE::sprintf(buff
, "%x", 0);
339 ASSERT_STREQ_LEN(written
, buff
, "0");
341 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0x", 0);
342 ASSERT_STREQ_LEN(written
, buff
, "");
344 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5x", 0x1F3);
345 ASSERT_STREQ_LEN(written
, buff
, "001f3");
347 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2x", 0x135);
348 ASSERT_STREQ_LEN(written
, buff
, "135");
352 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5x", 0x246);
353 ASSERT_STREQ_LEN(written
, buff
, "246 ");
355 written
= LIBC_NAMESPACE::sprintf(buff
, "%#x", 0xd3f);
356 ASSERT_STREQ_LEN(written
, buff
, "0xd3f");
358 written
= LIBC_NAMESPACE::sprintf(buff
, "%#x", 0);
359 ASSERT_STREQ_LEN(written
, buff
, "0");
361 written
= LIBC_NAMESPACE::sprintf(buff
, "%#X", 0xE40);
362 ASSERT_STREQ_LEN(written
, buff
, "0XE40");
364 written
= LIBC_NAMESPACE::sprintf(buff
, "%05x", 0x470);
365 ASSERT_STREQ_LEN(written
, buff
, "00470");
367 written
= LIBC_NAMESPACE::sprintf(buff
, "%0#6x", 0x8c3);
368 ASSERT_STREQ_LEN(written
, buff
, "0x08c3");
370 written
= LIBC_NAMESPACE::sprintf(buff
, "%-#6x", 0x5f0);
371 ASSERT_STREQ_LEN(written
, buff
, "0x5f0 ");
375 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-07x", 0x703);
376 ASSERT_STREQ_LEN(written
, buff
, "0x703 ");
378 written
= LIBC_NAMESPACE::sprintf(buff
, "%7.5x", 0x814);
379 ASSERT_STREQ_LEN(written
, buff
, " 00814");
381 written
= LIBC_NAMESPACE::sprintf(buff
, "%#9.5X", 0x9d4);
382 ASSERT_STREQ_LEN(written
, buff
, " 0X009D4");
384 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.x", 0);
385 ASSERT_STREQ_LEN(written
, buff
, "");
387 written
= LIBC_NAMESPACE::sprintf(buff
, "%-7.5x", 0x260);
388 ASSERT_STREQ_LEN(written
, buff
, "00260 ");
390 written
= LIBC_NAMESPACE::sprintf(buff
, "%5.4x", 0x10000);
391 ASSERT_STREQ_LEN(written
, buff
, "10000");
393 // Multiple Conversion Tests.
395 written
= LIBC_NAMESPACE::sprintf(buff
, "%10X %-#10x", 0x45b, 0x789);
396 ASSERT_STREQ_LEN(written
, buff
, " 45B 0x789 ");
398 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.4x%#.4x", 0x75, 0x25);
399 ASSERT_STREQ_LEN(written
, buff
, "0075 0x0025");
401 written
= LIBC_NAMESPACE::sprintf(buff
, "%04hhX %#.5llx %-6.3zX", 256 + 0x7f,
402 0x1000000000ll
, size_t(2));
403 ASSERT_STREQ_LEN(written
, buff
, "007F 0x1000000000 002 ");
406 TEST(LlvmLibcSPrintfTest
, BinConv
) {
412 written
= LIBC_NAMESPACE::sprintf(buff
, "%b", 42);
413 ASSERT_STREQ_LEN(written
, buff
, "101010");
415 written
= LIBC_NAMESPACE::sprintf(buff
, "%B", 12081991);
416 ASSERT_STREQ_LEN(written
, buff
, "101110000101101101000111");
420 written
= LIBC_NAMESPACE::sprintf(buff
, "%10b", 0b101010);
421 ASSERT_STREQ_LEN(written
, buff
, " 101010");
423 written
= LIBC_NAMESPACE::sprintf(buff
, "%2B", 0b101010);
424 ASSERT_STREQ_LEN(written
, buff
, "101010");
428 written
= LIBC_NAMESPACE::sprintf(buff
, "%b", 0);
429 ASSERT_STREQ_LEN(written
, buff
, "0");
431 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0b", 0);
432 ASSERT_STREQ_LEN(written
, buff
, "");
434 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5b", 0b111);
435 ASSERT_STREQ_LEN(written
, buff
, "00111");
437 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2b", 0b111);
438 ASSERT_STREQ_LEN(written
, buff
, "111");
440 written
= LIBC_NAMESPACE::sprintf(buff
, "%3b", 0b111);
441 ASSERT_STREQ_LEN(written
, buff
, "111");
445 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5b", 0b111);
446 ASSERT_STREQ_LEN(written
, buff
, "111 ");
448 written
= LIBC_NAMESPACE::sprintf(buff
, "%#b", 0b111);
449 ASSERT_STREQ_LEN(written
, buff
, "0b111");
451 written
= LIBC_NAMESPACE::sprintf(buff
, "%#b", 0);
452 ASSERT_STREQ_LEN(written
, buff
, "0");
454 written
= LIBC_NAMESPACE::sprintf(buff
, "%#B", 0b111);
455 ASSERT_STREQ_LEN(written
, buff
, "0B111");
457 written
= LIBC_NAMESPACE::sprintf(buff
, "%05b", 0b111);
458 ASSERT_STREQ_LEN(written
, buff
, "00111");
460 written
= LIBC_NAMESPACE::sprintf(buff
, "%0#6b", 0b111);
461 ASSERT_STREQ_LEN(written
, buff
, "0b0111");
463 written
= LIBC_NAMESPACE::sprintf(buff
, "%-#6b", 0b111);
464 ASSERT_STREQ_LEN(written
, buff
, "0b111 ");
468 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-07b", 0b111);
469 ASSERT_STREQ_LEN(written
, buff
, "0b111 ");
471 written
= LIBC_NAMESPACE::sprintf(buff
, "%7.5b", 0b111);
472 ASSERT_STREQ_LEN(written
, buff
, " 00111");
474 written
= LIBC_NAMESPACE::sprintf(buff
, "%#9.5B", 0b111);
475 ASSERT_STREQ_LEN(written
, buff
, " 0B00111");
477 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.b", 0);
478 ASSERT_STREQ_LEN(written
, buff
, "");
480 written
= LIBC_NAMESPACE::sprintf(buff
, "%-7.5b", 0b111);
481 ASSERT_STREQ_LEN(written
, buff
, "00111 ");
483 written
= LIBC_NAMESPACE::sprintf(buff
, "%5.4b", 0b1111);
484 ASSERT_STREQ_LEN(written
, buff
, " 1111");
486 // Multiple Conversion Tests.
488 written
= LIBC_NAMESPACE::sprintf(buff
, "%10B %-#10b", 0b101, 0b110);
489 ASSERT_STREQ_LEN(written
, buff
, " 101 0b110 ");
491 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.4b%#.4b", 0b101, 0b110);
492 ASSERT_STREQ_LEN(written
, buff
, "0101 0b0110");
495 TEST(LlvmLibcSPrintfTest
, PointerConv
) {
499 written
= LIBC_NAMESPACE::sprintf(buff
, "%p", nullptr);
500 ASSERT_STREQ_LEN(written
, buff
, "(nullptr)");
502 written
= LIBC_NAMESPACE::sprintf(buff
, "%p", 0x1a2b3c4d);
503 ASSERT_STREQ_LEN(written
, buff
, "0x1a2b3c4d");
505 if constexpr (sizeof(void *) > 4) {
506 written
= LIBC_NAMESPACE::sprintf(buff
, "%p", 0x1a2b3c4d5e6f7081);
507 ASSERT_STREQ_LEN(written
, buff
, "0x1a2b3c4d5e6f7081");
510 written
= LIBC_NAMESPACE::sprintf(buff
, "%p", &written
);
511 EXPECT_GT(written
, 0);
515 written
= LIBC_NAMESPACE::sprintf(buff
, "%20p", nullptr);
516 ASSERT_STREQ_LEN(written
, buff
, " (nullptr)");
518 written
= LIBC_NAMESPACE::sprintf(buff
, "%20p", 0x1a2b3c4d);
519 ASSERT_STREQ_LEN(written
, buff
, " 0x1a2b3c4d");
523 written
= LIBC_NAMESPACE::sprintf(buff
, "%-20p", nullptr);
524 ASSERT_STREQ_LEN(written
, buff
, "(nullptr) ");
526 written
= LIBC_NAMESPACE::sprintf(buff
, "%-20p", 0x1a2b3c4d);
527 ASSERT_STREQ_LEN(written
, buff
, "0x1a2b3c4d ");
529 // Using the 0 flag is technically undefined, but here we're following the
530 // convention of matching the behavior of %#x.
531 written
= LIBC_NAMESPACE::sprintf(buff
, "%020p", 0x1a2b3c4d);
532 ASSERT_STREQ_LEN(written
, buff
, "0x00000000001a2b3c4d");
535 // These are all undefined behavior. The precision option is undefined for %p.
537 // Precision specifies the number of characters for a string conversion.
538 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5p", nullptr);
539 ASSERT_STREQ_LEN(written
, buff
, "(null");
541 // Precision specifies the number of digits to be written for %x conversions,
542 // and the "0x" doesn't count as part of the digits.
543 written
= LIBC_NAMESPACE::sprintf(buff
, "%.20p", 0x1a2b3c4d);
544 ASSERT_STREQ_LEN(written
, buff
, "0x0000000000001a2b3c4d");
547 TEST(LlvmLibcSPrintfTest
, OctConv
) {
553 written
= LIBC_NAMESPACE::sprintf(buff
, "%o", 01234);
554 ASSERT_STREQ_LEN(written
, buff
, "1234");
556 written
= LIBC_NAMESPACE::sprintf(buff
, "%o", 04567);
557 ASSERT_STREQ_LEN(written
, buff
, "4567");
559 // Length Modifier Tests.
561 written
= LIBC_NAMESPACE::sprintf(buff
, "%hho", 0401);
562 ASSERT_STREQ_LEN(written
, buff
, "1");
564 written
= LIBC_NAMESPACE::sprintf(buff
, "%llo", 01777777777777777777777ull);
565 ASSERT_STREQ_LEN(written
, buff
, "1777777777777777777777"); // ull max
567 written
= LIBC_NAMESPACE::sprintf(buff
, "%to", ~ptrdiff_t(0));
568 if (sizeof(ptrdiff_t) == 8) {
569 ASSERT_STREQ_LEN(written
, buff
, "1777777777777777777777");
570 } else if (sizeof(ptrdiff_t) == 4) {
571 ASSERT_STREQ_LEN(written
, buff
, "37777777777");
576 written
= LIBC_NAMESPACE::sprintf(buff
, "%4o", 0701);
577 ASSERT_STREQ_LEN(written
, buff
, " 701");
579 written
= LIBC_NAMESPACE::sprintf(buff
, "%2o", 0107);
580 ASSERT_STREQ_LEN(written
, buff
, "107");
584 written
= LIBC_NAMESPACE::sprintf(buff
, "%o", 0);
585 ASSERT_STREQ_LEN(written
, buff
, "0");
587 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0o", 0);
588 ASSERT_STREQ_LEN(written
, buff
, "");
590 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5o", 0153);
591 ASSERT_STREQ_LEN(written
, buff
, "00153");
593 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2o", 0135);
594 ASSERT_STREQ_LEN(written
, buff
, "135");
598 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5o", 0246);
599 ASSERT_STREQ_LEN(written
, buff
, "246 ");
601 written
= LIBC_NAMESPACE::sprintf(buff
, "%#o", 0234);
602 ASSERT_STREQ_LEN(written
, buff
, "0234");
604 written
= LIBC_NAMESPACE::sprintf(buff
, "%#o", 0);
605 ASSERT_STREQ_LEN(written
, buff
, "0");
607 written
= LIBC_NAMESPACE::sprintf(buff
, "%05o", 0470);
608 ASSERT_STREQ_LEN(written
, buff
, "00470");
610 written
= LIBC_NAMESPACE::sprintf(buff
, "%0#6o", 0753);
611 ASSERT_STREQ_LEN(written
, buff
, "000753");
613 written
= LIBC_NAMESPACE::sprintf(buff
, "%-#6o", 0642);
614 ASSERT_STREQ_LEN(written
, buff
, "0642 ");
618 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-07o", 0703);
619 ASSERT_STREQ_LEN(written
, buff
, "0703 ");
621 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.o", 0);
622 ASSERT_STREQ_LEN(written
, buff
, "0");
624 written
= LIBC_NAMESPACE::sprintf(buff
, "%7.5o", 0314);
625 ASSERT_STREQ_LEN(written
, buff
, " 00314");
627 written
= LIBC_NAMESPACE::sprintf(buff
, "%#9.5o", 0234);
628 ASSERT_STREQ_LEN(written
, buff
, " 00234");
630 written
= LIBC_NAMESPACE::sprintf(buff
, "%-7.5o", 0260);
631 ASSERT_STREQ_LEN(written
, buff
, "00260 ");
633 written
= LIBC_NAMESPACE::sprintf(buff
, "%5.4o", 010000);
634 ASSERT_STREQ_LEN(written
, buff
, "10000");
636 // Multiple Conversion Tests.
638 written
= LIBC_NAMESPACE::sprintf(buff
, "%10o %-#10o", 0456, 0123);
639 ASSERT_STREQ_LEN(written
, buff
, " 456 0123 ");
641 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.4o%#.4o", 075, 025);
642 ASSERT_STREQ_LEN(written
, buff
, "0075 0025");
644 written
= LIBC_NAMESPACE::sprintf(buff
, "%04hho %#.5llo %-6.3zo", 256 + 077,
645 01000000000000ll, size_t(2));
646 ASSERT_STREQ_LEN(written
, buff
, "0077 01000000000000 002 ");
649 #ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
651 TEST(LlvmLibcSPrintfTest
, FloatHexExpConv
) {
655 ForceRoundingMode
r(RoundingMode::Nearest
);
656 double inf
= LIBC_NAMESPACE::fputil::FPBits
<double>::inf().get_val();
657 double nan
= LIBC_NAMESPACE::fputil::FPBits
<double>::quiet_nan().get_val();
658 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 1.0);
659 ASSERT_STREQ_LEN(written
, buff
, "0x1p+0");
661 written
= LIBC_NAMESPACE::sprintf(buff
, "%A", -1.0);
662 ASSERT_STREQ_LEN(written
, buff
, "-0X1P+0");
664 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", -0x1.abcdef12345p0
);
665 ASSERT_STREQ_LEN(written
, buff
, "-0x1.abcdef12345p+0");
667 written
= LIBC_NAMESPACE::sprintf(buff
, "%A", 0x1.abcdef12345p0
);
668 ASSERT_STREQ_LEN(written
, buff
, "0X1.ABCDEF12345P+0");
670 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0.0);
671 ASSERT_STREQ_LEN(written
, buff
, "0x0p+0");
673 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 1.0e100
);
674 ASSERT_STREQ_LEN(written
, buff
, "0x1.249ad2594c37dp+332");
676 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0.1);
677 ASSERT_STREQ_LEN(written
, buff
, "0x1.999999999999ap-4");
681 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0x1.0p
-1027);
682 ASSERT_STREQ_LEN(written
, buff
, "0x0.08p-1022");
684 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0x1.0p
-1025);
685 ASSERT_STREQ_LEN(written
, buff
, "0x0.2p-1022");
687 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0x1.0p
-1023);
688 ASSERT_STREQ_LEN(written
, buff
, "0x0.8p-1022");
690 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0x1.0p
-1022);
691 ASSERT_STREQ_LEN(written
, buff
, "0x1p-1022");
693 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", 0x1.0p
-1074);
694 ASSERT_STREQ_LEN(written
, buff
, "0x0.0000000000001p-1022");
698 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", inf
);
699 ASSERT_STREQ_LEN(written
, buff
, "inf");
701 written
= LIBC_NAMESPACE::sprintf(buff
, "%A", -inf
);
702 ASSERT_STREQ_LEN(written
, buff
, "-INF");
704 written
= LIBC_NAMESPACE::sprintf(buff
, "%a", nan
);
705 ASSERT_STREQ_LEN(written
, buff
, "nan");
707 written
= LIBC_NAMESPACE::sprintf(buff
, "%A", -nan
);
708 ASSERT_STREQ_LEN(written
, buff
, "-NAN");
710 // Length Modifier Tests.
712 written
= LIBC_NAMESPACE::sprintf(buff
, "%La", 0.1L);
713 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
714 ASSERT_STREQ_LEN(written
, buff
, "0xc.ccccccccccccccdp-7");
715 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
716 ASSERT_STREQ_LEN(written
, buff
, "0x1.999999999999ap-4");
717 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
718 ASSERT_STREQ_LEN(written
, buff
, "0x1.999999999999999999999999999ap-4");
721 written
= LIBC_NAMESPACE::sprintf(buff
, "%La", 1.0e1000L
);
722 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
723 ASSERT_STREQ_LEN(written
, buff
, "0xf.38db1f9dd3dac05p+3318");
724 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
725 ASSERT_STREQ_LEN(written
, buff
, "inf");
726 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
727 ASSERT_STREQ_LEN(written
, buff
, "0x1.e71b63f3ba7b580af1a52d2a7379p+3321");
730 written
= LIBC_NAMESPACE::sprintf(buff
, "%La", 1.0e-1000L);
731 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
732 ASSERT_STREQ_LEN(written
, buff
, "0x8.68a9188a89e1467p-3325");
733 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
734 ASSERT_STREQ_LEN(written
, buff
, "0x0p+0");
735 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
736 ASSERT_STREQ_LEN(written
, buff
, "0x1.0d152311513c28ce202627c06ec2p-3322");
741 written
= LIBC_NAMESPACE::sprintf(buff
, "%15a", 1.0);
742 ASSERT_STREQ_LEN(written
, buff
, " 0x1p+0");
744 written
= LIBC_NAMESPACE::sprintf(buff
, "%15a", -1.0);
745 ASSERT_STREQ_LEN(written
, buff
, " -0x1p+0");
747 written
= LIBC_NAMESPACE::sprintf(buff
, "%15a", 1.0e10
);
748 ASSERT_STREQ_LEN(written
, buff
, " 0x1.2a05f2p+33");
750 written
= LIBC_NAMESPACE::sprintf(buff
, "%15a", -1.0e10
);
751 ASSERT_STREQ_LEN(written
, buff
, "-0x1.2a05f2p+33");
753 written
= LIBC_NAMESPACE::sprintf(buff
, "%10a", 1.0e10
);
754 ASSERT_STREQ_LEN(written
, buff
, "0x1.2a05f2p+33");
756 written
= LIBC_NAMESPACE::sprintf(buff
, "%5a", inf
);
757 ASSERT_STREQ_LEN(written
, buff
, " inf");
759 written
= LIBC_NAMESPACE::sprintf(buff
, "%5a", -nan
);
760 ASSERT_STREQ_LEN(written
, buff
, " -nan");
764 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 1.0);
765 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
767 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0.0);
768 ASSERT_STREQ_LEN(written
, buff
, "0x0.0p+0");
770 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0.1);
771 ASSERT_STREQ_LEN(written
, buff
, "0x1.ap-4");
773 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.0fp0
);
774 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
776 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.07p0
);
777 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
779 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.08p0
);
780 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
782 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.18p0
);
783 ASSERT_STREQ_LEN(written
, buff
, "0x1.2p+0");
785 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.ffp0
);
786 ASSERT_STREQ_LEN(written
, buff
, "0x2.0p+0");
788 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5a", 1.25);
789 ASSERT_STREQ_LEN(written
, buff
, "0x1.40000p+0");
791 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0a", 1.25);
792 ASSERT_STREQ_LEN(written
, buff
, "0x1p+0");
794 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0a", 1.75);
795 ASSERT_STREQ_LEN(written
, buff
, "0x2p+0");
797 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.0p
-1023);
798 ASSERT_STREQ_LEN(written
, buff
, "0x0.8p-1022");
800 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.8p
-1023);
801 ASSERT_STREQ_LEN(written
, buff
, "0x0.cp-1022");
803 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.0p
-1024);
804 ASSERT_STREQ_LEN(written
, buff
, "0x0.4p-1022");
806 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0a", 0x1.0p
-1023);
807 ASSERT_STREQ_LEN(written
, buff
, "0x0p-1022");
809 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0a", 0x1.8p
-1023);
810 ASSERT_STREQ_LEN(written
, buff
, "0x1p-1022");
812 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0a", 0x1.0p
-1024);
813 ASSERT_STREQ_LEN(written
, buff
, "0x0p-1022");
815 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2a", 0x1.0p
-1027);
816 ASSERT_STREQ_LEN(written
, buff
, "0x0.08p-1022");
818 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.0p
-1027);
819 ASSERT_STREQ_LEN(written
, buff
, "0x0.0p-1022");
821 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5a", 0.0);
822 ASSERT_STREQ_LEN(written
, buff
, "0x0.00000p+0");
824 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5a", 0x1.008p0
);
825 ASSERT_STREQ_LEN(written
, buff
, "0x1.00800p+0");
827 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5a", 0x1.008p10
);
828 ASSERT_STREQ_LEN(written
, buff
, "0x1.00800p+10");
830 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5a", nan
);
831 ASSERT_STREQ_LEN(written
, buff
, "nan");
833 written
= LIBC_NAMESPACE::sprintf(buff
, "%La", 0.0L);
834 ASSERT_STREQ_LEN(written
, buff
, "0x0p+0");
836 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1La", 0.1L);
837 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
838 ASSERT_STREQ_LEN(written
, buff
, "0xc.dp-7");
839 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
840 ASSERT_STREQ_LEN(written
, buff
, "0x1.ap-4");
841 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
842 ASSERT_STREQ_LEN(written
, buff
, "0x1.ap-4");
845 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1La", 0xf.fffffffffffffffp16380L
);
846 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
847 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+16384");
848 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
849 ASSERT_STREQ_LEN(written
, buff
, "inf");
850 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
851 ASSERT_STREQ_LEN(written
, buff
, "0x2.0p+16383");
854 // Rounding Mode Tests.
856 if (ForceRoundingMode
r(RoundingMode::Nearest
); r
.success
) {
857 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.08p0
);
858 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
860 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.18p0
);
861 ASSERT_STREQ_LEN(written
, buff
, "0x1.2p+0");
863 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.04p0
);
864 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
866 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.14p0
);
867 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
869 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.08p0
);
870 ASSERT_STREQ_LEN(written
, buff
, "-0x1.0p+0");
872 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.18p0
);
873 ASSERT_STREQ_LEN(written
, buff
, "-0x1.2p+0");
875 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.04p0
);
876 ASSERT_STREQ_LEN(written
, buff
, "-0x1.0p+0");
878 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.14p0
);
879 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
882 if (ForceRoundingMode
r(RoundingMode::Upward
); r
.success
) {
883 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.08p0
);
884 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
886 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.18p0
);
887 ASSERT_STREQ_LEN(written
, buff
, "0x1.2p+0");
889 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.04p0
);
890 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
892 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.14p0
);
893 ASSERT_STREQ_LEN(written
, buff
, "0x1.2p+0");
895 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.08p0
);
896 ASSERT_STREQ_LEN(written
, buff
, "-0x1.0p+0");
898 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.18p0
);
899 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
901 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.04p0
);
902 ASSERT_STREQ_LEN(written
, buff
, "-0x1.0p+0");
904 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.14p0
);
905 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
908 if (ForceRoundingMode
r(RoundingMode::Downward
); r
.success
) {
909 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.08p0
);
910 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
912 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.18p0
);
913 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
915 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.04p0
);
916 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
918 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.14p0
);
919 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
921 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.08p0
);
922 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
924 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.18p0
);
925 ASSERT_STREQ_LEN(written
, buff
, "-0x1.2p+0");
927 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.04p0
);
928 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
930 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.14p0
);
931 ASSERT_STREQ_LEN(written
, buff
, "-0x1.2p+0");
934 if (ForceRoundingMode
r(RoundingMode::TowardZero
); r
.success
) {
935 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.08p0
);
936 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
938 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.18p0
);
939 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
941 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.04p0
);
942 ASSERT_STREQ_LEN(written
, buff
, "0x1.0p+0");
944 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", 0x1.14p0
);
945 ASSERT_STREQ_LEN(written
, buff
, "0x1.1p+0");
947 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.08p0
);
948 ASSERT_STREQ_LEN(written
, buff
, "-0x1.0p+0");
950 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.18p0
);
951 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
953 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.04p0
);
954 ASSERT_STREQ_LEN(written
, buff
, "-0x1.0p+0");
956 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1a", -0x1.14p0
);
957 ASSERT_STREQ_LEN(written
, buff
, "-0x1.1p+0");
962 written
= LIBC_NAMESPACE::sprintf(buff
, "%+a", nan
);
963 ASSERT_STREQ_LEN(written
, buff
, "+nan");
965 written
= LIBC_NAMESPACE::sprintf(buff
, "% A", inf
);
966 ASSERT_STREQ_LEN(written
, buff
, " INF");
968 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5a", inf
);
969 ASSERT_STREQ_LEN(written
, buff
, "inf ");
971 written
= LIBC_NAMESPACE::sprintf(buff
, "%+-5A", nan
);
972 ASSERT_STREQ_LEN(written
, buff
, "+NAN ");
974 written
= LIBC_NAMESPACE::sprintf(buff
, "%+a", 1.0);
975 ASSERT_STREQ_LEN(written
, buff
, "+0x1p+0");
977 written
= LIBC_NAMESPACE::sprintf(buff
, "% a", 0.0);
978 ASSERT_STREQ_LEN(written
, buff
, " 0x0p+0");
980 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10a", 1.5);
981 ASSERT_STREQ_LEN(written
, buff
, "0x1.8p+0 ");
983 written
= LIBC_NAMESPACE::sprintf(buff
, "%#a", 1.0);
984 ASSERT_STREQ_LEN(written
, buff
, "0x1.p+0");
986 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.0a", 1.5);
987 ASSERT_STREQ_LEN(written
, buff
, "0x2.p+0");
989 written
= LIBC_NAMESPACE::sprintf(buff
, "%010a", 1.5);
990 ASSERT_STREQ_LEN(written
, buff
, "0x001.8p+0");
992 written
= LIBC_NAMESPACE::sprintf(buff
, "%+- #0a", 0.0);
993 ASSERT_STREQ_LEN(written
, buff
, "+0x0.p+0");
997 written
= LIBC_NAMESPACE::sprintf(buff
, "%12.3a %-12.3A", 0.1, 256.0);
998 ASSERT_STREQ_LEN(written
, buff
, " 0x1.99ap-4 0X1.000P+8 ");
1000 written
= LIBC_NAMESPACE::sprintf(buff
, "%+-#12.3a % 012.3a", 0.1256, 1256.0);
1001 ASSERT_STREQ_LEN(written
, buff
, "+0x1.014p-3 0x1.3a0p+10");
1003 // These tests check that the padding is properly calculated based on the
1004 // min_width field. Specifically, they check that the extra zeroes added by
1005 // the high precision are accounted for correctly.
1006 written
= LIBC_NAMESPACE::sprintf(buff
, "%50.50a", 0x1.0p0
);
1007 ASSERT_STREQ_LEN(written
, buff
,
1008 "0x1.00000000000000000000000000000000000000000000000000p+0");
1010 // The difference with this test is that the formatted number is exactly 57
1011 // characters, so padding to 58 adds a space.
1012 written
= LIBC_NAMESPACE::sprintf(buff
, "%58.50a", 0x1.0p0
);
1015 " 0x1.00000000000000000000000000000000000000000000000000p+0");
1018 TEST(LlvmLibcSPrintfTest
, FloatDecimalConv
) {
1022 ForceRoundingMode
r(RoundingMode::Nearest
);
1023 double inf
= LIBC_NAMESPACE::fputil::FPBits
<double>::inf().get_val();
1024 double nan
= LIBC_NAMESPACE::fputil::FPBits
<double>::quiet_nan().get_val();
1025 long double ld_inf
=
1026 LIBC_NAMESPACE::fputil::FPBits
<long double>::inf().get_val();
1027 long double ld_nan
=
1028 LIBC_NAMESPACE::fputil::FPBits
<long double>::quiet_nan().get_val();
1030 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 1.0);
1031 ASSERT_STREQ_LEN(written
, buff
, "1.000000");
1033 written
= LIBC_NAMESPACE::sprintf(buff
, "%F", -1.0);
1034 ASSERT_STREQ_LEN(written
, buff
, "-1.000000");
1036 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", -1.234567);
1037 ASSERT_STREQ_LEN(written
, buff
, "-1.234567");
1039 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 0.0);
1040 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
1042 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 1.5);
1043 ASSERT_STREQ_LEN(written
, buff
, "1.500000");
1045 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1046 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 1e300
);
1049 "100000000000000005250476025520442024870446858110815915491585411551180245"
1050 "798890819578637137508044786404370444383288387817694252323536043057564479"
1051 "218478670698284838720092657580373783023379478809005936895323497079994508"
1052 "111903896764088007465274278014249457925878882005684283811566947219638686"
1053 "5459400540160.000000");
1054 #endif // LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1056 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 0.1);
1057 ASSERT_STREQ_LEN(written
, buff
, "0.100000");
1059 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 1234567890123456789.0);
1060 ASSERT_STREQ_LEN(written
, buff
, "1234567890123456768.000000");
1062 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 9999999999999.99);
1063 ASSERT_STREQ_LEN(written
, buff
, "9999999999999.990234");
1065 // Simple Subnormal Tests.
1067 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 0x1.0p
-1027);
1068 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
1070 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 0x1.0p
-1074);
1071 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
1075 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", inf
);
1076 ASSERT_STREQ_LEN(written
, buff
, "inf");
1078 written
= LIBC_NAMESPACE::sprintf(buff
, "%F", -inf
);
1079 ASSERT_STREQ_LEN(written
, buff
, "-INF");
1081 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", nan
);
1082 ASSERT_STREQ_LEN(written
, buff
, "nan");
1084 written
= LIBC_NAMESPACE::sprintf(buff
, "%F", -nan
);
1085 ASSERT_STREQ_LEN(written
, buff
, "-NAN");
1087 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lf", ld_inf
);
1088 ASSERT_STREQ_LEN(written
, buff
, "inf");
1090 written
= LIBC_NAMESPACE::sprintf(buff
, "%LF", -ld_inf
);
1091 ASSERT_STREQ_LEN(written
, buff
, "-INF");
1093 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lf", ld_nan
);
1094 ASSERT_STREQ_LEN(written
, buff
, "nan");
1096 // Some float128 systems (specifically the ones used for aarch64 buildbots)
1097 // don't respect signs for long double NaNs.
1098 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80) || \
1099 defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
1100 written
= LIBC_NAMESPACE::sprintf(buff
, "%LF", -ld_nan
);
1101 ASSERT_STREQ_LEN(written
, buff
, "-NAN");
1106 written
= LIBC_NAMESPACE::sprintf(buff
, "%15f", 1.0);
1107 ASSERT_STREQ_LEN(written
, buff
, " 1.000000");
1109 written
= LIBC_NAMESPACE::sprintf(buff
, "%15f", -1.0);
1110 ASSERT_STREQ_LEN(written
, buff
, " -1.000000");
1112 written
= LIBC_NAMESPACE::sprintf(buff
, "%15f", 1.0e5
);
1113 ASSERT_STREQ_LEN(written
, buff
, " 100000.000000");
1115 written
= LIBC_NAMESPACE::sprintf(buff
, "%15f", -1.0e5
);
1116 ASSERT_STREQ_LEN(written
, buff
, " -100000.000000");
1118 written
= LIBC_NAMESPACE::sprintf(buff
, "%10f", 1.0e5
);
1119 ASSERT_STREQ_LEN(written
, buff
, "100000.000000");
1123 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.0);
1124 ASSERT_STREQ_LEN(written
, buff
, "1.0");
1126 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 0.0);
1127 ASSERT_STREQ_LEN(written
, buff
, "0.0");
1129 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0f", 0.0);
1130 ASSERT_STREQ_LEN(written
, buff
, "0");
1132 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 0.1);
1133 ASSERT_STREQ_LEN(written
, buff
, "0.1");
1135 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.09);
1136 ASSERT_STREQ_LEN(written
, buff
, "1.1");
1138 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.04);
1139 ASSERT_STREQ_LEN(written
, buff
, "1.0");
1141 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.19);
1142 ASSERT_STREQ_LEN(written
, buff
, "1.2");
1144 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.99);
1145 ASSERT_STREQ_LEN(written
, buff
, "2.0");
1147 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 9.99);
1148 ASSERT_STREQ_LEN(written
, buff
, "10.0");
1150 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2f", 9999999999999.99);
1151 ASSERT_STREQ_LEN(written
, buff
, "9999999999999.99");
1153 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 9999999999999.99);
1154 ASSERT_STREQ_LEN(written
, buff
, "10000000000000.0");
1156 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5f", 1.25);
1157 ASSERT_STREQ_LEN(written
, buff
, "1.25000");
1159 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0f", 1.25);
1160 ASSERT_STREQ_LEN(written
, buff
, "1");
1162 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0f", 1.75);
1163 ASSERT_STREQ_LEN(written
, buff
, "2");
1165 written
= LIBC_NAMESPACE::sprintf(buff
, "%.20f", 1.234e-10);
1166 ASSERT_STREQ_LEN(written
, buff
, "0.00000000012340000000");
1168 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2f", -9.99);
1169 ASSERT_STREQ_LEN(written
, buff
, "-9.99");
1171 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -9.99);
1172 ASSERT_STREQ_LEN(written
, buff
, "-10.0");
1174 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5f", 0.0);
1175 ASSERT_STREQ_LEN(written
, buff
, "0.00000");
1177 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5f", 1.008);
1178 ASSERT_STREQ_LEN(written
, buff
, "1.00800");
1180 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5f", 1.008e3
);
1181 ASSERT_STREQ_LEN(written
, buff
, "1008.00000");
1183 // Found with the help of Fred Tydeman's tbin2dec test.
1184 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 0x1.1000000000006p
+3);
1185 ASSERT_STREQ_LEN(written
, buff
, "8.5");
1187 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0f", 0x1.1000000000006p
+3);
1188 ASSERT_STREQ_LEN(written
, buff
, "9");
1190 // Most of these tests are checking rounding behavior when the precision is
1191 // set. As an example, %.9f has a precision of 9, meaning it should be rounded
1192 // to 9 digits after the decimal point. In this case, that means that it
1193 // should be rounded up. Many of these tests have precisions divisible by 9
1194 // since when printing the floating point numbers are broken up into "blocks"
1195 // of 9 digits. They often also have a 5 after the end of what's printed,
1196 // since in round to nearest mode, that requires checking additional digits.
1197 written
= LIBC_NAMESPACE::sprintf(buff
, "%.9f", 1.9999999999999514);
1198 ASSERT_STREQ_LEN(written
, buff
, "2.000000000");
1200 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1201 // The number continues after the literal because floating point numbers can't
1202 // represent every value. The printed value is the closest value a double can
1203 // represent, rounded to the requested precision.
1204 written
= LIBC_NAMESPACE::sprintf(buff
, "%.238f", 1.131959884853339E-72);
1208 "000000000000000000000000000000000000000000000000000000000000000000000001"
1209 "131959884853339045938639911360973972585316399767392273697826861241937664"
1210 "824105639342441431495119762431744054912109728706985341609159156917030486"
1211 "5110665559768676757812");
1214 written
= LIBC_NAMESPACE::sprintf(buff
, "%.36f", 9.9e-77);
1215 ASSERT_STREQ_LEN(written
, buff
, "0.000000000000000000000000000000000000");
1217 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1218 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1071f", 2.0226568751604562E-314);
1222 "000000000000000000000000000000000000000000000000000000000000000000000000"
1223 "000000000000000000000000000000000000000000000000000000000000000000000000"
1224 "000000000000000000000000000000000000000000000000000000000000000000000000"
1225 "000000000000000000000000000000000000000000000000000000000000000000000000"
1226 "000000000000000000000000020226568751604561683387695750739190248658016786"
1227 "876938365740768295004457513021760887468117675879956193821375945376632621"
1228 "367998639317487303530427946024002091961988296562516210434394107910027236"
1229 "308233439098296717697919471698168200340836487924061502604112643734560622"
1230 "258525943451473162532620033398739382796482175564084902819878893430369431"
1231 "907237673154867595954110791891883281880339550955455702452422857027182100"
1232 "606009588295886640782228837851739241290179512817803196347460636150182981"
1233 "085084829941917048152725177119574542042352896161225179181967347829576272"
1234 "242480201291872969114441104973910102402751449901108484914924879541248714"
1235 "939096548775588293353689592872854495101242645279589976452453829724479805"
1236 "750016448075109469332839157162950982637994457036256790161132812");
1239 // If no precision is specified it defaults to 6 for %f.
1240 written
= LIBC_NAMESPACE::sprintf(buff
, "%f", 2325885.4901960781);
1241 ASSERT_STREQ_LEN(written
, buff
, "2325885.490196");
1243 // Subnormal Precision Tests
1245 written
= LIBC_NAMESPACE::sprintf(buff
, "%.310f", 0x1.0p
-1022);
1249 "000000000000000000000000000000000000000000000000000000000000000000000000"
1250 "000000000000000000000000000000000000000000000000000000000000000000000000"
1251 "000000000000000000000000000000000000000000000000000000000000000000000000"
1252 "000000000000000000000000000000000000000000000000000000000000000000000000"
1253 "0000000000000000000223");
1255 written
= LIBC_NAMESPACE::sprintf(buff
, "%.310f", 0x1.0p
-1023);
1259 "000000000000000000000000000000000000000000000000000000000000000000000000"
1260 "000000000000000000000000000000000000000000000000000000000000000000000000"
1261 "000000000000000000000000000000000000000000000000000000000000000000000000"
1262 "000000000000000000000000000000000000000000000000000000000000000000000000"
1263 "0000000000000000000111");
1265 written
= LIBC_NAMESPACE::sprintf(buff
, "%.315f", 9.99999e-310);
1269 "000000000000000000000000000000000000000000000000000000000000000000000000"
1270 "000000000000000000000000000000000000000000000000000000000000000000000000"
1271 "000000000000000000000000000000000000000000000000000000000000000000000000"
1272 "000000000000000000000000000000000000000000000000000000000000000000000000"
1273 "000000000000000000000999999");
1275 written
= LIBC_NAMESPACE::sprintf(buff
, "%.314f", 9.99999e-310);
1279 "000000000000000000000000000000000000000000000000000000000000000000000000"
1280 "000000000000000000000000000000000000000000000000000000000000000000000000"
1281 "000000000000000000000000000000000000000000000000000000000000000000000000"
1282 "000000000000000000000000000000000000000000000000000000000000000000000000"
1283 "00000000000000000000100000");
1285 written
= LIBC_NAMESPACE::sprintf(buff
, "%.330f", 0x1.0p
-1074);
1289 "000000000000000000000000000000000000000000000000000000000000000000000000"
1290 "000000000000000000000000000000000000000000000000000000000000000000000000"
1291 "000000000000000000000000000000000000000000000000000000000000000000000000"
1292 "000000000000000000000000000000000000000000000000000000000000000000000000"
1293 "000000000000000000000000000000000004940656");
1295 // Rounding Mode Tests.
1297 if (ForceRoundingMode
r(RoundingMode::Nearest
); r
.success
) {
1298 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.75);
1299 ASSERT_STREQ_LEN(written
, buff
, "1.8");
1301 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.25);
1302 ASSERT_STREQ_LEN(written
, buff
, "1.2");
1304 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.125);
1305 ASSERT_STREQ_LEN(written
, buff
, "1.1");
1307 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.625);
1308 ASSERT_STREQ_LEN(written
, buff
, "1.6");
1310 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.375);
1311 ASSERT_STREQ_LEN(written
, buff
, "1.4");
1313 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.875);
1314 ASSERT_STREQ_LEN(written
, buff
, "1.9");
1316 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.75);
1317 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
1319 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.25);
1320 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
1322 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.125);
1323 ASSERT_STREQ_LEN(written
, buff
, "-1.1");
1325 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.625);
1326 ASSERT_STREQ_LEN(written
, buff
, "-1.6");
1328 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.375);
1329 ASSERT_STREQ_LEN(written
, buff
, "-1.4");
1331 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.875);
1332 ASSERT_STREQ_LEN(written
, buff
, "-1.9");
1335 if (ForceRoundingMode
r(RoundingMode::Upward
); r
.success
) {
1336 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.75);
1337 ASSERT_STREQ_LEN(written
, buff
, "1.8");
1339 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.25);
1340 ASSERT_STREQ_LEN(written
, buff
, "1.3");
1342 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.125);
1343 ASSERT_STREQ_LEN(written
, buff
, "1.2");
1345 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.625);
1346 ASSERT_STREQ_LEN(written
, buff
, "1.7");
1348 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.375);
1349 ASSERT_STREQ_LEN(written
, buff
, "1.4");
1351 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.875);
1352 ASSERT_STREQ_LEN(written
, buff
, "1.9");
1354 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.75);
1355 ASSERT_STREQ_LEN(written
, buff
, "-1.7");
1357 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.25);
1358 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
1360 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.125);
1361 ASSERT_STREQ_LEN(written
, buff
, "-1.1");
1363 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.625);
1364 ASSERT_STREQ_LEN(written
, buff
, "-1.6");
1366 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.375);
1367 ASSERT_STREQ_LEN(written
, buff
, "-1.3");
1369 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.875);
1370 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
1373 if (ForceRoundingMode
r(RoundingMode::Downward
); r
.success
) {
1374 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.75);
1375 ASSERT_STREQ_LEN(written
, buff
, "1.7");
1377 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.25);
1378 ASSERT_STREQ_LEN(written
, buff
, "1.2");
1380 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.125);
1381 ASSERT_STREQ_LEN(written
, buff
, "1.1");
1383 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.625);
1384 ASSERT_STREQ_LEN(written
, buff
, "1.6");
1386 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.375);
1387 ASSERT_STREQ_LEN(written
, buff
, "1.3");
1389 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.875);
1390 ASSERT_STREQ_LEN(written
, buff
, "1.8");
1392 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.75);
1393 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
1395 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.25);
1396 ASSERT_STREQ_LEN(written
, buff
, "-1.3");
1398 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.125);
1399 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
1401 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.625);
1402 ASSERT_STREQ_LEN(written
, buff
, "-1.7");
1404 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.375);
1405 ASSERT_STREQ_LEN(written
, buff
, "-1.4");
1407 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.875);
1408 ASSERT_STREQ_LEN(written
, buff
, "-1.9");
1411 if (ForceRoundingMode
r(RoundingMode::TowardZero
); r
.success
) {
1412 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.75);
1413 ASSERT_STREQ_LEN(written
, buff
, "1.7");
1415 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.25);
1416 ASSERT_STREQ_LEN(written
, buff
, "1.2");
1418 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.125);
1419 ASSERT_STREQ_LEN(written
, buff
, "1.1");
1421 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.625);
1422 ASSERT_STREQ_LEN(written
, buff
, "1.6");
1424 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.375);
1425 ASSERT_STREQ_LEN(written
, buff
, "1.3");
1427 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", 1.875);
1428 ASSERT_STREQ_LEN(written
, buff
, "1.8");
1430 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.75);
1431 ASSERT_STREQ_LEN(written
, buff
, "-1.7");
1433 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.25);
1434 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
1436 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.125);
1437 ASSERT_STREQ_LEN(written
, buff
, "-1.1");
1439 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.625);
1440 ASSERT_STREQ_LEN(written
, buff
, "-1.6");
1442 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.375);
1443 ASSERT_STREQ_LEN(written
, buff
, "-1.3");
1445 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1f", -1.875);
1446 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
1450 written
= LIBC_NAMESPACE::sprintf(buff
, "%+f", 1.0);
1451 ASSERT_STREQ_LEN(written
, buff
, "+1.000000");
1453 written
= LIBC_NAMESPACE::sprintf(buff
, "%+f", -1.0);
1454 ASSERT_STREQ_LEN(written
, buff
, "-1.000000");
1456 written
= LIBC_NAMESPACE::sprintf(buff
, "% f", 1.0);
1457 ASSERT_STREQ_LEN(written
, buff
, " 1.000000");
1459 written
= LIBC_NAMESPACE::sprintf(buff
, "% f", -1.0);
1460 ASSERT_STREQ_LEN(written
, buff
, "-1.000000");
1462 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10f", 1.5);
1463 ASSERT_STREQ_LEN(written
, buff
, "1.500000 ");
1465 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.f", 1.0);
1466 ASSERT_STREQ_LEN(written
, buff
, "1.");
1468 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.0f", 1.5);
1469 ASSERT_STREQ_LEN(written
, buff
, "2.");
1471 written
= LIBC_NAMESPACE::sprintf(buff
, "%010f", 1.5);
1472 ASSERT_STREQ_LEN(written
, buff
, "001.500000");
1474 written
= LIBC_NAMESPACE::sprintf(buff
, "%010f", -1.5);
1475 ASSERT_STREQ_LEN(written
, buff
, "-01.500000");
1477 written
= LIBC_NAMESPACE::sprintf(buff
, "%+- #0f", 0.0);
1478 ASSERT_STREQ_LEN(written
, buff
, "+0.000000");
1482 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.2f", 9.99);
1483 ASSERT_STREQ_LEN(written
, buff
, " 9.99");
1485 written
= LIBC_NAMESPACE::sprintf(buff
, "%5.1f", 9.99);
1486 ASSERT_STREQ_LEN(written
, buff
, " 10.0");
1488 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.2f", 9.99);
1489 ASSERT_STREQ_LEN(written
, buff
, "9.99 ");
1491 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.1f", 9.99);
1492 ASSERT_STREQ_LEN(written
, buff
, "10.0 ");
1494 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.1f", 1.0e-50);
1495 ASSERT_STREQ_LEN(written
, buff
, "0.0 ");
1497 written
= LIBC_NAMESPACE::sprintf(buff
, "%30f", 1234567890123456789.0);
1498 ASSERT_STREQ_LEN(written
, buff
, " 1234567890123456768.000000");
1500 written
= LIBC_NAMESPACE::sprintf(buff
, "%-30f", 1234567890123456789.0);
1501 ASSERT_STREQ_LEN(written
, buff
, "1234567890123456768.000000 ");
1503 written
= LIBC_NAMESPACE::sprintf(buff
, "%20.2f", 9999999999999.99);
1504 ASSERT_STREQ_LEN(written
, buff
, " 9999999999999.99");
1506 written
= LIBC_NAMESPACE::sprintf(buff
, "%20.1f", 9999999999999.99);
1507 ASSERT_STREQ_LEN(written
, buff
, " 10000000000000.0");
1509 written
= LIBC_NAMESPACE::sprintf(buff
, "%12.3f %-12.3f", 0.1, 256.0);
1510 ASSERT_STREQ_LEN(written
, buff
, " 0.100 256.000 ");
1512 written
= LIBC_NAMESPACE::sprintf(buff
, "%+-#12.3f % 012.3f", 0.1256, 1256.0);
1513 ASSERT_STREQ_LEN(written
, buff
, "+0.126 0001256.000");
1516 // The long double tests are separated so that their performance can be directly
1518 TEST(LlvmLibcSPrintfTest
, FloatDecimalLongDoubleConv
) {
1522 ForceRoundingMode
r(RoundingMode::Nearest
);
1524 // Length Modifier Tests.
1526 // TODO(michaelrj): Add tests for LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64 and 128
1527 // bit long double systems.
1528 // TODO(michaelrj): Fix the tests to only depend on the digits the long double
1531 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lf", 1.0L);
1532 ASSERT_STREQ_LEN(written
, buff
, "1.000000");
1534 written
= LIBC_NAMESPACE::sprintf(buff
, "%.Lf", -2.5L);
1535 ASSERT_STREQ_LEN(written
, buff
, "-2");
1537 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
1539 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1540 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lf", 1e100L
);
1541 ASSERT_STREQ_LEN(written
, buff
,
1542 "99999999999999999996693535322073426194986990198284960792713"
1543 "91541752018669482644324418977840117055488.000000");
1546 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lf", 0xd.96ed1192687859ap
-24L);
1547 ASSERT_STREQ_LEN(written
, buff
, "0.000001");
1549 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lf", 10000000000000000.25L);
1550 ASSERT_STREQ_LEN(written
, buff
, "10000000000000000.250000");
1552 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1553 written
= LIBC_NAMESPACE::sprintf(buff
, "%.510Lf", 0x8p
-503L);
1557 "000000000000000000000000000000000000000000000000000000000000000000000000"
1558 "000000000000000000000000000000000000000000000000000000000000000000000000"
1559 "000000305493636349960468205197939321361769978940274057232666389361390928"
1560 "129162652472045770185723510801522825687515269359046715531785342780428396"
1561 "973513311420091788963072442053377285222203558881953188370081650866793017"
1562 "948791366338993705251636497892270212003524508209121908744820211960149463"
1563 "721109340307985507678283651836204093399373959982767701148986816406250000"
1567 written
= LIBC_NAMESPACE::sprintf(buff
, "%.500Lf", -4327677766926336.0L);
1570 "-4327677766926336."
1571 "000000000000000000000000000000000000000000000000000000000000000000000000"
1572 "000000000000000000000000000000000000000000000000000000000000000000000000"
1573 "000000000000000000000000000000000000000000000000000000000000000000000000"
1574 "000000000000000000000000000000000000000000000000000000000000000000000000"
1575 "000000000000000000000000000000000000000000000000000000000000000000000000"
1576 "000000000000000000000000000000000000000000000000000000000000000000000000"
1577 "00000000000000000000000000000000000000000000000000000000000000000000");
1579 char big_buff
[10000]; // Used for extremely wide numbers.
1581 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1582 written
= LIBC_NAMESPACE::sprintf(big_buff
, "%Lf", 1e1000L
);
1585 "999999999999999999973107317669562353428234857594552594925899449376328728"
1586 "202461036775511405481186963193066642191664822065529414252060696836533522"
1587 "387143501724276282079456797058697369889056407118642873669166717313763499"
1588 "277025985141177344925615052465165938514140943010597323750202561187880136"
1589 "174810574553749194614479541820148407958204853833697063267336294787191005"
1590 "628217462261955103745349844675732989944229689277833828743730290177882029"
1591 "042613704915899149603539993716885598351951895974316347947147507970269673"
1592 "097709017164643598452451201499004104341931127294141495501309305995449742"
1593 "273419524803597130450457553871345958049837885085168840317195672271085085"
1594 "950520957945970913451088104971436093671776829538796532762184174216651692"
1595 "640931965387852083906784898823494867055070322768919156031682291829761007"
1596 "101483799978382119231551218582499361996919560548090784230386907125151658"
1597 "086767207295524036170321059257942621398084478974000973622199163292708506"
1598 "2431457550909271560663602154947063707982236377366647567795879936."
1601 written
= LIBC_NAMESPACE::sprintf(big_buff
, "%Lf", 1e4900L
);
1604 "100000000000000000002708312230690349833224052504078834346502930111959028"
1605 "517260692666637048230414374897655201843766090626319971729765251179632020"
1606 "313912652522792711197087872698264530532442630109549129842736280196919130"
1607 "242615101228133188193853826983121366159061148351354364472807590931218045"
1608 "387490935930967150336231085015126034696883068553581691802388371635128003"
1609 "615577299166097675723780877126495909902479233742826339471026068806070433"
1610 "075629449530819183550315434973800271862658869400009022028602967197463980"
1611 "126881829804282202449930132940824361207087494829502385835258094836304011"
1612 "876250359661206802659650567866176246063987902366800491980400341950657151"
1613 "370854446585517805253310195469184699955519312761482572080479702840420595"
1614 "377369017651259376039167277822106875560385309101650382998482652792335482"
1615 "865443482342801545877390859444282105890147577937366066315975231014810320"
1616 "888482059656248277607763361589359794524314002443575149260630989130103550"
1617 "443177966380769341050735632338583912575890190136462629316287947355057647"
1618 "111088565611192544631519843618778618820046304429723908484879583579178075"
1619 "456701368334212923379389029311286386996015804122917416008806233549005183"
1620 "152461084266176543129004016414959261473645240454289630182591200574019087"
1621 "358223489767381636349719510715487188747217311279465814538495924567014916"
1622 "238565628036285599497236493491668884212847699052761266207598941300449276"
1623 "447201387520841811835583254242213093566548778954711633721122784159793843"
1624 "766802019309395771984693609426401362800013936338891483689127845928572536"
1625 "790651156184721483511507878883282891696900630100211914227950790472211403"
1626 "392549466062537498185758854079775888444518306635752468713312357556380082"
1627 "275500658967283696421824354930077523691855699312544373220921962817907078"
1628 "445538421941800259027487429330768616490865438859612697367766323925013940"
1629 "918384858952407145253573823848733994146335416209309233074165707437420756"
1630 "438833918763109580759409985573826485055208965115587885226774453455112406"
1631 "581351429640282227888764449360534584421929291565334894907337572527922691"
1632 "473242328379737396430908523008687037407295838014450772162091496534584696"
1633 "605157436893236842602956298545594095307060870397506421786236892553632163"
1634 "491468601982681381011940409602294892199042638682530687578982576819839451"
1635 "907594697546439533559153604700750696252355362322662219852740143212566818"
1636 "745528402265116534684566273868361460640280523251242059850044328669692159"
1637 "629900374576027104298177006629276014371540945261309319363704125592775129"
1638 "543526908667388673739382491147471395192495459318806593271282662311169392"
1639 "196897003517840025298267505925987901751541005546610016067658227181318892"
1640 "914686508281007582655667597441346214499847364272258631922040641860333431"
1641 "409838623713258383681350233064164940590695888300919626215847587544298023"
1642 "636416943680102708406086295669759876682046839368574433996997648445207805"
1643 "615784339667691231286807666753972942872019850432610318031627872612657513"
1644 "588188267160616660825719678199868371370527508463011236193719286066916786"
1645 "169956541349011494927225747024994619057884118692213564790598702879596058"
1646 "672338334720925179141906809470606964896245458600635183723159228561689808"
1647 "246141482736625197373238197777325580142168245885279594913851700941789475"
1648 "252421784152262567254611571822468808675893407728003047921107885664474662"
1649 "930921581384003950729114103689170603748380178682003976896397305836815761"
1650 "717676338115866650889936516794601457549097578905329423919798362140648664"
1651 "569177147076571576101649257502509463877402424847669830852345415301684820"
1652 "395813946416649808062227494112874521812750160935760825922220707178083076"
1653 "380203450993589198835885505461509442443773367592842795410339065860781804"
1654 "024975272228687688301824830333940416256885455008512598774611538878683158"
1655 "183931461086893832255176926531299425504132104728730288984598001187854507"
1656 "900417184206801359847651992484444933900133130832052346600926424167009902"
1657 "829803553087005800387704758687923428053612864451456596148162238935900033"
1658 "917094683141205188616000211702577553792389670853917118547527592495253773"
1659 "028135298405566315903922235989614934474805789300370437580494193066066314"
1660 "056627605207631392651010580925826419831250810981343093764403877594495896"
1661 "516881097415880926429607388979497471571321217205535961262051641426436441"
1662 "668989765107456413733909427384182109285933511623871034309722437967253289"
1663 "084018145083721513211807496392673952789642893241520398827805325610653506"
1664 "029060153153064455898648607959013571280930834475689835845791849456112104"
1665 "462337569019001580859906425911782967213265389744605395555069797947978230"
1666 "708108432086217134763779632408473684293543722127232658767439906910370146"
1667 "716836295909075482355827087389127370874842532825987593970846704144140471"
1668 "956027276735614286138656432085771988513977140957180090146798065497158947"
1669 "229765733489703157617307078835099906185890777007500964162371428641176460"
1670 "739074789794941408428328217107759915202650066155868439585510978709442590"
1671 "231934194956788626761834746430104077432547436359522462253411168467463134"
1675 LIBC_NAMESPACE::sprintf(big_buff
, "%Lf", 0xf.fffffffffffffffp
+16380L);
1678 "118973149535723176502126385303097020516906332229462420044032373389173700"
1679 "552297072261641029033652888285354569780749557731442744315367028843419812"
1680 "557385374367867359320070697326320191591828296152436552951064679108661431"
1681 "179063216977883889613478656060039914875343321145491116008867984515486651"
1682 "285234014977303760000912547939396622315138362241783854274391783813871780"
1683 "588948754057516822634765923557697480511372564902088485522249479139937758"
1684 "502601177354918009979622602685950855888360815984690023564513234659447638"
1685 "493985927645628457966177293040780660922910271504608538808795932778162298"
1686 "682754783076808004015069494230341172895777710033571401055977524212405734"
1687 "700738625166011082837911962300846927720096515350020847447079244384854591"
1688 "288672300061908512647211195136146752763351956292759795725027800298079590"
1689 "419313960302147099703527646744553092202267965628099149823208332964124103"
1690 "850923918473478612192169721054348428704835340811304257300221642134891734"
1691 "717423480071488075100206439051723424765600472176809648610799494341570347"
1692 "632064355862420744350442438056613601760883747816538902780957697597728686"
1693 "007148702828795556714140463261583262360276289631617397848425448686060994"
1694 "827086796804807870251185893083854658422304090880599629459458620190376604"
1695 "844679092600222541053077590106576067134720012584640695703025713896098375"
1696 "799892695455305236856075868317922311363951946885088077187210470520395758"
1697 "748001314313144425494391994017575316933939236688185618912993172910425292"
1698 "123683515992232205099800167710278403536014082929639811512287776813570604"
1699 "578934353545169653956125404884644716978689321167108722908808277835051822"
1700 "885764606221873970285165508372099234948333443522898475123275372663606621"
1701 "390228126470623407535207172405866507951821730346378263135339370677490195"
1702 "019784169044182473806316282858685774143258116536404021840272491339332094"
1703 "921949842244273042701987304453662035026238695780468200360144729199712309"
1704 "553005720614186697485284685618651483271597448120312194675168637934309618"
1705 "961510733006555242148519520176285859509105183947250286387163249416761380"
1706 "499631979144187025430270675849519200883791516940158174004671147787720145"
1707 "964446117520405945350476472180797576111172084627363927960033967047003761"
1708 "337450955318415007379641260504792325166135484129188421134082301547330475"
1709 "406707281876350361733290800595189632520707167390454777712968226520622565"
1710 "143991937680440029238090311243791261477625596469422198137514696707944687"
1711 "035800439250765945161837981185939204954403611491531078225107269148697980"
1712 "924094677214272701240437718740921675661363493890045123235166814608932240"
1713 "069799317601780533819184998193300841098599393876029260139091141452600372"
1714 "028487213241195542428210183120421610446740462163533690058366460659115629"
1715 "876474552506814500393294140413149540067760295100596225302282300363147382"
1716 "468105964844244132486457313743759509641616804802412935187620466813563687"
1717 "753281467553879887177183651289394719533506188500326760735438867336800207"
1718 "438784965701457609034985757124304510203873049485425670247933932280911052"
1719 "604153852899484920399109194612991249163328991799809438033787952209313146"
1720 "694614970593966415237594928589096048991612194498998638483702248667224914"
1721 "892467841020618336462741696957630763248023558797524525373703543388296086"
1722 "275342774001633343405508353704850737454481975472222897528108302089868263"
1723 "302028525992308416805453968791141829762998896457648276528750456285492426"
1724 "516521775079951625966922911497778896235667095662713848201819134832168799"
1725 "586365263762097828507009933729439678463987902491451422274252700636394232"
1726 "799848397673998715441855420156224415492665301451550468548925862027608576"
1727 "183712976335876121538256512963353814166394951655600026415918655485005705"
1728 "261143195291991880795452239464962763563017858089669222640623538289853586"
1729 "759599064700838568712381032959192649484625076899225841930548076362021508"
1730 "902214922052806984201835084058693849381549890944546197789302911357651677"
1731 "540623227829831403347327660395223160342282471752818181884430488092132193"
1732 "355086987339586127607367086665237555567580317149010847732009642431878007"
1733 "000879734603290627894355374356444885190719161645514115576193939969076741"
1734 "515640282654366402676009508752394550734155613586793306603174472092444651"
1735 "353236664764973540085196704077110364053815007348689179836404957060618953"
1736 "500508984091382686953509006678332447257871219660441528492484004185093281"
1737 "190896363417573989716659600075948780061916409485433875852065711654107226"
1738 "099628815012314437794400874930194474433078438899570184271000480830501217"
1739 "712356062289507626904285680004771889315808935851559386317665294808903126"
1740 "774702966254511086154895839508779675546413794489596052797520987481383976"
1741 "257859210575628440175934932416214833956535018919681138909184379573470326"
1742 "940634289008780584694035245347939808067427323629788710086717580253156130"
1743 "235606487870925986528841635097252953709111431720488774740553905400942537"
1744 "542411931794417513706468964386151771884986701034153254238591108962471088"
1745 "538580868883777725864856414593426212108664758848926003176234596076950884"
1746 "9149662444156604419552086811989770240.000000");
1749 written
= LIBC_NAMESPACE::sprintf(big_buff
, "%.10Lf", 1e-10L);
1750 ASSERT_STREQ_LEN(written
, big_buff
, "0.0000000001");
1752 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
1753 written
= LIBC_NAMESPACE::sprintf(big_buff
, "%.7500Lf", 1e-4900L);
1757 "000000000000000000000000000000000000000000000000000000000000000000000000"
1758 "000000000000000000000000000000000000000000000000000000000000000000000000"
1759 "000000000000000000000000000000000000000000000000000000000000000000000000"
1760 "000000000000000000000000000000000000000000000000000000000000000000000000"
1761 "000000000000000000000000000000000000000000000000000000000000000000000000"
1762 "000000000000000000000000000000000000000000000000000000000000000000000000"
1763 "000000000000000000000000000000000000000000000000000000000000000000000000"
1764 "000000000000000000000000000000000000000000000000000000000000000000000000"
1765 "000000000000000000000000000000000000000000000000000000000000000000000000"
1766 "000000000000000000000000000000000000000000000000000000000000000000000000"
1767 "000000000000000000000000000000000000000000000000000000000000000000000000"
1768 "000000000000000000000000000000000000000000000000000000000000000000000000"
1769 "000000000000000000000000000000000000000000000000000000000000000000000000"
1770 "000000000000000000000000000000000000000000000000000000000000000000000000"
1771 "000000000000000000000000000000000000000000000000000000000000000000000000"
1772 "000000000000000000000000000000000000000000000000000000000000000000000000"
1773 "000000000000000000000000000000000000000000000000000000000000000000000000"
1774 "000000000000000000000000000000000000000000000000000000000000000000000000"
1775 "000000000000000000000000000000000000000000000000000000000000000000000000"
1776 "000000000000000000000000000000000000000000000000000000000000000000000000"
1777 "000000000000000000000000000000000000000000000000000000000000000000000000"
1778 "000000000000000000000000000000000000000000000000000000000000000000000000"
1779 "000000000000000000000000000000000000000000000000000000000000000000000000"
1780 "000000000000000000000000000000000000000000000000000000000000000000000000"
1781 "000000000000000000000000000000000000000000000000000000000000000000000000"
1782 "000000000000000000000000000000000000000000000000000000000000000000000000"
1783 "000000000000000000000000000000000000000000000000000000000000000000000000"
1784 "000000000000000000000000000000000000000000000000000000000000000000000000"
1785 "000000000000000000000000000000000000000000000000000000000000000000000000"
1786 "000000000000000000000000000000000000000000000000000000000000000000000000"
1787 "000000000000000000000000000000000000000000000000000000000000000000000000"
1788 "000000000000000000000000000000000000000000000000000000000000000000000000"
1789 "000000000000000000000000000000000000000000000000000000000000000000000000"
1790 "000000000000000000000000000000000000000000000000000000000000000000000000"
1791 "000000000000000000000000000000000000000000000000000000000000000000000000"
1792 "000000000000000000000000000000000000000000000000000000000000000000000000"
1793 "000000000000000000000000000000000000000000000000000000000000000000000000"
1794 "000000000000000000000000000000000000000000000000000000000000000000000000"
1795 "000000000000000000000000000000000000000000000000000000000000000000000000"
1796 "000000000000000000000000000000000000000000000000000000000000000000000000"
1797 "000000000000000000000000000000000000000000000000000000000000000000000000"
1798 "000000000000000000000000000000000000000000000000000000000000000000000000"
1799 "000000000000000000000000000000000000000000000000000000000000000000000000"
1800 "000000000000000000000000000000000000000000000000000000000000000000000000"
1801 "000000000000000000000000000000000000000000000000000000000000000000000000"
1802 "000000000000000000000000000000000000000000000000000000000000000000000000"
1803 "000000000000000000000000000000000000000000000000000000000000000000000000"
1804 "000000000000000000000000000000000000000000000000000000000000000000000000"
1805 "000000000000000000000000000000000000000000000000000000000000000000000000"
1806 "000000000000000000000000000000000000000000000000000000000000000000000000"
1807 "000000000000000000000000000000000000000000000000000000000000000000000000"
1808 "000000000000000000000000000000000000000000000000000000000000000000000000"
1809 "000000000000000000000000000000000000000000000000000000000000000000000000"
1810 "000000000000000000000000000000000000000000000000000000000000000000000000"
1811 "000000000000000000000000000000000000000000000000000000000000000000000000"
1812 "000000000000000000000000000000000000000000000000000000000000000000000000"
1813 "000000000000000000000000000000000000000000000000000000000000000000000000"
1814 "000000000000000000000000000000000000000000000000000000000000000000000000"
1815 "000000000000000000000000000000000000000000000000000000000000000000000000"
1816 "000000000000000000000000000000000000000000000000000000000000000000000000"
1817 "000000000000000000000000000000000000000000000000000000000000000000000000"
1818 "000000000000000000000000000000000000000000000000000000000000000000000000"
1819 "000000000000000000000000000000000000000000000000000000000000000000000000"
1820 "000000000000000000000000000000000000000000000000000000000000000000000000"
1821 "000000000000000000000000000000000000000000000000000000000000000000000000"
1822 "000000000000000000000000000000000000000000000000000000000000000000000000"
1823 "000000000000000000000000000000000000000000000000000000000000000000000000"
1824 "000000000000000000000000000000000000000000000000000000000000000000000000"
1825 "000099999999999999999996962764452956071352139203248614920751610856665084"
1826 "549214352477698417183862158583009348897567779527408501588132175167211539"
1827 "462139941448204886585901454195352527238724272760638086779284030512649793"
1828 "039219351187928723378036480041948464946018272171365770411701020666925613"
1829 "422460317465324758217878522666789603627480490870456508256359180089236338"
1830 "765625231186929290294207420828927406735690318849109129700396907705735097"
1831 "663944722727287361650042373203763784830198232253311807069225650324196304"
1832 "532045014970637489181357566354288111205943347410488298480279857453705249"
1833 "232862728556860184412369114663536200895729846877559808001004454634804626"
1834 "541455540260282018142615835686583304903486353937549394736905011798466731"
1835 "536563240053860118551127061960208467764243724656897127545613968909523389"
1836 "577188368809623987105800147797280462974804046545425080530020901531407223"
1837 "191237123282274818236437397994019915368657474589800678444589412286037789"
1838 "891525464936023205313685584525510094270344601331453730179416773626565262"
1839 "480345858564672442896904520146956686863172737711483866766404977719744767"
1840 "834324844875237277613991088218774564658513875732403456058414595576806383"
1841 "115554713240005982141397577420073082470139244845624915873825746771661332"
1842 "098677966580506186966978746832443976821987300902957597498388211921362869"
1843 "017846215557612829071692275292036211064515305528052919611691470945774714"
1844 "135516559501572279732350629089770249554808690411603894492333360300589658"
1845 "470898965370892774715815089075170720164713889237058574941489766701880158"
1846 "060081295483989540170337129032188818293132770882381428397119039835946745"
1847 "549356649433406617266370644136291924838857814675939156677910783740103207"
1848 "523299367093130816446415259371931925208362367989095199399211644084543790"
1849 "110432339056231037520216864358899218874658268610955002763260912337688947"
1850 "822453100821038299301092582962825965939081817836419126254832772002214908"
1851 "085575905761843610944187009818156363893015929300295112598059949496854566"
1852 "638748010633726861510500653821408135845840123073754133549077708843800674"
1853 "328440913743105608636458354618912183716456158809545183074062249922212944"
1854 "249667793845728355381309084891765979111348980470647082269921872595470473"
1855 "719354467594516320911964549508538492057120740224559944452120552719041944"
1856 "961475548547884309626382512432626380881023756568143060204097921571153170"
1857 "723817845809196253498326358439807445210362177680590181657555380795450462"
1858 "223805222580359379367452693270553602179122419370586308101820559214330382"
1859 "570449525088342437216896462077260223998756027453411520977536701491759878"
1860 "422771447006016890777855573925295187921971811871399320142563330377888532"
1863 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
1866 TEST(LlvmLibcSPrintfTest
, FloatExponentConv
) {
1870 ForceRoundingMode
r(RoundingMode::Nearest
);
1871 double inf
= LIBC_NAMESPACE::fputil::FPBits
<double>::inf().get_val();
1872 double nan
= LIBC_NAMESPACE::fputil::FPBits
<double>::quiet_nan().get_val();
1874 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 1.0);
1875 ASSERT_STREQ_LEN(written
, buff
, "1.000000e+00");
1877 written
= LIBC_NAMESPACE::sprintf(buff
, "%E", -1.0);
1878 ASSERT_STREQ_LEN(written
, buff
, "-1.000000E+00");
1880 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", -1.234567);
1881 ASSERT_STREQ_LEN(written
, buff
, "-1.234567e+00");
1883 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0.0);
1884 ASSERT_STREQ_LEN(written
, buff
, "0.000000e+00");
1886 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 1.5);
1887 ASSERT_STREQ_LEN(written
, buff
, "1.500000e+00");
1889 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 1e300
);
1890 ASSERT_STREQ_LEN(written
, buff
, "1.000000e+300");
1892 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0.1);
1893 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-01");
1895 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0.001);
1896 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-03");
1898 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0.00001);
1899 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-05");
1901 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0.0000001);
1902 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-07");
1904 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0.000000001);
1905 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-09");
1907 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 1.0e-20);
1908 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-20");
1910 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 1234567890123456789.0);
1911 ASSERT_STREQ_LEN(written
, buff
, "1.234568e+18");
1913 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 9999999000000.00);
1914 ASSERT_STREQ_LEN(written
, buff
, "9.999999e+12");
1916 // Simple Subnormal Tests.
1918 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0x1.0p
-1027);
1919 ASSERT_STREQ_LEN(written
, buff
, "6.953356e-310");
1921 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", 0x1.0p
-1074);
1922 ASSERT_STREQ_LEN(written
, buff
, "4.940656e-324");
1926 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", inf
);
1927 ASSERT_STREQ_LEN(written
, buff
, "inf");
1929 written
= LIBC_NAMESPACE::sprintf(buff
, "%E", -inf
);
1930 ASSERT_STREQ_LEN(written
, buff
, "-INF");
1932 written
= LIBC_NAMESPACE::sprintf(buff
, "%e", nan
);
1933 ASSERT_STREQ_LEN(written
, buff
, "nan");
1935 written
= LIBC_NAMESPACE::sprintf(buff
, "%E", -nan
);
1936 ASSERT_STREQ_LEN(written
, buff
, "-NAN");
1940 written
= LIBC_NAMESPACE::sprintf(buff
, "%15e", 1.0);
1941 ASSERT_STREQ_LEN(written
, buff
, " 1.000000e+00");
1943 written
= LIBC_NAMESPACE::sprintf(buff
, "%15e", -1.0);
1944 ASSERT_STREQ_LEN(written
, buff
, " -1.000000e+00");
1946 written
= LIBC_NAMESPACE::sprintf(buff
, "%15e", 1.0e5
);
1947 ASSERT_STREQ_LEN(written
, buff
, " 1.000000e+05");
1949 written
= LIBC_NAMESPACE::sprintf(buff
, "%15e", -1.0e5
);
1950 ASSERT_STREQ_LEN(written
, buff
, " -1.000000e+05");
1952 written
= LIBC_NAMESPACE::sprintf(buff
, "%10e", 1.0e-5);
1953 ASSERT_STREQ_LEN(written
, buff
, "1.000000e-05");
1957 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.0);
1958 ASSERT_STREQ_LEN(written
, buff
, "1.0e+00");
1960 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 0.0);
1961 ASSERT_STREQ_LEN(written
, buff
, "0.0e+00");
1963 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 0.0);
1964 ASSERT_STREQ_LEN(written
, buff
, "0e+00");
1966 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 0.1);
1967 ASSERT_STREQ_LEN(written
, buff
, "1.0e-01");
1969 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.09);
1970 ASSERT_STREQ_LEN(written
, buff
, "1.1e+00");
1972 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.04);
1973 ASSERT_STREQ_LEN(written
, buff
, "1.0e+00");
1975 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.19);
1976 ASSERT_STREQ_LEN(written
, buff
, "1.2e+00");
1978 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.99);
1979 ASSERT_STREQ_LEN(written
, buff
, "2.0e+00");
1981 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 9.99);
1982 ASSERT_STREQ_LEN(written
, buff
, "1.0e+01");
1984 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2e", 99.9);
1985 ASSERT_STREQ_LEN(written
, buff
, "9.99e+01");
1987 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 99.9);
1988 ASSERT_STREQ_LEN(written
, buff
, "1.0e+02");
1990 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5e", 1.25);
1991 ASSERT_STREQ_LEN(written
, buff
, "1.25000e+00");
1993 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 1.25);
1994 ASSERT_STREQ_LEN(written
, buff
, "1e+00");
1996 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 1.75);
1997 ASSERT_STREQ_LEN(written
, buff
, "2e+00");
1999 written
= LIBC_NAMESPACE::sprintf(buff
, "%.20e", 1.234e-10);
2000 ASSERT_STREQ_LEN(written
, buff
, "1.23400000000000008140e-10");
2002 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2e", -9.99);
2003 ASSERT_STREQ_LEN(written
, buff
, "-9.99e+00");
2005 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -9.99);
2006 ASSERT_STREQ_LEN(written
, buff
, "-1.0e+01");
2008 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5e", 0.0);
2009 ASSERT_STREQ_LEN(written
, buff
, "0.00000e+00");
2011 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5e", 1.008);
2012 ASSERT_STREQ_LEN(written
, buff
, "1.00800e+00");
2014 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5e", 1.008e3
);
2015 ASSERT_STREQ_LEN(written
, buff
, "1.00800e+03");
2017 // These tests also focus on rounding. Almost all of them have a 5 right after
2018 // the printed string (e.g. 9.5 with precision 0 prints 0 digits after the
2019 // decimal point). This is again because rounding a number with a 5 after the
2020 // printed section means that more digits have to be checked to determine if
2021 // this should be rounded up (if there are non-zero digits after the 5) or to
2022 // even (if the 5 is the last non-zero digit). Additionally, the algorithm for
2023 // checking if a number is all 0s after the decimal point may not work since
2024 // the decimal point moves in this representation.
2025 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 2.5812229360061737E+200);
2026 ASSERT_STREQ_LEN(written
, buff
, "3e+200");
2028 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 9.059E+200);
2029 ASSERT_STREQ_LEN(written
, buff
, "9.1e+200");
2031 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 9.059E+200);
2032 ASSERT_STREQ_LEN(written
, buff
, "9e+200");
2034 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
2035 written
= LIBC_NAMESPACE::sprintf(buff
, "%.166e", 1.131959884853339E-72);
2036 ASSERT_STREQ_LEN(written
, buff
,
2038 "13195988485333904593863991136097397258531639976739227369782"
2039 "68612419376648241056393424414314951197624317440549121097287"
2040 "069853416091591569170304865110665559768676757812e-72");
2043 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 9.5);
2044 ASSERT_STREQ_LEN(written
, buff
, "1e+01");
2046 written
= LIBC_NAMESPACE::sprintf(buff
, "%.10e", 1.9999999999890936);
2047 ASSERT_STREQ_LEN(written
, buff
, "2.0000000000e+00");
2049 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 745362143563.03894);
2050 ASSERT_STREQ_LEN(written
, buff
, "7.5e+11");
2052 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 45181042688.0);
2053 ASSERT_STREQ_LEN(written
, buff
, "5e+10");
2055 written
= LIBC_NAMESPACE::sprintf(buff
, "%.35e", 1.3752441369139243);
2056 ASSERT_STREQ_LEN(written
, buff
, "1.37524413691392433101157166674965993e+00");
2058 // Subnormal Precision Tests
2060 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
2061 written
= LIBC_NAMESPACE::sprintf(buff
, "%.310e", 0x1.0p
-1022);
2065 "225073858507201383090232717332404064219215980462331830553327416887204434"
2066 "813918195854283159012511020564067339731035811005152434161553460108856012"
2067 "385377718821130777993532002330479610147442583636071921565046942503734208"
2068 "375250806650616658158948720491179968591639648500635908770118304874799780"
2069 "8877537499494515804516e-308");
2072 written
= LIBC_NAMESPACE::sprintf(buff
, "%.30e", 0x1.0p
-1022);
2073 ASSERT_STREQ_LEN(written
, buff
, "2.225073858507201383090232717332e-308");
2075 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
2076 written
= LIBC_NAMESPACE::sprintf(buff
, "%.310e", 0x1.0p
-1023);
2080 "112536929253600691545116358666202032109607990231165915276663708443602217"
2081 "406959097927141579506255510282033669865517905502576217080776730054428006"
2082 "192688859410565388996766001165239805073721291818035960782523471251867104"
2083 "187625403325308329079474360245589984295819824250317954385059152437399890"
2084 "4438768749747257902258e-308");
2087 written
= LIBC_NAMESPACE::sprintf(buff
, "%.6e", 9.99999e-310);
2088 ASSERT_STREQ_LEN(written
, buff
, "9.999990e-310");
2090 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5e", 9.99999e-310);
2091 ASSERT_STREQ_LEN(written
, buff
, "9.99999e-310");
2093 written
= LIBC_NAMESPACE::sprintf(buff
, "%.4e", 9.99999e-310);
2094 ASSERT_STREQ_LEN(written
, buff
, "1.0000e-309");
2096 written
= LIBC_NAMESPACE::sprintf(buff
, "%.3e", 9.99999e-310);
2097 ASSERT_STREQ_LEN(written
, buff
, "1.000e-309");
2099 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2e", 9.99999e-310);
2100 ASSERT_STREQ_LEN(written
, buff
, "1.00e-309");
2102 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 9.99999e-310);
2103 ASSERT_STREQ_LEN(written
, buff
, "1.0e-309");
2105 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0e", 9.99999e-310);
2106 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2108 written
= LIBC_NAMESPACE::sprintf(buff
, "%.10e", 0x1.0p
-1074);
2109 ASSERT_STREQ_LEN(written
, buff
, "4.9406564584e-324");
2111 // Rounding Mode Tests.
2113 if (ForceRoundingMode
r(RoundingMode::Nearest
); r
.success
) {
2114 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.75);
2115 ASSERT_STREQ_LEN(written
, buff
, "1.8e+00");
2117 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.25);
2118 ASSERT_STREQ_LEN(written
, buff
, "1.2e+00");
2120 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.125);
2121 ASSERT_STREQ_LEN(written
, buff
, "1.1e+00");
2123 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.625);
2124 ASSERT_STREQ_LEN(written
, buff
, "1.6e+00");
2126 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.375);
2127 ASSERT_STREQ_LEN(written
, buff
, "1.4e+00");
2129 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.875);
2130 ASSERT_STREQ_LEN(written
, buff
, "1.9e+00");
2132 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.75);
2133 ASSERT_STREQ_LEN(written
, buff
, "-1.8e+00");
2135 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.25);
2136 ASSERT_STREQ_LEN(written
, buff
, "-1.2e+00");
2138 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.125);
2139 ASSERT_STREQ_LEN(written
, buff
, "-1.1e+00");
2141 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.625);
2142 ASSERT_STREQ_LEN(written
, buff
, "-1.6e+00");
2144 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.375);
2145 ASSERT_STREQ_LEN(written
, buff
, "-1.4e+00");
2147 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.875);
2148 ASSERT_STREQ_LEN(written
, buff
, "-1.9e+00");
2151 if (ForceRoundingMode
r(RoundingMode::Upward
); r
.success
) {
2152 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.75);
2153 ASSERT_STREQ_LEN(written
, buff
, "1.8e+00");
2155 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.25);
2156 ASSERT_STREQ_LEN(written
, buff
, "1.3e+00");
2158 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.125);
2159 ASSERT_STREQ_LEN(written
, buff
, "1.2e+00");
2161 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.625);
2162 ASSERT_STREQ_LEN(written
, buff
, "1.7e+00");
2164 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.375);
2165 ASSERT_STREQ_LEN(written
, buff
, "1.4e+00");
2167 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.875);
2168 ASSERT_STREQ_LEN(written
, buff
, "1.9e+00");
2170 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.75);
2171 ASSERT_STREQ_LEN(written
, buff
, "-1.7e+00");
2173 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.25);
2174 ASSERT_STREQ_LEN(written
, buff
, "-1.2e+00");
2176 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.125);
2177 ASSERT_STREQ_LEN(written
, buff
, "-1.1e+00");
2179 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.625);
2180 ASSERT_STREQ_LEN(written
, buff
, "-1.6e+00");
2182 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.375);
2183 ASSERT_STREQ_LEN(written
, buff
, "-1.3e+00");
2185 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.875);
2186 ASSERT_STREQ_LEN(written
, buff
, "-1.8e+00");
2189 if (ForceRoundingMode
r(RoundingMode::Downward
); r
.success
) {
2190 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.75);
2191 ASSERT_STREQ_LEN(written
, buff
, "1.7e+00");
2193 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.25);
2194 ASSERT_STREQ_LEN(written
, buff
, "1.2e+00");
2196 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.125);
2197 ASSERT_STREQ_LEN(written
, buff
, "1.1e+00");
2199 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.625);
2200 ASSERT_STREQ_LEN(written
, buff
, "1.6e+00");
2202 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.375);
2203 ASSERT_STREQ_LEN(written
, buff
, "1.3e+00");
2205 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.875);
2206 ASSERT_STREQ_LEN(written
, buff
, "1.8e+00");
2208 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.75);
2209 ASSERT_STREQ_LEN(written
, buff
, "-1.8e+00");
2211 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.25);
2212 ASSERT_STREQ_LEN(written
, buff
, "-1.3e+00");
2214 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.125);
2215 ASSERT_STREQ_LEN(written
, buff
, "-1.2e+00");
2217 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.625);
2218 ASSERT_STREQ_LEN(written
, buff
, "-1.7e+00");
2220 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.375);
2221 ASSERT_STREQ_LEN(written
, buff
, "-1.4e+00");
2223 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.875);
2224 ASSERT_STREQ_LEN(written
, buff
, "-1.9e+00");
2227 if (ForceRoundingMode
r(RoundingMode::TowardZero
); r
.success
) {
2228 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.75);
2229 ASSERT_STREQ_LEN(written
, buff
, "1.7e+00");
2231 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.25);
2232 ASSERT_STREQ_LEN(written
, buff
, "1.2e+00");
2234 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.125);
2235 ASSERT_STREQ_LEN(written
, buff
, "1.1e+00");
2237 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.625);
2238 ASSERT_STREQ_LEN(written
, buff
, "1.6e+00");
2240 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.375);
2241 ASSERT_STREQ_LEN(written
, buff
, "1.3e+00");
2243 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", 1.875);
2244 ASSERT_STREQ_LEN(written
, buff
, "1.8e+00");
2246 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.75);
2247 ASSERT_STREQ_LEN(written
, buff
, "-1.7e+00");
2249 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.25);
2250 ASSERT_STREQ_LEN(written
, buff
, "-1.2e+00");
2252 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.125);
2253 ASSERT_STREQ_LEN(written
, buff
, "-1.1e+00");
2255 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.625);
2256 ASSERT_STREQ_LEN(written
, buff
, "-1.6e+00");
2258 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.375);
2259 ASSERT_STREQ_LEN(written
, buff
, "-1.3e+00");
2261 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1e", -1.875);
2262 ASSERT_STREQ_LEN(written
, buff
, "-1.8e+00");
2266 written
= LIBC_NAMESPACE::sprintf(buff
, "%+e", 1.0);
2267 ASSERT_STREQ_LEN(written
, buff
, "+1.000000e+00");
2269 written
= LIBC_NAMESPACE::sprintf(buff
, "%+e", -1.0);
2270 ASSERT_STREQ_LEN(written
, buff
, "-1.000000e+00");
2272 written
= LIBC_NAMESPACE::sprintf(buff
, "% e", 1.0);
2273 ASSERT_STREQ_LEN(written
, buff
, " 1.000000e+00");
2275 written
= LIBC_NAMESPACE::sprintf(buff
, "% e", -1.0);
2276 ASSERT_STREQ_LEN(written
, buff
, "-1.000000e+00");
2278 written
= LIBC_NAMESPACE::sprintf(buff
, "%-15e", 1.5);
2279 ASSERT_STREQ_LEN(written
, buff
, "1.500000e+00 ");
2281 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.e", 1.0);
2282 ASSERT_STREQ_LEN(written
, buff
, "1.e+00");
2284 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.0e", 1.5);
2285 ASSERT_STREQ_LEN(written
, buff
, "2.e+00");
2287 written
= LIBC_NAMESPACE::sprintf(buff
, "%015e", 1.5);
2288 ASSERT_STREQ_LEN(written
, buff
, "0001.500000e+00");
2290 written
= LIBC_NAMESPACE::sprintf(buff
, "%015e", -1.5);
2291 ASSERT_STREQ_LEN(written
, buff
, "-001.500000e+00");
2293 written
= LIBC_NAMESPACE::sprintf(buff
, "%+- #0e", 0.0);
2294 ASSERT_STREQ_LEN(written
, buff
, "+0.000000e+00");
2298 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.2e", 9.99);
2299 ASSERT_STREQ_LEN(written
, buff
, " 9.99e+00");
2301 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.1e", 9.99);
2302 ASSERT_STREQ_LEN(written
, buff
, " 1.0e+01");
2304 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.0e", 9.99);
2305 ASSERT_STREQ_LEN(written
, buff
, " 1e+01");
2307 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.0e", 0.0999);
2308 ASSERT_STREQ_LEN(written
, buff
, " 1e-01");
2310 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.2e", 9.99);
2311 ASSERT_STREQ_LEN(written
, buff
, "9.99e+00 ");
2313 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.1e", 9.99);
2314 ASSERT_STREQ_LEN(written
, buff
, "1.0e+01 ");
2316 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.1e", 1.0e-50);
2317 ASSERT_STREQ_LEN(written
, buff
, "1.0e-50 ");
2319 written
= LIBC_NAMESPACE::sprintf(buff
, "%30e", 1234567890123456789.0);
2320 ASSERT_STREQ_LEN(written
, buff
, " 1.234568e+18");
2322 written
= LIBC_NAMESPACE::sprintf(buff
, "%-30e", 1234567890123456789.0);
2323 ASSERT_STREQ_LEN(written
, buff
, "1.234568e+18 ");
2325 written
= LIBC_NAMESPACE::sprintf(buff
, "%25.14e", 9999999999999.99);
2326 ASSERT_STREQ_LEN(written
, buff
, " 9.99999999999999e+12");
2328 written
= LIBC_NAMESPACE::sprintf(buff
, "%25.13e", 9999999999999.99);
2329 ASSERT_STREQ_LEN(written
, buff
, " 1.0000000000000e+13");
2331 written
= LIBC_NAMESPACE::sprintf(buff
, "%25.12e", 9999999999999.99);
2332 ASSERT_STREQ_LEN(written
, buff
, " 1.000000000000e+13");
2334 written
= LIBC_NAMESPACE::sprintf(buff
, "%12.3e %-12.3e", 0.1, 256.0);
2335 ASSERT_STREQ_LEN(written
, buff
, " 1.000e-01 2.560e+02 ");
2337 written
= LIBC_NAMESPACE::sprintf(buff
, "%+-#12.3e % 012.3e", 0.1256, 1256.0);
2338 ASSERT_STREQ_LEN(written
, buff
, "+1.256e-01 001.256e+03");
2341 TEST(LlvmLibcSPrintfTest
, FloatExponentLongDoubleConv
) {
2345 ForceRoundingMode
r(RoundingMode::Nearest
);
2346 // Length Modifier Tests.
2348 written
= LIBC_NAMESPACE::sprintf(buff
, "%.9Le", 1000000000500000000.1L);
2349 ASSERT_STREQ_LEN(written
, buff
, "1.000000001e+18");
2351 written
= LIBC_NAMESPACE::sprintf(buff
, "%.9Le", 1000000000500000000.0L);
2352 ASSERT_STREQ_LEN(written
, buff
, "1.000000000e+18");
2354 written
= LIBC_NAMESPACE::sprintf(buff
, "%Le", 1e100L
);
2355 ASSERT_STREQ_LEN(written
, buff
, "1.000000e+100");
2357 written
= LIBC_NAMESPACE::sprintf(buff
, "%Le", 1.0L);
2358 ASSERT_STREQ_LEN(written
, buff
, "1.000000e+00");
2360 #if !defined(LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE)
2361 written
= LIBC_NAMESPACE::sprintf(buff
, "%Le", 0xf.fffffffffffffffp
+16380L);
2362 ASSERT_STREQ_LEN(written
, buff
, "1.189731e+4932");
2364 written
= LIBC_NAMESPACE::sprintf(buff
, "%Le", 1e1000L
);
2365 ASSERT_STREQ_LEN(written
, buff
, "1.000000e+1000");
2367 written
= LIBC_NAMESPACE::sprintf(buff
, "%Le", 1e4900L
);
2368 ASSERT_STREQ_LEN(written
, buff
, "1.000000e+4900");
2370 written
= LIBC_NAMESPACE::sprintf(buff
, "%Le", 1.2345678e4900L
);
2371 ASSERT_STREQ_LEN(written
, buff
, "1.234568e+4900");
2375 TEST(LlvmLibcSPrintfTest
, FloatAutoConv
) {
2379 ForceRoundingMode
r(RoundingMode::Nearest
);
2380 double inf
= LIBC_NAMESPACE::fputil::FPBits
<double>::inf().get_val();
2381 double nan
= LIBC_NAMESPACE::fputil::FPBits
<double>::quiet_nan().get_val();
2383 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 1.0);
2384 ASSERT_STREQ_LEN(written
, buff
, "1");
2386 written
= LIBC_NAMESPACE::sprintf(buff
, "%G", -1.0);
2387 ASSERT_STREQ_LEN(written
, buff
, "-1");
2389 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", -1.234567);
2390 ASSERT_STREQ_LEN(written
, buff
, "-1.23457");
2392 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0.0);
2393 ASSERT_STREQ_LEN(written
, buff
, "0");
2395 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", -0.0);
2396 ASSERT_STREQ_LEN(written
, buff
, "-0");
2398 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 1.5);
2399 ASSERT_STREQ_LEN(written
, buff
, "1.5");
2401 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 1e300
);
2402 ASSERT_STREQ_LEN(written
, buff
, "1e+300");
2404 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0.1);
2405 ASSERT_STREQ_LEN(written
, buff
, "0.1");
2407 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0.001);
2408 ASSERT_STREQ_LEN(written
, buff
, "0.001");
2410 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0.00001);
2411 ASSERT_STREQ_LEN(written
, buff
, "1e-05");
2413 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0.0000001);
2414 ASSERT_STREQ_LEN(written
, buff
, "1e-07");
2416 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0.000000001);
2417 ASSERT_STREQ_LEN(written
, buff
, "1e-09");
2419 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 1.0e-20);
2420 ASSERT_STREQ_LEN(written
, buff
, "1e-20");
2422 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 1234567890123456789.0);
2423 ASSERT_STREQ_LEN(written
, buff
, "1.23457e+18");
2425 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 9999990000000.00);
2426 ASSERT_STREQ_LEN(written
, buff
, "9.99999e+12");
2428 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 9999999000000.00);
2429 ASSERT_STREQ_LEN(written
, buff
, "1e+13");
2431 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0xa.aaaaaaaaaaaaaabp
-7);
2432 ASSERT_STREQ_LEN(written
, buff
, "0.0833333");
2434 // Simple Subnormal Tests.
2436 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0x1.0p
-1027);
2437 ASSERT_STREQ_LEN(written
, buff
, "6.95336e-310");
2439 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", 0x1.0p
-1074);
2440 ASSERT_STREQ_LEN(written
, buff
, "4.94066e-324");
2444 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", inf
);
2445 ASSERT_STREQ_LEN(written
, buff
, "inf");
2447 written
= LIBC_NAMESPACE::sprintf(buff
, "%G", -inf
);
2448 ASSERT_STREQ_LEN(written
, buff
, "-INF");
2450 written
= LIBC_NAMESPACE::sprintf(buff
, "%g", nan
);
2451 ASSERT_STREQ_LEN(written
, buff
, "nan");
2453 written
= LIBC_NAMESPACE::sprintf(buff
, "%G", -nan
);
2454 ASSERT_STREQ_LEN(written
, buff
, "-NAN");
2458 written
= LIBC_NAMESPACE::sprintf(buff
, "%15g", 1.0);
2459 ASSERT_STREQ_LEN(written
, buff
, " 1");
2461 written
= LIBC_NAMESPACE::sprintf(buff
, "%15g", -1.0);
2462 ASSERT_STREQ_LEN(written
, buff
, " -1");
2464 written
= LIBC_NAMESPACE::sprintf(buff
, "%15g", 1.0e5
);
2465 ASSERT_STREQ_LEN(written
, buff
, " 100000");
2467 written
= LIBC_NAMESPACE::sprintf(buff
, "%15g", -1.0e5
);
2468 ASSERT_STREQ_LEN(written
, buff
, " -100000");
2470 written
= LIBC_NAMESPACE::sprintf(buff
, "%10g", 1.0e-5);
2471 ASSERT_STREQ_LEN(written
, buff
, " 1e-05");
2475 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.23456789);
2476 ASSERT_STREQ_LEN(written
, buff
, "1.2");
2478 // Trimming trailing zeroes causes the precision to be ignored here.
2479 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1g", 0.0);
2480 ASSERT_STREQ_LEN(written
, buff
, "0");
2482 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0g", 0.0);
2483 ASSERT_STREQ_LEN(written
, buff
, "0");
2485 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 0.1);
2486 ASSERT_STREQ_LEN(written
, buff
, "0.1");
2488 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.09);
2489 ASSERT_STREQ_LEN(written
, buff
, "1.1");
2491 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.04);
2492 ASSERT_STREQ_LEN(written
, buff
, "1");
2494 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.19);
2495 ASSERT_STREQ_LEN(written
, buff
, "1.2");
2497 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.99);
2498 ASSERT_STREQ_LEN(written
, buff
, "2");
2500 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 9.99);
2501 ASSERT_STREQ_LEN(written
, buff
, "10");
2503 written
= LIBC_NAMESPACE::sprintf(buff
, "%.3g", 99.9);
2504 ASSERT_STREQ_LEN(written
, buff
, "99.9");
2506 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 99.9);
2507 ASSERT_STREQ_LEN(written
, buff
, "1e+02");
2509 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1g", 99.9);
2510 ASSERT_STREQ_LEN(written
, buff
, "1e+02");
2512 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5g", 1.25);
2513 ASSERT_STREQ_LEN(written
, buff
, "1.25");
2515 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0g", 1.25);
2516 ASSERT_STREQ_LEN(written
, buff
, "1");
2518 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0g", 1.75);
2519 ASSERT_STREQ_LEN(written
, buff
, "2");
2521 written
= LIBC_NAMESPACE::sprintf(buff
, "%.20g", 1.234e-10);
2522 ASSERT_STREQ_LEN(written
, buff
, "1.2340000000000000814e-10");
2524 written
= LIBC_NAMESPACE::sprintf(buff
, "%.3g", -9.99);
2525 ASSERT_STREQ_LEN(written
, buff
, "-9.99");
2527 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -9.99);
2528 ASSERT_STREQ_LEN(written
, buff
, "-10");
2530 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1g", -9.99);
2531 ASSERT_STREQ_LEN(written
, buff
, "-1e+01");
2533 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5g", 1.008);
2534 ASSERT_STREQ_LEN(written
, buff
, "1.008");
2536 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5g", 1.008e3
);
2537 ASSERT_STREQ_LEN(written
, buff
, "1008");
2539 written
= LIBC_NAMESPACE::sprintf(buff
, "%.4g", 9999.0);
2540 ASSERT_STREQ_LEN(written
, buff
, "9999");
2542 written
= LIBC_NAMESPACE::sprintf(buff
, "%.3g", 9999.0);
2543 ASSERT_STREQ_LEN(written
, buff
, "1e+04");
2545 written
= LIBC_NAMESPACE::sprintf(buff
, "%.3g", 1256.0);
2546 ASSERT_STREQ_LEN(written
, buff
, "1.26e+03");
2548 // Found through large scale testing.
2549 written
= LIBC_NAMESPACE::sprintf(buff
, "%.15g", 22.25);
2550 ASSERT_STREQ_LEN(written
, buff
, "22.25");
2552 // These tests also focus on rounding, but only in how it relates to the base
2553 // 10 exponent. The %g conversion selects between being a %f or %e conversion
2554 // based on what the exponent would be if it was %e. If we call the precision
2555 // P (equal to 6 if the precision is not set, 0 if the provided precision is
2556 // 0, and provided precision - 1 otherwise) and the exponent X, then the style
2557 // is %f with an effective precision of P - X + 1 if P > X >= -4, else the
2558 // style is %e with effective precision P - 1. Additionally, it attempts to
2559 // trim zeros that would be displayed after the decimal point.
2560 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1g", 9.059E+200);
2561 ASSERT_STREQ_LEN(written
, buff
, "9e+200");
2563 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 9.059E+200);
2564 ASSERT_STREQ_LEN(written
, buff
, "9.1e+200");
2566 // For this test, P = 0 and X = 1, so P > X >= -4 is false, giving a %e style.
2567 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0g", 9.5);
2568 ASSERT_STREQ_LEN(written
, buff
, "1e+01");
2570 // Subnormal Precision Tests
2571 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
2572 written
= LIBC_NAMESPACE::sprintf(buff
, "%.310g", 0x1.0p
-1022);
2576 "225073858507201383090232717332404064219215980462331830553327416887204434"
2577 "813918195854283159012511020564067339731035811005152434161553460108856012"
2578 "385377718821130777993532002330479610147442583636071921565046942503734208"
2579 "375250806650616658158948720491179968591639648500635908770118304874799780"
2580 "887753749949451580452e-308");
2583 written
= LIBC_NAMESPACE::sprintf(buff
, "%.30g", 0x1.0p
-1022);
2584 ASSERT_STREQ_LEN(written
, buff
, "2.22507385850720138309023271733e-308");
2586 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
2587 written
= LIBC_NAMESPACE::sprintf(buff
, "%.310g", 0x1.0p
-1023);
2591 "112536929253600691545116358666202032109607990231165915276663708443602217"
2592 "406959097927141579506255510282033669865517905502576217080776730054428006"
2593 "192688859410565388996766001165239805073721291818035960782523471251867104"
2594 "187625403325308329079474360245589984295819824250317954385059152437399890"
2595 "443876874974725790226e-308");
2598 written
= LIBC_NAMESPACE::sprintf(buff
, "%.7g", 9.99999e-310);
2599 ASSERT_STREQ_LEN(written
, buff
, "9.99999e-310");
2601 written
= LIBC_NAMESPACE::sprintf(buff
, "%.6g", 9.99999e-310);
2602 ASSERT_STREQ_LEN(written
, buff
, "9.99999e-310");
2604 written
= LIBC_NAMESPACE::sprintf(buff
, "%.5g", 9.99999e-310);
2605 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2607 written
= LIBC_NAMESPACE::sprintf(buff
, "%.4g", 9.99999e-310);
2608 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2610 written
= LIBC_NAMESPACE::sprintf(buff
, "%.3g", 9.99999e-310);
2611 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2613 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 9.99999e-310);
2614 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2616 written
= LIBC_NAMESPACE::sprintf(buff
, "%.1g", 9.99999e-310);
2617 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2619 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0g", 9.99999e-310);
2620 ASSERT_STREQ_LEN(written
, buff
, "1e-309");
2622 written
= LIBC_NAMESPACE::sprintf(buff
, "%.10g", 0x1.0p
-1074);
2623 ASSERT_STREQ_LEN(written
, buff
, "4.940656458e-324");
2625 // Rounding Mode Tests.
2627 if (ForceRoundingMode
r(RoundingMode::Nearest
); r
.success
) {
2628 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.75);
2629 ASSERT_STREQ_LEN(written
, buff
, "1.8");
2631 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.25);
2632 ASSERT_STREQ_LEN(written
, buff
, "1.2");
2634 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.125);
2635 ASSERT_STREQ_LEN(written
, buff
, "1.1");
2637 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.625);
2638 ASSERT_STREQ_LEN(written
, buff
, "1.6");
2640 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.375);
2641 ASSERT_STREQ_LEN(written
, buff
, "1.4");
2643 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.875);
2644 ASSERT_STREQ_LEN(written
, buff
, "1.9");
2646 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.75);
2647 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
2649 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.25);
2650 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
2652 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.125);
2653 ASSERT_STREQ_LEN(written
, buff
, "-1.1");
2655 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.625);
2656 ASSERT_STREQ_LEN(written
, buff
, "-1.6");
2658 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.375);
2659 ASSERT_STREQ_LEN(written
, buff
, "-1.4");
2661 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.875);
2662 ASSERT_STREQ_LEN(written
, buff
, "-1.9");
2665 if (ForceRoundingMode
r(RoundingMode::Upward
); r
.success
) {
2666 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.75);
2667 ASSERT_STREQ_LEN(written
, buff
, "1.8");
2669 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.25);
2670 ASSERT_STREQ_LEN(written
, buff
, "1.3");
2672 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.125);
2673 ASSERT_STREQ_LEN(written
, buff
, "1.2");
2675 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.625);
2676 ASSERT_STREQ_LEN(written
, buff
, "1.7");
2678 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.375);
2679 ASSERT_STREQ_LEN(written
, buff
, "1.4");
2681 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.875);
2682 ASSERT_STREQ_LEN(written
, buff
, "1.9");
2684 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.75);
2685 ASSERT_STREQ_LEN(written
, buff
, "-1.7");
2687 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.25);
2688 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
2690 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.125);
2691 ASSERT_STREQ_LEN(written
, buff
, "-1.1");
2693 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.625);
2694 ASSERT_STREQ_LEN(written
, buff
, "-1.6");
2696 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.375);
2697 ASSERT_STREQ_LEN(written
, buff
, "-1.3");
2699 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.875);
2700 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
2703 if (ForceRoundingMode
r(RoundingMode::Downward
); r
.success
) {
2704 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.75);
2705 ASSERT_STREQ_LEN(written
, buff
, "1.7");
2707 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.25);
2708 ASSERT_STREQ_LEN(written
, buff
, "1.2");
2710 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.125);
2711 ASSERT_STREQ_LEN(written
, buff
, "1.1");
2713 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.625);
2714 ASSERT_STREQ_LEN(written
, buff
, "1.6");
2716 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.375);
2717 ASSERT_STREQ_LEN(written
, buff
, "1.3");
2719 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.875);
2720 ASSERT_STREQ_LEN(written
, buff
, "1.8");
2722 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.75);
2723 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
2725 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.25);
2726 ASSERT_STREQ_LEN(written
, buff
, "-1.3");
2728 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.125);
2729 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
2731 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.625);
2732 ASSERT_STREQ_LEN(written
, buff
, "-1.7");
2734 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.375);
2735 ASSERT_STREQ_LEN(written
, buff
, "-1.4");
2737 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.875);
2738 ASSERT_STREQ_LEN(written
, buff
, "-1.9");
2741 if (ForceRoundingMode
r(RoundingMode::TowardZero
); r
.success
) {
2742 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.75);
2743 ASSERT_STREQ_LEN(written
, buff
, "1.7");
2745 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.25);
2746 ASSERT_STREQ_LEN(written
, buff
, "1.2");
2748 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.125);
2749 ASSERT_STREQ_LEN(written
, buff
, "1.1");
2751 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.625);
2752 ASSERT_STREQ_LEN(written
, buff
, "1.6");
2754 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.375);
2755 ASSERT_STREQ_LEN(written
, buff
, "1.3");
2757 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", 1.875);
2758 ASSERT_STREQ_LEN(written
, buff
, "1.8");
2760 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.75);
2761 ASSERT_STREQ_LEN(written
, buff
, "-1.7");
2763 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.25);
2764 ASSERT_STREQ_LEN(written
, buff
, "-1.2");
2766 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.125);
2767 ASSERT_STREQ_LEN(written
, buff
, "-1.1");
2769 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.625);
2770 ASSERT_STREQ_LEN(written
, buff
, "-1.6");
2772 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.375);
2773 ASSERT_STREQ_LEN(written
, buff
, "-1.3");
2775 written
= LIBC_NAMESPACE::sprintf(buff
, "%.2g", -1.875);
2776 ASSERT_STREQ_LEN(written
, buff
, "-1.8");
2780 written
= LIBC_NAMESPACE::sprintf(buff
, "%+g", 1.0);
2781 ASSERT_STREQ_LEN(written
, buff
, "+1");
2783 written
= LIBC_NAMESPACE::sprintf(buff
, "%+g", -1.0);
2784 ASSERT_STREQ_LEN(written
, buff
, "-1");
2786 written
= LIBC_NAMESPACE::sprintf(buff
, "% g", 1.0);
2787 ASSERT_STREQ_LEN(written
, buff
, " 1");
2789 written
= LIBC_NAMESPACE::sprintf(buff
, "% g", -1.0);
2790 ASSERT_STREQ_LEN(written
, buff
, "-1");
2792 written
= LIBC_NAMESPACE::sprintf(buff
, "%-15g", 1.5);
2793 ASSERT_STREQ_LEN(written
, buff
, "1.5 ");
2795 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.g", 1.0);
2796 ASSERT_STREQ_LEN(written
, buff
, "1.");
2798 written
= LIBC_NAMESPACE::sprintf(buff
, "%#g", 1.0);
2799 ASSERT_STREQ_LEN(written
, buff
, "1.00000");
2801 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.0g", 1.5);
2802 ASSERT_STREQ_LEN(written
, buff
, "2.");
2804 written
= LIBC_NAMESPACE::sprintf(buff
, "%015g", 1.5);
2805 ASSERT_STREQ_LEN(written
, buff
, "0000000000001.5");
2807 written
= LIBC_NAMESPACE::sprintf(buff
, "%015g", -1.5);
2808 ASSERT_STREQ_LEN(written
, buff
, "-000000000001.5");
2810 written
= LIBC_NAMESPACE::sprintf(buff
, "%+- #0g", 0.0);
2811 ASSERT_STREQ_LEN(written
, buff
, "+0.00000");
2815 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.3g", 9.99);
2816 ASSERT_STREQ_LEN(written
, buff
, " 9.99");
2818 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.2g", 9.99);
2819 ASSERT_STREQ_LEN(written
, buff
, " 10");
2821 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.1g", 9.99);
2822 ASSERT_STREQ_LEN(written
, buff
, " 1e+01");
2824 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.3g", 9.99);
2825 ASSERT_STREQ_LEN(written
, buff
, "9.99 ");
2827 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.2g", 9.99);
2828 ASSERT_STREQ_LEN(written
, buff
, "10 ");
2830 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.1g", 9.99);
2831 ASSERT_STREQ_LEN(written
, buff
, "1e+01 ");
2833 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.1g", 1.0e-50);
2834 ASSERT_STREQ_LEN(written
, buff
, "1e-50 ");
2836 written
= LIBC_NAMESPACE::sprintf(buff
, "%30g", 1234567890123456789.0);
2837 ASSERT_STREQ_LEN(written
, buff
, " 1.23457e+18");
2839 written
= LIBC_NAMESPACE::sprintf(buff
, "%-30g", 1234567890123456789.0);
2840 ASSERT_STREQ_LEN(written
, buff
, "1.23457e+18 ");
2842 written
= LIBC_NAMESPACE::sprintf(buff
, "%25.15g", 9999999999999.99);
2843 ASSERT_STREQ_LEN(written
, buff
, " 9999999999999.99");
2845 written
= LIBC_NAMESPACE::sprintf(buff
, "%25.14g", 9999999999999.99);
2846 ASSERT_STREQ_LEN(written
, buff
, " 10000000000000");
2848 written
= LIBC_NAMESPACE::sprintf(buff
, "%25.13g", 9999999999999.99);
2849 ASSERT_STREQ_LEN(written
, buff
, " 1e+13");
2851 written
= LIBC_NAMESPACE::sprintf(buff
, "%#12.3g %-12.3g", 0.1, 256.0);
2852 ASSERT_STREQ_LEN(written
, buff
, " 0.100 256 ");
2854 written
= LIBC_NAMESPACE::sprintf(buff
, "%+-#12.3g % 012.3g", 0.1256, 1256.0);
2855 ASSERT_STREQ_LEN(written
, buff
, "+0.126 0001.26e+03");
2858 TEST(LlvmLibcSPrintfTest
, FloatAutoLongDoubleConv
) {
2862 ForceRoundingMode
r(RoundingMode::Nearest
);
2864 // Length Modifier Tests.
2866 // TODO: Tests for other long double types
2867 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
2869 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lg", 0xf.fffffffffffffffp
+16380L);
2870 ASSERT_STREQ_LEN(written
, buff
, "1.18973e+4932");
2872 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lg", 0xa.aaaaaaaaaaaaaabp
-7L);
2873 ASSERT_STREQ_LEN(written
, buff
, "0.0833333");
2875 #ifndef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION
2876 written
= LIBC_NAMESPACE::sprintf(buff
, "%.60Lg", 0xa.aaaaaaaaaaaaaabp
-7L);
2879 "0.0833333333333333333355920878593448009041821933351457118988037");
2882 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
2884 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lg", 9.99999999999e-100L);
2885 ASSERT_STREQ_LEN(written
, buff
, "1e-99");
2887 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lg", 1e100L
);
2888 ASSERT_STREQ_LEN(written
, buff
, "1e+100");
2890 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lg", 1.0L);
2891 ASSERT_STREQ_LEN(written
, buff
, "1");
2893 written
= LIBC_NAMESPACE::sprintf(buff
, "%Lg", 0.1L);
2894 ASSERT_STREQ_LEN(written
, buff
, "0.1");
2896 char big_buff
[10000];
2897 written
= LIBC_NAMESPACE::sprintf(big_buff
, "%Lg", 1e1000L
);
2898 ASSERT_STREQ_LEN(written
, big_buff
, "1e+1000");
2900 written
= LIBC_NAMESPACE::sprintf(big_buff
, "%Lg", 1e4900L
);
2901 ASSERT_STREQ_LEN(written
, big_buff
, "1e+4900");
2904 #endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
2906 #if defined(LIBC_COMPILER_HAS_FIXED_POINT) && \
2907 !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT)
2908 TEST(LlvmLibcSPrintfTest
, FixedConv
) {
2912 // These numeric tests are potentially a little weak, but the fuzz test is
2913 // more thorough than my handwritten tests tend to be.
2915 // TODO: Replace hex literals with their appropriate fixed point literals.
2917 written
= LIBC_NAMESPACE::sprintf(buff
, "%k", 0x0); // 0.0
2918 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2920 written
= LIBC_NAMESPACE::sprintf(buff
, "%k", 0x80000000); // -0.0
2921 ASSERT_STREQ_LEN(written
, buff
, "-0.000000");
2923 written
= LIBC_NAMESPACE::sprintf(buff
, "%r", 0xffff); // -fract max
2924 ASSERT_STREQ_LEN(written
, buff
, "-0.999969");
2926 written
= LIBC_NAMESPACE::sprintf(buff
, "%R", 0xffff); // unsigned fract max
2927 ASSERT_STREQ_LEN(written
, buff
, "0.999985");
2929 written
= LIBC_NAMESPACE::sprintf(buff
, "%k", 0xffffffff); // -accum max
2930 ASSERT_STREQ_LEN(written
, buff
, "-65535.999969");
2933 LIBC_NAMESPACE::sprintf(buff
, "%K", 0xffffffff); // unsigned accum max
2934 ASSERT_STREQ_LEN(written
, buff
, "65535.999985");
2936 written
= LIBC_NAMESPACE::sprintf(buff
, "%r", 0x7fff); // fract max
2937 ASSERT_STREQ_LEN(written
, buff
, "0.999969");
2939 written
= LIBC_NAMESPACE::sprintf(buff
, "%k", 0x7fffffff); // accum max
2940 ASSERT_STREQ_LEN(written
, buff
, "65535.999969");
2942 // Length Modifier Tests.
2944 written
= LIBC_NAMESPACE::sprintf(buff
, "%hk", 0x0); // 0.0
2945 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2947 written
= LIBC_NAMESPACE::sprintf(buff
, "%hk", 0xffff); // -short accum max
2948 ASSERT_STREQ_LEN(written
, buff
, "-255.992188");
2950 written
= LIBC_NAMESPACE::sprintf(buff
, "%hr", 0x0); // 0.0
2951 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2953 written
= LIBC_NAMESPACE::sprintf(buff
, "%hr", 0xff); // -short fract max
2954 ASSERT_STREQ_LEN(written
, buff
, "-0.992188");
2956 written
= LIBC_NAMESPACE::sprintf(buff
, "%hK", 0x0); // 0.0
2957 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2960 LIBC_NAMESPACE::sprintf(buff
, "%hK", 0xffff); // unsigned short accum max
2961 ASSERT_STREQ_LEN(written
, buff
, "255.996094");
2963 written
= LIBC_NAMESPACE::sprintf(buff
, "%hR", 0x0); // 0.0
2964 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2967 LIBC_NAMESPACE::sprintf(buff
, "%hR", 0xff); // unsigned short fract max
2968 ASSERT_STREQ_LEN(written
, buff
, "0.996094");
2970 written
= LIBC_NAMESPACE::sprintf(buff
, "%lk", 0x0ll
); // 0.0
2971 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2973 written
= LIBC_NAMESPACE::sprintf(buff
, "%lk",
2974 0xffffffffffffffff); //-long accum max
2975 ASSERT_STREQ_LEN(written
, buff
, "-4294967296.000000");
2977 written
= LIBC_NAMESPACE::sprintf(buff
, "%lr", 0x0); // 0.0
2978 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2980 written
= LIBC_NAMESPACE::sprintf(buff
, "%lr",
2981 0xffffffff); //-long fract max
2982 ASSERT_STREQ_LEN(written
, buff
, "-1.000000");
2984 written
= LIBC_NAMESPACE::sprintf(buff
, "%lK", 0x0ll
); // 0.0
2985 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2988 LIBC_NAMESPACE::sprintf(buff
, "%lK",
2989 0xffffffffffffffff); // unsigned long accum max
2990 ASSERT_STREQ_LEN(written
, buff
, "4294967296.000000");
2992 written
= LIBC_NAMESPACE::sprintf(buff
, "%lR", 0x0); // 0.0
2993 ASSERT_STREQ_LEN(written
, buff
, "0.000000");
2995 written
= LIBC_NAMESPACE::sprintf(buff
, "%lR",
2996 0xffffffff); // unsigned long fract max
2997 ASSERT_STREQ_LEN(written
, buff
, "1.000000");
3001 written
= LIBC_NAMESPACE::sprintf(buff
, "%10k", 0x0000a000); // 1.25
3002 ASSERT_STREQ_LEN(written
, buff
, " 1.250000");
3004 written
= LIBC_NAMESPACE::sprintf(buff
, "%10k", 0x8000a000); //-1.25
3005 ASSERT_STREQ_LEN(written
, buff
, " -1.250000");
3007 written
= LIBC_NAMESPACE::sprintf(buff
, "%8k", 0x0000a000); // 1.25
3008 ASSERT_STREQ_LEN(written
, buff
, "1.250000");
3010 written
= LIBC_NAMESPACE::sprintf(buff
, "%9k", 0x8000a000); //-1.25
3011 ASSERT_STREQ_LEN(written
, buff
, "-1.250000");
3013 written
= LIBC_NAMESPACE::sprintf(buff
, "%4k", 0x0000a000); // 1.25
3014 ASSERT_STREQ_LEN(written
, buff
, "1.250000");
3016 written
= LIBC_NAMESPACE::sprintf(buff
, "%4k", 0x8000a000); //-1.25
3017 ASSERT_STREQ_LEN(written
, buff
, "-1.250000");
3022 LIBC_NAMESPACE::sprintf(buff
, "%.16K", 0xFFFFFFFF); // unsigned accum max
3023 ASSERT_STREQ_LEN(written
, buff
, "65535.9999847412109375");
3025 written
= LIBC_NAMESPACE::sprintf(
3026 buff
, "%.32lK", 0xFFFFFFFFFFFFFFFF); // unsigned long accum max
3027 ASSERT_STREQ_LEN(written
, buff
,
3028 "4294967295.99999999976716935634613037109375");
3031 LIBC_NAMESPACE::sprintf(buff
, "%.0K", 0xFFFFFFFF); // unsigned accum max
3032 ASSERT_STREQ_LEN(written
, buff
, "65536");
3034 written
= LIBC_NAMESPACE::sprintf(buff
, "%.0R", 0xFFFF); // unsigned fract max
3035 ASSERT_STREQ_LEN(written
, buff
, "1");
3039 written
= LIBC_NAMESPACE::sprintf(buff
, "%+k", 0x0000a000); // 1.25
3040 ASSERT_STREQ_LEN(written
, buff
, "+1.250000");
3042 written
= LIBC_NAMESPACE::sprintf(buff
, "%+k", 0x8000a000); //-1.25
3043 ASSERT_STREQ_LEN(written
, buff
, "-1.250000");
3045 written
= LIBC_NAMESPACE::sprintf(buff
, "% k", 0x0000a000); // 1.25
3046 ASSERT_STREQ_LEN(written
, buff
, " 1.250000");
3048 written
= LIBC_NAMESPACE::sprintf(buff
, "% k", 0x8000a000); //-1.25
3049 ASSERT_STREQ_LEN(written
, buff
, "-1.250000");
3051 // unsigned variants ignore sign flags.
3052 written
= LIBC_NAMESPACE::sprintf(buff
, "%+K", 0x00014000); // 1.25
3053 ASSERT_STREQ_LEN(written
, buff
, "1.250000");
3055 written
= LIBC_NAMESPACE::sprintf(buff
, "% K", 0x00014000); // 1.25
3056 ASSERT_STREQ_LEN(written
, buff
, "1.250000");
3058 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10k", 0x0000c000); // 1.5
3059 ASSERT_STREQ_LEN(written
, buff
, "1.500000 ");
3061 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.k", 0x00008000); // 1.0
3062 ASSERT_STREQ_LEN(written
, buff
, "1.");
3064 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.0k", 0x0000c000); // 1.5
3065 ASSERT_STREQ_LEN(written
, buff
, "2.");
3067 written
= LIBC_NAMESPACE::sprintf(buff
, "%010k", 0x0000c000); // 1.5
3068 ASSERT_STREQ_LEN(written
, buff
, "001.500000");
3070 written
= LIBC_NAMESPACE::sprintf(buff
, "%010k", 0x8000c000); //-1.5
3071 ASSERT_STREQ_LEN(written
, buff
, "-01.500000");
3073 written
= LIBC_NAMESPACE::sprintf(buff
, "%+- #0k", 0); // 0.0
3074 ASSERT_STREQ_LEN(written
, buff
, "+0.000000");
3078 written
= LIBC_NAMESPACE::sprintf(buff
, "%10.2k", 0x0004feb8); // 9.99
3079 ASSERT_STREQ_LEN(written
, buff
, " 9.99");
3081 written
= LIBC_NAMESPACE::sprintf(buff
, "%5.1k", 0x0004feb8); // 9.99
3082 ASSERT_STREQ_LEN(written
, buff
, " 10.0");
3084 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10.2k", 0x0004feb8); // 9.99
3085 ASSERT_STREQ_LEN(written
, buff
, "9.99 ");
3087 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.1k", 0x0004feb8); // 9.99
3088 ASSERT_STREQ_LEN(written
, buff
, "10.0 ");
3090 written
= LIBC_NAMESPACE::sprintf(buff
, "%-5.1k", 0x00000001); // accum min
3091 ASSERT_STREQ_LEN(written
, buff
, "0.0 ");
3093 written
= LIBC_NAMESPACE::sprintf(buff
, "%30k", 0x7fffffff); // accum max
3094 ASSERT_STREQ_LEN(written
, buff
, " 65535.999969");
3096 written
= LIBC_NAMESPACE::sprintf(buff
, "%-30k", 0x7fffffff); // accum max
3097 ASSERT_STREQ_LEN(written
, buff
, "65535.999969 ");
3099 written
= LIBC_NAMESPACE::sprintf(buff
, "%20.2lK",
3100 0x3b9ac9ffFD70A3D7); // 999999999.99
3101 ASSERT_STREQ_LEN(written
, buff
, " 999999999.99");
3103 written
= LIBC_NAMESPACE::sprintf(buff
, "%20.1lK",
3104 0x3b9ac9ffFD70A3D7); // 999999999.99
3105 ASSERT_STREQ_LEN(written
, buff
, " 1000000000.0");
3107 written
= LIBC_NAMESPACE::sprintf(buff
, "%12.3R %-12.3k", 0x1999,
3108 0x00800000); // 0.1, 256.0
3109 ASSERT_STREQ_LEN(written
, buff
, " 0.100 256.000 ");
3112 LIBC_NAMESPACE::sprintf(buff
, "%+-#12.3lk % 012.3k", 0x000000001013a92all
,
3113 0x02740000); // 0.126, 1256.0
3114 ASSERT_STREQ_LEN(written
, buff
, "+0.126 0001256.000");
3116 #endif // defined(LIBC_COMPILER_HAS_FIXED_POINT) &&
3117 // !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT)
3119 #ifndef LIBC_COPT_PRINTF_DISABLE_STRERROR
3120 TEST(LlvmLibcSPrintfTest
, StrerrorConv
) {
3124 LIBC_NAMESPACE::libc_errno
= 0;
3125 written
= LIBC_NAMESPACE::sprintf(buff
, "%m");
3126 ASSERT_STREQ_LEN(written
, buff
, "Success");
3128 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3129 written
= LIBC_NAMESPACE::sprintf(buff
, "%m");
3130 ASSERT_STREQ_LEN(written
, buff
, "Numerical result out of range");
3132 // Check that it correctly consumes no arguments.
3133 LIBC_NAMESPACE::libc_errno
= 0;
3134 written
= LIBC_NAMESPACE::sprintf(buff
, "%m %d", 1);
3135 ASSERT_STREQ_LEN(written
, buff
, "Success 1");
3139 LIBC_NAMESPACE::libc_errno
= 0;
3140 written
= LIBC_NAMESPACE::sprintf(buff
, "%10m");
3141 ASSERT_STREQ_LEN(written
, buff
, " Success");
3143 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3144 written
= LIBC_NAMESPACE::sprintf(buff
, "%10m");
3145 ASSERT_STREQ_LEN(written
, buff
, "Numerical result out of range");
3149 LIBC_NAMESPACE::libc_errno
= 0;
3150 written
= LIBC_NAMESPACE::sprintf(buff
, "%.10m");
3151 ASSERT_STREQ_LEN(written
, buff
, "Success");
3153 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3154 written
= LIBC_NAMESPACE::sprintf(buff
, "%.10m");
3155 ASSERT_STREQ_LEN(written
, buff
, "Numerical ");
3157 // Flag Tests (Only '-' since the others only affect ints)
3159 LIBC_NAMESPACE::libc_errno
= 0;
3160 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10m");
3161 ASSERT_STREQ_LEN(written
, buff
, "Success ");
3163 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3164 written
= LIBC_NAMESPACE::sprintf(buff
, "%-10m");
3165 ASSERT_STREQ_LEN(written
, buff
, "Numerical result out of range");
3168 // Since alt mode here is effectively a completely separate conversion, it
3169 // gets separate tests.
3171 LIBC_NAMESPACE::libc_errno
= 0;
3172 written
= LIBC_NAMESPACE::sprintf(buff
, "%#m");
3173 ASSERT_STREQ_LEN(written
, buff
, "0");
3175 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3176 written
= LIBC_NAMESPACE::sprintf(buff
, "%#m");
3177 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3179 LIBC_NAMESPACE::libc_errno
= -9999;
3180 written
= LIBC_NAMESPACE::sprintf(buff
, "%#m");
3181 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3185 LIBC_NAMESPACE::libc_errno
= 0;
3186 written
= LIBC_NAMESPACE::sprintf(buff
, "%#10m");
3187 ASSERT_STREQ_LEN(written
, buff
, " 0");
3189 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3190 written
= LIBC_NAMESPACE::sprintf(buff
, "%#10m");
3191 ASSERT_STREQ_LEN(written
, buff
, " ERANGE");
3193 LIBC_NAMESPACE::libc_errno
= -9999;
3194 written
= LIBC_NAMESPACE::sprintf(buff
, "%#10m");
3195 ASSERT_STREQ_LEN(written
, buff
, " -9999");
3197 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3198 written
= LIBC_NAMESPACE::sprintf(buff
, "%#3m");
3199 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3201 LIBC_NAMESPACE::libc_errno
= -9999;
3202 written
= LIBC_NAMESPACE::sprintf(buff
, "%#3m");
3203 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3205 // Alt Mode Precision
3207 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3208 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.10m");
3209 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3211 LIBC_NAMESPACE::libc_errno
= -9999;
3212 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.10m");
3213 ASSERT_STREQ_LEN(written
, buff
, "-0000009999");
3215 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3216 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.3m");
3217 ASSERT_STREQ_LEN(written
, buff
, "ERA");
3219 LIBC_NAMESPACE::libc_errno
= -9999;
3220 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.3m");
3221 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3223 // We don't test precision (or int flags) on errno = 0 because it behaves
3224 // weirdly, see the docs for more information.
3225 LIBC_NAMESPACE::libc_errno
= 0;
3226 written
= LIBC_NAMESPACE::sprintf(buff
, "%#.1m");
3227 ASSERT_STREQ_LEN(written
, buff
, "0");
3232 LIBC_NAMESPACE::libc_errno
= 0;
3233 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-10m");
3234 ASSERT_STREQ_LEN(written
, buff
, "0 ");
3236 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3237 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-10m");
3238 ASSERT_STREQ_LEN(written
, buff
, "ERANGE ");
3240 LIBC_NAMESPACE::libc_errno
= -9999;
3241 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-10m");
3242 ASSERT_STREQ_LEN(written
, buff
, "-9999 ");
3244 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3245 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-3m");
3246 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3248 LIBC_NAMESPACE::libc_errno
= -9999;
3249 written
= LIBC_NAMESPACE::sprintf(buff
, "%#-3m");
3250 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3253 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3254 written
= LIBC_NAMESPACE::sprintf(buff
, "%#+m");
3255 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3257 LIBC_NAMESPACE::libc_errno
= -9999;
3258 written
= LIBC_NAMESPACE::sprintf(buff
, "%#+m");
3259 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3261 // Technically 9999 could be a valid error, since the standard just says errno
3262 // macros are "distinct positive values". In practice I don't expect this to
3263 // come up, but I've avoided it for the other %m tests for ease of
3264 // refactoring if necessary. Here it needs to be positive to test that the
3265 // flags that only affect positive signed integers are properly passed along.
3266 LIBC_NAMESPACE::libc_errno
= 9999;
3267 written
= LIBC_NAMESPACE::sprintf(buff
, "%#+m");
3268 ASSERT_STREQ_LEN(written
, buff
, "+9999");
3271 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3272 written
= LIBC_NAMESPACE::sprintf(buff
, "%# m");
3273 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3275 LIBC_NAMESPACE::libc_errno
= -9999;
3276 written
= LIBC_NAMESPACE::sprintf(buff
, "%# m");
3277 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3279 LIBC_NAMESPACE::libc_errno
= 9999;
3280 written
= LIBC_NAMESPACE::sprintf(buff
, "%# m");
3281 ASSERT_STREQ_LEN(written
, buff
, " 9999");
3285 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3286 written
= LIBC_NAMESPACE::sprintf(buff
, "%#010m");
3287 ASSERT_STREQ_LEN(written
, buff
, " ERANGE");
3289 LIBC_NAMESPACE::libc_errno
= -9999;
3290 written
= LIBC_NAMESPACE::sprintf(buff
, "%#010m");
3291 ASSERT_STREQ_LEN(written
, buff
, "-000009999");
3293 LIBC_NAMESPACE::libc_errno
= ERANGE
;
3294 written
= LIBC_NAMESPACE::sprintf(buff
, "%#03m");
3295 ASSERT_STREQ_LEN(written
, buff
, "ERANGE");
3297 LIBC_NAMESPACE::libc_errno
= -9999;
3298 written
= LIBC_NAMESPACE::sprintf(buff
, "%#03m");
3299 ASSERT_STREQ_LEN(written
, buff
, "-9999");
3301 #endif // LIBC_COPT_PRINTF_DISABLE_STRERROR
3303 #ifndef LIBC_COPT_PRINTF_DISABLE_WRITE_INT
3304 TEST(LlvmLibcSPrintfTest
, WriteIntConv
) {
3310 written
= LIBC_NAMESPACE::sprintf(buff
, "12345%n67890", &test_val
);
3311 EXPECT_EQ(test_val
, 5);
3312 ASSERT_STREQ_LEN(written
, buff
, "1234567890");
3315 written
= LIBC_NAMESPACE::sprintf(buff
, "%n", &test_val
);
3316 EXPECT_EQ(test_val
, 0);
3317 ASSERT_STREQ_LEN(written
, buff
, "");
3320 written
= LIBC_NAMESPACE::sprintf(buff
, "ABC%hhnDEF", &test_val
);
3321 EXPECT_EQ(test_val
, 0x103);
3322 ASSERT_STREQ_LEN(written
, buff
, "ABCDEF");
3325 written
= LIBC_NAMESPACE::sprintf(buff
, "%s%n", "87654321", &test_val
);
3326 EXPECT_EQ(test_val
, 8);
3327 ASSERT_STREQ_LEN(written
, buff
, "87654321");
3329 #ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
3330 written
= LIBC_NAMESPACE::sprintf(buff
, "abc123%n", nullptr);
3331 EXPECT_LT(written
, 0);
3332 #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
3334 #endif // LIBC_COPT_PRINTF_DISABLE_WRITE_INT
3336 #ifndef LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
3337 TEST(LlvmLibcSPrintfTest
, IndexModeParsing
) {
3341 written
= LIBC_NAMESPACE::sprintf(buff
, "%1$s", "abcDEF123");
3342 ASSERT_STREQ_LEN(written
, buff
, "abcDEF123");
3344 written
= LIBC_NAMESPACE::sprintf(buff
, "%1$s %%", "abcDEF123");
3345 ASSERT_STREQ_LEN(written
, buff
, "abcDEF123 %");
3348 LIBC_NAMESPACE::sprintf(buff
, "%3$s %1$s %2$s", "is", "hard", "ordering");
3349 ASSERT_STREQ_LEN(written
, buff
, "ordering is hard");
3351 written
= LIBC_NAMESPACE::sprintf(
3352 buff
, "%10$s %9$s %8$c %7$s %6$s, %6$s %5$s %4$-*1$s %3$.*11$s %2$s. %%",
3353 6, "pain", "alphabetical", "such", "is", "this", "do", 'u', "would",
3355 ASSERT_STREQ_LEN(written
, buff
,
3356 "why would u do this, this is such a pain. %");
3358 #endif // LIBC_COPT_PRINTF_DISABLE_INDEX_MODE