Repository: TheAlgorithms/Java
Pull request #4256 (2023-07-24 13:23:31)

tests: add tests for `SumOfArithmeticSeries`
This PR adds test and _lints_ the class [`SumOfArithmeticSeries`](https://github.com/TheAlgorithms/Java/blob/e897a93166b7883f4abebf96b32f763e3f29f07c/src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java#L13).

<!-- 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/SumOfArithmeticSeries.java b/src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java
index 69be4c3d..315f0d3a 100644
--- a/src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java
+++ b/src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java
@@ -10,22 +10,8 @@ package com.thealgorithms.maths;
  * <p>
  * Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
  */
-public class SumOfArithmeticSeries {
-
-    public static void main(String[] args) {
-        /* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 */
-        assert Double.compare(55.0, sumOfSeries(1, 1, 10)) == 0;
-
-        /* 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 */
-        assert Double.compare(100.0, sumOfSeries(1, 2, 10)) == 0;
-
-        /* 1 + 11 + 21 + 31 + 41 + 51 + 61 + 71 + 81 + 91 */
-        assert Double.compare(460.0, sumOfSeries(1, 10, 10)) == 0;
-
-        /* 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 0.6 + 0.7 + 0.8 + 0.9 + 1.0 */
-        assert Double.compare(5.5, sumOfSeries(0.1, 0.1, 10)) == 0;
-
-        assert Double.compare(49600.0, sumOfSeries(1, 10, 100)) == 0;
+public final class SumOfArithmeticSeries {
+    private SumOfArithmeticSeries() {
     }
 
     /**
@@ -36,7 +22,10 @@ public class SumOfArithmeticSeries {
      * @param numOfTerms the total terms of an arithmetic series
      * @return sum of given arithmetic series
      */
-    private static double sumOfSeries(double firstTerm, double commonDiff, int numOfTerms) {
+    public static double sumOfSeries(final double firstTerm, final double commonDiff, final int numOfTerms) {
+        if (numOfTerms < 0) {
+            throw new IllegalArgumentException("numOfTerms nonnegative.");
+        }
         return (numOfTerms / 2.0 * (2 * firstTerm + (numOfTerms - 1) * commonDiff));
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/SumOfArithmeticSeriesTest.java b/src/test/java/com/thealgorithms/maths/SumOfArithmeticSeriesTest.java
new file mode 100644
index 00000000..527cef2c
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/SumOfArithmeticSeriesTest.java
@@ -0,0 +1,53 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+public class SumOfArithmeticSeriesTest {
+    @Test
+    public void testSumFrom1To10() {
+        assertEquals(55.0, SumOfArithmeticSeries.sumOfSeries(1.0, 1.0, 10));
+    }
+
+    @Test
+    public void testSumOfOddNumbers1To19() {
+        assertEquals(100.0, SumOfArithmeticSeries.sumOfSeries(1.0, 2.0, 10));
+    }
+
+    @Test
+    public void testA() {
+        assertEquals(460.0, SumOfArithmeticSeries.sumOfSeries(1.0, 10.0, 10));
+    }
+
+    @Test
+    public void testB() {
+        assertEquals(5.5, SumOfArithmeticSeries.sumOfSeries(0.1, 0.1, 10));
+    }
+
+    @Test
+    public void testC() {
+        assertEquals(49600.0, SumOfArithmeticSeries.sumOfSeries(1, 10, 100));
+    }
+
+    @Test
+    public void testForZeroTerms() {
+        assertEquals(0.0, SumOfArithmeticSeries.sumOfSeries(1.0, 100.0, 0), 0.00001);
+    }
+
+    @Test
+    public void testIfThrowsExceptionForNegativeNumberOfTerms() {
+        assertThrows(IllegalArgumentException.class, () -> SumOfArithmeticSeries.sumOfSeries(1.0, 1.0, -1));
+    }
+
+    @Test
+    public void testWithSingleTerm() {
+        assertEquals(123.0, SumOfArithmeticSeries.sumOfSeries(123.0, 5.0, 1));
+    }
+
+    @Test
+    public void testWithZeroCommonDiff() {
+        assertEquals(100.0, SumOfArithmeticSeries.sumOfSeries(1.0, 0.0, 100));
+    }
+}