Repository: TheAlgorithms/Java
Pull request #4276 (2023-07-31 18:41:41)

tests: add tests for `GenericRoot`
This PR:
- adds tests for the class [`GenericRoot`](https://github.com/TheAlgorithms/Java/blob/087d523ed0e747adb9a681499f40d427c84358d2/src/main/java/com/thealgorithms/maths/GenericRoot.java#L7),
- does some refactoring if it.

I do not know about the general policy here: should I use the existing methods from [`SumOfDigits`](https://github.com/TheAlgorithms/Java/blob/087d523ed0e747adb9a681499f40d427c84358d2/src/main/java/com/thealgorithms/maths/SumOfDigits.java#L3)?

<!-- 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/maths/GenericRoot.java b/src/main/java/com/thealgorithms/maths/GenericRoot.java
index 1058da9e..07f4756f 100644
--- a/src/main/java/com/thealgorithms/maths/GenericRoot.java
+++ b/src/main/java/com/thealgorithms/maths/GenericRoot.java
@@ -4,27 +4,27 @@ package com.thealgorithms.maths;
  * Algorithm explanation:
  * https://technotip.com/6774/c-program-to-find-generic-root-of-a-number/#:~:text=Generic%20Root%3A%20of%20a%20number,get%20a%20single%2Ddigit%20output.&text=For%20Example%3A%20If%20user%20input,%2B%204%20%2B%205%20%3D%2015.
  */
-public class GenericRoot {
+public final class GenericRoot {
+    private GenericRoot() {
+    }
+
+    private static int base = 10;
 
-    public static void main(String[] args) {
-        int number1 = 1234;
-        int number2 = 12345;
-        int result1 = genericRoot(number1);
-        int result2 = genericRoot(number2);
-        System.out.println("Generic root of " + number1 + " is: " + result1);
-        System.out.println("Generic root of " + number2 + " is: " + result2);
+    private static int sumOfDigits(final int n) {
+        assert n >= 0;
+        if (n < base) {
+            return n;
+        }
+        return n % base + sumOfDigits(n / base);
     }
 
-    private static int genericRoot(int n) {
-        int root = 0;
-        while (n > 0 || root > 9) {
-            if (n == 0) {
-                n = root;
-                root = 0;
-            }
-            root += n % 10;
-            n /= 10;
+    public static int genericRoot(final int n) {
+        if (n < 0) {
+            return genericRoot(-n);
+        }
+        if (n > base) {
+            return genericRoot(sumOfDigits(n));
         }
-        return root;
+        return n;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/GenericRootTest.java b/src/test/java/com/thealgorithms/maths/GenericRootTest.java
new file mode 100644
index 00000000..50858cde
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/GenericRootTest.java
@@ -0,0 +1,24 @@
+package com.thealgorithms.maths;
+
+import static java.util.Map.entry;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+public class GenericRootTest {
+    private final Map<Integer, Integer> testCases = Map.ofEntries(entry(0, 0), entry(1, 1), entry(12345, 6), entry(123, 6), entry(15937, 7), entry(222222, 3), entry(99999, 9));
+    @Test
+    public void testGenericRoot() {
+        for (final var tc : testCases.entrySet()) {
+            assertEquals(tc.getValue(), GenericRoot.genericRoot(tc.getKey()));
+        }
+    }
+
+    @Test
+    public void testGenericRootWithNegativeInputs() {
+        for (final var tc : testCases.entrySet()) {
+            assertEquals(tc.getValue(), GenericRoot.genericRoot(-tc.getKey()));
+        }
+    }
+}