Skip to content

Commit

Permalink
test(merge_sort): add merge_sort test
Browse files Browse the repository at this point in the history
  • Loading branch information
donjuanplatinum committed Aug 6, 2024
1 parent f93fd5b commit b32568e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/sorting/merge_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,44 @@ pub fn merge_sort<T: Clone>(array: &mut [T], comparator: fn(&T, &T) -> bool) {
}
}
}


#[cfg(test)]
mod tests {
use super::*;
use super::super::is_sorted;
#[test]
fn test_merge_sort() {
let mut a = [2, 3, 1, 34, 15, 8, 0, 7, 4, 3, 21, 4, 6, 7, 4, 2341, 321, 41231, 312, 62];
merge_sort(&mut a, |a, b| a <= b);
assert!(is_sorted(&a, |a, b| a <= b));
}

#[test]
fn test_merge_sort_empty() {
let mut a: [i32; 0] = [];
merge_sort(&mut a, |a, b| a <= b);
assert!(is_sorted(&a, |a, b| a <= b));
}

#[test]
fn test_merge_sort_single_element() {
let mut a = [1];
merge_sort(&mut a, |a, b| a <= b);
assert!(is_sorted(&a, |a, b| a <= b));
}

#[test]
fn test_merge_sort_already_sorted() {
let mut a = [1, 2, 3, 4, 5];
merge_sort(&mut a, |a, b| a <= b);
assert!(is_sorted(&a, |a, b| a <= b));
}

#[test]
fn test_merge_sort_reverse_sorted() {
let mut a = [5, 4, 3, 2, 1];
merge_sort(&mut a, |a, b| a <= b);
assert!(is_sorted(&a, |a, b| a <= b));
}
}

0 comments on commit b32568e

Please sign in to comment.