Skip to content

Commit 06ef351

Browse files
authored
Add tests for SumOfArithmeticSeries (TheAlgorithms#4256)
1 parent e897a93 commit 06ef351

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,8 @@
1010
* <p>
1111
* Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
1212
*/
13-
public class SumOfArithmeticSeries {
14-
15-
public static void main(String[] args) {
16-
/* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 */
17-
assert Double.compare(55.0, sumOfSeries(1, 1, 10)) == 0;
18-
19-
/* 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 */
20-
assert Double.compare(100.0, sumOfSeries(1, 2, 10)) == 0;
21-
22-
/* 1 + 11 + 21 + 31 + 41 + 51 + 61 + 71 + 81 + 91 */
23-
assert Double.compare(460.0, sumOfSeries(1, 10, 10)) == 0;
24-
25-
/* 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 0.6 + 0.7 + 0.8 + 0.9 + 1.0 */
26-
assert Double.compare(5.5, sumOfSeries(0.1, 0.1, 10)) == 0;
27-
28-
assert Double.compare(49600.0, sumOfSeries(1, 10, 100)) == 0;
13+
public final class SumOfArithmeticSeries {
14+
private SumOfArithmeticSeries() {
2915
}
3016

3117
/**
@@ -36,7 +22,10 @@ public static void main(String[] args) {
3622
* @param numOfTerms the total terms of an arithmetic series
3723
* @return sum of given arithmetic series
3824
*/
39-
private static double sumOfSeries(double firstTerm, double commonDiff, int numOfTerms) {
25+
public static double sumOfSeries(final double firstTerm, final double commonDiff, final int numOfTerms) {
26+
if (numOfTerms < 0) {
27+
throw new IllegalArgumentException("numOfTerms nonnegative.");
28+
}
4029
return (numOfTerms / 2.0 * (2 * firstTerm + (numOfTerms - 1) * commonDiff));
4130
}
4231
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class SumOfArithmeticSeriesTest {
9+
@Test
10+
public void testSumFrom1To10() {
11+
assertEquals(55.0, SumOfArithmeticSeries.sumOfSeries(1.0, 1.0, 10));
12+
}
13+
14+
@Test
15+
public void testSumOfOddNumbers1To19() {
16+
assertEquals(100.0, SumOfArithmeticSeries.sumOfSeries(1.0, 2.0, 10));
17+
}
18+
19+
@Test
20+
public void testA() {
21+
assertEquals(460.0, SumOfArithmeticSeries.sumOfSeries(1.0, 10.0, 10));
22+
}
23+
24+
@Test
25+
public void testB() {
26+
assertEquals(5.5, SumOfArithmeticSeries.sumOfSeries(0.1, 0.1, 10));
27+
}
28+
29+
@Test
30+
public void testC() {
31+
assertEquals(49600.0, SumOfArithmeticSeries.sumOfSeries(1, 10, 100));
32+
}
33+
34+
@Test
35+
public void testForZeroTerms() {
36+
assertEquals(0.0, SumOfArithmeticSeries.sumOfSeries(1.0, 100.0, 0), 0.00001);
37+
}
38+
39+
@Test
40+
public void testIfThrowsExceptionForNegativeNumberOfTerms() {
41+
assertThrows(IllegalArgumentException.class, () -> SumOfArithmeticSeries.sumOfSeries(1.0, 1.0, -1));
42+
}
43+
44+
@Test
45+
public void testWithSingleTerm() {
46+
assertEquals(123.0, SumOfArithmeticSeries.sumOfSeries(123.0, 5.0, 1));
47+
}
48+
49+
@Test
50+
public void testWithZeroCommonDiff() {
51+
assertEquals(100.0, SumOfArithmeticSeries.sumOfSeries(1.0, 0.0, 100));
52+
}
53+
}

0 commit comments

Comments
 (0)