test: cleanup PrimeFactorizationTest (#5382)

This commit is contained in:
Alex Klymenko
2024-08-25 08:56:02 +02:00
committed by GitHub
parent a8d3b6ad2d
commit e5c0e4bff0

View File

@ -1,36 +1,32 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class PrimeFactorizationTest { class PrimeFactorizationTest {
@Test @ParameterizedTest
void testpFactorsMustReturnEmptyList() { @MethodSource("provideNumbersAndFactors")
// given void testPrimeFactorization(int number, List<Integer> expectedFactors) {
int n = 0; assertEquals(expectedFactors, PrimeFactorization.pfactors(number), "Prime factors for number: " + number);
// then
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
} }
@Test @ParameterizedTest
void testpFactorsMustReturnNonEmptyList() { @MethodSource("provideNumbersAndSizes")
// given void testPrimeFactorsSize(int number, int expectedSize) {
int n = 198; assertEquals(expectedSize, PrimeFactorization.pfactors(number).size(), "Size of prime factors list for number: " + number);
int expectedListSize = 4; }
// when private static Stream<Arguments> provideNumbersAndFactors() {
List<Integer> actualResultList = PrimeFactorization.pfactors(n); 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)));
}
// then private static Stream<Arguments> provideNumbersAndSizes() {
assertEquals(expectedListSize, actualResultList.size()); 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));
assertEquals(2, actualResultList.get(0));
assertEquals(3, actualResultList.get(1));
assertEquals(3, actualResultList.get(2));
assertEquals(11, actualResultList.get(3));
} }
} }