更新单元测试case
This commit is contained in:
parent
171a1fa2af
commit
77ae3e7bac
|
@ -6,6 +6,7 @@ use std::io::{self, Seek as ioSeek};
|
||||||
use crate::lws_vfs::{FileInfo, Fstat};
|
use crate::lws_vfs::{FileInfo, Fstat};
|
||||||
|
|
||||||
extern crate winapi;
|
extern crate winapi;
|
||||||
|
use fs::remove_file;
|
||||||
use io::Write;
|
use io::Write;
|
||||||
use std::os::windows::fs::MetadataExt;
|
use std::os::windows::fs::MetadataExt;
|
||||||
use std::os::windows::prelude::*;
|
use std::os::windows::prelude::*;
|
||||||
|
@ -42,7 +43,10 @@ pub fn fread(
|
||||||
) -> Result<i32, Box<dyn Error>> {
|
) -> Result<i32, Box<dyn Error>> {
|
||||||
let mut file = fs::File::open(path)?;
|
let mut file = fs::File::open(path)?;
|
||||||
match file.seek_read(buffer, offsize as u64) {
|
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(_) => {
|
Err(_) => {
|
||||||
buffer.resize(file.stream_position()? as usize - offsize, 0);
|
buffer.resize(file.stream_position()? as usize - offsize, 0);
|
||||||
}
|
}
|
||||||
|
@ -50,16 +54,13 @@ pub fn fread(
|
||||||
Ok(0)
|
Ok(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fwrite(
|
pub fn fwrite(path: &String, buffer: &Vec<u8>, size: &mut usize, offsize: usize, _fi: &mut FileInfo,
|
||||||
path: &String,
|
|
||||||
buffer: &Vec<u8>,
|
|
||||||
size: &mut usize,
|
|
||||||
offsize: usize,
|
|
||||||
_fi: &mut FileInfo,
|
|
||||||
) -> Result<i32, Box<dyn Error>> {
|
) -> 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) {
|
match file.seek_write(buffer, offsize as u64) {
|
||||||
Ok(_) => {}
|
Ok(written) => {
|
||||||
|
*size = written as usize;
|
||||||
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
*size = file.stream_position()? as usize - offsize;
|
*size = file.stream_position()? as usize - offsize;
|
||||||
}
|
}
|
||||||
|
@ -177,21 +178,72 @@ pub fn faccess(path: &String, mask: u32) -> Result<i32, Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test_fread() {
|
||||||
use crate::fs_impl::*;
|
use crate::fs_impl::*;
|
||||||
let path = String::from("test.txt");
|
let path = String::from("test_fread.txt");
|
||||||
let mut file: fs::File = fs::File::open("test.txt").unwrap();
|
let file: fs::File = fs::File::options().write(true).create(true).open(&path).unwrap();
|
||||||
let _ = file.seek_write(b"This is a test file.\n", 0);
|
|
||||||
drop(file);
|
let ref_buffer = b"This is a test file.";
|
||||||
let mut size = 0;
|
let _ = file.seek_write(ref_buffer, 0);
|
||||||
let mut offsize: usize = 0;
|
|
||||||
let mut fi = FileInfo::default();
|
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 mut buffer = vec![0; size];
|
||||||
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
|
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
|
||||||
assert_eq!(buffer.len(), 4);
|
assert_eq!(buffer.len(), 4);
|
||||||
assert_eq!(buffer, b"is a");
|
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);
|
// let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
|
||||||
// println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
// println!("size: {:?}", size);
|
// println!("size: {:?}", size);
|
||||||
|
@ -215,4 +267,3 @@ fn test() {
|
||||||
// println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
// let result = faccess(&path, 0o777);
|
// let result = faccess(&path, 0o777);
|
||||||
// println!("result: {:?}", result);
|
// println!("result: {:?}", result);
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user