1//===-- Unittests for sprintf ---------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/stdio/sprintf.h"
10
11#include "src/__support/FPUtil/FPBits.h"
12#include "test/UnitTest/RoundingModeUtils.h"
13#include "test/UnitTest/Test.h"
14#include <inttypes.h>
15
16// TODO: Add a comment here explaining the printf format string.
17
18// #include <stdio.h>
19// namespace LIBC_NAMESPACE {
20// using ::sprintf;
21// }
22
23class LlvmLibcSPrintfTest : public LIBC_NAMESPACE::testing::Test {
24protected:
25 char buff[1000];
26 int written;
27};
28
29using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode;
30using LIBC_NAMESPACE::fputil::testing::RoundingMode;
31
32// Subtract 1 from sizeof(expected_str) to account for the null byte.
33#define ASSERT_STREQ_LEN(actual_written, actual_str, expected_str) \
34 EXPECT_EQ(actual_written, static_cast<int>(sizeof(expected_str) - 1)); \
35 EXPECT_STREQ(actual_str, expected_str);
36
37#define macro_test(FMT, X, expected) \
38 do { \
39 for (char &c : buff) { \
40 c = 0; \
41 } \
42 LIBC_NAMESPACE::sprintf(buff, "%" FMT, X); \
43 ASSERT_STREQ(buff, expected); \
44 } while (0)
45
46TEST(LlvmLibcSPrintfTest, Macros) {
47 char buff[128];
48 macro_test(PRIu8, 1, "1");
49 macro_test(PRIX16, 0xAA, "AA");
50 macro_test(PRId32, -123, "-123");
51 macro_test(PRIX32, 0xFFFFFF85, "FFFFFF85");
52 macro_test(PRIo8, 0xFF, "377");
53 macro_test(PRIo64, 0123, "123");
54}
55
56TEST(LlvmLibcSPrintfTest, SimpleNoConv) {
57 char buff[64];
58 int written;
59
60 written =
61 LIBC_NAMESPACE::sprintf(buffer: buff, format: "A simple string with no conversions.");
62 EXPECT_EQ(written, 36);
63 ASSERT_STREQ(buff, "A simple string with no conversions.");
64}
65
66TEST(LlvmLibcSPrintfTest, PercentConv) {
67 char buff[64];
68 int written;
69
70 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%%");
71 EXPECT_EQ(written, 1);
72 ASSERT_STREQ(buff, "%");
73
74 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "abc %% def");
75 EXPECT_EQ(written, 9);
76 ASSERT_STREQ(buff, "abc % def");
77
78 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%%%%%%");
79 EXPECT_EQ(written, 3);
80 ASSERT_STREQ(buff, "%%%");
81}
82
83TEST(LlvmLibcSPrintfTest, CharConv) {
84 char buff[64];
85 int written;
86
87 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%c", 'a');
88 EXPECT_EQ(written, 1);
89 ASSERT_STREQ(buff, "a");
90
91 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%3c %-3c", '1', '2');
92 EXPECT_EQ(written, 7);
93 ASSERT_STREQ(buff, " 1 2 ");
94
95 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%*c", 2, '3');
96 EXPECT_EQ(written, 2);
97 ASSERT_STREQ(buff, " 3");
98}
99
100TEST(LlvmLibcSPrintfTest, StringConv) {
101 char buff[64];
102 int written;
103
104 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%s", "abcDEF123");
105 EXPECT_EQ(written, 9);
106 ASSERT_STREQ(buff, "abcDEF123");
107
108 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10s %-10s", "centered", "title");
109 EXPECT_EQ(written, 21);
110 ASSERT_STREQ(buff, " centered title ");
111
112 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.4s%-4.4s", "words can describe",
113 "soups most delicious");
114 EXPECT_EQ(written, 9);
115 ASSERT_STREQ(buff, "word soup");
116
117 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%*s %.*s %*.*s", 10, "beginning", 2,
118 "isn't", 12, 10, "important. Ever.");
119 EXPECT_EQ(written, 26);
120 ASSERT_STREQ(buff, " beginning is important.");
121
122#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
123 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%s", nullptr);
124 EXPECT_EQ(written, 6);
125 ASSERT_STREQ(buff, "(null)");
126#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
127}
128
129TEST(LlvmLibcSPrintfTest, IntConv) {
130 char buff[64];
131 int written;
132
133 // Basic Tests.
134
135 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%d", 123);
136 EXPECT_EQ(written, 3);
137 ASSERT_STREQ(buff, "123");
138
139 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%i", -456);
140 EXPECT_EQ(written, 4);
141 ASSERT_STREQ(buff, "-456");
142
143 // Length Modifier Tests.
144
145 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%hhu", 257); // 0x101
146 EXPECT_EQ(written, 1);
147 ASSERT_STREQ(buff, "1");
148
149 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%llu", 18446744073709551615ull);
150 EXPECT_EQ(written, 20);
151 ASSERT_STREQ(buff, "18446744073709551615"); // ull max
152
153 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%u", ~0);
154 if (sizeof(int) == 4) {
155 EXPECT_EQ(written, 10);
156 ASSERT_STREQ(buff, "4294967295");
157 }
158
159 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%tu", ~ptrdiff_t(0));
160 if (sizeof(ptrdiff_t) == 8) {
161 EXPECT_EQ(written, 20);
162 ASSERT_STREQ(buff, "18446744073709551615");
163 } else if (sizeof(ptrdiff_t) == 4) {
164 EXPECT_EQ(written, 10);
165 ASSERT_STREQ(buff, "4294967295");
166 }
167
168 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%lld", -9223372036854775807ll - 1ll);
169 EXPECT_EQ(written, 20);
170 ASSERT_STREQ(buff, "-9223372036854775808"); // ll min
171
172 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w3d", 5807);
173 EXPECT_EQ(written, 1);
174 ASSERT_STREQ(buff, "7");
175
176 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w3d", 1);
177 EXPECT_EQ(written, 1);
178 ASSERT_STREQ(buff, "1");
179
180 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w64d", 9223372036854775807ll);
181 EXPECT_EQ(written, 19);
182 ASSERT_STREQ(buff, "9223372036854775807");
183
184 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w-1d", 5807);
185 EXPECT_EQ(written, 5);
186 ASSERT_STREQ(buff, "%w-1d");
187
188 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w0d", 5807);
189 EXPECT_EQ(written, 4);
190 ASSERT_STREQ(buff, "%w0d");
191
192 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w999d", 9223372036854775807ll);
193 EXPECT_EQ(written, 19);
194 ASSERT_STREQ(buff, "9223372036854775807");
195
196 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%winvalid%w1d", 5807, 5807);
197 EXPECT_EQ(written, 10);
198 ASSERT_STREQ(buff, "%winvalid1");
199
200 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w-1d%w1d", 5807, 5807);
201 EXPECT_EQ(written, 6);
202 ASSERT_STREQ(buff, "%w-1d1");
203
204 char format[64];
205 char uintmax[128];
206 LIBC_NAMESPACE::sprintf(buffer: format, format: "%%w%du", sizeof(uintmax_t) * CHAR_BIT);
207 const int uintmax_len =
208 LIBC_NAMESPACE::sprintf(buffer: uintmax, format: "%ju", sizeof(uintmax_t) * CHAR_BIT);
209 written = LIBC_NAMESPACE::sprintf(buffer: buff, format, sizeof(uintmax_t) * CHAR_BIT);
210 EXPECT_EQ(written, uintmax_len);
211 ASSERT_STREQ(buff, uintmax);
212
213 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w64u", 18446744073709551615ull);
214 EXPECT_EQ(written, 20);
215 ASSERT_STREQ(buff, "18446744073709551615"); // ull max
216
217 written =
218 LIBC_NAMESPACE::sprintf(buffer: buff, format: "%w64d", -9223372036854775807ll - 1ll);
219 EXPECT_EQ(written, 20);
220 ASSERT_STREQ(buff, "-9223372036854775808"); // ll min
221
222 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf3d", 5807);
223 EXPECT_EQ(written, 1);
224 ASSERT_STREQ(buff, "7");
225
226 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf3d", 1);
227 EXPECT_EQ(written, 1);
228 ASSERT_STREQ(buff, "1");
229
230 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf64u", 18446744073709551615ull);
231 EXPECT_EQ(written, 20);
232 ASSERT_STREQ(buff, "18446744073709551615"); // ull max
233
234 written =
235 LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf64d", -9223372036854775807ll - 1ll);
236 EXPECT_EQ(written, 20);
237 ASSERT_STREQ(buff, "-9223372036854775808"); // ll min
238
239 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf0d", 5807);
240 EXPECT_EQ(written, 5);
241 ASSERT_STREQ(buff, "%wf0d");
242
243 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf-1d", 5807);
244 EXPECT_EQ(written, 6);
245 ASSERT_STREQ(buff, "%wf-1d");
246
247 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wfinvalid%wf1d", 5807, 5807);
248 EXPECT_EQ(written, 11);
249 ASSERT_STREQ(buff, "%wfinvalid1");
250
251 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf-1d%wf1d", 5807, 5807);
252 EXPECT_EQ(written, 7);
253 ASSERT_STREQ(buff, "%wf-1d1");
254
255 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%wf999d", 9223372036854775807ll);
256 EXPECT_EQ(written, 19);
257 ASSERT_STREQ(buff, "9223372036854775807");
258
259 // Min Width Tests.
260
261 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%4d", 789);
262 EXPECT_EQ(written, 4);
263 ASSERT_STREQ(buff, " 789");
264
265 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%2d", 987);
266 EXPECT_EQ(written, 3);
267 ASSERT_STREQ(buff, "987");
268
269 // Precision Tests.
270
271 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%d", 0);
272 EXPECT_EQ(written, 1);
273 ASSERT_STREQ(buff, "0");
274
275 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0d", 0);
276 EXPECT_EQ(written, 0);
277 ASSERT_STREQ(buff, "");
278
279 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5d", 654);
280 EXPECT_EQ(written, 5);
281 ASSERT_STREQ(buff, "00654");
282
283 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5d", -321);
284 EXPECT_EQ(written, 6);
285 ASSERT_STREQ(buff, "-00321");
286
287 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2d", 135);
288 EXPECT_EQ(written, 3);
289 ASSERT_STREQ(buff, "135");
290
291 // Flag Tests.
292
293 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5d", -321);
294 EXPECT_EQ(written, 6);
295 ASSERT_STREQ(buff, "-00321");
296
297 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5d", 246);
298 EXPECT_EQ(written, 5);
299 ASSERT_STREQ(buff, "246 ");
300
301 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5d", -147);
302 EXPECT_EQ(written, 5);
303 ASSERT_STREQ(buff, "-147 ");
304
305 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+d", 258);
306 EXPECT_EQ(written, 4);
307 ASSERT_STREQ(buff, "+258");
308
309 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% d", 369);
310 EXPECT_EQ(written, 4);
311 ASSERT_STREQ(buff, " 369");
312
313 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%05d", 470);
314 EXPECT_EQ(written, 5);
315 ASSERT_STREQ(buff, "00470");
316
317 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%05d", -581);
318 EXPECT_EQ(written, 5);
319 ASSERT_STREQ(buff, "-0581");
320
321 // Combined Tests.
322
323 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+ u", 692);
324 EXPECT_EQ(written, 3);
325 ASSERT_STREQ(buff, "692");
326
327 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+ -05d", 703);
328 EXPECT_EQ(written, 5);
329 ASSERT_STREQ(buff, "+703 ");
330
331 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%7.5d", 814);
332 EXPECT_EQ(written, 7);
333 ASSERT_STREQ(buff, " 00814");
334
335 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%7.5d", -925);
336 EXPECT_EQ(written, 7);
337 ASSERT_STREQ(buff, " -00925");
338
339 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%7.5d", 159);
340 EXPECT_EQ(written, 7);
341 ASSERT_STREQ(buff, " 00159");
342
343 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% -7.5d", 260);
344 EXPECT_EQ(written, 7);
345 ASSERT_STREQ(buff, " 00260 ");
346
347 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5.4d", 10000);
348 EXPECT_EQ(written, 5);
349 ASSERT_STREQ(buff, "10000");
350
351 // Multiple Conversion Tests.
352
353 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10d %-10d", 456, -789);
354 EXPECT_EQ(written, 21);
355 ASSERT_STREQ(buff, " 456 -789 ");
356
357 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.4d%+.4u", 75, 25);
358 EXPECT_EQ(written, 9);
359 ASSERT_STREQ(buff, "0075 0025");
360
361 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% 05hhi %+-0.5llu %-+ 06.3zd",
362 256 + 127, 68719476736ll, size_t(2));
363 EXPECT_EQ(written, 24);
364 ASSERT_STREQ(buff, " 0127 68719476736 +002 ");
365}
366
367TEST(LlvmLibcSPrintfTest, HexConv) {
368 char buff[64];
369 int written;
370
371 // Basic Tests.
372
373 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%x", 0x123a);
374 EXPECT_EQ(written, 4);
375 ASSERT_STREQ(buff, "123a");
376
377 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%X", 0x456b);
378 EXPECT_EQ(written, 4);
379 ASSERT_STREQ(buff, "456B");
380
381 // Length Modifier Tests.
382
383 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%hhx", 0x10001);
384 EXPECT_EQ(written, 1);
385 ASSERT_STREQ(buff, "1");
386
387 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%llx", 0xffffffffffffffffull);
388 EXPECT_EQ(written, 16);
389 ASSERT_STREQ(buff, "ffffffffffffffff"); // ull max
390
391 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%tX", ~ptrdiff_t(0));
392 if (sizeof(ptrdiff_t) == 8) {
393 EXPECT_EQ(written, 16);
394 ASSERT_STREQ(buff, "FFFFFFFFFFFFFFFF");
395 } else if (sizeof(ptrdiff_t) == 4) {
396 EXPECT_EQ(written, 8);
397 ASSERT_STREQ(buff, "FFFFFFFF");
398 }
399
400 // Min Width Tests.
401
402 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%4x", 0x789);
403 EXPECT_EQ(written, 4);
404 ASSERT_STREQ(buff, " 789");
405
406 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%2X", 0x987);
407 EXPECT_EQ(written, 3);
408 ASSERT_STREQ(buff, "987");
409
410 // Precision Tests.
411
412 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%x", 0);
413 EXPECT_EQ(written, 1);
414 ASSERT_STREQ(buff, "0");
415
416 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0x", 0);
417 EXPECT_EQ(written, 0);
418 ASSERT_STREQ(buff, "");
419
420 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5x", 0x1F3);
421 EXPECT_EQ(written, 5);
422 ASSERT_STREQ(buff, "001f3");
423
424 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2x", 0x135);
425 EXPECT_EQ(written, 3);
426 ASSERT_STREQ(buff, "135");
427
428 // Flag Tests.
429
430 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5x", 0x246);
431 EXPECT_EQ(written, 5);
432 ASSERT_STREQ(buff, "246 ");
433
434 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#x", 0xd3f);
435 EXPECT_EQ(written, 5);
436 ASSERT_STREQ(buff, "0xd3f");
437
438 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#x", 0);
439 EXPECT_EQ(written, 1);
440 ASSERT_STREQ(buff, "0");
441
442 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#X", 0xE40);
443 EXPECT_EQ(written, 5);
444 ASSERT_STREQ(buff, "0XE40");
445
446 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%05x", 0x470);
447 EXPECT_EQ(written, 5);
448 ASSERT_STREQ(buff, "00470");
449
450 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%0#6x", 0x8c3);
451 EXPECT_EQ(written, 6);
452 ASSERT_STREQ(buff, "0x08c3");
453
454 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-#6x", 0x5f0);
455 EXPECT_EQ(written, 6);
456 ASSERT_STREQ(buff, "0x5f0 ");
457
458 // Combined Tests.
459
460 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#-07x", 0x703);
461 EXPECT_EQ(written, 7);
462 ASSERT_STREQ(buff, "0x703 ");
463
464 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%7.5x", 0x814);
465 EXPECT_EQ(written, 7);
466 ASSERT_STREQ(buff, " 00814");
467
468 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#9.5X", 0x9d4);
469 EXPECT_EQ(written, 9);
470 ASSERT_STREQ(buff, " 0X009D4");
471
472 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.x", 0);
473 EXPECT_EQ(written, 0);
474 ASSERT_STREQ(buff, "");
475
476 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-7.5x", 0x260);
477 EXPECT_EQ(written, 7);
478 ASSERT_STREQ(buff, "00260 ");
479
480 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5.4x", 0x10000);
481 EXPECT_EQ(written, 5);
482 ASSERT_STREQ(buff, "10000");
483
484 // Multiple Conversion Tests.
485
486 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10X %-#10x", 0x45b, 0x789);
487 EXPECT_EQ(written, 21);
488 ASSERT_STREQ(buff, " 45B 0x789 ");
489
490 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.4x%#.4x", 0x75, 0x25);
491 EXPECT_EQ(written, 11);
492 ASSERT_STREQ(buff, "0075 0x0025");
493
494 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%04hhX %#.5llx %-6.3zX", 256 + 0x7f,
495 0x1000000000ll, size_t(2));
496 EXPECT_EQ(written, 24);
497 ASSERT_STREQ(buff, "007F 0x1000000000 002 ");
498}
499
500TEST(LlvmLibcSPrintfTest, BinConv) {
501 char buff[64];
502 int written;
503
504 // Basic Tests.
505
506 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%b", 42);
507 EXPECT_EQ(written, 6);
508 ASSERT_STREQ(buff, "101010");
509
510 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%B", 12081991);
511 EXPECT_EQ(written, 24);
512 ASSERT_STREQ(buff, "101110000101101101000111");
513
514 // Min Width Tests.
515
516 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10b", 0b101010);
517 EXPECT_EQ(written, 10);
518 ASSERT_STREQ(buff, " 101010");
519
520 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%2B", 0b101010);
521 EXPECT_EQ(written, 6);
522 ASSERT_STREQ(buff, "101010");
523
524 // Precision Tests.
525
526 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%b", 0);
527 EXPECT_EQ(written, 1);
528 ASSERT_STREQ(buff, "0");
529
530 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0b", 0);
531 EXPECT_EQ(written, 0);
532 ASSERT_STREQ(buff, "");
533
534 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5b", 0b111);
535 EXPECT_EQ(written, 5);
536 ASSERT_STREQ(buff, "00111");
537
538 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2b", 0b111);
539 EXPECT_EQ(written, 3);
540 ASSERT_STREQ(buff, "111");
541
542 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%3b", 0b111);
543 EXPECT_EQ(written, 3);
544 ASSERT_STREQ(buff, "111");
545
546 // Flag Tests.
547
548 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5b", 0b111);
549 EXPECT_EQ(written, 5);
550 ASSERT_STREQ(buff, "111 ");
551
552 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#b", 0b111);
553 EXPECT_EQ(written, 5);
554 ASSERT_STREQ(buff, "0b111");
555
556 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#b", 0);
557 EXPECT_EQ(written, 1);
558 ASSERT_STREQ(buff, "0");
559
560 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#B", 0b111);
561 EXPECT_EQ(written, 5);
562 ASSERT_STREQ(buff, "0B111");
563
564 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%05b", 0b111);
565 EXPECT_EQ(written, 5);
566 ASSERT_STREQ(buff, "00111");
567
568 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%0#6b", 0b111);
569 EXPECT_EQ(written, 6);
570 ASSERT_STREQ(buff, "0b0111");
571
572 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-#6b", 0b111);
573 EXPECT_EQ(written, 6);
574 ASSERT_STREQ(buff, "0b111 ");
575
576 // Combined Tests.
577
578 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#-07b", 0b111);
579 EXPECT_EQ(written, 7);
580 ASSERT_STREQ(buff, "0b111 ");
581
582 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%7.5b", 0b111);
583 EXPECT_EQ(written, 7);
584 ASSERT_STREQ(buff, " 00111");
585
586 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#9.5B", 0b111);
587 EXPECT_EQ(written, 9);
588 ASSERT_STREQ(buff, " 0B00111");
589
590 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.b", 0);
591 EXPECT_EQ(written, 0);
592 ASSERT_STREQ(buff, "");
593
594 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-7.5b", 0b111);
595 EXPECT_EQ(written, 7);
596 ASSERT_STREQ(buff, "00111 ");
597
598 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5.4b", 0b1111);
599 EXPECT_EQ(written, 5);
600 ASSERT_STREQ(buff, " 1111");
601
602 // Multiple Conversion Tests.
603
604 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10B %-#10b", 0b101, 0b110);
605 EXPECT_EQ(written, 21);
606 ASSERT_STREQ(buff, " 101 0b110 ");
607
608 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.4b%#.4b", 0b101, 0b110);
609 EXPECT_EQ(written, 11);
610 ASSERT_STREQ(buff, "0101 0b0110");
611}
612
613TEST(LlvmLibcSPrintfTest, PointerConv) {
614 char buff[64];
615 int written;
616
617 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%p", nullptr);
618 EXPECT_EQ(written, 9);
619 ASSERT_STREQ(buff, "(nullptr)");
620
621 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%p", 0x1a2b3c4d);
622 EXPECT_EQ(written, 10);
623 ASSERT_STREQ(buff, "0x1a2b3c4d");
624
625 if constexpr (sizeof(void *) > 4) {
626 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%p", 0x1a2b3c4d5e6f7081);
627 EXPECT_EQ(written, 18);
628 ASSERT_STREQ(buff, "0x1a2b3c4d5e6f7081");
629 }
630
631 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%p", buff);
632 EXPECT_GT(written, 0);
633
634 // Width tests:
635
636 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%20p", nullptr);
637 EXPECT_EQ(written, 20);
638 ASSERT_STREQ(buff, " (nullptr)");
639
640 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%20p", 0x1a2b3c4d);
641 EXPECT_EQ(written, 20);
642 ASSERT_STREQ(buff, " 0x1a2b3c4d");
643
644 // Flag tests:
645
646 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-20p", nullptr);
647 EXPECT_EQ(written, 20);
648 ASSERT_STREQ(buff, "(nullptr) ");
649
650 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-20p", 0x1a2b3c4d);
651 EXPECT_EQ(written, 20);
652 ASSERT_STREQ(buff, "0x1a2b3c4d ");
653
654 // Using the 0 flag is technically undefined, but here we're following the
655 // convention of matching the behavior of %#x.
656 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%020p", 0x1a2b3c4d);
657 EXPECT_EQ(written, 20);
658 ASSERT_STREQ(buff, "0x00000000001a2b3c4d");
659
660 // Precision tests:
661 // These are all undefined behavior. The precision option is undefined for %p.
662
663 // Precision specifies the number of characters for a string conversion.
664 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5p", nullptr);
665 EXPECT_EQ(written, 5);
666 ASSERT_STREQ(buff, "(null");
667
668 // Precision specifies the number of digits to be written for %x conversions,
669 // and the "0x" doesn't count as part of the digits.
670 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.20p", 0x1a2b3c4d);
671 EXPECT_EQ(written, 22);
672 ASSERT_STREQ(buff, "0x0000000000001a2b3c4d");
673}
674
675TEST(LlvmLibcSPrintfTest, OctConv) {
676 char buff[64];
677 int written;
678
679 // Basic Tests.
680
681 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%o", 01234);
682 EXPECT_EQ(written, 4);
683 ASSERT_STREQ(buff, "1234");
684
685 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%o", 04567);
686 EXPECT_EQ(written, 4);
687 ASSERT_STREQ(buff, "4567");
688
689 // Length Modifier Tests.
690
691 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%hho", 0401);
692 EXPECT_EQ(written, 1);
693 ASSERT_STREQ(buff, "1");
694
695 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%llo", 01777777777777777777777ull);
696 EXPECT_EQ(written, 22);
697 ASSERT_STREQ(buff, "1777777777777777777777"); // ull max
698
699 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%to", ~ptrdiff_t(0));
700 if (sizeof(ptrdiff_t) == 8) {
701 EXPECT_EQ(written, 22);
702 ASSERT_STREQ(buff, "1777777777777777777777");
703 } else if (sizeof(ptrdiff_t) == 4) {
704 EXPECT_EQ(written, 11);
705 ASSERT_STREQ(buff, "37777777777");
706 }
707
708 // Min Width Tests.
709
710 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%4o", 0701);
711 EXPECT_EQ(written, 4);
712 ASSERT_STREQ(buff, " 701");
713
714 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%2o", 0107);
715 EXPECT_EQ(written, 3);
716 ASSERT_STREQ(buff, "107");
717
718 // Precision Tests.
719
720 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%o", 0);
721 EXPECT_EQ(written, 1);
722 ASSERT_STREQ(buff, "0");
723
724 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0o", 0);
725 EXPECT_EQ(written, 0);
726 ASSERT_STREQ(buff, "");
727
728 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5o", 0153);
729 EXPECT_EQ(written, 5);
730 ASSERT_STREQ(buff, "00153");
731
732 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2o", 0135);
733 EXPECT_EQ(written, 3);
734 ASSERT_STREQ(buff, "135");
735
736 // Flag Tests.
737
738 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5o", 0246);
739 EXPECT_EQ(written, 5);
740 ASSERT_STREQ(buff, "246 ");
741
742 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#o", 0234);
743 EXPECT_EQ(written, 4);
744 ASSERT_STREQ(buff, "0234");
745
746 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#o", 0);
747 EXPECT_EQ(written, 1);
748 ASSERT_STREQ(buff, "0");
749
750 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%05o", 0470);
751 EXPECT_EQ(written, 5);
752 ASSERT_STREQ(buff, "00470");
753
754 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%0#6o", 0753);
755 EXPECT_EQ(written, 6);
756 ASSERT_STREQ(buff, "000753");
757
758 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-#6o", 0642);
759 EXPECT_EQ(written, 6);
760 ASSERT_STREQ(buff, "0642 ");
761
762 // Combined Tests.
763
764 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#-07o", 0703);
765 EXPECT_EQ(written, 7);
766 ASSERT_STREQ(buff, "0703 ");
767
768 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.o", 0);
769 EXPECT_EQ(written, 1);
770 ASSERT_STREQ(buff, "0");
771
772 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%7.5o", 0314);
773 EXPECT_EQ(written, 7);
774 ASSERT_STREQ(buff, " 00314");
775
776 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#9.5o", 0234);
777 EXPECT_EQ(written, 9);
778 ASSERT_STREQ(buff, " 00234");
779
780 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-7.5o", 0260);
781 EXPECT_EQ(written, 7);
782 ASSERT_STREQ(buff, "00260 ");
783
784 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5.4o", 010000);
785 EXPECT_EQ(written, 5);
786 ASSERT_STREQ(buff, "10000");
787
788 // Multiple Conversion Tests.
789
790 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10o %-#10o", 0456, 0123);
791 EXPECT_EQ(written, 21);
792 ASSERT_STREQ(buff, " 456 0123 ");
793
794 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.4o%#.4o", 075, 025);
795 EXPECT_EQ(written, 9);
796 ASSERT_STREQ(buff, "0075 0025");
797
798 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%04hho %#.5llo %-6.3zo", 256 + 077,
799 01000000000000ll, size_t(2));
800 EXPECT_EQ(written, 26);
801 ASSERT_STREQ(buff, "0077 01000000000000 002 ");
802}
803
804#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
805
806TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
807 ForceRoundingMode r(RoundingMode::Nearest);
808 double inf = LIBC_NAMESPACE::fputil::FPBits<double>::inf().get_val();
809 double nan = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
810 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 1.0);
811 ASSERT_STREQ_LEN(written, buff, "0x1p+0");
812
813 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%A", -1.0);
814 ASSERT_STREQ_LEN(written, buff, "-0X1P+0");
815
816 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", -0x1.abcdef12345p0);
817 ASSERT_STREQ_LEN(written, buff, "-0x1.abcdef12345p+0");
818
819 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%A", 0x1.abcdef12345p0);
820 ASSERT_STREQ_LEN(written, buff, "0X1.ABCDEF12345P+0");
821
822 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0.0);
823 ASSERT_STREQ_LEN(written, buff, "0x0p+0");
824
825 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 1.0e100);
826 ASSERT_STREQ_LEN(written, buff, "0x1.249ad2594c37dp+332");
827
828 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0.1);
829 ASSERT_STREQ_LEN(written, buff, "0x1.999999999999ap-4");
830
831 // Subnormal Tests.
832
833 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0x1.0p-1027);
834 ASSERT_STREQ_LEN(written, buff, "0x0.08p-1022");
835
836 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0x1.0p-1025);
837 ASSERT_STREQ_LEN(written, buff, "0x0.2p-1022");
838
839 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0x1.0p-1023);
840 ASSERT_STREQ_LEN(written, buff, "0x0.8p-1022");
841
842 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0x1.0p-1022);
843 ASSERT_STREQ_LEN(written, buff, "0x1p-1022");
844
845 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", 0x1.0p-1074);
846 ASSERT_STREQ_LEN(written, buff, "0x0.0000000000001p-1022");
847
848 // Inf/Nan Tests.
849
850 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", inf);
851 ASSERT_STREQ_LEN(written, buff, "inf");
852
853 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%A", -inf);
854 ASSERT_STREQ_LEN(written, buff, "-INF");
855
856 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%a", nan);
857 ASSERT_STREQ_LEN(written, buff, "nan");
858
859 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%A", -nan);
860 ASSERT_STREQ_LEN(written, buff, "-NAN");
861
862 // Length Modifier Tests.
863
864 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%La", 0.1L);
865#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
866 ASSERT_STREQ_LEN(written, buff, "0xc.ccccccccccccccdp-7");
867#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
868 ASSERT_STREQ_LEN(written, buff, "0x1.999999999999ap-4");
869#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
870 ASSERT_STREQ_LEN(written, buff, "0x1.999999999999999999999999999ap-4");
871#endif
872
873 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%La", 1.0e1000L);
874#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
875 ASSERT_STREQ_LEN(written, buff, "0xf.38db1f9dd3dac05p+3318");
876#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
877 ASSERT_STREQ_LEN(written, buff, "inf");
878#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
879 ASSERT_STREQ_LEN(written, buff, "0x1.e71b63f3ba7b580af1a52d2a7379p+3321");
880#endif
881
882 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%La", 1.0e-1000L);
883#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
884 ASSERT_STREQ_LEN(written, buff, "0x8.68a9188a89e1467p-3325");
885#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
886 ASSERT_STREQ_LEN(written, buff, "0x0p+0");
887#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
888 ASSERT_STREQ_LEN(written, buff, "0x1.0d152311513c28ce202627c06ec2p-3322");
889#endif
890
891 // Min Width Tests.
892
893 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15a", 1.0);
894 ASSERT_STREQ_LEN(written, buff, " 0x1p+0");
895
896 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15a", -1.0);
897 ASSERT_STREQ_LEN(written, buff, " -0x1p+0");
898
899 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15a", 1.0e10);
900 ASSERT_STREQ_LEN(written, buff, " 0x1.2a05f2p+33");
901
902 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15a", -1.0e10);
903 ASSERT_STREQ_LEN(written, buff, "-0x1.2a05f2p+33");
904
905 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10a", 1.0e10);
906 ASSERT_STREQ_LEN(written, buff, "0x1.2a05f2p+33");
907
908 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5a", inf);
909 ASSERT_STREQ_LEN(written, buff, " inf");
910
911 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5a", -nan);
912 ASSERT_STREQ_LEN(written, buff, " -nan");
913
914 // Precision Tests.
915
916 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 1.0);
917 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
918
919 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0.0);
920 ASSERT_STREQ_LEN(written, buff, "0x0.0p+0");
921
922 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0.1);
923 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
924
925 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.0fp0);
926 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
927
928 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.07p0);
929 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
930
931 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.08p0);
932 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
933
934 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.18p0);
935 ASSERT_STREQ_LEN(written, buff, "0x1.2p+0");
936
937 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.ffp0);
938 ASSERT_STREQ_LEN(written, buff, "0x2.0p+0");
939
940 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5a", 1.25);
941 ASSERT_STREQ_LEN(written, buff, "0x1.40000p+0");
942
943 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0a", 1.25);
944 ASSERT_STREQ_LEN(written, buff, "0x1p+0");
945
946 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0a", 1.75);
947 ASSERT_STREQ_LEN(written, buff, "0x2p+0");
948
949 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.0p-1023);
950 ASSERT_STREQ_LEN(written, buff, "0x0.8p-1022");
951
952 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.8p-1023);
953 ASSERT_STREQ_LEN(written, buff, "0x0.cp-1022");
954
955 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.0p-1024);
956 ASSERT_STREQ_LEN(written, buff, "0x0.4p-1022");
957
958 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0a", 0x1.0p-1023);
959 ASSERT_STREQ_LEN(written, buff, "0x0p-1022");
960
961 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0a", 0x1.8p-1023);
962 ASSERT_STREQ_LEN(written, buff, "0x1p-1022");
963
964 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0a", 0x1.0p-1024);
965 ASSERT_STREQ_LEN(written, buff, "0x0p-1022");
966
967 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2a", 0x1.0p-1027);
968 ASSERT_STREQ_LEN(written, buff, "0x0.08p-1022");
969
970 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.0p-1027);
971 ASSERT_STREQ_LEN(written, buff, "0x0.0p-1022");
972
973 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5a", 0.0);
974 ASSERT_STREQ_LEN(written, buff, "0x0.00000p+0");
975
976 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5a", 0x1.008p0);
977 ASSERT_STREQ_LEN(written, buff, "0x1.00800p+0");
978
979 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5a", 0x1.008p10);
980 ASSERT_STREQ_LEN(written, buff, "0x1.00800p+10");
981
982 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5a", nan);
983 ASSERT_STREQ_LEN(written, buff, "nan");
984
985 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%La", 0.0L);
986 ASSERT_STREQ_LEN(written, buff, "0x0p+0");
987
988 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1La", 0.1L);
989#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
990 ASSERT_STREQ_LEN(written, buff, "0xc.dp-7");
991#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
992 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
993#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
994 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
995#endif
996
997 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1La", 0xf.fffffffffffffffp16380L);
998#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
999 ASSERT_STREQ_LEN(written, buff, "0x1.0p+16384");
1000#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
1001 ASSERT_STREQ_LEN(written, buff, "inf");
1002#elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
1003 ASSERT_STREQ_LEN(written, buff, "0x2.0p+16383");
1004#endif
1005
1006 // Rounding Mode Tests.
1007
1008 if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
1009 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.08p0);
1010 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
1011
1012 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.18p0);
1013 ASSERT_STREQ_LEN(written, buff, "0x1.2p+0");
1014
1015 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.04p0);
1016 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
1017
1018 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.14p0);
1019 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1020
1021 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.08p0);
1022 ASSERT_STREQ_LEN(written, buff, "-0x1.0p+0");
1023
1024 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.18p0);
1025 ASSERT_STREQ_LEN(written, buff, "-0x1.2p+0");
1026
1027 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.04p0);
1028 ASSERT_STREQ_LEN(written, buff, "-0x1.0p+0");
1029
1030 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.14p0);
1031 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1032 }
1033
1034 if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
1035 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.08p0);
1036 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1037
1038 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.18p0);
1039 ASSERT_STREQ_LEN(written, buff, "0x1.2p+0");
1040
1041 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.04p0);
1042 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1043
1044 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.14p0);
1045 ASSERT_STREQ_LEN(written, buff, "0x1.2p+0");
1046
1047 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.08p0);
1048 ASSERT_STREQ_LEN(written, buff, "-0x1.0p+0");
1049
1050 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.18p0);
1051 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1052
1053 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.04p0);
1054 ASSERT_STREQ_LEN(written, buff, "-0x1.0p+0");
1055
1056 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.14p0);
1057 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1058 }
1059
1060 if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
1061 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.08p0);
1062 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
1063
1064 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.18p0);
1065 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1066
1067 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.04p0);
1068 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
1069
1070 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.14p0);
1071 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1072
1073 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.08p0);
1074 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1075
1076 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.18p0);
1077 ASSERT_STREQ_LEN(written, buff, "-0x1.2p+0");
1078
1079 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.04p0);
1080 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1081
1082 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.14p0);
1083 ASSERT_STREQ_LEN(written, buff, "-0x1.2p+0");
1084 }
1085
1086 if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
1087 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.08p0);
1088 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
1089
1090 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.18p0);
1091 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1092
1093 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.04p0);
1094 ASSERT_STREQ_LEN(written, buff, "0x1.0p+0");
1095
1096 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", 0x1.14p0);
1097 ASSERT_STREQ_LEN(written, buff, "0x1.1p+0");
1098
1099 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.08p0);
1100 ASSERT_STREQ_LEN(written, buff, "-0x1.0p+0");
1101
1102 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.18p0);
1103 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1104
1105 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.04p0);
1106 ASSERT_STREQ_LEN(written, buff, "-0x1.0p+0");
1107
1108 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1a", -0x1.14p0);
1109 ASSERT_STREQ_LEN(written, buff, "-0x1.1p+0");
1110 }
1111
1112 // Flag Tests.
1113
1114 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+a", nan);
1115 ASSERT_STREQ_LEN(written, buff, "+nan");
1116
1117 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% A", inf);
1118 ASSERT_STREQ_LEN(written, buff, " INF");
1119
1120 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5a", inf);
1121 ASSERT_STREQ_LEN(written, buff, "inf ");
1122
1123 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+-5A", nan);
1124 ASSERT_STREQ_LEN(written, buff, "+NAN ");
1125
1126 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+a", 1.0);
1127 ASSERT_STREQ_LEN(written, buff, "+0x1p+0");
1128
1129 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% a", 0.0);
1130 ASSERT_STREQ_LEN(written, buff, " 0x0p+0");
1131
1132 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10a", 1.5);
1133 ASSERT_STREQ_LEN(written, buff, "0x1.8p+0 ");
1134
1135 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#a", 1.0);
1136 ASSERT_STREQ_LEN(written, buff, "0x1.p+0");
1137
1138 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.0a", 1.5);
1139 ASSERT_STREQ_LEN(written, buff, "0x2.p+0");
1140
1141 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%010a", 1.5);
1142 ASSERT_STREQ_LEN(written, buff, "0x001.8p+0");
1143
1144 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+- #0a", 0.0);
1145 ASSERT_STREQ_LEN(written, buff, "+0x0.p+0");
1146
1147 // Combined Tests.
1148
1149 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%12.3a %-12.3A", 0.1, 256.0);
1150 ASSERT_STREQ_LEN(written, buff, " 0x1.99ap-4 0X1.000P+8 ");
1151
1152 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+-#12.3a % 012.3a", 0.1256, 1256.0);
1153 ASSERT_STREQ_LEN(written, buff, "+0x1.014p-3 0x1.3a0p+10");
1154
1155 // These tests check that the padding is properly calculated based on the
1156 // min_width field. Specifically, they check that the extra zeroes added by
1157 // the high precision are accounted for correctly.
1158 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%50.50a", 0x1.0p0);
1159 ASSERT_STREQ_LEN(written, buff,
1160 "0x1.00000000000000000000000000000000000000000000000000p+0");
1161
1162 // The difference with this test is that the formatted number is exactly 57
1163 // characters, so padding to 58 adds a space.
1164 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%58.50a", 0x1.0p0);
1165 ASSERT_STREQ_LEN(
1166 written, buff,
1167 " 0x1.00000000000000000000000000000000000000000000000000p+0");
1168}
1169
1170TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
1171 ForceRoundingMode r(RoundingMode::Nearest);
1172 double inf = LIBC_NAMESPACE::fputil::FPBits<double>::inf().get_val();
1173 double nan = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
1174 long double ld_inf =
1175 LIBC_NAMESPACE::fputil::FPBits<long double>::inf().get_val();
1176 long double ld_nan =
1177 LIBC_NAMESPACE::fputil::FPBits<long double>::quiet_nan().get_val();
1178
1179 char big_buff[10000]; // Used for long doubles and other extremely wide
1180 // numbers.
1181
1182 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 1.0);
1183 ASSERT_STREQ_LEN(written, buff, "1.000000");
1184
1185 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%F", -1.0);
1186 ASSERT_STREQ_LEN(written, buff, "-1.000000");
1187
1188 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", -1.234567);
1189 ASSERT_STREQ_LEN(written, buff, "-1.234567");
1190
1191 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 0.0);
1192 ASSERT_STREQ_LEN(written, buff, "0.000000");
1193
1194 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 1.5);
1195 ASSERT_STREQ_LEN(written, buff, "1.500000");
1196
1197 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 1e300);
1198 ASSERT_STREQ_LEN(
1199 written, buff,
1200 "100000000000000005250476025520442024870446858110815915491585411551180245"
1201 "798890819578637137508044786404370444383288387817694252323536043057564479"
1202 "218478670698284838720092657580373783023379478809005936895323497079994508"
1203 "111903896764088007465274278014249457925878882005684283811566947219638686"
1204 "5459400540160.000000");
1205
1206 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 0.1);
1207 ASSERT_STREQ_LEN(written, buff, "0.100000");
1208
1209 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 1234567890123456789.0);
1210 ASSERT_STREQ_LEN(written, buff, "1234567890123456768.000000");
1211
1212 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 9999999999999.99);
1213 ASSERT_STREQ_LEN(written, buff, "9999999999999.990234");
1214
1215 // Simple Subnormal Tests.
1216
1217 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 0x1.0p-1027);
1218 ASSERT_STREQ_LEN(written, buff, "0.000000");
1219
1220 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 0x1.0p-1074);
1221 ASSERT_STREQ_LEN(written, buff, "0.000000");
1222
1223 // Inf/Nan Tests.
1224
1225 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", inf);
1226 ASSERT_STREQ_LEN(written, buff, "inf");
1227
1228 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%F", -inf);
1229 ASSERT_STREQ_LEN(written, buff, "-INF");
1230
1231 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", nan);
1232 ASSERT_STREQ_LEN(written, buff, "nan");
1233
1234 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%F", -nan);
1235 ASSERT_STREQ_LEN(written, buff, "-NAN");
1236
1237 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lf", ld_inf);
1238 ASSERT_STREQ_LEN(written, buff, "inf");
1239
1240 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%LF", -ld_inf);
1241 ASSERT_STREQ_LEN(written, buff, "-INF");
1242
1243 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lf", ld_nan);
1244 ASSERT_STREQ_LEN(written, buff, "nan");
1245
1246// Some float128 systems (specifically the ones used for aarch64 buildbots)
1247// don't respect signs for long double NaNs.
1248#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80) || \
1249 defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
1250 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%LF", -ld_nan);
1251 ASSERT_STREQ_LEN(written, buff, "-NAN");
1252#endif
1253
1254 // Min Width Tests.
1255
1256 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15f", 1.0);
1257 ASSERT_STREQ_LEN(written, buff, " 1.000000");
1258
1259 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15f", -1.0);
1260 ASSERT_STREQ_LEN(written, buff, " -1.000000");
1261
1262 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15f", 1.0e5);
1263 ASSERT_STREQ_LEN(written, buff, " 100000.000000");
1264
1265 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15f", -1.0e5);
1266 ASSERT_STREQ_LEN(written, buff, " -100000.000000");
1267
1268 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10f", 1.0e5);
1269 ASSERT_STREQ_LEN(written, buff, "100000.000000");
1270
1271 // Precision Tests.
1272
1273 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.0);
1274 ASSERT_STREQ_LEN(written, buff, "1.0");
1275
1276 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 0.0);
1277 ASSERT_STREQ_LEN(written, buff, "0.0");
1278
1279 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0f", 0.0);
1280 ASSERT_STREQ_LEN(written, buff, "0");
1281
1282 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 0.1);
1283 ASSERT_STREQ_LEN(written, buff, "0.1");
1284
1285 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.09);
1286 ASSERT_STREQ_LEN(written, buff, "1.1");
1287
1288 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.04);
1289 ASSERT_STREQ_LEN(written, buff, "1.0");
1290
1291 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.19);
1292 ASSERT_STREQ_LEN(written, buff, "1.2");
1293
1294 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.99);
1295 ASSERT_STREQ_LEN(written, buff, "2.0");
1296
1297 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 9.99);
1298 ASSERT_STREQ_LEN(written, buff, "10.0");
1299
1300 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2f", 9999999999999.99);
1301 ASSERT_STREQ_LEN(written, buff, "9999999999999.99");
1302
1303 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 9999999999999.99);
1304 ASSERT_STREQ_LEN(written, buff, "10000000000000.0");
1305
1306 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5f", 1.25);
1307 ASSERT_STREQ_LEN(written, buff, "1.25000");
1308
1309 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0f", 1.25);
1310 ASSERT_STREQ_LEN(written, buff, "1");
1311
1312 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0f", 1.75);
1313 ASSERT_STREQ_LEN(written, buff, "2");
1314
1315 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.20f", 1.234e-10);
1316 ASSERT_STREQ_LEN(written, buff, "0.00000000012340000000");
1317
1318 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2f", -9.99);
1319 ASSERT_STREQ_LEN(written, buff, "-9.99");
1320
1321 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -9.99);
1322 ASSERT_STREQ_LEN(written, buff, "-10.0");
1323
1324 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5f", 0.0);
1325 ASSERT_STREQ_LEN(written, buff, "0.00000");
1326
1327 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5f", 1.008);
1328 ASSERT_STREQ_LEN(written, buff, "1.00800");
1329
1330 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5f", 1.008e3);
1331 ASSERT_STREQ_LEN(written, buff, "1008.00000");
1332
1333 // Found with the help of Fred Tydeman's tbin2dec test.
1334 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 0x1.1000000000006p+3);
1335 ASSERT_STREQ_LEN(written, buff, "8.5");
1336
1337 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0f", 0x1.1000000000006p+3);
1338 ASSERT_STREQ_LEN(written, buff, "9");
1339
1340 // Most of these tests are checking rounding behavior when the precision is
1341 // set. As an example, %.9f has a precision of 9, meaning it should be rounded
1342 // to 9 digits after the decimal point. In this case, that means that it
1343 // should be rounded up. Many of these tests have precisions divisible by 9
1344 // since when printing the floating point numbers are broken up into "blocks"
1345 // of 9 digits. They often also have a 5 after the end of what's printed,
1346 // since in round to nearest mode, that requires checking additional digits.
1347 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.9f", 1.9999999999999514);
1348 ASSERT_STREQ_LEN(written, buff, "2.000000000");
1349
1350 // The number continues after the literal because floating point numbers can't
1351 // represent every value. The printed value is the closest value a double can
1352 // represent, rounded to the requested precision.
1353 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.238f", 1.131959884853339E-72);
1354 ASSERT_STREQ_LEN(
1355 written, buff,
1356 "0."
1357 "000000000000000000000000000000000000000000000000000000000000000000000001"
1358 "131959884853339045938639911360973972585316399767392273697826861241937664"
1359 "824105639342441431495119762431744054912109728706985341609159156917030486"
1360 "5110665559768676757812");
1361
1362 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.36f", 9.9e-77);
1363 ASSERT_STREQ_LEN(written, buff, "0.000000000000000000000000000000000000");
1364
1365 written =
1366 LIBC_NAMESPACE::sprintf(buffer: big_buff, format: "%.1071f", 2.0226568751604562E-314);
1367 ASSERT_STREQ_LEN(
1368 written, big_buff,
1369 "0."
1370 "000000000000000000000000000000000000000000000000000000000000000000000000"
1371 "000000000000000000000000000000000000000000000000000000000000000000000000"
1372 "000000000000000000000000000000000000000000000000000000000000000000000000"
1373 "000000000000000000000000000000000000000000000000000000000000000000000000"
1374 "000000000000000000000000020226568751604561683387695750739190248658016786"
1375 "876938365740768295004457513021760887468117675879956193821375945376632621"
1376 "367998639317487303530427946024002091961988296562516210434394107910027236"
1377 "308233439098296717697919471698168200340836487924061502604112643734560622"
1378 "258525943451473162532620033398739382796482175564084902819878893430369431"
1379 "907237673154867595954110791891883281880339550955455702452422857027182100"
1380 "606009588295886640782228837851739241290179512817803196347460636150182981"
1381 "085084829941917048152725177119574542042352896161225179181967347829576272"
1382 "242480201291872969114441104973910102402751449901108484914924879541248714"
1383 "939096548775588293353689592872854495101242645279589976452453829724479805"
1384 "750016448075109469332839157162950982637994457036256790161132812");
1385
1386 // If no precision is specified it defaults to 6 for %f.
1387 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%f", 2325885.4901960781);
1388 ASSERT_STREQ_LEN(written, buff, "2325885.490196");
1389
1390 // Subnormal Precision Tests
1391
1392 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.310f", 0x1.0p-1022);
1393 ASSERT_STREQ_LEN(
1394 written, buff,
1395 "0."
1396 "000000000000000000000000000000000000000000000000000000000000000000000000"
1397 "000000000000000000000000000000000000000000000000000000000000000000000000"
1398 "000000000000000000000000000000000000000000000000000000000000000000000000"
1399 "000000000000000000000000000000000000000000000000000000000000000000000000"
1400 "0000000000000000000223");
1401
1402 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.310f", 0x1.0p-1023);
1403 ASSERT_STREQ_LEN(
1404 written, buff,
1405 "0."
1406 "000000000000000000000000000000000000000000000000000000000000000000000000"
1407 "000000000000000000000000000000000000000000000000000000000000000000000000"
1408 "000000000000000000000000000000000000000000000000000000000000000000000000"
1409 "000000000000000000000000000000000000000000000000000000000000000000000000"
1410 "0000000000000000000111");
1411
1412 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.315f", 9.99999e-310);
1413 ASSERT_STREQ_LEN(
1414 written, buff,
1415 "0."
1416 "000000000000000000000000000000000000000000000000000000000000000000000000"
1417 "000000000000000000000000000000000000000000000000000000000000000000000000"
1418 "000000000000000000000000000000000000000000000000000000000000000000000000"
1419 "000000000000000000000000000000000000000000000000000000000000000000000000"
1420 "000000000000000000000999999");
1421
1422 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.314f", 9.99999e-310);
1423 ASSERT_STREQ_LEN(
1424 written, buff,
1425 "0."
1426 "000000000000000000000000000000000000000000000000000000000000000000000000"
1427 "000000000000000000000000000000000000000000000000000000000000000000000000"
1428 "000000000000000000000000000000000000000000000000000000000000000000000000"
1429 "000000000000000000000000000000000000000000000000000000000000000000000000"
1430 "00000000000000000000100000");
1431
1432 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.330f", 0x1.0p-1074);
1433 ASSERT_STREQ_LEN(
1434 written, buff,
1435 "0."
1436 "000000000000000000000000000000000000000000000000000000000000000000000000"
1437 "000000000000000000000000000000000000000000000000000000000000000000000000"
1438 "000000000000000000000000000000000000000000000000000000000000000000000000"
1439 "000000000000000000000000000000000000000000000000000000000000000000000000"
1440 "000000000000000000000000000000000004940656");
1441
1442 /*
1443 written = LIBC_NAMESPACE::sprintf(buff, "%.1La", 0.1L);
1444 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
1445 ASSERT_STREQ_LEN(written, buff, "0xc.dp-7");
1446 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
1447 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
1448 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
1449 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
1450 #endif
1451
1452 written = LIBC_NAMESPACE::sprintf(buff, "%.1La",
1453 0xf.fffffffffffffffp16380L); #if
1454 defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80) ASSERT_STREQ_LEN(written, buff,
1455 "0x1.0p+16384"); #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
1456 ASSERT_STREQ_LEN(written, buff, "inf");
1457 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
1458 ASSERT_STREQ_LEN(written, buff, "0x2.0p+16383");
1459 #endif
1460 */
1461
1462 // Rounding Mode Tests.
1463
1464 if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
1465 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.75);
1466 ASSERT_STREQ_LEN(written, buff, "1.8");
1467
1468 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.25);
1469 ASSERT_STREQ_LEN(written, buff, "1.2");
1470
1471 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.125);
1472 ASSERT_STREQ_LEN(written, buff, "1.1");
1473
1474 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.625);
1475 ASSERT_STREQ_LEN(written, buff, "1.6");
1476
1477 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.375);
1478 ASSERT_STREQ_LEN(written, buff, "1.4");
1479
1480 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.875);
1481 ASSERT_STREQ_LEN(written, buff, "1.9");
1482
1483 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.75);
1484 ASSERT_STREQ_LEN(written, buff, "-1.8");
1485
1486 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.25);
1487 ASSERT_STREQ_LEN(written, buff, "-1.2");
1488
1489 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.125);
1490 ASSERT_STREQ_LEN(written, buff, "-1.1");
1491
1492 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.625);
1493 ASSERT_STREQ_LEN(written, buff, "-1.6");
1494
1495 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.375);
1496 ASSERT_STREQ_LEN(written, buff, "-1.4");
1497
1498 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.875);
1499 ASSERT_STREQ_LEN(written, buff, "-1.9");
1500 }
1501
1502 if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
1503 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.75);
1504 ASSERT_STREQ_LEN(written, buff, "1.8");
1505
1506 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.25);
1507 ASSERT_STREQ_LEN(written, buff, "1.3");
1508
1509 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.125);
1510 ASSERT_STREQ_LEN(written, buff, "1.2");
1511
1512 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.625);
1513 ASSERT_STREQ_LEN(written, buff, "1.7");
1514
1515 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.375);
1516 ASSERT_STREQ_LEN(written, buff, "1.4");
1517
1518 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.875);
1519 ASSERT_STREQ_LEN(written, buff, "1.9");
1520
1521 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.75);
1522 ASSERT_STREQ_LEN(written, buff, "-1.7");
1523
1524 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.25);
1525 ASSERT_STREQ_LEN(written, buff, "-1.2");
1526
1527 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.125);
1528 ASSERT_STREQ_LEN(written, buff, "-1.1");
1529
1530 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.625);
1531 ASSERT_STREQ_LEN(written, buff, "-1.6");
1532
1533 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.375);
1534 ASSERT_STREQ_LEN(written, buff, "-1.3");
1535
1536 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.875);
1537 ASSERT_STREQ_LEN(written, buff, "-1.8");
1538 }
1539
1540 if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
1541 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.75);
1542 ASSERT_STREQ_LEN(written, buff, "1.7");
1543
1544 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.25);
1545 ASSERT_STREQ_LEN(written, buff, "1.2");
1546
1547 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.125);
1548 ASSERT_STREQ_LEN(written, buff, "1.1");
1549
1550 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.625);
1551 ASSERT_STREQ_LEN(written, buff, "1.6");
1552
1553 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.375);
1554 ASSERT_STREQ_LEN(written, buff, "1.3");
1555
1556 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.875);
1557 ASSERT_STREQ_LEN(written, buff, "1.8");
1558
1559 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.75);
1560 ASSERT_STREQ_LEN(written, buff, "-1.8");
1561
1562 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.25);
1563 ASSERT_STREQ_LEN(written, buff, "-1.3");
1564
1565 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.125);
1566 ASSERT_STREQ_LEN(written, buff, "-1.2");
1567
1568 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.625);
1569 ASSERT_STREQ_LEN(written, buff, "-1.7");
1570
1571 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.375);
1572 ASSERT_STREQ_LEN(written, buff, "-1.4");
1573
1574 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.875);
1575 ASSERT_STREQ_LEN(written, buff, "-1.9");
1576 }
1577
1578 if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
1579 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.75);
1580 ASSERT_STREQ_LEN(written, buff, "1.7");
1581
1582 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.25);
1583 ASSERT_STREQ_LEN(written, buff, "1.2");
1584
1585 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.125);
1586 ASSERT_STREQ_LEN(written, buff, "1.1");
1587
1588 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.625);
1589 ASSERT_STREQ_LEN(written, buff, "1.6");
1590
1591 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.375);
1592 ASSERT_STREQ_LEN(written, buff, "1.3");
1593
1594 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", 1.875);
1595 ASSERT_STREQ_LEN(written, buff, "1.8");
1596
1597 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.75);
1598 ASSERT_STREQ_LEN(written, buff, "-1.7");
1599
1600 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.25);
1601 ASSERT_STREQ_LEN(written, buff, "-1.2");
1602
1603 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.125);
1604 ASSERT_STREQ_LEN(written, buff, "-1.1");
1605
1606 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.625);
1607 ASSERT_STREQ_LEN(written, buff, "-1.6");
1608
1609 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.375);
1610 ASSERT_STREQ_LEN(written, buff, "-1.3");
1611
1612 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1f", -1.875);
1613 ASSERT_STREQ_LEN(written, buff, "-1.8");
1614 }
1615
1616 // Flag Tests.
1617 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+f", 1.0);
1618 ASSERT_STREQ_LEN(written, buff, "+1.000000");
1619
1620 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+f", -1.0);
1621 ASSERT_STREQ_LEN(written, buff, "-1.000000");
1622
1623 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% f", 1.0);
1624 ASSERT_STREQ_LEN(written, buff, " 1.000000");
1625
1626 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% f", -1.0);
1627 ASSERT_STREQ_LEN(written, buff, "-1.000000");
1628
1629 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10f", 1.5);
1630 ASSERT_STREQ_LEN(written, buff, "1.500000 ");
1631
1632 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.f", 1.0);
1633 ASSERT_STREQ_LEN(written, buff, "1.");
1634
1635 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.0f", 1.5);
1636 ASSERT_STREQ_LEN(written, buff, "2.");
1637
1638 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%010f", 1.5);
1639 ASSERT_STREQ_LEN(written, buff, "001.500000");
1640
1641 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%010f", -1.5);
1642 ASSERT_STREQ_LEN(written, buff, "-01.500000");
1643
1644 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+- #0f", 0.0);
1645 ASSERT_STREQ_LEN(written, buff, "+0.000000");
1646
1647 // Combined Tests.
1648
1649 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.2f", 9.99);
1650 ASSERT_STREQ_LEN(written, buff, " 9.99");
1651
1652 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%5.1f", 9.99);
1653 ASSERT_STREQ_LEN(written, buff, " 10.0");
1654
1655 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.2f", 9.99);
1656 ASSERT_STREQ_LEN(written, buff, "9.99 ");
1657
1658 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.1f", 9.99);
1659 ASSERT_STREQ_LEN(written, buff, "10.0 ");
1660
1661 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-5.1f", 1.0e-50);
1662 ASSERT_STREQ_LEN(written, buff, "0.0 ");
1663
1664 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%30f", 1234567890123456789.0);
1665 ASSERT_STREQ_LEN(written, buff, " 1234567890123456768.000000");
1666
1667 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-30f", 1234567890123456789.0);
1668 ASSERT_STREQ_LEN(written, buff, "1234567890123456768.000000 ");
1669
1670 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%20.2f", 9999999999999.99);
1671 ASSERT_STREQ_LEN(written, buff, " 9999999999999.99");
1672
1673 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%20.1f", 9999999999999.99);
1674 ASSERT_STREQ_LEN(written, buff, " 10000000000000.0");
1675
1676 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%12.3f %-12.3f", 0.1, 256.0);
1677 ASSERT_STREQ_LEN(written, buff, " 0.100 256.000 ");
1678
1679 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+-#12.3f % 012.3f", 0.1256, 1256.0);
1680 ASSERT_STREQ_LEN(written, buff, "+0.126 0001256.000");
1681}
1682
1683// The long double tests are separated so that their performance can be directly
1684// measured.
1685TEST_F(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) {
1686 ForceRoundingMode r(RoundingMode::Nearest);
1687
1688 char big_buff[10000]; // Used for long doubles and other extremely wide
1689 // numbers.
1690
1691 // Length Modifier Tests.
1692
1693 // TODO(michaelrj): Add tests for LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64 and 128
1694 // bit long double systems.
1695 // TODO(michaelrj): Fix the tests to only depend on the digits the long double
1696 // is accurate for.
1697
1698 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lf", 1.0L);
1699 ASSERT_STREQ_LEN(written, buff, "1.000000");
1700
1701 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.Lf", -2.5L);
1702 ASSERT_STREQ_LEN(written, buff, "-2");
1703
1704#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
1705
1706 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lf", 1e100L);
1707 ASSERT_STREQ_LEN(written, buff,
1708 "99999999999999999996693535322073426194986990198284960792713"
1709 "91541752018669482644324418977840117055488.000000");
1710
1711 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lf", 0xd.96ed1192687859ap-24L);
1712 ASSERT_STREQ_LEN(written, buff, "0.000001");
1713
1714 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lf", 10000000000000000.25L);
1715 ASSERT_STREQ_LEN(written, buff, "10000000000000000.250000");
1716
1717 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.510Lf", 0x8p-503L);
1718 ASSERT_STREQ_LEN(
1719 written, buff,
1720 "0."
1721 "000000000000000000000000000000000000000000000000000000000000000000000000"
1722 "000000000000000000000000000000000000000000000000000000000000000000000000"
1723 "000000305493636349960468205197939321361769978940274057232666389361390928"
1724 "129162652472045770185723510801522825687515269359046715531785342780428396"
1725 "973513311420091788963072442053377285222203558881953188370081650866793017"
1726 "948791366338993705251636497892270212003524508209121908744820211960149463"
1727 "721109340307985507678283651836204093399373959982767701148986816406250000"
1728 "000000");
1729
1730 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.500Lf", -4327677766926336.0L);
1731 ASSERT_STREQ_LEN(
1732 written, buff,
1733 "-4327677766926336."
1734 "000000000000000000000000000000000000000000000000000000000000000000000000"
1735 "000000000000000000000000000000000000000000000000000000000000000000000000"
1736 "000000000000000000000000000000000000000000000000000000000000000000000000"
1737 "000000000000000000000000000000000000000000000000000000000000000000000000"
1738 "000000000000000000000000000000000000000000000000000000000000000000000000"
1739 "000000000000000000000000000000000000000000000000000000000000000000000000"
1740 "00000000000000000000000000000000000000000000000000000000000000000000");
1741
1742 written = LIBC_NAMESPACE::sprintf(buffer: big_buff, format: "%Lf", 1e1000L);
1743 ASSERT_STREQ_LEN(
1744 written, big_buff,
1745 "999999999999999999973107317669562353428234857594552594925899449376328728"
1746 "202461036775511405481186963193066642191664822065529414252060696836533522"
1747 "387143501724276282079456797058697369889056407118642873669166717313763499"
1748 "277025985141177344925615052465165938514140943010597323750202561187880136"
1749 "174810574553749194614479541820148407958204853833697063267336294787191005"
1750 "628217462261955103745349844675732989944229689277833828743730290177882029"
1751 "042613704915899149603539993716885598351951895974316347947147507970269673"
1752 "097709017164643598452451201499004104341931127294141495501309305995449742"
1753 "273419524803597130450457553871345958049837885085168840317195672271085085"
1754 "950520957945970913451088104971436093671776829538796532762184174216651692"
1755 "640931965387852083906784898823494867055070322768919156031682291829761007"
1756 "101483799978382119231551218582499361996919560548090784230386907125151658"
1757 "086767207295524036170321059257942621398084478974000973622199163292708506"
1758 "2431457550909271560663602154947063707982236377366647567795879936."
1759 "000000");
1760
1761 written = LIBC_NAMESPACE::sprintf(buffer: big_buff, format: "%Lf", 1e4900L);
1762 ASSERT_STREQ_LEN(
1763 written, big_buff,
1764 "100000000000000000002708312230690349833224052504078834346502930111959028"
1765 "517260692666637048230414374897655201843766090626319971729765251179632020"
1766 "313912652522792711197087872698264530532442630109549129842736280196919130"
1767 "242615101228133188193853826983121366159061148351354364472807590931218045"
1768 "387490935930967150336231085015126034696883068553581691802388371635128003"
1769 "615577299166097675723780877126495909902479233742826339471026068806070433"
1770 "075629449530819183550315434973800271862658869400009022028602967197463980"
1771 "126881829804282202449930132940824361207087494829502385835258094836304011"
1772 "876250359661206802659650567866176246063987902366800491980400341950657151"
1773 "370854446585517805253310195469184699955519312761482572080479702840420595"
1774 "377369017651259376039167277822106875560385309101650382998482652792335482"
1775 "865443482342801545877390859444282105890147577937366066315975231014810320"
1776 "888482059656248277607763361589359794524314002443575149260630989130103550"
1777 "443177966380769341050735632338583912575890190136462629316287947355057647"
1778 "111088565611192544631519843618778618820046304429723908484879583579178075"
1779 "456701368334212923379389029311286386996015804122917416008806233549005183"
1780 "152461084266176543129004016414959261473645240454289630182591200574019087"
1781 "358223489767381636349719510715487188747217311279465814538495924567014916"
1782 "238565628036285599497236493491668884212847699052761266207598941300449276"
1783 "447201387520841811835583254242213093566548778954711633721122784159793843"
1784 "766802019309395771984693609426401362800013936338891483689127845928572536"
1785 "790651156184721483511507878883282891696900630100211914227950790472211403"
1786 "392549466062537498185758854079775888444518306635752468713312357556380082"
1787 "275500658967283696421824354930077523691855699312544373220921962817907078"
1788 "445538421941800259027487429330768616490865438859612697367766323925013940"
1789 "918384858952407145253573823848733994146335416209309233074165707437420756"
1790 "438833918763109580759409985573826485055208965115587885226774453455112406"
1791 "581351429640282227888764449360534584421929291565334894907337572527922691"
1792 "473242328379737396430908523008687037407295838014450772162091496534584696"
1793 "605157436893236842602956298545594095307060870397506421786236892553632163"
1794 "491468601982681381011940409602294892199042638682530687578982576819839451"
1795 "907594697546439533559153604700750696252355362322662219852740143212566818"
1796 "745528402265116534684566273868361460640280523251242059850044328669692159"
1797 "629900374576027104298177006629276014371540945261309319363704125592775129"
1798 "543526908667388673739382491147471395192495459318806593271282662311169392"
1799 "196897003517840025298267505925987901751541005546610016067658227181318892"
1800 "914686508281007582655667597441346214499847364272258631922040641860333431"
1801 "409838623713258383681350233064164940590695888300919626215847587544298023"
1802 "636416943680102708406086295669759876682046839368574433996997648445207805"
1803 "615784339667691231286807666753972942872019850432610318031627872612657513"
1804 "588188267160616660825719678199868371370527508463011236193719286066916786"
1805 "169956541349011494927225747024994619057884118692213564790598702879596058"
1806 "672338334720925179141906809470606964896245458600635183723159228561689808"
1807 "246141482736625197373238197777325580142168245885279594913851700941789475"
1808 "252421784152262567254611571822468808675893407728003047921107885664474662"
1809 "930921581384003950729114103689170603748380178682003976896397305836815761"
1810 "717676338115866650889936516794601457549097578905329423919798362140648664"
1811 "569177147076571576101649257502509463877402424847669830852345415301684820"
1812 "395813946416649808062227494112874521812750160935760825922220707178083076"
1813 "380203450993589198835885505461509442443773367592842795410339065860781804"
1814 "024975272228687688301824830333940416256885455008512598774611538878683158"
1815 "183931461086893832255176926531299425504132104728730288984598001187854507"
1816 "900417184206801359847651992484444933900133130832052346600926424167009902"
1817 "829803553087005800387704758687923428053612864451456596148162238935900033"
1818 "917094683141205188616000211702577553792389670853917118547527592495253773"
1819 "028135298405566315903922235989614934474805789300370437580494193066066314"
1820 "056627605207631392651010580925826419831250810981343093764403877594495896"
1821 "516881097415880926429607388979497471571321217205535961262051641426436441"
1822 "668989765107456413733909427384182109285933511623871034309722437967253289"
1823 "084018145083721513211807496392673952789642893241520398827805325610653506"
1824 "029060153153064455898648607959013571280930834475689835845791849456112104"
1825 "462337569019001580859906425911782967213265389744605395555069797947978230"
1826 "708108432086217134763779632408473684293543722127232658767439906910370146"
1827 "716836295909075482355827087389127370874842532825987593970846704144140471"
1828 "956027276735614286138656432085771988513977140957180090146798065497158947"
1829 "229765733489703157617307078835099906185890777007500964162371428641176460"
1830 "739074789794941408428328217107759915202650066155868439585510978709442590"
1831 "231934194956788626761834746430104077432547436359522462253411168467463134"
1832 "24896.000000");
1833
1834 written =
1835 LIBC_NAMESPACE::sprintf(buffer: big_buff, format: "%Lf", 0xf.fffffffffffffffp+16380L);
1836 ASSERT_STREQ_LEN(
1837 written, big_buff,
1838 "118973149535723176502126385303097020516906332229462420044032373389173700"
1839 "552297072261641029033652888285354569780749557731442744315367028843419812"
1840 "557385374367867359320070697326320191591828296152436552951064679108661431"
1841 "179063216977883889613478656060039914875343321145491116008867984515486651"
1842 "285234014977303760000912547939396622315138362241783854274391783813871780"
1843 "588948754057516822634765923557697480511372564902088485522249479139937758"
1844 "502601177354918009979622602685950855888360815984690023564513234659447638"
1845 "493985927645628457966177293040780660922910271504608538808795932778162298"
1846 "682754783076808004015069494230341172895777710033571401055977524212405734"
1847 "700738625166011082837911962300846927720096515350020847447079244384854591"
1848 "288672300061908512647211195136146752763351956292759795725027800298079590"
1849 "419313960302147099703527646744553092202267965628099149823208332964124103"
1850 "850923918473478612192169721054348428704835340811304257300221642134891734"
1851 "717423480071488075100206439051723424765600472176809648610799494341570347"
1852 "632064355862420744350442438056613601760883747816538902780957697597728686"
1853 "007148702828795556714140463261583262360276289631617397848425448686060994"
1854 "827086796804807870251185893083854658422304090880599629459458620190376604"
1855 "844679092600222541053077590106576067134720012584640695703025713896098375"
1856 "799892695455305236856075868317922311363951946885088077187210470520395758"
1857 "748001314313144425494391994017575316933939236688185618912993172910425292"
1858 "123683515992232205099800167710278403536014082929639811512287776813570604"
1859 "578934353545169653956125404884644716978689321167108722908808277835051822"
1860 "885764606221873970285165508372099234948333443522898475123275372663606621"
1861 "390228126470623407535207172405866507951821730346378263135339370677490195"
1862 "019784169044182473806316282858685774143258116536404021840272491339332094"
1863 "921949842244273042701987304453662035026238695780468200360144729199712309"
1864 "553005720614186697485284685618651483271597448120312194675168637934309618"
1865 "961510733006555242148519520176285859509105183947250286387163249416761380"
1866 "499631979144187025430270675849519200883791516940158174004671147787720145"
1867 "964446117520405945350476472180797576111172084627363927960033967047003761"
1868 "337450955318415007379641260504792325166135484129188421134082301547330475"
1869 "406707281876350361733290800595189632520707167390454777712968226520622565"
1870 "143991937680440029238090311243791261477625596469422198137514696707944687"
1871 "035800439250765945161837981185939204954403611491531078225107269148697980"
1872 "924094677214272701240437718740921675661363493890045123235166814608932240"
1873 "069799317601780533819184998193300841098599393876029260139091141452600372"
1874 "028487213241195542428210183120421610446740462163533690058366460659115629"
1875 "876474552506814500393294140413149540067760295100596225302282300363147382"
1876 "468105964844244132486457313743759509641616804802412935187620466813563687"
1877 "753281467553879887177183651289394719533506188500326760735438867336800207"
1878 "438784965701457609034985757124304510203873049485425670247933932280911052"
1879 "604153852899484920399109194612991249163328991799809438033787952209313146"
1880 "694614970593966415237594928589096048991612194498998638483702248667224914"
1881 "892467841020618336462741696957630763248023558797524525373703543388296086"
1882 "275342774001633343405508353704850737454481975472222897528108302089868263"
1883 "302028525992308416805453968791141829762998896457648276528750456285492426"
1884 "516521775079951625966922911497778896235667095662713848201819134832168799"
1885 "586365263762097828507009933729439678463987902491451422274252700636394232"
1886 "799848397673998715441855420156224415492665301451550468548925862027608576"
1887 "183712976335876121538256512963353814166394951655600026415918655485005705"
1888 "261143195291991880795452239464962763563017858089669222640623538289853586"
1889 "759599064700838568712381032959192649484625076899225841930548076362021508"
1890 "902214922052806984201835084058693849381549890944546197789302911357651677"
1891 "540623227829831403347327660395223160342282471752818181884430488092132193"
1892 "355086987339586127607367086665237555567580317149010847732009642431878007"
1893 "000879734603290627894355374356444885190719161645514115576193939969076741"
1894 "515640282654366402676009508752394550734155613586793306603174472092444651"
1895 "353236664764973540085196704077110364053815007348689179836404957060618953"
1896 "500508984091382686953509006678332447257871219660441528492484004185093281"
1897 "190896363417573989716659600075948780061916409485433875852065711654107226"
1898 "099628815012314437794400874930194474433078438899570184271000480830501217"
1899 "712356062289507626904285680004771889315808935851559386317665294808903126"
1900 "774702966254511086154895839508779675546413794489596052797520987481383976"
1901 "257859210575628440175934932416214833956535018919681138909184379573470326"
1902 "940634289008780584694035245347939808067427323629788710086717580253156130"
1903 "235606487870925986528841635097252953709111431720488774740553905400942537"
1904 "542411931794417513706468964386151771884986701034153254238591108962471088"
1905 "538580868883777725864856414593426212108664758848926003176234596076950884"
1906 "9149662444156604419552086811989770240.000000");
1907
1908 written = LIBC_NAMESPACE::sprintf(buffer: big_buff, format: "%.10Lf", 1e-10L);
1909 ASSERT_STREQ_LEN(written, big_buff, "0.0000000001");
1910
1911 written = LIBC_NAMESPACE::sprintf(buffer: big_buff, format: "%.7500Lf", 1e-4900L);
1912 ASSERT_STREQ_LEN(
1913 written, big_buff,
1914 "0."
1915 "000000000000000000000000000000000000000000000000000000000000000000000000"
1916 "000000000000000000000000000000000000000000000000000000000000000000000000"
1917 "000000000000000000000000000000000000000000000000000000000000000000000000"
1918 "000000000000000000000000000000000000000000000000000000000000000000000000"
1919 "000000000000000000000000000000000000000000000000000000000000000000000000"
1920 "000000000000000000000000000000000000000000000000000000000000000000000000"
1921 "000000000000000000000000000000000000000000000000000000000000000000000000"
1922 "000000000000000000000000000000000000000000000000000000000000000000000000"
1923 "000000000000000000000000000000000000000000000000000000000000000000000000"
1924 "000000000000000000000000000000000000000000000000000000000000000000000000"
1925 "000000000000000000000000000000000000000000000000000000000000000000000000"
1926 "000000000000000000000000000000000000000000000000000000000000000000000000"
1927 "000000000000000000000000000000000000000000000000000000000000000000000000"
1928 "000000000000000000000000000000000000000000000000000000000000000000000000"
1929 "000000000000000000000000000000000000000000000000000000000000000000000000"
1930 "000000000000000000000000000000000000000000000000000000000000000000000000"
1931 "000000000000000000000000000000000000000000000000000000000000000000000000"
1932 "000000000000000000000000000000000000000000000000000000000000000000000000"
1933 "000000000000000000000000000000000000000000000000000000000000000000000000"
1934 "000000000000000000000000000000000000000000000000000000000000000000000000"
1935 "000000000000000000000000000000000000000000000000000000000000000000000000"
1936 "000000000000000000000000000000000000000000000000000000000000000000000000"
1937 "000000000000000000000000000000000000000000000000000000000000000000000000"
1938 "000000000000000000000000000000000000000000000000000000000000000000000000"
1939 "000000000000000000000000000000000000000000000000000000000000000000000000"
1940 "000000000000000000000000000000000000000000000000000000000000000000000000"
1941 "000000000000000000000000000000000000000000000000000000000000000000000000"
1942 "000000000000000000000000000000000000000000000000000000000000000000000000"
1943 "000000000000000000000000000000000000000000000000000000000000000000000000"
1944 "000000000000000000000000000000000000000000000000000000000000000000000000"
1945 "000000000000000000000000000000000000000000000000000000000000000000000000"
1946 "000000000000000000000000000000000000000000000000000000000000000000000000"
1947 "000000000000000000000000000000000000000000000000000000000000000000000000"
1948 "000000000000000000000000000000000000000000000000000000000000000000000000"
1949 "000000000000000000000000000000000000000000000000000000000000000000000000"
1950 "000000000000000000000000000000000000000000000000000000000000000000000000"
1951 "000000000000000000000000000000000000000000000000000000000000000000000000"
1952 "000000000000000000000000000000000000000000000000000000000000000000000000"
1953 "000000000000000000000000000000000000000000000000000000000000000000000000"
1954 "000000000000000000000000000000000000000000000000000000000000000000000000"
1955 "000000000000000000000000000000000000000000000000000000000000000000000000"
1956 "000000000000000000000000000000000000000000000000000000000000000000000000"
1957 "000000000000000000000000000000000000000000000000000000000000000000000000"
1958 "000000000000000000000000000000000000000000000000000000000000000000000000"
1959 "000000000000000000000000000000000000000000000000000000000000000000000000"
1960 "000000000000000000000000000000000000000000000000000000000000000000000000"
1961 "000000000000000000000000000000000000000000000000000000000000000000000000"
1962 "000000000000000000000000000000000000000000000000000000000000000000000000"
1963 "000000000000000000000000000000000000000000000000000000000000000000000000"
1964 "000000000000000000000000000000000000000000000000000000000000000000000000"
1965 "000000000000000000000000000000000000000000000000000000000000000000000000"
1966 "000000000000000000000000000000000000000000000000000000000000000000000000"
1967 "000000000000000000000000000000000000000000000000000000000000000000000000"
1968 "000000000000000000000000000000000000000000000000000000000000000000000000"
1969 "000000000000000000000000000000000000000000000000000000000000000000000000"
1970 "000000000000000000000000000000000000000000000000000000000000000000000000"
1971 "000000000000000000000000000000000000000000000000000000000000000000000000"
1972 "000000000000000000000000000000000000000000000000000000000000000000000000"
1973 "000000000000000000000000000000000000000000000000000000000000000000000000"
1974 "000000000000000000000000000000000000000000000000000000000000000000000000"
1975 "000000000000000000000000000000000000000000000000000000000000000000000000"
1976 "000000000000000000000000000000000000000000000000000000000000000000000000"
1977 "000000000000000000000000000000000000000000000000000000000000000000000000"
1978 "000000000000000000000000000000000000000000000000000000000000000000000000"
1979 "000000000000000000000000000000000000000000000000000000000000000000000000"
1980 "000000000000000000000000000000000000000000000000000000000000000000000000"
1981 "000000000000000000000000000000000000000000000000000000000000000000000000"
1982 "000000000000000000000000000000000000000000000000000000000000000000000000"
1983 "000099999999999999999996962764452956071352139203248614920751610856665084"
1984 "549214352477698417183862158583009348897567779527408501588132175167211539"
1985 "462139941448204886585901454195352527238724272760638086779284030512649793"
1986 "039219351187928723378036480041948464946018272171365770411701020666925613"
1987 "422460317465324758217878522666789603627480490870456508256359180089236338"
1988 "765625231186929290294207420828927406735690318849109129700396907705735097"
1989 "663944722727287361650042373203763784830198232253311807069225650324196304"
1990 "532045014970637489181357566354288111205943347410488298480279857453705249"
1991 "232862728556860184412369114663536200895729846877559808001004454634804626"
1992 "541455540260282018142615835686583304903486353937549394736905011798466731"
1993 "536563240053860118551127061960208467764243724656897127545613968909523389"
1994 "577188368809623987105800147797280462974804046545425080530020901531407223"
1995 "191237123282274818236437397994019915368657474589800678444589412286037789"
1996 "891525464936023205313685584525510094270344601331453730179416773626565262"
1997 "480345858564672442896904520146956686863172737711483866766404977719744767"
1998 "834324844875237277613991088218774564658513875732403456058414595576806383"
1999 "115554713240005982141397577420073082470139244845624915873825746771661332"
2000 "098677966580506186966978746832443976821987300902957597498388211921362869"
2001 "017846215557612829071692275292036211064515305528052919611691470945774714"
2002 "135516559501572279732350629089770249554808690411603894492333360300589658"
2003 "470898965370892774715815089075170720164713889237058574941489766701880158"
2004 "060081295483989540170337129032188818293132770882381428397119039835946745"
2005 "549356649433406617266370644136291924838857814675939156677910783740103207"
2006 "523299367093130816446415259371931925208362367989095199399211644084543790"
2007 "110432339056231037520216864358899218874658268610955002763260912337688947"
2008 "822453100821038299301092582962825965939081817836419126254832772002214908"
2009 "085575905761843610944187009818156363893015929300295112598059949496854566"
2010 "638748010633726861510500653821408135845840123073754133549077708843800674"
2011 "328440913743105608636458354618912183716456158809545183074062249922212944"
2012 "249667793845728355381309084891765979111348980470647082269921872595470473"
2013 "719354467594516320911964549508538492057120740224559944452120552719041944"
2014 "961475548547884309626382512432626380881023756568143060204097921571153170"
2015 "723817845809196253498326358439807445210362177680590181657555380795450462"
2016 "223805222580359379367452693270553602179122419370586308101820559214330382"
2017 "570449525088342437216896462077260223998756027453411520977536701491759878"
2018 "422771447006016890777855573925295187921971811871399320142563330377888532"
2019 "179817332113");
2020#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
2021}
2022
2023TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) {
2024 ForceRoundingMode r(RoundingMode::Nearest);
2025 double inf = LIBC_NAMESPACE::fputil::FPBits<double>::inf().get_val();
2026 double nan = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
2027
2028 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 1.0);
2029 ASSERT_STREQ_LEN(written, buff, "1.000000e+00");
2030
2031 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%E", -1.0);
2032 ASSERT_STREQ_LEN(written, buff, "-1.000000E+00");
2033
2034 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", -1.234567);
2035 ASSERT_STREQ_LEN(written, buff, "-1.234567e+00");
2036
2037 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0.0);
2038 ASSERT_STREQ_LEN(written, buff, "0.000000e+00");
2039
2040 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 1.5);
2041 ASSERT_STREQ_LEN(written, buff, "1.500000e+00");
2042
2043 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 1e300);
2044 ASSERT_STREQ_LEN(written, buff, "1.000000e+300");
2045
2046 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0.1);
2047 ASSERT_STREQ_LEN(written, buff, "1.000000e-01");
2048
2049 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0.001);
2050 ASSERT_STREQ_LEN(written, buff, "1.000000e-03");
2051
2052 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0.00001);
2053 ASSERT_STREQ_LEN(written, buff, "1.000000e-05");
2054
2055 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0.0000001);
2056 ASSERT_STREQ_LEN(written, buff, "1.000000e-07");
2057
2058 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0.000000001);
2059 ASSERT_STREQ_LEN(written, buff, "1.000000e-09");
2060
2061 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 1.0e-20);
2062 ASSERT_STREQ_LEN(written, buff, "1.000000e-20");
2063
2064 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 1234567890123456789.0);
2065 ASSERT_STREQ_LEN(written, buff, "1.234568e+18");
2066
2067 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 9999999000000.00);
2068 ASSERT_STREQ_LEN(written, buff, "9.999999e+12");
2069
2070 // Simple Subnormal Tests.
2071
2072 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0x1.0p-1027);
2073 ASSERT_STREQ_LEN(written, buff, "6.953356e-310");
2074
2075 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", 0x1.0p-1074);
2076 ASSERT_STREQ_LEN(written, buff, "4.940656e-324");
2077
2078 // Inf/Nan Tests.
2079
2080 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", inf);
2081 ASSERT_STREQ_LEN(written, buff, "inf");
2082
2083 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%E", -inf);
2084 ASSERT_STREQ_LEN(written, buff, "-INF");
2085
2086 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%e", nan);
2087 ASSERT_STREQ_LEN(written, buff, "nan");
2088
2089 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%E", -nan);
2090 ASSERT_STREQ_LEN(written, buff, "-NAN");
2091
2092 // Min Width Tests.
2093
2094 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15e", 1.0);
2095 ASSERT_STREQ_LEN(written, buff, " 1.000000e+00");
2096
2097 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15e", -1.0);
2098 ASSERT_STREQ_LEN(written, buff, " -1.000000e+00");
2099
2100 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15e", 1.0e5);
2101 ASSERT_STREQ_LEN(written, buff, " 1.000000e+05");
2102
2103 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15e", -1.0e5);
2104 ASSERT_STREQ_LEN(written, buff, " -1.000000e+05");
2105
2106 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10e", 1.0e-5);
2107 ASSERT_STREQ_LEN(written, buff, "1.000000e-05");
2108
2109 // Precision Tests.
2110
2111 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.0);
2112 ASSERT_STREQ_LEN(written, buff, "1.0e+00");
2113
2114 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 0.0);
2115 ASSERT_STREQ_LEN(written, buff, "0.0e+00");
2116
2117 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 0.0);
2118 ASSERT_STREQ_LEN(written, buff, "0e+00");
2119
2120 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 0.1);
2121 ASSERT_STREQ_LEN(written, buff, "1.0e-01");
2122
2123 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.09);
2124 ASSERT_STREQ_LEN(written, buff, "1.1e+00");
2125
2126 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.04);
2127 ASSERT_STREQ_LEN(written, buff, "1.0e+00");
2128
2129 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.19);
2130 ASSERT_STREQ_LEN(written, buff, "1.2e+00");
2131
2132 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.99);
2133 ASSERT_STREQ_LEN(written, buff, "2.0e+00");
2134
2135 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 9.99);
2136 ASSERT_STREQ_LEN(written, buff, "1.0e+01");
2137
2138 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2e", 99.9);
2139 ASSERT_STREQ_LEN(written, buff, "9.99e+01");
2140
2141 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 99.9);
2142 ASSERT_STREQ_LEN(written, buff, "1.0e+02");
2143
2144 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5e", 1.25);
2145 ASSERT_STREQ_LEN(written, buff, "1.25000e+00");
2146
2147 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 1.25);
2148 ASSERT_STREQ_LEN(written, buff, "1e+00");
2149
2150 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 1.75);
2151 ASSERT_STREQ_LEN(written, buff, "2e+00");
2152
2153 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.20e", 1.234e-10);
2154 ASSERT_STREQ_LEN(written, buff, "1.23400000000000008140e-10");
2155
2156 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2e", -9.99);
2157 ASSERT_STREQ_LEN(written, buff, "-9.99e+00");
2158
2159 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -9.99);
2160 ASSERT_STREQ_LEN(written, buff, "-1.0e+01");
2161
2162 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5e", 0.0);
2163 ASSERT_STREQ_LEN(written, buff, "0.00000e+00");
2164
2165 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5e", 1.008);
2166 ASSERT_STREQ_LEN(written, buff, "1.00800e+00");
2167
2168 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5e", 1.008e3);
2169 ASSERT_STREQ_LEN(written, buff, "1.00800e+03");
2170
2171 // These tests also focus on rounding. Almost all of them have a 5 right after
2172 // the printed string (e.g. 9.5 with precision 0 prints 0 digits after the
2173 // decimal point). This is again because rounding a number with a 5 after the
2174 // printed section means that more digits have to be checked to determine if
2175 // this should be rounded up (if there are non-zero digits after the 5) or to
2176 // even (if the 5 is the last non-zero digit). Additionally, the algorithm for
2177 // checking if a number is all 0s after the decimal point may not work since
2178 // the decimal point moves in this representation.
2179 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 2.5812229360061737E+200);
2180 ASSERT_STREQ_LEN(written, buff, "3e+200");
2181
2182 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 9.059E+200);
2183 ASSERT_STREQ_LEN(written, buff, "9.1e+200");
2184
2185 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 9.059E+200);
2186 ASSERT_STREQ_LEN(written, buff, "9e+200");
2187
2188 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.166e", 1.131959884853339E-72);
2189 ASSERT_STREQ_LEN(written, buff,
2190 "1."
2191 "13195988485333904593863991136097397258531639976739227369782"
2192 "68612419376648241056393424414314951197624317440549121097287"
2193 "069853416091591569170304865110665559768676757812e-72");
2194
2195 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 9.5);
2196 ASSERT_STREQ_LEN(written, buff, "1e+01");
2197
2198 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.10e", 1.9999999999890936);
2199 ASSERT_STREQ_LEN(written, buff, "2.0000000000e+00");
2200
2201 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 745362143563.03894);
2202 ASSERT_STREQ_LEN(written, buff, "7.5e+11");
2203
2204 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 45181042688.0);
2205 ASSERT_STREQ_LEN(written, buff, "5e+10");
2206
2207 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.35e", 1.3752441369139243);
2208 ASSERT_STREQ_LEN(written, buff, "1.37524413691392433101157166674965993e+00");
2209
2210 // Subnormal Precision Tests
2211
2212 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.310e", 0x1.0p-1022);
2213 ASSERT_STREQ_LEN(
2214 written, buff,
2215 "2."
2216 "225073858507201383090232717332404064219215980462331830553327416887204434"
2217 "813918195854283159012511020564067339731035811005152434161553460108856012"
2218 "385377718821130777993532002330479610147442583636071921565046942503734208"
2219 "375250806650616658158948720491179968591639648500635908770118304874799780"
2220 "8877537499494515804516e-308");
2221
2222 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.30e", 0x1.0p-1022);
2223 ASSERT_STREQ_LEN(written, buff, "2.225073858507201383090232717332e-308");
2224
2225 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.310e", 0x1.0p-1023);
2226 ASSERT_STREQ_LEN(
2227 written, buff,
2228 "1."
2229 "112536929253600691545116358666202032109607990231165915276663708443602217"
2230 "406959097927141579506255510282033669865517905502576217080776730054428006"
2231 "192688859410565388996766001165239805073721291818035960782523471251867104"
2232 "187625403325308329079474360245589984295819824250317954385059152437399890"
2233 "4438768749747257902258e-308");
2234
2235 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.6e", 9.99999e-310);
2236 ASSERT_STREQ_LEN(written, buff, "9.999990e-310");
2237
2238 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5e", 9.99999e-310);
2239 ASSERT_STREQ_LEN(written, buff, "9.99999e-310");
2240
2241 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.4e", 9.99999e-310);
2242 ASSERT_STREQ_LEN(written, buff, "1.0000e-309");
2243
2244 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.3e", 9.99999e-310);
2245 ASSERT_STREQ_LEN(written, buff, "1.000e-309");
2246
2247 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2e", 9.99999e-310);
2248 ASSERT_STREQ_LEN(written, buff, "1.00e-309");
2249
2250 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 9.99999e-310);
2251 ASSERT_STREQ_LEN(written, buff, "1.0e-309");
2252
2253 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0e", 9.99999e-310);
2254 ASSERT_STREQ_LEN(written, buff, "1e-309");
2255
2256 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.10e", 0x1.0p-1074);
2257 ASSERT_STREQ_LEN(written, buff, "4.9406564584e-324");
2258
2259 /*
2260 written = LIBC_NAMESPACE::sprintf(buff, "%.1La", 0.1L);
2261 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
2262 ASSERT_STREQ_LEN(written, buff, "0xc.dp-7");
2263 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
2264 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
2265 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
2266 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
2267 #endif
2268
2269 written = LIBC_NAMESPACE::sprintf(buff, "%.1La",
2270 0xf.fffffffffffffffp16380L); #if
2271 defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80) ASSERT_STREQ_LEN(written, buff,
2272 "0x1.0p+16384"); #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
2273 ASSERT_STREQ_LEN(written, buff, "inf");
2274 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
2275 ASSERT_STREQ_LEN(written, buff, "0x2.0p+16383");
2276 #endif
2277 */
2278
2279 // Rounding Mode Tests.
2280
2281 if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
2282 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.75);
2283 ASSERT_STREQ_LEN(written, buff, "1.8e+00");
2284
2285 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.25);
2286 ASSERT_STREQ_LEN(written, buff, "1.2e+00");
2287
2288 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.125);
2289 ASSERT_STREQ_LEN(written, buff, "1.1e+00");
2290
2291 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.625);
2292 ASSERT_STREQ_LEN(written, buff, "1.6e+00");
2293
2294 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.375);
2295 ASSERT_STREQ_LEN(written, buff, "1.4e+00");
2296
2297 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.875);
2298 ASSERT_STREQ_LEN(written, buff, "1.9e+00");
2299
2300 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.75);
2301 ASSERT_STREQ_LEN(written, buff, "-1.8e+00");
2302
2303 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.25);
2304 ASSERT_STREQ_LEN(written, buff, "-1.2e+00");
2305
2306 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.125);
2307 ASSERT_STREQ_LEN(written, buff, "-1.1e+00");
2308
2309 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.625);
2310 ASSERT_STREQ_LEN(written, buff, "-1.6e+00");
2311
2312 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.375);
2313 ASSERT_STREQ_LEN(written, buff, "-1.4e+00");
2314
2315 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.875);
2316 ASSERT_STREQ_LEN(written, buff, "-1.9e+00");
2317 }
2318
2319 if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
2320 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.75);
2321 ASSERT_STREQ_LEN(written, buff, "1.8e+00");
2322
2323 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.25);
2324 ASSERT_STREQ_LEN(written, buff, "1.3e+00");
2325
2326 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.125);
2327 ASSERT_STREQ_LEN(written, buff, "1.2e+00");
2328
2329 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.625);
2330 ASSERT_STREQ_LEN(written, buff, "1.7e+00");
2331
2332 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.375);
2333 ASSERT_STREQ_LEN(written, buff, "1.4e+00");
2334
2335 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.875);
2336 ASSERT_STREQ_LEN(written, buff, "1.9e+00");
2337
2338 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.75);
2339 ASSERT_STREQ_LEN(written, buff, "-1.7e+00");
2340
2341 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.25);
2342 ASSERT_STREQ_LEN(written, buff, "-1.2e+00");
2343
2344 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.125);
2345 ASSERT_STREQ_LEN(written, buff, "-1.1e+00");
2346
2347 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.625);
2348 ASSERT_STREQ_LEN(written, buff, "-1.6e+00");
2349
2350 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.375);
2351 ASSERT_STREQ_LEN(written, buff, "-1.3e+00");
2352
2353 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.875);
2354 ASSERT_STREQ_LEN(written, buff, "-1.8e+00");
2355 }
2356
2357 if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
2358 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.75);
2359 ASSERT_STREQ_LEN(written, buff, "1.7e+00");
2360
2361 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.25);
2362 ASSERT_STREQ_LEN(written, buff, "1.2e+00");
2363
2364 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.125);
2365 ASSERT_STREQ_LEN(written, buff, "1.1e+00");
2366
2367 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.625);
2368 ASSERT_STREQ_LEN(written, buff, "1.6e+00");
2369
2370 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.375);
2371 ASSERT_STREQ_LEN(written, buff, "1.3e+00");
2372
2373 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.875);
2374 ASSERT_STREQ_LEN(written, buff, "1.8e+00");
2375
2376 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.75);
2377 ASSERT_STREQ_LEN(written, buff, "-1.8e+00");
2378
2379 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.25);
2380 ASSERT_STREQ_LEN(written, buff, "-1.3e+00");
2381
2382 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.125);
2383 ASSERT_STREQ_LEN(written, buff, "-1.2e+00");
2384
2385 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.625);
2386 ASSERT_STREQ_LEN(written, buff, "-1.7e+00");
2387
2388 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.375);
2389 ASSERT_STREQ_LEN(written, buff, "-1.4e+00");
2390
2391 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.875);
2392 ASSERT_STREQ_LEN(written, buff, "-1.9e+00");
2393 }
2394
2395 if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
2396 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.75);
2397 ASSERT_STREQ_LEN(written, buff, "1.7e+00");
2398
2399 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.25);
2400 ASSERT_STREQ_LEN(written, buff, "1.2e+00");
2401
2402 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.125);
2403 ASSERT_STREQ_LEN(written, buff, "1.1e+00");
2404
2405 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.625);
2406 ASSERT_STREQ_LEN(written, buff, "1.6e+00");
2407
2408 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.375);
2409 ASSERT_STREQ_LEN(written, buff, "1.3e+00");
2410
2411 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", 1.875);
2412 ASSERT_STREQ_LEN(written, buff, "1.8e+00");
2413
2414 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.75);
2415 ASSERT_STREQ_LEN(written, buff, "-1.7e+00");
2416
2417 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.25);
2418 ASSERT_STREQ_LEN(written, buff, "-1.2e+00");
2419
2420 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.125);
2421 ASSERT_STREQ_LEN(written, buff, "-1.1e+00");
2422
2423 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.625);
2424 ASSERT_STREQ_LEN(written, buff, "-1.6e+00");
2425
2426 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.375);
2427 ASSERT_STREQ_LEN(written, buff, "-1.3e+00");
2428
2429 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1e", -1.875);
2430 ASSERT_STREQ_LEN(written, buff, "-1.8e+00");
2431 }
2432
2433 // Flag Tests.
2434 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+e", 1.0);
2435 ASSERT_STREQ_LEN(written, buff, "+1.000000e+00");
2436
2437 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+e", -1.0);
2438 ASSERT_STREQ_LEN(written, buff, "-1.000000e+00");
2439
2440 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% e", 1.0);
2441 ASSERT_STREQ_LEN(written, buff, " 1.000000e+00");
2442
2443 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% e", -1.0);
2444 ASSERT_STREQ_LEN(written, buff, "-1.000000e+00");
2445
2446 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-15e", 1.5);
2447 ASSERT_STREQ_LEN(written, buff, "1.500000e+00 ");
2448
2449 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.e", 1.0);
2450 ASSERT_STREQ_LEN(written, buff, "1.e+00");
2451
2452 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.0e", 1.5);
2453 ASSERT_STREQ_LEN(written, buff, "2.e+00");
2454
2455 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%015e", 1.5);
2456 ASSERT_STREQ_LEN(written, buff, "0001.500000e+00");
2457
2458 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%015e", -1.5);
2459 ASSERT_STREQ_LEN(written, buff, "-001.500000e+00");
2460
2461 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+- #0e", 0.0);
2462 ASSERT_STREQ_LEN(written, buff, "+0.000000e+00");
2463
2464 // Combined Tests.
2465
2466 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.2e", 9.99);
2467 ASSERT_STREQ_LEN(written, buff, " 9.99e+00");
2468
2469 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.1e", 9.99);
2470 ASSERT_STREQ_LEN(written, buff, " 1.0e+01");
2471
2472 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.0e", 9.99);
2473 ASSERT_STREQ_LEN(written, buff, " 1e+01");
2474
2475 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.0e", 0.0999);
2476 ASSERT_STREQ_LEN(written, buff, " 1e-01");
2477
2478 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.2e", 9.99);
2479 ASSERT_STREQ_LEN(written, buff, "9.99e+00 ");
2480
2481 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.1e", 9.99);
2482 ASSERT_STREQ_LEN(written, buff, "1.0e+01 ");
2483
2484 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.1e", 1.0e-50);
2485 ASSERT_STREQ_LEN(written, buff, "1.0e-50 ");
2486
2487 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%30e", 1234567890123456789.0);
2488 ASSERT_STREQ_LEN(written, buff, " 1.234568e+18");
2489
2490 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-30e", 1234567890123456789.0);
2491 ASSERT_STREQ_LEN(written, buff, "1.234568e+18 ");
2492
2493 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%25.14e", 9999999999999.99);
2494 ASSERT_STREQ_LEN(written, buff, " 9.99999999999999e+12");
2495
2496 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%25.13e", 9999999999999.99);
2497 ASSERT_STREQ_LEN(written, buff, " 1.0000000000000e+13");
2498
2499 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%25.12e", 9999999999999.99);
2500 ASSERT_STREQ_LEN(written, buff, " 1.000000000000e+13");
2501
2502 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%12.3e %-12.3e", 0.1, 256.0);
2503 ASSERT_STREQ_LEN(written, buff, " 1.000e-01 2.560e+02 ");
2504
2505 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+-#12.3e % 012.3e", 0.1256, 1256.0);
2506 ASSERT_STREQ_LEN(written, buff, "+1.256e-01 001.256e+03");
2507}
2508
2509TEST_F(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) {
2510 ForceRoundingMode r(RoundingMode::Nearest);
2511 // Length Modifier Tests.
2512
2513#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
2514 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.9Le", 1000000000500000000.1L);
2515 ASSERT_STREQ_LEN(written, buff, "1.000000001e+18");
2516
2517 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.9Le", 1000000000500000000.0L);
2518 ASSERT_STREQ_LEN(written, buff, "1.000000000e+18");
2519
2520 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Le", 0xf.fffffffffffffffp+16380L);
2521 ASSERT_STREQ_LEN(written, buff, "1.189731e+4932");
2522#endif
2523
2524 // TODO: Fix long doubles (needs bigger table or alternate algorithm.)
2525 // Currently the table values are generated, which is very slow.
2526 /*
2527 written = LIBC_NAMESPACE::sprintf(buff, "%Lf", 1e100L);
2528 ASSERT_STREQ_LEN(written, buff,
2529 "99999999999999999996693535322073426194986990198284960792713"
2530 "91541752018669482644324418977840117055488.000000");
2531
2532 written = LIBC_NAMESPACE::sprintf(buff, "%Lf", 1.0L);
2533 ASSERT_STREQ_LEN(written, buff, "1.000000");
2534
2535 char big_buff[10000];
2536 written = LIBC_NAMESPACE::sprintf(big_buff, "%Lf", 1e1000L);
2537 ASSERT_STREQ_LEN(
2538 written, big_buff,
2539 "999999999999999999973107317669562353428234857594552594925899449376328728"
2540 "202461036775511405481186963193066642191664822065529414252060696836533522"
2541 "387143501724276282079456797058697369889056407118642873669166717313763499"
2542 "277025985141177344925615052465165938514140943010597323750202561187880136"
2543 "174810574553749194614479541820148407958204853833697063267336294787191005"
2544 "628217462261955103745349844675732989944229689277833828743730290177882029"
2545 "042613704915899149603539993716885598351951895974316347947147507970269673"
2546 "097709017164643598452451201499004104341931127294141495501309305995449742"
2547 "273419524803597130450457553871345958049837885085168840317195672271085085"
2548 "950520957945970913451088104971436093671776829538796532762184174216651692"
2549 "640931965387852083906784898823494867055070322768919156031682291829761007"
2550 "101483799978382119231551218582499361996919560548090784230386907125151658"
2551 "086767207295524036170321059257942621398084478974000973622199163292708506"
2552 "2431457550909271560663602154947063707982236377366647567795879936."
2553 "000000");
2554
2555 written = LIBC_NAMESPACE::sprintf(big_buff, "%Lf", 1e4900L);
2556 ASSERT_STREQ_LEN(
2557 written, big_buff,
2558 "100000000000000000002708312230690349833224052504078834346502930111959028"
2559 "517260692666637048230414374897655201843766090626319971729765251179632020"
2560 "313912652522792711197087872698264530532442630109549129842736280196919130"
2561 "242615101228133188193853826983121366159061148351354364472807590931218045"
2562 "387490935930967150336231085015126034696883068553581691802388371635128003"
2563 "615577299166097675723780877126495909902479233742826339471026068806070433"
2564 "075629449530819183550315434973800271862658869400009022028602967197463980"
2565 "126881829804282202449930132940824361207087494829502385835258094836304011"
2566 "876250359661206802659650567866176246063987902366800491980400341950657151"
2567 "370854446585517805253310195469184699955519312761482572080479702840420595"
2568 "377369017651259376039167277822106875560385309101650382998482652792335482"
2569 "865443482342801545877390859444282105890147577937366066315975231014810320"
2570 "888482059656248277607763361589359794524314002443575149260630989130103550"
2571 "443177966380769341050735632338583912575890190136462629316287947355057647"
2572 "111088565611192544631519843618778618820046304429723908484879583579178075"
2573 "456701368334212923379389029311286386996015804122917416008806233549005183"
2574 "152461084266176543129004016414959261473645240454289630182591200574019087"
2575 "358223489767381636349719510715487188747217311279465814538495924567014916"
2576 "238565628036285599497236493491668884212847699052761266207598941300449276"
2577 "447201387520841811835583254242213093566548778954711633721122784159793843"
2578 "766802019309395771984693609426401362800013936338891483689127845928572536"
2579 "790651156184721483511507878883282891696900630100211914227950790472211403"
2580 "392549466062537498185758854079775888444518306635752468713312357556380082"
2581 "275500658967283696421824354930077523691855699312544373220921962817907078"
2582 "445538421941800259027487429330768616490865438859612697367766323925013940"
2583 "918384858952407145253573823848733994146335416209309233074165707437420756"
2584 "438833918763109580759409985573826485055208965115587885226774453455112406"
2585 "581351429640282227888764449360534584421929291565334894907337572527922691"
2586 "473242328379737396430908523008687037407295838014450772162091496534584696"
2587 "605157436893236842602956298545594095307060870397506421786236892553632163"
2588 "491468601982681381011940409602294892199042638682530687578982576819839451"
2589 "907594697546439533559153604700750696252355362322662219852740143212566818"
2590 "745528402265116534684566273868361460640280523251242059850044328669692159"
2591 "629900374576027104298177006629276014371540945261309319363704125592775129"
2592 "543526908667388673739382491147471395192495459318806593271282662311169392"
2593 "196897003517840025298267505925987901751541005546610016067658227181318892"
2594 "914686508281007582655667597441346214499847364272258631922040641860333431"
2595 "409838623713258383681350233064164940590695888300919626215847587544298023"
2596 "636416943680102708406086295669759876682046839368574433996997648445207805"
2597 "615784339667691231286807666753972942872019850432610318031627872612657513"
2598 "588188267160616660825719678199868371370527508463011236193719286066916786"
2599 "169956541349011494927225747024994619057884118692213564790598702879596058"
2600 "672338334720925179141906809470606964896245458600635183723159228561689808"
2601 "246141482736625197373238197777325580142168245885279594913851700941789475"
2602 "252421784152262567254611571822468808675893407728003047921107885664474662"
2603 "930921581384003950729114103689170603748380178682003976896397305836815761"
2604 "717676338115866650889936516794601457549097578905329423919798362140648664"
2605 "569177147076571576101649257502509463877402424847669830852345415301684820"
2606 "395813946416649808062227494112874521812750160935760825922220707178083076"
2607 "380203450993589198835885505461509442443773367592842795410339065860781804"
2608 "024975272228687688301824830333940416256885455008512598774611538878683158"
2609 "183931461086893832255176926531299425504132104728730288984598001187854507"
2610 "900417184206801359847651992484444933900133130832052346600926424167009902"
2611 "829803553087005800387704758687923428053612864451456596148162238935900033"
2612 "917094683141205188616000211702577553792389670853917118547527592495253773"
2613 "028135298405566315903922235989614934474805789300370437580494193066066314"
2614 "056627605207631392651010580925826419831250810981343093764403877594495896"
2615 "516881097415880926429607388979497471571321217205535961262051641426436441"
2616 "668989765107456413733909427384182109285933511623871034309722437967253289"
2617 "084018145083721513211807496392673952789642893241520398827805325610653506"
2618 "029060153153064455898648607959013571280930834475689835845791849456112104"
2619 "462337569019001580859906425911782967213265389744605395555069797947978230"
2620 "708108432086217134763779632408473684293543722127232658767439906910370146"
2621 "716836295909075482355827087389127370874842532825987593970846704144140471"
2622 "956027276735614286138656432085771988513977140957180090146798065497158947"
2623 "229765733489703157617307078835099906185890777007500964162371428641176460"
2624 "739074789794941408428328217107759915202650066155868439585510978709442590"
2625 "231934194956788626761834746430104077432547436359522462253411168467463134"
2626 "24896.000000");
2627*/
2628}
2629
2630TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) {
2631 ForceRoundingMode r(RoundingMode::Nearest);
2632 double inf = LIBC_NAMESPACE::fputil::FPBits<double>::inf().get_val();
2633 double nan = LIBC_NAMESPACE::fputil::FPBits<double>::quiet_nan().get_val();
2634
2635 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 1.0);
2636 ASSERT_STREQ_LEN(written, buff, "1");
2637
2638 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%G", -1.0);
2639 ASSERT_STREQ_LEN(written, buff, "-1");
2640
2641 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", -1.234567);
2642 ASSERT_STREQ_LEN(written, buff, "-1.23457");
2643
2644 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0.0);
2645 ASSERT_STREQ_LEN(written, buff, "0");
2646
2647 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", -0.0);
2648 ASSERT_STREQ_LEN(written, buff, "-0");
2649
2650 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 1.5);
2651 ASSERT_STREQ_LEN(written, buff, "1.5");
2652
2653 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 1e300);
2654 ASSERT_STREQ_LEN(written, buff, "1e+300");
2655
2656 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0.1);
2657 ASSERT_STREQ_LEN(written, buff, "0.1");
2658
2659 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0.001);
2660 ASSERT_STREQ_LEN(written, buff, "0.001");
2661
2662 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0.00001);
2663 ASSERT_STREQ_LEN(written, buff, "1e-05");
2664
2665 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0.0000001);
2666 ASSERT_STREQ_LEN(written, buff, "1e-07");
2667
2668 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0.000000001);
2669 ASSERT_STREQ_LEN(written, buff, "1e-09");
2670
2671 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 1.0e-20);
2672 ASSERT_STREQ_LEN(written, buff, "1e-20");
2673
2674 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 1234567890123456789.0);
2675 ASSERT_STREQ_LEN(written, buff, "1.23457e+18");
2676
2677 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 9999990000000.00);
2678 ASSERT_STREQ_LEN(written, buff, "9.99999e+12");
2679
2680 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 9999999000000.00);
2681 ASSERT_STREQ_LEN(written, buff, "1e+13");
2682
2683 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0xa.aaaaaaaaaaaaaabp-7);
2684 ASSERT_STREQ_LEN(written, buff, "0.0833333");
2685
2686 // Simple Subnormal Tests.
2687
2688 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0x1.0p-1027);
2689 ASSERT_STREQ_LEN(written, buff, "6.95336e-310");
2690
2691 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", 0x1.0p-1074);
2692 ASSERT_STREQ_LEN(written, buff, "4.94066e-324");
2693
2694 // Inf/Nan Tests.
2695
2696 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", inf);
2697 ASSERT_STREQ_LEN(written, buff, "inf");
2698
2699 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%G", -inf);
2700 ASSERT_STREQ_LEN(written, buff, "-INF");
2701
2702 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%g", nan);
2703 ASSERT_STREQ_LEN(written, buff, "nan");
2704
2705 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%G", -nan);
2706 ASSERT_STREQ_LEN(written, buff, "-NAN");
2707
2708 // Min Width Tests.
2709
2710 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15g", 1.0);
2711 ASSERT_STREQ_LEN(written, buff, " 1");
2712
2713 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15g", -1.0);
2714 ASSERT_STREQ_LEN(written, buff, " -1");
2715
2716 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15g", 1.0e5);
2717 ASSERT_STREQ_LEN(written, buff, " 100000");
2718
2719 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%15g", -1.0e5);
2720 ASSERT_STREQ_LEN(written, buff, " -100000");
2721
2722 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10g", 1.0e-5);
2723 ASSERT_STREQ_LEN(written, buff, " 1e-05");
2724
2725 // Precision Tests.
2726
2727 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.23456789);
2728 ASSERT_STREQ_LEN(written, buff, "1.2");
2729
2730 // Trimming trailing zeroes causes the precision to be ignored here.
2731 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1g", 0.0);
2732 ASSERT_STREQ_LEN(written, buff, "0");
2733
2734 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0g", 0.0);
2735 ASSERT_STREQ_LEN(written, buff, "0");
2736
2737 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 0.1);
2738 ASSERT_STREQ_LEN(written, buff, "0.1");
2739
2740 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.09);
2741 ASSERT_STREQ_LEN(written, buff, "1.1");
2742
2743 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.04);
2744 ASSERT_STREQ_LEN(written, buff, "1");
2745
2746 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.19);
2747 ASSERT_STREQ_LEN(written, buff, "1.2");
2748
2749 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.99);
2750 ASSERT_STREQ_LEN(written, buff, "2");
2751
2752 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 9.99);
2753 ASSERT_STREQ_LEN(written, buff, "10");
2754
2755 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.3g", 99.9);
2756 ASSERT_STREQ_LEN(written, buff, "99.9");
2757
2758 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 99.9);
2759 ASSERT_STREQ_LEN(written, buff, "1e+02");
2760
2761 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1g", 99.9);
2762 ASSERT_STREQ_LEN(written, buff, "1e+02");
2763
2764 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5g", 1.25);
2765 ASSERT_STREQ_LEN(written, buff, "1.25");
2766
2767 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0g", 1.25);
2768 ASSERT_STREQ_LEN(written, buff, "1");
2769
2770 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0g", 1.75);
2771 ASSERT_STREQ_LEN(written, buff, "2");
2772
2773 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.20g", 1.234e-10);
2774 ASSERT_STREQ_LEN(written, buff, "1.2340000000000000814e-10");
2775
2776 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.3g", -9.99);
2777 ASSERT_STREQ_LEN(written, buff, "-9.99");
2778
2779 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -9.99);
2780 ASSERT_STREQ_LEN(written, buff, "-10");
2781
2782 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1g", -9.99);
2783 ASSERT_STREQ_LEN(written, buff, "-1e+01");
2784
2785 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5g", 1.008);
2786 ASSERT_STREQ_LEN(written, buff, "1.008");
2787
2788 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5g", 1.008e3);
2789 ASSERT_STREQ_LEN(written, buff, "1008");
2790
2791 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.4g", 9999.0);
2792 ASSERT_STREQ_LEN(written, buff, "9999");
2793
2794 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.3g", 9999.0);
2795 ASSERT_STREQ_LEN(written, buff, "1e+04");
2796
2797 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.3g", 1256.0);
2798 ASSERT_STREQ_LEN(written, buff, "1.26e+03");
2799
2800 // Found through large scale testing.
2801 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.15g", 22.25);
2802 ASSERT_STREQ_LEN(written, buff, "22.25");
2803
2804 // These tests also focus on rounding, but only in how it relates to the base
2805 // 10 exponent. The %g conversion selects between being a %f or %e conversion
2806 // based on what the exponent would be if it was %e. If we call the precision
2807 // P (equal to 6 if the precision is not set, 0 if the provided precision is
2808 // 0, and provided precision - 1 otherwise) and the exponent X, then the style
2809 // is %f with an effective precision of P - X + 1 if P > X >= -4, else the
2810 // style is %e with effective precision P - 1. Additionally, it attempts to
2811 // trim zeros that would be displayed after the decimal point.
2812 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1g", 9.059E+200);
2813 ASSERT_STREQ_LEN(written, buff, "9e+200");
2814
2815 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 9.059E+200);
2816 ASSERT_STREQ_LEN(written, buff, "9.1e+200");
2817
2818 // For this test, P = 0 and X = 1, so P > X >= -4 is false, giving a %e style.
2819 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0g", 9.5);
2820 ASSERT_STREQ_LEN(written, buff, "1e+01");
2821
2822 // Subnormal Precision Tests
2823 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.310g", 0x1.0p-1022);
2824 ASSERT_STREQ_LEN(
2825 written, buff,
2826 "2."
2827 "225073858507201383090232717332404064219215980462331830553327416887204434"
2828 "813918195854283159012511020564067339731035811005152434161553460108856012"
2829 "385377718821130777993532002330479610147442583636071921565046942503734208"
2830 "375250806650616658158948720491179968591639648500635908770118304874799780"
2831 "887753749949451580452e-308");
2832
2833 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.30g", 0x1.0p-1022);
2834 ASSERT_STREQ_LEN(written, buff, "2.22507385850720138309023271733e-308");
2835
2836 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.310g", 0x1.0p-1023);
2837 ASSERT_STREQ_LEN(
2838 written, buff,
2839 "1."
2840 "112536929253600691545116358666202032109607990231165915276663708443602217"
2841 "406959097927141579506255510282033669865517905502576217080776730054428006"
2842 "192688859410565388996766001165239805073721291818035960782523471251867104"
2843 "187625403325308329079474360245589984295819824250317954385059152437399890"
2844 "443876874974725790226e-308");
2845
2846 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.7g", 9.99999e-310);
2847 ASSERT_STREQ_LEN(written, buff, "9.99999e-310");
2848
2849 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.6g", 9.99999e-310);
2850 ASSERT_STREQ_LEN(written, buff, "9.99999e-310");
2851
2852 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.5g", 9.99999e-310);
2853 ASSERT_STREQ_LEN(written, buff, "1e-309");
2854
2855 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.4g", 9.99999e-310);
2856 ASSERT_STREQ_LEN(written, buff, "1e-309");
2857
2858 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.3g", 9.99999e-310);
2859 ASSERT_STREQ_LEN(written, buff, "1e-309");
2860
2861 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 9.99999e-310);
2862 ASSERT_STREQ_LEN(written, buff, "1e-309");
2863
2864 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.1g", 9.99999e-310);
2865 ASSERT_STREQ_LEN(written, buff, "1e-309");
2866
2867 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.0g", 9.99999e-310);
2868 ASSERT_STREQ_LEN(written, buff, "1e-309");
2869
2870 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.10g", 0x1.0p-1074);
2871 ASSERT_STREQ_LEN(written, buff, "4.940656458e-324");
2872
2873#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
2874
2875 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.60Lg", 0xa.aaaaaaaaaaaaaabp-7L);
2876 ASSERT_STREQ_LEN(
2877 written, buff,
2878 "0.0833333333333333333355920878593448009041821933351457118988037");
2879
2880#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
2881
2882 // Long double precision tests.
2883 // These are currently commented out because they require long double support
2884 // that isn't ready yet.
2885 /*
2886 written = LIBC_NAMESPACE::sprintf(buff, "%.1La", 0.1L);
2887 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
2888 ASSERT_STREQ_LEN(written, buff, "0xc.dp-7");
2889 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
2890 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
2891 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
2892 ASSERT_STREQ_LEN(written, buff, "0x1.ap-4");
2893 #endif
2894
2895 written = LIBC_NAMESPACE::sprintf(buff, "%.1La",
2896 0xf.fffffffffffffffp16380L); #if
2897 defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80) ASSERT_STREQ_LEN(written, buff,
2898 "0x1.0p+16384"); #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
2899 ASSERT_STREQ_LEN(written, buff, "inf");
2900 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
2901 ASSERT_STREQ_LEN(written, buff, "0x2.0p+16383");
2902 #endif
2903 */
2904
2905 // Rounding Mode Tests.
2906
2907 if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
2908 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.75);
2909 ASSERT_STREQ_LEN(written, buff, "1.8");
2910
2911 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.25);
2912 ASSERT_STREQ_LEN(written, buff, "1.2");
2913
2914 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.125);
2915 ASSERT_STREQ_LEN(written, buff, "1.1");
2916
2917 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.625);
2918 ASSERT_STREQ_LEN(written, buff, "1.6");
2919
2920 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.375);
2921 ASSERT_STREQ_LEN(written, buff, "1.4");
2922
2923 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.875);
2924 ASSERT_STREQ_LEN(written, buff, "1.9");
2925
2926 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.75);
2927 ASSERT_STREQ_LEN(written, buff, "-1.8");
2928
2929 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.25);
2930 ASSERT_STREQ_LEN(written, buff, "-1.2");
2931
2932 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.125);
2933 ASSERT_STREQ_LEN(written, buff, "-1.1");
2934
2935 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.625);
2936 ASSERT_STREQ_LEN(written, buff, "-1.6");
2937
2938 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.375);
2939 ASSERT_STREQ_LEN(written, buff, "-1.4");
2940
2941 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.875);
2942 ASSERT_STREQ_LEN(written, buff, "-1.9");
2943 }
2944
2945 if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
2946 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.75);
2947 ASSERT_STREQ_LEN(written, buff, "1.8");
2948
2949 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.25);
2950 ASSERT_STREQ_LEN(written, buff, "1.3");
2951
2952 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.125);
2953 ASSERT_STREQ_LEN(written, buff, "1.2");
2954
2955 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.625);
2956 ASSERT_STREQ_LEN(written, buff, "1.7");
2957
2958 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.375);
2959 ASSERT_STREQ_LEN(written, buff, "1.4");
2960
2961 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.875);
2962 ASSERT_STREQ_LEN(written, buff, "1.9");
2963
2964 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.75);
2965 ASSERT_STREQ_LEN(written, buff, "-1.7");
2966
2967 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.25);
2968 ASSERT_STREQ_LEN(written, buff, "-1.2");
2969
2970 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.125);
2971 ASSERT_STREQ_LEN(written, buff, "-1.1");
2972
2973 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.625);
2974 ASSERT_STREQ_LEN(written, buff, "-1.6");
2975
2976 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.375);
2977 ASSERT_STREQ_LEN(written, buff, "-1.3");
2978
2979 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.875);
2980 ASSERT_STREQ_LEN(written, buff, "-1.8");
2981 }
2982
2983 if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
2984 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.75);
2985 ASSERT_STREQ_LEN(written, buff, "1.7");
2986
2987 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.25);
2988 ASSERT_STREQ_LEN(written, buff, "1.2");
2989
2990 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.125);
2991 ASSERT_STREQ_LEN(written, buff, "1.1");
2992
2993 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.625);
2994 ASSERT_STREQ_LEN(written, buff, "1.6");
2995
2996 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.375);
2997 ASSERT_STREQ_LEN(written, buff, "1.3");
2998
2999 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.875);
3000 ASSERT_STREQ_LEN(written, buff, "1.8");
3001
3002 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.75);
3003 ASSERT_STREQ_LEN(written, buff, "-1.8");
3004
3005 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.25);
3006 ASSERT_STREQ_LEN(written, buff, "-1.3");
3007
3008 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.125);
3009 ASSERT_STREQ_LEN(written, buff, "-1.2");
3010
3011 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.625);
3012 ASSERT_STREQ_LEN(written, buff, "-1.7");
3013
3014 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.375);
3015 ASSERT_STREQ_LEN(written, buff, "-1.4");
3016
3017 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.875);
3018 ASSERT_STREQ_LEN(written, buff, "-1.9");
3019 }
3020
3021 if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
3022 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.75);
3023 ASSERT_STREQ_LEN(written, buff, "1.7");
3024
3025 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.25);
3026 ASSERT_STREQ_LEN(written, buff, "1.2");
3027
3028 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.125);
3029 ASSERT_STREQ_LEN(written, buff, "1.1");
3030
3031 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.625);
3032 ASSERT_STREQ_LEN(written, buff, "1.6");
3033
3034 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.375);
3035 ASSERT_STREQ_LEN(written, buff, "1.3");
3036
3037 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", 1.875);
3038 ASSERT_STREQ_LEN(written, buff, "1.8");
3039
3040 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.75);
3041 ASSERT_STREQ_LEN(written, buff, "-1.7");
3042
3043 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.25);
3044 ASSERT_STREQ_LEN(written, buff, "-1.2");
3045
3046 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.125);
3047 ASSERT_STREQ_LEN(written, buff, "-1.1");
3048
3049 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.625);
3050 ASSERT_STREQ_LEN(written, buff, "-1.6");
3051
3052 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.375);
3053 ASSERT_STREQ_LEN(written, buff, "-1.3");
3054
3055 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%.2g", -1.875);
3056 ASSERT_STREQ_LEN(written, buff, "-1.8");
3057 }
3058
3059 // Flag Tests.
3060 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+g", 1.0);
3061 ASSERT_STREQ_LEN(written, buff, "+1");
3062
3063 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+g", -1.0);
3064 ASSERT_STREQ_LEN(written, buff, "-1");
3065
3066 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% g", 1.0);
3067 ASSERT_STREQ_LEN(written, buff, " 1");
3068
3069 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "% g", -1.0);
3070 ASSERT_STREQ_LEN(written, buff, "-1");
3071
3072 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-15g", 1.5);
3073 ASSERT_STREQ_LEN(written, buff, "1.5 ");
3074
3075 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.g", 1.0);
3076 ASSERT_STREQ_LEN(written, buff, "1.");
3077
3078 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#g", 1.0);
3079 ASSERT_STREQ_LEN(written, buff, "1.00000");
3080
3081 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#.0g", 1.5);
3082 ASSERT_STREQ_LEN(written, buff, "2.");
3083
3084 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%015g", 1.5);
3085 ASSERT_STREQ_LEN(written, buff, "0000000000001.5");
3086
3087 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%015g", -1.5);
3088 ASSERT_STREQ_LEN(written, buff, "-000000000001.5");
3089
3090 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+- #0g", 0.0);
3091 ASSERT_STREQ_LEN(written, buff, "+0.00000");
3092
3093 // Combined Tests.
3094
3095 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.3g", 9.99);
3096 ASSERT_STREQ_LEN(written, buff, " 9.99");
3097
3098 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.2g", 9.99);
3099 ASSERT_STREQ_LEN(written, buff, " 10");
3100
3101 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%10.1g", 9.99);
3102 ASSERT_STREQ_LEN(written, buff, " 1e+01");
3103
3104 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.3g", 9.99);
3105 ASSERT_STREQ_LEN(written, buff, "9.99 ");
3106
3107 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.2g", 9.99);
3108 ASSERT_STREQ_LEN(written, buff, "10 ");
3109
3110 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.1g", 9.99);
3111 ASSERT_STREQ_LEN(written, buff, "1e+01 ");
3112
3113 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-10.1g", 1.0e-50);
3114 ASSERT_STREQ_LEN(written, buff, "1e-50 ");
3115
3116 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%30g", 1234567890123456789.0);
3117 ASSERT_STREQ_LEN(written, buff, " 1.23457e+18");
3118
3119 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%-30g", 1234567890123456789.0);
3120 ASSERT_STREQ_LEN(written, buff, "1.23457e+18 ");
3121
3122 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%25.15g", 9999999999999.99);
3123 ASSERT_STREQ_LEN(written, buff, " 9999999999999.99");
3124
3125 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%25.14g", 9999999999999.99);
3126 ASSERT_STREQ_LEN(written, buff, " 10000000000000");
3127
3128 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%25.13g", 9999999999999.99);
3129 ASSERT_STREQ_LEN(written, buff, " 1e+13");
3130
3131 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%#12.3g %-12.3g", 0.1, 256.0);
3132 ASSERT_STREQ_LEN(written, buff, " 0.100 256 ");
3133
3134 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%+-#12.3g % 012.3g", 0.1256, 1256.0);
3135 ASSERT_STREQ_LEN(written, buff, "+0.126 0001.26e+03");
3136}
3137
3138TEST_F(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) {
3139 ForceRoundingMode r(RoundingMode::Nearest);
3140
3141 // Length Modifier Tests.
3142
3143#if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
3144
3145 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lg", 0xf.fffffffffffffffp+16380L);
3146 ASSERT_STREQ_LEN(written, buff, "1.18973e+4932");
3147
3148 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lg", 0xa.aaaaaaaaaaaaaabp-7L);
3149 ASSERT_STREQ_LEN(written, buff, "0.0833333");
3150
3151 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%Lg", 9.99999999999e-100L);
3152 ASSERT_STREQ_LEN(written, buff, "1e-99");
3153
3154#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
3155
3156 // TODO: Uncomment the below tests after long double support is added
3157 /*
3158 written = LIBC_NAMESPACE::sprintf(buff, "%Lf", 1e100L);
3159 ASSERT_STREQ_LEN(written, buff,
3160 "99999999999999999996693535322073426194986990198284960792713"
3161 "91541752018669482644324418977840117055488.000000");
3162
3163 written = LIBC_NAMESPACE::sprintf(buff, "%Lf", 1.0L);
3164 ASSERT_STREQ_LEN(written, buff, "1.000000");
3165
3166 char big_buff[10000];
3167 written = LIBC_NAMESPACE::sprintf(big_buff, "%Lf", 1e1000L);
3168 ASSERT_STREQ_LEN(
3169 written, big_buff,
3170 "999999999999999999973107317669562353428234857594552594925899449376328728"
3171 "202461036775511405481186963193066642191664822065529414252060696836533522"
3172 "387143501724276282079456797058697369889056407118642873669166717313763499"
3173 "277025985141177344925615052465165938514140943010597323750202561187880136"
3174 "174810574553749194614479541820148407958204853833697063267336294787191005"
3175 "628217462261955103745349844675732989944229689277833828743730290177882029"
3176 "042613704915899149603539993716885598351951895974316347947147507970269673"
3177 "097709017164643598452451201499004104341931127294141495501309305995449742"
3178 "273419524803597130450457553871345958049837885085168840317195672271085085"
3179 "950520957945970913451088104971436093671776829538796532762184174216651692"
3180 "640931965387852083906784898823494867055070322768919156031682291829761007"
3181 "101483799978382119231551218582499361996919560548090784230386907125151658"
3182 "086767207295524036170321059257942621398084478974000973622199163292708506"
3183 "2431457550909271560663602154947063707982236377366647567795879936."
3184 "000000");
3185
3186 written = LIBC_NAMESPACE::sprintf(big_buff, "%Lf", 1e4900L);
3187 ASSERT_STREQ_LEN(
3188 written, big_buff,
3189 "100000000000000000002708312230690349833224052504078834346502930111959028"
3190 "517260692666637048230414374897655201843766090626319971729765251179632020"
3191 "313912652522792711197087872698264530532442630109549129842736280196919130"
3192 "242615101228133188193853826983121366159061148351354364472807590931218045"
3193 "387490935930967150336231085015126034696883068553581691802388371635128003"
3194 "615577299166097675723780877126495909902479233742826339471026068806070433"
3195 "075629449530819183550315434973800271862658869400009022028602967197463980"
3196 "126881829804282202449930132940824361207087494829502385835258094836304011"
3197 "876250359661206802659650567866176246063987902366800491980400341950657151"
3198 "370854446585517805253310195469184699955519312761482572080479702840420595"
3199 "377369017651259376039167277822106875560385309101650382998482652792335482"
3200 "865443482342801545877390859444282105890147577937366066315975231014810320"
3201 "888482059656248277607763361589359794524314002443575149260630989130103550"
3202 "443177966380769341050735632338583912575890190136462629316287947355057647"
3203 "111088565611192544631519843618778618820046304429723908484879583579178075"
3204 "456701368334212923379389029311286386996015804122917416008806233549005183"
3205 "152461084266176543129004016414959261473645240454289630182591200574019087"
3206 "358223489767381636349719510715487188747217311279465814538495924567014916"
3207 "238565628036285599497236493491668884212847699052761266207598941300449276"
3208 "447201387520841811835583254242213093566548778954711633721122784159793843"
3209 "766802019309395771984693609426401362800013936338891483689127845928572536"
3210 "790651156184721483511507878883282891696900630100211914227950790472211403"
3211 "392549466062537498185758854079775888444518306635752468713312357556380082"
3212 "275500658967283696421824354930077523691855699312544373220921962817907078"
3213 "445538421941800259027487429330768616490865438859612697367766323925013940"
3214 "918384858952407145253573823848733994146335416209309233074165707437420756"
3215 "438833918763109580759409985573826485055208965115587885226774453455112406"
3216 "581351429640282227888764449360534584421929291565334894907337572527922691"
3217 "473242328379737396430908523008687037407295838014450772162091496534584696"
3218 "605157436893236842602956298545594095307060870397506421786236892553632163"
3219 "491468601982681381011940409602294892199042638682530687578982576819839451"
3220 "907594697546439533559153604700750696252355362322662219852740143212566818"
3221 "745528402265116534684566273868361460640280523251242059850044328669692159"
3222 "629900374576027104298177006629276014371540945261309319363704125592775129"
3223 "543526908667388673739382491147471395192495459318806593271282662311169392"
3224 "196897003517840025298267505925987901751541005546610016067658227181318892"
3225 "914686508281007582655667597441346214499847364272258631922040641860333431"
3226 "409838623713258383681350233064164940590695888300919626215847587544298023"
3227 "636416943680102708406086295669759876682046839368574433996997648445207805"
3228 "615784339667691231286807666753972942872019850432610318031627872612657513"
3229 "588188267160616660825719678199868371370527508463011236193719286066916786"
3230 "169956541349011494927225747024994619057884118692213564790598702879596058"
3231 "672338334720925179141906809470606964896245458600635183723159228561689808"
3232 "246141482736625197373238197777325580142168245885279594913851700941789475"
3233 "252421784152262567254611571822468808675893407728003047921107885664474662"
3234 "930921581384003950729114103689170603748380178682003976896397305836815761"
3235 "717676338115866650889936516794601457549097578905329423919798362140648664"
3236 "569177147076571576101649257502509463877402424847669830852345415301684820"
3237 "395813946416649808062227494112874521812750160935760825922220707178083076"
3238 "380203450993589198835885505461509442443773367592842795410339065860781804"
3239 "024975272228687688301824830333940416256885455008512598774611538878683158"
3240 "183931461086893832255176926531299425504132104728730288984598001187854507"
3241 "900417184206801359847651992484444933900133130832052346600926424167009902"
3242 "829803553087005800387704758687923428053612864451456596148162238935900033"
3243 "917094683141205188616000211702577553792389670853917118547527592495253773"
3244 "028135298405566315903922235989614934474805789300370437580494193066066314"
3245 "056627605207631392651010580925826419831250810981343093764403877594495896"
3246 "516881097415880926429607388979497471571321217205535961262051641426436441"
3247 "668989765107456413733909427384182109285933511623871034309722437967253289"
3248 "084018145083721513211807496392673952789642893241520398827805325610653506"
3249 "029060153153064455898648607959013571280930834475689835845791849456112104"
3250 "462337569019001580859906425911782967213265389744605395555069797947978230"
3251 "708108432086217134763779632408473684293543722127232658767439906910370146"
3252 "716836295909075482355827087389127370874842532825987593970846704144140471"
3253 "956027276735614286138656432085771988513977140957180090146798065497158947"
3254 "229765733489703157617307078835099906185890777007500964162371428641176460"
3255 "739074789794941408428328217107759915202650066155868439585510978709442590"
3256 "231934194956788626761834746430104077432547436359522462253411168467463134"
3257 "24896.000000");
3258*/
3259 /*
3260 written = LIBC_NAMESPACE::sprintf(buff, "%La", 0.1L);
3261 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
3262 ASSERT_STREQ_LEN(written, buff, "0xc.ccccccccccccccdp-7");
3263 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
3264 ASSERT_STREQ_LEN(written, buff, "0x1.999999999999ap-4");
3265 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
3266 ASSERT_STREQ_LEN(written, buff, "0x1.999999999999999999999999999ap-4");
3267 #endif
3268
3269 written = LIBC_NAMESPACE::sprintf(buff, "%La", 1.0e1000L);
3270 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
3271 ASSERT_STREQ_LEN(written, buff, "0xf.38db1f9dd3dac05p+3318");
3272 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
3273 ASSERT_STREQ_LEN(written, buff, "inf");
3274 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
3275 ASSERT_STREQ_LEN(written, buff, "0x1.e71b63f3ba7b580af1a52d2a7379p+3321");
3276 #endif
3277
3278 written = LIBC_NAMESPACE::sprintf(buff, "%La", 1.0e-1000L);
3279 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80)
3280 ASSERT_STREQ_LEN(written, buff, "0x8.68a9188a89e1467p-3325");
3281 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
3282 ASSERT_STREQ_LEN(written, buff, "0x0p+0");
3283 #elif defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
3284 ASSERT_STREQ_LEN(written, buff, "0x1.0d152311513c28ce202627c06ec2p-3322");
3285 #endif
3286 */
3287}
3288
3289#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
3290
3291#if defined(LIBC_COMPILER_HAS_FIXED_POINT) && \
3292 !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT)
3293TEST_F(LlvmLibcSPrintfTest, FixedConv) {
3294
3295 // These numeric tests are potentially a little weak, but the fuzz test is
3296 // more thorough than my handwritten tests tend to be.
3297
3298 // TODO: Replace hex literals with their appropriate fixed point literals.
3299
3300 written = LIBC_NAMESPACE::sprintf(buff, "%k", 0x0); // 0.0
3301 ASSERT_STREQ_LEN(written, buff, "0.000000");
3302
3303 written = LIBC_NAMESPACE::sprintf(buff, "%k", 0x80000000); // -0.0
3304 ASSERT_STREQ_LEN(written, buff, "-0.000000");
3305
3306 written = LIBC_NAMESPACE::sprintf(buff, "%r", 0xffff); // -fract max
3307 ASSERT_STREQ_LEN(written, buff, "-0.999969");
3308
3309 written = LIBC_NAMESPACE::sprintf(buff, "%R", 0xffff); // unsigned fract max
3310 ASSERT_STREQ_LEN(written, buff, "0.999985");
3311
3312 written = LIBC_NAMESPACE::sprintf(buff, "%k", 0xffffffff); // -accum max
3313 ASSERT_STREQ_LEN(written, buff, "-65535.999969");
3314
3315 written =
3316 LIBC_NAMESPACE::sprintf(buff, "%K", 0xffffffff); // unsigned accum max
3317 ASSERT_STREQ_LEN(written, buff, "65535.999985");
3318
3319 written = LIBC_NAMESPACE::sprintf(buff, "%r", 0x7fff); // fract max
3320 ASSERT_STREQ_LEN(written, buff, "0.999969");
3321
3322 written = LIBC_NAMESPACE::sprintf(buff, "%k", 0x7fffffff); // accum max
3323 ASSERT_STREQ_LEN(written, buff, "65535.999969");
3324
3325 // Length Modifier Tests.
3326
3327 written = LIBC_NAMESPACE::sprintf(buff, "%hk", 0x0); // 0.0
3328 ASSERT_STREQ_LEN(written, buff, "0.000000");
3329
3330 written = LIBC_NAMESPACE::sprintf(buff, "%hk", 0xffff); // -short accum max
3331 ASSERT_STREQ_LEN(written, buff, "-255.992188");
3332
3333 written = LIBC_NAMESPACE::sprintf(buff, "%hr", 0x0); // 0.0
3334 ASSERT_STREQ_LEN(written, buff, "0.000000");
3335
3336 written = LIBC_NAMESPACE::sprintf(buff, "%hr", 0xff); // -short fract max
3337 ASSERT_STREQ_LEN(written, buff, "-0.992188");
3338
3339 written = LIBC_NAMESPACE::sprintf(buff, "%hK", 0x0); // 0.0
3340 ASSERT_STREQ_LEN(written, buff, "0.000000");
3341
3342 written =
3343 LIBC_NAMESPACE::sprintf(buff, "%hK", 0xffff); // unsigned short accum max
3344 ASSERT_STREQ_LEN(written, buff, "255.996094");
3345
3346 written = LIBC_NAMESPACE::sprintf(buff, "%hR", 0x0); // 0.0
3347 ASSERT_STREQ_LEN(written, buff, "0.000000");
3348
3349 written =
3350 LIBC_NAMESPACE::sprintf(buff, "%hR", 0xff); // unsigned short fract max
3351 ASSERT_STREQ_LEN(written, buff, "0.996094");
3352
3353 written = LIBC_NAMESPACE::sprintf(buff, "%lk", 0x0); // 0.0
3354 ASSERT_STREQ_LEN(written, buff, "0.000000");
3355
3356 written = LIBC_NAMESPACE::sprintf(buff, "%lk",
3357 0xffffffffffffffff); //-long accum max
3358 ASSERT_STREQ_LEN(written, buff, "-4294967296.000000");
3359
3360 written = LIBC_NAMESPACE::sprintf(buff, "%lr", 0x0); // 0.0
3361 ASSERT_STREQ_LEN(written, buff, "0.000000");
3362
3363 written = LIBC_NAMESPACE::sprintf(buff, "%lr",
3364 0xffffffff); //-long fract max
3365 ASSERT_STREQ_LEN(written, buff, "-1.000000");
3366
3367 written = LIBC_NAMESPACE::sprintf(buff, "%lK", 0x0); // 0.0
3368 ASSERT_STREQ_LEN(written, buff, "0.000000");
3369
3370 written =
3371 LIBC_NAMESPACE::sprintf(buff, "%lK",
3372 0xffffffffffffffff); // unsigned long accum max
3373 ASSERT_STREQ_LEN(written, buff, "4294967296.000000");
3374
3375 written = LIBC_NAMESPACE::sprintf(buff, "%lR", 0x0); // 0.0
3376 ASSERT_STREQ_LEN(written, buff, "0.000000");
3377
3378 written = LIBC_NAMESPACE::sprintf(buff, "%lR",
3379 0xffffffff); // unsigned long fract max
3380 ASSERT_STREQ_LEN(written, buff, "1.000000");
3381
3382 // Min Width Tests.
3383
3384 written = LIBC_NAMESPACE::sprintf(buff, "%10k", 0x0000a000); // 1.25
3385 ASSERT_STREQ_LEN(written, buff, " 1.250000");
3386
3387 written = LIBC_NAMESPACE::sprintf(buff, "%10k", 0x8000a000); //-1.25
3388 ASSERT_STREQ_LEN(written, buff, " -1.250000");
3389
3390 written = LIBC_NAMESPACE::sprintf(buff, "%8k", 0x0000a000); // 1.25
3391 ASSERT_STREQ_LEN(written, buff, "1.250000");
3392
3393 written = LIBC_NAMESPACE::sprintf(buff, "%9k", 0x8000a000); //-1.25
3394 ASSERT_STREQ_LEN(written, buff, "-1.250000");
3395
3396 written = LIBC_NAMESPACE::sprintf(buff, "%4k", 0x0000a000); // 1.25
3397 ASSERT_STREQ_LEN(written, buff, "1.250000");
3398
3399 written = LIBC_NAMESPACE::sprintf(buff, "%4k", 0x8000a000); //-1.25
3400 ASSERT_STREQ_LEN(written, buff, "-1.250000");
3401
3402 // Precision Tests.
3403
3404 written =
3405 LIBC_NAMESPACE::sprintf(buff, "%.16K", 0xFFFFFFFF); // unsigned accum max
3406 ASSERT_STREQ_LEN(written, buff, "65535.9999847412109375");
3407
3408 written = LIBC_NAMESPACE::sprintf(
3409 buff, "%.32lK", 0xFFFFFFFFFFFFFFFF); // unsigned long accum max
3410 ASSERT_STREQ_LEN(written, buff,
3411 "4294967295.99999999976716935634613037109375");
3412
3413 written =
3414 LIBC_NAMESPACE::sprintf(buff, "%.0K", 0xFFFFFFFF); // unsigned accum max
3415 ASSERT_STREQ_LEN(written, buff, "65536");
3416
3417 written = LIBC_NAMESPACE::sprintf(buff, "%.0R", 0xFFFF); // unsigned fract max
3418 ASSERT_STREQ_LEN(written, buff, "1");
3419
3420 // Flag Tests.
3421
3422 written = LIBC_NAMESPACE::sprintf(buff, "%+k", 0x0000a000); // 1.25
3423 ASSERT_STREQ_LEN(written, buff, "+1.250000");
3424
3425 written = LIBC_NAMESPACE::sprintf(buff, "%+k", 0x8000a000); //-1.25
3426 ASSERT_STREQ_LEN(written, buff, "-1.250000");
3427
3428 written = LIBC_NAMESPACE::sprintf(buff, "% k", 0x0000a000); // 1.25
3429 ASSERT_STREQ_LEN(written, buff, " 1.250000");
3430
3431 written = LIBC_NAMESPACE::sprintf(buff, "% k", 0x8000a000); //-1.25
3432 ASSERT_STREQ_LEN(written, buff, "-1.250000");
3433
3434 // unsigned variants ignore sign flags.
3435 written = LIBC_NAMESPACE::sprintf(buff, "%+K", 0x00014000); // 1.25
3436 ASSERT_STREQ_LEN(written, buff, "1.250000");
3437
3438 written = LIBC_NAMESPACE::sprintf(buff, "% K", 0x00014000); // 1.25
3439 ASSERT_STREQ_LEN(written, buff, "1.250000");
3440
3441 written = LIBC_NAMESPACE::sprintf(buff, "%-10k", 0x0000c000); // 1.5
3442 ASSERT_STREQ_LEN(written, buff, "1.500000 ");
3443
3444 written = LIBC_NAMESPACE::sprintf(buff, "%#.k", 0x00008000); // 1.0
3445 ASSERT_STREQ_LEN(written, buff, "1.");
3446
3447 written = LIBC_NAMESPACE::sprintf(buff, "%#.0k", 0x0000c000); // 1.5
3448 ASSERT_STREQ_LEN(written, buff, "2.");
3449
3450 written = LIBC_NAMESPACE::sprintf(buff, "%010k", 0x0000c000); // 1.5
3451 ASSERT_STREQ_LEN(written, buff, "001.500000");
3452
3453 written = LIBC_NAMESPACE::sprintf(buff, "%010k", 0x8000c000); //-1.5
3454 ASSERT_STREQ_LEN(written, buff, "-01.500000");
3455
3456 written = LIBC_NAMESPACE::sprintf(buff, "%+- #0k", 0); // 0.0
3457 ASSERT_STREQ_LEN(written, buff, "+0.000000");
3458
3459 // Combined Tests.
3460
3461 written = LIBC_NAMESPACE::sprintf(buff, "%10.2k", 0x0004feb8); // 9.99
3462 ASSERT_STREQ_LEN(written, buff, " 9.99");
3463
3464 written = LIBC_NAMESPACE::sprintf(buff, "%5.1k", 0x0004feb8); // 9.99
3465 ASSERT_STREQ_LEN(written, buff, " 10.0");
3466
3467 written = LIBC_NAMESPACE::sprintf(buff, "%-10.2k", 0x0004feb8); // 9.99
3468 ASSERT_STREQ_LEN(written, buff, "9.99 ");
3469
3470 written = LIBC_NAMESPACE::sprintf(buff, "%-5.1k", 0x0004feb8); // 9.99
3471 ASSERT_STREQ_LEN(written, buff, "10.0 ");
3472
3473 written = LIBC_NAMESPACE::sprintf(buff, "%-5.1k", 0x00000001); // accum min
3474 ASSERT_STREQ_LEN(written, buff, "0.0 ");
3475
3476 written = LIBC_NAMESPACE::sprintf(buff, "%30k", 0x7fffffff); // accum max
3477 ASSERT_STREQ_LEN(written, buff, " 65535.999969");
3478
3479 written = LIBC_NAMESPACE::sprintf(buff, "%-30k", 0x7fffffff); // accum max
3480 ASSERT_STREQ_LEN(written, buff, "65535.999969 ");
3481
3482 written = LIBC_NAMESPACE::sprintf(buff, "%20.2lK",
3483 0x3b9ac9ffFD70A3D7); // 999999999.99
3484 ASSERT_STREQ_LEN(written, buff, " 999999999.99");
3485
3486 written = LIBC_NAMESPACE::sprintf(buff, "%20.1lK",
3487 0x3b9ac9ffFD70A3D7); // 999999999.99
3488 ASSERT_STREQ_LEN(written, buff, " 1000000000.0");
3489
3490 written = LIBC_NAMESPACE::sprintf(buff, "%12.3R %-12.3k", 0x1999,
3491 0x00800000); // 0.1, 256.0
3492 ASSERT_STREQ_LEN(written, buff, " 0.100 256.000 ");
3493
3494 written =
3495 LIBC_NAMESPACE::sprintf(buff, "%+-#12.3lk % 012.3k", 0x000000001013a92a,
3496 0x02740000); // 0.126, 1256.0
3497 ASSERT_STREQ_LEN(written, buff, "+0.126 0001256.000");
3498}
3499#endif // defined(LIBC_COMPILER_HAS_FIXED_POINT) &&
3500 // !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT)
3501
3502#ifndef LIBC_COPT_PRINTF_DISABLE_WRITE_INT
3503TEST(LlvmLibcSPrintfTest, WriteIntConv) {
3504 char buff[64];
3505 int written;
3506 int test_val = -1;
3507
3508 test_val = -1;
3509 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "12345%n67890", &test_val);
3510 EXPECT_EQ(written, 10);
3511 EXPECT_EQ(test_val, 5);
3512 ASSERT_STREQ(buff, "1234567890");
3513
3514 test_val = -1;
3515 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%n", &test_val);
3516 EXPECT_EQ(written, 0);
3517 EXPECT_EQ(test_val, 0);
3518 ASSERT_STREQ(buff, "");
3519
3520 test_val = 0x100;
3521 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "ABC%hhnDEF", &test_val);
3522 EXPECT_EQ(written, 6);
3523 EXPECT_EQ(test_val, 0x103);
3524 ASSERT_STREQ(buff, "ABCDEF");
3525
3526 test_val = -1;
3527 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%s%n", "87654321", &test_val);
3528 EXPECT_EQ(written, 8);
3529 EXPECT_EQ(test_val, 8);
3530 ASSERT_STREQ(buff, "87654321");
3531
3532#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
3533 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "abc123%n", nullptr);
3534 EXPECT_LT(written, 0);
3535#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
3536}
3537#endif // LIBC_COPT_PRINTF_DISABLE_WRITE_INT
3538
3539#ifndef LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
3540TEST(LlvmLibcSPrintfTest, IndexModeParsing) {
3541 char buff[64];
3542 int written;
3543
3544 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%1$s", "abcDEF123");
3545 EXPECT_EQ(written, 9);
3546 ASSERT_STREQ(buff, "abcDEF123");
3547
3548 written = LIBC_NAMESPACE::sprintf(buffer: buff, format: "%1$s %%", "abcDEF123");
3549 EXPECT_EQ(written, 11);
3550 ASSERT_STREQ(buff, "abcDEF123 %");
3551
3552 written =
3553 LIBC_NAMESPACE::sprintf(buffer: buff, format: "%3$s %1$s %2$s", "is", "hard", "ordering");
3554 EXPECT_EQ(written, 16);
3555 ASSERT_STREQ(buff, "ordering is hard");
3556
3557 written = LIBC_NAMESPACE::sprintf(
3558 buffer: buff, format: "%10$s %9$s %8$c %7$s %6$s, %6$s %5$s %4$-*1$s %3$.*11$s %2$s. %%",
3559 6, "pain", "alphabetical", "such", "is", "this", "do", 'u', "would",
3560 "why", 1);
3561 EXPECT_EQ(written, 45);
3562 ASSERT_STREQ(buff, "why would u do this, this is such a pain. %");
3563}
3564#endif // LIBC_COPT_PRINTF_DISABLE_INDEX_MODE
3565

source code of libc/test/src/stdio/sprintf_test.cpp