Skip to content

Commit

Permalink
Improving tests performance
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed May 17, 2021
1 parent a4ca0a6 commit f4e1798
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ oof = { path = "./oof" }

[dev-dependencies]
tempdir = "0.3.7"
rand = "0.8.3"
rand = { version = "0.8.3", default-features = false, features = ["small_rng", "std"] }

[profile.release]
lto = true
Expand Down
49 changes: 26 additions & 23 deletions tests/compress_and_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ use std::{
};

use ouch::{cli::Command, commands::run};
use rand::random;
use rand::{rngs::SmallRng, RngCore, SeedableRng};
use tempdir::TempDir;

#[test]
/// Tests each format that supports multiple files with random input.
/// TODO: test the remaining formats.
/// TODO2: Fix testing of .tar.zip and .zip.zip
fn test_each_format() {
test_compression_and_decompression("tar");
test_compression_and_decompression("tar.gz");
test_compression_and_decompression("tar.bz");
test_compression_and_decompression("tar.bz2");
test_compression_and_decompression("tar.xz");
test_compression_and_decompression("tar.lz");
test_compression_and_decompression("tar.lzma");
// test_compression_and_decompression("tar.zip");
test_compression_and_decompression("zip");
test_compression_and_decompression("zip.gz");
test_compression_and_decompression("zip.bz");
test_compression_and_decompression("zip.bz2");
test_compression_and_decompression("zip.xz");
test_compression_and_decompression("zip.lz");
test_compression_and_decompression("zip.lzma");
// test_compression_and_decompression("zip.zip");
let mut rng = SmallRng::from_entropy();
test_compression_and_decompression(&mut rng, "tar");
test_compression_and_decompression(&mut rng, "tar.gz");
test_compression_and_decompression(&mut rng, "tar.bz");
test_compression_and_decompression(&mut rng, "tar.bz2");
test_compression_and_decompression(&mut rng, "tar.xz");
test_compression_and_decompression(&mut rng, "tar.lz");
test_compression_and_decompression(&mut rng, "tar.lzma");
// test_compression_and_decompression(&mut rng, "tar.zip");
test_compression_and_decompression(&mut rng, "zip");
test_compression_and_decompression(&mut rng, "zip.gz");
test_compression_and_decompression(&mut rng, "zip.bz");
test_compression_and_decompression(&mut rng, "zip.bz2");
test_compression_and_decompression(&mut rng, "zip.xz");
test_compression_and_decompression(&mut rng, "zip.lz");
test_compression_and_decompression(&mut rng, "zip.lzma");
// test_compression_and_decompression(&mut rng, "zip.zip");
}

type FileContent = Vec<u8>;

fn test_compression_and_decompression(format: &str) {
fn test_compression_and_decompression(rng: &mut impl RngCore, format: &str) {
// System temporary directory depends on the platform
// For linux it is /tmp
let system_tmp = env::temp_dir();
Expand All @@ -43,10 +44,10 @@ fn test_compression_and_decompression(format: &str) {
let testing_dir = testing_dir.path();

// Quantity of compressed files vary from 1 to 10
let quantity_of_files = random::<u32>() % 10 + 1;
let quantity_of_files = rng.next_u32() % 10 + 1;

let contents_of_files: Vec<FileContent> =
(0..quantity_of_files).map(|_| generate_random_file_content()).collect();
(0..quantity_of_files).map(|_| generate_random_file_content(rng)).collect();

let mut file_paths = create_files(&testing_dir, &contents_of_files);
let archive_path = compress_files(&testing_dir, &file_paths, &format);
Expand All @@ -63,9 +64,11 @@ fn test_compression_and_decompression(format: &str) {
}

// Crate file contents from 1024 up to 8192 random bytes
fn generate_random_file_content() -> FileContent {
let quantity = 1024 + random::<u32>() % (8192 - 1024);
(0..quantity).map(|_| random()).collect()
fn generate_random_file_content(rng: &mut impl RngCore) -> FileContent {
let quantity = 1024 + rng.next_u32() % (8192 - 1024);
let mut vec = vec![0; quantity as usize];
rng.fill_bytes(&mut vec);
vec
}

// Create files using the indexes as file names (eg. 0, 1, 2 and 3)
Expand Down

0 comments on commit f4e1798

Please sign in to comment.