Add tests for merge sort (#3715)

This commit is contained in:
shlokam 2022-11-01 00:56:49 +05:30 committed by GitHub
parent 3542f1c4c1
commit 9e7456a2a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.thealgorithms.strings;
/**
* Reverse String using Recursion
*/
public class ReverseStringRecursive {
/**
* @param str string to be reversed
* @return reversed string
*/
public static String reverse(String str)
{
if(str.isEmpty()){
return str;
} else {
return reverse(str.substring(1))+str.charAt(0);
}
}
}

View File

@ -0,0 +1,83 @@
package com.thealgorithms.sorts;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class MergeSortTest {
private static MergeSort mergeSort= new MergeSort();
@Test
void shouldAcceptWhenEmptyArrayIsPassed() {
Integer [] array = new Integer[]{};
Integer [] expected = new Integer[]{};
Integer []sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenSingleValuedArrayIsPassed() {
Integer [] array = new Integer[]{2};
Integer [] expected = new Integer[]{2};
Integer [] sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithAllPositiveValuesIsPassed() {
Integer [] array = new Integer[]{60, 7, 55, 9, 999, 3};
Integer [] expected = new Integer[]{3, 7, 9, 55, 60, 999};
Integer [] sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithAllNegativeValuesIsPassed() {
Integer [] array = new Integer[]{-60, -7, -55, -9, -999, -3};
Integer [] expected = new Integer[]{-999, -60, -55, -9, -7, -3};
Integer [] sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithRealNumberValuesIsPassed() {
Integer [] array = new Integer[]{60, -7, 55, 9, -999, -3};
Integer [] expected = new Integer[]{-999, -7, -3, 9, 55, 60};
Integer [] sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenArrayWithDuplicateValueIsPassed() {
Integer [] array = new Integer[]{60, 7, 55, 55, 999, 3};
Integer [] expected = new Integer[]{3, 7, 55, 55, 60, 999};
Integer [] sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
@Test
void shouldAcceptWhenStringValueArrayIsPassed() {
String[] array = {"z", "a", "x", "b", "y"};
String[] expected = {"a", "b", "x", "y", "z"};
String[] sorted = mergeSort.sort(array);
assertArrayEquals(expected, sorted);
}
}

View File

@ -0,0 +1,33 @@
package com.thealgorithms.strings;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ReverseStringRecursiveTest {
ReverseStringRecursive stringRecursive = new ReverseStringRecursive();
@Test
void shouldAcceptWhenEmptyStringIsPassed() {
String expected = "";
String reversed = stringRecursive.reverse("");
assertEquals(expected,reversed);
}
@Test
void shouldAcceptNotWhenWhenSingleCharacterIsPassed() {
String expected = "a";
String reversed = stringRecursive.reverse("a");
assertEquals(expected,reversed);
}
@Test
void shouldAcceptWhenStringIsPassed() {
String expected = "dlroWolleH";
String reversed = stringRecursive.reverse("HelloWorld");
assertEquals(expected,reversed);
}
}