Skip to content

Commit 8512f12

Browse files
authored
Fix the formatting issue with clang-format (TheAlgorithms#6346)
1 parent 4b6006c commit 8512f12

File tree

6 files changed

+515
-0
lines changed

6 files changed

+515
-0
lines changed

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44

5+
import java.util.Objects;
56
import org.junit.jupiter.api.Test;
67

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

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44

5+
import java.util.Objects;
56
import org.junit.jupiter.api.Test;
67

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

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44

5+
import java.util.Objects;
56
import org.junit.jupiter.api.Test;
67

78
/**
@@ -91,4 +92,87 @@ public void bubbleSortStringArray() {
9192
};
9293
assertArrayEquals(outputArray, expectedOutput);
9394
}
95+
96+
@Test
97+
public void bubbleSortAlreadySortedArray() {
98+
Integer[] inputArray = {-12, -6, -3, 0, 2, 2, 13, 46};
99+
Integer[] outputArray = bubbleSort.sort(inputArray);
100+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
101+
assertArrayEquals(outputArray, expectedOutput);
102+
}
103+
104+
@Test
105+
public void bubbleSortReversedSortedArray() {
106+
Integer[] inputArray = {46, 13, 2, 2, 0, -3, -6, -12};
107+
Integer[] outputArray = bubbleSort.sort(inputArray);
108+
Integer[] expectedOutput = {-12, -6, -3, 0, 2, 2, 13, 46};
109+
assertArrayEquals(outputArray, expectedOutput);
110+
}
111+
112+
@Test
113+
public void bubbleSortAllEqualArray() {
114+
Integer[] inputArray = {2, 2, 2, 2, 2};
115+
Integer[] outputArray = bubbleSort.sort(inputArray);
116+
Integer[] expectedOutput = {2, 2, 2, 2, 2};
117+
assertArrayEquals(outputArray, expectedOutput);
118+
}
119+
120+
@Test
121+
public void bubbleSortMixedCaseStrings() {
122+
String[] inputArray = {"banana", "Apple", "apple", "Banana"};
123+
String[] expectedOutput = {"Apple", "Banana", "apple", "banana"};
124+
String[] outputArray = bubbleSort.sort(inputArray);
125+
assertArrayEquals(expectedOutput, outputArray);
126+
}
127+
128+
/**
129+
* Custom Comparable class for testing.
130+
**/
131+
static class Person implements Comparable<Person> {
132+
String name;
133+
int age;
134+
135+
Person(String name, int age) {
136+
this.name = name;
137+
this.age = age;
138+
}
139+
140+
@Override
141+
public int compareTo(Person o) {
142+
return Integer.compare(this.age, o.age);
143+
}
144+
145+
@Override
146+
public boolean equals(Object o) {
147+
if (this == o) {
148+
return true;
149+
}
150+
if (o == null || getClass() != o.getClass()) {
151+
return false;
152+
}
153+
Person person = (Person) o;
154+
return age == person.age && Objects.equals(name, person.name);
155+
}
156+
157+
@Override
158+
public int hashCode() {
159+
return Objects.hash(name, age);
160+
}
161+
}
162+
163+
@Test
164+
public void bubbleSortCustomObjects() {
165+
Person[] inputArray = {
166+
new Person("Alice", 32),
167+
new Person("Bob", 25),
168+
new Person("Charlie", 28),
169+
};
170+
Person[] expectedOutput = {
171+
new Person("Bob", 25),
172+
new Person("Charlie", 28),
173+
new Person("Alice", 32),
174+
};
175+
Person[] outputArray = bubbleSort.sort(inputArray);
176+
assertArrayEquals(expectedOutput, outputArray);
177+
}
94178
}

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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

6+
import java.util.Objects;
57
import org.junit.jupiter.api.DisplayName;
68
import org.junit.jupiter.api.Test;
79

@@ -79,4 +81,92 @@ public void gnomeSortDuplicateStringArray() {
7981
gnomeSort.sort(inputArray);
8082
assertThat(inputArray).isEqualTo(expectedOutput);
8183
}
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+
}
82172
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy