Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@@ -1,11 +1,11 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class ADTFractionTest {
private final ADTFraction fraction1 = new ADTFraction(3, 5);
@@ -13,7 +13,10 @@ public class ADTFractionTest {
@Test
void testConstructorWithDenominatorEqualToZero() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> new ADTFraction(1, 0));
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> new ADTFraction(1, 0)
);
assertEquals("Denominator cannot be 0", exception.getMessage());
}

View File

@@ -1,10 +1,10 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class AbsoluteMaxTest {
@Test
@@ -15,7 +15,10 @@ public class AbsoluteMaxTest {
@Test
void testGetMaxValueWithNoArguments() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMax.getMaxValue());
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> AbsoluteMax.getMaxValue()
);
assertEquals("Numbers array cannot be empty", exception.getMessage());
}
}

View File

@@ -1,10 +1,10 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class AbsoluteMinTest {
@Test
@@ -15,7 +15,10 @@ public class AbsoluteMinTest {
@Test
void testGetMinValueWithNoArguments() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMin.getMinValue());
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> AbsoluteMin.getMinValue()
);
assertEquals("Numbers array cannot be empty", exception.getMessage());
}
}

View File

@@ -1,18 +1,23 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class AbsoluteValueTest {
@Test
void testGetAbsValue() {
Stream.generate(() -> ThreadLocalRandom.current().nextInt())
.limit(1000)
.forEach(number -> assertEquals(Math.abs(number), AbsoluteValue.getAbsValue(number)));
Stream
.generate(() -> ThreadLocalRandom.current().nextInt())
.limit(1000)
.forEach(number ->
assertEquals(
Math.abs(number),
AbsoluteValue.getAbsValue(number)
)
);
}
}

View File

@@ -1,9 +1,9 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class AliquotSumTest {
@Test

View File

@@ -1,15 +1,15 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class AmicableNumberTest {
@Test
void testAmicableNumber() {
assertThat(AmicableNumber.isAmicableNumber(220, 284)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(1184, 1210)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(2620, 2924)).isTrue();
}
}
package com.thealgorithms.maths;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class AmicableNumberTest {
@Test
void testAmicableNumber() {
assertThat(AmicableNumber.isAmicableNumber(220, 284)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(1184, 1210)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(2620, 2924)).isTrue();
}
}

View File

@@ -1,9 +1,9 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
/**
* @author Vivek
* @since 15/03/22

View File

@@ -1,16 +1,16 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class AutomorphicNumberTest{
@Test
void testAutomorphicNumber(){
assertThat(AutomorphicNumber.isAutomorphic(625)).isTrue();
assertThat(AutomorphicNumber.isAutomorphic(144)).isFalse();
assertThat(AutomorphicNumber.isAutomorphic(9376)).isTrue();
assertThat(AutomorphicNumber.isAutomorphic(169)).isFalse();
}
}
package com.thealgorithms.maths;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class AutomorphicNumberTest {
@Test
void testAutomorphicNumber() {
assertThat(AutomorphicNumber.isAutomorphic(625)).isTrue();
assertThat(AutomorphicNumber.isAutomorphic(144)).isFalse();
assertThat(AutomorphicNumber.isAutomorphic(9376)).isTrue();
assertThat(AutomorphicNumber.isAutomorphic(169)).isFalse();
}
}

View File

@@ -3,12 +3,12 @@ package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class AverageTest {
double [] numbers = {3, 6, 9, 12, 15, 18, 21};
double[] numbers = { 3, 6, 9, 12, 15, 18, 21 };
@Test
public void testAverage() {
Assertions.assertEquals(12, Average.average(numbers));
}
}

View File

@@ -1,16 +1,16 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BinaryPowTest {
@Test
void testBinPow() {
assertEquals(4, BinaryPow.binPow(2, 2));
assertEquals(256, BinaryPow.binPow(4, 4));
assertEquals(729, BinaryPow.binPow(9, 3));
assertEquals(262144, BinaryPow.binPow(8, 6));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BinaryPowTest {
@Test
void testBinPow() {
assertEquals(4, BinaryPow.binPow(2, 2));
assertEquals(256, BinaryPow.binPow(4, 4));
assertEquals(729, BinaryPow.binPow(9, 3));
assertEquals(262144, BinaryPow.binPow(8, 6));
}
}

View File

@@ -1,18 +1,16 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BinomialCoefficientTest {
@Test
void testBinomialCoefficient() {
assertEquals(190, BinomialCoefficient.binomialCoefficient(20, 2));
assertEquals(792, BinomialCoefficient.binomialCoefficient(12, 5));
assertEquals(84, BinomialCoefficient.binomialCoefficient(9, 3));
assertEquals(1, BinomialCoefficient.binomialCoefficient(17, 17));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BinomialCoefficientTest {
@Test
void testBinomialCoefficient() {
assertEquals(190, BinomialCoefficient.binomialCoefficient(20, 2));
assertEquals(792, BinomialCoefficient.binomialCoefficient(12, 5));
assertEquals(84, BinomialCoefficient.binomialCoefficient(9, 3));
assertEquals(1, BinomialCoefficient.binomialCoefficient(17, 17));
}
}

View File

@@ -1,17 +1,17 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CeilTest {
@Test
void testCeil() {
assertEquals(8, Ceil.ceil(7.057));
assertEquals(8, Ceil.ceil(7.004));
assertEquals(-13, Ceil.ceil(-13.004));
assertEquals(1, Ceil.ceil(.98));
assertEquals(-11, Ceil.ceil(-11.357));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CeilTest {
@Test
void testCeil() {
assertEquals(8, Ceil.ceil(7.057));
assertEquals(8, Ceil.ceil(7.004));
assertEquals(-13, Ceil.ceil(-13.004));
assertEquals(1, Ceil.ceil(.98));
assertEquals(-11, Ceil.ceil(-11.357));
}
}

View File

@@ -1,11 +1,10 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class CollatzConjectureTest {
@@ -28,13 +27,34 @@ class CollatzConjectureTest {
@Test
void collatzConjecture() {
final List<Integer> expected = List.of(35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1);
final List<Integer> expected = List.of(
35,
106,
53,
160,
80,
40,
20,
10,
5,
16,
8,
4,
2,
1
);
assertIterableEquals(expected, cConjecture.collatzConjecture(35));
}
@Test
void sequenceOfNotNaturalFirstNumber() {
assertThrows(IllegalArgumentException.class, () -> cConjecture.collatzConjecture(0));
assertThrows(IllegalArgumentException.class, () -> cConjecture.collatzConjecture(-1));
assertThrows(
IllegalArgumentException.class,
() -> cConjecture.collatzConjecture(0)
);
assertThrows(
IllegalArgumentException.class,
() -> cConjecture.collatzConjecture(-1)
);
}
}
}

View File

@@ -1,30 +1,26 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CombinationsTest {
@Test
void testCombination() {
assertEquals(1, Combinations.combinations(1, 1));
assertEquals(252, Combinations.combinations(10, 5));
assertEquals(20, Combinations.combinations(6, 3));
assertEquals(15504, Combinations.combinations(20, 5));
}
@Test
void testCombinationOptimised() {
assertEquals(100, Combinations.combinationsOptimized(100, 1));
assertEquals(1, Combinations.combinationsOptimized(1, 1));
assertEquals(252, Combinations.combinationsOptimized(10, 5));
assertEquals(20, Combinations.combinationsOptimized(6, 3));
assertEquals(15504, Combinations.combinationsOptimized(20, 5));
assertEquals(2535650040L, Combinations.combinationsOptimized(200, 5));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CombinationsTest {
@Test
void testCombination() {
assertEquals(1, Combinations.combinations(1, 1));
assertEquals(252, Combinations.combinations(10, 5));
assertEquals(20, Combinations.combinations(6, 3));
assertEquals(15504, Combinations.combinations(20, 5));
}
@Test
void testCombinationOptimised() {
assertEquals(100, Combinations.combinationsOptimized(100, 1));
assertEquals(1, Combinations.combinationsOptimized(1, 1));
assertEquals(252, Combinations.combinationsOptimized(10, 5));
assertEquals(20, Combinations.combinationsOptimized(6, 3));
assertEquals(15504, Combinations.combinationsOptimized(20, 5));
assertEquals(2535650040L, Combinations.combinationsOptimized(200, 5));
}
}

View File

@@ -1,20 +1,18 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class DigitalRootTest {
@Test
void testDigitalroot() {
assertEquals(4, DigitalRoot.digitalRoot(4));
assertEquals(9, DigitalRoot.digitalRoot(9));
assertEquals(4, DigitalRoot.digitalRoot(49));
assertEquals(6, DigitalRoot.digitalRoot(78));
assertEquals(4, DigitalRoot.digitalRoot(1228));
assertEquals(5, DigitalRoot.digitalRoot(71348));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class DigitalRootTest {
@Test
void testDigitalroot() {
assertEquals(4, DigitalRoot.digitalRoot(4));
assertEquals(9, DigitalRoot.digitalRoot(9));
assertEquals(4, DigitalRoot.digitalRoot(49));
assertEquals(6, DigitalRoot.digitalRoot(78));
assertEquals(4, DigitalRoot.digitalRoot(1228));
assertEquals(5, DigitalRoot.digitalRoot(71348));
}
}

View File

@@ -6,80 +6,105 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DistanceFormulaTest {
@Test
void euclideanTest1() {
Assertions.assertEquals(DistanceFormula.euclideanDistance(1, 1, 2, 2), 1.4142135623730951);
}
@Test
void euclideanTest2() {
Assertions.assertEquals(DistanceFormula.euclideanDistance(1, 3, 8, 0), 7.0710678118654755);
}
@Test
void euclideanTest1() {
Assertions.assertEquals(
DistanceFormula.euclideanDistance(1, 1, 2, 2),
1.4142135623730951
);
}
@Test
void euclideanTest3() {
Assertions.assertEquals(DistanceFormula.euclideanDistance(2.4, 9.1, 55.1, 100), 110.91911467371168);
}
@Test
void euclideanTest2() {
Assertions.assertEquals(
DistanceFormula.euclideanDistance(1, 3, 8, 0),
7.0710678118654755
);
}
@Test
void euclideanTest4() {
Assertions.assertEquals(DistanceFormula.euclideanDistance(1000, 13, 20000, 84), 19022.067605809836);
}
@Test
void euclideanTest3() {
Assertions.assertEquals(
DistanceFormula.euclideanDistance(2.4, 9.1, 55.1, 100),
110.91911467371168
);
}
@Test
public void manhattantest1() {
assertEquals(DistanceFormula.manhattanDistance(1, 2, 3, 4), 4);
}
@Test
void euclideanTest4() {
Assertions.assertEquals(
DistanceFormula.euclideanDistance(1000, 13, 20000, 84),
19022.067605809836
);
}
@Test
public void manhattantest2() {
assertEquals(DistanceFormula.manhattanDistance(6.5, 8.4, 20.1, 13.6), 18.8);
}
@Test
public void manhattantest1() {
assertEquals(DistanceFormula.manhattanDistance(1, 2, 3, 4), 4);
}
@Test
public void manhattanTest3() {
assertEquals(DistanceFormula.manhattanDistance(10.112, 50, 8, 25.67), 26.442);
}
@Test
public void manhattantest2() {
assertEquals(
DistanceFormula.manhattanDistance(6.5, 8.4, 20.1, 13.6),
18.8
);
}
@Test
public void hammingTest1() {
int[] array1 = { 1, 1, 1, 1 };
int[] array2 = { 0, 0, 0, 0 };
assertEquals(DistanceFormula.hammingDistance(array1, array2), 4);
}
@Test
public void manhattanTest3() {
assertEquals(
DistanceFormula.manhattanDistance(10.112, 50, 8, 25.67),
26.442
);
}
@Test
public void hammingTest2() {
int[] array1 = { 1, 1, 1, 1 };
int[] array2 = { 1, 1, 1, 1 };
assertEquals(DistanceFormula.hammingDistance(array1, array2), 0);
}
@Test
public void hammingTest1() {
int[] array1 = { 1, 1, 1, 1 };
int[] array2 = { 0, 0, 0, 0 };
assertEquals(DistanceFormula.hammingDistance(array1, array2), 4);
}
@Test
public void hammingTest3() {
int[] array1 = { 1, 0, 0, 1, 1, 0, 1, 1, 0 };
int[] array2 = { 0, 1, 0, 0, 1, 1, 1, 0, 0 };
assertEquals(DistanceFormula.hammingDistance(array1, array2), 5);
}
@Test
public void hammingTest2() {
int[] array1 = { 1, 1, 1, 1 };
int[] array2 = { 1, 1, 1, 1 };
assertEquals(DistanceFormula.hammingDistance(array1, array2), 0);
}
@Test
public void minkowskiTest1() {
double[] array1 = { 1, 3, 8, 5 };
double[] array2 = { 4, 2, 6, 9 };
assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 1), 10);
}
@Test
public void hammingTest3() {
int[] array1 = { 1, 0, 0, 1, 1, 0, 1, 1, 0 };
int[] array2 = { 0, 1, 0, 0, 1, 1, 1, 0, 0 };
assertEquals(DistanceFormula.hammingDistance(array1, array2), 5);
}
@Test
public void minkowskiTest2() {
double[] array1 = { 1, 3, 8, 5 };
double[] array2 = { 4, 2, 6, 9 };
assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 2), 5.477225575051661);
}
@Test
public void minkowskiTest1() {
double[] array1 = { 1, 3, 8, 5 };
double[] array2 = { 4, 2, 6, 9 };
assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 1), 10);
}
@Test
public void minkowskiTest3() {
double[] array1 = { 1, 3, 8, 5 };
double[] array2 = { 4, 2, 6, 9 };
assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 3), 4.641588833612778);
}
@Test
public void minkowskiTest2() {
double[] array1 = { 1, 3, 8, 5 };
double[] array2 = { 4, 2, 6, 9 };
assertEquals(
DistanceFormula.minkowskiDistance(array1, array2, 2),
5.477225575051661
);
}
@Test
public void minkowskiTest3() {
double[] array1 = { 1, 3, 8, 5 };
double[] array2 = { 4, 2, 6, 9 };
assertEquals(
DistanceFormula.minkowskiDistance(array1, array2, 3),
4.641588833612778
);
}
}

View File

@@ -1,147 +1,137 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
class FFTTest {
// Testing the simple function getReal
@Test
void getRealtest()
{
FFT.Complex complex = new FFT.Complex(1.0,1.0);
assertEquals(1.0,complex.getReal());
void getRealtest() {
FFT.Complex complex = new FFT.Complex(1.0, 1.0);
assertEquals(1.0, complex.getReal());
}
// Testing the simple function getImaginary
@Test
void getImaginaryTest()
{
void getImaginaryTest() {
FFT.Complex complex = new FFT.Complex();
assertEquals(0.0,complex.getImaginary());
assertEquals(0.0, complex.getImaginary());
}
// Testing the function add, assertEqual test
@Test
void addTest()
{
FFT.Complex complex1 = new FFT.Complex(1.0,1.0);
FFT.Complex complex2 = new FFT.Complex(2.0,2.0);
void addTest() {
FFT.Complex complex1 = new FFT.Complex(1.0, 1.0);
FFT.Complex complex2 = new FFT.Complex(2.0, 2.0);
double add = complex1.add(complex2).getReal();
assertEquals(3.0,add);
assertEquals(3.0, add);
}
// Testing the function add, assertNotEqual test
@Test
void addFalseTest()
{
FFT.Complex complex1 = new FFT.Complex(1.0,1.0);
FFT.Complex complex2 = new FFT.Complex(2.0,2.0);
void addFalseTest() {
FFT.Complex complex1 = new FFT.Complex(1.0, 1.0);
FFT.Complex complex2 = new FFT.Complex(2.0, 2.0);
double add = complex1.add(complex2).getReal();
assertNotEquals(2.0,add);
assertNotEquals(2.0, add);
}
// Testing the function substract, assertEqual test
@Test
void subtractTest()
{
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
FFT.Complex complex2 = new FFT.Complex(1.0,1.0);
void subtractTest() {
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
FFT.Complex complex2 = new FFT.Complex(1.0, 1.0);
double sub = complex1.subtract(complex2).getReal();
assertEquals(1.0,sub);
assertEquals(1.0, sub);
}
// Testing the function multiply complex, assertEqual test
@Test
void multiplyWithComplexTest()
{
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
FFT.Complex complex2 = new FFT.Complex(1.0,1.0);
void multiplyWithComplexTest() {
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
FFT.Complex complex2 = new FFT.Complex(1.0, 1.0);
double multiReal = complex1.multiply(complex2).getReal();
double multiImg = complex1.multiply(complex2).getImaginary();
assertEquals(0.0,multiReal);
assertEquals(4.0,multiImg);
assertEquals(0.0, multiReal);
assertEquals(4.0, multiImg);
}
// Testing the function multiply scalar, assertEqual test
@Test
void multiplyWithScalarTest()
{
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
void multiplyWithScalarTest() {
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
double multiReal = complex1.multiply(2).getReal();
double multiImg = complex1.multiply(3).getImaginary();
assertEquals(4.0,multiReal);
assertEquals(6.0,multiImg);
assertEquals(4.0, multiReal);
assertEquals(6.0, multiImg);
}
// Testing the function conjugate, assertEqual test
@Test
void conjugateTest()
{
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
void conjugateTest() {
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
double conReal = complex1.conjugate().getReal();
double conImg = complex1.conjugate().getImaginary();
assertEquals(2.0,conReal);
assertEquals(-2.0,conImg);
assertEquals(2.0, conReal);
assertEquals(-2.0, conImg);
}
// Testing the function abs, assertEqual test
@Test
void abs()
{
FFT.Complex complex1 = new FFT.Complex(2.0,3.0);
void abs() {
FFT.Complex complex1 = new FFT.Complex(2.0, 3.0);
double abs = complex1.abs();
assertEquals(Math.sqrt(13),abs);
assertEquals(Math.sqrt(13), abs);
}
// Testing the function divide complex, assertEqual test.
@Test
void divideWithComplexTest()
{
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
FFT.Complex complex2 = new FFT.Complex(1.0,2.0);
void divideWithComplexTest() {
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
FFT.Complex complex2 = new FFT.Complex(1.0, 2.0);
double divReal = complex1.divide(complex2).getReal();
double divImg = complex1.divide(complex2).getImaginary();
assertEquals(1.2,divReal);
assertEquals(-0.4,divImg);
assertEquals(1.2, divReal);
assertEquals(-0.4, divImg);
}
// Testing the function divide scalar, assertEqual test.
@Test
void divideWithScalarTest()
{
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
void divideWithScalarTest() {
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
double divReal = complex1.divide(2).getReal();
double divImg = complex1.divide(2).getImaginary();
assertEquals(1,divReal);
assertEquals(1,divImg);
assertEquals(1, divReal);
assertEquals(1, divImg);
}
// Testing the function fft, assertEqual test.
// https://scistatcalc.blogspot.com/2013/12/fft-calculator.html used this link to
// ensure the result
@Test
void fft()
{
void fft() {
ArrayList<FFT.Complex> arr = new ArrayList<FFT.Complex>();
FFT.Complex complex1 = new FFT.Complex(2.0,2.0);
FFT.Complex complex2 = new FFT.Complex(1.0,3.0);
FFT.Complex complex3 = new FFT.Complex(3.0,1.0);
FFT.Complex complex4 = new FFT.Complex(2.0,2.0);
FFT.Complex complex1 = new FFT.Complex(2.0, 2.0);
FFT.Complex complex2 = new FFT.Complex(1.0, 3.0);
FFT.Complex complex3 = new FFT.Complex(3.0, 1.0);
FFT.Complex complex4 = new FFT.Complex(2.0, 2.0);
arr.add(complex1);
arr.add(complex2);
arr.add(complex3);
arr.add(complex4);
arr = FFT.fft(arr,false);
double realV1= arr.get(0).getReal();
double realV2= arr.get(2).getReal();
arr = FFT.fft(arr, false);
double realV1 = arr.get(0).getReal();
double realV2 = arr.get(2).getReal();
double imgV1 = arr.get(0).getImaginary();
double imgV2 = arr.get(2).getImaginary();
assertEquals(8.0,realV1);
assertEquals(2.0,realV2);
assertEquals(8.0, realV1);
assertEquals(2.0, realV2);
assertEquals(8.0, imgV1);
assertEquals(-2.0,imgV2);
assertEquals(-2.0, imgV2);
}
}

View File

@@ -1,16 +1,14 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class FactorialTest {
@Test
public void test() {
Factorial fact = new Factorial();
assertEquals(120,fact.factorial(5));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class FactorialTest {
@Test
public void test() {
Factorial fact = new Factorial();
assertEquals(120, fact.factorial(5));
}
}

View File

@@ -1,56 +1,50 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class FastInverseSqrtTests {
@Test
void testForOneElement()
{
assertFalse(FastInverseSqrt.inverseSqrt(1332));
void testForOneElement() {
assertFalse(FastInverseSqrt.inverseSqrt(1332));
// calls for the 2nd inverse method
}
@Test
void testForsecond()
{
assertFalse(FastInverseSqrt.inverseSqrt(1332f));
@Test
void testForsecond() {
assertFalse(FastInverseSqrt.inverseSqrt(1332f));
// calls for the 1st inverse method
}
@Test
void testForThird()
{
assertFalse(FastInverseSqrt.inverseSqrt(1));
}
@Test
void testForFourth()
{
assertFalse(FastInverseSqrt.inverseSqrt(1f));
}
@Test
void testForFifth()
{
assertFalse(FastInverseSqrt.inverseSqrt(4522));
}
@Test
void testForSixth()
{
assertFalse(FastInverseSqrt.inverseSqrt(4522f));
}
@Test
void testForSeventh()
{
assertFalse(FastInverseSqrt.inverseSqrt(21));
}
@Test
void testForEighth()
{
assertFalse(FastInverseSqrt.inverseSqrt(21f));
}
@Test
void testForThird() {
assertFalse(FastInverseSqrt.inverseSqrt(1));
}
@Test
void testForFourth() {
assertFalse(FastInverseSqrt.inverseSqrt(1f));
}
@Test
void testForFifth() {
assertFalse(FastInverseSqrt.inverseSqrt(4522));
}
@Test
void testForSixth() {
assertFalse(FastInverseSqrt.inverseSqrt(4522f));
}
@Test
void testForSeventh() {
assertFalse(FastInverseSqrt.inverseSqrt(21));
}
@Test
void testForEighth() {
assertFalse(FastInverseSqrt.inverseSqrt(21f));
}
}

View File

@@ -1,12 +1,16 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class FindMaxTest {
@Test
public void testFindMaxValue(){
assertEquals(10, FindMax.findMax(new int[] {1,2,3,4,5,6,7,8,9,10}));
public void testFindMaxValue() {
assertEquals(
10,
FindMax.findMax(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })
);
}
}

View File

@@ -1,21 +1,26 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class FindMinTest {
@Test
public void testFindMinValue(){
assertEquals(1, FindMin.findMin(new int[] {1,2,3,4,5,6,7,8,9,10}));
}
import org.junit.jupiter.api.Test;
@Test
public void test1(){
assertEquals(1, FindMin.findMin(new int[] {1, 3, 5, 7, 9}));
}
@Test
public void test2(){
assertEquals(0, FindMin.findMin(new int[] {0, 192, 384, 576}));
}
public class FindMinTest {
@Test
public void testFindMinValue() {
assertEquals(
1,
FindMin.findMin(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })
);
}
@Test
public void test1() {
assertEquals(1, FindMin.findMin(new int[] { 1, 3, 5, 7, 9 }));
}
@Test
public void test2() {
assertEquals(0, FindMin.findMin(new int[] { 0, 192, 384, 576 }));
}
}

View File

@@ -4,19 +4,29 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class GCDTest {
@Test
void test1() {
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-1,0));
Assertions.assertThrows(
ArithmeticException.class,
() -> GCD.gcd(-1, 0)
);
}
@Test
void test2() {
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(10, -2));
Assertions.assertThrows(
ArithmeticException.class,
() -> GCD.gcd(10, -2)
);
}
@Test
void test3() {
Assertions.assertThrows(ArithmeticException.class, () -> GCD.gcd(-5, -3));
Assertions.assertThrows(
ArithmeticException.class,
() -> GCD.gcd(-5, -3)
);
}
@Test

View File

@@ -1,17 +1,16 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static com.thealgorithms.maths.Gaussian.gaussian;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
public class GaussianTest {
// easy pass test for the whole class. Matrix of 2*3.
@Test
void passTest1()
{
void passTest1() {
ArrayList<Double> list = new ArrayList<Double>();
ArrayList<Double> gaussian = new ArrayList<Double>();
ArrayList<Double> answer = new ArrayList<Double>();
@@ -25,8 +24,8 @@ public class GaussianTest {
list.add(2.0);
list.add(1.0);
list.add(1.0);
gaussian=gaussian(matrixSize,list);
gaussian = gaussian(matrixSize, list);
assertEquals(answer,gaussian);
assertEquals(answer, gaussian);
}
}
}

View File

@@ -4,24 +4,30 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class HeronsFormulaTest {
@Test
void test1()
{
Assertions.assertEquals(HeronsFormula.Herons(3,4,5), 6.0);
}
@Test
void test2()
{
Assertions.assertEquals(HeronsFormula.Herons(24,30,18), 216.0);
}
@Test
void test3()
{
Assertions.assertEquals(HeronsFormula.Herons(1,1,1), 0.4330127018922193);
}
@Test
void test4()
{
Assertions.assertEquals(HeronsFormula.Herons(4,5,8), 8.181534085976786);
}
@Test
void test1() {
Assertions.assertEquals(HeronsFormula.Herons(3, 4, 5), 6.0);
}
@Test
void test2() {
Assertions.assertEquals(HeronsFormula.Herons(24, 30, 18), 216.0);
}
@Test
void test3() {
Assertions.assertEquals(
HeronsFormula.Herons(1, 1, 1),
0.4330127018922193
);
}
@Test
void test4() {
Assertions.assertEquals(
HeronsFormula.Herons(4, 5, 8),
8.181534085976786
);
}
}

View File

@@ -1,15 +1,14 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class JosephusProblemTest {
@Test
void testJosephusProblem(){
assertEquals(3, JosephusProblem.findTheWinner(5,2));
assertEquals(5, JosephusProblem.findTheWinner(6,4));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class JosephusProblemTest {
@Test
void testJosephusProblem() {
assertEquals(3, JosephusProblem.findTheWinner(5, 2));
assertEquals(5, JosephusProblem.findTheWinner(6, 4));
}
}

View File

@@ -1,70 +1,87 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
public class KaprekarNumbersTest {
@Test
void testFor1()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(1));
}
@Test
void testFor1() {
assertTrue(KaprekarNumbers.isKaprekarNumber(1));
}
@Test
void testFor45()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(45));
}
@Test
void testFor45() {
assertTrue(KaprekarNumbers.isKaprekarNumber(45));
}
@Test
void testFor297()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(297));
}
@Test
void testFor297() {
assertTrue(KaprekarNumbers.isKaprekarNumber(297));
}
@Test
void testFor2223()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(2223));
}
@Test
void testFor2223() {
assertTrue(KaprekarNumbers.isKaprekarNumber(2223));
}
@Test
void testFor857143()
{
assertTrue(KaprekarNumbers.isKaprekarNumber(857143));
}
@Test
void testFor857143() {
assertTrue(KaprekarNumbers.isKaprekarNumber(857143));
}
@Test
void testFor3() {
assertFalse(KaprekarNumbers.isKaprekarNumber(3));
}
@Test
void testFor3()
{
assertFalse(KaprekarNumbers.isKaprekarNumber(3));
}
@Test
void testFor26() {
assertFalse(KaprekarNumbers.isKaprekarNumber(26));
}
@Test
void testFor26()
{
assertFalse(KaprekarNumbers.isKaprekarNumber(26));
}
@Test
void testFor98() {
assertFalse(KaprekarNumbers.isKaprekarNumber(98));
}
@Test
void testFor98()
{
assertFalse(KaprekarNumbers.isKaprekarNumber(98));
}
@Test
void testForRangeOfNumber() { try {
ArrayList<Long> rangedNumbers = KaprekarNumbers.kaprekarNumberInRange(1,100000);
long[] allTheNumbers = {1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4950, 5050, 7272, 7777, 9999, 17344, 22222, 77778, 82656, 95121, 99999};
for (long i:allTheNumbers) {
assert rangedNumbers.contains(i);
}
} catch (Exception e) {
assert false;
}
}
}
@Test
void testForRangeOfNumber() {
try {
ArrayList<Long> rangedNumbers = KaprekarNumbers.kaprekarNumberInRange(
1,
100000
);
long[] allTheNumbers = {
1,
9,
45,
55,
99,
297,
703,
999,
2223,
2728,
4950,
5050,
7272,
7777,
9999,
17344,
22222,
77778,
82656,
95121,
99999,
};
for (long i : allTheNumbers) {
assert rangedNumbers.contains(i);
}
} catch (Exception e) {
assert false;
}
}
}

View File

@@ -4,24 +4,24 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class LeastCommonMultipleTest {
/*
* Test for first number greater than second number
*/
@Test
public void testForFirst() {
int result = LeastCommonMultiple.lcm(6,8);
int expected = 24;
Assertions.assertEquals(result, expected);
}
/*
* Test for second number greater than first number
*/
@Test
public void testForSecond() {
int result = LeastCommonMultiple.lcm(8,6);
int expected = 24;
Assertions.assertEquals(result, expected);
}
* Test for first number greater than second number
*/
@Test
public void testForFirst() {
int result = LeastCommonMultiple.lcm(6, 8);
int expected = 24;
Assertions.assertEquals(result, expected);
}
/*
* Test for second number greater than first number
*/
@Test
public void testForSecond() {
int result = LeastCommonMultiple.lcm(8, 6);
int expected = 24;
Assertions.assertEquals(result, expected);
}
}

View File

@@ -6,61 +6,67 @@ import org.junit.jupiter.api.Test;
class LiouvilleLambdaFunctionTest {
@Test
void testLiouvilleLambdaMustThrowExceptionIfNumberIsZero() {
//given
int number = 0;
String expectedMessage = "Number must be greater than zero.";
@Test
void testLiouvilleLambdaMustThrowExceptionIfNumberIsZero() {
//given
int number = 0;
String expectedMessage = "Number must be greater than zero.";
//when
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
LiouvilleLambdaFunction.liouvilleLambda(number);
});
String actualMessage = exception.getMessage();
//when
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> {
LiouvilleLambdaFunction.liouvilleLambda(number);
}
);
String actualMessage = exception.getMessage();
//then
assertEquals(expectedMessage, actualMessage);
}
//then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testLiouvilleLambdaMustThrowExceptionIfNumberIsNegative() {
//given
int number = -1;
String expectedMessage = "Number must be greater than zero.";
@Test
void testLiouvilleLambdaMustThrowExceptionIfNumberIsNegative() {
//given
int number = -1;
String expectedMessage = "Number must be greater than zero.";
//when
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
LiouvilleLambdaFunction.liouvilleLambda(number);
});
String actualMessage = exception.getMessage();
//when
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> {
LiouvilleLambdaFunction.liouvilleLambda(number);
}
);
String actualMessage = exception.getMessage();
//then
assertEquals(expectedMessage, actualMessage);
}
//then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testLiouvilleLambdaMustReturnNegativeOne() {
//given
int number = 11;
int expectedOutput = -1;
@Test
void testLiouvilleLambdaMustReturnNegativeOne() {
//given
int number = 11;
int expectedOutput = -1;
//when
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
//when
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
//then
assertEquals(expectedOutput, actualOutput);
}
//then
assertEquals(expectedOutput, actualOutput);
}
@Test
void testLiouvilleLambdaMustReturnPositiveOne() {
//given
int number = 10;
int expectedOutput = 1;
@Test
void testLiouvilleLambdaMustReturnPositiveOne() {
//given
int number = 10;
int expectedOutput = 1;
//when
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
//when
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
//then
assertEquals(expectedOutput, actualOutput);
}
//then
assertEquals(expectedOutput, actualOutput);
}
}

View File

@@ -6,58 +6,158 @@ import org.junit.jupiter.api.Test;
class MobiusFunctionTest {
@Test
void testMobiusForZero() {
//given
int number = 0;
String expectedMessage = "Number must be greater than zero.";
@Test
void testMobiusForZero() {
//given
int number = 0;
String expectedMessage = "Number must be greater than zero.";
//when
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
MobiusFunction.mobius(number);
});
String actualMessage = exception.getMessage();
//when
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> {
MobiusFunction.mobius(number);
}
);
String actualMessage = exception.getMessage();
//then
assertEquals(expectedMessage, actualMessage);
}
//then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testMobiusForNegativeNumber() {
//given
int number = -1;
String expectedMessage = "Number must be greater than zero.";
@Test
void testMobiusForNegativeNumber() {
//given
int number = -1;
String expectedMessage = "Number must be greater than zero.";
//when
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
MobiusFunction.mobius(number);
});
String actualMessage = exception.getMessage();
//when
Exception exception = assertThrows(
IllegalArgumentException.class,
() -> {
MobiusFunction.mobius(number);
}
);
String actualMessage = exception.getMessage();
//then
assertEquals(expectedMessage, actualMessage);
}
//then
assertEquals(expectedMessage, actualMessage);
}
@Test
void testMobiusFunction(){
int[] expectedResultArray = {
1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0,
0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0,
0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1, -1, 0, -1, 1,
0, 0, 1, -1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 0, -1, 0, 1, 0, 1, 1, 1, 0, -1, 0, 0, 0
};
@Test
void testMobiusFunction() {
int[] expectedResultArray = {
1,
-1,
-1,
0,
-1,
1,
-1,
0,
0,
1,
-1,
0,
-1,
1,
1,
0,
-1,
0,
-1,
0,
1,
1,
-1,
0,
0,
1,
0,
0,
-1,
-1,
-1,
0,
1,
1,
1,
0,
-1,
1,
1,
0,
-1,
-1,
-1,
0,
0,
1,
-1,
0,
0,
0,
1,
0,
-1,
0,
1,
0,
1,
1,
-1,
0,
-1,
1,
0,
0,
1,
-1,
-1,
0,
1,
-1,
-1,
0,
-1,
1,
0,
0,
1,
-1,
-1,
0,
0,
1,
-1,
0,
1,
1,
1,
0,
-1,
0,
1,
0,
1,
1,
1,
0,
-1,
0,
0,
0,
};
for(int i = 1; i <= 100; i++) {
for (int i = 1; i <= 100; i++) {
//given
int expectedValue = expectedResultArray[i - 1];
//given
int expectedValue = expectedResultArray[i-1];
//when
int actualValue = MobiusFunction.mobius(i);
//then
assertEquals(expectedValue,actualValue);
}
}
//when
int actualValue = MobiusFunction.mobius(i);
//then
assertEquals(expectedValue, actualValue);
}
}
}

View File

@@ -1,33 +1,34 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class PascalTriangleTest {
@Test
void testForOne() {
int[][] result = PascalTriangle.pascal(1);
int[][] expected = {{1}};
int[][] expected = { { 1 } };
assertArrayEquals(result, expected);
}
@Test
void testForTwo() {
int[][] result = PascalTriangle.pascal(2);
int[][] expected = {{1, 0}, {1, 1}};
int[][] expected = { { 1, 0 }, { 1, 1 } };
assertArrayEquals(result, expected);
}
@Test
void testForFive() {
int[][] result = PascalTriangle.pascal(5);
int[][] expected = {{1, 0, 0, 0, 0},
{1, 1, 0, 0, 0},
{1, 2, 1, 0, 0},
{1, 3, 3, 1, 0},
{1, 4, 6, 4, 1}
int[][] expected = {
{ 1, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0 },
{ 1, 2, 1, 0, 0 },
{ 1, 3, 3, 1, 0 },
{ 1, 4, 6, 4, 1 },
};
assertArrayEquals(result, expected);
}
@@ -35,14 +36,15 @@ class PascalTriangleTest {
@Test
void testForEight() {
int[][] result = PascalTriangle.pascal(8);
int[][] expected = {{1, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0},
{1, 2, 1, 0, 0, 0, 0, 0},
{1, 3, 3, 1, 0, 0, 0, 0},
{1, 4, 6, 4, 1, 0, 0, 0},
{1, 5, 10, 10, 5, 1, 0, 0},
{1, 6, 15, 20, 15, 6, 1, 0},
{1, 7, 21, 35, 35, 21, 7, 1}
int[][] expected = {
{ 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 2, 1, 0, 0, 0, 0, 0 },
{ 1, 3, 3, 1, 0, 0, 0, 0 },
{ 1, 4, 6, 4, 1, 0, 0, 0 },
{ 1, 5, 10, 10, 5, 1, 0, 0 },
{ 1, 6, 15, 20, 15, 6, 1, 0 },
{ 1, 7, 21, 35, 35, 21, 7, 1 },
};
assertArrayEquals(expected, result);
}

View File

@@ -1,37 +1,38 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PerfectSquareTest{
@Test
public void TestPerfectSquareifiscorrect(){
//Valid Partition
int number = 9;
boolean result = PerfectSquare.isPerfectSquare(number);
assertTrue(result);
}
@Test
public void TestPerfectSquareifisnotcorrect(){
//Invalid Partition 1
int number = 3;
boolean result = PerfectSquare.isPerfectSquare(number);
assertFalse(result);
}
@Test
public void TestPerfectSquareifisNegativeNumber(){
//Invalid Partition 2
int number = -10;
public class PerfectSquareTest {
boolean result = PerfectSquare.isPerfectSquare(number);
@Test
public void TestPerfectSquareifiscorrect() {
//Valid Partition
int number = 9;
assertFalse(result);
}
boolean result = PerfectSquare.isPerfectSquare(number);
assertTrue(result);
}
@Test
public void TestPerfectSquareifisnotcorrect() {
//Invalid Partition 1
int number = 3;
boolean result = PerfectSquare.isPerfectSquare(number);
assertFalse(result);
}
@Test
public void TestPerfectSquareifisNegativeNumber() {
//Invalid Partition 2
int number = -10;
boolean result = PerfectSquare.isPerfectSquare(number);
assertFalse(result);
}
}

View File

@@ -7,45 +7,48 @@ import org.junit.jupiter.api.Test;
class PollardRhoTest {
@Test
void testPollardRhoForNumber315MustReturn5() {
//given
int number = 315;
int expectedResult = 5;
@Test
void testPollardRhoForNumber315MustReturn5() {
//given
int number = 315;
int expectedResult = 5;
//when
int actualResult = PollardRho.pollardRho(number);
//when
int actualResult = PollardRho.pollardRho(number);
//then
assertEquals(expectedResult, actualResult);
}
//then
assertEquals(expectedResult, actualResult);
}
@Test
void testPollardRhoForNumber187MustReturn11() {
//given
int number = 187;
int expectedResult = 11;
@Test
void testPollardRhoForNumber187MustReturn11() {
//given
int number = 187;
int expectedResult = 11;
//when
int actualResult = PollardRho.pollardRho(number);
//when
int actualResult = PollardRho.pollardRho(number);
//then
assertEquals(expectedResult, actualResult);
}
//then
assertEquals(expectedResult, actualResult);
}
@Test
void testPollardRhoForNumber239MustThrowException() {
//given
int number = 239;
String expectedMessage = "GCD cannot be found.";
@Test
void testPollardRhoForNumber239MustThrowException() {
//given
int number = 239;
String expectedMessage = "GCD cannot be found.";
//when
Exception exception = assertThrows(RuntimeException.class, () -> {
PollardRho.pollardRho(number);
});
String actualMessage = exception.getMessage();
//when
Exception exception = assertThrows(
RuntimeException.class,
() -> {
PollardRho.pollardRho(number);
}
);
String actualMessage = exception.getMessage();
//then
assertEquals(expectedMessage, actualMessage);
}
//then
assertEquals(expectedMessage, actualMessage);
}
}

View File

@@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class PrimeCheckTest {
@Test
void test1() {
Assertions.assertTrue(PrimeCheck.isPrime(2));

View File

@@ -3,34 +3,33 @@ package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.Test;
class PrimeFactorizationTest {
@Test
void testpFactorsMustReturnEmptyList() {
//given
int n = 0;
//then
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
}
@Test
void testpFactorsMustReturnNonEmptyList() {
//given
int n = 198;
int expectedListSize = 4;
//when
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
//then
assertEquals(expectedListSize, actualResultList.size());
assertEquals(2, actualResultList.get(0));
assertEquals(3, actualResultList.get(1));
assertEquals(3, actualResultList.get(2));
assertEquals(11, actualResultList.get(3));
}
@Test
void testpFactorsMustReturnEmptyList() {
//given
int n = 0;
//then
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
}
@Test
void testpFactorsMustReturnNonEmptyList() {
//given
int n = 198;
int expectedListSize = 4;
//when
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
//then
assertEquals(expectedListSize, actualResultList.size());
assertEquals(2, actualResultList.get(0));
assertEquals(3, actualResultList.get(1));
assertEquals(3, actualResultList.get(2));
assertEquals(11, actualResultList.get(3));
}
}

View File

@@ -1,34 +1,32 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PronicNumberTest {
@Test
void testForPronicNumber() {
//given
int number = 30;
//when
boolean result = PronicNumber.isPronic(number);
//then
assertTrue(result);
}
@Test
void testForNonPronicNumber() {
//given
int number = 21;
//when
boolean result = PronicNumber.isPronic(number);
//then
assertFalse(result);
}
}
}

View File

@@ -1,20 +1,21 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class PythagoreanTripleTest {
@Test
public void Testpythagoreantriple(){
assertEquals(true, PythagoreanTriple.isPythagTriple(3,4,5));
assertEquals(true, PythagoreanTriple.isPythagTriple(6,8,10));
assertEquals(true, PythagoreanTriple.isPythagTriple(9,12,15));
assertEquals(true, PythagoreanTriple.isPythagTriple(12,16,20));
assertEquals(true, PythagoreanTriple.isPythagTriple(15,20,25));
assertEquals(true, PythagoreanTriple.isPythagTriple(18,24,30));
assertEquals(false, PythagoreanTriple.isPythagTriple(5,20,30));
assertEquals(false, PythagoreanTriple.isPythagTriple(6,8,100));
assertEquals(false, PythagoreanTriple.isPythagTriple(-2,-2,2));
public void Testpythagoreantriple() {
assertEquals(true, PythagoreanTriple.isPythagTriple(3, 4, 5));
assertEquals(true, PythagoreanTriple.isPythagTriple(6, 8, 10));
assertEquals(true, PythagoreanTriple.isPythagTriple(9, 12, 15));
assertEquals(true, PythagoreanTriple.isPythagTriple(12, 16, 20));
assertEquals(true, PythagoreanTriple.isPythagTriple(15, 20, 25));
assertEquals(true, PythagoreanTriple.isPythagTriple(18, 24, 30));
assertEquals(false, PythagoreanTriple.isPythagTriple(5, 20, 30));
assertEquals(false, PythagoreanTriple.isPythagTriple(6, 8, 100));
assertEquals(false, PythagoreanTriple.isPythagTriple(-2, -2, 2));
}
}

View File

@@ -1,23 +1,31 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SquareRootWithNewtonRaphsonTestMethod
{
public class SquareRootWithNewtonRaphsonTestMethod {
@Test
void testfor1(){
Assertions.assertEquals(1,SquareRootWithNewtonRaphsonMethod.squareRoot(1));
void testfor1() {
Assertions.assertEquals(
1,
SquareRootWithNewtonRaphsonMethod.squareRoot(1)
);
}
@Test
void testfor2(){
Assertions.assertEquals(1.414213562373095,SquareRootWithNewtonRaphsonMethod.squareRoot(2));
void testfor2() {
Assertions.assertEquals(
1.414213562373095,
SquareRootWithNewtonRaphsonMethod.squareRoot(2)
);
}
@Test
void testfor625(){
Assertions.assertEquals(25.0,SquareRootWithNewtonRaphsonMethod.squareRoot(625));
void testfor625() {
Assertions.assertEquals(
25.0,
SquareRootWithNewtonRaphsonMethod.squareRoot(625)
);
}
}

View File

@@ -4,23 +4,36 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class SquareRootwithBabylonianMethodTest {
@Test
void testfor4(){
Assertions.assertEquals(2,SquareRootWithBabylonianMethod.square_Root(4));
void testfor4() {
Assertions.assertEquals(
2,
SquareRootWithBabylonianMethod.square_Root(4)
);
}
@Test
void testfor1(){
Assertions.assertEquals(1,SquareRootWithBabylonianMethod.square_Root(1));
void testfor1() {
Assertions.assertEquals(
1,
SquareRootWithBabylonianMethod.square_Root(1)
);
}
@Test
void testfor2(){
Assertions.assertEquals(1.4142135381698608,SquareRootWithBabylonianMethod.square_Root(2));
void testfor2() {
Assertions.assertEquals(
1.4142135381698608,
SquareRootWithBabylonianMethod.square_Root(2)
);
}
@Test
void testfor625(){
Assertions.assertEquals(25,SquareRootWithBabylonianMethod.square_Root(625));
void testfor625() {
Assertions.assertEquals(
25,
SquareRootWithBabylonianMethod.square_Root(625)
);
}
}

View File

@@ -3,30 +3,44 @@ package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class StandardDeviationTest{
@Test
void test1()
{
double[] t1 = new double[]{1,1,1,1,1};
Assertions.assertEquals(StandardDeviation.stdDev(t1), 0.0);
}
@Test
void test2()
{
double[] t2 = new double[]{1,2,3,4,5,6,7,8,9,10};
Assertions.assertEquals(StandardDeviation.stdDev(t2), 2.8722813232690143);
}
@Test
void test3()
{
double[] t3= new double[]{1.1, 8.5, 20.3, 2.4, 6.2};
Assertions.assertEquals(StandardDeviation.stdDev(t3), 6.8308125431752265);
}
@Test
void test4()
{
double[] t4 = new double[]{3.14, 2.22222, 9.89898989, 100.00045, 56.7};
Assertions.assertEquals(StandardDeviation.stdDev(t4), 38.506117353865775);
}
public class StandardDeviationTest {
@Test
void test1() {
double[] t1 = new double[] { 1, 1, 1, 1, 1 };
Assertions.assertEquals(StandardDeviation.stdDev(t1), 0.0);
}
@Test
void test2() {
double[] t2 = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Assertions.assertEquals(
StandardDeviation.stdDev(t2),
2.8722813232690143
);
}
@Test
void test3() {
double[] t3 = new double[] { 1.1, 8.5, 20.3, 2.4, 6.2 };
Assertions.assertEquals(
StandardDeviation.stdDev(t3),
6.8308125431752265
);
}
@Test
void test4() {
double[] t4 = new double[] {
3.14,
2.22222,
9.89898989,
100.00045,
56.7,
};
Assertions.assertEquals(
StandardDeviation.stdDev(t4),
38.506117353865775
);
}
}

View File

@@ -3,25 +3,28 @@ package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class StandardScoreTest{
@Test
void test1()
{
Assertions.assertEquals(StandardScore.zScore(2, 0, 5), 0.4);
}
@Test
void test2()
{
Assertions.assertEquals(StandardScore.zScore(1, 1, 1), 0.0);
}
@Test
void test3()
{
Assertions.assertEquals(StandardScore.zScore(2.5, 1.8, 0.7), 1.0);
}
@Test
void test4()
{
Assertions.assertEquals(StandardScore.zScore(8.9, 3, 4.2), 1.4047619047619049);
}
public class StandardScoreTest {
@Test
void test1() {
Assertions.assertEquals(StandardScore.zScore(2, 0, 5), 0.4);
}
@Test
void test2() {
Assertions.assertEquals(StandardScore.zScore(1, 1, 1), 0.0);
}
@Test
void test3() {
Assertions.assertEquals(StandardScore.zScore(2.5, 1.8, 0.7), 1.0);
}
@Test
void test4() {
Assertions.assertEquals(
StandardScore.zScore(8.9, 3, 4.2),
1.4047619047619049
);
}
}

View File

@@ -1,31 +1,31 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SumOfDigitsTest {
SumOfDigits SoD = new SumOfDigits();
@Test
void testZero() {
assertEquals(0, SoD.sumOfDigits(0));
assertEquals(0, SoD.sumOfDigitsRecursion(0));
assertEquals(0, SoD.sumOfDigitsFast(0));
}
@Test
void testPositive() {
assertEquals(15, SoD.sumOfDigits(12345));
assertEquals(15, SoD.sumOfDigitsRecursion(12345));
assertEquals(15, SoD.sumOfDigitsFast(12345));
}
@Test
void testNegative() {
assertEquals(6, SoD.sumOfDigits(-123));
assertEquals(6, SoD.sumOfDigitsRecursion(-123));
assertEquals(6, SoD.sumOfDigitsFast(-123));
}
}
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class SumOfDigitsTest {
SumOfDigits SoD = new SumOfDigits();
@Test
void testZero() {
assertEquals(0, SoD.sumOfDigits(0));
assertEquals(0, SoD.sumOfDigitsRecursion(0));
assertEquals(0, SoD.sumOfDigitsFast(0));
}
@Test
void testPositive() {
assertEquals(15, SoD.sumOfDigits(12345));
assertEquals(15, SoD.sumOfDigitsRecursion(12345));
assertEquals(15, SoD.sumOfDigitsFast(12345));
}
@Test
void testNegative() {
assertEquals(6, SoD.sumOfDigits(-123));
assertEquals(6, SoD.sumOfDigitsRecursion(-123));
assertEquals(6, SoD.sumOfDigitsFast(-123));
}
}

View File

@@ -1,9 +1,9 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class TestArmstrong {
@Test

View File

@@ -7,31 +7,34 @@ public class perimeterTest {
//Perimeter of polygon
@Test
void testcase1(){
Assertions.assertEquals(20.0,Perimeter.perimeter_polygon(4,5));
void testcase1() {
Assertions.assertEquals(20.0, Perimeter.perimeter_polygon(4, 5));
}
@Test
void testcase2(){
Assertions.assertEquals(30.0,Perimeter.perimeter_polygon(5,6));
void testcase2() {
Assertions.assertEquals(30.0, Perimeter.perimeter_polygon(5, 6));
}
//Perimeter of Rectangle
@Test
void testcase3(){
Assertions.assertEquals(18.0,Perimeter.perimeter_rectangle(4,5));
void testcase3() {
Assertions.assertEquals(18.0, Perimeter.perimeter_rectangle(4, 5));
}
@Test
void testcase4(){
Assertions.assertEquals(14.0,Perimeter.perimeter_rectangle(4,3));
void testcase4() {
Assertions.assertEquals(14.0, Perimeter.perimeter_rectangle(4, 3));
}
//Circumference of a circle
@Test
void testcase5(){
Assertions.assertEquals(31.41592653589793,Perimeter.circumference(5));
void testcase5() {
Assertions.assertEquals(31.41592653589793, Perimeter.circumference(5));
}
@Test
void testcase6(){
Assertions.assertEquals(43.982297150257104,Perimeter.circumference(7));
void testcase6() {
Assertions.assertEquals(43.982297150257104, Perimeter.circumference(7));
}
}