@@ -29,84 +29,83 @@ extern crate bytesize;
2929```
3030
3131## Example
32- ### Human readable representations (SI unit and Binary unit )
32+ ### Human readable representations (SI units and Binary units )
3333``` rust
3434#[allow(dead_code)]
3535fn assert_display (expected : & str , b : ByteSize ) {
36- assert_eq! (expected , format! (" {}" , b ));
36+ assert_eq! (expected , format! (" {}" , b ));
3737}
3838
3939#[test]
40- fn test_display () {
41- assert_display (" 215 B" , ByteSize (215 ));
40+ fn test_display () {
4241 assert_display (" 215 B" , ByteSize :: b (215 ));
43- assert_display (" 1.0 KB" , ByteSize :: kb (1 ));
44- assert_display (" 301.0 KB" , ByteSize :: kb (301 ));
45- assert_display (" 419.0 MB" , ByteSize :: mb (419 ));
46- assert_display (" 518.0 GB" , ByteSize :: gb (518 ));
47- assert_display (" 815.0 TB" , ByteSize :: tb (815 ));
48- assert_display (" 609.0 PB" , ByteSize :: pb (609 ));
49- }
50-
51- fn assert_to_string (expected : & str , b : ByteSize , si : bool ) {
52- assert_eq! (expected . to_string (), b . to_string_as (si ));
53- }
42+ assert_display (" 1.0 KiB" , ByteSize :: kib (1 ));
43+ assert_display (" 301.0 KiB" , ByteSize :: kib (301 ));
44+ assert_display (" 419.0 MiB" , ByteSize :: mib (419 ));
45+ assert_display (" 518.0 GiB" , ByteSize :: gib (518 ));
46+ assert_display (" 815.0 TiB" , ByteSize :: tib (815 ));
47+ assert_display (" 609.0 PiB" , ByteSize :: pib (609 ));
48+ }
5449
55- #[test]
56- fn test_to_string () {
57- assert_to_string (" 215 B" , ByteSize (215 ), true );
58- assert_to_string (" 215 B" , ByteSize (215 ), false );
50+ fn assert_to_string (expected : & str , b : ByteSize , si : bool ) {
51+ assert_eq! (expected . to_string (), b . to_string_as (si ));
52+ }
5953
60- assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
54+ #[test]
55+ fn test_to_string_as () {
6156 assert_to_string (" 215 B" , ByteSize :: b (215 ), false );
57+ assert_to_string (" 215 B" , ByteSize :: b (215 ), true );
58+
59+ assert_to_string (" 1.0 KiB" , ByteSize :: kib (1 ), false );
60+ assert_to_string (" 1.0 kB" , ByteSize :: kib (1 ), true );
61+
62+ assert_to_string (" 293.9 KiB" , ByteSize :: kb (301 ), false );
63+ assert_to_string (" 301.0 kB" , ByteSize :: kb (301 ), true );
6264
63- assert_to_string (" 1.0 kiB" , ByteSize :: kib (1 ), true );
64- assert_to_string (" 1.0 KB" , ByteSize :: kib (1 ), false );
65-
66- assert_to_string (" 293.9 kiB" , ByteSize :: kb (301 ), true );
67- assert_to_string (" 301.0 KB" , ByteSize :: kb (301 ), false );
68-
69- assert_to_string (" 1.0 MiB" , ByteSize :: mib (1 ), true );
70- assert_to_string (" 1048.6 KB" , ByteSize :: mib (1 ), false );
71-
72- assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), true );
73- assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), false );
74-
75- assert_to_string (" 482.4 GiB" , ByteSize :: gb (518 ), true );
76- assert_to_string (" 518.0 GB" , ByteSize :: gb (518 ), false );
77-
78- assert_to_string (" 741.2 TiB" , ByteSize :: tb (815 ), true );
79- assert_to_string (" 815.0 TB" , ByteSize :: tb (815 ), false );
80-
81- assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), true );
82- assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), false );
83- }
84-
85- #[test]
86- fn test_parsing_from_str () {
87- // shortcut for writing test cases
88- fn parse (s : & str ) -> u64 {
89- s . parse :: <ByteSize >(). unwrap (). 0
90- }
91-
92- assert_eq! (" 0" . parse :: <ByteSize >(). unwrap (). 0 , 0 );
93- assert_eq! (parse (" 0" ), 0 );
94- assert_eq! (parse (" 500" ), 500 );
95- assert_eq! (parse (" 1K" ), Unit :: KiloByte * 1 );
96- assert_eq! (parse (" 1Ki" ), Unit :: KibiByte * 1 );
97- assert_eq! (parse (" 1.5Ki" ), (1.5 * Unit :: KibiByte ) as u64 );
98- assert_eq! (parse (" 1KiB" ), 1 * Unit :: KibiByte );
99- assert_eq! (parse (" 1.5KiB" ), (1.5 * Unit :: KibiByte ) as u64 );
100- assert_eq! (parse (" 3 MB" ), Unit :: MegaByte * 3 );
101- assert_eq! (parse (" 4 MiB" ), Unit :: MebiByte * 4 );
102- assert_eq! (parse (" 6 GB" ), 6 * Unit :: GigaByte );
103- assert_eq! (parse (" 4 GiB" ), 4 * Unit :: GibiByte );
104- assert_eq! (parse (" 88TB" ), 88 * Unit :: TeraByte );
105- assert_eq! (parse (" 521TiB" ), 521 * Unit :: TebiByte );
106- assert_eq! (parse (" 8 PB" ), 8 * Unit :: PetaByte );
107- assert_eq! (parse (" 8P" ), 8 * Unit :: PetaByte );
108- assert_eq! (parse (" 12 PiB" ), 12 * Unit :: PebiByte );
109- }
65+ assert_to_string (" 1.0 MiB" , ByteSize :: mib (1 ), false );
66+ assert_to_string (" 1048.6 kB" , ByteSize :: mib (1 ), true );
67+
68+ assert_to_string (" 1.9 GiB" , ByteSize :: mib (1907 ), false );
69+ assert_to_string (" 2.0 GB" , ByteSize :: mib (1908 ), true );
70+
71+ assert_to_string (" 399.6 MiB" , ByteSize :: mb (419 ), false );
72+ assert_to_string (" 419.0 MB" , ByteSize :: mb (419 ), true );
73+
74+ assert_to_string (" 482.4 GiB" , ByteSize :: gb (518 ), false );
75+ assert_to_string (" 518.0 GB" , ByteSize :: gb (518 ), true );
76+
77+ assert_to_string (" 741.2 TiB" , ByteSize :: tb (815 ), false );
78+ assert_to_string (" 815.0 TB" , ByteSize :: tb (815 ), true );
79+
80+ assert_to_string (" 540.9 PiB" , ByteSize :: pb (609 ), false );
81+ assert_to_string (" 609.0 PB" , ByteSize :: pb (609 ), true );
82+ }
83+
84+ #[test]
85+ fn test_parsing_from_str () {
86+ // shortcut for writing test cases
87+ fn parse (s : & str ) -> u64 {
88+ s . parse :: <ByteSize >(). unwrap (). 0
89+ }
90+
91+ assert_eq! (" 0" . parse :: <ByteSize >(). unwrap (). 0 , 0 );
92+ assert_eq! (parse (" 0" ), 0 );
93+ assert_eq! (parse (" 500" ), 500 );
94+ assert_eq! (parse (" 1K" ), Unit :: KiloByte * 1 );
95+ assert_eq! (parse (" 1Ki" ), Unit :: KibiByte * 1 );
96+ assert_eq! (parse (" 1.5Ki" ), (1.5 * Unit :: KibiByte ) as u64 );
97+ assert_eq! (parse (" 1KiB" ), 1 * Unit :: KibiByte );
98+ assert_eq! (parse (" 1.5KiB" ), (1.5 * Unit :: KibiByte ) as u64 );
99+ assert_eq! (parse (" 3 MB" ), Unit :: MegaByte * 3 );
100+ assert_eq! (parse (" 4 MiB" ), Unit :: MebiByte * 4 );
101+ assert_eq! (parse (" 6 GB" ), 6 * Unit :: GigaByte );
102+ assert_eq! (parse (" 4 GiB" ), 4 * Unit :: GibiByte );
103+ assert_eq! (parse (" 88TB" ), 88 * Unit :: TeraByte );
104+ assert_eq! (parse (" 521TiB" ), 521 * Unit :: TebiByte );
105+ assert_eq! (parse (" 8 PB" ), 8 * Unit :: PetaByte );
106+ assert_eq! (parse (" 8P" ), 8 * Unit :: PetaByte );
107+ assert_eq! (parse (" 12 PiB" ), 12 * Unit :: PebiByte );
108+ }
110109```
111110
112111### Arithmetic operations
@@ -116,13 +115,13 @@ extern crate bytesize;
116115use bytesize :: ByteSize ;
117116
118117fn byte_arithmetic_operator () {
119- let x = ByteSize :: mb (1 );
120- let y = ByteSize :: kb (100 );
118+ let x = ByteSize :: mb (1 );
119+ let y = ByteSize :: kb (100 );
121120
122- let plus = x + y ;
123- print ! (" {}" , plus );
121+ let plus = x + y ;
122+ println ! (" {}" , plus );
124123
125- let minus = ByteSize :: tb (100 ) + ByteSize :: gb (4 );
126- print ! (" {}" , minus );
124+ let minus = ByteSize :: tb (100 ) + ByteSize :: gb (4 );
125+ println ! (" {}" , minus );
127126}
128127```
0 commit comments