diff --git a/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java b/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java index 9d94b165d81b..8cb401802c4b 100644 --- a/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.util.Objects; import org.junit.jupiter.api.Test; public class AdaptiveMergeSortTest { @@ -50,4 +51,92 @@ public void testSortSingleElement() { Integer[] result = adaptiveMergeSort.sort(input); assertArrayEquals(expected, result); } + + @Test + public void testSortAlreadySortedArray() { + AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; + Integer[] outputArray = adaptiveMergeSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortReversedSortedArray() { + AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; + Integer[] outputArray = adaptiveMergeSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortAllEqualArray() { + AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); + Integer[] inputArray = {2, 2, 2, 2, 2}; + Integer[] outputArray = adaptiveMergeSort.sort(inputArray); + Integer[] expectedOutput = {2, 2, 2, 2, 2}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortMixedCaseStrings() { + AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; + String[] outputArray = adaptiveMergeSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + /** + * Custom Comparable class for testing. + **/ + static class Person implements Comparable { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public int compareTo(Person o) { + return Integer.compare(this.age, o.age); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + @Test + public void testSortCustomObjects() { + AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort(); + Person[] inputArray = { + new Person("Alice", 32), + new Person("Bob", 25), + new Person("Charlie", 28), + }; + Person[] expectedOutput = { + new Person("Bob", 25), + new Person("Charlie", 28), + new Person("Alice", 32), + }; + Person[] outputArray = adaptiveMergeSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } } diff --git a/src/test/java/com/thealgorithms/sorts/BogoSortTest.java b/src/test/java/com/thealgorithms/sorts/BogoSortTest.java index 3ebfb7a305b0..dc4f9e1c25bb 100644 --- a/src/test/java/com/thealgorithms/sorts/BogoSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BogoSortTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.util.Objects; import org.junit.jupiter.api.Test; public class BogoSortTest { @@ -63,4 +64,87 @@ public void bogoSortDuplicateStringArray() { String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"}; assertArrayEquals(outputArray, expectedOutput); } + + @Test + public void bogoSortAlreadySortedArray() { + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; + Integer[] outputArray = bogoSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bogoSortReversedSortedArray() { + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; + Integer[] outputArray = bogoSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bogoSortAllEqualArray() { + Integer[] inputArray = {2, 2, 2, 2, 2}; + Integer[] outputArray = bogoSort.sort(inputArray); + Integer[] expectedOutput = {2, 2, 2, 2, 2}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bogoSortMixedCaseStrings() { + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; + String[] outputArray = bogoSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + /** + * Custom Comparable class for testing. + **/ + static class Person implements Comparable { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public int compareTo(Person o) { + return Integer.compare(this.age, o.age); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + @Test + public void bogoSortCustomObjects() { + Person[] inputArray = { + new Person("Alice", 32), + new Person("Bob", 25), + new Person("Charlie", 28), + }; + Person[] expectedOutput = { + new Person("Bob", 25), + new Person("Charlie", 28), + new Person("Alice", 32), + }; + Person[] outputArray = bogoSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } } diff --git a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java index 8690a3f5435c..3b99dca13b06 100644 --- a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.util.Objects; import org.junit.jupiter.api.Test; /** @@ -91,4 +92,87 @@ public void bubbleSortStringArray() { }; assertArrayEquals(outputArray, expectedOutput); } + + @Test + public void bubbleSortAlreadySortedArray() { + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortReversedSortedArray() { + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortAllEqualArray() { + Integer[] inputArray = {2, 2, 2, 2, 2}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {2, 2, 2, 2, 2}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void bubbleSortMixedCaseStrings() { + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; + String[] outputArray = bubbleSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + /** + * Custom Comparable class for testing. + **/ + static class Person implements Comparable { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public int compareTo(Person o) { + return Integer.compare(this.age, o.age); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + @Test + public void bubbleSortCustomObjects() { + Person[] inputArray = { + new Person("Alice", 32), + new Person("Bob", 25), + new Person("Charlie", 28), + }; + Person[] expectedOutput = { + new Person("Bob", 25), + new Person("Charlie", 28), + new Person("Alice", 32), + }; + Person[] outputArray = bubbleSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } } diff --git a/src/test/java/com/thealgorithms/sorts/GnomeSortTest.java b/src/test/java/com/thealgorithms/sorts/GnomeSortTest.java index d86546472580..1d875d1fad0d 100644 --- a/src/test/java/com/thealgorithms/sorts/GnomeSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/GnomeSortTest.java @@ -1,7 +1,9 @@ package com.thealgorithms.sorts; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.util.Objects; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -79,4 +81,92 @@ public void gnomeSortDuplicateStringArray() { gnomeSort.sort(inputArray); assertThat(inputArray).isEqualTo(expectedOutput); } + + @Test + @DisplayName("GnomeSort for sorted Array") + public void testSortAlreadySortedArray() { + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; + Integer[] outputArray = gnomeSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + @DisplayName("GnomeSort for reversed sorted Array") + public void testSortReversedSortedArray() { + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; + Integer[] outputArray = gnomeSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + @DisplayName("GnomeSort for All equal Array") + public void testSortAllEqualArray() { + Integer[] inputArray = {2, 2, 2, 2, 2}; + Integer[] outputArray = gnomeSort.sort(inputArray); + Integer[] expectedOutput = {2, 2, 2, 2, 2}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + @DisplayName("GnomeSort String Array with mixed cases") + public void testSortMixedCaseStrings() { + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; + String[] outputArray = gnomeSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + /** + * Custom Comparable class for testing. + **/ + static class Person implements Comparable { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public int compareTo(Person o) { + return Integer.compare(this.age, o.age); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + @Test + @DisplayName("GnomeSort Custom Object Array") + public void testSortCustomObjects() { + Person[] inputArray = { + new Person("Alice", 32), + new Person("Bob", 25), + new Person("Charlie", 28), + }; + Person[] expectedOutput = { + new Person("Bob", 25), + new Person("Charlie", 28), + new Person("Alice", 32), + }; + Person[] outputArray = gnomeSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } } diff --git a/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java index 78744973355d..32a2a807295b 100644 --- a/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Objects; import java.util.function.Function; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -111,4 +112,87 @@ private void testWithRandomArray(Function sortAlgorithm) { Double[] sorted = sortAlgorithm.apply(array); assertTrue(SortUtils.isSorted(sorted)); } + + @Test + public void testSortAlreadySortedArray() { + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; + Integer[] outputArray = insertionSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortReversedSortedArray() { + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; + Integer[] outputArray = insertionSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortAllEqualArray() { + Integer[] inputArray = {2, 2, 2, 2, 2}; + Integer[] outputArray = insertionSort.sort(inputArray); + Integer[] expectedOutput = {2, 2, 2, 2, 2}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortMixedCaseStrings() { + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; + String[] outputArray = insertionSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + /** + * Custom Comparable class for testing. + **/ + static class Person implements Comparable { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public int compareTo(Person o) { + return Integer.compare(this.age, o.age); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + @Test + public void testSortCustomObjects() { + Person[] inputArray = { + new Person("Alice", 32), + new Person("Bob", 25), + new Person("Charlie", 28), + }; + Person[] expectedOutput = { + new Person("Bob", 25), + new Person("Charlie", 28), + new Person("Alice", 32), + }; + Person[] outputArray = insertionSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } } diff --git a/src/test/java/com/thealgorithms/sorts/SlowSortTest.java b/src/test/java/com/thealgorithms/sorts/SlowSortTest.java index d4d9eaa1c275..5fbdf8477092 100644 --- a/src/test/java/com/thealgorithms/sorts/SlowSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/SlowSortTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.util.Objects; import org.junit.jupiter.api.Test; /** @@ -76,4 +77,87 @@ public void slowSortStringSymbolArray() { String[] expectedOutput = {"(a", "(b", ")", "a", "au", "auk", "auk", "bhy", "cba", "cba", "cbf", "á", "ó"}; assertArrayEquals(outputArray, expectedOutput); } + + @Test + public void testSortAlreadySortedArray() { + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; + Integer[] outputArray = slowSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortReversedSortedArray() { + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; + Integer[] outputArray = slowSort.sort(inputArray); + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortAllEqualArray() { + Integer[] inputArray = {2, 2, 2, 2, 2}; + Integer[] outputArray = slowSort.sort(inputArray); + Integer[] expectedOutput = {2, 2, 2, 2, 2}; + assertArrayEquals(outputArray, expectedOutput); + } + + @Test + public void testSortMixedCaseStrings() { + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; + String[] outputArray = slowSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + /** + * Custom Comparable class for testing. + **/ + static class Person implements Comparable { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public int compareTo(Person o) { + return Integer.compare(this.age, o.age); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + @Test + public void testSortCustomObjects() { + Person[] inputArray = { + new Person("Alice", 32), + new Person("Bob", 25), + new Person("Charlie", 28), + }; + Person[] expectedOutput = { + new Person("Bob", 25), + new Person("Charlie", 28), + new Person("Alice", 32), + }; + Person[] outputArray = slowSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } }