@@ -134,3 +134,82 @@ pub enum BinFileError {
134134 actual : u16 ,
135135 } ,
136136}
137+
138+ #[ cfg( test) ]
139+ mod tests {
140+ use std:: fs;
141+
142+ use super :: * ;
143+
144+ #[ test]
145+ fn create ( ) {
146+ const MY_MAGIC : u64 = u64:: from_be_bytes ( * b"MYMAGIC!" ) ;
147+ let mut file = BinFile :: < MY_MAGIC , 1 > :: create ( "target/test1" ) . unwrap ( ) ;
148+ file. write_all ( b"hello world" ) . unwrap ( ) ;
149+ file. flush ( ) . unwrap ( ) ;
150+
151+ let check = fs:: read ( "target/test1" ) . unwrap ( ) ;
152+ assert_eq ! ( check, b"MYMAGIC!\x00 \x01 hello world" ) ;
153+ }
154+
155+ #[ test]
156+ fn create_new ( ) {
157+ const MY_MAGIC : u64 = u64:: from_be_bytes ( * b"MYMAGIC!" ) ;
158+ fs:: remove_file ( "target/test2" ) . ok ( ) ;
159+ let mut file = BinFile :: < MY_MAGIC , 1 > :: create_new ( "target/test2" ) . unwrap ( ) ;
160+ file. write_all ( b"hello world" ) . unwrap ( ) ;
161+ file. flush ( ) . unwrap ( ) ;
162+
163+ let fail = BinFile :: < MY_MAGIC , 1 > :: create_new ( "target/test2" ) ;
164+ assert_eq ! ( fail. unwrap_err( ) . kind( ) , io:: ErrorKind :: AlreadyExists ) ;
165+
166+ let check = fs:: read ( "target/test2" ) . unwrap ( ) ;
167+ assert_eq ! ( check, b"MYMAGIC!\x00 \x01 hello world" ) ;
168+ }
169+
170+ #[ test]
171+ fn open ( ) {
172+ const MY_MAGIC : u64 = u64:: from_be_bytes ( * b"MYMAGIC!" ) ;
173+ let mut file = BinFile :: < MY_MAGIC , 1 > :: create ( "target/test3" ) . unwrap ( ) ;
174+ file. write_all ( b"hello world" ) . unwrap ( ) ;
175+ file. flush ( ) . unwrap ( ) ;
176+
177+ let mut file = BinFile :: < MY_MAGIC , 1 > :: open ( "target/test3" ) . unwrap ( ) ;
178+ let mut buf = Vec :: new ( ) ;
179+ let check = file. read_to_end ( & mut buf) . unwrap ( ) ;
180+ assert_eq ! ( check, 11 ) ;
181+ assert_eq ! ( buf, b"hello world" ) ;
182+ }
183+
184+ #[ test]
185+ fn open_wrong_magic ( ) {
186+ const MY_MAGIC : u64 = u64:: from_be_bytes ( * b"MYMAGIC!" ) ;
187+ let mut file = BinFile :: < MY_MAGIC , 1 > :: create ( "target/test4" ) . unwrap ( ) ;
188+ file. write_all ( b"hello world" ) . unwrap ( ) ;
189+ file. flush ( ) . unwrap ( ) ;
190+
191+ let err = BinFile :: < 0xFFFFFF_FFFFFF , 1 > :: open ( "target/test4" ) . unwrap_err ( ) ;
192+ assert_eq ! ( err. kind( ) , io:: ErrorKind :: Other ) ;
193+ assert_eq ! ( err. downcast:: <BinFileError >( ) . unwrap( ) , BinFileError :: InvalidMagic {
194+ filename: "target/test4" . to_string( ) ,
195+ expected: 0xFFFFFF_FFFFFF ,
196+ actual: MY_MAGIC ,
197+ } ) ;
198+ }
199+
200+ #[ test]
201+ fn open_wrong_version ( ) {
202+ const MY_MAGIC : u64 = u64:: from_be_bytes ( * b"MYMAGIC!" ) ;
203+ let mut file = BinFile :: < MY_MAGIC , 1 > :: create ( "target/test5" ) . unwrap ( ) ;
204+ file. write_all ( b"hello world" ) . unwrap ( ) ;
205+ file. flush ( ) . unwrap ( ) ;
206+
207+ let err = BinFile :: < MY_MAGIC , 0x0100 > :: open ( "target/test5" ) . unwrap_err ( ) ;
208+ assert_eq ! ( err. kind( ) , io:: ErrorKind :: Other ) ;
209+ assert_eq ! ( err. downcast:: <BinFileError >( ) . unwrap( ) , BinFileError :: InvalidVersion {
210+ filename: "target/test5" . to_string( ) ,
211+ expected: 0x0100 ,
212+ actual: 1 ,
213+ } ) ;
214+ }
215+ }
0 commit comments