Debian Codegraph @

tests/by-util/test_printf.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore fffffffffffffffc DFFF
7
8 use uutests::new_ucmd;
9
10 #[test]
11 fn basic_literal() {
12 new_ucmd!()
13 .args(&["hello world"])
14 .succeeds()
15 .stdout_only("hello world");
16 }
17
18 #[test]
19 fn test_missing_escaped_hex_value() {
20 new_ucmd!()
21 .arg(r"\x")
22 .fails_with_code(1)
23 .stderr_only("printf: missing hexadecimal number in escape\n");
24 }
25
26 #[test]
27 fn escaped_octal_and_newline() {
28 new_ucmd!()
29 .args(&["\\101\\0377\\n"])
30 .succeeds()
31 .stdout_only("A\x1F7\n");
32 }
33
34 #[test]
35 fn variable_sized_octal() {
36 for x in ["|\\5|", "|\\05|", "|\\005|"] {
37 new_ucmd!()
38 .arg(x)
39 .succeeds()
40 .stdout_only_bytes([b'|', 5u8, b'|']);
41 }
42
43 new_ucmd!()
44 .arg("|\\0005|")
45 .succeeds()
46 .stdout_only_bytes([b'|', 0, b'5', b'|']);
47 }
48
49 #[test]
50 fn escaped_unicode_four_digit() {
51 new_ucmd!().args(&["\\u0125"]).succeeds().stdout_only("ฤฅ");
52 }
53
54 #[test]
55 fn escaped_unicode_eight_digit() {
56 new_ucmd!()
57 .args(&["\\U00000125"])
58 .succeeds()
59 .stdout_only("ฤฅ");
60 }
61
62 #[test]
63 fn escaped_unicode_null_byte() {
64 new_ucmd!()
65 .args(&["\\0001_"])
66 .succeeds()
67 .stdout_is_bytes([0u8, b'1', b'_']);
68
69 new_ucmd!()
70 .args(&["%b", "\\0001_"])
71 .succeeds()
72 .stdout_is_bytes([1u8, b'_']);
73 }
74
75 #[test]
76 fn escaped_unicode_incomplete() {
77 for arg in ["\\u", "\\U", "\\uabc", "\\Uabcd"] {
78 new_ucmd!()
79 .arg(arg)
80 .fails_with_code(1)
81 .stderr_only("printf: missing hexadecimal number in escape\n");
82 }
83 }
84
85 #[test]
86 fn escaped_unicode_invalid() {
87 for arg in ["\\ud9d0", "\\U0000D8F9"] {
88 new_ucmd!()
89 .arg(arg)
90 .fails_with_code(1)
91 .stderr_only(format!("printf: invalid universal character name {arg}\n"));
92 }
93 }
94
95 #[test]
96 fn escaped_percent_sign() {
97 new_ucmd!()
98 .args(&["hello%% world"])
99 .succeeds()
100 .stdout_only("hello% world");
101 }
102
103 #[test]
104 fn escaped_unrecognized() {
105 new_ucmd!().args(&["c\\d"]).succeeds().stdout_only("c\\d");
106 }
107
108 #[test]
109 fn sub_b_string_handle_escapes() {
110 new_ucmd!()
111 .args(&["hello %b", "\\tworld"])
112 .succeeds()
113 .stdout_only("hello \tworld");
114 }
115
116 #[test]
117 fn sub_b_string_variable_size_unicode() {
118 for x in ["\\5|", "\\05|", "\\005|", "\\0005|"] {
119 new_ucmd!()
120 .args(&["|%b", x])
121 .succeeds()
122 .stdout_only_bytes([b'|', 5u8, b'|']);
123 }
124
125 new_ucmd!()
126 .args(&["|%b", "\\00005|"])
127 .succeeds()
128 .stdout_only_bytes([b'|', 0, b'5', b'|']);
129 }
130
131 #[test]
132 fn sub_b_string_validate_field_params() {
133 new_ucmd!()
134 .args(&["hello %7b", "world"])
135 .fails()
136 .stdout_is("hello ")
137 .stderr_is("printf: %7b: invalid conversion specification\n");
138 }
139
140 #[test]
141 fn sub_b_string_ignore_subs() {
142 new_ucmd!()
143 .args(&["hello %b", "world %% %i"])
144 .succeeds()
145 .stdout_only("hello world %% %i");
146 }
147
148 #[test]
149 fn sub_q_string_non_printable() {
150 new_ucmd!()
151 .args(&["non-printable: %q", "\"$test\""])
152 .succeeds()
153 .stdout_only("non-printable: '\"$test\"'");
154 }
155
156 #[test]
157 fn sub_q_string_validate_field_params() {
158 new_ucmd!()
159 .args(&["hello %7q", "world"])
160 .fails()
161 .stdout_is("hello ")
162 .stderr_is("printf: %7q: invalid conversion specification\n");
163 }
164
165 #[test]
166 fn sub_q_string_special_non_printable() {
167 new_ucmd!()
168 .args(&["non-printable: %q", "test~"])
169 .succeeds()
170 .stdout_only("non-printable: test~");
171 }
172
173 #[test]
174 fn sub_q_string_empty() {
175 new_ucmd!().args(&["%q", ""]).succeeds().stdout_only("''");
176 }
177
178 #[test]
179 fn sub_char() {
180 new_ucmd!()
181 .args(&["the letter %c", "A"])
182 .succeeds()
183 .stdout_only("the letter A");
184 }
185
186 #[test]
187 fn sub_char_from_string() {
188 new_ucmd!()
189 .args(&["%c%c%c", "five", "%", "oval"])
190 .succeeds()
191 .stdout_only("f%o");
192 }
193
194 #[test]
195 fn sub_num_int() {
196 new_ucmd!()
197 .args(&["twenty is %i", "20"])
198 .succeeds()
199 .stdout_only("twenty is 20");
200 }
201
202 #[test]
203 fn sub_num_int_min_width() {
204 new_ucmd!()
205 .args(&["twenty is %1i", "20"])
206 .succeeds()
207 .stdout_only("twenty is 20");
208 }
209
210 #[test]
211 fn sub_num_int_neg() {
212 new_ucmd!()
213 .args(&["neg. twenty is %i", "-20"])
214 .succeeds()
215 .stdout_only("neg. twenty is -20");
216 }
217
218 #[test]
219 fn sub_num_int_oct_in() {
220 new_ucmd!()
221 .args(&["twenty is %i", "024"])
222 .succeeds()
223 .stdout_only("twenty is 20");
224 }
225
226 #[test]
227 fn sub_num_int_oct_in_neg() {
228 new_ucmd!()
229 .args(&["neg. twenty is %i", "-024"])
230 .succeeds()
231 .stdout_only("neg. twenty is -20");
232 }
233
234 #[test]
235 fn sub_num_int_hex_in() {
236 new_ucmd!()
237 .args(&["twenty is %i", "0x14"])
238 .succeeds()
239 .stdout_only("twenty is 20");
240 }
241
242 #[test]
243 fn sub_num_int_hex_in_neg() {
244 new_ucmd!()
245 .args(&["neg. twenty is %i", "-0x14"])
246 .succeeds()
247 .stdout_only("neg. twenty is -20");
248 }
249
250 #[test]
251 fn sub_num_int_char_const_in() {
252 new_ucmd!()
253 .args(&["ninety seven is %i", "'a"])
254 .succeeds()
255 .stdout_only("ninety seven is 97");
256
257 new_ucmd!()
258 .args(&["emoji is %i", "'๐Ÿ™ƒ"])
259 .succeeds()
260 .stdout_only("emoji is 128579");
261
262 new_ucmd!()
263 .args(&["ninety seven is %i", "\"a"])
264 .succeeds()
265 .stdout_only("ninety seven is 97");
266
267 new_ucmd!()
268 .args(&["emoji is %i", "\"๐Ÿ™ƒ"])
269 .succeeds()
270 .stdout_only("emoji is 128579");
271 }
272
273 #[test]
274 fn sub_num_thousands() {
275 // For "C" locale, the thousands separator is ignored but should
276 // not result in an error
277 new_ucmd!()
278 .args(&["%'i", "123456"])
279 .succeeds()
280 .stdout_only("123456");
281 }
282
283 #[test]
284 fn sub_num_uint() {
285 new_ucmd!()
286 .args(&["twenty is %u", "20"])
287 .succeeds()
288 .stdout_only("twenty is 20");
289 }
290
291 #[test]
292 fn sub_num_octal() {
293 new_ucmd!()
294 .args(&["twenty in octal is %o", "20"])
295 .succeeds()
296 .stdout_only("twenty in octal is 24");
297 }
298
299 #[test]
300 fn sub_num_hex_lower() {
301 new_ucmd!()
302 .args(&["thirty in hex is %x", "30"])
303 .succeeds()
304 .stdout_only("thirty in hex is 1e");
305 }
306
307 #[test]
308 fn sub_num_hex_upper() {
309 new_ucmd!()
310 .args(&["thirty in hex is %X", "30"])
311 .succeeds()
312 .stdout_only("thirty in hex is 1E");
313 }
314
315 #[test]
316 fn sub_num_hex_non_numerical() {
317 new_ucmd!()
318 .args(&["parameters need to be numbers %X", "%194"])
319 .fails_with_code(1);
320 }
321
322 #[test]
323 fn sub_num_float() {
324 new_ucmd!()
325 .args(&["twenty is %f", "20"])
326 .succeeds()
327 .stdout_only("twenty is 20.000000");
328 }
329
330 #[test]
331 fn sub_num_float_e_round() {
332 new_ucmd!()
333 .args(&["%e", "99999999"])
334 .succeeds()
335 .stdout_only("1.000000e+08");
336 }
337
338 #[test]
339 fn sub_num_float_e_no_round() {
340 new_ucmd!()
341 .args(&["%e", "99999994"])
342 .succeeds()
343 .stdout_only("9.999999e+07");
344 }
345
346 #[test]
347 fn sub_num_float_round_to_one() {
348 new_ucmd!()
349 .args(&["one is %f", "0.9999995"])
350 .succeeds()
351 .stdout_only("one is 1.000000");
352 }
353
354 #[test]
355 #[ignore = "Requires 'long double' precision floats to be used internally"]
356 fn sub_num_float_round_to_two() {
357 new_ucmd!()
358 .args(&["two is %f", "1.9999995"])
359 .succeeds()
360 .stdout_only("two is 2.000000");
361 }
362
363 #[test]
364 fn sub_num_float_round_nines_dec() {
365 new_ucmd!()
366 .args(&["%f", "0.99999999"])
367 .succeeds()
368 .stdout_only("1.000000");
369 }
370
371 #[test]
372 fn sub_num_sci_lower() {
373 new_ucmd!()
374 .args(&["twenty is %e", "20"])
375 .succeeds()
376 .stdout_only("twenty is 2.000000e+01");
377 }
378
379 #[test]
380 fn sub_num_sci_upper() {
381 new_ucmd!()
382 .args(&["twenty is %E", "20"])
383 .succeeds()
384 .stdout_only("twenty is 2.000000E+01");
385 }
386
387 #[test]
388 fn sub_num_sci_trunc() {
389 new_ucmd!()
390 .args(&["pi is ~ %e", "3.1415926535"])
391 .succeeds()
392 .stdout_only("pi is ~ 3.141593e+00");
393 }
394
395 #[test]
396 fn sub_num_dec_trunc() {
397 new_ucmd!()
398 .args(&["pi is ~ %g", "3.1415926535"])
399 .succeeds()
400 .stdout_only("pi is ~ 3.14159");
401 }
402
403 #[test]
404 fn sub_num_sci_negative() {
405 new_ucmd!()
406 .args(&["-1234 is %e", "-1234"])
407 .succeeds()
408 .stdout_only("-1234 is -1.234000e+03");
409 }
410
411 #[test]
412 fn sub_num_hex_float_lower() {
413 new_ucmd!()
414 .args(&["%a", ".875"])
415 .succeeds()
416 .stdout_only("0xep-4");
417 }
418
419 #[test]
420 fn sub_num_hex_float_upper() {
421 new_ucmd!()
422 .args(&["%A", ".875"])
423 .succeeds()
424 .stdout_only("0XEP-4");
425 }
426
427 #[test]
428 fn sub_min_width() {
429 new_ucmd!()
430 .args(&["hello %7s", "world"])
431 .succeeds()
432 .stdout_only("hello world");
433 }
434
435 #[test]
436 fn sub_min_width_negative() {
437 new_ucmd!()
438 .args(&["hello %-7s", "world"])
439 .succeeds()
440 .stdout_only("hello world ");
441 }
442
443 #[test]
444 fn sub_string_char_width_above_u16_max_no_panic() {
445 // A %s/%c field width above u16::MAX must not panic (#12593, #12900).
446 new_ucmd!()
447 .args(&["%100000c", "A"])
448 .succeeds()
449 .stdout_only(format!("{}A", " ".repeat(99999)));
450 new_ucmd!()
451 .args(&["%-100000s", "hi"])
452 .succeeds()
453 .stdout_only(format!("hi{}", " ".repeat(99998)));
454 }
455
456 #[test]
457 fn sub_str_max_chars_input() {
458 new_ucmd!()
459 .args(&["hello %7.2s", "world"])
460 .succeeds()
461 .stdout_only("hello wo");
462 }
463
464 #[test]
465 fn sub_int_decimal() {
466 new_ucmd!()
467 .args(&["%0.i", "11"])
468 .succeeds()
469 .stdout_only("11");
470 }
471
472 #[test]
473 fn sub_int_leading_zeroes() {
474 new_ucmd!()
475 .args(&["%.4i", "11"])
476 .succeeds()
477 .stdout_only("0011");
478 }
479
480 #[test]
481 fn sub_int_leading_zeroes_padded() {
482 new_ucmd!()
483 .args(&["%5.4i", "11"])
484 .succeeds()
485 .stdout_only(" 0011");
486 }
487
488 #[test]
489 fn sub_float_dec_places() {
490 new_ucmd!()
491 .args(&["pi is ~ %.11f", "3.1415926535"])
492 .succeeds()
493 .stdout_only("pi is ~ 3.14159265350");
494 }
495
496 #[test]
497 fn sub_float_hex_in() {
498 new_ucmd!()
499 .args(&["%f", "0xF1.1F"])
500 .succeeds()
501 .stdout_only("241.121094");
502 }
503
504 #[test]
505 fn sub_float_no_octal_in() {
506 new_ucmd!()
507 .args(&["%f", "077"])
508 .succeeds()
509 .stdout_only("77.000000");
510 }
511
512 #[test]
513 fn sub_any_asterisk_first_param() {
514 new_ucmd!()
515 .args(&["%*i", "3", "11", "4", "12"])
516 .succeeds()
517 .stdout_only(" 11 12");
518 }
519
520 #[test]
521 fn sub_any_asterisk_second_param() {
522 new_ucmd!()
523 .args(&["%.*i", "3", "11", "4", "12"])
524 .succeeds()
525 .stdout_only("0110012");
526 }
527
528 #[test]
529 fn sub_any_asterisk_both_params() {
530 new_ucmd!()
531 .args(&["%*.*i", "4", "3", "11", "5", "4", "12"])
532 .succeeds()
533 .stdout_only(" 011 0012");
534 }
535
536 #[test]
537 fn sub_any_asterisk_octal_arg() {
538 new_ucmd!()
539 .args(&["%.*i", "011", "12345678"])
540 .succeeds()
541 .stdout_only("012345678");
542 }
543
544 #[test]
545 fn sub_any_asterisk_hex_arg() {
546 new_ucmd!()
547 .args(&["%.*i", "0xA", "123456789"])
548 .succeeds()
549 .stdout_only("0123456789");
550 }
551
552 #[test]
553 fn sub_any_asterisk_negative_first_param() {
554 new_ucmd!()
555 .args(&["a(%*s)b", "-5", "xyz"])
556 .succeeds()
557 .stdout_only("a(xyz )b"); // Would be 'a( xyz)b' if -5 was 5
558
559 // Negative octal
560 new_ucmd!()
561 .args(&["a(%*s)b", "-010", "xyz"])
562 .succeeds()
563 .stdout_only("a(xyz )b");
564
565 // Negative hexadecimal
566 new_ucmd!()
567 .args(&["a(%*s)b", "-0x10", "xyz"])
568 .succeeds()
569 .stdout_only("a(xyz )b");
570
571 // Should also work on %c
572 new_ucmd!()
573 .args(&["a(%*c)b", "-5", "x"])
574 .succeeds()
575 .stdout_only("a(x )b"); // Would be 'a( x)b' if -5 was 5
576 }
577
578 #[test]
579 fn sub_any_asterisk_first_param_with_integer() {
580 new_ucmd!()
581 .args(&["|%*d|", "3", "0"])
582 .succeeds()
583 .stdout_only("| 0|");
584
585 new_ucmd!()
586 .args(&["|%*d|", "1", "0"])
587 .succeeds()
588 .stdout_only("|0|");
589
590 new_ucmd!()
591 .args(&["|%*d|", "0", "0"])
592 .succeeds()
593 .stdout_only("|0|");
594
595 new_ucmd!()
596 .args(&["|%*d|", "-1", "0"])
597 .succeeds()
598 .stdout_only("|0|");
599
600 // Negative widths are left-aligned
601 new_ucmd!()
602 .args(&["|%*d|", "-3", "0"])
603 .succeeds()
604 .stdout_only("|0 |");
605 }
606
607 #[test]
608 fn sub_any_asterisk_second_param_with_integer() {
609 new_ucmd!()
610 .args(&["|%.*d|", "3", "10"])
611 .succeeds()
612 .stdout_only("|010|");
613
614 new_ucmd!()
615 .args(&["|%*.d|", "1", "10"])
616 .succeeds()
617 .stdout_only("|10|");
618
619 new_ucmd!()
620 .args(&["|%.*d|", "0", "10"])
621 .succeeds()
622 .stdout_only("|10|");
623
624 new_ucmd!()
625 .args(&["|%.*d|", "-1", "10"])
626 .succeeds()
627 .stdout_only("|10|");
628
629 new_ucmd!()
630 .args(&["|%.*d|", "-2", "10"])
631 .succeeds()
632 .stdout_only("|10|");
633
634 new_ucmd!()
635 .args(&["|%.*d|", &i64::MIN.to_string(), "10"])
636 .succeeds()
637 .stdout_only("|10|");
638
639 new_ucmd!()
640 .args(&["|%.*d|", &format!("-{}", u128::MAX), "10"])
641 .fails_with_code(1)
642 .stdout_is("|10|")
643 .stderr_is(
644 "printf: '-340282366920938463463374607431768211455': Numerical result out of range\n",
645 );
646 }
647
648 #[test]
649 fn sub_any_specifiers() {
650 // spell-checker:disable-next-line
651 for format in ["%ztlhLji", "%0ztlhLji", "%0.ztlhLji"] {
652 new_ucmd!().args(&[format, "3"]).succeeds().stdout_only("3");
653 }
654 }
655
656 #[test]
657 fn unspecified_left_justify_is_1_width() {
658 new_ucmd!().args(&["%-o"]).succeeds().stdout_only("0");
659 }
660
661 #[test]
662 fn sub_any_specifiers_after_second_param() {
663 new_ucmd!()
664 .args(&["%0.0ztlhLji", "3"]) //spell-checker:disable-line
665 .succeeds()
666 .stdout_only("3");
667 }
668
669 #[test]
670 fn stop_after_additional_escape() {
671 new_ucmd!()
672 .args(&["A%sC\\cD%sF", "B", "E"])
673 .succeeds()
674 .stdout_only("ABC");
675 }
676
677 #[test]
678 fn stop_after_additional_escape_in_b_string() {
679 // A `\c` inside a %b argument must stop the entire invocation, not just
680 // that argument's own expansion.
681 new_ucmd!()
682 .args(&["A%bB\\n", "x\\cy"])
683 .succeeds()
684 .stdout_only("Ax");
685 }
686
687 #[test]
688 fn sub_float_leading_zeroes() {
689 new_ucmd!()
690 .args(&["%010f", "1"])
691 .succeeds()
692 .stdout_only("001.000000");
693 }
694
695 #[test]
696 fn sub_general_float() {
697 new_ucmd!()
698 .args(&["%g", "1.1"])
699 .succeeds()
700 .stdout_only("1.1");
701 }
702
703 #[test]
704 fn sub_general_truncate_to_integer() {
705 new_ucmd!().args(&["%g", "1.0"]).succeeds().stdout_only("1");
706 }
707
708 #[test]
709 fn sub_general_scientific_notation() {
710 new_ucmd!()
711 .args(&["%g", "1000010"])
712 .succeeds()
713 .stdout_only("1.00001e+06");
714 }
715
716 #[test]
717 fn sub_general_round_scientific_notation() {
718 new_ucmd!()
719 .args(&["%g", "123456789"])
720 .succeeds()
721 .stdout_only("1.23457e+08");
722 }
723
724 #[test]
725 fn sub_general_round_float() {
726 new_ucmd!()
727 .args(&["%g", "12345.6789"])
728 .succeeds()
729 .stdout_only("12345.7");
730 }
731
732 #[test]
733 fn sub_general_round_float_to_integer() {
734 new_ucmd!()
735 .args(&["%g", "123456.7"])
736 .succeeds()
737 .stdout_only("123457");
738 }
739
740 #[test]
741 fn sub_general_round_float_leading_zeroes() {
742 new_ucmd!()
743 .args(&["%g", "1.000009"])
744 .succeeds()
745 .stdout_only("1.00001");
746 }
747
748 #[test]
749 fn partial_float() {
750 new_ucmd!()
751 .args(&["%.2f is %s", "42.03x", "a lot"])
752 .fails_with_code(1)
753 .stdout_is("42.03 is a lot")
754 .stderr_is("printf: '42.03x': value not completely converted\n");
755 }
756
757 #[test]
758 fn partial_integer() {
759 new_ucmd!()
760 .args(&["%d is %s", "42x23", "a lot"])
761 .fails_with_code(1)
762 .stdout_is("42 is a lot")
763 .stderr_is("printf: '42x23': value not completely converted\n");
764
765 new_ucmd!()
766 .args(&["%d is not %s", "0xwa", "a lot"])
767 .fails_with_code(1)
768 .stdout_is("0 is not a lot")
769 .stderr_is("printf: '0xwa': value not completely converted\n");
770 }
771
772 #[test]
773 fn unsigned_hex_negative_wraparound() {
774 new_ucmd!()
775 .args(&["%x", "-0b100"])
776 .succeeds()
777 .stdout_only("fffffffffffffffc");
778
779 new_ucmd!()
780 .args(&["%x", "-0100"])
781 .succeeds()
782 .stdout_only("ffffffffffffffc0");
783
784 new_ucmd!()
785 .args(&["%x", "-100"])
786 .succeeds()
787 .stdout_only("ffffffffffffff9c");
788
789 new_ucmd!()
790 .args(&["%x", "-0x100"])
791 .succeeds()
792 .stdout_only("ffffffffffffff00");
793
794 new_ucmd!()
795 .args(&["%x", "-92233720368547758150"])
796 .fails_with_code(1)
797 .stdout_is("ffffffffffffffff")
798 .stderr_is("printf: '-92233720368547758150': Numerical result out of range\n");
799
800 new_ucmd!()
801 .args(&["%u", "-1002233720368547758150"])
802 .fails_with_code(1)
803 .stdout_is("18446744073709551615")
804 .stderr_is("printf: '-1002233720368547758150': Numerical result out of range\n");
805 }
806
807 #[test]
808 fn test_overflow() {
809 new_ucmd!()
810 .args(&["%d", "36893488147419103232"])
811 .fails_with_code(1)
812 .stdout_is("9223372036854775807")
813 .stderr_is("printf: '36893488147419103232': Numerical result out of range\n");
814
815 new_ucmd!()
816 .args(&["%d", "-36893488147419103232"])
817 .fails_with_code(1)
818 .stdout_is("-9223372036854775808")
819 .stderr_is("printf: '-36893488147419103232': Numerical result out of range\n");
820
821 new_ucmd!()
822 .args(&["%u", "36893488147419103232"])
823 .fails_with_code(1)
824 .stdout_is("18446744073709551615")
825 .stderr_is("printf: '36893488147419103232': Numerical result out of range\n");
826 }
827
828 #[test]
829 fn test_extreme_exponent_does_not_overflow() {
830 // A value whose decimal exponent does not fit in an i32 used to overflow the
831 // exponent arithmetic in the float formatters (panic in debug, wrong value or
832 // bad allocation in release). GNU printf treats such a magnitude the same
833 // way it would treat any other out-of-range float: it reports the overflow
834 // on stderr but still prints inf (or 0, on underflow) to stdout.
835 for spec in ["%a", "%e", "%g", "%f"] {
836 new_ucmd!()
837 .args(&[spec, "5e8123456789012345678"])
838 .fails_with_code(1)
839 .stderr_contains("Numerical result out of range")
840 .stdout_contains("inf");
841
842 let zero = match spec {
843 "%a" => "0x0p+0",
844 "%e" => "0.000000e+00",
845 "%g" => "0",
846 "%f" => "0.000000",
847 _ => unreachable!(),
848 };
849 new_ucmd!()
850 .args(&[spec, "7E-8123456789012345678"])
851 .fails_with_code(1)
852 .stderr_contains("Numerical result out of range")
853 .stdout_is(zero);
854 }
855 }
856
857 #[test]
858 fn partial_char() {
859 new_ucmd!()
860 .args(&["%d", "'abc"])
861 .succeeds()
862 .stdout_is("97")
863 .stderr_is(
864 "printf: warning: bc: character(s) following character constant have been ignored\n",
865 );
866 }
867
868 #[test]
869 fn partial_char_posixly_correct() {
870 // GNU suppresses the "character(s) following character constant" warning
871 // when POSIXLY_CORRECT is set. Only the presence of the variable matters,
872 // its value is irrelevant.
873 for value in ["1", "", "0"] {
874 for arg in ["'AB", "'ABC", "\"AB", "'-1"] {
875 new_ucmd!()
876 .args(&["%d", arg])
877 .env("POSIXLY_CORRECT", value)
878 .succeeds()
879 .no_stderr();
880 }
881 }
882
883 // Without POSIXLY_CORRECT the warning is still emitted.
884 for (arg, rest) in [("'AB", "B"), ("'ABC", "BC"), ("\"AB", "B"), ("'-1", "1")] {
885 new_ucmd!().args(&["%d", arg]).succeeds().stderr_is(format!(
886 "printf: warning: {rest}: character(s) following character constant have been ignored\n"
887 ));
888 }
889 }
890
891 #[test]
892 fn value_not_completely_converted_ignores_posixly_correct() {
893 // POSIXLY_CORRECT only silences the character-constant warning; the
894 // unrelated "value not completely converted" error is unaffected.
895 new_ucmd!()
896 .args(&["%d", "42abc"])
897 .env("POSIXLY_CORRECT", "1")
898 .fails_with_code(1)
899 .stderr_contains("value not completely converted");
900 }
901
902 #[test]
903 fn sub_alternative_lower_hex_0() {
904 new_ucmd!().args(&["%#x", "0"]).succeeds().stdout_only("0");
905 }
906
907 #[test]
908 fn sub_alternative_lower_hex() {
909 new_ucmd!()
910 .args(&["%#x", "42"])
911 .succeeds()
912 .stdout_only("0x2a");
913 }
914
915 #[test]
916 fn sub_alternative_upper_hex_0() {
917 new_ucmd!().args(&["%#X", "0"]).succeeds().stdout_only("0");
918 }
919
920 #[test]
921 fn sub_alternative_upper_hex() {
922 new_ucmd!()
923 .args(&["%#X", "42"])
924 .succeeds()
925 .stdout_only("0X2A");
926 }
927
928 #[test]
929 fn char_as_byte() {
930 new_ucmd!()
931 .args(&["%c", "๐Ÿ™ƒ"])
932 .succeeds()
933 .no_stderr()
934 .stdout_is_bytes(b"\xf0");
935 }
936
937 #[test]
938 fn no_infinite_loop() {
939 new_ucmd!()
940 .args(&["a", "b"])
941 .succeeds()
942 .stdout_is("a")
943 .stderr_contains("warning: ignoring excess arguments, starting with 'b'");
944 }
945
946 #[test]
947 fn pad_octal_with_prefix() {
948 new_ucmd!()
949 .args(&[">%#15.6o<", "0"])
950 .succeeds()
951 .stdout_only("> 000000<");
952
953 new_ucmd!()
954 .args(&[">%#15.6o<", "01"])
955 .succeeds()
956 .stdout_only("> 000001<");
957
958 new_ucmd!()
959 .args(&[">%#15.6o<", "01234"])
960 .succeeds()
961 .stdout_only("> 001234<");
962
963 new_ucmd!()
964 .args(&[">%#15.6o<", "012345"])
965 .succeeds()
966 .stdout_only("> 012345<");
967
968 new_ucmd!()
969 .args(&[">%#15.6o<", "0123456"])
970 .succeeds()
971 .stdout_only("> 0123456<");
972 }
973
974 #[test]
975 fn pad_unsigned_zeroes() {
976 for format in ["%.3u", "%.3x", "%.3X", "%.3o"] {
977 new_ucmd!()
978 .args(&[format, "0"])
979 .succeeds()
980 .stdout_only("000");
981 }
982 }
983
984 #[test]
985 fn pad_unsigned_three() {
986 for (format, expected) in [
987 ("%.3u", "003"),
988 ("%.3x", "003"),
989 ("%.3X", "003"),
990 ("%.3o", "003"),
991 ("%#.3x", "0x003"),
992 ("%#.3X", "0X003"),
993 ("%#.3o", "003"),
994 ("%#05x", "0x003"),
995 ("%#05X", "0X003"),
996 ("%3x", " 3"),
997 ("%3X", " 3"),
998 ] {
999 new_ucmd!()
1000 .args(&[format, "3"])
1001 .succeeds()
1002 .stdout_only(expected);
1003 }
1004 }
1005
1006 #[test]
1007 fn pad_char() {
1008 for (format, expected) in [("%3c", " X"), ("%1c", "X"), ("%-1c", "X"), ("%-3c", "X ")] {
1009 new_ucmd!()
1010 .args(&[format, "X"])
1011 .succeeds()
1012 .stdout_only(expected);
1013 }
1014 }
1015
1016 #[test]
1017 fn pad_string() {
1018 for (format, expected) in [
1019 ("%8s", " bottle"),
1020 ("%-8s", "bottle "),
1021 ("%6s", "bottle"),
1022 ("%-6s", "bottle"),
1023 ] {
1024 new_ucmd!()
1025 .args(&[format, "bottle"])
1026 .succeeds()
1027 .stdout_only(expected);
1028 }
1029 }
1030
1031 #[test]
1032 fn format_spec_zero_fails() {
1033 // It is invalid to have the format spec
1034 for format in ["%0c", "%0s"] {
1035 new_ucmd!().args(&[format, "3"]).fails_with_code(1);
1036 }
1037 }
1038
1039 #[test]
1040 fn invalid_precision_tests() {
1041 // It is invalid to have length of output string greater than i32::MAX
1042 for format in ["%.*d", "%.*f"] {
1043 let expected_error = "printf: invalid precision: '2147483648'\n";
1044 new_ucmd!()
1045 .args(&[format, "2147483648", "0"])
1046 .fails()
1047 .stderr_is(expected_error);
1048 }
1049 }
1050
1051 // The following padding-tests test for the cases in which flags in ['0', ' '] are given.
1052 // For integer, only try to pad when no precision is given, while
1053 // for float, always try to pad
1054 #[test]
1055 fn space_padding_with_space_test() {
1056 // Check if printf gives an extra space in the beginning
1057 new_ucmd!()
1058 .args(&["% 3d", "1"])
1059 .succeeds()
1060 .stdout_only(" 1");
1061 }
1062
1063 #[test]
1064 fn zero_padding_with_space_test() {
1065 new_ucmd!()
1066 .args(&["% 03d", "1"])
1067 .succeeds()
1068 .stdout_only(" 01");
1069 }
1070
1071 #[test]
1072 fn zero_padding_with_plus_test() {
1073 new_ucmd!()
1074 .args(&["%+04d", "1"])
1075 .succeeds()
1076 .stdout_only("+001");
1077 }
1078
1079 #[test]
1080 fn negative_zero_padding_test() {
1081 new_ucmd!()
1082 .args(&["%03d", "-1"])
1083 .succeeds()
1084 .stdout_only("-01");
1085 }
1086
1087 #[test]
1088 fn negative_zero_padding_with_space_test() {
1089 new_ucmd!()
1090 .args(&["% 03d", "-1"])
1091 .succeeds()
1092 .stdout_only("-01");
1093 }
1094
1095 #[test]
1096 fn spaces_before_numbers_are_ignored() {
1097 new_ucmd!()
1098 .args(&["%*.*d", " 5", " 3", " 6"])
1099 .succeeds()
1100 .stdout_only(" 006");
1101 }
1102
1103 #[test]
1104 fn float_with_zero_precision_should_pad() {
1105 new_ucmd!()
1106 .args(&["%03.0f", "-1"])
1107 .succeeds()
1108 .stdout_only("-01");
1109 }
1110
1111 #[test]
1112 fn int_with_zero_precision_and_zero_value() {
1113 new_ucmd!().args(&["%.0d", "0"]).succeeds().stdout_only("");
1114 new_ucmd!()
1115 .args(&["%#.0o", "0"])
1116 .succeeds()
1117 .stdout_only("0");
1118 // A negative `*` precision counts as no precision at all.
1119 new_ucmd!()
1120 .args(&["%.*d", "-1", "0"])
1121 .succeeds()
1122 .stdout_only("0");
1123 }
1124
1125 #[test]
1126 fn float_non_finite() {
1127 new_ucmd!()
1128 .args(&[
1129 "%f %f %F %f %f %F",
1130 "nan",
1131 "-nan",
1132 "nan",
1133 "inf",
1134 "-inf",
1135 "inf",
1136 ])
1137 .succeeds()
1138 .stdout_only("nan -nan NAN inf -inf INF");
1139 }
1140
1141 #[test]
1142 fn float_zero_neg_zero() {
1143 new_ucmd!()
1144 .args(&["%f %f", "0.0", "-0.0"])
1145 .succeeds()
1146 .stdout_only("0.000000 -0.000000");
1147 }
1148
1149 #[test]
1150 fn precision_check() {
1151 new_ucmd!()
1152 .args(&["%.3d", "1"])
1153 .succeeds()
1154 .stdout_only("001");
1155 }
1156
1157 #[test]
1158 fn space_padding_with_precision() {
1159 new_ucmd!()
1160 .args(&["%4.3d", "1"])
1161 .succeeds()
1162 .stdout_only(" 001");
1163 }
1164
1165 #[test]
1166 fn float_zero_padding_with_precision() {
1167 new_ucmd!()
1168 .args(&["%04.1f", "1"])
1169 .succeeds()
1170 .stdout_only("01.0");
1171 }
1172
1173 #[test]
1174 fn float_space_padding_with_precision() {
1175 new_ucmd!()
1176 .args(&["%4.1f", "1"])
1177 .succeeds()
1178 .stdout_only(" 1.0");
1179 }
1180
1181 #[test]
1182 fn negative_float_zero_padding_with_precision() {
1183 new_ucmd!()
1184 .args(&["%05.1f", "-1"])
1185 .succeeds()
1186 .stdout_only("-01.0");
1187 }
1188
1189 #[test]
1190 fn float_default_precision_space_padding() {
1191 new_ucmd!()
1192 .args(&["%10f", "1"])
1193 .succeeds()
1194 .stdout_only(" 1.000000");
1195 }
1196
1197 #[test]
1198 fn float_default_precision_zero_padding() {
1199 new_ucmd!()
1200 .args(&["%010f", "1"])
1201 .succeeds()
1202 .stdout_only("001.000000");
1203 }
1204
1205 #[test]
1206 fn flag_position_space_padding() {
1207 new_ucmd!()
1208 .args(&["% +3.1d", "1"])
1209 .succeeds()
1210 .stdout_only(" +1");
1211 }
1212
1213 #[test]
1214 fn float_flag_position_space_padding() {
1215 new_ucmd!()
1216 .args(&["% +5.1f", "1"])
1217 .succeeds()
1218 .stdout_only(" +1.0");
1219 }
1220
1221 #[test]
1222 fn float_large_precision() {
1223 // Note: This does not match GNU coreutils output (0.100000000000000000001355252716 on x86),
1224 // as we parse and format using ExtendedBigDecimal, which provides arbitrary precision.
1225 new_ucmd!()
1226 .args(&["%.30f", "0.1"])
1227 .succeeds()
1228 .stdout_only("0.100000000000000000000000000000");
1229 }
1230
1231 #[test]
1232 fn float_non_finite_space_padding() {
1233 new_ucmd!()
1234 .args(&["% 5.2f|% 5.2f|% 5.2f|% 5.2f", "inf", "-inf", "nan", "-nan"])
1235 .succeeds()
1236 .stdout_only(" inf| -inf| nan| -nan");
1237 }
1238
1239 #[test]
1240 fn float_non_finite_zero_padding() {
1241 // Zero-padding pads non-finite numbers with spaces.
1242 new_ucmd!()
1243 .args(&["%05.2f|%05.2f|%05.2f|%05.2f", "inf", "-inf", "nan", "-nan"])
1244 .succeeds()
1245 .stdout_only(" inf| -inf| nan| -nan");
1246 }
1247
1248 #[test]
1249 fn float_abs_value_less_than_one() {
1250 new_ucmd!()
1251 .args(&["%g", "0.1171875"])
1252 .succeeds()
1253 .stdout_only("0.117188");
1254
1255 // The original value from #7031 issue
1256 new_ucmd!()
1257 .args(&["%g", "-0.1171875"])
1258 .succeeds()
1259 .stdout_only("-0.117188");
1260
1261 new_ucmd!()
1262 .args(&["%g", "0.01171875"])
1263 .succeeds()
1264 .stdout_only("0.0117188");
1265
1266 new_ucmd!()
1267 .args(&["%g", "-0.01171875"])
1268 .succeeds()
1269 .stdout_only("-0.0117188");
1270
1271 new_ucmd!()
1272 .args(&["%g", "0.001171875001"])
1273 .succeeds()
1274 .stdout_only("0.00117188");
1275
1276 new_ucmd!()
1277 .args(&["%g", "-0.001171875001"])
1278 .succeeds()
1279 .stdout_only("-0.00117188");
1280 }
1281
1282 #[test]
1283 fn float_switch_switch_decimal_scientific() {
1284 new_ucmd!()
1285 .args(&["%g", "0.0001"])
1286 .succeeds()
1287 .stdout_only("0.0001");
1288
1289 new_ucmd!()
1290 .args(&["%g", "0.00001"])
1291 .succeeds()
1292 .stdout_only("1e-05");
1293 }
1294
1295 #[test]
1296 fn float_arg_zero() {
1297 new_ucmd!()
1298 .args(&["%f", "0."])
1299 .succeeds()
1300 .stdout_only("0.000000");
1301
1302 new_ucmd!()
1303 .args(&["%f", ".0"])
1304 .succeeds()
1305 .stdout_only("0.000000");
1306
1307 new_ucmd!()
1308 .args(&["%f", ".0e100000"])
1309 .succeeds()
1310 .stdout_only("0.000000");
1311 }
1312
1313 #[test]
1314 fn float_arg_invalid() {
1315 // Just a dot fails.
1316 new_ucmd!()
1317 .args(&["%f", "."])
1318 .fails()
1319 .stdout_is("0.000000")
1320 .stderr_contains("expected a numeric value");
1321
1322 new_ucmd!()
1323 .args(&["%f", "-."])
1324 .fails()
1325 .stdout_is("0.000000")
1326 .stderr_contains("expected a numeric value");
1327
1328 // Just an exponent indicator fails.
1329 new_ucmd!()
1330 .args(&["%f", "e"])
1331 .fails()
1332 .stdout_is("0.000000")
1333 .stderr_contains("expected a numeric value");
1334
1335 // No digit but only exponent fails
1336 new_ucmd!()
1337 .args(&["%f", ".e12"])
1338 .fails()
1339 .stdout_is("0.000000")
1340 .stderr_contains("expected a numeric value");
1341
1342 // No exponent partially fails
1343 new_ucmd!()
1344 .args(&["%f", "123e"])
1345 .fails()
1346 .stdout_is("123.000000")
1347 .stderr_contains("value not completely converted");
1348
1349 // Nothing past `0x` parses as zero
1350 new_ucmd!()
1351 .args(&["%f", "0x"])
1352 .fails()
1353 .stdout_is("0.000000")
1354 .stderr_contains("value not completely converted");
1355
1356 new_ucmd!()
1357 .args(&["%f", "0x."])
1358 .fails()
1359 .stdout_is("0.000000")
1360 .stderr_contains("value not completely converted");
1361
1362 new_ucmd!()
1363 .args(&["%f", "0xp12"])
1364 .fails()
1365 .stdout_is("0.000000")
1366 .stderr_contains("value not completely converted");
1367 }
1368
1369 #[test]
1370 fn float_arg_with_whitespace() {
1371 new_ucmd!()
1372 .args(&["%f", " \u{0020}\u{000d}\t\n0.000001"])
1373 .succeeds()
1374 .stdout_only("0.000001");
1375
1376 new_ucmd!()
1377 .args(&["%f", "0.1 "])
1378 .fails()
1379 .stderr_contains("value not completely converted");
1380
1381 // Unicode whitespace should not be allowed in a number
1382 new_ucmd!()
1383 .args(&["%f", "\u{2029}0.1"])
1384 .fails()
1385 .stderr_contains("expected a numeric value");
1386
1387 // An input string with a whitespace special character that has
1388 // not already been expanded should fail.
1389 new_ucmd!()
1390 .args(&["%f", "\\t0.1"])
1391 .fails()
1392 .stderr_contains("expected a numeric value");
1393 }
1394
1395 #[test]
1396 fn mb_input() {
1397 let cases = vec![
1398 ("%04x\n", "\"รก", "00e1\n"),
1399 ("%04x\n", "'รก", "00e1\n"),
1400 ("%04x\n", "'\u{e1}", "00e1\n"),
1401 ("%i\n", "\"รก", "225\n"),
1402 ("%i\n", "'รก", "225\n"),
1403 ("%i\n", "'\u{e1}", "225\n"),
1404 ("%f\n", "'รก", "225.000000\n"),
1405 ];
1406 for (format, arg, stdout) in cases {
1407 new_ucmd!()
1408 .args(&[format, arg])
1409 .succeeds()
1410 .stdout_only(stdout);
1411 }
1412
1413 let cases = vec![
1414 ("%04x\n", "\"รก=", "00e1\n", "="),
1415 ("%04x\n", "'รก-", "00e1\n", "-"),
1416 ("%04x\n", "'รก=-==", "00e1\n", "=-=="),
1417 ("%04x\n", "'รก'", "00e1\n", "'"),
1418 ("%04x\n", "'\u{e1}++", "00e1\n", "++"),
1419 ("%04x\n", "''รก'", "0027\n", "รก'"),
1420 ("%i\n", "\"รก=", "225\n", "="),
1421 ];
1422 for (format, arg, stdout, stderr) in cases {
1423 new_ucmd!()
1424 .args(&[format, arg])
1425 .succeeds()
1426 .stdout_is(stdout)
1427 .stderr_is(format!("printf: warning: {stderr}: character(s) following character constant have been ignored\n"));
1428 }
1429
1430 for arg in ["\"", "'"] {
1431 new_ucmd!()
1432 .args(&["%04x\n", arg])
1433 .fails()
1434 .stderr_contains("expected a numeric value");
1435 }
1436 }
1437
1438 #[test]
1439 #[cfg(target_family = "unix")]
1440 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
1441 fn mb_invalid_unicode() {
1442 use std::ffi::OsStr;
1443 use std::os::unix::ffi::OsStrExt;
1444
1445 let cases = vec![
1446 ("%04x\n", b"\"\xe1", "00e1\n"),
1447 ("%04x\n", b"'\xe1", "00e1\n"),
1448 ("%i\n", b"\"\xe1", "225\n"),
1449 ("%i\n", b"'\xe1", "225\n"),
1450 ("%f\n", b"'\xe1", "225.000000\n"),
1451 ];
1452 for (format, arg, stdout) in cases {
1453 new_ucmd!()
1454 .arg(format)
1455 .arg(OsStr::from_bytes(arg))
1456 .succeeds()
1457 .stdout_only(stdout);
1458 }
1459
1460 let cases = vec![
1461 (b"\"\xe1=".as_slice(), "="),
1462 (b"'\xe1-".as_slice(), "-"),
1463 (b"'\xe1=-==".as_slice(), "=-=="),
1464 (b"'\xe1'".as_slice(), "'"),
1465 // unclear if original or replacement character is better in stderr
1466 //(b"''\xe1'".as_slice(), "'๏ฟฝ'"),
1467 ];
1468 for (arg, expected) in cases {
1469 new_ucmd!()
1470 .arg("%04x\n")
1471 .arg(OsStr::from_bytes(arg))
1472 .succeeds()
1473 .stdout_is("00e1\n")
1474 .stderr_is(format!("printf: warning: {expected}: character(s) following character constant have been ignored\n"));
1475 }
1476 }
1477
1478 #[test]
1479 fn positional_format_specifiers() {
1480 new_ucmd!()
1481 .args(&["%1$d%d-", "5", "10", "6", "20"])
1482 .succeeds()
1483 .stdout_only("55-1010-66-2020-");
1484
1485 new_ucmd!()
1486 .args(&["%2$d%d-", "5", "10", "6", "20"])
1487 .succeeds()
1488 .stdout_only("105-206-");
1489
1490 new_ucmd!()
1491 .args(&["%3$d%d-", "5", "10", "6", "20"])
1492 .succeeds()
1493 .stdout_only("65-020-");
1494
1495 new_ucmd!()
1496 .args(&["%4$d%d-", "5", "10", "6", "20"])
1497 .succeeds()
1498 .stdout_only("205-");
1499
1500 new_ucmd!()
1501 .args(&["%5$d%d-", "5", "10", "6", "20"])
1502 .succeeds()
1503 .stdout_only("05-");
1504
1505 new_ucmd!()
1506 .args(&["%0$d%d-", "5", "10", "6", "20"])
1507 .fails_with_code(1)
1508 .stderr_only("printf: %0$: invalid conversion specification\n");
1509
1510 new_ucmd!()
1511 .args(&[
1512 "Octal: %6$o, Int: %1$d, Float: %4$f, String: %2$s, Hex: %7$x, Scientific: %5$e, Char: %9$c, Unsigned: %3$u, Integer: %8$i",
1513 "42", // 1$d - Int
1514 "hello", // 2$s - String
1515 "100", // 3$u - Unsigned
1516 "3.14159", // 4$f - Float
1517 "0.00001", // 5$e - Scientific
1518 "77", // 6$o - Octal
1519 "255", // 7$x - Hex
1520 "123", // 8$i - Integer
1521 "A", // 9$c - Char
1522 ])
1523 .succeeds()
1524 .stdout_only("Octal: 115, Int: 42, Float: 3.141590, String: hello, Hex: ff, Scientific: 1.000000e-05, Char: A, Unsigned: 100, Integer: 123");
1525 }
1526
1527 #[test]
1528 #[cfg(target_family = "unix")]
1529 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
1530 fn non_utf_8_input() {
1531 use std::ffi::OsStr;
1532 use std::os::unix::ffi::OsStrExt;
1533
1534 // ISO-8859-1 encoded text
1535 // spell-checker:disable
1536 const INPUT_AND_OUTPUT: &[u8] =
1537 b"Swer an rehte g\xFCete wendet s\xEEn gem\xFCete, dem volget s\xE6lde und \xEAre.";
1538 // spell-checker:enable
1539
1540 let os_str = OsStr::from_bytes(INPUT_AND_OUTPUT);
1541
1542 new_ucmd!()
1543 .arg("%s")
1544 .arg(os_str)
1545 .succeeds()
1546 .stdout_only_bytes(INPUT_AND_OUTPUT);
1547
1548 new_ucmd!()
1549 .arg(os_str)
1550 .succeeds()
1551 .stdout_only_bytes(INPUT_AND_OUTPUT);
1552
1553 new_ucmd!()
1554 .arg("%d")
1555 .arg(os_str)
1556 .fails()
1557 .stderr_contains("expected a numeric value");
1558 }
1559
1560 #[test]
1561 fn test_emoji_formatting() {
1562 new_ucmd!()
1563 .args(&["Status: %s ๐ŸŽฏ Count: %d\n", "Success ๐Ÿš€", "42"])
1564 .succeeds()
1565 .stdout_only("Status: Success ๐Ÿš€ ๐ŸŽฏ Count: 42\n");
1566 }
1567
1568 #[cfg(target_os = "linux")]
1569 #[test]
1570 fn test_write_error_omits_errno() {
1571 let dev_full = std::fs::File::create("/dev/full").expect("failed to open /dev/full");
1572
1573 new_ucmd!()
1574 .arg("\n")
1575 .set_stdout(dev_full)
1576 .fails_with_code(1)
1577 .stderr_only("printf: write error: No space left on device\n");
1578 }
1579
1580 #[test]
1581 fn test_large_width_format() {
1582 // Test that extremely large width specifications fail gracefully with an error
1583 // rather than panicking.
1584 // When printf tries to format with a width of 20 million, it should return
1585 // an error message and exit code 1, not panic with exit code 101.
1586 let test_cases = [
1587 ("%20000000f", "0"), // float formatting
1588 ("%10000000s", "test"), // string formatting
1589 ("%15000000d", "42"), // integer formatting
1590 ];
1591
1592 for (format, arg) in test_cases {
1593 new_ucmd!()
1594 .args(&[format, arg])
1595 .fails_with_code(1)
1596 .stderr_contains("write error")
1597 .no_stdout();
1598 }
1599 }
1600
1601 #[test]
1602 fn test_numeric_field_width_above_u16_max() {
1603 const WIDTH: usize = 65_536;
1604
1605 let result = new_ucmd!().args(&["%65536d", "5"]).succeeds();
1606 let stdout = result.stdout();
1607
1608 assert_eq!(stdout.len(), WIDTH);
1609 assert!(stdout[..WIDTH - 1].iter().all(|&byte| byte == b' '));
1610 assert_eq!(stdout[WIDTH - 1], b'5');
1611 }
1612
1613 #[test]
1614 fn test_extreme_field_width_overflow() {
1615 // Test the specific case that was causing panic due to integer overflow
1616 // in the field width parsing.
1617 new_ucmd!()
1618 .args(&["%999999999999999999999999d", "1"])
1619 .fails_with_code(1)
1620 .stderr_contains("printf: write error"); //could contains additional message like "formatting width too large" not in GNU, thats fine.
1621 }
1622
1623 #[test]
1624 fn test_asterisk_width_i64_min_no_panic() {
1625 // Regression test for https://github.com/uutils/coreutils/issues/13766
1626 // An `i64::MIN` '*' width used to panic with "attempt to negate with overflow".
1627 // It must not panic: on 64-bit it fails with a write error, on 32-bit the
1628 // width is clamped to 0 and printf succeeds.
1629 let result = new_ucmd!()
1630 .args(&["|%*d|", &i64::MIN.to_string(), "1"])
1631 .run();
1632 assert!(
1633 result.succeeded() || result.code() == 1,
1634 "printf must not panic on an i64::MIN '*' width (got exit code {})",
1635 result.code()
1636 );
1637 }
1638
1639 #[test]
1640 fn test_q_string_control_chars_with_quotes() {
1641 // Test %q with control characters and single quotes combined.
1642 // This tests the fix for the GNU compatibility issue where control
1643 // characters combined with single quotes should use the segmented
1644 // quoting approach rather than double quotes.
1645 let input = "\x01'\x01";
1646 new_ucmd!()
1647 .args(&["%q", input])
1648 .succeeds()
1649 .stdout_only("''$'\\001'\\'''$'\\001'");
1650 }
1651
1652 // Output with no trailing newline stays in the buffer until the process exits,
1653 // so the failure is only visible when it is flushed.
1654 #[test]
1655 #[cfg(target_os = "linux")]
1656 fn test_unterminated_write_error_is_reported() {
1657 new_ucmd!()
1658 .arg("greeting")
1659 .set_stdout(std::fs::File::create("/dev/full").unwrap())
1660 .fails()
1661 .stderr_is("printf: write error: No space left on device\n");
1662 }
1663
1664 // Producing no output at all is not a write failure.
1665 #[test]
1666 #[cfg(target_os = "linux")]
1667 fn test_empty_output_succeeds_on_full_device() {
1668 new_ucmd!()
1669 .arg("")
1670 .set_stdout(std::fs::File::create("/dev/full").unwrap())
1671 .succeeds()
1672 .no_output();
1673 }
1674
1675 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
1676 mod diagnostics {
1677 #[cfg(unix)]
1678 use super::*;
1679
1680 #[cfg(unix)]
1681 #[test]
1682 fn test_snippet_points_at_the_bad_spec() {
1683 let result = new_ucmd!()
1684 .terminal_sim_stderr()
1685 .args(&["%5.2c", "q"])
1686 .fails_with_code(1);
1687
1688 // The caret covers the whole rejected spec, % included.
1689 assert_eq!(
1690 result.stderr_as_displayed(),
1691 "\
1692 printf: %5.2c: invalid conversion specification
1693 โ•ญโ”€[ printf:1:8 ]
1694 โ”‚
1695 1 โ”‚ printf %5.2c q
1696 โ”‚ โ”€โ”€โ”€โ”€โ”€
1697 โ”‚
1698 โ”‚ Help: %d, %s, %x, %f and the other C conversions are accepted, plus %b and %q; a literal % is written %%
1699 โ”€โ”€โ”€โ•ฏ"
1700 );
1701 }
1702
1703 #[cfg(unix)]
1704 #[test]
1705 fn test_snippet_points_at_the_incomplete_hex_escape() {
1706 let result = new_ucmd!()
1707 .terminal_sim_stderr()
1708 .args(&[r"a\xzb", "q"])
1709 .fails_with_code(1);
1710
1711 // The caret covers `\x` alone โ€” the characters around it are fine.
1712 assert_eq!(
1713 result.stderr_as_displayed(),
1714 "\
1715 printf: missing hexadecimal number in escape
1716 โ•ญโ”€[ printf:1:9 ]
1717 โ”‚
1718 1 โ”‚ printf a\\xzb q
1719 โ”‚ โ”€โ”€
1720 โ”‚
1721 โ”‚ Help: \\x takes one or two hexadecimal digits, \\u takes four and \\U takes eight
1722 โ”€โ”€โ”€โ•ฏ"
1723 );
1724 }
1725
1726 #[cfg(unix)]
1727 #[test]
1728 fn test_snippet_points_at_the_bad_code_point() {
1729 let result = new_ucmd!()
1730 .terminal_sim_stderr()
1731 .args(&[r"x\ud800y"])
1732 .fails_with_code(1);
1733
1734 assert_eq!(
1735 result.stderr_as_displayed(),
1736 "\
1737 printf: invalid universal character name \\ud800
1738 โ•ญโ”€[ printf:1:9 ]
1739 โ”‚
1740 1 โ”‚ printf x\\ud800y
1741 โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€
1742 โ”‚
1743 โ”‚ Help: code points between D800 and DFFF or above 10FFFF are not Unicode characters
1744 โ”€โ”€โ”€โ•ฏ"
1745 );
1746 }
1747
1748 #[cfg(unix)]
1749 #[test]
1750 fn test_snippet_points_inside_a_quoted_format() {
1751 let result = new_ucmd!()
1752 .terminal_sim_stderr()
1753 .args(&["hello %s and %z", "world"])
1754 .fails_with_code(1);
1755
1756 // A format with a space in it is echoed back quoted. The quotes only
1757 // wrap it, so the caret still finds the one spec at fault rather than
1758 // underlining the whole operand.
1759 assert_eq!(
1760 result.stderr_as_displayed(),
1761 "\
1762 printf: %z: invalid conversion specification
1763 โ•ญโ”€[ printf:1:22 ]
1764 โ”‚
1765 1 โ”‚ printf 'hello %s and %z' world
1766 โ”‚ โ”€โ”€
1767 โ”‚
1768 โ”‚ Help: %d, %s, %x, %f and the other C conversions are accepted, plus %b and %q; a literal % is written %%
1769 โ”€โ”€โ”€โ•ฏ"
1770 );
1771 }
1772
1773 #[cfg(unix)]
1774 #[test]
1775 fn test_snippet_points_at_a_format_that_looks_like_an_option() {
1776 let result = new_ucmd!()
1777 .terminal_sim_stderr()
1778 .args(&["-%z", "world"])
1779 .fails_with_code(1);
1780
1781 // printf takes hyphen values, so `-%z` is the format and not an
1782 // option; the caret belongs on it rather than on the argument after.
1783 assert_eq!(
1784 result.stderr_as_displayed(),
1785 "\
1786 printf: %z: invalid conversion specification
1787 โ•ญโ”€[ printf:1:9 ]
1788 โ”‚
1789 1 โ”‚ printf -%z world
1790 โ”‚ โ”€โ”€
1791 โ”‚
1792 โ”‚ Help: %d, %s, %x, %f and the other C conversions are accepted, plus %b and %q; a literal % is written %%
1793 โ”€โ”€โ”€โ•ฏ"
1794 );
1795 }
1796
1797 #[cfg(unix)]
1798 #[test]
1799 fn test_snippet_keeps_an_operand_that_draws_like_the_report() {
1800 let result = new_ucmd!()
1801 .terminal_sim_stderr()
1802 .args(&["โ•ฐ%z"])
1803 .fails_with_code(1);
1804
1805 // The report is drawn with box characters, and so is this operand;
1806 // echoing the arguments must not lose the line to that collision.
1807 assert_eq!(
1808 result.stderr_as_displayed(),
1809 "\
1810 printf: %z: invalid conversion specification
1811 โ•ญโ”€[ printf:1:9 ]
1812 โ”‚
1813 1 โ”‚ printf โ•ฐ%z
1814 โ”‚ โ”€โ”€
1815 โ”‚
1816 โ”‚ Help: %d, %s, %x, %f and the other C conversions are accepted, plus %b and %q; a literal % is written %%
1817 โ”€โ”€โ”€โ•ฏ"
1818 );
1819 }
1820
1821 #[cfg(unix)]
1822 #[test]
1823 fn test_piped_stderr_keeps_the_plain_message() {
1824 new_ucmd!()
1825 .args(&["%5.2c", "q"])
1826 .fails_with_code(1)
1827 .stderr_only("printf: %5.2c: invalid conversion specification\n");
1828 }
1829 }
1830
1831 #[test]
1832 fn leading_double_dash_ends_the_options() {
1833 new_ucmd!()
1834 .args(&["--", "%s\n", "a"])
1835 .succeeds()
1836 .stdout_only("a\n");
1837 }
1838
1839 #[test]
1840 fn double_dash_after_the_format_is_an_argument() {
1841 new_ucmd!()
1842 .args(&["%s\n", "--"])
1843 .succeeds()
1844 .stdout_only("--\n");
1845
1846 new_ucmd!()
1847 .args(&["%s %s\n", "--", "--"])
1848 .succeeds()
1849 .stdout_only("-- --\n");
1850
1851 // Only the first `--` terminates the options; the second one is data.
1852 new_ucmd!()
1853 .args(&["--", "%s\n", "--"])
1854 .succeeds()
1855 .stdout_only("--\n");
1856 }
1857
1858 #[test]
1859 fn double_dash_as_the_format_is_printed_literally() {
1860 new_ucmd!()
1861 .args(&["--", "--", "x"])
1862 .succeeds()
1863 .stdout_is("--")
1864 .stderr_contains("warning: ignoring excess arguments, starting with 'x'");
1865 }
1866
1867 #[test]
1868 fn help_and_version_past_the_format_are_arguments() {
1869 new_ucmd!()
1870 .args(&["%s", "--help"])
1871 .succeeds()
1872 .stdout_only("--help");
1873
1874 new_ucmd!()
1875 .args(&["%s", "--version"])
1876 .succeeds()
1877 .stdout_only("--version");
1878
1879 new_ucmd!()
1880 .args(&["--", "--help"])
1881 .succeeds()
1882 .stdout_only("--help");
1883 }