Skip to content

Commit

Permalink
Add unit tests for MergeIter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Emann committed Oct 13, 2023
1 parent dcb2104 commit c6ec23a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn write_byte_string(f: &mut Formatter, byte_string: &[u8]) -> fmt::Result {
Ok(())
}

/// An iterator that merges two sorted iterators into one sorted iterator
pub(crate) struct MergeIter<It1, It2>
where
It1: Iterator,
Expand Down Expand Up @@ -284,4 +285,36 @@ mod tests {
]);
assert_eq!(format!("{:?}", bytes), r##""Class IRI=\"#B\"""##);
}

#[test]
fn merge_empty() {
let iter = MergeIter::new(vec![].into_iter(), vec![].into_iter());
assert_eq!(iter.collect::<Vec<usize>>(), vec![]);
}

#[test]
fn merge_single_empty() {
let iter = MergeIter::new(vec![1].into_iter(), vec![].into_iter());
assert_eq!(iter.collect::<Vec<usize>>(), vec![1]);
let iter = MergeIter::new(vec![].into_iter(), vec![1].into_iter());
assert_eq!(iter.collect::<Vec<usize>>(), vec![1]);
}

#[test]
fn merge_whole_side_before() {
let iter = MergeIter::new(vec![1, 2, 3].into_iter(), vec![4, 5, 6].into_iter());
assert_eq!(iter.collect::<Vec<usize>>(), vec![1, 2, 3, 4, 5, 6]);
}

#[test]
fn merge_interleave() {
let iter = MergeIter::new(
vec![1, 2, 8, 20].into_iter(),
vec![3, 4, 22, 23].into_iter(),
);
assert_eq!(
iter.collect::<Vec<usize>>(),
vec![1, 2, 3, 4, 8, 20, 22, 23]
);
}
}

0 comments on commit c6ec23a

Please sign in to comment.