From ef5810d2fbd983e3233a535a7d553b7de4bce9c8 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 25 Aug 2024 21:06:21 +0200 Subject: [PATCH 1/3] refactor: EulersFunction --- .../thealgorithms/others/EulersFunction.java | 20 +++++--- .../others/EulersFunctionTest.java | 51 +++++++++---------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/EulersFunction.java b/src/main/java/com/thealgorithms/others/EulersFunction.java index f08e5e4fa395..7a6f49d41758 100644 --- a/src/main/java/com/thealgorithms/others/EulersFunction.java +++ b/src/main/java/com/thealgorithms/others/EulersFunction.java @@ -1,12 +1,19 @@ package com.thealgorithms.others; /** - * @brief utility class for Euler's totient function + * Utility class for computing + * Euler's totient function. */ public final class EulersFunction { private EulersFunction() { } + /** + * Validates that the input is a positive integer. + * + * @param n the input number to validate + * @throws IllegalArgumentException if {@code n} is non-positive + */ private static void checkInput(int n) { if (n <= 0) { throw new IllegalArgumentException("n must be positive."); @@ -14,11 +21,12 @@ private static void checkInput(int n) { } /** - * @brief computes the value of Euler's totient function for given input - * @details has time complexity of O(sqrt(n)) - * @param n the input - * @exception IllegalArgumentException n is non-positive - * @return the value of Euler's totient function for the input + * Computes the value of Euler's totient function for a given input. + * This function has a time complexity of O(sqrt(n)). + * + * @param n the input number + * @return the value of Euler's totient function for the given input + * @throws IllegalArgumentException if {@code n} is non-positive */ public static int getEuler(int n) { checkInput(n); diff --git a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java index b80926b8c2dc..50f791c31210 100644 --- a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java +++ b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java @@ -3,37 +3,32 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.util.HashMap; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; class EulersFunctionTest { - @Test - public void testGetEuler() { - HashMap testCases = new HashMap<>(); - testCases.put(1, 1); - testCases.put(2, 1); - testCases.put(3, 2); - testCases.put(4, 2); - testCases.put(5, 4); - testCases.put(6, 2); - testCases.put(10, 4); - testCases.put(21, 12); - testCases.put(69, 44); - testCases.put(47, 46); - testCases.put(46, 22); - testCases.put(55, 40); - testCases.put(34, 16); - testCases.put(20, 8); - testCases.put(20, 8); - testCases.put(1024, 512); - - for (final var tc : testCases.entrySet()) { - assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey())); - } + + @ParameterizedTest + @MethodSource("provideNumbersForGetEuler") + void testGetEuler(int input, int expected) { + assertEquals(expected, EulersFunction.getEuler(input)); + } + + @ParameterizedTest + @MethodSource("provideInvalidNumbersForGetEuler") + void testGetEulerThrowsExceptionForNonPositiveInput(int input) { + assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(input)); } - @Test - public void testGetEulerThrowsExceptionForNonPositiveInput() { - assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0)); + private static Stream provideNumbersForGetEuler() { + 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), Arguments.of(20, 8), Arguments.of(1024, 512)); + } + + private static Stream provideInvalidNumbersForGetEuler() { + return Stream.of(Arguments.of(0), Arguments.of(-1), Arguments.of(-10)); } } + From b44a29551a4d5f7d559a7636c3e0c8a4d4393681 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 25 Aug 2024 21:09:47 +0200 Subject: [PATCH 2/3] checkstyle: fix formatting --- .../java/com/thealgorithms/others/EulersFunctionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java index 50f791c31210..3c3390025ce3 100644 --- a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java +++ b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java @@ -24,11 +24,11 @@ void testGetEulerThrowsExceptionForNonPositiveInput(int input) { } private static Stream provideNumbersForGetEuler() { - 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), Arguments.of(20, 8), Arguments.of(1024, 512)); + 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), + Arguments.of(20, 8), Arguments.of(1024, 512)); } private static Stream provideInvalidNumbersForGetEuler() { return Stream.of(Arguments.of(0), Arguments.of(-1), Arguments.of(-10)); } } - From 7dabf3c8f3c182e7f976fe75ecdd2416190b27f3 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 25 Aug 2024 21:11:23 +0200 Subject: [PATCH 3/3] checkstyle: fix import ordering --- src/test/java/com/thealgorithms/others/EulersFunctionTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java index 3c3390025ce3..655c67c83aaa 100644 --- a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java +++ b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java @@ -3,12 +3,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +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; -import java.util.stream.Stream; - class EulersFunctionTest { @ParameterizedTest