Skip to content

Commit

Permalink
Add a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlees committed May 26, 2023
1 parent f7f2486 commit 20eac85
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion src/ska_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::ska_dict::count_min_filter::KmerFilter;
pub mod nthash;

/// Holds the split-kmer dictionary, and basic information such as k-mer size.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct SkaDict<IntT> {
/// K-mer size
k: usize,
Expand Down Expand Up @@ -256,3 +256,49 @@ where
&self.name
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_add_palindrome_to_dict() {
// Initialize the test subject
let mut test_obj = SkaDict::<u64>::default(); // Replace YourStruct with the actual struct name

// Test case 1: Updating existing entry
test_obj.split_kmers.insert(123, b'W');
test_obj.add_palindrome_to_dict(123, 1);
assert_eq!(test_obj.split_kmers[&123], b'N');

// Test case 2: Adding new entry with base 0
test_obj.split_kmers.clear();
test_obj.add_palindrome_to_dict(456, 0);
assert_eq!(test_obj.split_kmers[&456], b'W');

// Test case 3: Adding new entry with base 3
test_obj.split_kmers.clear();
test_obj.add_palindrome_to_dict(789, 3);
assert_eq!(test_obj.split_kmers[&789], b'S');
}

#[test]
#[should_panic]
fn test_panic_add_palindrome_to_dict() {
// Test case 4: Panicking with invalid base
let mut test_obj_panic = SkaDict::<u64>::default();
test_obj_panic.add_palindrome_to_dict(987, 5);
}

#[test]
#[should_panic]
fn test_panic2_add_palindrome_to_dict() {
// Test case 5: Panicking with invalid middle base
let mut test_obj_panic = SkaDict::<u64>::default();
test_obj_panic.split_kmers.clear();
test_obj_panic.split_kmers.insert(555, b'A');
test_obj_panic.add_palindrome_to_dict(555, 1);
}
}

2 changes: 1 addition & 1 deletion src/ska_dict/count_min_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const CM_WIDTH: usize = 1 << 24;
/// This has the advantage of using less memory than a larger countmin filter,
/// being a bit faster (bloom is ~3x faster than countmin, but having count also
/// allows entry to dictionary to be only checked once for each passing k-mer)
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct KmerFilter {
/// Size of the bloom filter
buf_size: u64,
Expand Down

0 comments on commit 20eac85

Please sign in to comment.