mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 23:15:17 +08:00
@ -13,10 +13,8 @@ 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());
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,8 @@ 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());
|
||||
}
|
||||
}
|
||||
|
@ -10,14 +10,8 @@ public class AbsoluteValueTest {
|
||||
|
||||
@Test
|
||||
void testGetAbsValue() {
|
||||
Stream
|
||||
.generate(() -> ThreadLocalRandom.current().nextInt())
|
||||
Stream.generate(() -> ThreadLocalRandom.current().nextInt())
|
||||
.limit(1000)
|
||||
.forEach(number ->
|
||||
assertEquals(
|
||||
Math.abs(number),
|
||||
AbsoluteValue.getAbsValue(number)
|
||||
)
|
||||
);
|
||||
.forEach(number -> assertEquals(Math.abs(number), AbsoluteValue.getAbsValue(number)));
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Amarildo Aliaj
|
||||
*/
|
||||
@ -67,24 +67,52 @@ class AreaTest {
|
||||
@Test
|
||||
void testAllIllegalInput() {
|
||||
assertAll(
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCube(0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSphere(0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaRectangle(0, 10)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaRectangle(10, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCylinder(0, 1)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCylinder(1, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSquare(0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTriangleRectangle(0, 1)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTriangleRectangle(1, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaParallelogram(0, 1)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaParallelogram(1, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(0, 1, 1)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(1, 0, 1)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(1, 1, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCircle(0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaHemisphere(0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCone(1, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCone(0, 1))
|
||||
);
|
||||
()
|
||||
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCube(0)),
|
||||
()
|
||||
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSphere(0)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaRectangle(0, 10)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaRectangle(10, 0)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaCylinder(0, 1)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaCylinder(1, 0)),
|
||||
()
|
||||
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSquare(0)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaTriangleRectangle(0, 1)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaTriangleRectangle(1, 0)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaParallelogram(0, 1)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaParallelogram(1, 0)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(0, 1, 1)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(1, 0, 1)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(1, 1, 0)),
|
||||
()
|
||||
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCircle(0)),
|
||||
()
|
||||
-> assertThrows(
|
||||
IllegalArgumentException.class, () -> Area.surfaceAreaHemisphere(0)),
|
||||
()
|
||||
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCone(1, 0)),
|
||||
() -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCone(0, 1)));
|
||||
}
|
||||
}
|
@ -1,14 +1,15 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AutomorphicNumberTest {
|
||||
|
||||
@Test
|
||||
void testAutomorphicNumber() {
|
||||
int[] trueTestCases = { 0, 1, 25, 625, 12890625};
|
||||
int[] falseTestCases = { -5, 2, 26, 1234 };
|
||||
int[] trueTestCases = {0, 1, 25, 625, 12890625};
|
||||
int[] falseTestCases = {-5, 2, 26, 1234};
|
||||
for (Integer n : trueTestCases) {
|
||||
assertTrue(AutomorphicNumber.isAutomorphic(n));
|
||||
assertTrue(AutomorphicNumber.isAutomorphic2(n));
|
||||
@ -19,7 +20,9 @@ public class AutomorphicNumberTest {
|
||||
assertFalse(AutomorphicNumber.isAutomorphic2(n));
|
||||
assertFalse(AutomorphicNumber.isAutomorphic3(String.valueOf(n)));
|
||||
}
|
||||
assertTrue(AutomorphicNumber.isAutomorphic3("59918212890625")); // Special case for BigInteger
|
||||
assertFalse(AutomorphicNumber.isAutomorphic3("12345678912345")); // Special case for BigInteger
|
||||
assertTrue(
|
||||
AutomorphicNumber.isAutomorphic3("59918212890625")); // Special case for BigInteger
|
||||
assertFalse(
|
||||
AutomorphicNumber.isAutomorphic3("12345678912345")); // Special case for BigInteger
|
||||
}
|
||||
}
|
||||
|
@ -30,5 +30,4 @@ public class AverageTest {
|
||||
int[] numbers = {2, 4, 10};
|
||||
Assertions.assertEquals(5, Average.average(numbers));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,34 +27,13 @@ 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));
|
||||
}
|
||||
}
|
||||
|
@ -9,34 +9,24 @@ public class DistanceFormulaTest {
|
||||
|
||||
@Test
|
||||
void euclideanTest1() {
|
||||
Assertions.assertEquals(
|
||||
DistanceFormula.euclideanDistance(1, 1, 2, 2),
|
||||
1.4142135623730951
|
||||
);
|
||||
Assertions.assertEquals(DistanceFormula.euclideanDistance(1, 1, 2, 2), 1.4142135623730951);
|
||||
}
|
||||
|
||||
@Test
|
||||
void euclideanTest2() {
|
||||
Assertions.assertEquals(
|
||||
DistanceFormula.euclideanDistance(1, 3, 8, 0),
|
||||
7.0710678118654755
|
||||
);
|
||||
Assertions.assertEquals(DistanceFormula.euclideanDistance(1, 3, 8, 0), 7.0710678118654755);
|
||||
}
|
||||
|
||||
@Test
|
||||
void euclideanTest3() {
|
||||
Assertions.assertEquals(
|
||||
DistanceFormula.euclideanDistance(2.4, 9.1, 55.1, 100),
|
||||
110.91911467371168
|
||||
);
|
||||
DistanceFormula.euclideanDistance(2.4, 9.1, 55.1, 100), 110.91911467371168);
|
||||
}
|
||||
|
||||
@Test
|
||||
void euclideanTest4() {
|
||||
Assertions.assertEquals(
|
||||
DistanceFormula.euclideanDistance(1000, 13, 20000, 84),
|
||||
19022.067605809836
|
||||
);
|
||||
DistanceFormula.euclideanDistance(1000, 13, 20000, 84), 19022.067605809836);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -46,65 +36,53 @@ public class DistanceFormulaTest {
|
||||
|
||||
@Test
|
||||
public void manhattantest2() {
|
||||
assertEquals(
|
||||
DistanceFormula.manhattanDistance(6.5, 8.4, 20.1, 13.6),
|
||||
18.8
|
||||
);
|
||||
assertEquals(DistanceFormula.manhattanDistance(6.5, 8.4, 20.1, 13.6), 18.8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void manhattanTest3() {
|
||||
assertEquals(
|
||||
DistanceFormula.manhattanDistance(10.112, 50, 8, 25.67),
|
||||
26.442
|
||||
);
|
||||
assertEquals(DistanceFormula.manhattanDistance(10.112, 50, 8, 25.67), 26.442);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hammingTest1() {
|
||||
int[] array1 = { 1, 1, 1, 1 };
|
||||
int[] array2 = { 0, 0, 0, 0 };
|
||||
int[] array1 = {1, 1, 1, 1};
|
||||
int[] array2 = {0, 0, 0, 0};
|
||||
assertEquals(DistanceFormula.hammingDistance(array1, array2), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hammingTest2() {
|
||||
int[] array1 = { 1, 1, 1, 1 };
|
||||
int[] array2 = { 1, 1, 1, 1 };
|
||||
int[] array1 = {1, 1, 1, 1};
|
||||
int[] array2 = {1, 1, 1, 1};
|
||||
assertEquals(DistanceFormula.hammingDistance(array1, array2), 0);
|
||||
}
|
||||
|
||||
@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 };
|
||||
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 minkowskiTest1() {
|
||||
double[] array1 = { 1, 3, 8, 5 };
|
||||
double[] array2 = { 4, 2, 6, 9 };
|
||||
double[] array1 = {1, 3, 8, 5};
|
||||
double[] array2 = {4, 2, 6, 9};
|
||||
assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 1), 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void minkowskiTest2() {
|
||||
double[] array1 = { 1, 3, 8, 5 };
|
||||
double[] array2 = { 4, 2, 6, 9 };
|
||||
assertEquals(
|
||||
DistanceFormula.minkowskiDistance(array1, array2, 2),
|
||||
5.477225575051661
|
||||
);
|
||||
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
|
||||
);
|
||||
double[] array1 = {1, 3, 8, 5};
|
||||
double[] array2 = {4, 2, 6, 9};
|
||||
assertEquals(DistanceFormula.minkowskiDistance(array1, array2, 3), 4.641588833612778);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DudeneyNumberTest {
|
||||
|
||||
@Test
|
||||
@ -13,6 +13,5 @@ class DudeneyNumberTest {
|
||||
|
||||
assertTrue(() -> DudeneyNumber.isDudeney(validDudeneyNumber));
|
||||
assertFalse(() -> DudeneyNumber.isDudeney(invalidDudeneyNumber));
|
||||
|
||||
}
|
||||
}
|
@ -9,33 +9,21 @@ public class FindMaxTest {
|
||||
|
||||
@Test
|
||||
public void testFindMax0() {
|
||||
assertEquals(
|
||||
10,
|
||||
FindMax.findMax(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })
|
||||
);
|
||||
assertEquals(10, FindMax.findMax(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindMax1() {
|
||||
assertEquals(
|
||||
7,
|
||||
FindMax.findMax(new int[] { 6, 3, 5, 1, 7, 4, 1 })
|
||||
);
|
||||
assertEquals(7, FindMax.findMax(new int[] {6, 3, 5, 1, 7, 4, 1}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindMax2() {
|
||||
assertEquals(
|
||||
10,
|
||||
FindMax.findMax(new int[] { 10, 0 })
|
||||
);
|
||||
assertEquals(10, FindMax.findMax(new int[] {10, 0}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindMaxThrowsExceptionForEmptyInput() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> FindMax.findMax(new int[]{})
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class, () -> FindMax.findMax(new int[] {}));
|
||||
}
|
||||
}
|
||||
|
@ -9,32 +9,26 @@ public class FindMinTest {
|
||||
|
||||
@Test
|
||||
public void testFindMinValue() {
|
||||
assertEquals(
|
||||
1,
|
||||
FindMin.findMin(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })
|
||||
);
|
||||
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 }));
|
||||
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 }));
|
||||
assertEquals(0, FindMin.findMin(new int[] {0, 192, 384, 576}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
assertEquals(0, FindMin.findMin(new int[] { 10, 10, 0, 10 }));
|
||||
assertEquals(0, FindMin.findMin(new int[] {10, 10, 0, 10}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindMinThrowsExceptionForEmptyInput() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> FindMin.findMin(new int[]{})
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class, () -> FindMin.findMin(new int[] {}));
|
||||
}
|
||||
}
|
||||
|
@ -6,43 +6,23 @@ import org.junit.jupiter.api.Test;
|
||||
public class FrizzyNumberTest {
|
||||
@Test
|
||||
public void testFrizziesForBase2() {
|
||||
assertEquals(
|
||||
1,
|
||||
FrizzyNumber.getNthFrizzy(2, 1));
|
||||
assertEquals(
|
||||
3,
|
||||
FrizzyNumber.getNthFrizzy(2, 3));
|
||||
assertEquals(
|
||||
1000,
|
||||
FrizzyNumber.getNthFrizzy(2, 1000));
|
||||
assertEquals(1, FrizzyNumber.getNthFrizzy(2, 1));
|
||||
assertEquals(3, FrizzyNumber.getNthFrizzy(2, 3));
|
||||
assertEquals(1000, FrizzyNumber.getNthFrizzy(2, 1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrizziesForBase3() {
|
||||
assertEquals(
|
||||
1,
|
||||
FrizzyNumber.getNthFrizzy(3, 1));
|
||||
assertEquals(
|
||||
3,
|
||||
FrizzyNumber.getNthFrizzy(3, 2));
|
||||
assertEquals(
|
||||
29430,
|
||||
FrizzyNumber.getNthFrizzy(3, 1000));
|
||||
assertEquals(1, FrizzyNumber.getNthFrizzy(3, 1));
|
||||
assertEquals(3, FrizzyNumber.getNthFrizzy(3, 2));
|
||||
assertEquals(29430, FrizzyNumber.getNthFrizzy(3, 1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrizziesForBase69() {
|
||||
assertEquals(
|
||||
1,
|
||||
FrizzyNumber.getNthFrizzy(69, 1));
|
||||
assertEquals(
|
||||
69,
|
||||
FrizzyNumber.getNthFrizzy(69, 2));
|
||||
assertEquals(
|
||||
328510,
|
||||
FrizzyNumber.getNthFrizzy(69, 9));
|
||||
assertEquals(
|
||||
333340,
|
||||
FrizzyNumber.getNthFrizzy(69, 15));
|
||||
assertEquals(1, FrizzyNumber.getNthFrizzy(69, 1));
|
||||
assertEquals(69, FrizzyNumber.getNthFrizzy(69, 2));
|
||||
assertEquals(328510, FrizzyNumber.getNthFrizzy(69, 9));
|
||||
assertEquals(333340, FrizzyNumber.getNthFrizzy(69, 15));
|
||||
}
|
||||
}
|
||||
|
@ -7,26 +7,17 @@ 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
|
||||
@ -48,19 +39,20 @@ public class GCDTest {
|
||||
void test7() {
|
||||
Assertions.assertEquals(GCD.gcd(9, 6), 3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testArrayGcd1() {
|
||||
Assertions.assertEquals(GCD.gcd(new int[]{9, 6}), 3);
|
||||
Assertions.assertEquals(GCD.gcd(new int[] {9, 6}), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArrayGcd2() {
|
||||
Assertions.assertEquals(GCD.gcd(new int[]{2*3*5*7, 2*5*5*5, 2*5*11, 5*5*5*13}), 5);
|
||||
Assertions.assertEquals(
|
||||
GCD.gcd(new int[] {2 * 3 * 5 * 7, 2 * 5 * 5 * 5, 2 * 5 * 11, 5 * 5 * 5 * 13}), 5);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testArrayGcdForEmptyInput() {
|
||||
Assertions.assertEquals(GCD.gcd(new int[]{}), 0);
|
||||
}
|
||||
Assertions.assertEquals(GCD.gcd(new int[] {}), 0);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HarshadNumberTest {
|
||||
|
||||
@Test
|
||||
public void harshadNumber() {
|
||||
|
||||
|
||||
assertTrue(HarshadNumber.isHarshad(18));
|
||||
assertFalse(HarshadNumber.isHarshad(-18));
|
||||
assertFalse(HarshadNumber.isHarshad(19));
|
||||
|
@ -17,17 +17,11 @@ public class HeronsFormulaTest {
|
||||
|
||||
@Test
|
||||
void test3() {
|
||||
Assertions.assertEquals(
|
||||
HeronsFormula.Herons(1, 1, 1),
|
||||
0.4330127018922193
|
||||
);
|
||||
Assertions.assertEquals(HeronsFormula.Herons(1, 1, 1), 0.4330127018922193);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test4() {
|
||||
Assertions.assertEquals(
|
||||
HeronsFormula.Herons(4, 5, 8),
|
||||
8.181534085976786
|
||||
);
|
||||
Assertions.assertEquals(HeronsFormula.Herons(4, 5, 8), 8.181534085976786);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package com.thealgorithms.maths;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KaprekarNumbersTest {
|
||||
@ -51,10 +50,7 @@ public class KaprekarNumbersTest {
|
||||
@Test
|
||||
void testForRangeOfNumber() {
|
||||
try {
|
||||
List<Long> rangedNumbers = KaprekarNumbers.kaprekarNumberInRange(
|
||||
1,
|
||||
100000
|
||||
);
|
||||
List<Long> rangedNumbers = KaprekarNumbers.kaprekarNumberInRange(1, 100000);
|
||||
long[] allTheNumbers = {
|
||||
1,
|
||||
9,
|
||||
|
@ -1,13 +1,14 @@
|
||||
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 LeonardoNumberTest {
|
||||
@Test
|
||||
void leonardoNumberNegative() {
|
||||
assertThrows(ArithmeticException.class, ()-> LeonardoNumber.leonardoNumber(-1));
|
||||
assertThrows(ArithmeticException.class, () -> LeonardoNumber.leonardoNumber(-1));
|
||||
}
|
||||
@Test
|
||||
void leonardoNumberZero() {
|
||||
@ -23,6 +24,6 @@ public class LeonardoNumberTest {
|
||||
}
|
||||
@Test
|
||||
void leonardoNumberTwenty() {
|
||||
assertEquals(21891 , LeonardoNumber.leonardoNumber(20));
|
||||
assertEquals(21891, LeonardoNumber.leonardoNumber(20));
|
||||
}
|
||||
}
|
||||
|
@ -8,65 +8,57 @@ class LiouvilleLambdaFunctionTest {
|
||||
|
||||
@Test
|
||||
void testLiouvilleLambdaMustThrowExceptionIfNumberIsZero() {
|
||||
//given
|
||||
// given
|
||||
int number = 0;
|
||||
String expectedMessage = "Number must be greater than zero.";
|
||||
|
||||
//when
|
||||
Exception exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
LiouvilleLambdaFunction.liouvilleLambda(number);
|
||||
}
|
||||
);
|
||||
// when
|
||||
Exception exception = assertThrows(IllegalArgumentException.class,
|
||||
() -> { LiouvilleLambdaFunction.liouvilleLambda(number); });
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLiouvilleLambdaMustThrowExceptionIfNumberIsNegative() {
|
||||
//given
|
||||
// given
|
||||
int number = -1;
|
||||
String expectedMessage = "Number must be greater than zero.";
|
||||
|
||||
//when
|
||||
Exception exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
LiouvilleLambdaFunction.liouvilleLambda(number);
|
||||
}
|
||||
);
|
||||
// when
|
||||
Exception exception = assertThrows(IllegalArgumentException.class,
|
||||
() -> { LiouvilleLambdaFunction.liouvilleLambda(number); });
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLiouvilleLambdaMustReturnNegativeOne() {
|
||||
//given
|
||||
// given
|
||||
int number = 11;
|
||||
int expectedOutput = -1;
|
||||
|
||||
//when
|
||||
// when
|
||||
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedOutput, actualOutput);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLiouvilleLambdaMustReturnPositiveOne() {
|
||||
//given
|
||||
// given
|
||||
int number = 10;
|
||||
int expectedOutput = 1;
|
||||
|
||||
//when
|
||||
// when
|
||||
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedOutput, actualOutput);
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,58 @@
|
||||
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 LongDivisionTest {
|
||||
|
||||
// Requirement: Dividend (positive) is greater than divisor (positive), returns correct integer after division
|
||||
// Requirement: Dividend (positive) is greater than divisor (positive), returns correct integer
|
||||
// after division
|
||||
@Test
|
||||
void testOne() {
|
||||
assertEquals(3, LongDivision.divide(10,3));
|
||||
assertEquals(3, LongDivision.divide(10, 3));
|
||||
}
|
||||
|
||||
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division
|
||||
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer
|
||||
// after division
|
||||
@Test
|
||||
void testTwo() {
|
||||
assertEquals(-2, LongDivision.divide(7,-3));
|
||||
assertEquals(-2, LongDivision.divide(7, -3));
|
||||
}
|
||||
|
||||
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer after division
|
||||
// Basically the same as in the first test
|
||||
|
||||
// Requirement: Dividend (positive) is greater than divisor (negative), returns correct integer
|
||||
// after division Basically the same as in the first test
|
||||
@Test
|
||||
void testThree() {
|
||||
assertEquals(10, LongDivision.divide(105,10));
|
||||
assertEquals(10, LongDivision.divide(105, 10));
|
||||
}
|
||||
|
||||
// Requirement: Dividend (negative), divisor (positive), returns correct integer after division
|
||||
// Tests the case where the dividend is less than 0.
|
||||
@Test
|
||||
void testNegativeDividend() {
|
||||
assertEquals(-1, LongDivision.divide(-5,3));
|
||||
assertEquals(-1, LongDivision.divide(-5, 3));
|
||||
}
|
||||
|
||||
|
||||
// Requirement: Dividend (positive), divisor (positive), returns correct integer after division
|
||||
// Tests the case where the dividend is less than the divisor. The test should return 0 in this case.
|
||||
// Tests the case where the dividend is less than the divisor. The test should return 0 in this
|
||||
// case.
|
||||
@Test
|
||||
void testDividendLessThanDivisor() {
|
||||
assertEquals(0, LongDivision.divide(3,5));
|
||||
assertEquals(0, LongDivision.divide(3, 5));
|
||||
}
|
||||
|
||||
// Requirement: Dividend (neither), divisor (positive), returns correct integer after division
|
||||
// Tests the case where the dividend is 0. This should return a 0.
|
||||
@Test
|
||||
void testDividendIsZero() {
|
||||
assertEquals(0, LongDivision.divide(0,5));
|
||||
assertEquals(0, LongDivision.divide(0, 5));
|
||||
}
|
||||
|
||||
// Requirement: Dividend (positive), divisor (neither), returns correct integer after division
|
||||
// Tests the case where the divisor is 0. This should return a 0.
|
||||
@Test
|
||||
void testDivisionByZero() {
|
||||
assertEquals(0, LongDivision.divide(5,0));
|
||||
assertEquals(0, LongDivision.divide(5, 0));
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +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 LucasSeriesTest {
|
||||
@Test
|
||||
void lucasSeriesTwo() {
|
||||
|
@ -1,8 +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 MedianTest {
|
||||
@Test
|
||||
void medianSingleValue() {
|
||||
|
@ -8,39 +8,31 @@ class MobiusFunctionTest {
|
||||
|
||||
@Test
|
||||
void testMobiusForZero() {
|
||||
//given
|
||||
// given
|
||||
int number = 0;
|
||||
String expectedMessage = "Number must be greater than zero.";
|
||||
|
||||
//when
|
||||
// when
|
||||
Exception exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
MobiusFunction.mobius(number);
|
||||
}
|
||||
);
|
||||
IllegalArgumentException.class, () -> { MobiusFunction.mobius(number); });
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMobiusForNegativeNumber() {
|
||||
//given
|
||||
// given
|
||||
int number = -1;
|
||||
String expectedMessage = "Number must be greater than zero.";
|
||||
|
||||
//when
|
||||
// when
|
||||
Exception exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
MobiusFunction.mobius(number);
|
||||
}
|
||||
);
|
||||
IllegalArgumentException.class, () -> { MobiusFunction.mobius(number); });
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
|
||||
@ -150,13 +142,13 @@ class MobiusFunctionTest {
|
||||
};
|
||||
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
//given
|
||||
// given
|
||||
int expectedValue = expectedResultArray[i - 1];
|
||||
|
||||
//when
|
||||
// when
|
||||
int actualValue = MobiusFunction.mobius(i);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedValue, actualValue);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class NthUglyNumberTest {
|
||||
@ -51,7 +50,7 @@ public class NthUglyNumberTest {
|
||||
for (final var tc : testCases.entrySet()) {
|
||||
assertEquals(uglyNumbers.get(tc.getKey()), tc.getValue());
|
||||
}
|
||||
|
||||
|
||||
assertEquals(uglyNumbers.get(999), 385875);
|
||||
}
|
||||
|
||||
@ -67,21 +66,14 @@ public class NthUglyNumberTest {
|
||||
assertEquals(uglyNumbers.get(5), 32);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetThrowsAnErrorForNegativeInput() {
|
||||
var uglyNumbers = new NthUglyNumber(new int[] {1, 2});
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> uglyNumbers.get(-1)
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class, () -> uglyNumbers.get(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorThrowsAnErrorForEmptyInput() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> new NthUglyNumber(new int[] {})
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class, () -> new NthUglyNumber(new int[] {}));
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,14 @@ 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);
|
||||
}
|
||||
|
||||
@ -24,11 +24,11 @@ class PascalTriangleTest {
|
||||
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 },
|
||||
{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);
|
||||
}
|
||||
@ -37,14 +37,14 @@ class PascalTriangleTest {
|
||||
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 },
|
||||
{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);
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PerfectNumberTest {
|
||||
|
||||
@Test
|
||||
public void perfectNumber() {
|
||||
int[] trueTestCases = { 6, 28, 496, 8128, 33550336 };
|
||||
int[] falseTestCases = { -6, 0, 1, 9, 123 };
|
||||
int[] trueTestCases = {6, 28, 496, 8128, 33550336};
|
||||
int[] falseTestCases = {-6, 0, 1, 9, 123};
|
||||
for (Integer n : trueTestCases) {
|
||||
assertTrue(PerfectNumber.isPerfectNumber(n));
|
||||
assertTrue(PerfectNumber.isPerfectNumber2(n));
|
||||
|
@ -8,7 +8,7 @@ public class PerfectSquareTest {
|
||||
|
||||
@Test
|
||||
public void TestPerfectSquareifiscorrect() {
|
||||
//Valid Partition
|
||||
// Valid Partition
|
||||
int number = 9;
|
||||
|
||||
boolean result = PerfectSquare.isPerfectSquare(number);
|
||||
@ -18,7 +18,7 @@ public class PerfectSquareTest {
|
||||
|
||||
@Test
|
||||
public void TestPerfectSquareifisnotcorrect() {
|
||||
//Invalid Partition 1
|
||||
// Invalid Partition 1
|
||||
int number = 3;
|
||||
|
||||
boolean result = PerfectSquare.isPerfectSquare(number);
|
||||
@ -28,7 +28,7 @@ public class PerfectSquareTest {
|
||||
|
||||
@Test
|
||||
public void TestPerfectSquareifisNegativeNumber() {
|
||||
//Invalid Partition 2
|
||||
// Invalid Partition 2
|
||||
int number = -10;
|
||||
|
||||
boolean result = PerfectSquare.isPerfectSquare(number);
|
||||
|
@ -37,7 +37,7 @@ public class PerimeterTest {
|
||||
void testcase6() {
|
||||
Assertions.assertEquals(43.982297150257104, Perimeter.perimeterCircle(7));
|
||||
}
|
||||
|
||||
|
||||
// Perimeter of Irregular polygon
|
||||
@Test
|
||||
void testcase7() {
|
||||
|
@ -9,46 +9,42 @@ class PollardRhoTest {
|
||||
|
||||
@Test
|
||||
void testPollardRhoForNumber315MustReturn5() {
|
||||
//given
|
||||
// given
|
||||
int number = 315;
|
||||
int expectedResult = 5;
|
||||
|
||||
//when
|
||||
// when
|
||||
int actualResult = PollardRho.pollardRho(number);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPollardRhoForNumber187MustReturn11() {
|
||||
//given
|
||||
// given
|
||||
int number = 187;
|
||||
int expectedResult = 11;
|
||||
|
||||
//when
|
||||
// when
|
||||
int actualResult = PollardRho.pollardRho(number);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPollardRhoForNumber239MustThrowException() {
|
||||
//given
|
||||
// given
|
||||
int number = 239;
|
||||
String expectedMessage = "GCD cannot be found.";
|
||||
|
||||
//when
|
||||
Exception exception = assertThrows(
|
||||
RuntimeException.class,
|
||||
() -> {
|
||||
PollardRho.pollardRho(number);
|
||||
}
|
||||
);
|
||||
// when
|
||||
Exception exception
|
||||
= assertThrows(RuntimeException.class, () -> { PollardRho.pollardRho(number); });
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
}
|
||||
|
@ -9,23 +9,23 @@ class PrimeFactorizationTest {
|
||||
|
||||
@Test
|
||||
void testpFactorsMustReturnEmptyList() {
|
||||
//given
|
||||
// given
|
||||
int n = 0;
|
||||
|
||||
//then
|
||||
// then
|
||||
assertTrue(PrimeFactorization.pfactors(n).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testpFactorsMustReturnNonEmptyList() {
|
||||
//given
|
||||
// given
|
||||
int n = 198;
|
||||
int expectedListSize = 4;
|
||||
|
||||
//when
|
||||
// when
|
||||
List<Integer> actualResultList = PrimeFactorization.pfactors(n);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertEquals(expectedListSize, actualResultList.size());
|
||||
assertEquals(2, actualResultList.get(0));
|
||||
assertEquals(3, actualResultList.get(1));
|
||||
|
@ -8,25 +8,25 @@ public class PronicNumberTest {
|
||||
|
||||
@Test
|
||||
void testForPronicNumber() {
|
||||
//given
|
||||
// given
|
||||
int number = 30;
|
||||
|
||||
//when
|
||||
// when
|
||||
boolean result = PronicNumber.isPronic(number);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForNonPronicNumber() {
|
||||
//given
|
||||
// given
|
||||
int number = 21;
|
||||
|
||||
//when
|
||||
// when
|
||||
boolean result = PronicNumber.isPronic(number);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ReverseNumberTest {
|
||||
@ -25,9 +24,6 @@ public class ReverseNumberTest {
|
||||
|
||||
@Test
|
||||
public void testReverseNumberThrowsExceptionForNegativeInput() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ReverseNumber.reverseNumber(-1)
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(-1));
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -7,25 +7,16 @@ public class SquareRootWithNewtonRaphsonTestMethod {
|
||||
|
||||
@Test
|
||||
void testfor1() {
|
||||
Assertions.assertEquals(
|
||||
1,
|
||||
SquareRootWithNewtonRaphsonMethod.squareRoot(1)
|
||||
);
|
||||
Assertions.assertEquals(1, SquareRootWithNewtonRaphsonMethod.squareRoot(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testfor2() {
|
||||
Assertions.assertEquals(
|
||||
1.414213562373095,
|
||||
SquareRootWithNewtonRaphsonMethod.squareRoot(2)
|
||||
);
|
||||
Assertions.assertEquals(1.414213562373095, SquareRootWithNewtonRaphsonMethod.squareRoot(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testfor625() {
|
||||
Assertions.assertEquals(
|
||||
25.0,
|
||||
SquareRootWithNewtonRaphsonMethod.squareRoot(625)
|
||||
);
|
||||
Assertions.assertEquals(25.0, SquareRootWithNewtonRaphsonMethod.squareRoot(625));
|
||||
}
|
||||
}
|
||||
|
@ -7,33 +7,21 @@ public class SquareRootwithBabylonianMethodTest {
|
||||
|
||||
@Test
|
||||
void testfor4() {
|
||||
Assertions.assertEquals(
|
||||
2,
|
||||
SquareRootWithBabylonianMethod.square_Root(4)
|
||||
);
|
||||
Assertions.assertEquals(2, SquareRootWithBabylonianMethod.square_Root(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testfor1() {
|
||||
Assertions.assertEquals(
|
||||
1,
|
||||
SquareRootWithBabylonianMethod.square_Root(1)
|
||||
);
|
||||
Assertions.assertEquals(1, SquareRootWithBabylonianMethod.square_Root(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testfor2() {
|
||||
Assertions.assertEquals(
|
||||
1.4142135381698608,
|
||||
SquareRootWithBabylonianMethod.square_Root(2)
|
||||
);
|
||||
Assertions.assertEquals(1.4142135381698608, SquareRootWithBabylonianMethod.square_Root(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testfor625() {
|
||||
Assertions.assertEquals(
|
||||
25,
|
||||
SquareRootWithBabylonianMethod.square_Root(625)
|
||||
);
|
||||
Assertions.assertEquals(25, SquareRootWithBabylonianMethod.square_Root(625));
|
||||
}
|
||||
}
|
||||
|
@ -7,26 +7,20 @@ public class StandardDeviationTest {
|
||||
|
||||
@Test
|
||||
void test1() {
|
||||
double[] t1 = new double[] { 1, 1, 1, 1, 1 };
|
||||
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
|
||||
);
|
||||
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
|
||||
);
|
||||
double[] t3 = new double[] {1.1, 8.5, 20.3, 2.4, 6.2};
|
||||
Assertions.assertEquals(StandardDeviation.stdDev(t3), 6.8308125431752265);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -38,9 +32,6 @@ public class StandardDeviationTest {
|
||||
100.00045,
|
||||
56.7,
|
||||
};
|
||||
Assertions.assertEquals(
|
||||
StandardDeviation.stdDev(t4),
|
||||
38.506117353865775
|
||||
);
|
||||
Assertions.assertEquals(StandardDeviation.stdDev(t4), 38.506117353865775);
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,6 @@ public class StandardScoreTest {
|
||||
|
||||
@Test
|
||||
void test4() {
|
||||
Assertions.assertEquals(
|
||||
StandardScore.zScore(8.9, 3, 4.2),
|
||||
1.4047619047619049
|
||||
);
|
||||
Assertions.assertEquals(StandardScore.zScore(8.9, 3, 4.2), 1.4047619047619049);
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,40 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SumWithoutArithmeticOperatorsTest {
|
||||
SumWithoutArithmeticOperators obj = new SumWithoutArithmeticOperators();
|
||||
|
||||
@Test
|
||||
void addZerotoZero(){
|
||||
assertEquals(0,obj.getSum(0, 0));
|
||||
void addZerotoZero() {
|
||||
assertEquals(0, obj.getSum(0, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addZerotoNumber(){
|
||||
assertEquals(5,obj.getSum(0, 5));
|
||||
assertEquals(28,obj.getSum(28, 0));
|
||||
void addZerotoNumber() {
|
||||
assertEquals(5, obj.getSum(0, 5));
|
||||
assertEquals(28, obj.getSum(28, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addOddtoEven(){
|
||||
assertEquals(13,obj.getSum(3, 10));
|
||||
assertEquals(55,obj.getSum(49, 6));
|
||||
void addOddtoEven() {
|
||||
assertEquals(13, obj.getSum(3, 10));
|
||||
assertEquals(55, obj.getSum(49, 6));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addEventoOdd(){
|
||||
assertEquals(13,obj.getSum(10, 3));
|
||||
assertEquals(41,obj.getSum(40, 1));
|
||||
void addEventoOdd() {
|
||||
assertEquals(13, obj.getSum(10, 3));
|
||||
assertEquals(41, obj.getSum(40, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addRandoms(){
|
||||
assertEquals(88,obj.getSum(44, 44));
|
||||
assertEquals(370,obj.getSum(100, 270));
|
||||
assertEquals(3,obj.getSum(1, 2));
|
||||
assertEquals(5,obj.getSum(2, 3));
|
||||
void addRandoms() {
|
||||
assertEquals(88, obj.getSum(44, 44));
|
||||
assertEquals(370, obj.getSum(100, 270));
|
||||
assertEquals(3, obj.getSum(1, 2));
|
||||
assertEquals(5, obj.getSum(2, 3));
|
||||
}
|
||||
}
|
||||
|
@ -6,55 +6,55 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
class TwinPrimeTest {
|
||||
|
||||
@Test
|
||||
void shouldReturn7() {
|
||||
//given
|
||||
int number = 5;
|
||||
int expectedResult = 7;
|
||||
|
||||
//when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
//then
|
||||
assertEquals(expectedResult,actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn5() {
|
||||
//given
|
||||
int number = 3;
|
||||
int expectedResult = 5;
|
||||
|
||||
//when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
//then
|
||||
assertEquals(expectedResult,actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNegative1() {
|
||||
//given
|
||||
int number = 4;
|
||||
int expectedResult = -1;
|
||||
|
||||
//when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
//then
|
||||
assertEquals(expectedResult,actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn19() {
|
||||
//given
|
||||
int number = 17;
|
||||
int expectedResult = 19;
|
||||
|
||||
//when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
//then
|
||||
assertEquals(expectedResult,actualResult);
|
||||
}
|
||||
@Test
|
||||
void shouldReturn7() {
|
||||
// given
|
||||
int number = 5;
|
||||
int expectedResult = 7;
|
||||
|
||||
// when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
// then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn5() {
|
||||
// given
|
||||
int number = 3;
|
||||
int expectedResult = 5;
|
||||
|
||||
// when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
// then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNegative1() {
|
||||
// given
|
||||
int number = 4;
|
||||
int expectedResult = -1;
|
||||
|
||||
// when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
// then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn19() {
|
||||
// given
|
||||
int number = 17;
|
||||
int expectedResult = 19;
|
||||
|
||||
// when
|
||||
int actualResult = TwinPrime.getTwinPrime(number);
|
||||
|
||||
// then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class VolumeTest {
|
||||
|
Reference in New Issue
Block a user