rust/tests/run-pass/file_read.rs
2019-10-02 08:43:23 -05:00

24 lines
875 B
Rust

// ignore-windows: File handling is not implemented yet
// compile-flags: -Zmiri-disable-isolation
use std::fs::File;
use std::io::{ Read, Write };
fn main() {
// FIXME: remove the file and delete it when `rm` is implemented.
let path = "./tests/hello.txt";
let bytes = b"Hello, World!\n";
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
let mut file = File::create(path).unwrap();
file.write(bytes).unwrap();
// Test opening, reading and closing a file.
let mut file = File::open(path).unwrap();
let mut contents = Vec::new();
// Reading 0 bytes should not fill `contents`.
file.read(&mut contents).unwrap();
assert!(contents.is_empty());
// Reading until EOF should get the whole text.
file.read_to_end(&mut contents).unwrap();
assert_eq!(bytes, contents.as_slice());
}