Repository: TheAlgorithms/Java
Pull request #4225 (2023-07-01 12:23:13)

Added unit tests to FibonacciNumberCheck
- added unit tests
- minor refactoring

<!-- 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/DIRECTORY.md b/DIRECTORY.md
index 4e20adc8..782bb74a 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -272,7 +272,7 @@
             * [FFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFT.java)
             * [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFTBluestein.java)
             * [FibonacciJavaStreams](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java)
-            * [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FibonacciNumber.java)
+            * [FibonacciNumberCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java)
             * [FindKthNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FindKthNumber.java)
             * [FindMax](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FindMax.java)
             * [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java)
@@ -637,6 +637,7 @@
             * [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java)
             * [FastInverseSqrtTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java)
             * [FFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FFTTest.java)
+            * [FibonacciNumberCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java)
             * [FindMaxTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMaxTest.java)
             * [FindMinTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMinTest.java)
             * [FrizzyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FrizzyNumberTest.java)
diff --git a/src/main/java/com/thealgorithms/maths/FibonacciNumber.java b/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
similarity index 60%
rename from src/main/java/com/thealgorithms/maths/FibonacciNumber.java
rename to src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
index c39f2a8b..93778654 100644
--- a/src/main/java/com/thealgorithms/maths/FibonacciNumber.java
+++ b/src/main/java/com/thealgorithms/maths/FibonacciNumberCheck.java
@@ -2,17 +2,10 @@ package com.thealgorithms.maths;
 
 /**
  * Fibonacci: 0 1 1 2 3 5 8 13 21 ...
+ * This code checks Fibonacci Numbers up to 45th number.
+ * Other checks fail because of 'long'-type overflow.
  */
-public class FibonacciNumber {
-
-    public static void main(String[] args) {
-        assert isFibonacciNumber(1);
-        assert isFibonacciNumber(2);
-        assert isFibonacciNumber(21);
-        assert !isFibonacciNumber(9);
-        assert !isFibonacciNumber(10);
-    }
-
+public class FibonacciNumberCheck {
     /**
      * Check if a number is perfect square number
      *
@@ -20,8 +13,8 @@ public class FibonacciNumber {
      * @return <tt>true</tt> if {@code number} is a perfect square, otherwise
      *         <tt>false</tt>
      */
-    public static boolean isPerfectSquare(int number) {
-        int sqrt = (int) Math.sqrt(number);
+    public static boolean isPerfectSquare(long number) {
+        long sqrt = (long) Math.sqrt(number);
         return sqrt * sqrt == number;
     }
 
@@ -34,9 +27,9 @@ public class FibonacciNumber {
      *         <tt>false</tt>
      * @link https://en.wikipedia.org/wiki/Fibonacci_number#Identification
      */
-    public static boolean isFibonacciNumber(int number) {
-        int value1 = 5 * number * number + 4;
-        int value2 = 5 * number * number - 4;
+    public static boolean isFibonacciNumber(long number) {
+        long value1 = 5 * number * number + 4;
+        long value2 = 5 * number * number - 4;
         return isPerfectSquare(value1) || isPerfectSquare(value2);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java b/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java
new file mode 100644
index 00000000..6ba81639
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java
@@ -0,0 +1,30 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
+ *
+ * @author Albina Gimaletdinova on 01/07/2023
+ */
+public class FibonacciNumberCheckTest {
+    @Test
+    public void testNumberIsFibonacciNumber() {
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(1));
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(2));
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(21));
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(6765)); // 20th number
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(832040)); // 30th number
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(102334155)); // 40th number
+        Assertions.assertTrue(FibonacciNumberCheck.isFibonacciNumber(701408733)); // 45th number
+    }
+
+    @Test
+    public void testNumberIsNotFibonacciNumber() {
+        Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(9));
+        Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(10));
+        Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(145));
+        Assertions.assertFalse(FibonacciNumberCheck.isFibonacciNumber(701408734));
+    }
+}