Skip to content

Commit

Permalink
Merge pull request #3499 from cakebaker/fix_size_header_for_multiples…
Browse files Browse the repository at this point in the history
…_of_1000_and_1024

df: fix "Size" header for multiples of 1000 & 1024
  • Loading branch information
tertsdiepraam authored May 7, 2022
2 parents 122c7d6 + 5a3933a commit 275f938
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn to_magnitude_and_suffix_1024(n: u128) -> Result<String, ()> {
Err(())
}

/// Convert a number, except multiples of 1024, into a string like "12kB" or "34MB".
/// Convert a number into a string like "12kB" or "34MB".
///
/// Powers of 1000 become "1kB", "1MB", "1GB", etc.
///
Expand Down Expand Up @@ -110,7 +110,7 @@ fn to_magnitude_and_suffix_not_powers_of_1024(n: u128) -> Result<String, ()> {
///
/// If the number is too large to represent.
fn to_magnitude_and_suffix(n: u128) -> Result<String, ()> {
if n % 1024 == 0 {
if n % 1024 == 0 && n % 1000 != 0 {
to_magnitude_and_suffix_1024(n)
} else {
to_magnitude_and_suffix_not_powers_of_1024(n)
Expand Down Expand Up @@ -239,6 +239,13 @@ mod tests {
assert_eq!(to_magnitude_and_suffix(1_000_000_001).unwrap(), "1.1GB");
}

#[test]
fn test_to_magnitude_and_suffix_multiples_of_1000_and_1024() {
assert_eq!(to_magnitude_and_suffix(128_000).unwrap(), "128kB");
assert_eq!(to_magnitude_and_suffix(1000 * 1024).unwrap(), "1.1MB");
assert_eq!(to_magnitude_and_suffix(1_000_000_000_000).unwrap(), "1TB");
}

#[test]
fn test_block_size_display() {
assert_eq!(format!("{}", BlockSize::Bytes(1024)), "1K");
Expand Down
5 changes: 5 additions & 0 deletions tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ fn test_block_size_1024() {
assert_eq!(get_header(2 * 1024 * 1024), "2M-blocks");
assert_eq!(get_header(1024 * 1024 * 1024), "1G-blocks");
assert_eq!(get_header(34 * 1024 * 1024 * 1024), "34G-blocks");

// multiples of both 1024 and 1000
assert_eq!(get_header(128_000), "128kB-blocks");
assert_eq!(get_header(1000 * 1024), "1.1MB-blocks");
assert_eq!(get_header(1_000_000_000_000), "1TB-blocks");
}

#[test]
Expand Down

0 comments on commit 275f938

Please sign in to comment.