forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
plugins { | ||
`java-library` | ||
} | ||
|
||
dependencies { | ||
api(project(":fibonacci-spi")) | ||
compileOnly("com.google.auto.service:auto-service-annotations:1.0-rc6") | ||
annotationProcessor("com.google.auto.service:auto-service:1.0-rc6") | ||
|
||
testImplementation(testFixtures(project(":fibonacci-spi"))) | ||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.5.2") | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.5.2") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
20 changes: 20 additions & 0 deletions
20
...e-6/fibonacci-recursive/src/main/java/com/baeldung/fibonacci/impl/RecursiveFibonacci.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.fibonacci.impl; | ||
|
||
import com.baeldung.fibonacci.FibonacciSequenceGenerator; | ||
import com.google.auto.service.AutoService; | ||
|
||
/** Recursive implementation of the {@link FibonacciSequenceGenerator}. */ | ||
@AutoService(FibonacciSequenceGenerator.class) | ||
public final class RecursiveFibonacci implements FibonacciSequenceGenerator { | ||
|
||
@Override | ||
public int generate(int nth) { | ||
if (nth < 0) { | ||
throw new IllegalArgumentException("sequence number must be 0 or greater"); | ||
} | ||
if (nth <= 1) { | ||
return nth; | ||
} | ||
return generate(nth - 1) + generate(nth - 2); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...nacci-recursive/src/test/java/com/baeldung/fibonacci/impl/RecursiveFibonacciUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.fibonacci.impl; | ||
|
||
import com.baeldung.fibonacci.FibonacciSequenceGenerator; | ||
import com.baeldung.fibonacci.FibonacciSequenceGeneratorFixture; | ||
|
||
/** | ||
* Unit test which reuses the {@link FibonacciSequenceGeneratorFixture} test mix-in exported from | ||
* the fibonacci-spi project. | ||
*/ | ||
final class RecursiveFibonacciUnitTest implements FibonacciSequenceGeneratorFixture { | ||
|
||
@Override | ||
public FibonacciSequenceGenerator provide() { | ||
return new RecursiveFibonacci(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
plugins { | ||
`java-library` | ||
`java-test-fixtures` | ||
} | ||
|
||
dependencies { | ||
testFixturesApi("org.junit.jupiter:junit-jupiter-api:5.5.2") | ||
testFixturesImplementation("org.junit.jupiter:junit-jupiter-engine:5.5.2") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
11 changes: 11 additions & 0 deletions
11
gradle-6/fibonacci-spi/src/main/java/com/baeldung/fibonacci/FibonacciSequenceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.baeldung.fibonacci; | ||
|
||
/** Describes an SPI for a Fibonacci sequence generator function. */ | ||
public interface FibonacciSequenceGenerator { | ||
|
||
/** | ||
* @param nth the index of the number in the fibonacci sequence | ||
* @return the nth number in the fibonacci sequence | ||
*/ | ||
int generate(int nth); | ||
} |
31 changes: 31 additions & 0 deletions
31
...i-spi/src/testFixtures/java/com/baeldung/fibonacci/FibonacciSequenceGeneratorFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.baeldung.fibonacci; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Reusable test fixture for {@link FibonacciSequenceGenerator} implementations. Tests will be | ||
* skipped if no such implementation exists. | ||
*/ | ||
public interface FibonacciSequenceGeneratorFixture { | ||
|
||
/** @return the implementation of {@link FibonacciSequenceGenerator} to test. Must not be null */ | ||
FibonacciSequenceGenerator provide(); | ||
|
||
@Test | ||
default void when_sequence_index_is_negative_then_throws() { | ||
final FibonacciSequenceGenerator generator = provide(); | ||
assertThrows(IllegalArgumentException.class, () -> generator.generate(-1)); | ||
} | ||
|
||
@Test | ||
default void when_given_index_then_generates_fibonacci_number() { | ||
final FibonacciSequenceGenerator generator = provide(); | ||
final int[] sequence = {0, 1, 1, 2, 3, 5, 8}; | ||
for (int i = 0; i < sequence.length; i++) { | ||
assertEquals(sequence[i], generator.generate(i)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.gradle.parallel=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters