1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 23:40:05 +08:00

add read_str_to_lines, tests, fix bug

This commit is contained in:
2020-05-05 13:27:21 +08:00
parent 66d3540b65
commit a672cdfa45
4 changed files with 90 additions and 5 deletions

View File

@@ -3,8 +3,8 @@ use super::XResult;
pub const SIZE_KB: i64 = 1024;
pub const SIZE_MB: i64 = SIZE_KB * SIZE_KB;
pub const SIZE_GB: i64 = SIZE_MB * SIZE_KB;
pub const SIZE_PB: i64 = SIZE_GB * SIZE_KB;
pub const SIZE_TB: i64 = SIZE_PB * SIZE_KB;
pub const SIZE_TB: i64 = SIZE_GB * SIZE_KB;
pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB;
pub fn parse_size(size: &str) -> XResult<i64> {
@@ -44,3 +44,25 @@ pub fn get_display_size(size: i64) -> String {
format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.)
}
}
#[test]
fn test_parse_size() {
assert_eq!(parse_size("1").unwrap(), 1);
assert_eq!(parse_size("1k").unwrap(), 1024);
assert_eq!(parse_size("1m").unwrap(), 1024 * 1024);
assert_eq!(parse_size("1g").unwrap(), 1024 * 1024 * 1024);
assert_eq!(parse_size("1t").unwrap(), 1024 * 1024 * 1024 * 1024);
assert_eq!(parse_size("1p").unwrap(), 1024 * 1024 * 1024 * 1024 * 1024);
}
#[test]
fn test_get_display_size() {
assert_eq!(get_display_size(0), "0");
assert_eq!(get_display_size(111), "111");
assert_eq!(get_display_size(1024), "1.00KB");
assert_eq!(get_display_size(1024 * 1024), "1.00MB");
assert_eq!(get_display_size(1024 * 1024 * 1024), "1.00GB");
assert_eq!(get_display_size(1024 * 1024 * 1024 * 1024), "1.00TB");
assert_eq!(get_display_size(1024 * 1024 * 1024 * 1024 * 1024), "1.00PB");
}