Skip to content

Commit

Permalink
temp files in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Jan 14, 2024
1 parent bd99d2e commit 9df4f80
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 54 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ Cargo.lock
/data/index_unit_test/index
/data/test
/data/small
/data/illinois
/data/illinois

# TODO
todo.md
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rust-stemmers = "1.2.0"
rayon = "1.8.0"
indicatif = {version = "0.17.0", features = ["rayon", "improved_unicode"]}
fxhash = "0.2.1"
tempdir = "0.3.7"
17 changes: 10 additions & 7 deletions src/disk/bits_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ impl BitsReader {
}

#[cfg(test)]
mod test {

mod tests {
use super::*;
use crate::disk::bits_writer::BitsWriter;
use crate::{disk::bits_writer::BitsWriter, test_utils::utils::create_temporary_file_path};

#[test]
fn test_read() {
let mut w = BitsWriter::new("data/test/writer_unit.bin");
let test_output_path = create_temporary_file_path("test_read");

let mut w = BitsWriter::new(&test_output_path);

(1..100).for_each(|i| {
w.write_vbyte(i);
Expand All @@ -160,7 +161,7 @@ mod test {

w.flush();

let mut r = BitsReader::new("data/test/writer_unit.bin");
let mut r = BitsReader::new(&test_output_path);

(1..100).for_each(|i| assert_eq!(i, r.read_vbyte()));
(1..100).for_each(|i| assert_eq!(i, r.read_gamma()));
Expand All @@ -173,14 +174,16 @@ mod test {

#[test]
fn test_seek() {
let mut w = BitsWriter::new("data/test/writer_seek.bin");
let test_output_path = create_temporary_file_path("test_seek");

let mut w = BitsWriter::new(&test_output_path);

let offset = (0..1000).map(|i| w.write_gamma(i)).sum();
w.write_gamma(10);

w.flush();

let mut r = BitsReader::new("data/test/writer_seek.bin");
let mut r = BitsReader::new(&test_output_path);

r.seek(offset);
assert_eq!(r.read_gamma(), 10);
Expand Down
5 changes: 2 additions & 3 deletions src/disk/bits_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl BitsWriter {
#[cfg(test)]
mod test {

use super::*;
use crate::{disk::bits_writer::BitsWriter, test_utils::utils::create_temporary_file_path};

#[test]
fn test_gamma_coding() {
Expand Down Expand Up @@ -132,13 +132,12 @@ mod test {
let word = (1 << 10) - 1;
let len = 10;

let mut w = BitsWriter::new("data/test/writer.bin");
let mut w = BitsWriter::new(&create_temporary_file_path("overflow"));
w.written = 125;

w.write_internal(word, len);

let b = w.buffer;
println!("{:b}", b);
assert_eq!(b, (1 << 7) - 1)
}
}
11 changes: 6 additions & 5 deletions src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,21 @@ impl Display for Index {

#[cfg(test)]
mod test {
use crate::test_utils::utils::create_temporary_dir_path;

use super::*;

#[test]
fn test_build() {
let index_path = &create_temporary_dir_path();

Index::build_index(
"data/index_unit_test/docs",
"data/index_unit_test/index/test",
index_path,
"data/index_unit_test/test_tokenizer",
);

let mut idx = Index::load_index(
"data/index_unit_test/index/test",
"data/index_unit_test/test_tokenizer",
);
let mut idx = Index::load_index(index_path, "data/index_unit_test/test_tokenizer");

for ele in ["hello", "man", "world"] {
assert!(idx.term_offset_map.contains_key(ele));
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod disk;
pub mod index;
pub mod query;
mod test_utils;
19 changes: 19 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(test)]
pub mod utils {
use tempdir::TempDir;

pub fn create_temporary_dir_path() -> String {
TempDir::new("tmp")
.expect("Failed to create temporary directory")
.path()
.to_str()
.unwrap()
.to_string()
}

pub fn create_temporary_file_path(prefix: &str) -> String {
let temp_dir = TempDir::new("tmp").expect("Failed to create temporary directory");
let file_path = temp_dir.path().join(prefix);
file_path.to_str().unwrap().to_string()
}
}
38 changes: 0 additions & 38 deletions tests/read_write_integration_test.rs

This file was deleted.

0 comments on commit 9df4f80

Please sign in to comment.