Skip to content

Commit 77bd121

Browse files
author
pushkar
committed
Added test cases for sorting algorithms
1 parent 9b4dec0 commit 77bd121

6 files changed

Lines changed: 492 additions & 0 deletions

File tree

src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import org.junit.jupiter.api.Test;
66

7+
import java.util.Objects;
8+
79
public class AdaptiveMergeSortTest {
810

911
@Test
@@ -50,4 +52,87 @@ public void testSortSingleElement() {
5052
Integer[] result = adaptiveMergeSort.sort(input);
5153
assertArrayEquals(expected, result);
5254
}
55+
56+
@Test
57+
public void testSortAlreadySortedArray() {
58+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
59+
Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46};
60+
Integer[] outputArray = adaptiveMergeSort.sort(inputArray);
61+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
62+
assertArrayEquals(outputArray, expectedOutput);
63+
}
64+
65+
@Test
66+
public void testSortReversedSortedArray() {
67+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
68+
Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12};
69+
Integer[] outputArray = adaptiveMergeSort.sort(inputArray);
70+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
71+
assertArrayEquals(outputArray, expectedOutput);
72+
}
73+
74+
@Test
75+
public void testSortAllEqualArray() {
76+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
77+
Integer[] inputArray = {2, 2, 2, 2, 2};
78+
Integer[] outputArray = adaptiveMergeSort.sort(inputArray);
79+
Integer[] expectedOutput = {2, 2, 2, 2, 2};
80+
assertArrayEquals(outputArray, expectedOutput);
81+
}
82+
83+
@Test
84+
public void testSortMixedCaseStrings() {
85+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
86+
String[] inputArray = {"banana", "Apple", "apple", "Banana"};
87+
String[] expectedOutput = {"Apple", "Banana", "apple", "banana"};
88+
String[] outputArray = adaptiveMergeSort.sort(inputArray);
89+
assertArrayEquals(expectedOutput, outputArray);
90+
}
91+
92+
/**
93+
* Custom Comparable class for testing.
94+
**/
95+
static class Person implements Comparable<Person> {
96+
String name;
97+
int age;
98+
99+
Person(String name, int age) {
100+
this.name = name;
101+
this.age = age;
102+
}
103+
104+
@Override
105+
public int compareTo(Person o) {
106+
return Integer.compare(this.age, o.age);
107+
}
108+
109+
@Override
110+
public boolean equals(Object o) {
111+
if (!(o instanceof Person)) return false;
112+
Person p = (Person) o;
113+
return this.name.equals(p.name) && this.age == p.age;
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
return Objects.hash(name, age);
119+
}
120+
}
121+
122+
@Test
123+
public void testSortCustomObjects() {
124+
AdaptiveMergeSort adaptiveMergeSort = new AdaptiveMergeSort();
125+
Person[] inputArray = {
126+
new Person("Alice", 32),
127+
new Person("Bob", 25),
128+
new Person("Charlie", 28),
129+
};
130+
Person[] expectedOutput = {
131+
new Person("Bob", 25),
132+
new Person("Charlie", 28),
133+
new Person("Alice", 32),
134+
};
135+
Person[] outputArray = adaptiveMergeSort.sort(inputArray);
136+
assertArrayEquals(expectedOutput, outputArray);
137+
}
53138
}

src/test/java/com/thealgorithms/sorts/BogoSortTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import org.junit.jupiter.api.Test;
66

7+
import java.util.Objects;
8+
79
public class BogoSortTest {
810

911
private BogoSort bogoSort = new BogoSort();
@@ -63,4 +65,82 @@ public void bogoSortDuplicateStringArray() {
6365
String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"};
6466
assertArrayEquals(outputArray, expectedOutput);
6567
}
68+
69+
@Test
70+
public void bogoSortAlreadySortedArray() {
71+
Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46};
72+
Integer[] outputArray = bogoSort.sort(inputArray);
73+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
74+
assertArrayEquals(outputArray, expectedOutput);
75+
}
76+
77+
@Test
78+
public void bogoSortReversedSortedArray() {
79+
Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12};
80+
Integer[] outputArray = bogoSort.sort(inputArray);
81+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
82+
assertArrayEquals(outputArray, expectedOutput);
83+
}
84+
85+
@Test
86+
public void bogoSortAllEqualArray() {
87+
Integer[] inputArray = {2, 2, 2, 2, 2};
88+
Integer[] outputArray = bogoSort.sort(inputArray);
89+
Integer[] expectedOutput = {2, 2, 2, 2, 2};
90+
assertArrayEquals(outputArray, expectedOutput);
91+
}
92+
93+
@Test
94+
public void bogoSortMixedCaseStrings() {
95+
String[] inputArray = {"banana", "Apple", "apple", "Banana"};
96+
String[] expectedOutput = {"Apple", "Banana", "apple", "banana"};
97+
String[] outputArray = bogoSort.sort(inputArray);
98+
assertArrayEquals(expectedOutput, outputArray);
99+
}
100+
101+
/**
102+
* Custom Comparable class for testing.
103+
**/
104+
static class Person implements Comparable<Person> {
105+
String name;
106+
int age;
107+
108+
Person(String name, int age) {
109+
this.name = name;
110+
this.age = age;
111+
}
112+
113+
@Override
114+
public int compareTo(Person o) {
115+
return Integer.compare(this.age, o.age);
116+
}
117+
118+
@Override
119+
public boolean equals(Object o) {
120+
if (!(o instanceof Person)) return false;
121+
Person p = (Person) o;
122+
return this.name.equals(p.name) && this.age == p.age;
123+
}
124+
125+
@Override
126+
public int hashCode() {
127+
return Objects.hash(name, age);
128+
}
129+
}
130+
131+
@Test
132+
public void bogoSortCustomObjects() {
133+
Person[] inputArray = {
134+
new Person("Alice", 32),
135+
new Person("Bob", 25),
136+
new Person("Charlie", 28),
137+
};
138+
Person[] expectedOutput = {
139+
new Person("Bob", 25),
140+
new Person("Charlie", 28),
141+
new Person("Alice", 32),
142+
};
143+
Person[] outputArray = bogoSort.sort(inputArray);
144+
assertArrayEquals(expectedOutput, outputArray);
145+
}
66146
}

src/test/java/com/thealgorithms/sorts/BubbleSortTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import org.junit.jupiter.api.Test;
66

7+
import java.util.Objects;
8+
79
/**
810
* @author Aitor Fidalgo (https://github.com/aitorfi)
911
* @see BubbleSort
@@ -91,4 +93,83 @@ public void bubbleSortStringArray() {
9193
};
9294
assertArrayEquals(outputArray, expectedOutput);
9395
}
96+
97+
@Test
98+
public void bubbleSortAlreadySortedArray() {
99+
Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46};
100+
Integer[] outputArray = bubbleSort.sort(inputArray);
101+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
102+
assertArrayEquals(outputArray, expectedOutput);
103+
}
104+
105+
@Test
106+
public void bubbleSortReversedSortedArray() {
107+
Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12};
108+
Integer[] outputArray = bubbleSort.sort(inputArray);
109+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
110+
assertArrayEquals(outputArray, expectedOutput);
111+
}
112+
113+
@Test
114+
public void bubbleSortAllEqualArray() {
115+
Integer[] inputArray = {2, 2, 2, 2, 2};
116+
Integer[] outputArray = bubbleSort.sort(inputArray);
117+
Integer[] expectedOutput = {2, 2, 2, 2, 2};
118+
assertArrayEquals(outputArray, expectedOutput);
119+
}
120+
121+
@Test
122+
public void bubbleSortMixedCaseStrings() {
123+
String[] inputArray = {"banana", "Apple", "apple", "Banana"};
124+
String[] expectedOutput = {"Apple", "Banana", "apple", "banana"};
125+
String[] outputArray = bubbleSort.sort(inputArray);
126+
assertArrayEquals(expectedOutput, outputArray);
127+
}
128+
129+
/**
130+
* Custom Comparable class for testing.
131+
**/
132+
static class Person implements Comparable<Person> {
133+
String name;
134+
int age;
135+
136+
Person(String name, int age) {
137+
this.name = name;
138+
this.age = age;
139+
}
140+
141+
@Override
142+
public int compareTo(Person o) {
143+
return Integer.compare(this.age, o.age);
144+
}
145+
146+
@Override
147+
public boolean equals(Object o) {
148+
if (!(o instanceof Person)) return false;
149+
Person p = (Person) o;
150+
return this.name.equals(p.name) && this.age == p.age;
151+
}
152+
153+
@Override
154+
public int hashCode() {
155+
return Objects.hash(name, age);
156+
}
157+
}
158+
159+
@Test
160+
public void bubbleSortCustomObjects() {
161+
Person[] inputArray = {
162+
new Person("Alice", 32),
163+
new Person("Bob", 25),
164+
new Person("Charlie", 28),
165+
};
166+
Person[] expectedOutput = {
167+
new Person("Bob", 25),
168+
new Person("Charlie", 28),
169+
new Person("Alice", 32),
170+
};
171+
Person[] outputArray = bubbleSort.sort(inputArray);
172+
assertArrayEquals(expectedOutput, outputArray);
173+
}
174+
94175
}

src/test/java/com/thealgorithms/sorts/GnomeSortTest.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.thealgorithms.sorts;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
45

56
import org.junit.jupiter.api.DisplayName;
67
import org.junit.jupiter.api.Test;
78

9+
import java.util.Objects;
10+
811
public class GnomeSortTest {
912

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

0 commit comments

Comments
 (0)