Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Mar 3, 2024
1 parent 62ca446 commit f682af8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
19 changes: 8 additions & 11 deletions src/core/src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@ mod test {

let b_hashes = b.to_vec();

let s1: HashSet<_> = a.mins().into_iter().collect();
let s1: HashSet<_> = a.iter_mins().copied().collect();
let s2: HashSet<_> = b_hashes.iter().map(|x| x.hash).collect();
let i1 = &s1 & &s2;

assert!(i1.len() == a.size());
assert!(i1.len() == b_hashes.len());

if let Some(abunds) = a.abunds() {
let mins = a.mins();
let smap: HashMap<_, _> = mins.iter().zip(abunds.iter()).collect();
let smap: HashMap<_, _> = a.iter_mins().zip(abunds.iter()).collect();
println!("{:?}", smap);
for item in b_hashes.iter() {
assert!(smap.contains_key(&{ item.hash }));
Expand All @@ -101,19 +100,17 @@ mod test {

let c = KmerMinHash::from(b);

let s1: HashSet<_> = a.mins().into_iter().collect();
let s2: HashSet<_> = c.mins().into_iter().collect();
let s1: HashSet<_> = a.iter_mins().collect();
let s2: HashSet<_> = c.iter_mins().collect();
let i1 = &s1 & &s2;

assert!(i1.len() == a.mins().len());
assert!(i1.len() == c.mins().len());
assert!(i1.len() == a.iter_mins().count());
assert!(i1.len() == c.iter_mins().count());

if let Some(a_abunds) = a.abunds() {
if let Some(c_abunds) = c.abunds() {
let a_mins = a.mins();
let a_smap: HashMap<_, _> = a_mins.iter().zip(a_abunds.iter()).collect();
let c_mins = c.mins();
let c_smap: HashMap<_, _> = c_mins.iter().zip(c_abunds.iter()).collect();
let a_smap: HashMap<_, _> = a.iter_mins().zip(a_abunds.iter()).collect();
let c_smap: HashMap<_, _> = c.iter_mins().zip(c_abunds.iter()).collect();
for item in a_smap.iter() {
assert!(c_smap.contains_key(*item.0));
assert!(c_smap.get(*item.0).unwrap() == item.1);
Expand Down
73 changes: 44 additions & 29 deletions src/core/tests/minhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use sourmash::signature::{Signature, SigsTrait};
use sourmash::sketch::minhash::{
max_hash_for_scaled, scaled_for_max_hash, KmerMinHash, KmerMinHashBTree,
};
use sourmash::sketch::Sketch;

// TODO: use f64::EPSILON when we bump MSRV
const EPSILON: f64 = 0.01;
Expand Down Expand Up @@ -58,11 +57,11 @@ fn invalid_dna() {
let mut a = KmerMinHash::new(0, 3, HashFunctions::Murmur64Dna, 42, false, 20);

a.add_sequence(b"AAANNCCCTN", true).unwrap();
assert_eq!(a.mins().len(), 3);
assert_eq!(a.iter_mins().count(), 3);

let mut b = KmerMinHash::new(0, 3, HashFunctions::Murmur64Dna, 42, false, 20);
b.add_sequence(b"NAAA", true).unwrap();
assert_eq!(b.mins().len(), 1);
assert_eq!(b.iter_mins().count(), 1);
}

#[test]
Expand Down Expand Up @@ -209,8 +208,8 @@ fn oracle_mins(hashes in vec(u64::ANY, 1..10000)) {
d.add_from(&b).unwrap();
d.remove_many(to_remove.iter().copied()).unwrap();

assert_eq!(a.mins(), b.mins());
assert_eq!(c.mins(), d.mins());
assert!(a.iter_mins().zip(b.iter_mins()).all(|(a, b)| a == b));
assert!(c.iter_mins().zip(d.iter_mins()).all(|(a, b)| a == b));

assert_eq!(a.count_common(&c, false).unwrap(), b.count_common(&d, false).unwrap());
assert_eq!(a.count_common(&c, true).unwrap(), b.count_common(&d, true).unwrap());
Expand Down Expand Up @@ -251,8 +250,8 @@ fn oracle_mins_scaled(hashes in vec(u64::ANY, 1..10000)) {
a.remove_hash(hashes[0]);
b.remove_hash(hashes[0]);

assert_eq!(a.mins(), b.mins());
assert_eq!(c.mins(), d.mins());
assert!(a.iter_mins().zip(b.iter_mins()).all(|(a, b)| a == b));
assert!(c.iter_mins().zip(d.iter_mins()).all(|(a, b)| a == b));

assert_eq!(a.md5sum(), b.md5sum());
assert_eq!(c.md5sum(), d.md5sum());
Expand Down Expand Up @@ -343,8 +342,8 @@ fn prop_merge(seq1 in "[ACGT]{6,100}", seq2 in "[ACGT]{6,200}") {
a.merge(&c).unwrap();
b.merge(&d).unwrap();

assert_eq!(a.mins(), b.mins());
assert_eq!(c.mins(), d.mins());
assert!(a.iter_mins().zip(b.iter_mins()).all(|(a, b)| a == b));
assert!(c.iter_mins().zip(d.iter_mins()).all(|(a, b)| a == b));

assert_eq!(a.abunds(), b.abunds());
assert_eq!(c.abunds(), d.abunds());
Expand Down Expand Up @@ -402,10 +401,13 @@ fn load_save_minhash_sketches() {
assert_eq!(bmh.md5sum(), new_mh.md5sum());
assert_eq!(mh.md5sum(), new_bmh.md5sum());

assert_eq!(mh.mins(), new_mh.mins());
assert_eq!(bmh.mins(), new_bmh.mins());
assert_eq!(bmh.mins(), new_mh.mins());
assert_eq!(mh.mins(), new_bmh.mins());
assert!(mh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(bmh
.iter_mins()
.zip(new_bmh.iter_mins())
.all(|(a, b)| a == b));
assert!(bmh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(mh.iter_mins().zip(new_bmh.iter_mins()).all(|(a, b)| a == b));

assert_eq!(mh.abunds(), new_mh.abunds());
assert_eq!(bmh.abunds(), new_bmh.abunds());
Expand Down Expand Up @@ -440,10 +442,13 @@ fn load_save_minhash_sketches() {
assert_eq!(bmh.md5sum(), new_mh.md5sum());
assert_eq!(mh.md5sum(), new_bmh.md5sum());

assert_eq!(mh.mins(), new_mh.mins());
assert_eq!(bmh.mins(), new_bmh.mins());
assert_eq!(bmh.mins(), new_mh.mins());
assert_eq!(mh.mins(), new_bmh.mins());
assert!(mh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(bmh
.iter_mins()
.zip(new_bmh.iter_mins())
.all(|(a, b)| a == b));
assert!(bmh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(mh.iter_mins().zip(new_bmh.iter_mins()).all(|(a, b)| a == b));

assert_eq!(mh.abunds(), new_mh.abunds());
assert_eq!(bmh.abunds(), new_bmh.abunds());
Expand Down Expand Up @@ -501,10 +506,13 @@ fn load_save_minhash_sketches_abund() {
assert_eq!(bmh.md5sum(), new_mh.md5sum());
assert_eq!(mh.md5sum(), new_bmh.md5sum());

assert_eq!(mh.mins(), new_mh.mins());
assert_eq!(bmh.mins(), new_bmh.mins());
assert_eq!(bmh.mins(), new_mh.mins());
assert_eq!(mh.mins(), new_bmh.mins());
assert!(mh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(bmh
.iter_mins()
.zip(new_bmh.iter_mins())
.all(|(a, b)| a == b));
assert!(bmh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(mh.iter_mins().zip(new_bmh.iter_mins()).all(|(a, b)| a == b));

assert_eq!(mh.abunds(), new_mh.abunds());
assert_eq!(bmh.abunds(), new_bmh.abunds());
Expand Down Expand Up @@ -549,10 +557,13 @@ fn load_save_minhash_sketches_abund() {
assert_eq!(bmh.md5sum(), new_mh.md5sum());
assert_eq!(mh.md5sum(), new_bmh.md5sum());

assert_eq!(mh.mins(), new_mh.mins());
assert_eq!(bmh.mins(), new_bmh.mins());
assert_eq!(bmh.mins(), new_mh.mins());
assert_eq!(mh.mins(), new_bmh.mins());
assert!(mh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(bmh
.iter_mins()
.zip(new_bmh.iter_mins())
.all(|(a, b)| a == b));
assert!(bmh.iter_mins().zip(new_mh.iter_mins()).all(|(a, b)| a == b));
assert!(mh.iter_mins().zip(new_bmh.iter_mins()).all(|(a, b)| a == b));

assert_eq!(mh.abunds(), new_mh.abunds());
assert_eq!(bmh.abunds(), new_bmh.abunds());
Expand Down Expand Up @@ -755,10 +766,12 @@ fn seq_to_hashes(seq in "ACGTGTAGCTAGACACTGACTGACTGAC") {
}
}

mh.mins().sort_unstable();
hashes.sort_unstable();
assert_eq!(mh.mins(), hashes);
assert!(mh.iter_mins().zip(hashes.iter()).all(|(a, b)| a == b));

let mut mins: Vec<_> = mh.iter_mins().copied().collect();
mins.sort_unstable();
assert!(mins.iter().zip(hashes.iter()).all(|(a, b)| a == b));
}

#[test]
Expand All @@ -778,10 +791,12 @@ fn seq_to_hashes_2(seq in "QRMTHINK") {
}
}

mh.mins().sort_unstable();
hashes.sort_unstable();
assert_eq!(mh.mins(), hashes);
assert!(mh.iter_mins().zip(hashes.iter()).all(|(a, b)| a == b));

let mut mins: Vec<_> = mh.iter_mins().copied().collect();
mins.sort_unstable();
assert!(mins.iter().zip(hashes.iter()).all(|(a, b)| a == b));
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/core/tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ fn zipstorage_list_sbts() -> Result<(), Box<dyn std::error::Error>> {
fn zipstorage_parallel_access() -> Result<(), Box<dyn std::error::Error>> {
use rayon::prelude::*;

use sourmash::signature::SigsTrait;

let mut filename = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
filename.push("../../tests/test-data/v6.sbt.zip");

Expand Down

0 comments on commit f682af8

Please sign in to comment.