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

dd: only print concise byte counts if large enough

Update `dd` to only print a concise form of the number of bytes with
an SI prefix (like "1 MB" or "2 GB") if the number is at least
1000. Similarly, only print the concise form with an IEC prefix (like
"1 MiB" or "2 GiB") if the number is at least 1024. For example,

    $ head -c 999 /dev/zero | dd > /dev/null
    1+1 records in
    1+1 records out
    999 bytes copied, 0.0 s, 999.0 KB/s

    $ head -c 1000 /dev/zero | dd > /dev/null
    1+1 records in
    1+1 records out
    1000 bytes (1000 B) copied, 0.0 s, 1000.0 KB/s

    $ head -c 1024 /dev/zero | dd > /dev/null
    2+0 records in
    2+0 records out
    1024 bytes (1 KB, 1024 B) copied, 0.0 s, 1.0 MB/s
This commit is contained in:
Jeffrey Finkelstein 2022-06-11 22:41:54 -04:00
parent 98cf16586e
commit a375644c50
2 changed files with 96 additions and 47 deletions

View file

@ -248,17 +248,10 @@ fn test_final_stats_noxfer() {
#[test]
fn test_final_stats_unspec() {
let output = vec![
"0+0 records in",
"0+0 records out",
"0 bytes (0 B, 0 B) copied, 0.0 s, 0 B/s",
];
let output = output.into_iter().fold(String::new(), |mut acc, s| {
acc.push_str(s);
acc.push('\n');
acc
});
new_ucmd!().run().stderr_only(&output).success();
new_ucmd!()
.run()
.stderr_only("0+0 records in\n0+0 records out\n0 bytes copied, 0.0 s, 0 B/s\n")
.success();
}
#[cfg(any(target_os = "linux", target_os = "android"))]
@ -370,20 +363,10 @@ fn test_existing_file_truncated() {
#[test]
fn test_null_stats() {
let stats = vec![
"0+0 records in\n",
"0+0 records out\n",
"0 bytes (0 B, 0 B) copied, 0.0 s, 0 B/s\n",
];
let stats = stats.into_iter().fold(String::new(), |mut acc, s| {
acc.push_str(s);
acc
});
new_ucmd!()
.args(&["if=null.txt"])
.run()
.stderr_only(stats)
.stderr_only("0+0 records in\n0+0 records out\n0 bytes copied, 0.0 s, 0 B/s\n")
.success();
}
@ -1184,3 +1167,22 @@ fn test_bytes_oseek_seek_additive() {
.succeeds()
.stdout_is_fixture_bytes("dd-bytes-alphabet-null.spec");
}
#[test]
fn test_final_stats_si_iec() {
let result = new_ucmd!().pipe_in("0".repeat(999)).succeeds();
let s = result.stderr_str();
assert!(s.starts_with("1+1 records in\n1+1 records out\n999 bytes copied,"));
let result = new_ucmd!().pipe_in("0".repeat(1000)).succeeds();
let s = result.stderr_str();
assert!(s.starts_with("1+1 records in\n1+1 records out\n1000 bytes (1000 B) copied,"));
let result = new_ucmd!().pipe_in("0".repeat(1023)).succeeds();
let s = result.stderr_str();
assert!(s.starts_with("1+1 records in\n1+1 records out\n1023 bytes (1 KB) copied,"));
let result = new_ucmd!().pipe_in("0".repeat(1024)).succeeds();
let s = result.stderr_str();
assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1 KB, 1024 B) copied,"));
}