|
1 | 1 | package com.thealgorithms.sorts; |
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 | 5 |
|
| 6 | +import java.util.Objects; |
5 | 7 | import org.junit.jupiter.api.DisplayName; |
6 | 8 | import org.junit.jupiter.api.Test; |
7 | 9 |
|
@@ -79,4 +81,92 @@ public void gnomeSortDuplicateStringArray() { |
79 | 81 | gnomeSort.sort(inputArray); |
80 | 82 | assertThat(inputArray).isEqualTo(expectedOutput); |
81 | 83 | } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("GnomeSort for sorted Array") |
| 87 | + public void testSortAlreadySortedArray() { |
| 88 | + Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46}; |
| 89 | + Integer[] outputArray = gnomeSort.sort(inputArray); |
| 90 | + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; |
| 91 | + assertArrayEquals(outputArray, expectedOutput); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @DisplayName("GnomeSort for reversed sorted Array") |
| 96 | + public void testSortReversedSortedArray() { |
| 97 | + Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12}; |
| 98 | + Integer[] outputArray = gnomeSort.sort(inputArray); |
| 99 | + Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46}; |
| 100 | + assertArrayEquals(outputArray, expectedOutput); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("GnomeSort for All equal Array") |
| 105 | + public void testSortAllEqualArray() { |
| 106 | + Integer[] inputArray = {2, 2, 2, 2, 2}; |
| 107 | + Integer[] outputArray = gnomeSort.sort(inputArray); |
| 108 | + Integer[] expectedOutput = {2, 2, 2, 2, 2}; |
| 109 | + assertArrayEquals(outputArray, expectedOutput); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @DisplayName("GnomeSort String Array with mixed cases") |
| 114 | + public void testSortMixedCaseStrings() { |
| 115 | + String[] inputArray = {"banana", "Apple", "apple", "Banana"}; |
| 116 | + String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; |
| 117 | + String[] outputArray = gnomeSort.sort(inputArray); |
| 118 | + assertArrayEquals(expectedOutput, outputArray); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Custom Comparable class for testing. |
| 123 | + **/ |
| 124 | + static class Person implements Comparable<Person> { |
| 125 | + String name; |
| 126 | + int age; |
| 127 | + |
| 128 | + Person(String name, int age) { |
| 129 | + this.name = name; |
| 130 | + this.age = age; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public int compareTo(Person o) { |
| 135 | + return Integer.compare(this.age, o.age); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean equals(Object o) { |
| 140 | + if (this == o) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + if (o == null || getClass() != o.getClass()) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + Person person = (Person) o; |
| 147 | + return age == person.age && Objects.equals(name, person.name); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int hashCode() { |
| 152 | + return Objects.hash(name, age); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + @DisplayName("GnomeSort Custom Object Array") |
| 158 | + public void testSortCustomObjects() { |
| 159 | + Person[] inputArray = { |
| 160 | + new Person("Alice", 32), |
| 161 | + new Person("Bob", 25), |
| 162 | + new Person("Charlie", 28), |
| 163 | + }; |
| 164 | + Person[] expectedOutput = { |
| 165 | + new Person("Bob", 25), |
| 166 | + new Person("Charlie", 28), |
| 167 | + new Person("Alice", 32), |
| 168 | + }; |
| 169 | + Person[] outputArray = gnomeSort.sort(inputArray); |
| 170 | + assertArrayEquals(expectedOutput, outputArray); |
| 171 | + } |
82 | 172 | } |
0 commit comments