Skip to content

Commit

Permalink
removed methods from bin, added tests to node and edge iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
sriram98v committed Oct 12, 2023
1 parent aa6fc91 commit 5d794b4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
66 changes: 33 additions & 33 deletions src/bin/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,49 @@ fn save_tree(tree: &mut KGST<char, String>, output_path: String){
println!("Saved");
}

fn save_tree_strings(tree: &mut KGST<char, String>, output_path: String){
println!("Saving tree strings to {}.", &output_path);
std::fs::write(
output_path,
serde_json::to_string_pretty(&tree.export_all_string_nodes()).unwrap(),
).unwrap();
println!("Saved");
}
// fn save_tree_strings(tree: &mut KGST<char, String>, output_path: String){
// println!("Saving tree strings to {}.", &output_path);
// std::fs::write(
// output_path,
// serde_json::to_string_pretty(&tree.export_all_string_nodes()).unwrap(),
// ).unwrap();
// println!("Saved");
// }

fn save_nodes(tree: &mut KGST<char, String>, output_path: String){
println!("Saving tree nodes to {}.", &output_path);
let tree_nodes = &tree.export_all_nodes();
println!("Writing nodes");
std::fs::write(
output_path.clone(),
serde_json::to_string_pretty(&tree_nodes.0).expect("Node export error"),
).unwrap();
let mut strings_path = output_path.clone();
strings_path.push_str("string.json");
std::fs::write(
strings_path,
&tree_nodes.1.iter().map(|(k, v)|{
let feature_str = (0..tree.num_nodes()).map(|idx| (match v.contains(&idx){
true => "1".to_string(),
false => "0".to_string(),
})).collect::<String>();
return format!("{},{}", k.clone(), feature_str);
}).collect::<Vec<String>>()
.join("\n"),
serde_json::to_string_pretty(&tree_nodes).expect("Node export error"),
).unwrap();
// let mut strings_path = output_path.clone();
// strings_path.push_str("string.json");
// std::fs::write(
// strings_path,
// &tree_nodes.1.iter().map(|(k, v)|{
// let feature_str = (0..tree.num_nodes()).map(|idx| (match v.contains(&idx){
// true => "1".to_string(),
// false => "0".to_string(),
// })).collect::<String>();
// return format!("{},{}", k.clone(), feature_str);
// }).collect::<Vec<String>>()
// .join("\n"),
// ).unwrap();
println!("Saved");
}

fn save_edges(tree: &mut KGST<char, String>, output_path: String){
println!("Saving tree nodes to {}.", &output_path);
let tree_edges = &tree.export_all_edges();
println!("Writing nodes");
std::fs::write(
output_path,
tree_edges.join("\n"),
).unwrap();
println!("Saved");
}
// fn save_edges(tree: &mut KGST<char, String>, output_path: String){
// println!("Saving tree nodes to {}.", &output_path);
// let tree_edges = &tree.export_all_edges();
// println!("Writing nodes");
// std::fs::write(
// output_path,
// tree_edges.join("\n"),
// ).unwrap();
// println!("Saved");
// }

fn main(){
let matches = Command::new("Generalized suffix tree")
Expand Down Expand Up @@ -187,7 +187,7 @@ fn main(){
sub_m.get_one::<usize>("num").expect("required"),
sub_m.get_one::<usize>("depth").expect("required")
);
save_edges(&mut tree, sub_m.get_one::<String>("out").expect("required").to_string());
// save_edges(&mut tree, sub_m.get_one::<String>("out").expect("required").to_string());
},
_ => {
println!("Either build a tree or query an existing tree. Refer help page (-h flag)");
Expand Down
29 changes: 29 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ fn serialize_deserialize_tree(){
assert_eq!(tree.get_nodes(), tree_2.get_nodes());
}

#[test]
fn preorder_nodes(){
let mut tree: KGST<char, String> = KGST::new('$');
let item_string:Vec<char> = "abcabxabcd".chars().collect();
let item_id:String = "World".to_string();
tree.insert(item_id.clone(), item_string.clone(), &0);
assert_eq!(tree.iter_nodes_pre().collect::<Vec<usize>>(), vec![0, 16, 15, 13, 14, 3, 8, 6, 11, 12, 2, 7, 4, 9, 10, 1, 5]);
}

#[test]
fn postorder_nodes(){
let mut tree: KGST<char, String> = KGST::new('$');
let item_string:Vec<char> = "abcabxabcd".chars().collect();
let item_id:String = "World".to_string();
tree.insert(item_id.clone(), item_string.clone(), &0);
let node_post = tree.iter_nodes_post();
assert_eq!(node_post.collect::<Vec<usize>>(), vec![16, 15, 14, 3, 13, 8, 12, 2, 11, 7, 6, 10, 1, 9, 5, 4, 0]);
}

#[test]
fn postorder_edges(){
let mut tree: KGST<char, String> = KGST::new('$');
let item_string:Vec<char> = "abcabxabcd".chars().collect();
let item_id:String = "World".to_string();
tree.insert(item_id.clone(), item_string.clone(), &0);
let edge_post = tree.iter_edges_post();
dbg!(edge_post.collect::<Vec<(usize, usize)>>());
}

#[test]
fn export_all_nodes(){
let mut tree: KGST<char, String> = KGST::new('$');
Expand Down

0 comments on commit 5d794b4

Please sign in to comment.