use std::io::{self, BufRead, BufReader, Cursor, Read, Seek, SeekFrom, Write};
use std::process::{Command, Stdio};
#[cfg(target_os = "linux")]
use std::ffi::OsString;
#[cfg(target_os = "linux")]
use std::fs;
#[cfg(target_os = "linux")]
use std::os::unix::ffi::{OsStrExt, OsStringExt};
#[cfg(target_os = "linux")]
use std::os::unix::fs::symlink;
#[cfg(target_os = "linux")]
use std::path::{Path, PathBuf};
#[cfg(target_os = "linux")]
use std::sync::atomic::{AtomicUsize, Ordering};
use super::{
fgetc, fgets, fputc, fputs, fread, fseek, ftell, fwrite, getchar, putchar, puts, rewind,
};
#[cfg(target_os = "linux")]
use super::{remove, rename};
const STANDARD_STREAM_CHILD: &str = "PROCTOR_LIBC_STANDARD_STREAM_CHILD";
fn unwrap_stdio<T>((value, status): (T, io::Result<()>)) -> T {
status.unwrap();
value
}
#[cfg(target_os = "linux")]
struct StdioTestDir(PathBuf);
#[cfg(target_os = "linux")]
impl StdioTestDir {
fn new() -> Self {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
loop {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"proctor-libc-stdio-test-{}-{id}",
std::process::id()
));
match fs::create_dir(&path) {
Ok(()) => return Self(path),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}
Err(error) => panic!("failed to create stdio test directory: {error}"),
}
}
}
fn join(&self, path: impl AsRef<Path>) -> PathBuf {
self.0.join(path)
}
}
#[cfg(target_os = "linux")]
impl Drop for StdioTestDir {
fn drop(&mut self) {
if let Err(error) = fs::remove_dir_all(&self.0) {
if std::thread::panicking() {
eprintln!(
"failed to clean up stdio test directory {}: {error}",
self.0.display()
);
} else {
panic!(
"failed to clean up stdio test directory {}: {error}",
self.0.display()
);
}
}
}
}
#[cfg(target_os = "linux")]
fn null_terminated_path(path: &Path) -> Vec<i8> {
let mut bytes: Vec<i8> = path
.as_os_str()
.as_bytes()
.iter()
.map(|&byte| byte as i8)
.collect();
bytes.push(0);
bytes
}
#[cfg(target_os = "linux")]
fn assert_path_missing(path: &Path) {
assert_eq!(
fs::symlink_metadata(path).unwrap_err().kind(),
io::ErrorKind::NotFound
);
}
#[test]
fn standard_stream_child() {
match std::env::var(STANDARD_STREAM_CHILD).as_deref() {
Err(std::env::VarError::NotPresent) => {}
Ok("getchar") => {
assert_eq!(unwrap_stdio(getchar()), 0);
assert_eq!(unwrap_stdio(getchar()), 127);
assert_eq!(unwrap_stdio(getchar()), 128);
assert_eq!(unwrap_stdio(getchar()), 255);
assert_eq!(unwrap_stdio(getchar()), -1);
assert_eq!(unwrap_stdio(getchar()), -1);
}
Ok("putchar") => {
assert_eq!(unwrap_stdio(putchar(-1)), 255);
assert_eq!(unwrap_stdio(putchar(256)), 0);
assert_eq!(unwrap_stdio(putchar(65)), 65);
}
Ok("puts") => {
assert_eq!(unwrap_stdio(puts(&[1, 2, 0, 3])), 0);
assert_eq!(unwrap_stdio(puts(&[0])), 0);
assert_eq!(unwrap_stdio(puts(&[-1, -128, 0])), 0);
}
Ok(mode) => panic!("unknown standard-stream child mode: {mode}"),
Err(error) => panic!("invalid standard-stream child mode: {error}"),
}
}
fn standard_stream_command(mode: &str) -> Command {
let mut command = Command::new(std::env::current_exe().unwrap());
command
.args([
"--exact",
"stdio::tests::standard_stream_child",
"--nocapture",
])
.env(STANDARD_STREAM_CHILD, mode);
command
}
fn contains_subslice(haystack: &[u8], needle: &[u8]) -> bool {
haystack
.windows(needle.len())
.any(|window| window == needle)
}
#[test]
fn getchar_reads_unsigned_bytes_and_reports_end_of_file() {
let mut child = standard_stream_command("getchar")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.spawn()
.unwrap();
child
.stdin
.take()
.unwrap()
.write_all(&[0, 127, 128, 255])
.unwrap();
assert!(child.wait().unwrap().success());
}
#[test]
fn putchar_writes_and_returns_the_input_converted_to_an_unsigned_byte() {
let output = standard_stream_command("putchar").output().unwrap();
assert!(output.status.success());
assert!(contains_subslice(&output.stdout, &[255, 0, 65]));
}
#[test]
fn puts_stops_at_null_and_appends_a_newline() {
let output = standard_stream_command("puts").output().unwrap();
assert!(output.status.success());
assert!(contains_subslice(
&output.stdout,
&[1, 2, b'\n', b'\n', 255, 128, b'\n']
));
}
#[cfg(target_os = "linux")]
#[test]
fn remove_deletes_a_regular_file_and_ignores_bytes_after_null() {
let temp = StdioTestDir::new();
let file = temp.join("file");
fs::write(&file, b"contents").unwrap();
let mut path = null_terminated_path(&file);
path.extend([b'x' as i8, b'y' as i8]);
assert_eq!(unwrap_stdio(remove(&path)), 0);
assert_path_missing(&file);
}
#[cfg(target_os = "linux")]
#[test]
fn remove_deletes_an_empty_directory_with_a_trailing_slash() {
let temp = StdioTestDir::new();
let directory = temp.join("empty");
fs::create_dir(&directory).unwrap();
let mut path = null_terminated_path(&directory);
path.pop();
path.extend([b'/' as i8, 0]);
assert_eq!(unwrap_stdio(remove(&path)), 0);
assert_path_missing(&directory);
}
#[cfg(target_os = "linux")]
#[test]
fn remove_rejects_a_nonempty_directory_without_changing_it() {
let temp = StdioTestDir::new();
let directory = temp.join("nonempty");
let child = directory.join("child");
fs::create_dir(&directory).unwrap();
fs::write(&child, b"contents").unwrap();
let (value, status) = remove(&null_terminated_path(&directory));
assert_eq!(value, -1);
assert_eq!(status.unwrap_err().kind(), io::ErrorKind::DirectoryNotEmpty);
assert!(directory.is_dir());
assert_eq!(fs::read(child).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn remove_unlinks_a_symbolic_link_to_a_file_without_changing_the_target() {
let temp = StdioTestDir::new();
let target = temp.join("target-file");
let link = temp.join("file-link");
fs::write(&target, b"contents").unwrap();
symlink(&target, &link).unwrap();
assert_eq!(unwrap_stdio(remove(&null_terminated_path(&link))), 0);
assert_path_missing(&link);
assert_eq!(fs::read(target).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn remove_unlinks_a_symbolic_link_to_a_directory_without_changing_the_target() {
let temp = StdioTestDir::new();
let target = temp.join("target-directory");
let child = target.join("child");
let link = temp.join("directory-link");
fs::create_dir(&target).unwrap();
fs::write(&child, b"contents").unwrap();
symlink(&target, &link).unwrap();
assert_eq!(unwrap_stdio(remove(&null_terminated_path(&link))), 0);
assert_path_missing(&link);
assert!(target.is_dir());
assert_eq!(fs::read(child).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn remove_unlinks_a_broken_symbolic_link() {
let temp = StdioTestDir::new();
let target = temp.join("missing-target");
let link = temp.join("broken-link");
symlink(&target, &link).unwrap();
assert_eq!(unwrap_stdio(remove(&null_terminated_path(&link))), 0);
assert_path_missing(&link);
assert_path_missing(&target);
}
#[cfg(target_os = "linux")]
#[test]
fn remove_decrements_a_files_link_count_without_changing_its_other_link() {
let temp = StdioTestDir::new();
let file = temp.join("file");
let other_link = temp.join("other-link");
fs::write(&file, b"contents").unwrap();
fs::hard_link(&file, &other_link).unwrap();
assert_eq!(unwrap_stdio(remove(&null_terminated_path(&file))), 0);
assert_path_missing(&file);
assert_eq!(fs::read(other_link).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn remove_rejects_a_trailing_slash_on_a_regular_file_without_changing_it() {
let temp = StdioTestDir::new();
let file = temp.join("file");
fs::write(&file, b"contents").unwrap();
let mut path = null_terminated_path(&file);
path.pop();
path.extend([b'/' as i8, 0]);
let (value, status) = remove(&path);
assert_eq!(value, -1);
assert_eq!(status.unwrap_err().kind(), io::ErrorKind::NotADirectory);
assert_eq!(fs::read(file).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn remove_accepts_a_non_utf8_path() {
let temp = StdioTestDir::new();
let file = temp.join(OsString::from_vec(b"non-utf8-\xff".to_vec()));
fs::write(&file, b"contents").unwrap();
assert_eq!(unwrap_stdio(remove(&null_terminated_path(&file))), 0);
assert_path_missing(&file);
}
#[cfg(target_os = "linux")]
#[test]
fn remove_reports_a_missing_path_without_changing_its_parent() {
let temp = StdioTestDir::new();
let missing = temp.join("missing");
let (value, status) = remove(&null_terminated_path(&missing));
assert_eq!(value, -1);
assert_eq!(status.unwrap_err().kind(), io::ErrorKind::NotFound);
assert!(temp.0.is_dir());
assert_path_missing(&missing);
}
#[cfg(target_os = "linux")]
#[test]
fn remove_reports_an_empty_path() {
let (value, status) = remove(&[0]);
assert_eq!(value, -1);
assert_eq!(status.unwrap_err().kind(), io::ErrorKind::NotFound);
}
#[cfg(target_os = "linux")]
#[test]
fn remove_unlinks_an_open_file_without_invalidating_the_open_handle() {
let temp = StdioTestDir::new();
let file = temp.join("open-file");
fs::write(&file, b"contents").unwrap();
let mut open_file = fs::File::open(&file).unwrap();
assert_eq!(unwrap_stdio(remove(&null_terminated_path(&file))), 0);
assert_path_missing(&file);
let mut contents = String::new();
open_file.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_moves_a_regular_file_between_directories_and_ignores_bytes_after_each_null() {
let temp = StdioTestDir::new();
let old_parent = temp.join("old-parent");
let new_parent = temp.join("new-parent");
fs::create_dir(&old_parent).unwrap();
fs::create_dir(&new_parent).unwrap();
let old = old_parent.join("old");
let new = new_parent.join("new");
fs::write(&old, b"contents").unwrap();
let mut old_path = null_terminated_path(&old);
let mut new_path = null_terminated_path(&new);
old_path.extend([b'x' as i8, b'y' as i8]);
new_path.extend([b'a' as i8, b'b' as i8]);
assert_eq!(unwrap_stdio(rename(&old_path, &new_path)), 0);
assert_path_missing(&old);
assert_eq!(fs::read(new).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_moves_a_populated_directory() {
let temp = StdioTestDir::new();
let old = temp.join("old-directory");
let new = temp.join("new-directory");
fs::create_dir(&old).unwrap();
fs::write(old.join("child"), b"contents").unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert_eq!(fs::read(new.join("child")).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_replaces_an_existing_regular_file() {
let temp = StdioTestDir::new();
let old = temp.join("old");
let new = temp.join("new");
fs::write(&old, b"old contents").unwrap();
fs::write(&new, b"new contents").unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert_eq!(fs::read(new).unwrap(), b"old contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_replaces_an_empty_directory() {
let temp = StdioTestDir::new();
let old = temp.join("old-directory");
let new = temp.join("new-directory");
fs::create_dir(&old).unwrap();
fs::write(old.join("child"), b"contents").unwrap();
fs::create_dir(&new).unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert_eq!(fs::read(new.join("child")).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_rejects_a_nonempty_destination_directory_without_changing_either_directory() {
let temp = StdioTestDir::new();
let old = temp.join("old-directory");
let new = temp.join("new-directory");
fs::create_dir(&old).unwrap();
fs::create_dir(&new).unwrap();
fs::write(old.join("old-child"), b"old contents").unwrap();
fs::write(new.join("new-child"), b"new contents").unwrap();
let (value, status) = rename(&null_terminated_path(&old), &null_terminated_path(&new));
assert_eq!(value, -1);
assert_eq!(status.unwrap_err().kind(), io::ErrorKind::DirectoryNotEmpty);
assert_eq!(fs::read(old.join("old-child")).unwrap(), b"old contents");
assert_eq!(fs::read(new.join("new-child")).unwrap(), b"new contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_rejects_file_directory_type_mismatches_without_changing_either_path() {
let temp = StdioTestDir::new();
let old_file = temp.join("old-file");
let new_directory = temp.join("new-directory");
let old_directory = temp.join("old-directory");
let new_file = temp.join("new-file");
fs::write(&old_file, b"old file").unwrap();
fs::create_dir(&new_directory).unwrap();
fs::create_dir(&old_directory).unwrap();
fs::write(old_directory.join("child"), b"old directory").unwrap();
fs::write(&new_file, b"new file").unwrap();
let (file_value, file_status) = rename(
&null_terminated_path(&old_file),
&null_terminated_path(&new_directory),
);
let (directory_value, directory_status) = rename(
&null_terminated_path(&old_directory),
&null_terminated_path(&new_file),
);
assert_eq!(file_value, -1);
assert_eq!(file_status.unwrap_err().kind(), io::ErrorKind::IsADirectory);
assert_eq!(directory_value, -1);
assert_eq!(
directory_status.unwrap_err().kind(),
io::ErrorKind::NotADirectory
);
assert_eq!(fs::read(old_file).unwrap(), b"old file");
assert!(new_directory.is_dir());
assert_eq!(
fs::read(old_directory.join("child")).unwrap(),
b"old directory"
);
assert_eq!(fs::read(new_file).unwrap(), b"new file");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_moves_a_broken_symbolic_link_without_resolving_it() {
let temp = StdioTestDir::new();
let target = temp.join("missing-target");
let old = temp.join("old-link");
let new = temp.join("new-link");
symlink(&target, &old).unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert_eq!(fs::read_link(&new).unwrap(), target);
assert_path_missing(&target);
}
#[cfg(target_os = "linux")]
#[test]
fn rename_replaces_a_destination_symlink_without_changing_its_target() {
let temp = StdioTestDir::new();
let old = temp.join("old-file");
let target = temp.join("target-directory");
let new = temp.join("new-link");
fs::write(&old, b"contents").unwrap();
fs::create_dir(&target).unwrap();
fs::write(target.join("child"), b"target contents").unwrap();
symlink(&target, &new).unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert!(!fs::symlink_metadata(&new).unwrap().file_type().is_symlink());
assert_eq!(fs::read(new).unwrap(), b"contents");
assert_eq!(fs::read(target.join("child")).unwrap(), b"target contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_succeeds_without_changing_a_path_renamed_to_itself() {
let temp = StdioTestDir::new();
let file = temp.join("file");
fs::write(&file, b"contents").unwrap();
let path = null_terminated_path(&file);
assert_eq!(unwrap_stdio(rename(&path, &path)), 0);
assert_eq!(fs::read(file).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_succeeds_without_removing_different_hard_links_to_the_same_file() {
let temp = StdioTestDir::new();
let old = temp.join("old-link");
let new = temp.join("new-link");
fs::write(&old, b"contents").unwrap();
fs::hard_link(&old, &new).unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_eq!(fs::read(old).unwrap(), b"contents");
assert_eq!(fs::read(new).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_does_not_invalidate_an_open_handle_to_the_replaced_file() {
let temp = StdioTestDir::new();
let old = temp.join("old");
let new = temp.join("new");
fs::write(&old, b"old contents").unwrap();
fs::write(&new, b"new contents").unwrap();
let mut replaced_file = fs::File::open(&new).unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert_eq!(fs::read(&new).unwrap(), b"old contents");
let mut contents = String::new();
replaced_file.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "new contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_accepts_non_utf8_paths() {
let temp = StdioTestDir::new();
let old = temp.join(OsString::from_vec(b"old-\xff".to_vec()));
let new = temp.join(OsString::from_vec(b"new-\xfe".to_vec()));
fs::write(&old, b"contents").unwrap();
assert_eq!(
unwrap_stdio(rename(
&null_terminated_path(&old),
&null_terminated_path(&new)
)),
0
);
assert_path_missing(&old);
assert_eq!(fs::read(new).unwrap(), b"contents");
}
#[cfg(target_os = "linux")]
#[test]
fn rename_reports_empty_paths_without_changing_the_existing_file() {
let temp = StdioTestDir::new();
let file = temp.join("file");
let missing = temp.join("missing");
fs::write(&file, b"contents").unwrap();
let (old_value, old_status) = rename(&[0], &null_terminated_path(&missing));
let (new_value, new_status) = rename(&null_terminated_path(&file), &[0]);
assert_eq!(old_value, -1);
assert_eq!(old_status.unwrap_err().kind(), io::ErrorKind::NotFound);
assert_eq!(new_value, -1);
assert_eq!(new_status.unwrap_err().kind(), io::ErrorKind::NotFound);
assert_eq!(fs::read(file).unwrap(), b"contents");
assert_path_missing(&missing);
}
#[cfg(target_os = "linux")]
#[test]
fn rename_reports_missing_paths_without_changing_existing_entries() {
let temp = StdioTestDir::new();
let missing_old = temp.join("missing-old");
let existing_new = temp.join("existing-new");
let existing_old = temp.join("existing-old");
let missing_parent = temp.join("missing-parent");
let missing_new = missing_parent.join("new");
fs::write(&existing_new, b"new contents").unwrap();
fs::write(&existing_old, b"old contents").unwrap();
let (old_value, old_status) = rename(
&null_terminated_path(&missing_old),
&null_terminated_path(&existing_new),
);
let (new_value, new_status) = rename(
&null_terminated_path(&existing_old),
&null_terminated_path(&missing_new),
);
assert_eq!(old_value, -1);
assert_eq!(old_status.unwrap_err().kind(), io::ErrorKind::NotFound);
assert_eq!(new_value, -1);
assert_eq!(new_status.unwrap_err().kind(), io::ErrorKind::NotFound);
assert_path_missing(&missing_old);
assert_eq!(fs::read(existing_new).unwrap(), b"new contents");
assert_eq!(fs::read(existing_old).unwrap(), b"old contents");
assert_path_missing(&missing_parent);
}
#[cfg(target_os = "linux")]
#[test]
fn rename_rejects_trailing_slashes_on_regular_file_paths_without_changing_them() {
let temp = StdioTestDir::new();
let first_old = temp.join("first-old");
let first_new = temp.join("first-new");
let second_old = temp.join("second-old");
let second_new = temp.join("second-new");
fs::write(&first_old, b"first contents").unwrap();
fs::write(&second_old, b"second contents").unwrap();
let mut first_old_path = null_terminated_path(&first_old);
first_old_path.pop();
first_old_path.extend([b'/' as i8, 0]);
let mut second_new_path = null_terminated_path(&second_new);
second_new_path.pop();
second_new_path.extend([b'/' as i8, 0]);
let (old_value, old_status) = rename(&first_old_path, &null_terminated_path(&first_new));
let (new_value, new_status) = rename(&null_terminated_path(&second_old), &second_new_path);
assert_eq!(old_value, -1);
assert_eq!(old_status.unwrap_err().kind(), io::ErrorKind::NotADirectory);
assert_eq!(new_value, -1);
assert_eq!(new_status.unwrap_err().kind(), io::ErrorKind::NotADirectory);
assert_eq!(fs::read(first_old).unwrap(), b"first contents");
assert_path_missing(&first_new);
assert_eq!(fs::read(second_old).unwrap(), b"second contents");
assert_path_missing(&second_new);
}
#[cfg(target_os = "linux")]
#[test]
fn rename_rejects_moving_a_directory_beneath_itself_without_changing_it() {
let temp = StdioTestDir::new();
let old = temp.join("directory");
let child = old.join("child");
let new = child.join("moved");
fs::create_dir(&old).unwrap();
fs::create_dir(&child).unwrap();
fs::write(child.join("file"), b"contents").unwrap();
let (value, status) = rename(&null_terminated_path(&old), &null_terminated_path(&new));
assert_eq!(value, -1);
assert_eq!(status.unwrap_err().kind(), io::ErrorKind::InvalidInput);
assert_eq!(fs::read(child.join("file")).unwrap(), b"contents");
assert_path_missing(&new);
}
#[test]
fn fseek_repositions_from_the_start_current_position_and_end() {
let mut stream = Cursor::new([0; 8]);
assert_eq!(unwrap_stdio(fseek(&mut stream, SeekFrom::Start(3))), 0);
assert_eq!(stream.position(), 3);
assert_eq!(unwrap_stdio(fseek(&mut stream, SeekFrom::Current(2))), 0);
assert_eq!(stream.position(), 5);
assert_eq!(unwrap_stdio(fseek(&mut stream, SeekFrom::End(-1))), 0);
assert_eq!(stream.position(), 7);
}
#[test]
fn fseek_allows_positions_beyond_the_end() {
let mut stream = Cursor::new([0; 8]);
assert_eq!(unwrap_stdio(fseek(&mut stream, SeekFrom::Start(12))), 0);
assert_eq!(unwrap_stdio(ftell(&mut stream)), 12);
}
#[test]
fn ftell_returns_the_current_position_without_changing_it() {
let mut stream = Cursor::new([0; 8]);
stream.set_position(5);
assert_eq!(unwrap_stdio(ftell(&mut stream)), 5);
assert_eq!(stream.position(), 5);
}
#[test]
fn rewind_sets_the_position_to_the_beginning() {
let mut stream = Cursor::new([0; 8]);
stream.set_position(5);
rewind(&mut stream).unwrap();
assert_eq!(stream.position(), 0);
}
#[test]
fn seek_functions_accept_dynamically_sized_streams() {
let mut bytes = Cursor::new([0; 8]);
let stream: &mut dyn Seek = &mut bytes;
assert_eq!(unwrap_stdio(fseek(stream, SeekFrom::Start(6))), 0);
assert_eq!(unwrap_stdio(ftell(stream)), 6);
rewind(stream).unwrap();
assert_eq!(unwrap_stdio(ftell(stream)), 0);
}
#[test]
fn seek_functions_propagate_seek_errors() {
struct FailingSeek;
impl Seek for FailingSeek {
fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
Err(io::Error::other("seek failed"))
}
}
let (fseek_value, fseek_status) = fseek(&mut FailingSeek, SeekFrom::Start(1));
let (ftell_value, ftell_status) = ftell(&mut FailingSeek);
assert_eq!(fseek_value, -1);
assert_eq!(ftell_value, -1);
for error in [
fseek_status.unwrap_err(),
ftell_status.unwrap_err(),
rewind(&mut FailingSeek).unwrap_err(),
] {
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "seek failed");
}
}
#[test]
fn fseek_and_ftell_accept_i64_max_and_reject_larger_positions() {
struct PositionSeek(u64);
impl Seek for PositionSeek {
fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
Ok(self.0)
}
}
let mut maximum = PositionSeek(i64::MAX as u64);
assert_eq!(unwrap_stdio(fseek(&mut maximum, SeekFrom::Start(0))), 0);
assert_eq!(unwrap_stdio(ftell(&mut maximum)), i64::MAX);
let mut too_large = PositionSeek(i64::MAX as u64 + 1);
let (fseek_value, fseek_status) = fseek(&mut too_large, SeekFrom::Start(0));
let (ftell_value, ftell_status) = ftell(&mut too_large);
assert_eq!(fseek_value, -1);
assert_eq!(ftell_value, -1);
assert_eq!(fseek_status.unwrap_err().kind(), io::ErrorKind::InvalidData);
assert_eq!(ftell_status.unwrap_err().kind(), io::ErrorKind::InvalidData);
}
#[test]
fn reads_bytes_as_unsigned_integers_and_advances_the_reader() {
let mut reader = Cursor::new([0, 127, 128, 255]);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), 0);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), 127);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), 128);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), 255);
}
#[test]
fn returns_minus_one_at_end_of_file() {
let mut reader = Cursor::new([42]);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), 42);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), -1);
assert_eq!(unwrap_stdio(fgetc(&mut reader)), -1);
}
#[test]
fn accepts_dynamically_sized_readers() {
let mut bytes = Cursor::new([42]);
let reader: &mut dyn Read = &mut bytes;
assert_eq!(unwrap_stdio(fgetc(reader)), 42);
}
#[test]
fn propagates_read_errors() {
struct FailingReader;
impl Read for FailingReader {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::other("read failed"))
}
}
let (value, status) = fgetc(&mut FailingReader);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "read failed");
}
#[test]
fn fputc_writes_bytes_and_returns_their_unsigned_values() {
let mut writer = Vec::new();
assert_eq!(unwrap_stdio(fputc(0, &mut writer)), 0);
assert_eq!(unwrap_stdio(fputc(127, &mut writer)), 127);
assert_eq!(unwrap_stdio(fputc(128, &mut writer)), 128);
assert_eq!(unwrap_stdio(fputc(255, &mut writer)), 255);
assert_eq!(writer, [0, 127, 128, 255]);
}
#[test]
fn fputc_converts_the_input_to_an_unsigned_byte() {
let mut writer = Vec::new();
assert_eq!(unwrap_stdio(fputc(-1, &mut writer)), 255);
assert_eq!(unwrap_stdio(fputc(256, &mut writer)), 0);
assert_eq!(unwrap_stdio(fputc(511, &mut writer)), 255);
assert_eq!(writer, [255, 0, 255]);
}
#[test]
fn fputc_accepts_dynamically_sized_writers() {
let mut bytes = Vec::new();
let writer: &mut dyn Write = &mut bytes;
assert_eq!(unwrap_stdio(fputc(42, writer)), 42);
assert_eq!(bytes, [42]);
}
#[test]
fn fputc_reports_a_writer_that_makes_no_progress() {
struct ZeroWriter;
impl Write for ZeroWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Ok(0)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let (value, status) = fputc(42, &mut ZeroWriter);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::WriteZero);
}
#[test]
fn fputc_propagates_write_errors() {
struct FailingWriter;
impl Write for FailingWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Err(io::Error::other("write failed"))
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let (value, status) = fputc(42, &mut FailingWriter);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "write failed");
}
#[test]
fn fputc_does_not_retry_an_interrupted_write() {
struct InterruptedWriter {
calls: usize,
}
impl Write for InterruptedWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.calls += 1;
if self.calls == 1 {
Err(io::ErrorKind::Interrupted.into())
} else {
Ok(buf.len())
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut writer = InterruptedWriter { calls: 0 };
let (value, status) = fputc(42, &mut writer);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::Interrupted);
assert_eq!(writer.calls, 1);
}
#[test]
fn fputs_writes_bytes_before_the_first_null_and_returns_zero() {
let mut writer = Vec::new();
assert_eq!(
unwrap_stdio(fputs(&[b'a' as i8, -128, -1, 0, b'b' as i8], &mut writer)),
0
);
assert_eq!(writer, [b'a', 128, 255]);
}
#[test]
fn fputs_does_not_write_an_empty_string() {
struct PanickingWriter;
impl Write for PanickingWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
panic!("write called for an empty string")
}
fn flush(&mut self) -> io::Result<()> {
panic!("flush called")
}
}
assert_eq!(
unwrap_stdio(fputs(&[0, b'a' as i8], &mut PanickingWriter)),
0
);
}
#[test]
fn fputs_completes_partial_writes_without_flushing() {
struct PartialWriter {
bytes: Vec<u8>,
calls: usize,
}
impl Write for PartialWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.calls += 1;
let len = buf.len().min(2);
self.bytes.extend_from_slice(&buf[..len]);
Ok(len)
}
fn flush(&mut self) -> io::Result<()> {
panic!("flush called")
}
}
let mut writer = PartialWriter {
bytes: Vec::new(),
calls: 0,
};
assert_eq!(unwrap_stdio(fputs(&[1, 2, 3, 4, 5, 0], &mut writer)), 0);
assert_eq!(writer.bytes, [1, 2, 3, 4, 5]);
assert_eq!(writer.calls, 3);
}
#[test]
fn fputs_reports_a_writer_that_makes_no_progress() {
struct ZeroWriter;
impl Write for ZeroWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Ok(0)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let (value, status) = fputs(&[1, 0], &mut ZeroWriter);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::WriteZero);
}
#[test]
fn fputs_propagates_write_errors_after_partial_output() {
struct FailingWriter {
bytes: Vec<u8>,
}
impl Write for FailingWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.bytes.is_empty() {
self.bytes.extend_from_slice(&buf[..2]);
Ok(2)
} else {
Err(io::Error::other("write failed"))
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut writer = FailingWriter { bytes: Vec::new() };
let (value, status) = fputs(&[1, 2, 3, 4, 0], &mut writer);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "write failed");
assert_eq!(writer.bytes, [1, 2]);
}
#[test]
fn fputs_does_not_retry_an_interrupted_write() {
struct InterruptedWriter {
calls: usize,
}
impl Write for InterruptedWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.calls += 1;
if self.calls == 1 {
Err(io::ErrorKind::Interrupted.into())
} else {
Ok(buf.len())
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut writer = InterruptedWriter { calls: 0 };
let (value, status) = fputs(&[1, 2, 0], &mut writer);
let error = status.unwrap_err();
assert_eq!(value, -1);
assert_eq!(error.kind(), io::ErrorKind::Interrupted);
assert_eq!(writer.calls, 1);
}
#[test]
fn fputs_accepts_dynamically_sized_writers() {
let mut bytes = Vec::new();
let writer: &mut dyn Write = &mut bytes;
assert_eq!(unwrap_stdio(fputs(&[1, 2, 0], writer)), 0);
assert_eq!(bytes, [1, 2]);
}
#[test]
fn fgets_reads_through_newline_and_returns_the_entire_buffer() {
let mut reader = Cursor::new(b"first line\nsecond line");
let mut buf = [99; 16];
let result = unwrap_stdio(fgets(&mut buf, &mut reader)).unwrap();
assert_eq!(result.len(), 16);
assert_eq!(
result,
&[
102, 105, 114, 115, 116, 32, 108, 105, 110, 101, 10, 0, 99, 99, 99, 99
]
);
assert_eq!(reader.fill_buf().unwrap(), b"second line");
}
#[test]
fn fgets_stops_when_the_buffer_is_full_without_overreading() {
let mut reader = Cursor::new(b"abcdef");
let mut first = [99; 4];
let mut second = [99; 4];
assert_eq!(
unwrap_stdio(fgets(&mut first, &mut reader)).unwrap(),
&[b'a' as i8, b'b' as i8, b'c' as i8, 0]
);
assert_eq!(
unwrap_stdio(fgets(&mut second, &mut reader)).unwrap(),
&[b'd' as i8, b'e' as i8, b'f' as i8, 0]
);
}
#[test]
fn fgets_leaves_a_newline_beyond_the_buffer_limit_for_the_next_call() {
let mut reader = Cursor::new(b"abc\ndef");
let mut first = [99; 4];
let mut second = [99; 4];
assert_eq!(
unwrap_stdio(fgets(&mut first, &mut reader)).unwrap(),
&[b'a' as i8, b'b' as i8, b'c' as i8, 0]
);
assert_eq!(
unwrap_stdio(fgets(&mut second, &mut reader)).unwrap(),
&[b'\n' as i8, 0, 99, 99]
);
assert_eq!(reader.fill_buf().unwrap(), b"def");
}
#[test]
fn fgets_reads_across_buffered_chunks() {
let mut reader = BufReader::with_capacity(2, Cursor::new(b"abc\ndef"));
let mut buf = [99; 8];
assert_eq!(
unwrap_stdio(fgets(&mut buf, &mut reader)).unwrap(),
&[
b'a' as i8,
b'b' as i8,
b'c' as i8,
b'\n' as i8,
0,
99,
99,
99
]
);
assert_eq!(reader.fill_buf().unwrap(), b"de");
}
#[test]
fn fgets_returns_none_at_eof_without_modifying_the_buffer() {
let mut reader = Cursor::new([]);
let mut buf = [99; 4];
assert!(unwrap_stdio(fgets(&mut buf, &mut reader)).is_none());
assert_eq!(buf, [99; 4]);
}
#[test]
fn fgets_returns_data_when_eof_follows_bytes() {
let mut reader = Cursor::new(b"abc");
let mut buf = [99; 5];
assert_eq!(
unwrap_stdio(fgets(&mut buf, &mut reader)).unwrap(),
&[b'a' as i8, b'b' as i8, b'c' as i8, 0, 99]
);
}
#[test]
fn fgets_treats_null_and_high_bytes_as_input() {
let mut reader = Cursor::new([0, 128, 255, b'\n', b'x']);
let mut buf = [99; 6];
assert_eq!(
unwrap_stdio(fgets(&mut buf, &mut reader)).unwrap(),
&[0, -128, -1, b'\n' as i8, 0, 99]
);
assert_eq!(reader.fill_buf().unwrap(), b"x");
}
#[test]
fn fgets_with_one_byte_buffer_writes_only_null_without_reading() {
let mut reader = Cursor::new(b"abc");
let mut buf = [99];
assert_eq!(unwrap_stdio(fgets(&mut buf, &mut reader)).unwrap(), &[0]);
assert_eq!(reader.position(), 0);
}
#[test]
fn fgets_accepts_dynamically_sized_buffered_readers() {
let mut bytes = Cursor::new(b"abc\n");
let reader: &mut dyn BufRead = &mut bytes;
let mut buf = [99; 5];
assert_eq!(
unwrap_stdio(fgets(&mut buf, reader)).unwrap(),
&[b'a' as i8, b'b' as i8, b'c' as i8, b'\n' as i8, 0]
);
}
#[test]
fn fgets_propagates_buffered_read_errors() {
struct FailingReader;
impl Read for FailingReader {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::other("read failed"))
}
}
impl BufRead for FailingReader {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
Err(io::Error::other("read failed"))
}
fn consume(&mut self, _amt: usize) {}
}
let mut buf = [99; 4];
let (value, status) = fgets(&mut buf, &mut FailingReader);
let error = status.unwrap_err();
assert!(value.is_none());
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "read failed");
}
#[test]
fn fgets_propagates_an_error_after_copying_available_bytes() {
struct PartialThenFail {
consumed: bool,
}
impl Read for PartialThenFail {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::other("read failed"))
}
}
impl BufRead for PartialThenFail {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
if self.consumed {
Err(io::Error::other("read failed"))
} else {
Ok(b"ab")
}
}
fn consume(&mut self, amt: usize) {
assert_eq!(amt, 2);
self.consumed = true;
}
}
let mut reader = PartialThenFail { consumed: false };
let mut buf = [99; 4];
let (value, status) = fgets(&mut buf, &mut reader);
let error = status.unwrap_err();
assert!(value.is_none());
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "read failed");
assert_eq!(buf, [b'a' as i8, b'b' as i8, 99, 99]);
}
#[test]
fn fread_reads_complete_elements() {
let mut reader = Cursor::new([1, 2, 3, 4]);
let mut buf = [0_u16; 2];
let (count, status) = fread(&mut buf, &mut reader);
assert_eq!(count, 2);
status.unwrap();
assert_eq!(
buf,
[u16::from_ne_bytes([1, 2]), u16::from_ne_bytes([3, 4])]
);
}
#[test]
fn fread_does_not_read_past_the_destination() {
let mut reader = Cursor::new(b"abcde");
let mut buf = [0_u8; 3];
let (count, status) = fread(&mut buf, &mut reader);
assert_eq!(count, 3);
status.unwrap();
assert_eq!(buf, *b"abc");
assert_eq!(reader.fill_buf().unwrap(), b"de");
}
#[test]
fn fread_returns_only_complete_elements_at_end_of_file() {
let mut reader = Cursor::new([1, 2, 3]);
let mut buf = [u16::from_ne_bytes([9, 9]); 2];
let (count, status) = fread(&mut buf, &mut reader);
assert_eq!(count, 1);
status.unwrap();
assert_eq!(bytemuck::cast_slice::<_, u8>(&buf), &[1, 2, 3, 9]);
}
#[test]
fn fread_reads_elements_across_buffered_chunks() {
let mut reader = BufReader::with_capacity(3, Cursor::new([1, 2, 3, 4, 5, 6, 7, 8]));
let mut buf = [0_u32; 2];
let (count, status) = fread(&mut buf, &mut reader);
assert_eq!(count, 2);
status.unwrap();
assert_eq!(
buf,
[
u32::from_ne_bytes([1, 2, 3, 4]),
u32::from_ne_bytes([5, 6, 7, 8])
]
);
}
#[test]
fn fread_does_not_read_for_an_empty_buffer() {
let mut reader = Cursor::new(b"abc");
let (count, status) = fread::<u8, _>(&mut [], &mut reader);
assert_eq!(count, 0);
status.unwrap();
assert_eq!(reader.position(), 0);
}
#[test]
fn fread_does_not_read_zero_sized_elements() {
let mut reader = Cursor::new(b"abc");
let mut buf = [(); 3];
let (count, status) = fread(&mut buf, &mut reader);
assert_eq!(count, 0);
status.unwrap();
assert_eq!(reader.position(), 0);
}
#[test]
fn fread_accepts_dynamically_sized_buffered_readers() {
let mut bytes = Cursor::new(b"abc");
let reader: &mut dyn BufRead = &mut bytes;
let mut buf = [0_u8; 3];
let (count, status) = fread(&mut buf, reader);
assert_eq!(count, 3);
status.unwrap();
assert_eq!(buf, *b"abc");
}
#[test]
fn fread_propagates_an_error_before_reading_any_bytes() {
struct FailingReader;
impl Read for FailingReader {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::other("read failed"))
}
}
impl BufRead for FailingReader {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
Err(io::Error::other("read failed"))
}
fn consume(&mut self, _amt: usize) {}
}
let mut buf = [9_u8; 2];
let (count, status) = fread(&mut buf, &mut FailingReader);
let error = status.unwrap_err();
assert_eq!(count, 0);
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "read failed");
assert_eq!(buf, [9; 2]);
}
#[test]
fn fread_returns_an_error_without_discarding_the_element_count() {
struct PartialThenFail {
consumed: bool,
}
impl Read for PartialThenFail {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::other("read failed"))
}
}
impl BufRead for PartialThenFail {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
if self.consumed {
Err(io::Error::other("read failed"))
} else {
Ok(b"abc")
}
}
fn consume(&mut self, amt: usize) {
assert_eq!(amt, 3);
self.consumed = true;
}
}
let mut reader = PartialThenFail { consumed: false };
let mut buf = [u16::from_ne_bytes([9, 9]); 2];
let (count, status) = fread(&mut buf, &mut reader);
let error = status.unwrap_err();
assert_eq!(count, 1);
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "read failed");
assert_eq!(bytemuck::cast_slice::<_, u8>(&buf), &[b'a', b'b', b'c', 9]);
}
#[test]
fn fread_accepts_any_bit_pattern_types_with_padding() {
#[repr(C)]
#[derive(Clone, Copy)]
struct Padded {
byte: u8,
number: u16,
}
unsafe impl bytemuck::Zeroable for Padded {}
unsafe impl bytemuck::AnyBitPattern for Padded {}
let mut reader = Cursor::new([1, 2, 3, 4]);
let mut buf = [Padded { byte: 0, number: 0 }];
let (count, status) = fread(&mut buf, &mut reader);
assert_eq!(count, 1);
status.unwrap();
assert_eq!(buf[0].byte, 1);
assert_eq!(buf[0].number, u16::from_ne_bytes([3, 4]));
}
#[test]
fn fwrite_writes_complete_elements() {
let buf = [u16::from_ne_bytes([1, 2]), u16::from_ne_bytes([3, 4])];
let mut writer = Vec::new();
let (count, status) = fwrite(&buf, &mut writer);
assert_eq!(count, 2);
status.unwrap();
assert_eq!(writer, [1, 2, 3, 4]);
}
#[test]
fn fwrite_accepts_no_uninit_types_that_are_not_pod() {
let mut writer = Vec::new();
let (count, status) = fwrite(&[true, false], &mut writer);
assert_eq!(count, 2);
status.unwrap();
assert_eq!(writer, [1, 0]);
}
#[test]
fn fwrite_does_not_write_an_empty_buffer() {
struct PanickingWriter;
impl Write for PanickingWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
panic!("write called for an empty buffer")
}
fn flush(&mut self) -> io::Result<()> {
panic!("flush called")
}
}
let (count, status) = fwrite::<u8, _>(&[], &mut PanickingWriter);
assert_eq!(count, 0);
status.unwrap();
}
#[test]
fn fwrite_does_not_write_zero_sized_elements() {
struct PanickingWriter;
impl Write for PanickingWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
panic!("write called for zero-sized elements")
}
fn flush(&mut self) -> io::Result<()> {
panic!("flush called")
}
}
let (count, status) = fwrite(&[(); 3], &mut PanickingWriter);
assert_eq!(count, 0);
status.unwrap();
}
#[test]
fn fwrite_completes_partial_writes_without_flushing() {
struct PartialWriter {
bytes: Vec<u8>,
offered_lengths: Vec<usize>,
}
impl Write for PartialWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.offered_lengths.push(buf.len());
let len = buf.len().min(3);
self.bytes.extend_from_slice(&buf[..len]);
Ok(len)
}
fn flush(&mut self) -> io::Result<()> {
panic!("flush called")
}
}
let buf = [
u16::from_ne_bytes([1, 2]),
u16::from_ne_bytes([3, 4]),
u16::from_ne_bytes([5, 6]),
];
let mut writer = PartialWriter {
bytes: Vec::new(),
offered_lengths: Vec::new(),
};
let (count, status) = fwrite(&buf, &mut writer);
assert_eq!(count, 3);
status.unwrap();
assert_eq!(writer.bytes, [1, 2, 3, 4, 5, 6]);
assert_eq!(writer.offered_lengths, [6, 3]);
}
#[test]
fn fwrite_reports_a_writer_that_makes_no_progress() {
struct ZeroWriter;
impl Write for ZeroWriter {
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Ok(0)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let (count, status) = fwrite(&[1_u8], &mut ZeroWriter);
let error = status.unwrap_err();
assert_eq!(count, 0);
assert_eq!(error.kind(), io::ErrorKind::WriteZero);
}
#[test]
fn fwrite_returns_only_complete_elements_when_writing_stalls() {
struct PartialThenZero {
bytes: Vec<u8>,
}
impl Write for PartialThenZero {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.bytes.is_empty() {
self.bytes.extend_from_slice(&buf[..3]);
Ok(3)
} else {
Ok(0)
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let buf = [u16::from_ne_bytes([1, 2]), u16::from_ne_bytes([3, 4])];
let mut writer = PartialThenZero { bytes: Vec::new() };
let (count, status) = fwrite(&buf, &mut writer);
let error = status.unwrap_err();
assert_eq!(count, 1);
assert_eq!(error.kind(), io::ErrorKind::WriteZero);
assert_eq!(writer.bytes, [1, 2, 3]);
}
#[test]
fn fwrite_returns_an_error_without_discarding_the_element_count() {
struct PartialThenFail {
bytes: Vec<u8>,
}
impl Write for PartialThenFail {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.bytes.is_empty() {
self.bytes.extend_from_slice(&buf[..3]);
Ok(3)
} else {
Err(io::Error::other("write failed"))
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let buf = [u16::from_ne_bytes([1, 2]), u16::from_ne_bytes([3, 4])];
let mut writer = PartialThenFail { bytes: Vec::new() };
let (count, status) = fwrite(&buf, &mut writer);
let error = status.unwrap_err();
assert_eq!(count, 1);
assert_eq!(error.kind(), io::ErrorKind::Other);
assert_eq!(error.to_string(), "write failed");
assert_eq!(writer.bytes, [1, 2, 3]);
}
#[test]
fn fwrite_does_not_retry_an_interrupted_write() {
struct InterruptedWriter {
calls: usize,
}
impl Write for InterruptedWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.calls += 1;
if self.calls == 1 {
Ok(2)
} else if self.calls == 2 {
Err(io::ErrorKind::Interrupted.into())
} else {
Ok(buf.len())
}
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut writer = InterruptedWriter { calls: 0 };
let (count, status) = fwrite(&[1_u16, 2], &mut writer);
let error = status.unwrap_err();
assert_eq!(count, 1);
assert_eq!(error.kind(), io::ErrorKind::Interrupted);
assert_eq!(writer.calls, 2);
}
#[test]
fn fwrite_accepts_dynamically_sized_writers() {
let mut bytes = Vec::new();
let writer: &mut dyn Write = &mut bytes;
let (count, status) = fwrite(&[1_u8, 2, 3], writer);
assert_eq!(count, 3);
status.unwrap();
assert_eq!(bytes, [1, 2, 3]);
}