Wrote tests for compressor functions (#48)

* Wrote tests for create_new_tree

* wrote tests for get_delta
This commit is contained in:
Azrenbeth
2021-08-16 15:10:36 +01:00
committed by GitHub
parent aa6137ce52
commit 9a59b1121c
3 changed files with 779 additions and 128 deletions

View File

@@ -250,132 +250,7 @@ impl<'a> Compressor<'a> {
}
#[cfg(test)]
mod level_tests {
use crate::compressor::Level;
#[test]
fn new_produces_empty_level() {
let l = Level::new(15);
assert_eq!(l.max_length, 15);
assert_eq!(l.current_chain_length, 0);
assert_eq!(l.current, None);
}
mod level_tests;
#[test]
fn update_adds_to_non_full_level() {
let mut l = Level::new(10);
l.update(7, true);
assert_eq!(l.max_length, 10);
assert_eq!(l.current_chain_length, 1);
assert_eq!(l.current, Some(7));
}
#[test]
#[should_panic(expected = "Tried to add to an already full level")]
fn update_panics_if_adding_and_too_full() {
let mut l = Level::new(5);
l.update(1, true);
l.update(2, true);
l.update(3, true);
l.update(4, true);
l.update(5, true);
l.update(6, true);
}
#[test]
fn update_resets_level_correctly() {
let mut l = Level::new(5);
l.update(1, true);
l.update(2, true);
l.update(3, true);
l.update(4, true);
l.update(5, true);
l.update(6, false);
assert_eq!(l.max_length, 5);
assert_eq!(l.current_chain_length, 1);
assert_eq!(l.current, Some(6));
}
#[test]
fn get_current_returns_current() {
let mut l = Level::new(5);
assert_eq!(l.get_current(), None);
l.update(23, true);
assert_eq!(l.get_current(), Some(23));
}
#[test]
fn has_space_returns_true_if_empty() {
let l = Level::new(15);
assert_eq!(l.has_space(), true);
}
#[test]
fn has_space_returns_true_if_part_full() {
let mut l = Level::new(15);
l.update(12, true);
l.update(234, true);
l.update(1, true);
l.update(143, true);
l.update(15, true);
assert_eq!(l.has_space(), true);
}
#[test]
fn has_space_returns_false_if_full() {
let mut l = Level::new(5);
l.update(1, true);
l.update(2, true);
l.update(3, true);
l.update(4, true);
l.update(5, true);
assert_eq!(l.has_space(), false);
}
}
#[test]
fn test_new_map() {
let mut initial: BTreeMap<i64, StateGroupEntry> = BTreeMap::new();
let mut prev = None;
for i in 0i64..=13i64 {
initial.insert(
i,
StateGroupEntry {
in_range: true,
prev_state_group: prev,
state_map: StateMap::new(),
},
);
prev = Some(i)
}
let compressor = Compressor::compress(&initial, &[3, 3]);
let new_state = compressor.new_state_group_map;
let expected_edges: BTreeMap<i64, i64> = vec![
(1, 0),
(2, 1),
(4, 3),
(5, 4),
(6, 3),
(7, 6),
(8, 7),
(9, 6),
(10, 9),
(11, 10),
(13, 12),
]
.into_iter()
.collect();
for sg in 0i64..=13i64 {
assert_eq!(
expected_edges.get(&sg).cloned(),
new_state[&sg].prev_state_group,
"state group {} did not match expected",
sg,
);
}
}
#[cfg(test)]
mod compressor_tests;