Repository: TheAlgorithms/Java
Pull request #4216 (2023-06-19 14:32:10)

tests: add tests for `EulersFunction`
This PR:
- adds tests for [`EulersFunction`](https://github.com/TheAlgorithms/Java/blob/e29a22770ba4b89be32df07448868379682d50f0/src/main/java/com/thealgorithms/others/EulersFunction.java#L9),
- updates `EulersFunction.getEuler` to throw an `IllegalArgumentException` for non-positive inputs, 
- makes `EulersFunction` a proper _utility_ class,
- updates the documentation.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/others/EulersFunction.java b/src/main/java/com/thealgorithms/others/EulersFunction.java
index b8ab2acf..27c9aed8 100644
--- a/src/main/java/com/thealgorithms/others/EulersFunction.java
+++ b/src/main/java/com/thealgorithms/others/EulersFunction.java
@@ -1,17 +1,27 @@
 package com.thealgorithms.others;
 
 /**
- * You can read more about Euler's totient function
- *
- * <p>
- * See https://en.wikipedia.org/wiki/Euler%27s_totient_function
+ * @brief utility class for <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>
  */
-public class EulersFunction {
+final public class EulersFunction {
+    private EulersFunction() {
+    }
 
-    // This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time
-    // complexity;
+    private static void checkInput(int n) {
+        if (n <= 0) {
+            throw new IllegalArgumentException("n must be positive.");
+        }
+    }
 
+    /**
+     * @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
+     */
     public static int getEuler(int n) {
+        checkInput(n);
         int result = n;
         for (int i = 2; i * i <= n; i++) {
             if (n % i == 0) {
@@ -26,10 +36,4 @@ public class EulersFunction {
         }
         return result;
     }
-
-    public static void main(String[] args) {
-        for (int i = 1; i < 100; i++) {
-            System.out.println(getEuler(i));
-        }
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/EulersFunctionTest.java b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java
new file mode 100644
index 00000000..b80926b8
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/EulersFunctionTest.java
@@ -0,0 +1,39 @@
+package com.thealgorithms.others;
+
+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;
+
+class EulersFunctionTest {
+    @Test
+    public void testGetEuler() {
+        HashMap<Integer, Integer> 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()));
+        }
+    }
+
+    @Test
+    public void testGetEulerThrowsExceptionForNonPositiveInput() {
+        assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0));
+    }
+}