mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 05:22:39 +08:00
test: cleanup PrimeFactorizationTest
(#5382)
This commit is contained in:
@ -1,36 +1,32 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
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 {
|
||||
|
||||
@Test
|
||||
void testpFactorsMustReturnEmptyList() {
|
||||
// given
|
||||
int n = 0;
|
||||
|
||||
// then
|
||||
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideNumbersAndFactors")
|
||||
void testPrimeFactorization(int number, List<Integer> expectedFactors) {
|
||||
assertEquals(expectedFactors, PrimeFactorization.pfactors(number), "Prime factors for number: " + number);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testpFactorsMustReturnNonEmptyList() {
|
||||
// given
|
||||
int n = 198;
|
||||
int expectedListSize = 4;
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideNumbersAndSizes")
|
||||
void testPrimeFactorsSize(int number, int expectedSize) {
|
||||
assertEquals(expectedSize, PrimeFactorization.pfactors(number).size(), "Size of prime factors list for number: " + number);
|
||||
}
|
||||
|
||||
// when
|
||||
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
|
||||
private static Stream<Arguments> provideNumbersAndFactors() {
|
||||
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
|
||||
assertEquals(expectedListSize, actualResultList.size());
|
||||
assertEquals(2, actualResultList.get(0));
|
||||
assertEquals(3, actualResultList.get(1));
|
||||
assertEquals(3, actualResultList.get(2));
|
||||
assertEquals(11, actualResultList.get(3));
|
||||
private static Stream<Arguments> provideNumbersAndSizes() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user