Skip to content

test: cleanup PrimeFactorizationTest #5382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions src/test/java/com/thealgorithms/maths/PrimeFactorizationTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}