Skip to content

Commit

Permalink
Merge pull request #5 from AngheloAlf/more-tests
Browse files Browse the repository at this point in the history
Add 2 more test cases and enable all tests for Yaz0
  • Loading branch information
ethteck committed Dec 6, 2023
2 parents 261e262 + 6e96e3f commit 91620d6
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/yaz0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ pub fn compress_yaz0(bytes: &[u8]) -> Box<[u8]> {
if cur_layout_bit == 0 {
cur_layout_bit = 0x80;
index_cur_layout_byte = index_out_ptr;
output.push(0);
index_out_ptr += 1;
output[index_cur_layout_byte] = 0;
}

group_size = new_size;
Expand Down Expand Up @@ -178,37 +178,61 @@ pub fn compress_yaz0(bytes: &[u8]) -> Box<[u8]> {

#[cfg(test)]
mod tests {
#[test]
fn test_matching_decompression() {
let compressed_file = include_bytes!("../test_data/small.txt.Yaz0");
let decompressed_file = include_bytes!("../test_data/small.txt");
use core::panic;
use rstest::rstest;
use std::{
fs::File,
io::{BufReader, Read},
path::PathBuf,
};

pub fn read_test_file(path: PathBuf) -> Vec<u8> {
let file = match File::open(path) {
Ok(file) => file,
Err(_error) => {
panic!("Failed to open file");
}
};

let mut buf_reader = BufReader::new(file);
let mut buffer = Vec::new();

let _ = buf_reader.read_to_end(&mut buffer);

buffer
}

#[rstest]
fn test_matching_decompression(#[files("test_data/*.Yaz0")] path: PathBuf) {
let compressed_file = &read_test_file(path.clone());
let decompressed_file = &read_test_file(path.with_extension(""));

let decompressed: Box<[u8]> = super::decompress_yaz0(compressed_file);
assert_eq!(decompressed_file, decompressed.as_ref());
}

#[test]
fn test_matching_compression() {
let compressed_file = include_bytes!("../test_data/small.txt.Yaz0");
let decompressed_file = include_bytes!("../test_data/small.txt");
#[rstest]
fn test_matching_compression(#[files("test_data/*.Yaz0")] path: PathBuf) {
let compressed_file = &read_test_file(path.clone());
let decompressed_file = &read_test_file(path.with_extension(""));

let compressed = super::compress_yaz0(decompressed_file.as_slice());
assert_eq!(compressed_file, compressed.as_ref());
}

#[test]
fn test_cycle_decompressed() {
let decompressed_file = include_bytes!("../test_data/small.txt");
#[rstest]
fn test_cycle_decompressed(#[files("test_data/*.Yaz0")] path: PathBuf) {
let decompressed_file = &read_test_file(path.with_extension(""));

assert_eq!(
decompressed_file,
super::decompress_yaz0(&super::compress_yaz0(decompressed_file.as_ref())).as_ref()
);
}

#[test]
fn test_cycle_compressed() {
let compressed_file = include_bytes!("../test_data/small.txt.Yaz0");
#[rstest]
fn test_cycle_compressed(#[files("test_data/*.Yaz0")] path: PathBuf) {
let compressed_file = &read_test_file(path);

assert_eq!(
compressed_file,
Expand Down
Binary file added test_data/dirt.png.Yaz0
Binary file not shown.
Binary file added test_data/dirt.png.bin.Yaz0
Binary file not shown.
Binary file added test_data/ground.png.Yaz0
Binary file not shown.
Binary file added test_data/ground.png.bin.Yaz0
Binary file not shown.
Binary file added test_data/mips_gist_wiseguy_yaz0.bin
Binary file not shown.
Binary file added test_data/mips_gist_wiseguy_yaz0.bin.Yay0
Binary file not shown.
Binary file added test_data/mips_gist_wiseguy_yaz0.bin.Yaz0
Binary file not shown.
Binary file added test_data/stones.png.Yaz0
Binary file not shown.
Binary file added test_data/stones.png.bin.Yaz0
Binary file not shown.
Binary file added test_data/tile.png.Yaz0
Binary file not shown.
Binary file added test_data/tile.png.bin.Yaz0
Binary file not shown.
Binary file added test_data/x86-64_rabbitizer.bin
Binary file not shown.
Binary file added test_data/x86-64_rabbitizer.bin.Yay0
Binary file not shown.
Binary file added test_data/x86-64_rabbitizer.bin.Yaz0
Binary file not shown.

0 comments on commit 91620d6

Please sign in to comment.