| 1 | use humansize::{ |
| 2 | format_size, format_size_i, BaseUnit, FixedAt, FormatSizeOptions, BINARY, DECIMAL, WINDOWS, |
| 3 | }; |
| 4 | |
| 5 | #[test] |
| 6 | fn test_sizes() { |
| 7 | assert_eq!(format_size(0u32, BINARY), "0 B"); |
| 8 | assert_eq!(format_size(999u32, BINARY), "999 B"); |
| 9 | assert_eq!(format_size(1000u32, BINARY), "1000 B"); |
| 10 | assert_eq!(format_size(1000u32, DECIMAL), "1 kB"); |
| 11 | assert_eq!(format_size(1023u32, BINARY), "1023 B"); |
| 12 | assert_eq!(format_size(1023u32, DECIMAL), "1.02 kB"); |
| 13 | assert_eq!(format_size(1024u32, BINARY), "1 KiB"); |
| 14 | assert_eq!(format_size(1024u32, WINDOWS), "1 kB"); |
| 15 | |
| 16 | assert_eq!(format_size_i(1000f32, DECIMAL), "1 kB"); |
| 17 | assert_eq!(format_size_i(1000f64, DECIMAL), "1 kB"); |
| 18 | |
| 19 | |
| 20 | let custom_options = FormatSizeOptions::from(DECIMAL).space_after_value(false); |
| 21 | assert_eq!(format_size(1000u32, custom_options), "1kB"); |
| 22 | |
| 23 | let custom_options = FormatSizeOptions::from(BINARY).suffix("/s"); |
| 24 | assert_eq!(format_size(999u32, custom_options), "999 B/s"); |
| 25 | |
| 26 | let custom_options = FormatSizeOptions::from(DECIMAL) |
| 27 | .suffix("/day") |
| 28 | .space_after_value(false); |
| 29 | assert_eq!(format_size(1000u32, custom_options), "1kB/day"); |
| 30 | |
| 31 | let custom_options = FormatSizeOptions::from(BINARY).fixed_at(Some(FixedAt::Base)); |
| 32 | assert_eq!(format_size(2048u32, custom_options), "2048 B"); |
| 33 | |
| 34 | let custom_options = FormatSizeOptions::from(BINARY) |
| 35 | .fixed_at(Some(FixedAt::Base)) |
| 36 | .long_units(true); |
| 37 | assert_eq!(format_size(2048u32, custom_options), "2048 Bytes"); |
| 38 | |
| 39 | let custom_options = FormatSizeOptions::from(BINARY).fixed_at(Some(FixedAt::Kilo)); |
| 40 | assert_eq!(format_size(16584975u32, custom_options), "16196.26 KiB"); |
| 41 | assert_eq!(format_size_i(-16584975, custom_options), "-16196.26 KiB"); |
| 42 | |
| 43 | let custom_options = FormatSizeOptions::from(BINARY) |
| 44 | .fixed_at(Some(FixedAt::Tera)) |
| 45 | .decimal_places(10); |
| 46 | assert_eq!(format_size(15284975u32, custom_options), "0.0000139016 TiB"); |
| 47 | |
| 48 | assert_eq!((format_size_i(-5500, DECIMAL)), "-5.50 kB"); |
| 49 | assert_eq!((format_size(5500u32, DECIMAL)), "5.50 kB"); |
| 50 | |
| 51 | let custom_options = FormatSizeOptions::from(DECIMAL).base_unit(BaseUnit::Bit); |
| 52 | assert_eq!((format_size(1usize, custom_options)), "1 bit"); |
| 53 | assert_eq!((format_size(150usize, custom_options)), "150 bits"); |
| 54 | assert_eq!((format_size(1000usize, custom_options)), "1 kbit"); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn use_custom_option_struct_twice() { |
| 59 | let options = FormatSizeOptions::from(DECIMAL).long_units(true); |
| 60 | |
| 61 | assert_eq!(format_size(1500u32, &options), "1.50 Kilobyte",); |
| 62 | assert_eq!(format_size(2500u32, &options), "2.50 Kilobytes",); |
| 63 | assert_eq!(format_size_i(-2500000, &options), "-2.50 Megabytes",); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn pluralization_works() { |
| 68 | let options = FormatSizeOptions::from(DECIMAL) |
| 69 | .long_units(true) |
| 70 | .decimal_zeroes(2); |
| 71 | |
| 72 | assert_eq!(format_size(1u32, &options), "1.00 Byte",); |
| 73 | |
| 74 | assert_eq!(format_size(1000u32, &options), "1.00 Kilobyte",); |
| 75 | |
| 76 | assert_eq!(format_size(1000000u32, &options), "1.00 Megabyte",); |
| 77 | |
| 78 | assert_eq!(format_size(1000000000u32, &options), "1.00 Gigabyte",); |
| 79 | |
| 80 | assert_eq!(format_size_i(1000000000000_i64, &options), "1.00 Terabyte",); |
| 81 | |
| 82 | assert_eq!( |
| 83 | format_size_i(1000000000000000_i64, &options), |
| 84 | "1.00 Petabyte", |
| 85 | ); |
| 86 | |
| 87 | assert_eq!( |
| 88 | format_size_i(1000000000000000000_i64, &options), |
| 89 | "1.00 Exabyte", |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn max_value_decimal() { |
| 95 | let options = FormatSizeOptions::from(DECIMAL) |
| 96 | .decimal_places(7) |
| 97 | .long_units(true); |
| 98 | assert_eq!(format_size(core::u64::MAX, &options), "18.4467441 Exabytes",); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn max_value_binary() { |
| 103 | let options = FormatSizeOptions::from(BINARY) |
| 104 | .decimal_places(7) |
| 105 | .long_units(true); |
| 106 | |
| 107 | assert_eq!(format_size(core::u64::MAX, &options), "16 Exbibytes",); |
| 108 | } |