Repository: TheAlgorithms/Java
Pull request #4148 (2023-04-07 07:20:49)

JUnit Tests Added
<!-- 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.

What's New :

**Modified:**

- **_src/main/java/com/thealgorithms/maths/LeonardoNumber.java_** added a link to Wikipedia and description.
- **_src/main/java/com/thealgorithms/maths/LeonardoNumber.java_** main method removed.
- **_src/main/java/com/thealgorithms/maths/LucasSeries.java_** main method removed.
- **_src/main/java/com/thealgorithms/maths/Median.java_** main method removed.

**Added:**

- **_src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java_** LeonardoNumber JUnit test cases.
- **_src/test/java/com/thealgorithms/maths/LucasSeriesTest.java_** LucasSeries JUnit test cases.
- **_src/test/java/com/thealgorithms/maths/MedianTest.java_** Median JUnit test cases.

**Updates:**
- **_src/main/java/com/thealgorithms/maths/LeonardoNumber.java_** ArithmeticException added to negative **@_param n_** 
- **_src/main/java/com/thealgorithms/maths/LucasSeries.java_** description changed.

- **_src/main/java/com/thealgorithms/maths/Median.java_** medianUnsortedValues test removed.
- **_src/main/java/com/thealgorithms/maths/LeonardoNumberTest.java_** redundant comments removed.
- **_src/main/java/com/thealgorithms/maths/LucasSeriesTest.java_** redundant comments removed.





Patch:
diff --git a/src/main/java/com/thealgorithms/maths/LeonardoNumber.java b/src/main/java/com/thealgorithms/maths/LeonardoNumber.java
index 8af36e80..7b9620a4 100644
--- a/src/main/java/com/thealgorithms/maths/LeonardoNumber.java
+++ b/src/main/java/com/thealgorithms/maths/LeonardoNumber.java
@@ -1,20 +1,23 @@
 package com.thealgorithms.maths;
 
+ /**
+  * https://en.wikipedia.org/wiki/Leonardo_number
+  */
 public class LeonardoNumber {
 
+    /**
+     * Calculate nth Leonardo Number (1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, ...)
+     *
+     * @param n the index of Leonardo Number to calculate
+     * @return nth number of Leonardo sequences
+     */
     public static int leonardoNumber(int n) {
         if (n < 0) {
-            return 0;
+            throw new ArithmeticException();
         }
         if (n == 0 || n == 1) {
             return 1;
         }
         return (leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1);
     }
-
-    public static void main(String args[]) {
-        for (int i = 0; i < 20; i++) {
-            System.out.print(leonardoNumber(i) + " ");
-        }
-    }
 }
diff --git a/src/main/java/com/thealgorithms/maths/LucasSeries.java b/src/main/java/com/thealgorithms/maths/LucasSeries.java
index e1d9c336..59c9a9f3 100644
--- a/src/main/java/com/thealgorithms/maths/LucasSeries.java
+++ b/src/main/java/com/thealgorithms/maths/LucasSeries.java
@@ -5,22 +5,12 @@ package com.thealgorithms.maths;
  */
 public class LucasSeries {
 
-    public static void main(String[] args) {
-        assert lucasSeries(1) == 2 && lucasSeriesIteration(1) == 2;
-        assert lucasSeries(2) == 1 && lucasSeriesIteration(2) == 1;
-        assert lucasSeries(3) == 3 && lucasSeriesIteration(3) == 3;
-        assert lucasSeries(4) == 4 && lucasSeriesIteration(4) == 4;
-        assert lucasSeries(5) == 7 && lucasSeriesIteration(5) == 7;
-        assert lucasSeries(6) == 11 && lucasSeriesIteration(6) == 11;
-        assert lucasSeries(11) == 123 && lucasSeriesIteration(11) == 123;
-    }
-
     /**
-     * Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
+     * Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
      * 123, ....) using recursion
      *
      * @param n nth
-     * @return nth number of lucas series
+     * @return nth number of Lucas Series
      */
     public static int lucasSeries(int n) {
         return n == 1
@@ -29,7 +19,7 @@ public class LucasSeries {
     }
 
     /**
-     * Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
+     * Calculate nth number of Lucas Series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76,
      * 123, ....) using iteration
      *
      * @param n nth
diff --git a/src/main/java/com/thealgorithms/maths/Median.java b/src/main/java/com/thealgorithms/maths/Median.java
index 3bc8bae2..44f94ad6 100644
--- a/src/main/java/com/thealgorithms/maths/Median.java
+++ b/src/main/java/com/thealgorithms/maths/Median.java
@@ -7,18 +7,9 @@ import java.util.Arrays;
  */
 public class Median {
 
-    public static void main(String[] args) {
-        assert median(new int[] { 0 }) == 0;
-        assert median(new int[] { 1, 2 }) == 1.5;
-        assert median(new int[] { 4, 1, 3, 2 }) == 2.5;
-        assert median(new int[] { 1, 3, 3, 6, 7, 8, 9 }) == 6;
-        assert median(new int[] { 1, 2, 3, 4, 5, 6, 8, 9 }) == 4.5;
-    }
-
     /**
      * Calculate average median
-     *
-     * @param values number series
+     * @param values sorted numbers to find median of
      * @return median of given {@code values}
      */
     public static double median(int[] values) {


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java b/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java
new file mode 100644
index 00000000..7af452e1
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java
@@ -0,0 +1,28 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class LeonardoNumberTest {
+    @Test
+    void leonardoNumberNegative() {
+        assertThrows(ArithmeticException.class, ()-> LeonardoNumber.leonardoNumber(-1));
+    }
+    @Test
+    void leonardoNumberZero() {
+        assertEquals(1, LeonardoNumber.leonardoNumber(0));
+    }
+    @Test
+    void leonardoNumberOne() {
+        assertEquals(1, LeonardoNumber.leonardoNumber(1));
+    }
+    @Test
+    void leonardoNumberFive() {
+        assertEquals(15, LeonardoNumber.leonardoNumber(5));
+    }
+    @Test
+    void leonardoNumberTwenty() {
+        assertEquals(21891 , LeonardoNumber.leonardoNumber(20));
+    }
+}
diff --git a/src/test/java/com/thealgorithms/maths/LucasSeriesTest.java b/src/test/java/com/thealgorithms/maths/LucasSeriesTest.java
new file mode 100644
index 00000000..e5ac6224
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/LucasSeriesTest.java
@@ -0,0 +1,27 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class LucasSeriesTest {
+    @Test
+    void lucasSeriesTwo() {
+        assertEquals(2, LucasSeries.lucasSeries(1));
+        assertEquals(2, LucasSeries.lucasSeriesIteration(1));
+    }
+    @Test
+    void lucasSeriesOne() {
+        assertEquals(1, LucasSeries.lucasSeries(2));
+        assertEquals(1, LucasSeries.lucasSeriesIteration(2));
+    }
+    @Test
+    void lucasSeriesSeven() {
+        assertEquals(7, LucasSeries.lucasSeries(5));
+        assertEquals(7, LucasSeries.lucasSeriesIteration(5));
+    }
+    @Test
+    void lucasSeriesEleven() {
+        assertEquals(123, LucasSeries.lucasSeries(11));
+        assertEquals(123, LucasSeries.lucasSeriesIteration(11));
+    }
+}
diff --git a/src/test/java/com/thealgorithms/maths/MedianTest.java b/src/test/java/com/thealgorithms/maths/MedianTest.java
new file mode 100644
index 00000000..f3825b7f
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/MedianTest.java
@@ -0,0 +1,36 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class MedianTest {
+    @Test
+    void medianSingleValue() {
+        int[] arr = {0};
+        assertEquals(0, Median.median(arr));
+    }
+
+    @Test
+    void medianTwoValues() {
+        int[] arr = {1, 2};
+        assertEquals(1.5, Median.median(arr));
+    }
+
+    @Test
+    void medianThreeValues() {
+        int[] arr = {1, 2, 3};
+        assertEquals(2, Median.median(arr));
+    }
+
+    @Test
+    void medianDecimalValueReturn() {
+        int[] arr = {1, 2, 3, 4, 5, 6, 8, 9};
+        assertEquals(4.5, Median.median(arr));
+    }
+
+    @Test
+    void medianNegativeValues() {
+        int[] arr = {-27, -16, -7, -4, -2, -1};
+        assertEquals(-5.5, Median.median(arr));
+    }
+}