Repository: TheAlgorithms/Java
Pull request #3715 (2022-10-28 06:23:14)

Added test cases for Merge sort.
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

#3594 


Patch:
diff --git a/src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java b/src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java
new file mode 100644
index 00000000..95833220
--- /dev/null
+++ b/src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java
@@ -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);
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/MergeSortTest.java b/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
new file mode 100644
index 00000000..8958fb30
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/MergeSortTest.java
@@ -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);
+    }
+
+
+}
diff --git a/src/test/java/com/thealgorithms/strings/ReverseStringRecursiveTest.java b/src/test/java/com/thealgorithms/strings/ReverseStringRecursiveTest.java
new file mode 100644
index 00000000..ccf68e1c
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/ReverseStringRecursiveTest.java
@@ -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);
+    }
+}