Skip to content

Commit fdcbdfc

Browse files
committed
GP-39 fun-prime-numbers
* update task description * fix tests
1 parent 131d7ce commit fdcbdfc

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

5-0-functional-programming/5-4-1-fun-prime-numbers/src/main/java/com/bobocode/fp/OOSumOfPrimes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
/**
44
* This examples demonstrates how to calculate the sum of prime numbers using Object-Oriented approach.
5-
* Please note that that in order to perform this calculation in OOP-style we have to use mutable variables
6-
* (e.g. sumOfPrimes, i, primes). I'll also notice that implementing the same functionality in a functional style
7-
* using Steam API and lambdas, you will not create and modify variables.
5+
* Please note that in order to perform this calculation in OOP-style we have to use mutable variables
6+
* (e.g. sumOfPrimes, i, primes). Implementing the same functionality in a functional style using Steam API and lambdas,
7+
* you will not create and/or modify variables.
88
*/
99
public class OOSumOfPrimes {
1010
public static void main(String[] args) {

5-0-functional-programming/5-4-1-fun-prime-numbers/src/main/java/com/bobocode/fp/PrimeNumbers.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import java.util.function.IntConsumer;
77

88
/**
9-
* {@link PrimeNumbers} provides an API to work with prime numbers. It is using a stream of prime numbers.
9+
* {@link PrimeNumbers} provides an API to work with prime numbers. The implementation is based on the
10+
* {@link java.util.stream.IntStream} of prime numbers. That stream is used in all public methods on this class.
1011
* <p>
1112
* See {@link OOSumOfPrimes} for a reference
1213
*/
@@ -38,7 +39,7 @@ public static List<Integer> collect(int n) {
3839
/**
3940
* Find a prime number by index and then applies a provided consumer passing found prime number
4041
*
41-
* @param idx the position of a prime number (index)
42+
* @param idx the position of a prime number (index), starting from 0
4243
* @param consumer a logic that should be applied to the found prime number
4344
*/
4445
public static void processByIndex(int idx, IntConsumer consumer) {
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.bobocode.fp;
22

3+
import org.junit.jupiter.api.MethodOrderer;
4+
import org.junit.jupiter.api.Order;
5+
import org.junit.jupiter.api.TestMethodOrder;
36
import org.junit.jupiter.params.ParameterizedTest;
47
import org.junit.jupiter.params.provider.Arguments;
58
import org.junit.jupiter.params.provider.CsvSource;
@@ -12,16 +15,19 @@
1215
import static org.assertj.core.api.Assertions.assertThat;
1316
import static org.junit.jupiter.params.provider.Arguments.arguments;
1417

18+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1519
class PrimeNumbersTest {
1620

21+
@Order(1)
1722
@ParameterizedTest
18-
@CsvSource({"0,0", "1,2", "2,5", "3,10", "4,17", "5,28", "10,129", "20,639"})
23+
@CsvSource({"0, 0", "1, 2", "2, 5", "3, 10", "4, 17", "5, 28", "10, 129", "20, 639"})
1924
void sum(int n, int sumOfPrimes) {
2025
int result = PrimeNumbers.sum(n);
2126

2227
assertThat(result).isEqualTo(sumOfPrimes);
2328
}
2429

30+
@Order(2)
2531
@ParameterizedTest
2632
@MethodSource("collectArgs")
2733
void collect(int n, List<Integer> primeNumbersList) {
@@ -30,9 +36,9 @@ void collect(int n, List<Integer> primeNumbersList) {
3036
assertThat(result).isEqualTo(primeNumbersList);
3137
}
3238

33-
39+
@Order(3)
3440
@ParameterizedTest
35-
@CsvSource({"1,1", "2,2", "3,3", "4,5", "5,7", "10,23", "20,67"})
41+
@CsvSource({"0, 2", "1, 3", "2, 5", "3, 7", "10, 31", "20, 73", "279, 1811"})
3642
void processByIndexFindsCorrectPrimeNumber(int index, int primeNumber) {
3743
var list = new ArrayList<>();
3844

@@ -43,12 +49,12 @@ void processByIndexFindsCorrectPrimeNumber(int index, int primeNumber) {
4349

4450
static Stream<Arguments> collectArgs() {
4551
return Stream.of(
46-
arguments(1, List.of(1)),
47-
arguments(2, List.of(1, 2)),
48-
arguments(3, List.of(1, 2, 3)),
49-
arguments(4, List.of(1, 2, 3, 5)),
50-
arguments(5, List.of(1, 2, 3, 5, 7)),
51-
arguments(10, List.of(1, 2, 3, 5, 7, 11, 13, 17, 19, 23))
52+
arguments(1, List.of(2)),
53+
arguments(2, List.of(2, 3)),
54+
arguments(3, List.of(2, 3, 5)),
55+
arguments(4, List.of(2, 3, 5, 7)),
56+
arguments(5, List.of(2, 3, 5, 7, 11)),
57+
arguments(10, List.of(2, 3, 5, 7, 11, 13, 17, 19, 23, 29))
5258
);
5359
}
5460
}

0 commit comments

Comments
 (0)