Skip to content

Commit 8e7c57d

Browse files
Andrew15-5robjtede
authored andcommitted
fix: invert SI/IEC 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 05bcfc4 commit 8e7c57d

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/lib.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
//! ```
2424
//! use bytesize::ByteSize;
2525
//!
26-
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true));
27-
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false));
26+
//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false));
27+
//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true));
2828
//! ```
2929
3030
mod parse;
@@ -177,14 +177,14 @@ impl ByteSize {
177177
}
178178

179179
pub fn to_string(bytes: u64, si_prefix: bool) -> String {
180-
let unit = if si_prefix { KIB } else { KB };
181-
let unit_base = if si_prefix { LN_KIB } else { LN_KB };
180+
let unit = if si_prefix { KB } else { KIB };
181+
let unit_base = if si_prefix { LN_KB } else { LN_KIB };
182182
let unit_prefix = if si_prefix {
183183
UNITS_SI.as_bytes()
184184
} else {
185185
UNITS.as_bytes()
186186
};
187-
let unit_suffix = if si_prefix { "iB" } else { "B" };
187+
let unit_suffix = if si_prefix { "B" } else { "iB" };
188188

189189
if bytes < unit {
190190
format!("{} B", bytes)
@@ -396,6 +396,7 @@ mod tests {
396396
assert!(ByteSize::b(0) < ByteSize::tib(1));
397397
}
398398

399+
#[track_caller]
399400
fn assert_display(expected: &str, b: ByteSize) {
400401
assert_eq!(expected, format!("{}", b));
401402
}
@@ -423,39 +424,40 @@ mod tests {
423424
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357)));
424425
}
425426

427+
#[track_caller]
426428
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
427429
assert_eq!(expected.to_string(), b.to_string_as(si));
428430
}
429431

430432
#[test]
431433
fn test_to_string_as() {
432-
assert_to_string("215 B", ByteSize::b(215), true);
433434
assert_to_string("215 B", ByteSize::b(215), false);
435+
assert_to_string("215 B", ByteSize::b(215), true);
434436

435-
assert_to_string("1.0 KiB", ByteSize::kib(1), true);
436-
assert_to_string("1.0 KB", ByteSize::kib(1), false);
437+
assert_to_string("1.0 KiB", ByteSize::kib(1), false);
438+
assert_to_string("1.0 kB", ByteSize::kib(1), true);
437439

438-
assert_to_string("293.9 KiB", ByteSize::kb(301), true);
439-
assert_to_string("301.0 KB", ByteSize::kb(301), false);
440+
assert_to_string("293.9 KiB", ByteSize::kb(301), false);
441+
assert_to_string("301.0 kB", ByteSize::kb(301), true);
440442

441-
assert_to_string("1.0 MiB", ByteSize::mib(1), true);
442-
assert_to_string("1048.6 KB", ByteSize::mib(1), false);
443+
assert_to_string("1.0 MiB", ByteSize::mib(1), false);
444+
assert_to_string("1048.6 kB", ByteSize::mib(1), true);
443445

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

448-
assert_to_string("399.6 MiB", ByteSize::mb(419), true);
449-
assert_to_string("419.0 MB", ByteSize::mb(419), false);
450+
assert_to_string("399.6 MiB", ByteSize::mb(419), false);
451+
assert_to_string("419.0 MB", ByteSize::mb(419), true);
450452

451-
assert_to_string("482.4 GiB", ByteSize::gb(518), true);
452-
assert_to_string("518.0 GB", ByteSize::gb(518), false);
453+
assert_to_string("482.4 GiB", ByteSize::gb(518), false);
454+
assert_to_string("518.0 GB", ByteSize::gb(518), true);
453455

454-
assert_to_string("741.2 TiB", ByteSize::tb(815), true);
455-
assert_to_string("815.0 TB", ByteSize::tb(815), false);
456+
assert_to_string("741.2 TiB", ByteSize::tb(815), false);
457+
assert_to_string("815.0 TB", ByteSize::tb(815), true);
456458

457-
assert_to_string("540.9 PiB", ByteSize::pb(609), true);
458-
assert_to_string("609.0 PB", ByteSize::pb(609), false);
459+
assert_to_string("540.9 PiB", ByteSize::pb(609), false);
460+
assert_to_string("609.0 PB", ByteSize::pb(609), true);
459461
}
460462

461463
#[test]
@@ -465,6 +467,6 @@ mod tests {
465467

466468
#[test]
467469
fn test_to_string() {
468-
assert_to_string("609.0 PB", ByteSize::pb(609), false);
470+
assert_to_string("609.0 PB", ByteSize::pb(609), true);
469471
}
470472
}

src/parse.rs

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

235235
assert_eq!(parse(&format!("{}", parse("128GB"))), 128 * Unit::GigaByte);
236236
assert_eq!(
237-
parse(&crate::to_string(parse("128.000 GiB"), true)),
237+
parse(&crate::to_string(parse("128.000 GiB"), false)),
238238
128 * Unit::GibiByte
239239
);
240240
}

0 commit comments

Comments
 (0)