Add tests for SumOfArithmeticSeries
(#4256)
This commit is contained in:
parent
e897a93166
commit
06ef351987
@ -10,22 +10,8 @@ package com.thealgorithms.maths;
|
|||||||
* <p>
|
* <p>
|
||||||
* Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
|
* Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
|
||||||
*/
|
*/
|
||||||
public class SumOfArithmeticSeries {
|
public final class SumOfArithmeticSeries {
|
||||||
|
private 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,7 +22,10 @@ public class SumOfArithmeticSeries {
|
|||||||
* @param numOfTerms the total terms of an arithmetic series
|
* @param numOfTerms the total terms of an arithmetic series
|
||||||
* @return sum of given 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));
|
return (numOfTerms / 2.0 * (2 * firstTerm + (numOfTerms - 1) * commonDiff));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user