Add tests for recursive merge sort (#3510)

This commit is contained in:
Thanh-Thai Tran 2022-11-12 23:10:13 +07:00 committed by GitHub
parent b294ddcb38
commit f7dee0d958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 2 deletions

View File

@ -12,9 +12,9 @@ public class MergeSortRecursive {
this.arr = arr;
}
public void mergeSort() {
public List<Integer> mergeSort() {
List<Integer> arrSorted = merge(arr);
System.out.println(arrSorted);
return arrSorted;
}
private static List<Integer> merge(List<Integer> arr) {

View File

@ -0,0 +1,35 @@
package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class HeapSortTest {
private HeapSort heapSort = new HeapSort();
@Test
void testHeapSortCase1() {
Integer[] array = { 49, 4, 36, 9, 144, 1 };
Integer[] sorted = heapSort.sort(array);
Integer[] expected = { 1, 4, 9, 36, 49, 144 };
assertArrayEquals(expected, sorted);
}
@Test
void testHeapSortCase2() {
Integer[] array = { };
Integer[] sorted = heapSort.sort(array);
Integer[] expected = { };
assertArrayEquals(expected, sorted);
}
@Test
void testHeapSortCase3 () {
Integer[] array = { -3, 5, 3, 4, 3, 7, 40, -20, 30, 0 };
Integer[] sorted = heapSort.sort(array);
Integer[] expected = { -20, -3, 0, 3, 3, 4, 5, 7, 30, 40 };
assertArrayEquals(expected, sorted);
}
}

View File

@ -0,0 +1,35 @@
package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class MergeSortRecursiveTest {
// private MergeSortRecursive mergeSortRecursive = new MergeSortRecursive();
@Test
void testMergeSortRecursiveCase1 () {
MergeSortRecursive mergeSortRecursive = new MergeSortRecursive(Arrays.asList(5, 12, 9, 3, 15, 88));
List<Integer> expected = Arrays.asList(3, 5, 9, 12, 15, 88);
List<Integer> sorted = mergeSortRecursive.mergeSort();
assertEquals(expected, sorted);
}
@Test
void testMergeSortRecursiveCase2 () {
MergeSortRecursive mergeSortRecursive = new MergeSortRecursive(Arrays.asList(-3, 5, 3, 4, 3, 7, 40, -20, 30, 0));
List<Integer> expected = Arrays.asList(-20, -3, 0, 3, 3, 4, 5, 7, 30, 40);
List<Integer> sorted = mergeSortRecursive.mergeSort();
assertEquals(expected, sorted);
}
}