Skip to content

Commit 203b620

Browse files
committed
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
1 parent f6d6795 commit 203b620

File tree

5 files changed

+227
-217
lines changed

5 files changed

+227
-217
lines changed

src/test/java/com/thealgorithms/maths/FactorialTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,41 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6+
import java.math.BigInteger;
7+
import org.junit.jupiter.api.DisplayName;
68
import org.junit.jupiter.api.Test;
79

8-
public class FactorialTest {
10+
/**
11+
* Unit tests for Factorial.
12+
*/
13+
class FactorialTest {
14+
915
private static final String EXCEPTION_MESSAGE = "Input number cannot be negative";
1016

1117
@Test
12-
public void testWhenInvalidInoutProvidedShouldThrowException() {
13-
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
14-
assertEquals(exception.getMessage(), EXCEPTION_MESSAGE);
18+
@DisplayName("Factorial should throw exception for negative input")
19+
void testWhenInvalidInputProvidedShouldThrowException() {
20+
IllegalArgumentException exception =
21+
assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
22+
23+
assertEquals(EXCEPTION_MESSAGE, exception.getMessage());
1524
}
1625

1726
@Test
18-
public void testCorrectFactorialCalculation() {
27+
@DisplayName("Factorial should return correct values for small numbers")
28+
void testCorrectFactorialCalculation() {
1929
assertEquals(1, Factorial.factorial(0));
2030
assertEquals(1, Factorial.factorial(1));
2131
assertEquals(120, Factorial.factorial(5));
22-
assertEquals(3628800, Factorial.factorial(10));
32+
assertEquals(3_628_800, Factorial.factorial(10));
33+
}
34+
35+
@Test
36+
@DisplayName("Factorial should correctly calculate large values")
37+
void testLargeFactorial() {
38+
assertEquals(
39+
new BigInteger("2432902008176640000"),
40+
Factorial.factorial(20)
41+
);
2342
}
2443
}
Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
11
package com.thealgorithms.maths;
22

3-
import org.junit.jupiter.api.Assertions;
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.DisplayName;
47
import org.junit.jupiter.api.Test;
58

69
/**
7-
* Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
10+
* Unit tests for FibonacciNumberCheck.
11+
*
12+
* Fibonacci sequence:
13+
* 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
814
*
9-
* @author Albina Gimaletdinova on 01/07/2023
15+
* @author Albina
1016
*/
11-
public class FibonacciNumberCheckTest {
17+
class FibonacciNumberCheckTest {
18+
19+
@Test
20+
@DisplayName("Should return true for Fibonacci numbers")
21+
void testNumberIsFibonacciNumber() {
22+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(0));
23+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(1));
24+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(2));
25+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(21));
26+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(6765)); // 20th number
27+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(832040)); // 30th number
28+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(102334155)); // 40th number
29+
assertTrue(FibonacciNumberCheck.isFibonacciNumber(701408733)); // 45th number
30+
}
31+
1232
@Test
13-
public void testNumberIsFibonacciNumber() {
14-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(1));
15-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(2));
16-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(21));
17-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(6765)); // 20th number
18-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(832040)); // 30th number
19-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(102334155)); // 40th number
20-
Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(701408733)); // 45th number
33+
@DisplayName("Should return false for non-Fibonacci numbers")
34+
void testNumberIsNotFibonacciNumber() {
35+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(9));
36+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(10));
37+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(145));
38+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(701408734));
2139
}
2240

2341
@Test
24-
public void testNumberIsNotFibonacciNumber() {
25-
Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(9));
26-
Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(10));
27-
Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(145));
28-
Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(701408734));
42+
@DisplayName("Should return false for negative numbers")
43+
void testNegativeNumbersAreNotFibonacci() {
44+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(-1));
45+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(-5));
46+
assertFalse(FibonacciNumberCheck.isFibonacciNumber(Integer.MIN_VALUE));
2947
}
3048
}
Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,98 @@
11
package com.thealgorithms.maths;
22

3-
import org.junit.jupiter.api.Assertions;
4-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
public class GCDTest {
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
78

8-
@Test
9-
void testNegativeAndZeroThrowsException() {
10-
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-1, 0));
11-
}
9+
/**
10+
* Unit tests for GCD implementation.
11+
*/
12+
class GCDTest {
1213

1314
@Test
14-
void testPositiveAndNegativeThrowsException() {
15-
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(10, -2));
16-
}
17-
18-
@Test
19-
void testBothNegativeThrowsException() {
20-
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-5, -3));
15+
@DisplayName("GCD should handle zero and positive number")
16+
void testZeroAndPositiveReturnsPositive() {
17+
assertEquals(2, GCD.gcd(0, 2));
18+
assertEquals(10, GCD.gcd(10, 0));
19+
assertEquals(1, GCD.gcd(1, 0));
2120
}
2221

2322
@Test
24-
void testZeroAndPositiveReturnsPositive() {
25-
Assertions.assertEquals(2, GCD.gcd(0, 2));
23+
@DisplayName("GCD should work with negative inputs using absolute values")
24+
void testNegativeInputsHandledCorrectly() {
25+
assertEquals(1, GCD.gcd(-1, 0));
26+
assertEquals(2, GCD.gcd(10, -2));
27+
assertEquals(1, GCD.gcd(-5, -3));
28+
assertEquals(3, GCD.gcd(-9, 6));
2629
}
2730

2831
@Test
29-
void testPositiveAndZeroReturnsPositive() {
30-
Assertions.assertEquals(10, GCD.gcd(10, 0));
32+
@DisplayName("GCD should return correct result for two positive numbers")
33+
void testTwoPositiveNumbers() {
34+
assertEquals(3, GCD.gcd(9, 6));
3135
}
3236

3337
@Test
34-
void testOneAndZeroReturnsOne() {
35-
Assertions.assertEquals(1, GCD.gcd(1, 0));
38+
@DisplayName("GCD should return same number when both inputs are equal")
39+
void testSameNumbers() {
40+
assertEquals(7, GCD.gcd(7, 7));
3641
}
3742

3843
@Test
39-
void testTwoPositiveNumbers() {
40-
Assertions.assertEquals(3, GCD.gcd(9, 6));
44+
@DisplayName("GCD of two prime numbers should be one")
45+
void testPrimeNumbersHaveGcdOne() {
46+
assertEquals(1, GCD.gcd(13, 17));
4147
}
4248

4349
@Test
50+
@DisplayName("GCD should handle multiple arguments")
4451
void testMultipleArgumentsGcd() {
45-
Assertions.assertEquals(6, GCD.gcd(48, 18, 30, 12));
52+
assertEquals(6, GCD.gcd(48, 18, 30, 12));
4653
}
4754

4855
@Test
56+
@DisplayName("GCD should handle array input")
4957
void testArrayInputGcd() {
50-
Assertions.assertEquals(3, GCD.gcd(new int[] {9, 6}));
58+
assertEquals(3, GCD.gcd(new int[] {9, 6}));
5159
}
5260

5361
@Test
62+
@DisplayName("GCD should handle array with common factor")
5463
void testArrayWithCommonFactor() {
55-
Assertions.assertEquals(5, GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13}));
64+
assertEquals(
65+
5,
66+
GCD.gcd(new int[] {
67+
2 * 3 * 5 * 7,
68+
2 * 5 * 5 * 5,
69+
2 * 5 * 11,
70+
5 * 5 * 5 * 13
71+
})
72+
);
5673
}
5774

5875
@Test
76+
@DisplayName("GCD of empty array should return zero")
5977
void testEmptyArrayReturnsZero() {
60-
Assertions.assertEquals(0, GCD.gcd(new int[] {}));
61-
}
62-
63-
@Test
64-
void testSameNumbers() {
65-
Assertions.assertEquals(7, GCD.gcd(7, 7));
78+
assertEquals(0, GCD.gcd(new int[] {}));
6679
}
6780

6881
@Test
69-
void testPrimeNumbersHaveGcdOne() {
70-
Assertions.assertEquals(1, GCD.gcd(13, 17));
82+
@DisplayName("GCD of single-element array should return that element")
83+
void testSingleElementArrayReturnsElement() {
84+
assertEquals(42, GCD.gcd(new int[] {42}));
7185
}
7286

7387
@Test
74-
void testSingleElementArrayReturnsElement() {
75-
Assertions.assertEquals(42, GCD.gcd(new int[] {42}));
88+
@DisplayName("GCD should handle large numbers")
89+
void testLargeNumbers() {
90+
assertEquals(12, GCD.gcd(123456, 789012));
7691
}
7792

7893
@Test
79-
void testLargeNumbers() {
80-
Assertions.assertEquals(12, GCD.gcd(123456, 789012));
94+
@DisplayName("GCD should throw exception for null array")
95+
void testNullArrayThrowsException() {
96+
assertThrows(IllegalArgumentException.class, () -> GCD.gcd((int[]) null));
8197
}
8298
}
Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,85 @@
11
package com.thealgorithms.searches;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.*;
44

55
import java.util.stream.IntStream;
6+
import org.junit.jupiter.api.DisplayName;
67
import org.junit.jupiter.api.Test;
78

89
/**
910
* Unit tests for the BinarySearch class.
1011
*/
1112
class BinarySearchTest {
1213

13-
/**
14-
* Test for basic binary search functionality.
15-
*/
14+
private final BinarySearch binarySearch = new BinarySearch();
15+
1616
@Test
17+
@DisplayName("BinarySearch should find existing element")
1718
void testBinarySearchFound() {
18-
BinarySearch binarySearch = new BinarySearch();
1919
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2020
int key = 7;
21-
int expectedIndex = 6; // Index of the key in the array
22-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6.");
21+
assertEquals(6, binarySearch.find(array, key));
2322
}
2423

25-
/**
26-
* Test for binary search when the element is not present.
27-
*/
2824
@Test
25+
@DisplayName("BinarySearch should return -1 when element is not found")
2926
void testBinarySearchNotFound() {
30-
BinarySearch binarySearch = new BinarySearch();
3127
Integer[] array = {1, 2, 3, 4, 5};
32-
int key = 6; // Element not present in the array
33-
int expectedIndex = -1; // Key not found
34-
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
28+
int key = 6;
29+
assertEquals(-1, binarySearch.find(array, key));
3530
}
3631

37-
/**
38-
* Test for binary search with first element as the key.
39-
*/
4032
@Test
33+
@DisplayName("BinarySearch should find first element")
4134
void testBinarySearchFirstElement() {
42-
BinarySearch binarySearch = new BinarySearch();
4335
Integer[] array = {1, 2, 3, 4, 5};
44-
int key = 1; // First element
45-
int expectedIndex = 0; // Index of the key in the array
46-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0.");
36+
assertEquals(0, binarySearch.find(array, 1));
4737
}
4838

49-
/**
50-
* Test for binary search with last element as the key.
51-
*/
5239
@Test
40+
@DisplayName("BinarySearch should find last element")
5341
void testBinarySearchLastElement() {
54-
BinarySearch binarySearch = new BinarySearch();
5542
Integer[] array = {1, 2, 3, 4, 5};
56-
int key = 5; // Last element
57-
int expectedIndex = 4; // Index of the key in the array
58-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4.");
43+
assertEquals(4, binarySearch.find(array, 5));
5944
}
6045

61-
/**
62-
* Test for binary search with a single element present.
63-
*/
6446
@Test
47+
@DisplayName("BinarySearch should handle single-element array (found)")
6548
void testBinarySearchSingleElementFound() {
66-
BinarySearch binarySearch = new BinarySearch();
6749
Integer[] array = {1};
68-
int key = 1; // Only element present
69-
int expectedIndex = 0; // Index of the key in the array
70-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0.");
50+
assertEquals(0, binarySearch.find(array, 1));
7151
}
7252

73-
/**
74-
* Test for binary search with a single element not present.
75-
*/
7653
@Test
54+
@DisplayName("BinarySearch should handle single-element array (not found)")
7755
void testBinarySearchSingleElementNotFound() {
78-
BinarySearch binarySearch = new BinarySearch();
7956
Integer[] array = {1};
80-
int key = 2; // Key not present
81-
int expectedIndex = -1; // Key not found
82-
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
57+
assertEquals(-1, binarySearch.find(array, 2));
8358
}
8459

85-
/**
86-
* Test for binary search with an empty array.
87-
*/
8860
@Test
61+
@DisplayName("BinarySearch should return -1 for empty array")
8962
void testBinarySearchEmptyArray() {
90-
BinarySearch binarySearch = new BinarySearch();
91-
Integer[] array = {}; // Empty array
92-
int key = 1; // Key not present
93-
int expectedIndex = -1; // Key not found
94-
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array.");
63+
Integer[] array = {};
64+
assertEquals(-1, binarySearch.find(array, 1));
9565
}
9666

97-
/**
98-
* Test for binary search on large array.
99-
*/
10067
@Test
68+
@DisplayName("BinarySearch should handle large sorted array")
10169
void testBinarySearchLargeArray() {
102-
BinarySearch binarySearch = new BinarySearch();
103-
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
104-
int key = 9999; // Last element
105-
int expectedIndex = 9999; // Index of the last element
106-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
70+
Integer[] array = IntStream.range(0, 10_000)
71+
.boxed()
72+
.toArray(Integer[]::new);
73+
74+
assertEquals(9999, binarySearch.find(array, 9999));
75+
}
76+
77+
@Test
78+
@DisplayName("BinarySearch should throw exception for null array")
79+
void testBinarySearchNullArray() {
80+
assertThrows(
81+
IllegalArgumentException.class,
82+
() -> binarySearch.find(null, 5)
83+
);
10784
}
10885
}

0 commit comments

Comments
 (0)