Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-16045-3

This commit is contained in:
amit2103
2019-10-27 19:39:47 +05:30
4564 changed files with 284202 additions and 23978 deletions

View File

@@ -1,68 +0,0 @@
package com.baeldung.algorithms.primechecker;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PrimeCheckerUnitTest {
private final BigIntegerPrimeChecker primeChecker = new BigIntegerPrimeChecker();
@Test
public void whenCheckIsPrime_thenTrue() {
assertTrue(primeChecker.isPrime(13l));
assertTrue(primeChecker.isPrime(1009L));
assertTrue(primeChecker.isPrime(74207281L));
}
@Test
public void whenCheckIsPrime_thenFalse() {
assertTrue(!primeChecker.isPrime(50L));
assertTrue(!primeChecker.isPrime(1001L));
assertTrue(!primeChecker.isPrime(74207282L));
}
private final BruteForcePrimeChecker bfPrimeChecker = new BruteForcePrimeChecker();
@Test
public void whenBFCheckIsPrime_thenTrue() {
assertTrue(bfPrimeChecker.isPrime(13));
assertTrue(bfPrimeChecker.isPrime(1009));
}
@Test
public void whenBFCheckIsPrime_thenFalse() {
assertFalse(bfPrimeChecker.isPrime(50));
assertFalse(bfPrimeChecker.isPrime(1001));
}
private final OptimisedPrimeChecker optimisedPrimeChecker = new OptimisedPrimeChecker();
@Test
public void whenOptCheckIsPrime_thenTrue() {
assertTrue(optimisedPrimeChecker.isPrime(13));
assertTrue(optimisedPrimeChecker.isPrime(1009));
}
@Test
public void whenOptCheckIsPrime_thenFalse() {
assertFalse(optimisedPrimeChecker.isPrime(50));
assertFalse(optimisedPrimeChecker.isPrime(1001));
}
private final PrimesPrimeChecker primesPrimeChecker = new PrimesPrimeChecker();
@Test
public void whenPrimesCheckIsPrime_thenTrue() {
assertTrue(primesPrimeChecker.isPrime(13));
assertTrue(primesPrimeChecker.isPrime(1009));
}
@Test
public void whenPrimesCheckIsPrime_thenFalse() {
assertFalse(primesPrimeChecker.isPrime(50));
assertFalse(primesPrimeChecker.isPrime(1001));
}
}

View File

@@ -1,175 +0,0 @@
package com.baeldung.java.math;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MathUnitTest {
@Test
public void whenAbsInteger_thenReturnAbsoluteValue() {
assertEquals(5,Math.abs(-5));
}
@Test
public void whenMaxBetweenTwoValue_thenReturnMaximum() {
assertEquals(10, Math.max(5,10));
}
@Test
public void whenMinBetweenTwoValue_thenReturnMinimum() {
assertEquals(5, Math.min(5,10));
}
@Test
public void whenSignumWithNegativeNumber_thenReturnMinusOne() {
assertEquals(-1, Math.signum(-5), 0);
}
@Test
public void whenCopySignWithNegativeSign_thenReturnNegativeArgument() {
assertEquals(-5, Math.copySign(5,-1), 0);
}
@Test
public void whenPow_thenReturnPoweredValue() {
assertEquals(25, Math.pow(5,2),0);
}
@Test
public void whenSqrt_thenReturnSquareRoot() {
assertEquals(5, Math.sqrt(25),0);
}
@Test
public void whenCbrt_thenReturnCubeRoot() {
assertEquals(5, Math.cbrt(125),0);
}
@Test
public void whenExp_thenReturnEulerNumberRaised() {
assertEquals(2.718, Math.exp(1),0.1);
}
@Test
public void whenExpm1_thenReturnEulerNumberMinusOne() {
assertEquals(1.718, Math.expm1(1),0.1);
}
@Test
public void whenGetExponent_thenReturnUnbiasedExponent() {
assertEquals(8, Math.getExponent(333.3),0);
assertEquals(7, Math.getExponent(222.2f),0);
}
@Test
public void whenLog_thenReturnValue() {
assertEquals(1, Math.log(Math.E),0);
}
@Test
public void whenLog10_thenReturnValue() {
assertEquals(1, Math.log10(10),0);
}
@Test
public void whenLog1p_thenReturnValue() {
assertEquals(1.31, Math.log1p(Math.E),0.1);
}
@Test
public void whenSin_thenReturnValue() {
assertEquals(1, Math.sin(Math.PI/2),0);
}
@Test
public void whenCos_thenReturnValue() {
assertEquals(1, Math.cos(0),0);
}
@Test
public void whenTan_thenReturnValue() {
assertEquals(1, Math.tan(Math.PI/4),0.1);
}
@Test
public void whenAsin_thenReturnValue() {
assertEquals(Math.PI/2, Math.asin(1),0);
}
@Test
public void whenAcos_thenReturnValue() {
assertEquals(Math.PI/2, Math.acos(0),0);
}
@Test
public void whenAtan_thenReturnValue() {
assertEquals(Math.PI/4, Math.atan(1),0);
}
@Test
public void whenAtan2_thenReturnValue() {
assertEquals(Math.PI/4, Math.atan2(1,1),0);
}
@Test
public void whenToDegrees_thenReturnValue() {
assertEquals(180, Math.toDegrees(Math.PI),0);
}
@Test
public void whenToRadians_thenReturnValue() {
assertEquals(Math.PI, Math.toRadians(180),0);
}
@Test
public void whenCeil_thenReturnValue() {
assertEquals(4, Math.ceil(Math.PI),0);
}
@Test
public void whenFloor_thenReturnValue() {
assertEquals(3, Math.floor(Math.PI),0);
}
@Test
public void whenGetExponent_thenReturnValue() {
assertEquals(8, Math.getExponent(333.3),0);
}
@Test
public void whenIEEERemainder_thenReturnValue() {
assertEquals(1.0, Math.IEEEremainder(5,2),0);
}
@Test
public void whenNextAfter_thenReturnValue() {
assertEquals(1.9499999284744263, Math.nextAfter(1.95f,1),0.0000001);
}
@Test
public void whenNextUp_thenReturnValue() {
assertEquals(1.9500002, Math.nextUp(1.95f),0.0000001);
}
@Test
public void whenRint_thenReturnValue() {
assertEquals(2.0, Math.rint(1.95f),0.0);
}
@Test
public void whenRound_thenReturnValue() {
assertEquals(2.0, Math.round(1.95f),0.0);
}
@Test
public void whenScalb_thenReturnValue() {
assertEquals(48, Math.scalb(3, 4),0.0);
}
@Test
public void whenHypot_thenReturnValue() {
assertEquals(5, Math.hypot(4, 3),0.0);
}
}

View File

@@ -1,19 +1,22 @@
package com.baeldung.nth.root.calculator;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
public class NthRootCalculatorUnitTest {
private NthRootCalculator nthRootCalculator = new NthRootCalculator();
@Test
public void whenBaseIs125AndNIs3_thenNthRootIs5() {
Double result = nthRootCalculator.calculate(125.0, 3.0);
assertEquals(result, (Double) 5.0d);
public void whenBaseIs125AndNIs3_thenNthIs5() {
double nth = nthRootCalculator.calculateWithRound(125,3);
assertEquals(5, nth, 0);
}
@Test
public void whenBaseIs625AndNIs4_thenNthIs5() {
double nth = nthRootCalculator.calculate(625,4);
assertEquals(5, nth, 0.00001);
}
}

View File

@@ -1,34 +0,0 @@
package com.baeldung.nth.root.main;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import static org.junit.Assert.assertEquals;
public class MainUnitTest {
@InjectMocks
private Main main;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
@After
public void restoreStreams() {
System.setOut(originalOut);
}
@Test
public void givenThatTheBaseIs125_andTheExpIs3_whenMainIsCalled_thenTheCorrectResultIsPrinted() {
main.main(new String[]{"125.0", "3.0"});
assertEquals("The 3.0 root of 125.0 equals to 5.0.\n", outContent.toString().replaceAll("\r", ""));
}
}

View File

@@ -1,28 +0,0 @@
package com.baeldung.prime;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static com.baeldung.prime.PrimeGenerator.*;
import static org.junit.Assert.*;
public class PrimeGeneratorUnitTest {
@Test
public void whenBruteForced_returnsSuccessfully() {
final List<Integer> primeNumbers = primeNumbersBruteForce(20);
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
}
@Test
public void whenOptimized_returnsSuccessfully() {
final List<Integer> primeNumbers = primeNumbersTill(20);
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
}
@Test
public void whenSieveOfEratosthenes_returnsSuccessfully() {
final List<Integer> primeNumbers = sieveOfEratosthenes(20);
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
}
}