mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Fix the formatting issue with clang-format (#6346)
This commit is contained in:
@@ -2,6 +2,7 @@ package com.thealgorithms.sorts;
|
||||
|
||||
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 class AdaptiveMergeSortTest {
|
||||
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<Person> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.thealgorithms.sorts;
|
||||
|
||||
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 class BogoSortTest {
|
||||
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<Person> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@@ -91,4 +92,87 @@ public class BubbleSortTest {
|
||||
};
|
||||
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<Person> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 class GnomeSortTest {
|
||||
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<Person> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.thealgorithms.sorts;
|
||||
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 @@ class InsertionSortTest {
|
||||
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<Person> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.thealgorithms.sorts;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@@ -76,4 +77,87 @@ public class SlowSortTest {
|
||||
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<Person> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user