Skip to content

Commit

Permalink
test(merge_sort): add merge_sort test (#42)
Browse files Browse the repository at this point in the history
* test(merge_sort): add merge_sort test

* fmt
  • Loading branch information
donjuanplatinum authored Aug 5, 2024
1 parent f93fd5b commit 0565af5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/sorting/merge_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,45 @@ pub fn merge_sort<T: Clone>(array: &mut [T], comparator: fn(&T, &T) -> bool) {
}
}
}

#[cfg(test)]
mod tests {
use super::super::is_sorted;
use super::*;
#[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 0565af5

Please sign in to comment.