Skip to content

Commit 2be53b1

Browse files
authored
Merge branch 'master' into refactor/CheckVowels
2 parents 69e77f7 + 25b8010 commit 2be53b1

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

src/main/java/com/thealgorithms/others/EulersFunction.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package com.thealgorithms.others;
22

33
/**
4-
* @brief utility class for <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>
4+
* Utility class for computing
5+
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
56
*/
67
public final class EulersFunction {
78
private EulersFunction() {
89
}
910

11+
/**
12+
* Validates that the input is a positive integer.
13+
*
14+
* @param n the input number to validate
15+
* @throws IllegalArgumentException if {@code n} is non-positive
16+
*/
1017
private static void checkInput(int n) {
1118
if (n <= 0) {
1219
throw new IllegalArgumentException("n must be positive.");
1320
}
1421
}
1522

1623
/**
17-
* @brief computes the value of Euler's totient function for given input
18-
* @details has time complexity of O(sqrt(n))
19-
* @param n the input
20-
* @exception IllegalArgumentException n is non-positive
21-
* @return the value of Euler's totient function for the input
24+
* Computes the value of Euler's totient function for a given input.
25+
* This function has a time complexity of O(sqrt(n)).
26+
*
27+
* @param n the input number
28+
* @return the value of Euler's totient function for the given input
29+
* @throws IllegalArgumentException if {@code n} is non-positive
2230
*/
2331
public static int getEuler(int n) {
2432
checkInput(n);

src/test/java/com/thealgorithms/others/EulersFunctionTest.java

+21-27
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,31 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.util.HashMap;
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 EulersFunctionTest {
10-
@Test
11-
public void testGetEuler() {
12-
HashMap<Integer, Integer> testCases = new HashMap<>();
13-
testCases.put(1, 1);
14-
testCases.put(2, 1);
15-
testCases.put(3, 2);
16-
testCases.put(4, 2);
17-
testCases.put(5, 4);
18-
testCases.put(6, 2);
19-
testCases.put(10, 4);
20-
testCases.put(21, 12);
21-
testCases.put(69, 44);
22-
testCases.put(47, 46);
23-
testCases.put(46, 22);
24-
testCases.put(55, 40);
25-
testCases.put(34, 16);
26-
testCases.put(20, 8);
27-
testCases.put(20, 8);
28-
testCases.put(1024, 512);
2912

30-
for (final var tc : testCases.entrySet()) {
31-
assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey()));
32-
}
13+
@ParameterizedTest
14+
@MethodSource("provideNumbersForGetEuler")
15+
void testGetEuler(int input, int expected) {
16+
assertEquals(expected, EulersFunction.getEuler(input));
3317
}
3418

35-
@Test
36-
public void testGetEulerThrowsExceptionForNonPositiveInput() {
37-
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0));
19+
@ParameterizedTest
20+
@MethodSource("provideInvalidNumbersForGetEuler")
21+
void testGetEulerThrowsExceptionForNonPositiveInput(int input) {
22+
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(input));
23+
}
24+
25+
private static Stream<Arguments> provideNumbersForGetEuler() {
26+
return Stream.of(Arguments.of(1, 1), Arguments.of(2, 1), Arguments.of(3, 2), Arguments.of(4, 2), Arguments.of(5, 4), Arguments.of(6, 2), Arguments.of(10, 4), Arguments.of(21, 12), Arguments.of(69, 44), Arguments.of(47, 46), Arguments.of(46, 22), Arguments.of(55, 40), Arguments.of(34, 16),
27+
Arguments.of(20, 8), Arguments.of(1024, 512));
28+
}
29+
30+
private static Stream<Arguments> provideInvalidNumbersForGetEuler() {
31+
return Stream.of(Arguments.of(0), Arguments.of(-1), Arguments.of(-10));
3832
}
3933
}

0 commit comments

Comments
 (0)