Skip to content

Commit e5c0e4b

Browse files
authored
test: cleanup PrimeFactorizationTest (#5382)
1 parent a8d3b6a commit e5c0e4b

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
54

65
import java.util.List;
7-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
810

911
class PrimeFactorizationTest {
1012

11-
@Test
12-
void testpFactorsMustReturnEmptyList() {
13-
// given
14-
int n = 0;
15-
16-
// then
17-
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
13+
@ParameterizedTest
14+
@MethodSource("provideNumbersAndFactors")
15+
void testPrimeFactorization(int number, List<Integer> expectedFactors) {
16+
assertEquals(expectedFactors, PrimeFactorization.pfactors(number), "Prime factors for number: " + number);
1817
}
1918

20-
@Test
21-
void testpFactorsMustReturnNonEmptyList() {
22-
// given
23-
int n = 198;
24-
int expectedListSize = 4;
19+
@ParameterizedTest
20+
@MethodSource("provideNumbersAndSizes")
21+
void testPrimeFactorsSize(int number, int expectedSize) {
22+
assertEquals(expectedSize, PrimeFactorization.pfactors(number).size(), "Size of prime factors list for number: " + number);
23+
}
2524

26-
// when
27-
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
25+
private static Stream<Arguments> provideNumbersAndFactors() {
26+
return Stream.of(Arguments.of(0, List.of()), Arguments.of(1, List.of()), Arguments.of(2, List.of(2)), Arguments.of(3, List.of(3)), Arguments.of(4, List.of(2, 2)), Arguments.of(18, List.of(2, 3, 3)), Arguments.of(100, List.of(2, 2, 5, 5)), Arguments.of(198, List.of(2, 3, 3, 11)));
27+
}
2828

29-
// then
30-
assertEquals(expectedListSize, actualResultList.size());
31-
assertEquals(2, actualResultList.get(0));
32-
assertEquals(3, actualResultList.get(1));
33-
assertEquals(3, actualResultList.get(2));
34-
assertEquals(11, actualResultList.get(3));
29+
private static Stream<Arguments> provideNumbersAndSizes() {
30+
return Stream.of(Arguments.of(2, 1), Arguments.of(3, 1), Arguments.of(4, 2), Arguments.of(18, 3), Arguments.of(100, 4), Arguments.of(198, 4));
3531
}
3632
}

0 commit comments

Comments
 (0)