From f6d679519e9352115bd08b6fdbac91a51f0a5afd Mon Sep 17 00:00:00 2001 From: P-Ved <139991624+P-Ved@users.noreply.github.com> Date: Wed, 7 Jan 2026 12:05:43 +0530 Subject: [PATCH 1/2] test: add unit test for BubbleSort with negative numbers - Added testBubbleSortNegativeNumbers to verify sorting works correctly with negative values - Ensures the sort handles negative numbers properly --- src/test/java/com/thealgorithms/sorts/BubbleSortTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java index 3b99dca13b06..ce1eac39f772 100644 --- a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java @@ -160,6 +160,14 @@ public int hashCode() { } } + @Test + public void bubbleSortNegativeNumbers() { + Integer[] inputArray = {5, -1, 7, 0}; + Integer[] outputArray = bubbleSort.sort(inputArray); + Integer[] expectedOutput = {-1, 0, 5, 7}; + assertArrayEquals(expectedOutput, outputArray); + } + @Test public void bubbleSortCustomObjects() { Person[] inputArray = { From 203b620d4a9a471a6059875cf29743f07a9b8278 Mon Sep 17 00:00:00 2001 From: P-Ved <139991624+P-Ved@users.noreply.github.com> Date: Wed, 7 Jan 2026 12:23:55 +0530 Subject: [PATCH 2/2] test: improve BubbleSort tests and add edge case coverage test: improve BinarySearch tests and add null input handling test: enhance Factorial tests and add large input coverage test: correct GCD tests for negative inputs and improve coverage test: improve FibonacciNumberCheck tests and add edge cases --- .../thealgorithms/maths/FactorialTest.java | 31 ++- .../maths/FibonacciNumberCheckTest.java | 52 +++-- .../java/com/thealgorithms/maths/GCDTest.java | 90 +++++---- .../searches/BinarySearchTest.java | 93 ++++----- .../thealgorithms/sorts/BubbleSortTest.java | 178 ++++++++---------- 5 files changed, 227 insertions(+), 217 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/FactorialTest.java b/src/test/java/com/thealgorithms/maths/FactorialTest.java index b38dc45589ee..e2aa742ab2b1 100644 --- a/src/test/java/com/thealgorithms/maths/FactorialTest.java +++ b/src/test/java/com/thealgorithms/maths/FactorialTest.java @@ -3,22 +3,41 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.math.BigInteger; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -public class FactorialTest { +/** + * Unit tests for Factorial. + */ +class FactorialTest { + private static final String EXCEPTION_MESSAGE = "Input number cannot be negative"; @Test - public void testWhenInvalidInoutProvidedShouldThrowException() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); - assertEquals(exception.getMessage(), EXCEPTION_MESSAGE); + @DisplayName("Factorial should throw exception for negative input") + void testWhenInvalidInputProvidedShouldThrowException() { + IllegalArgumentException exception = + assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1)); + + assertEquals(EXCEPTION_MESSAGE, exception.getMessage()); } @Test - public void testCorrectFactorialCalculation() { + @DisplayName("Factorial should return correct values for small numbers") + void testCorrectFactorialCalculation() { assertEquals(1, Factorial.factorial(0)); assertEquals(1, Factorial.factorial(1)); assertEquals(120, Factorial.factorial(5)); - assertEquals(3628800, Factorial.factorial(10)); + assertEquals(3_628_800, Factorial.factorial(10)); + } + + @Test + @DisplayName("Factorial should correctly calculate large values") + void testLargeFactorial() { + assertEquals( + new BigInteger("2432902008176640000"), + Factorial.factorial(20) + ); } } diff --git a/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java b/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java index 6ba81639a11a..b4f5ca374c2e 100644 --- a/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java +++ b/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java @@ -1,30 +1,48 @@ package com.thealgorithms.maths; -import org.junit.jupiter.api.Assertions; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; /** - * Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144... + * Unit tests for FibonacciNumberCheck. + * + * Fibonacci sequence: + * 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144... * - * @author Albina Gimaletdinova on 01/07/2023 + * @author Albina */ -public class FibonacciNumberCheckTest { +class FibonacciNumberCheckTest { + + @Test + @DisplayName("Should return true for Fibonacci numbers") + void testNumberIsFibonacciNumber() { + assertTrue(FibonacciNumberCheck.isFibonacciNumber(0)); + assertTrue(FibonacciNumberCheck.isFibonacciNumber(1)); + assertTrue(FibonacciNumberCheck.isFibonacciNumber(2)); + assertTrue(FibonacciNumberCheck.isFibonacciNumber(21)); + assertTrue(FibonacciNumberCheck.isFibonacciNumber(6765)); // 20th number + assertTrue(FibonacciNumberCheck.isFibonacciNumber(832040)); // 30th number + assertTrue(FibonacciNumberCheck.isFibonacciNumber(102334155)); // 40th number + assertTrue(FibonacciNumberCheck.isFibonacciNumber(701408733)); // 45th number + } + @Test - public void testNumberIsFibonacciNumber() { - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(1)); - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(2)); - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(21)); - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(6765)); // 20th number - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(832040)); // 30th number - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(102334155)); // 40th number - Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(701408733)); // 45th number + @DisplayName("Should return false for non-Fibonacci numbers") + void testNumberIsNotFibonacciNumber() { + assertFalse(FibonacciNumberCheck.isFibonacciNumber(9)); + assertFalse(FibonacciNumberCheck.isFibonacciNumber(10)); + assertFalse(FibonacciNumberCheck.isFibonacciNumber(145)); + assertFalse(FibonacciNumberCheck.isFibonacciNumber(701408734)); } @Test - public void testNumberIsNotFibonacciNumber() { - Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(9)); - Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(10)); - Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(145)); - Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(701408734)); + @DisplayName("Should return false for negative numbers") + void testNegativeNumbersAreNotFibonacci() { + assertFalse(FibonacciNumberCheck.isFibonacciNumber(-1)); + assertFalse(FibonacciNumberCheck.isFibonacciNumber(-5)); + assertFalse(FibonacciNumberCheck.isFibonacciNumber(Integer.MIN_VALUE)); } } diff --git a/src/test/java/com/thealgorithms/maths/GCDTest.java b/src/test/java/com/thealgorithms/maths/GCDTest.java index 6bc870e94df9..3491423f1572 100644 --- a/src/test/java/com/thealgorithms/maths/GCDTest.java +++ b/src/test/java/com/thealgorithms/maths/GCDTest.java @@ -1,82 +1,98 @@ package com.thealgorithms.maths; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -public class GCDTest { +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; - @Test - void testNegativeAndZeroThrowsException() { - Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-1, 0)); - } +/** + * Unit tests for GCD implementation. + */ +class GCDTest { @Test - void testPositiveAndNegativeThrowsException() { - Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(10, -2)); - } - - @Test - void testBothNegativeThrowsException() { - Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-5, -3)); + @DisplayName("GCD should handle zero and positive number") + void testZeroAndPositiveReturnsPositive() { + assertEquals(2, GCD.gcd(0, 2)); + assertEquals(10, GCD.gcd(10, 0)); + assertEquals(1, GCD.gcd(1, 0)); } @Test - void testZeroAndPositiveReturnsPositive() { - Assertions.assertEquals(2, GCD.gcd(0, 2)); + @DisplayName("GCD should work with negative inputs using absolute values") + void testNegativeInputsHandledCorrectly() { + assertEquals(1, GCD.gcd(-1, 0)); + assertEquals(2, GCD.gcd(10, -2)); + assertEquals(1, GCD.gcd(-5, -3)); + assertEquals(3, GCD.gcd(-9, 6)); } @Test - void testPositiveAndZeroReturnsPositive() { - Assertions.assertEquals(10, GCD.gcd(10, 0)); + @DisplayName("GCD should return correct result for two positive numbers") + void testTwoPositiveNumbers() { + assertEquals(3, GCD.gcd(9, 6)); } @Test - void testOneAndZeroReturnsOne() { - Assertions.assertEquals(1, GCD.gcd(1, 0)); + @DisplayName("GCD should return same number when both inputs are equal") + void testSameNumbers() { + assertEquals(7, GCD.gcd(7, 7)); } @Test - void testTwoPositiveNumbers() { - Assertions.assertEquals(3, GCD.gcd(9, 6)); + @DisplayName("GCD of two prime numbers should be one") + void testPrimeNumbersHaveGcdOne() { + assertEquals(1, GCD.gcd(13, 17)); } @Test + @DisplayName("GCD should handle multiple arguments") void testMultipleArgumentsGcd() { - Assertions.assertEquals(6, GCD.gcd(48, 18, 30, 12)); + assertEquals(6, GCD.gcd(48, 18, 30, 12)); } @Test + @DisplayName("GCD should handle array input") void testArrayInputGcd() { - Assertions.assertEquals(3, GCD.gcd(new int[] {9, 6})); + assertEquals(3, GCD.gcd(new int[] {9, 6})); } @Test + @DisplayName("GCD should handle array with common factor") void testArrayWithCommonFactor() { - Assertions.assertEquals(5, GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13})); + assertEquals( + 5, + GCD.gcd(new int[] { + 2 * 3 * 5 * 7, + 2 * 5 * 5 * 5, + 2 * 5 * 11, + 5 * 5 * 5 * 13 + }) + ); } @Test + @DisplayName("GCD of empty array should return zero") void testEmptyArrayReturnsZero() { - Assertions.assertEquals(0, GCD.gcd(new int[] {})); - } - - @Test - void testSameNumbers() { - Assertions.assertEquals(7, GCD.gcd(7, 7)); + assertEquals(0, GCD.gcd(new int[] {})); } @Test - void testPrimeNumbersHaveGcdOne() { - Assertions.assertEquals(1, GCD.gcd(13, 17)); + @DisplayName("GCD of single-element array should return that element") + void testSingleElementArrayReturnsElement() { + assertEquals(42, GCD.gcd(new int[] {42})); } @Test - void testSingleElementArrayReturnsElement() { - Assertions.assertEquals(42, GCD.gcd(new int[] {42})); + @DisplayName("GCD should handle large numbers") + void testLargeNumbers() { + assertEquals(12, GCD.gcd(123456, 789012)); } @Test - void testLargeNumbers() { - Assertions.assertEquals(12, GCD.gcd(123456, 789012)); + @DisplayName("GCD should throw exception for null array") + void testNullArrayThrowsException() { + assertThrows(IllegalArgumentException.class, () -> GCD.gcd((int[]) null)); } } diff --git a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java index bd4620a7fa7d..84a3dbbdbdff 100644 --- a/src/test/java/com/thealgorithms/searches/BinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/BinarySearchTest.java @@ -1,8 +1,9 @@ package com.thealgorithms.searches; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; import java.util.stream.IntStream; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; /** @@ -10,99 +11,75 @@ */ class BinarySearchTest { - /** - * Test for basic binary search functionality. - */ + private final BinarySearch binarySearch = new BinarySearch(); + @Test + @DisplayName("BinarySearch should find existing element") void testBinarySearchFound() { - BinarySearch binarySearch = new BinarySearch(); Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int key = 7; - int expectedIndex = 6; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6."); + assertEquals(6, binarySearch.find(array, key)); } - /** - * Test for binary search when the element is not present. - */ @Test + @DisplayName("BinarySearch should return -1 when element is not found") void testBinarySearchNotFound() { - BinarySearch binarySearch = new BinarySearch(); Integer[] array = {1, 2, 3, 4, 5}; - int key = 6; // Element not present in the array - int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array."); + int key = 6; + assertEquals(-1, binarySearch.find(array, key)); } - /** - * Test for binary search with first element as the key. - */ @Test + @DisplayName("BinarySearch should find first element") void testBinarySearchFirstElement() { - BinarySearch binarySearch = new BinarySearch(); Integer[] array = {1, 2, 3, 4, 5}; - int key = 1; // First element - int expectedIndex = 0; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0."); + assertEquals(0, binarySearch.find(array, 1)); } - /** - * Test for binary search with last element as the key. - */ @Test + @DisplayName("BinarySearch should find last element") void testBinarySearchLastElement() { - BinarySearch binarySearch = new BinarySearch(); Integer[] array = {1, 2, 3, 4, 5}; - int key = 5; // Last element - int expectedIndex = 4; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4."); + assertEquals(4, binarySearch.find(array, 5)); } - /** - * Test for binary search with a single element present. - */ @Test + @DisplayName("BinarySearch should handle single-element array (found)") void testBinarySearchSingleElementFound() { - BinarySearch binarySearch = new BinarySearch(); Integer[] array = {1}; - int key = 1; // Only element present - int expectedIndex = 0; // Index of the key in the array - assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0."); + assertEquals(0, binarySearch.find(array, 1)); } - /** - * Test for binary search with a single element not present. - */ @Test + @DisplayName("BinarySearch should handle single-element array (not found)") void testBinarySearchSingleElementNotFound() { - BinarySearch binarySearch = new BinarySearch(); Integer[] array = {1}; - int key = 2; // Key not present - int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array."); + assertEquals(-1, binarySearch.find(array, 2)); } - /** - * Test for binary search with an empty array. - */ @Test + @DisplayName("BinarySearch should return -1 for empty array") void testBinarySearchEmptyArray() { - BinarySearch binarySearch = new BinarySearch(); - Integer[] array = {}; // Empty array - int key = 1; // Key not present - int expectedIndex = -1; // Key not found - assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array."); + Integer[] array = {}; + assertEquals(-1, binarySearch.find(array, 1)); } - /** - * Test for binary search on large array. - */ @Test + @DisplayName("BinarySearch should handle large sorted array") void testBinarySearchLargeArray() { - BinarySearch binarySearch = new BinarySearch(); - Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999 - int key = 9999; // Last element - int expectedIndex = 9999; // Index of the last element - assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999."); + Integer[] array = IntStream.range(0, 10_000) + .boxed() + .toArray(Integer[]::new); + + assertEquals(9999, binarySearch.find(array, 9999)); + } + + @Test + @DisplayName("BinarySearch should throw exception for null array") + void testBinarySearchNullArray() { + assertThrows( + IllegalArgumentException.class, + () -> binarySearch.find(null, 5) + ); } } diff --git a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java index ce1eac39f772..4ef0a58f87b7 100644 --- a/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BubbleSortTest.java @@ -1,133 +1,141 @@ package com.thealgorithms.sorts; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.*; import java.util.Objects; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; /** - * @author Aitor Fidalgo (https://github.com/aitorfi) + * Tests for BubbleSort implementation. + * + * @author Aitor Fidalgo * @see BubbleSort */ -public class BubbleSortTest { +class BubbleSortTest { - private BubbleSort bubbleSort = new BubbleSort(); + private final BubbleSort bubbleSort = new BubbleSort(); @Test - public void bubbleSortEmptyArray() { + @DisplayName("BubbleSort should handle empty array") + void bubbleSortEmptyArray() { Integer[] inputArray = {}; Integer[] outputArray = bubbleSort.sort(inputArray); - Integer[] expectedOutput = {}; - assertArrayEquals(outputArray, expectedOutput); + assertArrayEquals(new Integer[] {}, outputArray); } @Test - public void bubbleSortSingleIntegerElementArray() { + @DisplayName("BubbleSort should handle single integer element") + void bubbleSortSingleIntegerElementArray() { Integer[] inputArray = {4}; Integer[] outputArray = bubbleSort.sort(inputArray); - Integer[] expectedOutput = {4}; - assertArrayEquals(outputArray, expectedOutput); + assertArrayEquals(new Integer[] {4}, outputArray); } @Test - public void bubbleSortSingleStringElementArray() { + @DisplayName("BubbleSort should handle single string element") + void bubbleSortSingleStringElementArray() { String[] inputArray = {"s"}; String[] outputArray = bubbleSort.sort(inputArray); - String[] expectedOutput = {"s"}; - assertArrayEquals(outputArray, expectedOutput); + assertArrayEquals(new String[] {"s"}, outputArray); } @Test - public void bubbleSortIntegerArray() { + @DisplayName("BubbleSort should sort integer array with negatives and duplicates") + void bubbleSortIntegerArray() { Integer[] inputArray = {4, 23, -6, 78, 1, 54, 23, -6, -231, 9, 12}; Integer[] outputArray = bubbleSort.sort(inputArray); Integer[] expectedOutput = { - -231, - -6, - -6, - 1, - 4, - 9, - 12, - 23, - 23, - 54, - 78, + -231, -6, -6, 1, 4, 9, 12, 23, 23, 54, 78 }; - assertArrayEquals(outputArray, expectedOutput); + assertArrayEquals(expectedOutput, outputArray); } @Test - public void bubbleSortStringArray() { + @DisplayName("BubbleSort should sort string array") + void bubbleSortStringArray() { String[] inputArray = { - "cbf", - "auk", - "ó", - "(b", - "a", - ")", - "au", - "á", - "cba", - "auk", - "(a", - "bhy", - "cba", + "cbf", "auk", "ó", "(b", "a", ")", "au", + "á", "cba", "auk", "(a", "bhy", "cba" }; - String[] outputArray = bubbleSort.sort(inputArray); String[] expectedOutput = { - "(a", - "(b", - ")", - "a", - "au", - "auk", - "auk", - "bhy", - "cba", - "cba", - "cbf", - "á", - "ó", + "(a", "(b", ")", "a", "au", "auk", "auk", + "bhy", "cba", "cba", "cbf", "á", "ó" }; - assertArrayEquals(outputArray, expectedOutput); + String[] outputArray = bubbleSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); } @Test - public void bubbleSortAlreadySortedArray() { + @DisplayName("BubbleSort should handle already sorted array") + 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); + assertArrayEquals(inputArray, outputArray); } @Test - public void bubbleSortReversedSortedArray() { + @DisplayName("BubbleSort should sort reverse ordered array") + 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); + Integer[] outputArray = bubbleSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); } @Test - public void bubbleSortAllEqualArray() { + @DisplayName("BubbleSort should handle array with all equal elements") + void bubbleSortAllEqualArray() { Integer[] inputArray = {2, 2, 2, 2, 2}; Integer[] outputArray = bubbleSort.sort(inputArray); - Integer[] expectedOutput = {2, 2, 2, 2, 2}; - assertArrayEquals(outputArray, expectedOutput); + assertArrayEquals(inputArray, outputArray); } @Test - public void bubbleSortMixedCaseStrings() { + @DisplayName("BubbleSort should sort mixed case strings using natural order") + void bubbleSortMixedCaseStrings() { String[] inputArray = {"banana", "Apple", "apple", "Banana"}; String[] expectedOutput = {"Apple", "Banana", "apple", "banana"}; String[] outputArray = bubbleSort.sort(inputArray); assertArrayEquals(expectedOutput, outputArray); } + @Test + @DisplayName("BubbleSort should sort negative numbers correctly") + void bubbleSortNegativeNumbers() { + Integer[] inputArray = {5, -1, 7, 0}; + Integer[] expectedOutput = {-1, 0, 5, 7}; + Integer[] outputArray = bubbleSort.sort(inputArray); + assertArrayEquals(expectedOutput, outputArray); + } + + @Test + @DisplayName("BubbleSort should be stable for equal elements") + void bubbleSortStabilityTest() { + Person[] inputArray = { + new Person("Alice", 25), + new Person("Bob", 25), + new Person("Charlie", 30), + }; + + Person[] outputArray = bubbleSort.sort(inputArray); + + assertEquals("Alice", outputArray[0].name); + assertEquals("Bob", outputArray[1].name); + } + + @Test + @DisplayName("BubbleSort should throw exception for null input") + void bubbleSortNullArray() { + assertThrows( + IllegalArgumentException.class, + () -> bubbleSort.sort(null) + ); + } + /** - * Custom Comparable class for testing. - **/ + * Custom Comparable class for testing stability. + */ static class Person implements Comparable { String name; int age; @@ -138,18 +146,14 @@ static class Person implements Comparable { } @Override - public int compareTo(Person o) { - return Integer.compare(this.age, o.age); + public int compareTo(Person other) { + return Integer.compare(this.age, other.age); } @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } + if (this == o) return true; + if (!(o instanceof Person)) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @@ -159,28 +163,4 @@ public int hashCode() { return Objects.hash(name, age); } } - - @Test - public void bubbleSortNegativeNumbers() { - Integer[] inputArray = {5, -1, 7, 0}; - Integer[] outputArray = bubbleSort.sort(inputArray); - Integer[] expectedOutput = {-1, 0, 5, 7}; - assertArrayEquals(expectedOutput, outputArray); - } - - @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); - } }