Repository: TheAlgorithms/Java
Pull request #5645 (2024-10-08 10:22:36)

Add tests, remove `main`, add negativity test in `Fibonacci.java`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- 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.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5645?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 58.42%. Comparing base [(`0d68b65`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0d68b655d21d7f8d6a996f64c2e6f5d33eba95b5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`b4e7a85`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b4e7a8530da479b3d1675c4b0a0f207d045c8eed?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5645      +/-   ##
============================================
+ Coverage     58.20%   58.42%   +0.21%     
- Complexity     3693     3707      +14     
============================================
  Files           549      549              
  Lines         15850    15850              
  Branches       3015     3017       +2     
============================================
+ Hits           9226     9260      +34     
+ Misses         6232     6197      -35     
- Partials        392      393       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5645?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 520e7b33..94f5890b 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -791,6 +791,7 @@
             * [DPTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/DPTest.java)
             * [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
             * [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
+            * [FibonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java)
             * [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
             * [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
             * [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java b/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java
index 5855030f..0d6aff2b 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/Fibonacci.java
@@ -2,7 +2,6 @@ package com.thealgorithms.dynamicprogramming;
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Scanner;
 
 /**
  * @author Varun Upadhyay (https://github.com/varunu28)
@@ -11,27 +10,19 @@ public final class Fibonacci {
     private Fibonacci() {
     }
 
-    private static final Map<Integer, Integer> CACHE = new HashMap<>();
-
-    public static void main(String[] args) {
-        // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
-        Scanner sc = new Scanner(System.in);
-        int n = sc.nextInt();
-
-        System.out.println(fibMemo(n));
-        System.out.println(fibBotUp(n));
-        System.out.println(fibOptimized(n));
-        System.out.println(fibBinet(n));
-        sc.close();
-    }
+    static final Map<Integer, Integer> CACHE = new HashMap<>();
 
     /**
      * This method finds the nth fibonacci number using memoization technique
      *
      * @param n The input n for which we have to determine the fibonacci number
      * Outputs the nth fibonacci number
+     * @throws IllegalArgumentException if n is negative
      */
     public static int fibMemo(int n) {
+        if (n < 0) {
+            throw new IllegalArgumentException("Input n must be non-negative");
+        }
         if (CACHE.containsKey(n)) {
             return CACHE.get(n);
         }
@@ -52,8 +43,12 @@ public final class Fibonacci {
      *
      * @param n The input n for which we have to determine the fibonacci number
      * Outputs the nth fibonacci number
+     * @throws IllegalArgumentException if n is negative
      */
     public static int fibBotUp(int n) {
+        if (n < 0) {
+            throw new IllegalArgumentException("Input n must be non-negative");
+        }
         Map<Integer, Integer> fib = new HashMap<>();
 
         for (int i = 0; i <= n; i++) {
@@ -80,9 +75,13 @@ public final class Fibonacci {
      * Time Complexity will be O(n)
      * <p>
      * Whereas , the above functions will take O(n) Space.
+     * @throws IllegalArgumentException if n is negative
      * @author Shoaib Rayeen (https://github.com/shoaibrayeen)
      */
     public static int fibOptimized(int n) {
+        if (n < 0) {
+            throw new IllegalArgumentException("Input n must be non-negative");
+        }
         if (n == 0) {
             return 0;
         }
@@ -105,9 +104,14 @@ public final class Fibonacci {
      * = 1.6180339887... Now, let's look at Binet's formula: Sn = Φⁿ–(– Φ⁻ⁿ)/√5 We first calculate
      * the squareRootof5 and phi and store them in variables. Later, we apply Binet's formula to get
      * the required term. Time Complexity will be O(1)
+     * @param n The input n for which we have to determine the fibonacci number
+     * Outputs the nth fibonacci number
+     * @throws IllegalArgumentException if n is negative
      */
-
     public static int fibBinet(int n) {
+        if (n < 0) {
+            throw new IllegalArgumentException("Input n must be non-negative");
+        }
         double squareRootOf5 = Math.sqrt(5);
         double phi = (1 + squareRootOf5) / 2;
         return (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java
new file mode 100644
index 00000000..166e20c3
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java
@@ -0,0 +1,89 @@
+package com.thealgorithms.dynamicprogramming;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class FibonacciTest {
+
+    @BeforeEach
+    void setUp() {
+        // Clear the cache before each test to avoid interference
+        Fibonacci.CACHE.clear();
+    }
+
+    @Test
+    void testFibMemo() {
+        // Test memoization method
+        assertEquals(0, Fibonacci.fibMemo(0));
+        assertEquals(1, Fibonacci.fibMemo(1));
+        assertEquals(1, Fibonacci.fibMemo(2));
+        assertEquals(2, Fibonacci.fibMemo(3));
+        assertEquals(3, Fibonacci.fibMemo(4));
+        assertEquals(5, Fibonacci.fibMemo(5));
+        assertEquals(8, Fibonacci.fibMemo(6));
+        assertEquals(13, Fibonacci.fibMemo(7));
+        assertEquals(21, Fibonacci.fibMemo(8));
+        assertEquals(34, Fibonacci.fibMemo(9));
+        assertEquals(55, Fibonacci.fibMemo(10));
+    }
+
+    @Test
+    void testFibBotUp() {
+        // Test bottom-up method
+        assertEquals(0, Fibonacci.fibBotUp(0));
+        assertEquals(1, Fibonacci.fibBotUp(1));
+        assertEquals(1, Fibonacci.fibBotUp(2));
+        assertEquals(2, Fibonacci.fibBotUp(3));
+        assertEquals(3, Fibonacci.fibBotUp(4));
+        assertEquals(5, Fibonacci.fibBotUp(5));
+        assertEquals(8, Fibonacci.fibBotUp(6));
+        assertEquals(13, Fibonacci.fibBotUp(7));
+        assertEquals(21, Fibonacci.fibBotUp(8));
+        assertEquals(34, Fibonacci.fibBotUp(9));
+        assertEquals(55, Fibonacci.fibBotUp(10));
+    }
+
+    @Test
+    void testFibOptimized() {
+        // Test optimized Fibonacci method
+        assertEquals(0, Fibonacci.fibOptimized(0));
+        assertEquals(1, Fibonacci.fibOptimized(1));
+        assertEquals(1, Fibonacci.fibOptimized(2));
+        assertEquals(2, Fibonacci.fibOptimized(3));
+        assertEquals(3, Fibonacci.fibOptimized(4));
+        assertEquals(5, Fibonacci.fibOptimized(5));
+        assertEquals(8, Fibonacci.fibOptimized(6));
+        assertEquals(13, Fibonacci.fibOptimized(7));
+        assertEquals(21, Fibonacci.fibOptimized(8));
+        assertEquals(34, Fibonacci.fibOptimized(9));
+        assertEquals(55, Fibonacci.fibOptimized(10));
+    }
+
+    @Test
+    void testFibBinet() {
+        // Test Binet's formula method
+        assertEquals(0, Fibonacci.fibBinet(0));
+        assertEquals(1, Fibonacci.fibBinet(1));
+        assertEquals(1, Fibonacci.fibBinet(2));
+        assertEquals(2, Fibonacci.fibBinet(3));
+        assertEquals(3, Fibonacci.fibBinet(4));
+        assertEquals(5, Fibonacci.fibBinet(5));
+        assertEquals(8, Fibonacci.fibBinet(6));
+        assertEquals(13, Fibonacci.fibBinet(7));
+        assertEquals(21, Fibonacci.fibBinet(8));
+        assertEquals(34, Fibonacci.fibBinet(9));
+        assertEquals(55, Fibonacci.fibBinet(10));
+    }
+
+    @Test
+    void testNegativeInput() {
+        // Test negative input; Fibonacci is not defined for negative numbers
+        assertThrows(IllegalArgumentException.class, () -> { Fibonacci.fibMemo(-1); });
+        assertThrows(IllegalArgumentException.class, () -> { Fibonacci.fibBotUp(-1); });
+        assertThrows(IllegalArgumentException.class, () -> { Fibonacci.fibOptimized(-1); });
+        assertThrows(IllegalArgumentException.class, () -> { Fibonacci.fibBinet(-1); });
+    }
+}