Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 25 additions & 6 deletions src/test/java/com/thealgorithms/maths/FactorialTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
90 changes: 53 additions & 37 deletions src/test/java/com/thealgorithms/maths/GCDTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
93 changes: 35 additions & 58 deletions src/test/java/com/thealgorithms/searches/BinarySearchTest.java
Original file line number Diff line number Diff line change
@@ -1,108 +1,85 @@
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;

/**
* Unit tests for the BinarySearch class.
*/
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)
);
}
}
Loading
Loading