From 06ef351987706c6fba2786c8024545376b42485a Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Mon, 24 Jul 2023 18:25:40 +0200 Subject: [PATCH] Add tests for `SumOfArithmeticSeries` (#4256) --- .../maths/SumOfArithmeticSeries.java | 23 +++----- .../maths/SumOfArithmeticSeriesTest.java | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/SumOfArithmeticSeriesTest.java 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; *

* 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)); } } 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)); + } +}