1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

printf: Consistently handle negative widths/precision

Also allows character constants with " instead of ', and for
interpolated values with %b to use \0XXX notation for octal bytes
This commit is contained in:
Joseph Jon Booker 2025-03-30 22:53:16 -05:00
parent 856e92c381
commit 95e5396c4c
4 changed files with 154 additions and 32 deletions

View file

@ -82,6 +82,19 @@ fn escaped_unicode_eight_digit() {
.stdout_only("ĥ");
}
#[test]
fn escaped_unicode_null_byte() {
new_ucmd!()
.args(&["\\0001_"])
.succeeds()
.stdout_is_bytes([0u8, b'1', b'_']);
new_ucmd!()
.args(&["%b", "\\0001_"])
.succeeds()
.stdout_is_bytes([1u8, b'_']);
}
#[test]
fn escaped_percent_sign() {
new_ucmd!()
@ -260,6 +273,16 @@ fn sub_num_int_char_const_in() {
.args(&["emoji is %i", "'🙃"])
.succeeds()
.stdout_only("emoji is 128579");
new_ucmd!()
.args(&["ninety seven is %i", "\"a"])
.succeeds()
.stdout_only("ninety seven is 97");
new_ucmd!()
.args(&["emoji is %i", "\"🙃"])
.succeeds()
.stdout_only("emoji is 128579");
}
#[test]
@ -544,6 +567,76 @@ fn sub_any_asterisk_negative_first_param() {
.stdout_only("a(x )b"); // Would be 'a( x)b' if -5 was 5
}
#[test]
fn sub_any_asterisk_first_param_with_integer() {
new_ucmd!()
.args(&["|%*d|", "3", "0"])
.succeeds()
.stdout_only("| 0|");
new_ucmd!()
.args(&["|%*d|", "1", "0"])
.succeeds()
.stdout_only("|0|");
new_ucmd!()
.args(&["|%*d|", "0", "0"])
.succeeds()
.stdout_only("|0|");
new_ucmd!()
.args(&["|%*d|", "-1", "0"])
.succeeds()
.stdout_only("|0|");
// Negative widths are left-aligned
new_ucmd!()
.args(&["|%*d|", "-3", "0"])
.succeeds()
.stdout_only("|0 |");
}
#[test]
fn sub_any_asterisk_second_param_with_integer() {
new_ucmd!()
.args(&["|%.*d|", "3", "10"])
.succeeds()
.stdout_only("|010|");
new_ucmd!()
.args(&["|%*.d|", "1", "10"])
.succeeds()
.stdout_only("|10|");
new_ucmd!()
.args(&["|%.*d|", "0", "10"])
.succeeds()
.stdout_only("|10|");
new_ucmd!()
.args(&["|%.*d|", "-1", "10"])
.succeeds()
.stdout_only("|10|");
new_ucmd!()
.args(&["|%.*d|", "-2", "10"])
.succeeds()
.stdout_only("|10|");
new_ucmd!()
.args(&["|%.*d|", &i64::MIN.to_string(), "10"])
.succeeds()
.stdout_only("|10|");
new_ucmd!()
.args(&["|%.*d|", &format!("-{}", u128::MAX), "10"])
.fails_with_code(1)
.stdout_is("|10|")
.stderr_is(
"printf: '-340282366920938463463374607431768211455': Numerical result out of range\n",
);
}
#[test]
fn sub_any_specifiers_no_params() {
new_ucmd!()
@ -899,6 +992,14 @@ fn negative_zero_padding_with_space_test() {
.stdout_only("-01");
}
#[test]
fn spaces_before_numbers_are_ignored() {
new_ucmd!()
.args(&["%*.*d", " 5", " 3", " 6"])
.succeeds()
.stdout_only(" 006");
}
#[test]
fn float_with_zero_precision_should_pad() {
new_ucmd!()