Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azizkayumov committed Nov 11, 2023
1 parent 6162706 commit eb43a42
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: coverage
name: Coverage

on:
push:
Expand Down
18 changes: 18 additions & 0 deletions src/lctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,22 @@ mod tests {
assert_eq!(lctree.path(4, 3).sum, 15.);
assert_eq!(lctree.path(5, 7).sum, 9.);
}

#[test]
pub fn test_extend_forest() {
let weights = vec![1.0, 2.0, 3.0];
let mut lctree = LinkCutTree::default();
let trees_ids = lctree.extend_forest(&weights);
assert_eq!(trees_ids, vec![0, 1, 2]);
}

#[test]
#[should_panic]
pub fn delete_tree() {
let mut lctree = LinkCutTree::default();
let alice = lctree.make_tree(0.0);
let bob = lctree.make_tree(1.0);
lctree.link(alice, bob);
lctree.remove_tree(alice); // should panic
}
}
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<T: Path> Node<T> {
}

#[allow(dead_code)]
#[cfg_attr(tarpaulin, ignore)]
#[cfg(not(tarpaulin_include))]
pub fn to_str(&self) -> String {
let parent = match self.parent {
Parent::Node(idx) => format!("Node({idx})"),
Expand Down
49 changes: 49 additions & 0 deletions src/splay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,55 @@ mod tests {
forest
}

#[test]
pub fn create_node() {
let mut forest: Forest<FindMax> = super::Forest::new();
let alice = forest.create_node(0.0);
let bob = forest.create_node(1.0);
let charlie = forest.create_node(2.0);
assert_eq!([alice, bob, charlie], [0, 1, 2]);

forest.delete_node(bob);
let david = forest.create_node(4.0);
// Should reuse the space of bob's tree (which was removed)
assert_eq!(david, bob);
}

#[test]
#[should_panic]
pub fn set_right() {
let mut forest: Forest<FindMax> = super::Forest::new();
let alice = forest.create_node(0.0);
let bob = forest.create_node(1.0);
forest.set_right(alice, bob);
let charlie = forest.create_node(2.0);
// Should panic because alice already has a right child
forest.set_right(alice, charlie);
}

#[test]
#[should_panic]
pub fn set_left() {
let mut forest: Forest<FindMax> = super::Forest::new();
let alice = forest.create_node(0.0);
let bob = forest.create_node(1.0);
forest.set_left(alice, bob);
let charlie = forest.create_node(2.0);
// Should panic because alice already has a left child
forest.set_left(alice, charlie);
}

#[test]
#[should_panic]
pub fn cut_left() {
let mut forest: Forest<FindMax> = super::Forest::new();
let alice = forest.create_node(0.0);
let bob = forest.create_node(1.0);
forest.set_left(alice, bob);
// Should panic because alice does not have a left child
forest.cut_left(bob);
}

#[test]
pub fn rotate_left_root() {
// form the following tree and rotate left on '0':
Expand Down

0 comments on commit eb43a42

Please sign in to comment.