Merge pull request #3499 from cakebaker/fix_size_header_for_multiples_of_1000_and_1024

df: fix "Size" header for multiples of 1000 & 1024
This commit is contained in:
Terts Diepraam 2022-05-07 11:44:35 +02:00 committed by GitHub
commit 275f938f26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -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.
///
@ -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)
@ -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");

View file

@ -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]