1#[cfg(test)]
2use crate::*;
3
4#[cfg(test)]
5fn size_formatter_test(bytes: BYTES, expected: &str, si: bool) {
6 let mut buf = String::new();
7 if si {
9 write!(&mut buf, "{:?}", bytes.into_size()).unwrap();
10 } else {
11 write!(&mut buf, "{}", bytes.into_size()).unwrap();
12 }
13 assert_eq!(buf, expected);
14}
15
16#[test]
17fn size_formatter_under_1kib() {
18 size_formatter_test(495, "495B", false)
19}
20
21#[test]
22fn size_formatter_exactly_1_kib() {
23 size_formatter_test(1024, "1KiB", false)
24}
25
26#[test]
27fn size_formatter_under_1mib() {
28 size_formatter_test(1024 * 512, "512KiB", false)
29}
30
31#[test]
32fn size_formatter_exactly_1mib() {
33 size_formatter_test(1024 * 1024, "1MiB", false)
34}
35
36#[test]
37fn size_formatter_under_1gib() {
38 size_formatter_test(299 * 1024 * 1024, "299MiB", false)
39}
40
41#[test]
42fn size_formatter_exactly_1gib() {
43 size_formatter_test(KIB.pow(3), "1GiB", false)
44}
45
46#[test]
47fn size_formatter_under_1tib() {
48 size_formatter_test(KIB.pow(3) * 128, "128GiB", false)
49}
50
51#[test]
52fn size_formatter_exactly_1tib() {
53 size_formatter_test(KIB.pow(4), "1TiB", false)
54}
55
56#[test]
57fn size_formatter_under_1pib() {
58 size_formatter_test(KIB.pow(4) * 256, "256TiB", false)
59}
60
61#[test]
62fn size_formatter_exactly_1pib() {
63 size_formatter_test(KIB.pow(5), "1PiB", false)
64}
65
66#[test]
67fn exactsize_formatter_3pib_2gb_3b() {
68 let mut buf = String::new();
69 write!(&mut buf, "{}", (3 * PIB + 2 * GIB + 3 * B).into_longsize()).unwrap();
70 assert_eq!(buf, "3PiB 2GiB 3B");
71}