更新单元测试case

This commit is contained in:
Ekko.bao 2024-07-06 00:52:04 +08:00
parent 171a1fa2af
commit 77ae3e7bac
2 changed files with 70 additions and 20 deletions

View File

@ -6,6 +6,7 @@ use std::io::{self, Seek as ioSeek};
use crate::lws_vfs::{FileInfo, Fstat};
extern crate winapi;
use fs::remove_file;
use io::Write;
use std::os::windows::fs::MetadataExt;
use std::os::windows::prelude::*;
@ -42,7 +43,10 @@ pub fn fread(
) -> Result<i32, Box<dyn Error>> {
let mut file = fs::File::open(path)?;
match file.seek_read(buffer, offsize as u64) {
Ok(_) => {}
Ok(size) => {
println!("size is:{}, buffer is {}", size, String::from_utf8(buffer.to_vec()).unwrap());
buffer.resize(size, 0)
}
Err(_) => {
buffer.resize(file.stream_position()? as usize - offsize, 0);
}
@ -50,16 +54,13 @@ pub fn fread(
Ok(0)
}
pub fn fwrite(
path: &String,
buffer: &Vec<u8>,
size: &mut usize,
offsize: usize,
_fi: &mut FileInfo,
pub fn fwrite(path: &String, buffer: &Vec<u8>, size: &mut usize, offsize: usize, _fi: &mut FileInfo,
) -> Result<i32, Box<dyn Error>> {
let mut file = fs::File::open(path)?;
let mut file = fs::File::options().write(true).create(true).open(path)?;
match file.seek_write(buffer, offsize as u64) {
Ok(_) => {}
Ok(written) => {
*size = written as usize;
}
Err(_) => {
*size = file.stream_position()? as usize - offsize;
}
@ -177,21 +178,72 @@ pub fn faccess(path: &String, mask: u32) -> Result<i32, Box<dyn Error>> {
}
#[test]
fn test() {
fn test_fread() {
use crate::fs_impl::*;
let path = String::from("test.txt");
let mut file: fs::File = fs::File::open("test.txt").unwrap();
let _ = file.seek_write(b"This is a test file.\n", 0);
drop(file);
let mut size = 0;
let mut offsize: usize = 0;
let path = String::from("test_fread.txt");
let file: fs::File = fs::File::options().write(true).create(true).open(&path).unwrap();
let ref_buffer = b"This is a test file.";
let _ = file.seek_write(ref_buffer, 0);
let mut fi = FileInfo::default();
size = 4;
offsize = 5;
//read from file middle
let mut size = 4;
let mut offsize: usize = 5;
let mut buffer = vec![0; size];
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
assert_eq!(buffer.len(), 4);
assert_eq!(buffer, b"is a");
//read from file head
offsize = 0;
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
assert_eq!(buffer.len(), 4);
assert_eq!(buffer, b"This");
// read from the file tail, read len > file remain
offsize = ref_buffer.len() - 2;
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
assert_eq!(buffer.len(), 2);
assert_eq!(buffer, b"e.");
// cleanup
let _ = fs::remove_file(path);
}
#[test]
fn test_fwrite() {
use crate::fs_impl::*;
let path = String::from("test_fwrite.txt");
let _ = fs::remove_file(&path);
let mut buffer = Vec::from("This is a test file");
let mut size = buffer.len();
//write from file head
let mut offsize: usize = 0;
let mut fi = FileInfo::default();
let _ = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
assert_eq!(size, buffer.len());
//write from file middle
offsize = 5;
let _ = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
assert_eq!(size, buffer.len());
let file_metadata = fs::metadata(&path).unwrap();
let file_size = file_metadata.len();
assert_eq!(file_size, (buffer.len() + offsize) as u64);
//write from file ended
offsize = file_size as usize + 10;
let _ = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
assert_eq!(size, buffer.len());
let file_metadata = fs::metadata(&path).unwrap();
let file_size = file_metadata.len();
assert_eq!(file_size, (buffer.len() + offsize) as u64);
// cleanup
//let _ = fs::remove_file(path);
}
// let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
// println!("result: {:?}", result);
// println!("size: {:?}", size);
@ -215,4 +267,3 @@ fn test() {
// println!("result: {:?}", result);
// let result = faccess(&path, 0o777);
// println!("result: {:?}", result);
}

View File

@ -1 +0,0 @@
This is a test file.