mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57: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:
parent
c06967a45a
commit
d06f91fbe2
7 changed files with 341 additions and 41 deletions
|
@ -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");
|
||||
}
|
||||
|
|
49
tests/fixtures/numfmt/gnutest_iec-i_result.txt
vendored
Normal file
49
tests/fixtures/numfmt/gnutest_iec-i_result.txt
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
-1.1Ki
|
||||
-1.1Ki
|
||||
-1.0Ki
|
||||
-1.0Ki
|
||||
-1023
|
||||
0
|
||||
1
|
||||
1023
|
||||
1.0Ki
|
||||
1.1Ki
|
||||
1.1Ki
|
||||
1.2Ki
|
||||
1.5Ki
|
||||
1.6Ki
|
||||
1.9Ki
|
||||
2.0Ki
|
||||
2.0Ki
|
||||
2.0Ki
|
||||
2.0Ki
|
||||
2.1Ki
|
||||
10Ki
|
||||
10Ki
|
||||
10Ki
|
||||
100Ki
|
||||
100Ki
|
||||
100Ki
|
||||
949Ki
|
||||
950Ki
|
||||
950Ki
|
||||
951Ki
|
||||
951Ki
|
||||
952Ki
|
||||
990Ki
|
||||
991Ki
|
||||
995Ki
|
||||
995Ki
|
||||
996Ki
|
||||
996Ki
|
||||
997Ki
|
||||
999Ki
|
||||
1000Ki
|
||||
1023Ki
|
||||
1.0Mi
|
||||
1.0Mi
|
||||
1.0Mi
|
||||
1.1Mi
|
||||
1.0Gi
|
||||
1.0Gi
|
||||
1.1Gi
|
49
tests/fixtures/numfmt/gnutest_iec_input.txt
vendored
Normal file
49
tests/fixtures/numfmt/gnutest_iec_input.txt
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
-1025
|
||||
-1024.1
|
||||
-1024
|
||||
-1023.1
|
||||
-1023
|
||||
0
|
||||
1
|
||||
1023
|
||||
1024
|
||||
1025
|
||||
1126
|
||||
1127
|
||||
1536
|
||||
1537
|
||||
1945
|
||||
1946
|
||||
1996
|
||||
1997
|
||||
2048
|
||||
2049
|
||||
10188
|
||||
10189
|
||||
10240
|
||||
102348
|
||||
102349
|
||||
102400
|
||||
971776
|
||||
972288
|
||||
972800
|
||||
972801
|
||||
973824
|
||||
973825
|
||||
1013760
|
||||
1013761
|
||||
1018879
|
||||
1018880
|
||||
1018881
|
||||
1019904
|
||||
1019905
|
||||
1022976
|
||||
1022977
|
||||
1047552
|
||||
1047553
|
||||
1048575
|
||||
1048576
|
||||
1048577
|
||||
1073741823
|
||||
1073741824
|
||||
1073741825
|
49
tests/fixtures/numfmt/gnutest_iec_result.txt
vendored
Normal file
49
tests/fixtures/numfmt/gnutest_iec_result.txt
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
-1.1K
|
||||
-1.1K
|
||||
-1.0K
|
||||
-1.0K
|
||||
-1023
|
||||
0
|
||||
1
|
||||
1023
|
||||
1.0K
|
||||
1.1K
|
||||
1.1K
|
||||
1.2K
|
||||
1.5K
|
||||
1.6K
|
||||
1.9K
|
||||
2.0K
|
||||
2.0K
|
||||
2.0K
|
||||
2.0K
|
||||
2.1K
|
||||
10K
|
||||
10K
|
||||
10K
|
||||
100K
|
||||
100K
|
||||
100K
|
||||
949K
|
||||
950K
|
||||
950K
|
||||
951K
|
||||
951K
|
||||
952K
|
||||
990K
|
||||
991K
|
||||
995K
|
||||
995K
|
||||
996K
|
||||
996K
|
||||
997K
|
||||
999K
|
||||
1000K
|
||||
1023K
|
||||
1.0M
|
||||
1.0M
|
||||
1.0M
|
||||
1.1M
|
||||
1.0G
|
||||
1.0G
|
||||
1.1G
|
39
tests/fixtures/numfmt/gnutest_si_input.txt
vendored
Normal file
39
tests/fixtures/numfmt/gnutest_si_input.txt
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
-1001
|
||||
-999.1
|
||||
-999
|
||||
1
|
||||
500
|
||||
999
|
||||
999.1
|
||||
1000
|
||||
1000.1
|
||||
1001
|
||||
9900
|
||||
9901
|
||||
9949
|
||||
9950
|
||||
9951
|
||||
10000
|
||||
10001
|
||||
10500
|
||||
10999
|
||||
50000
|
||||
99000
|
||||
99001
|
||||
99900
|
||||
99949
|
||||
99950
|
||||
100000
|
||||
100001
|
||||
100999
|
||||
101000
|
||||
101001
|
||||
999000
|
||||
999001
|
||||
999949
|
||||
999950
|
||||
999999
|
||||
1000000
|
||||
1000001
|
||||
999000000.1
|
||||
999000001
|
39
tests/fixtures/numfmt/gnutest_si_result.txt
vendored
Normal file
39
tests/fixtures/numfmt/gnutest_si_result.txt
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
-1.1K
|
||||
-1.0K
|
||||
-999
|
||||
1
|
||||
500
|
||||
999
|
||||
1.0K
|
||||
1.0K
|
||||
1.1K
|
||||
1.1K
|
||||
9.9K
|
||||
10K
|
||||
10K
|
||||
10K
|
||||
10K
|
||||
10K
|
||||
11K
|
||||
11K
|
||||
11K
|
||||
50K
|
||||
99K
|
||||
100K
|
||||
100K
|
||||
100K
|
||||
100K
|
||||
100K
|
||||
101K
|
||||
101K
|
||||
101K
|
||||
102K
|
||||
999K
|
||||
1.0M
|
||||
1.0M
|
||||
1.0M
|
||||
1.0M
|
||||
1.0M
|
||||
1.1M
|
||||
1.0G
|
||||
1.0G
|
Loading…
Add table
Add a link
Reference in a new issue