Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed inverted SI conditions (and related things) #38

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Features:

- Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB).
- Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, GB, GiB, ... PiB).
- `ByteSize` type which presents size units convertible to different size units.
- Arithmetic operations for `ByteSize`.
- FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB.
Expand Down Expand Up @@ -55,31 +55,45 @@ fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
fn test_to_string_as() {
assert_to_string("215 B", ByteSize::b(215), true);
assert_to_string("215 B", ByteSize::b(215), false);
assert_to_string("215 B", ByteSize::b(215), true);

assert_to_string("1.0 KiB", ByteSize::kib(1), true);
assert_to_string("1.0 KB", ByteSize::kib(1), false);

assert_to_string("293.9 KiB", ByteSize::kb(301), true);
assert_to_string("301.0 KB", ByteSize::kb(301), false);

assert_to_string("1.0 MiB", ByteSize::mib(1), true);
assert_to_string("1048.6 KB", ByteSize::mib(1), false);
assert_to_string("1.0 MiB", ByteSize::mib(1), false);
assert_to_string("1048.6 kB", ByteSize::mib(1), true);

// a bug case: https://github.com/flang-project/bytesize/issues/8
assert_to_string("1.9 GiB", ByteSize::mib(1907), true);
assert_to_string("2.0 GB", ByteSize::mib(1908), false);

assert_to_string("399.6 MiB", ByteSize::mb(419), true);
assert_to_string("419.0 MB", ByteSize::mb(419), false);

assert_to_string("482.4 GiB", ByteSize::gb(518), true);
assert_to_string("518.0 GB", ByteSize::gb(518), false);
assert_to_string("399.6 MiB", ByteSize::mb(419), false);
assert_to_string("419.0 MB", ByteSize::mb(419), true);

assert_to_string("482.4 GiB", ByteSize::gb(518), false);
assert_to_string("518.0 GB", ByteSize::gb(518), true);

assert_to_string("741.2 TiB", ByteSize::tb(815), true);
assert_to_string("815.0 TB", ByteSize::tb(815), false);
assert_to_string("741.2 TiB", ByteSize::tb(815), false);
assert_to_string("815.0 TB", ByteSize::tb(815), true);

assert_to_string("540.9 PiB", ByteSize::pb(609), true);
assert_to_string("609.0 PB", ByteSize::pb(609), false);
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
assert_to_string("609.0 PB", ByteSize::pb(609), true);
}

#[test]
fn test_parsing_from_str() {
// shortcut for writing test cases
fn parse(s: &str) -> u64 {
s.parse::<ByteSize>().unwrap().0
}

assert_to_string("540.9 PiB", ByteSize::pb(609), false);
assert_to_string("609.0 PB", ByteSize::pb(609), true);
}
```

Expand All @@ -89,13 +103,13 @@ fn test_to_string_as() {
use bytesize::ByteSize;

fn byte_arithmetic_operator() {
let x = ByteSize::mb(1);
let y = ByteSize::kb(100);
let x = ByteSize::mb(1);
let y = ByteSize::kb(100);

let plus = x + y;
print!("{}", plus);
let plus = x + y;
println!("{}", plus);

let minus = ByteSize::tb(100) + ByteSize::gb(4);
print!("{}", minus);
let minus = ByteSize::tb(100) + ByteSize::gb(4);
println!("{}", minus);
}
```
55 changes: 27 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
//! ```
//! use bytesize::ByteSize;
//!
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false));
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true));
//! ```

mod parse;
Expand Down Expand Up @@ -66,9 +66,9 @@ pub const TIB: u64 = 1_099_511_627_776;
pub const PIB: u64 = 1_125_899_906_842_624;

static UNITS: &str = "KMGTPE";
static UNITS_SI: &str = "KMGTPE";
static LN_KB: f64 = 6.931471806; // ln 1024
static LN_KIB: f64 = 6.907755279; // ln 1000
static UNITS_SI: &str = "kMGTPE";
static LN_KIB: f64 = 6.931471805599453; // ln 1024
static LN_KB: f64 = 6.907755278982137; // ln 1000

pub fn kb<V: Into<u64>>(size: V) -> u64 {
size.into() * KB
Expand Down Expand Up @@ -183,14 +183,14 @@ impl ByteSize {
}

pub fn to_string(bytes: u64, si_prefix: bool) -> String {
let unit = if si_prefix { KIB } else { KB };
let unit_base = if si_prefix { LN_KIB } else { LN_KB };
let unit = if si_prefix { KB } else { KIB };
let unit_base = if si_prefix { LN_KB } else { LN_KIB };
Andrew15-5 marked this conversation as resolved.
Show resolved Hide resolved
let unit_prefix = if si_prefix {
UNITS_SI.as_bytes()
} else {
UNITS.as_bytes()
};
let unit_suffix = if si_prefix { "iB" } else { "B" };
let unit_suffix = if si_prefix { "B" } else { "iB" };

if bytes < unit {
format!("{} B", bytes)
Expand All @@ -212,7 +212,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {

impl Display for ByteSize {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(&to_string(self.0, true))
f.pad(&to_string(self.0, false))
}
}

Expand Down Expand Up @@ -452,33 +452,32 @@ mod tests {

#[test]
fn test_to_string_as() {
assert_to_string("215 B", ByteSize::b(215), true);
assert_to_string("215 B", ByteSize::b(215), false);
assert_to_string("215 B", ByteSize::b(215), true);

assert_to_string("1.0 KiB", ByteSize::kib(1), true);
assert_to_string("1.0 KB", ByteSize::kib(1), false);
assert_to_string("1.0 KiB", ByteSize::kib(1), false);
assert_to_string("1.0 kB", ByteSize::kib(1), true);

assert_to_string("293.9 KiB", ByteSize::kb(301), true);
assert_to_string("301.0 KB", ByteSize::kb(301), false);
assert_to_string("293.9 KiB", ByteSize::kb(301), false);
assert_to_string("301.0 kB", ByteSize::kb(301), true);

assert_to_string("1.0 MiB", ByteSize::mib(1), true);
assert_to_string("1048.6 KB", ByteSize::mib(1), false);
assert_to_string("1.0 MiB", ByteSize::mib(1), false);
assert_to_string("1.0 MB", ByteSize::mib(1), true);

// a bug case: https://github.com/flang-project/bytesize/issues/8
assert_to_string("1.9 GiB", ByteSize::mib(1907), true);
assert_to_string("2.0 GB", ByteSize::mib(1908), false);
assert_to_string("1.9 GiB", ByteSize::mib(1907), false);
assert_to_string("2.0 GB", ByteSize::mib(1907), true);

assert_to_string("399.6 MiB", ByteSize::mb(419), true);
assert_to_string("419.0 MB", ByteSize::mb(419), false);
assert_to_string("399.6 MiB", ByteSize::mb(419), false);
assert_to_string("419.0 MB", ByteSize::mb(419), true);

assert_to_string("482.4 GiB", ByteSize::gb(518), true);
assert_to_string("518.0 GB", ByteSize::gb(518), false);
assert_to_string("482.4 GiB", ByteSize::gb(518), false);
assert_to_string("518.0 GB", ByteSize::gb(518), true);

assert_to_string("741.2 TiB", ByteSize::tb(815), true);
assert_to_string("815.0 TB", ByteSize::tb(815), false);
assert_to_string("741.2 TiB", ByteSize::tb(815), false);
assert_to_string("815.0 TB", ByteSize::tb(815), true);

assert_to_string("540.9 PiB", ByteSize::pb(609), true);
assert_to_string("609.0 PB", ByteSize::pb(609), false);
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
assert_to_string("609.0 PB", ByteSize::pb(609), true);
}

#[test]
Expand All @@ -488,7 +487,7 @@ mod tests {

#[test]
fn test_to_string() {
assert_to_string("609.0 PB", ByteSize::pb(609), false);
assert_to_string("609.0 PB", ByteSize::pb(609), true);
}

#[cfg(feature = "serde")]
Expand Down
13 changes: 10 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ mod tests {
s.parse::<ByteSize>().unwrap().0
}

assert_eq!("0".parse::<ByteSize>().unwrap().0, 0);
assert_eq!(parse("0"), 0);
assert_eq!(parse("500"), 500);
assert_eq!(parse("1K"), Unit::KiloByte * 1);
Expand Down Expand Up @@ -229,9 +228,17 @@ mod tests {
s.parse::<ByteSize>().unwrap().0
}

assert_eq!(parse(&format!("{}", parse("128GB"))), 128 * Unit::GigaByte);
let expected = {
let init_value = 128 * Unit::GigaByte;
// Display for ByteSize is printing to IEC units with
// at least 1 digit after period.
let iec_float = init_value as f64 / Unit::GibiByte.factor() as f64;
let iec_float_with_1_digit = (iec_float * 10.0).round() / 10.0;
(iec_float_with_1_digit * Unit::GibiByte) as u64 // bytes
};
assert_eq!(parse(&format!("{}", ByteSize(parse("128GB")))), expected);
assert_eq!(
parse(&crate::to_string(parse("128.000 GiB"), true)),
parse(&crate::to_string(parse("128.000 GiB"), false)),
128 * Unit::GibiByte
);
}
Expand Down