]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/file_read.rs
a17f948980a935bce931b1a2f4c13f2c39cac3a2
[rust.git] / tests / run-pass / file_read.rs
1 // ignore-windows: File handling is not implemented yet
2 // compile-flags: -Zmiri-disable-isolation
3
4 use std::fs::File;
5 use std::io::{ Read, Write };
6
7 fn main() {
8     // FIXME: remove the file and delete it when `rm` is implemented.
9     let path = "./tests/hello.txt";
10     let bytes = b"Hello, World!\n";
11     // Test creating, writing and closing a file (closing is tested when `file` is dropped).
12     let mut file = File::create(path).unwrap();
13     file.write(bytes).unwrap();
14     // Test opening, reading and closing a file.
15     let mut file = File::open(path).unwrap();
16     let mut contents = Vec::new();
17     // Reading 0 bytes should not fill `contents`.
18     file.read(&mut contents).unwrap();
19     assert!(contents.is_empty());
20     // Reading until EOF should get the whole text.
21     file.read_to_end(&mut contents).unwrap();
22     assert_eq!(bytes, contents.as_slice());
23 }