humansize/tests/test.rs

138 lines
3.4 KiB
Rust
Raw Normal View History

2019-12-15 18:39:36 +00:00
use humansize::{
2022-08-25 08:06:55 +00:00
format_size,format_size_i, BINARY, DECIMAL, CONVENTIONAL, FormatSizeOptions, FixedAt
2019-12-15 18:39:36 +00:00
};
2019-10-27 01:04:24 +00:00
2019-10-27 01:09:40 +00:00
#[test]
2019-10-27 01:04:24 +00:00
fn test_sizes() {
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(0u32, BINARY), "0 B");
assert_eq!(format_size(999u32, BINARY), "999 B");
assert_eq!(format_size(1000u32, BINARY), "1000 B");
assert_eq!(format_size(1000u32, DECIMAL), "1 KB");
assert_eq!(format_size(1023u32, BINARY), "1023 B");
assert_eq!(format_size(1023u32, DECIMAL), "1.02 KB");
assert_eq!(format_size(1024u32, BINARY), "1 KiB");
assert_eq!(format_size(1024u32, CONVENTIONAL), "1 KB");
let semi_custom_options = FormatSizeOptions {
2019-10-27 01:04:24 +00:00
space: false,
2022-08-25 08:06:55 +00:00
..DECIMAL
2019-10-27 01:04:24 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1000u32, semi_custom_options), "1KB");
2019-10-27 01:04:24 +00:00
2022-08-25 08:06:55 +00:00
let semi_custom_options2 = FormatSizeOptions {
2019-10-27 01:04:24 +00:00
suffix: "/s",
2022-08-25 08:06:55 +00:00
..BINARY
2019-10-27 01:04:24 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(999u32, semi_custom_options2), "999 B/s");
2019-10-27 01:04:24 +00:00
2022-08-25 08:06:55 +00:00
let semi_custom_options3 = FormatSizeOptions {
2019-10-27 01:04:24 +00:00
suffix: "/day",
space: false,
2022-08-25 08:06:55 +00:00
..DECIMAL
2019-10-27 01:04:24 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1000u32, semi_custom_options3), "1KB/day");
2019-10-27 01:04:24 +00:00
2022-08-25 08:06:55 +00:00
let semi_custom_options4 = FormatSizeOptions {
fixed_at: FixedAt::Byte,
..BINARY
2019-10-27 01:04:24 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(2048u32, semi_custom_options4), "2048 B");
2019-10-27 01:04:24 +00:00
2022-08-25 08:06:55 +00:00
let semi_custom_options5 = FormatSizeOptions {
fixed_at: FixedAt::Kilo,
..BINARY
2019-10-27 01:04:24 +00:00
};
assert_eq!(
2022-08-25 08:06:55 +00:00
format_size(16584975u32, semi_custom_options5),
2019-10-27 01:04:24 +00:00
"16196.26 KiB"
);
2022-08-25 08:06:55 +00:00
let semi_custom_options6 = FormatSizeOptions {
fixed_at: FixedAt::Tera,
2019-10-27 01:04:24 +00:00
decimal_places: 10,
2022-08-25 08:06:55 +00:00
..BINARY
2019-10-27 01:04:24 +00:00
};
assert_eq!(
2022-08-25 08:06:55 +00:00
format_size(15284975u32, semi_custom_options6),
2019-10-27 01:04:24 +00:00
"0.0000139016 TiB"
);
2022-08-25 08:06:55 +00:00
let semi_custom_options7 = FormatSizeOptions {
..DECIMAL
2019-10-27 01:04:24 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!
((format_size_i(-5500, &semi_custom_options7)),
2019-10-27 01:04:24 +00:00
"-5.50 KB"
);
2022-08-25 08:06:55 +00:00
assert_eq!((format_size(5500u32, &semi_custom_options7)), "5.50 KB");
2019-11-24 17:12:06 +00:00
}
#[test]
fn use_custom_option_struct_twice() {
2022-08-25 08:06:55 +00:00
let options = FormatSizeOptions {
2019-11-24 17:12:06 +00:00
long_units: true,
2022-08-25 08:06:55 +00:00
..DECIMAL
2019-11-24 17:12:06 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1500u32, &options), "1.50 Kilobyte",);
2019-11-24 17:12:06 +00:00
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(2500u32, &options), "2.50 Kilobytes",);
2019-11-24 17:12:06 +00:00
}
#[test]
fn pluralization_works() {
2022-08-25 08:06:55 +00:00
let options = FormatSizeOptions {
2019-11-24 17:12:06 +00:00
long_units: true,
decimal_zeroes: 2,
2022-08-25 08:06:55 +00:00
..DECIMAL
2019-11-24 17:12:06 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1u32, &options), "1.00 Byte",);
2019-11-24 17:12:06 +00:00
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1000u32, &options), "1.00 Kilobyte",);
2019-11-24 17:12:06 +00:00
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1000000u32, &options), "1.00 Megabyte",);
2019-11-24 17:12:06 +00:00
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(1000000000u32, &options), "1.00 Gigabyte",);
2019-11-24 17:12:06 +00:00
assert_eq!(
2022-08-25 08:06:55 +00:00
format_size_i(1000000000000_i64, &options),
2019-11-24 17:12:06 +00:00
"1.00 Terabyte",
);
assert_eq!(
2022-08-25 08:06:55 +00:00
format_size_i(1000000000000000_i64, &options),
2019-11-24 17:12:06 +00:00
"1.00 Petabyte",
);
assert_eq!(
2022-08-25 08:06:55 +00:00
format_size_i(1000000000000000000_i64, &options),
2019-11-24 17:12:06 +00:00
"1.00 Exabyte",
);
}
#[test]
fn max_value_decimal() {
2022-08-25 08:06:55 +00:00
let options = FormatSizeOptions {
2019-11-24 17:12:06 +00:00
long_units: true,
decimal_places: 7,
2022-08-25 08:06:55 +00:00
..DECIMAL
2019-11-24 17:12:06 +00:00
};
assert_eq!(
2022-08-25 08:06:55 +00:00
format_size(std::u64::MAX, &options),
2019-11-24 17:12:06 +00:00
"18.4467441 Exabytes",
);
}
#[test]
fn max_value_binary() {
2022-08-25 08:06:55 +00:00
let options = FormatSizeOptions {
2019-11-24 17:12:06 +00:00
long_units: true,
decimal_places: 7,
2022-08-25 08:06:55 +00:00
..BINARY
2019-11-24 17:12:06 +00:00
};
2022-08-25 08:06:55 +00:00
assert_eq!(format_size(std::u64::MAX, &options), "16 Exbibytes",);
2019-12-15 18:39:36 +00:00
}