Skip to content

Commit aa6137c

Browse files
authored
Wrote tests for Level struct functions (#47)
1 parent e5174f4 commit aa6137c

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

src/compressor.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use string_cache::DefaultAtom as Atom;
3636
use super::{collapse_state_maps, StateGroupEntry};
3737

3838
/// Holds information about a particular level.
39+
#[derive(Debug)]
3940
struct Level {
4041
/// The maximum size this level is allowed to be
4142
max_length: usize,
@@ -67,7 +68,7 @@ impl Level {
6768
// If we're referencing the previous head then increment our chain
6869
// length estimate
6970
if !self.has_space() {
70-
panic!("Tried to add to a already full level");
71+
panic!("Tried to add to an already full level");
7172
}
7273

7374
self.current_chain_length += 1;
@@ -248,6 +249,89 @@ impl<'a> Compressor<'a> {
248249
}
249250
}
250251

252+
#[cfg(test)]
253+
mod level_tests {
254+
use crate::compressor::Level;
255+
#[test]
256+
fn new_produces_empty_level() {
257+
let l = Level::new(15);
258+
assert_eq!(l.max_length, 15);
259+
assert_eq!(l.current_chain_length, 0);
260+
assert_eq!(l.current, None);
261+
}
262+
263+
#[test]
264+
fn update_adds_to_non_full_level() {
265+
let mut l = Level::new(10);
266+
l.update(7, true);
267+
assert_eq!(l.max_length, 10);
268+
assert_eq!(l.current_chain_length, 1);
269+
assert_eq!(l.current, Some(7));
270+
}
271+
272+
#[test]
273+
#[should_panic(expected = "Tried to add to an already full level")]
274+
fn update_panics_if_adding_and_too_full() {
275+
let mut l = Level::new(5);
276+
l.update(1, true);
277+
l.update(2, true);
278+
l.update(3, true);
279+
l.update(4, true);
280+
l.update(5, true);
281+
l.update(6, true);
282+
}
283+
284+
#[test]
285+
fn update_resets_level_correctly() {
286+
let mut l = Level::new(5);
287+
l.update(1, true);
288+
l.update(2, true);
289+
l.update(3, true);
290+
l.update(4, true);
291+
l.update(5, true);
292+
l.update(6, false);
293+
assert_eq!(l.max_length, 5);
294+
assert_eq!(l.current_chain_length, 1);
295+
assert_eq!(l.current, Some(6));
296+
}
297+
298+
#[test]
299+
fn get_current_returns_current() {
300+
let mut l = Level::new(5);
301+
assert_eq!(l.get_current(), None);
302+
l.update(23, true);
303+
assert_eq!(l.get_current(), Some(23));
304+
}
305+
306+
#[test]
307+
fn has_space_returns_true_if_empty() {
308+
let l = Level::new(15);
309+
assert_eq!(l.has_space(), true);
310+
}
311+
312+
#[test]
313+
fn has_space_returns_true_if_part_full() {
314+
let mut l = Level::new(15);
315+
l.update(12, true);
316+
l.update(234, true);
317+
l.update(1, true);
318+
l.update(143, true);
319+
l.update(15, true);
320+
assert_eq!(l.has_space(), true);
321+
}
322+
323+
#[test]
324+
fn has_space_returns_false_if_full() {
325+
let mut l = Level::new(5);
326+
l.update(1, true);
327+
l.update(2, true);
328+
l.update(3, true);
329+
l.update(4, true);
330+
l.update(5, true);
331+
assert_eq!(l.has_space(), false);
332+
}
333+
}
334+
251335
#[test]
252336
fn test_new_map() {
253337
let mut initial: BTreeMap<i64, StateGroupEntry> = BTreeMap::new();

0 commit comments

Comments
 (0)