Skip to content

Commit 04c4c29

Browse files
committed
Fixed inverted SI conditions
- Conditions that check whether the SI or IEC units must be used were inverted, i.e., when `si_prefix == true` it would use `"iB"` instead of `"B"`. - KB & kiB were used instead of kB & KiB. - Switches (true/false) in tests are also fixed.
1 parent 6630ad4 commit 04c4c29

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/lib.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
//! ```
2626
//! use bytesize::ByteSize;
2727
//!
28-
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
29-
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));
28+
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false));
29+
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true));
3030
//! ```
3131
3232
mod parse;
@@ -185,14 +185,14 @@ impl ByteSize {
185185
}
186186

187187
pub fn to_string(bytes: u64, si_prefix: bool) -> String {
188-
let unit = if si_prefix { KIB } else { KB };
189-
let unit_base = if si_prefix { LN_KIB } else { LN_KB };
188+
let unit = if si_prefix { KB } else { KIB };
189+
let unit_base = if si_prefix { LN_KB } else { LN_KIB };
190190
let unit_prefix = if si_prefix {
191191
UNITS_SI.as_bytes()
192192
} else {
193193
UNITS.as_bytes()
194194
};
195-
let unit_suffix = if si_prefix { "iB" } else { "B" };
195+
let unit_suffix = if si_prefix { "B" } else { "iB" };
196196

197197
if bytes < unit {
198198
format!("{} B", bytes)
@@ -427,12 +427,12 @@ mod tests {
427427
#[test]
428428
fn test_display() {
429429
assert_display("215 B", ByteSize::b(215));
430-
assert_display("1.0 KB", ByteSize::kb(1));
431-
assert_display("301.0 KB", ByteSize::kb(301));
432-
assert_display("419.0 MB", ByteSize::mb(419));
433-
assert_display("518.0 GB", ByteSize::gb(518));
434-
assert_display("815.0 TB", ByteSize::tb(815));
435-
assert_display("609.0 PB", ByteSize::pb(609));
430+
assert_display("1.0 KiB", ByteSize::kib(1));
431+
assert_display("301.0 KiB", ByteSize::kib(301));
432+
assert_display("419.0 MiB", ByteSize::mib(419));
433+
assert_display("518.0 GiB", ByteSize::gib(518));
434+
assert_display("815.0 TiB", ByteSize::tib(815));
435+
assert_display("609.0 PiB", ByteSize::pib(609));
436436
}
437437

438438
#[test]
@@ -453,33 +453,33 @@ mod tests {
453453

454454
#[test]
455455
fn test_to_string_as() {
456-
assert_to_string("215 B", ByteSize::b(215), true);
457456
assert_to_string("215 B", ByteSize::b(215), false);
457+
assert_to_string("215 B", ByteSize::b(215), true);
458458

459-
assert_to_string("1.0 kiB", ByteSize::kib(1), true);
460-
assert_to_string("1.0 KB", ByteSize::kib(1), false);
459+
assert_to_string("1.0 KiB", ByteSize::kib(1), false);
460+
assert_to_string("1.0 kB", ByteSize::kib(1), true);
461461

462-
assert_to_string("293.9 kiB", ByteSize::kb(301), true);
463-
assert_to_string("301.0 KB", ByteSize::kb(301), false);
462+
assert_to_string("293.9 KiB", ByteSize::kb(301), false);
463+
assert_to_string("301.0 kB", ByteSize::kb(301), true);
464464

465-
assert_to_string("1.0 MiB", ByteSize::mib(1), true);
466-
assert_to_string("1048.6 KB", ByteSize::mib(1), false);
465+
assert_to_string("1.0 MiB", ByteSize::mib(1), false);
466+
assert_to_string("1048.6 kB", ByteSize::mib(1), true);
467467

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

472-
assert_to_string("399.6 MiB", ByteSize::mb(419), true);
473-
assert_to_string("419.0 MB", ByteSize::mb(419), false);
472+
assert_to_string("399.6 MiB", ByteSize::mb(419), false);
473+
assert_to_string("419.0 MB", ByteSize::mb(419), true);
474474

475-
assert_to_string("482.4 GiB", ByteSize::gb(518), true);
476-
assert_to_string("518.0 GB", ByteSize::gb(518), false);
475+
assert_to_string("482.4 GiB", ByteSize::gb(518), false);
476+
assert_to_string("518.0 GB", ByteSize::gb(518), true);
477477

478-
assert_to_string("741.2 TiB", ByteSize::tb(815), true);
479-
assert_to_string("815.0 TB", ByteSize::tb(815), false);
478+
assert_to_string("741.2 TiB", ByteSize::tb(815), false);
479+
assert_to_string("815.0 TB", ByteSize::tb(815), true);
480480

481-
assert_to_string("540.9 PiB", ByteSize::pb(609), true);
482-
assert_to_string("609.0 PB", ByteSize::pb(609), false);
481+
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
482+
assert_to_string("609.0 PB", ByteSize::pb(609), true);
483483
}
484484

485485
#[test]
@@ -489,7 +489,7 @@ mod tests {
489489

490490
#[test]
491491
fn test_to_string() {
492-
assert_to_string("609.0 PB", ByteSize::pb(609), false);
492+
assert_to_string("609.0 PB", ByteSize::pb(609), true);
493493
}
494494

495495
#[cfg(feature = "serde")]

src/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ mod tests {
231231

232232
assert_eq!(parse(&format!("{}", parse("128GB"))), 128 * Unit::GigaByte);
233233
assert_eq!(
234-
parse(&crate::to_string(parse("128.000 GiB"), true)),
234+
parse(&crate::to_string(parse("128.000 GiB"), false)),
235235
128 * Unit::GibiByte
236236
);
237237
}

0 commit comments

Comments
 (0)