Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add tests for SumOfArithmeticSeries #4256

Merged
merged 7 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,8 @@
* <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() {
}

/**
Expand All @@ -36,7 +22,10 @@ public static void main(String[] args) {
* @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));
}
}
Original file line number Diff line number Diff line change
@@ -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