Skip to content

Commit

Permalink
test(integration): new tests to validate File's Read + Seek behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Oakchris1955 committed Jul 30, 2024
1 parent dd2823d commit ba128d5
Show file tree
Hide file tree
Showing 4 changed files with 1,419 additions and 1 deletion.
Binary file modified imgs/fat16.img
Binary file not shown.
2 changes: 1 addition & 1 deletion imgs/fat16.img.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148369b22ef287408233feda539365f34c03cd169f7d3bc360791dbbf5b56943 *imgs/fat16.img
4eddf671476c0ac64ca0ae3bf8837e07515082f89975ddd2ff43113fde521558 *imgs/fat16.img
55 changes: 55 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,61 @@ mod tests {
assert_eq!(file_string, EXPECTED_STR);
}

#[test]
fn read_huge_file() {
use std::io::Cursor;

static BEE_MOVIE_SCRIPT: &str = include_str!("../tests/bee movie script.txt");

let mut storage = Cursor::new(FAT16.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

let mut file = fs.get_file(PathBuf::from("/bee movie script.txt")).unwrap();
let mut file_bytes = [0_u8; 65536];
let bytes_read = file.read(&mut file_bytes).unwrap();

const EXPECTED_FILESIZE: usize = 49474;
assert_eq!(bytes_read, EXPECTED_FILESIZE);

//panic!("{}", String::from_utf8_lossy(&file_bytes[..bytes_read]));
let utf8_string = str::from_utf8(&file_bytes[..bytes_read]).unwrap();
assert_eq!(utf8_string, BEE_MOVIE_SCRIPT);
}

#[test]
fn seek_n_read() {
// this uses the famous "I'd like to interject for a moment" copypasta as a test file
// you can find it online by just searching this term

use std::io::Cursor;

let mut storage = Cursor::new(FAT16.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

let mut file = fs
.get_file(PathBuf::from("/GNU ⁄ Linux copypasta.txt"))
.unwrap();
let mut file_bytes = [0_u8; 4096];

// we first perform a forward seek...
const EXPECTED_STR1: &str = "Linux is the kernel";
file.seek(SeekFrom::Start(792)).unwrap();
let bytes_read = file.read(&mut file_bytes[..EXPECTED_STR1.len()]).unwrap();
assert_eq!(
String::from_utf8_lossy(&file_bytes[..bytes_read]),
EXPECTED_STR1
);

// ...then a backward one
const EXPECTED_STR2: &str = "What you're referring to as Linux, is in fact, GNU/Linux";
file.seek(SeekFrom::Start(39)).unwrap();
let bytes_read = file.read(&mut file_bytes[..EXPECTED_STR2.len()]).unwrap();
assert_eq!(
String::from_utf8_lossy(&file_bytes[..bytes_read]),
EXPECTED_STR2
);
}

#[test]
fn read_file_in_subdir() {
use std::io::Cursor;
Expand Down
Loading

0 comments on commit ba128d5

Please sign in to comment.