Skip to content

Commit

Permalink
chore(test): add a test to verify get_topo_and_suf_len_sorted_node_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
ChieloNewctle committed Oct 18, 2023
1 parent 595ac24 commit 2267184
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
68 changes: 68 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ exclude = ["release-plz.toml", "cliff.tolm"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "general_sam"

[dev-dependencies]
rand = "0.8.5"
58 changes: 57 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{sam::GeneralSAM, trie::Trie};
use rand::{
distributions::{Alphanumeric, DistString},
rngs::StdRng,
Rng, SeedableRng,
};

use crate::{sam::GeneralSAM, trie::Trie, SAM_ROOT_NODE_ID};

#[test]
fn test_example_from_chars() {
Expand Down Expand Up @@ -165,3 +171,53 @@ fn test_simple_trie_suffix() {
let vocab = ["ac", "bb", "b", "cc", "aabb", "a", "ba", "c", "aa"];
test_trie_suffix(&vocab);
}

#[test]
fn test_topo_and_suf_len_sorted_order() {
let mut rng = StdRng::seed_from_u64(1134759173975);
for _ in 0..10000 {
let mut trie = Trie::default();
for _ in 0..rng.gen_range(0..32) {
let len = rng.gen_range(0..9);
let string = Alphanumeric.sample_string(&mut rng, len);
trie.insert_ref_iter(string.as_bytes().iter());
}

let sam: GeneralSAM<u8> = GeneralSAM::construct_from_trie(trie.get_root_state());

let order = sam.get_topo_and_suf_len_sorted_node_ids();
let rank = {
let mut rank = vec![0; sam.num_of_nodes()];
order.iter().enumerate().for_each(|(k, i)| {
rank[*i] = k;
});
rank
};

// verify that max suffix lengths should be sorted
for pos in 0..order.len() - 1 {
assert!(
sam.get_node(order[pos]).unwrap().max_suffix_len()
<= sam.get_node(order[pos + 1]).unwrap().max_suffix_len()
);
}

// verify topological ordering
order.iter().for_each(|node_id| {
let node = sam.get_node(*node_id).unwrap();

node.get_trans().values().for_each(|next_node_id| {
assert!(rank[*next_node_id] > rank[*node_id]);
});
});

// verify suffix parent tree depth ordering
order.iter().for_each(|node_id| {
let node = sam.get_node(*node_id).unwrap();

if *node_id != SAM_ROOT_NODE_ID {
assert!(rank[node.get_suffix_parent_id()] < rank[*node_id]);
}
});
}
}

0 comments on commit 2267184

Please sign in to comment.