Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added test cases for sorting algorithms#6345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
pushkar0406 wants to merge3 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
frompushkar0406:testcase
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 {
Expand DownExpand Up@@ -50,4 +51,89 @@ 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<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 (!(o instanceof Person)) {
return false;
}
Person p = (Person) o;
return this.name.equals(p.name) && this.age == p.age;
}

@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);
}
}
81 changes: 81 additions & 0 deletionssrc/test/java/com/thealgorithms/sorts/BogoSortTest.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 {
Expand DownExpand Up@@ -63,4 +64,84 @@ 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<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 (!(o instanceof Person)) {
return false;
}
Person p = (Person) o;
return this.name.equals(p.name) && this.age == p.age;
}

@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);
}
}
81 changes: 81 additions & 0 deletionssrc/test/java/com/thealgorithms/sorts/BubbleSortTest.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@

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

import java.util.Objects;
import org.junit.jupiter.api.Test;

/**
Expand DownExpand Up@@ -91,4 +92,84 @@ 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<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 (!(o instanceof Person)) {
return false;
}
Person p = (Person) o;
return this.name.equals(p.name) && this.age == p.age;
}

@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);
}
}
87 changes: 87 additions & 0 deletionssrc/test/java/com/thealgorithms/sorts/GnomeSortTest.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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;

Expand DownExpand Up@@ -79,4 +81,89 @@ 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<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 (!(o instanceof Person)) {
return false;
}
Person p = (Person) o;
return this.name.equals(p.name) && this.age == p.age;
}

@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);
}
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp