Refactor FibonacciJavaStreams and add unit tests (#4260)

This commit is contained in:
Albina Gimaletdinova 2023-07-25 22:42:09 +03:00 committed by GitHub
parent ef4ef42ed3
commit dec3b98e4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 70 deletions

View File

@ -608,6 +608,7 @@
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
* [OptimalJobSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/OptimalJobSchedulingTest.java)
* [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java)
* [SubsetCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java)
@ -637,6 +638,7 @@
* [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java)
* [FastInverseSqrtTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java)
* [FFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FFTTest.java)
* [FibonacciJavaStreamsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciJavaStreamsTest.java)
* [FibonacciNumberCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java)
* [FindMaxTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMaxTest.java)
* [FindMinTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMinTest.java)
@ -673,6 +675,7 @@
* [SquareRootWithNewtonRaphsonTestMethod](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java)
* [StandardDeviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/StandardDeviationTest.java)
* [StandardScoreTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/StandardScoreTest.java)
* [SumOfArithmeticSeriesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumOfArithmeticSeriesTest.java)
* [SumOfDigitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumOfDigitsTest.java)
* [SumWithoutArithmeticOperatorsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java)
* [TestArmstrong](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TestArmstrong.java)

View File

@ -2,7 +2,6 @@ package com.thealgorithms.maths;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
@ -14,7 +13,7 @@ public class FibonacciJavaStreams {
public static Optional<BigDecimal> calculate(final BigDecimal index) {
if (index == null || index.compareTo(BigDecimal.ZERO) < 0) {
return Optional.empty();
throw new IllegalArgumentException("Input index cannot be null or negative!");
}
if (index.compareTo(BigDecimal.ONE) < 0) {
@ -30,72 +29,4 @@ public class FibonacciJavaStreams {
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(results.size() - 1));
}
public static void assertThat(final Object actual, final Object expected) {
if (!Objects.equals(actual, expected)) {
throw new AssertionError(String.format("expected=%s but was actual=%s", expected, actual));
}
}
public static void main(final String[] args) {
{
final Optional<BigDecimal> result = calculate(new BigDecimal(-1));
assertThat(result.isEmpty(), true);
}
{
final Optional<BigDecimal> result = calculate(BigDecimal.ZERO);
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, BigDecimal.ZERO));
}
{
final Optional<BigDecimal> result = calculate(BigDecimal.ONE);
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, BigDecimal.ONE));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(2));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, BigDecimal.ONE));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(3));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal(2)));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(10));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal(55)));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(20));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal(6765)));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(30));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal(832040)));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(40));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal(102334155)));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(50));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal(12586269025L)));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(100));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal("354224848179261915075")));
}
{
final Optional<BigDecimal> result = calculate(new BigDecimal(200));
assertThat(result.isPresent(), true);
result.ifPresent(value -> assertThat(value, new BigDecimal("280571172992510140037611932413038677189525")));
}
}
}

View File

@ -0,0 +1,71 @@
package com.thealgorithms.maths;
import java.math.BigDecimal;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* @author Albina Gimaletdinova on 25/07/2023
*/
public class FibonacciJavaStreamsTest {
private static final String EXCEPTION_MESSAGE = "Input index cannot be null or negative!";
@Test
public void testWithNegativeIndexShouldThrowException() {
Exception exception = Assertions.assertThrows(IllegalArgumentException.class, () -> FibonacciJavaStreams.calculate(new BigDecimal(-1)));
Assertions.assertEquals(EXCEPTION_MESSAGE, exception.getMessage());
}
@Test
public void testCheckTheFirst4SequenceElements() {
checkElement(BigDecimal.ZERO, BigDecimal.ZERO);
checkElement(BigDecimal.ONE, BigDecimal.ONE);
checkElement(new BigDecimal(2), BigDecimal.ONE);
checkElement(new BigDecimal(3), new BigDecimal(2));
}
@Test
public void testCheck10thSequenceElement() {
checkElement(new BigDecimal(10), new BigDecimal(55));
}
@Test
public void testCheck20thSequenceElement() {
checkElement(new BigDecimal(20), new BigDecimal(6765));
}
@Test
public void testCheck30thSequenceElement() {
checkElement(new BigDecimal(30), new BigDecimal(832040));
}
@Test
public void testCheck40thSequenceElement() {
checkElement(new BigDecimal(40), new BigDecimal(102334155));
}
@Test
public void testCheck50thSequenceElement() {
checkElement(new BigDecimal(50), new BigDecimal(12586269025L));
}
@Test
public void testCheck100thSequenceElement() {
checkElement(new BigDecimal(100), new BigDecimal("354224848179261915075"));
}
@Test
public void testCheck200thSequenceElement() {
checkElement(new BigDecimal(200), new BigDecimal("280571172992510140037611932413038677189525"));
}
private static void checkElement(BigDecimal index, BigDecimal expected) {
// when
Optional<BigDecimal> result = FibonacciJavaStreams.calculate(index);
// then
Assertions.assertTrue(result.isPresent());
Assertions.assertEquals(result.get(), expected);
}
}