rust/tests/run-pass/file_read.rs

26 lines
934 B
Rust
Raw Normal View History

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