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

numfmt: align format output values with GNU (#1745)

When converting to SI or IEC, produce values that align with the conventions
used by GNU numfmt.

- values > 10 are represented without a decimal place, so 10000 becomes 10K
  instead of 10.0K

- when truncating, take the ceiling of the value, so 100001 becomes 101K

- values < 10 are truncated to the highest tenth, so 1001 becomes 1.1K

closes #1726
This commit is contained in:
Daniel Rocco 2021-03-06 12:26:05 -05:00 committed by GitHub
parent c06967a45a
commit d06f91fbe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 341 additions and 41 deletions

View file

@ -15,16 +15,25 @@ fn test_from_iec() {
.args(&["--from=iec"])
.pipe_in("1024\n1.1M\n0.1G")
.run()
.stdout_is("1024\n1153434\n107374182\n");
.stdout_is("1024\n1153434\n107374183\n");
}
#[test]
fn test_from_iec_i() {
new_ucmd!()
.args(&["--from=iec-i"])
.pipe_in("1024\n1.1Mi\n0.1Gi")
.pipe_in("1.1Mi\n0.1Gi")
.run()
.stdout_is("1024\n1153434\n107374182\n");
.stdout_is("1153434\n107374183\n");
}
#[test]
#[ignore] // FIXME: GNU from iec-i requires suffix
fn test_from_iec_i_requires_suffix() {
new_ucmd!()
.args(&["--from=iec-i", "1024"])
.fails()
.stderr_is("numfmt: missing 'i' suffix in input: 1024 (e.g Ki/Mi/Gi)");
}
#[test]
@ -42,7 +51,7 @@ fn test_to_si() {
.args(&["--to=si"])
.pipe_in("1000\n1100000\n100000000")
.run()
.stdout_is("1.0K\n1.1M\n100.0M\n");
.stdout_is("1.0K\n1.1M\n100M\n");
}
#[test]
@ -51,7 +60,7 @@ fn test_to_iec() {
.args(&["--to=iec"])
.pipe_in("1024\n1153434\n107374182")
.run()
.stdout_is("1.0K\n1.1M\n102.4M\n");
.stdout_is("1.0K\n1.2M\n103M\n");
}
#[test]
@ -60,7 +69,7 @@ fn test_to_iec_i() {
.args(&["--to=iec-i"])
.pipe_in("1024\n1153434\n107374182")
.run()
.stdout_is("1.0Ki\n1.1Mi\n102.4Mi\n");
.stdout_is("1.0Ki\n1.2Mi\n103Mi\n");
}
#[test]
@ -142,7 +151,7 @@ fn test_negative() {
.args(&["--to=iec-i"])
.pipe_in("-1024\n-1153434\n-107374182")
.run()
.stdout_is("-1.0Ki\n-1.1Mi\n-102.4Mi\n");
.stdout_is("-1.0Ki\n-1.2Mi\n-103Mi\n");
}
#[test]
@ -159,7 +168,7 @@ fn test_normalize() {
.args(&["--from=si", "--to=si"])
.pipe_in("10000000K\n0.001K")
.run()
.stdout_is("10.0G\n1\n");
.stdout_is("10G\n1\n");
}
#[test]
@ -167,7 +176,7 @@ fn test_si_to_iec() {
new_ucmd!()
.args(&["--from=si", "--to=iec", "15334263563K"])
.run()
.stdout_is("13.9T\n");
.stdout_is("14T\n");
}
#[test]
@ -279,3 +288,30 @@ fn test_should_calculate_implicit_padding_per_free_argument() {
.run()
.stdout_is(" 1024\n 2000\n");
}
#[test]
fn test_to_si_should_truncate_output() {
new_ucmd!()
.args(&["--to=si"])
.pipe_in_fixture("gnutest_si_input.txt")
.succeeds()
.stdout_is_fixture("gnutest_si_result.txt");
}
#[test]
fn test_to_iec_should_truncate_output() {
new_ucmd!()
.args(&["--to=iec"])
.pipe_in_fixture("gnutest_iec_input.txt")
.succeeds()
.stdout_is_fixture("gnutest_iec_result.txt");
}
#[test]
fn test_to_iec_i_should_truncate_output() {
new_ucmd!()
.args(&["--to=iec-i"])
.pipe_in_fixture("gnutest_iec_input.txt")
.succeeds()
.stdout_is_fixture("gnutest_iec-i_result.txt");
}