JAVA-12730: Rename java-numbers-3 to core-java-numbers-3
This commit is contained in:
17
core-java-modules/core-java-numbers-3/README.md
Normal file
17
core-java-modules/core-java-numbers-3/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
## Java Number Cookbooks and Examples
|
||||
|
||||
This module contains articles about numbers in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Generating Random Numbers in Java](https://www.baeldung.com/java-generating-random-numbers)
|
||||
- [Convert Double to Long in Java](https://www.baeldung.com/java-convert-double-long)
|
||||
- [Check for null Before Calling Parse in Double.parseDouble](https://www.baeldung.com/java-check-null-parse-double)
|
||||
- [Generating Random Numbers in a Range in Java](https://www.baeldung.com/java-generating-random-numbers-in-range)
|
||||
- [Listing Numbers Within a Range in Java](https://www.baeldung.com/java-listing-numbers-within-a-range)
|
||||
- [Fibonacci Series in Java](https://www.baeldung.com/java-fibonacci)
|
||||
- [Guide to the Number Class in Java](https://www.baeldung.com/java-number-class)
|
||||
- [Print an Integer in Binary Format in Java](https://www.baeldung.com/java-print-integer-binary)
|
||||
- [Number Formatting in Java](https://www.baeldung.com/java-number-formatting)
|
||||
- [Division by Zero in Java: Exception, Infinity, or Not a Number](https://www.baeldung.com/java-division-by-zero)
|
||||
- More articles: [[<-- prev]](../java-numbers-2) [[next -->]](../java-numbers-4)
|
||||
49
core-java-modules/core-java-numbers-3/pom.xml
Normal file
49
core-java-modules/core-java-numbers-3/pom.xml
Normal file
@@ -0,0 +1,49 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-numbers-3</artifactId>
|
||||
<name>core-java-numbers-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>it.unimi.dsi</groupId>
|
||||
<artifactId>dsiutils</artifactId>
|
||||
<version>${dsiutils.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-numbers-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<dsiutils.version>2.6.0</dsiutils.version>
|
||||
<vavr.version>0.10.2</vavr.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.fibonacci;
|
||||
|
||||
public class FibonacciSeriesUtils {
|
||||
|
||||
public static int nthFibonacciTermRecursiveMethod(int n) {
|
||||
if (n == 0 || n == 1) {
|
||||
return n;
|
||||
}
|
||||
return nthFibonacciTermRecursiveMethod(n - 1) + nthFibonacciTermRecursiveMethod(n - 2);
|
||||
}
|
||||
|
||||
public static int nthFibonacciTermIterativeMethod(int n) {
|
||||
if (n == 0 || n == 1) {
|
||||
return n;
|
||||
}
|
||||
int n0 = 0, n1 = 1;
|
||||
int tempNthTerm;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
tempNthTerm = n0 + n1;
|
||||
n0 = n1;
|
||||
n1 = tempNthTerm;
|
||||
}
|
||||
return n1;
|
||||
}
|
||||
|
||||
public static int nthFibonacciTermUsingBinetsFormula(int n) {
|
||||
final double squareRootOf5 = Math.sqrt(5);
|
||||
final double phi = (1 + squareRootOf5)/2;
|
||||
int nthTerm = (int) ((Math.pow(phi, n) - Math.pow(-phi, -n))/squareRootOf5);
|
||||
return nthTerm;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.formatNumber;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
public class FormatNumber {
|
||||
public static double withBigDecimal(double value, int places) {
|
||||
if (places < 0)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
BigDecimal bigDecimal = new BigDecimal(value);
|
||||
bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP);
|
||||
return bigDecimal.doubleValue();
|
||||
}
|
||||
|
||||
public static double withMathRound(double value, int places) {
|
||||
double scale = Math.pow(10, places);
|
||||
return Math.round(value * scale) / scale;
|
||||
}
|
||||
|
||||
public static double withDecimalFormatPattern(double value, int places) {
|
||||
DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00");
|
||||
DecimalFormat df3 = new DecimalFormat("#,###,###,##0.000");
|
||||
if (places == 2)
|
||||
return new Double(df2.format(value));
|
||||
else if (places == 3)
|
||||
return new Double(df3.format(value));
|
||||
else
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public static double withDecimalFormatLocal(double value) {
|
||||
DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
|
||||
return new Double(df.format(value));
|
||||
}
|
||||
|
||||
public static String withStringFormat(double value, int places) {
|
||||
return String.format("%." + places + "f", value);
|
||||
}
|
||||
|
||||
public static String byPaddingZeros(int value, int paddingLength) {
|
||||
return String.format("%0" + paddingLength + "d", value);
|
||||
}
|
||||
|
||||
public static double withTwoDecimalPlaces(double value) {
|
||||
DecimalFormat df = new DecimalFormat("#.00");
|
||||
return new Double(df.format(value));
|
||||
}
|
||||
|
||||
public static String withLargeIntegers(double value) {
|
||||
DecimalFormat df = new DecimalFormat("###,###,###");
|
||||
return df.format(value);
|
||||
}
|
||||
|
||||
public static String forPercentages(double value, Locale localisation) {
|
||||
NumberFormat nf = NumberFormat.getPercentInstance(localisation);
|
||||
return nf.format(value);
|
||||
}
|
||||
|
||||
public static String currencyWithChosenLocalisation(double value, Locale localisation) {
|
||||
NumberFormat nf = NumberFormat.getCurrencyInstance(localisation);
|
||||
return nf.format(value);
|
||||
}
|
||||
|
||||
public static String currencyWithDefaultLocalisation(double value) {
|
||||
NumberFormat nf = NumberFormat.getCurrencyInstance();
|
||||
return nf.format(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.integerToBinary;
|
||||
|
||||
public class IntegerToBinary {
|
||||
public static String convertIntegerToBinary(int n) {
|
||||
if(n == 0) {
|
||||
return "0";
|
||||
}
|
||||
StringBuilder binaryNumber = new StringBuilder();
|
||||
while (n > 0) {
|
||||
int remainder = n % 2;
|
||||
binaryNumber.append(remainder);
|
||||
n /= 2;
|
||||
}
|
||||
binaryNumber = binaryNumber.reverse();
|
||||
return binaryNumber.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.numbersinrange;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class NumbersInARange {
|
||||
|
||||
public List<Integer> getNumbersInRange(int start, int end) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
for (int i = start; i < end; i++) {
|
||||
result.add(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<Integer> getNumbersUsingIntStreamRange(int start, int end) {
|
||||
return IntStream.range(start, end)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Integer> getNumbersUsingIntStreamRangeClosed(int start, int end) {
|
||||
return IntStream.rangeClosed(start, end)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Integer> getNumbersUsingIntStreamIterate(int start, int limit) {
|
||||
return IntStream.iterate(start, i -> i + 1)
|
||||
.limit(limit)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.numbersinrange;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomNumbersInARange {
|
||||
|
||||
public int getRandomNumber(int min, int max) {
|
||||
return (int) ((Math.random() * (max - min)) + min);
|
||||
}
|
||||
|
||||
public int getRandomNumberUsingNextInt(int min, int max) {
|
||||
Random random = new Random();
|
||||
return random.nextInt(max - min) + min;
|
||||
}
|
||||
|
||||
public int getRandomNumberUsingInts(int min, int max) {
|
||||
Random random = new Random();
|
||||
return random.ints(min, max)
|
||||
.findFirst()
|
||||
.getAsInt();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.baeldung.randomnumbers;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.apache.commons.math3.random.RandomDataGenerator;
|
||||
|
||||
import it.unimi.dsi.util.XoRoShiRo128PlusRandom;
|
||||
|
||||
public class RandomNumbersGenerator {
|
||||
|
||||
public Integer generateRandomWithMathRandom(int max, int min) {
|
||||
return (int) ((Math.random() * (max - min)) + min);
|
||||
}
|
||||
|
||||
public Integer generateRandomWithNextInt() {
|
||||
Random random = new Random();
|
||||
int randomWithNextInt = random.nextInt();
|
||||
return randomWithNextInt;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithNextIntWithinARange(int min, int max) {
|
||||
Random random = new Random();
|
||||
int randomWintNextIntWithinARange = random.nextInt(max - min) + min;
|
||||
return randomWintNextIntWithinARange;
|
||||
}
|
||||
|
||||
public IntStream generateRandomUnlimitedIntStream() {
|
||||
Random random = new Random();
|
||||
IntStream unlimitedIntStream = random.ints();
|
||||
return unlimitedIntStream;
|
||||
}
|
||||
|
||||
public IntStream generateRandomLimitedIntStream(long streamSize) {
|
||||
Random random = new Random();
|
||||
IntStream limitedIntStream = random.ints(streamSize);
|
||||
return limitedIntStream;
|
||||
}
|
||||
|
||||
public IntStream generateRandomLimitedIntStreamWithinARange(int min, int max, long streamSize) {
|
||||
Random random = new Random();
|
||||
IntStream limitedIntStreamWithinARange = random.ints(streamSize, min, max);
|
||||
return limitedIntStreamWithinARange;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithThreadLocalRandom() {
|
||||
int randomWithThreadLocalRandom = ThreadLocalRandom.current()
|
||||
.nextInt();
|
||||
return randomWithThreadLocalRandom;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithThreadLocalRandomInARange(int min, int max) {
|
||||
int randomWithThreadLocalRandomInARange = ThreadLocalRandom.current()
|
||||
.nextInt(min, max);
|
||||
return randomWithThreadLocalRandomInARange;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithThreadLocalRandomFromZero(int max) {
|
||||
int randomWithThreadLocalRandomFromZero = ThreadLocalRandom.current()
|
||||
.nextInt(max);
|
||||
return randomWithThreadLocalRandomFromZero;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithSplittableRandom(int min, int max) {
|
||||
SplittableRandom splittableRandom = new SplittableRandom();
|
||||
int randomWithSplittableRandom = splittableRandom.nextInt(min, max);
|
||||
return randomWithSplittableRandom;
|
||||
}
|
||||
|
||||
public IntStream generateRandomWithSplittableRandomLimitedIntStreamWithinARange(int min, int max, long streamSize) {
|
||||
SplittableRandom splittableRandom = new SplittableRandom();
|
||||
IntStream limitedIntStreamWithinARangeWithSplittableRandom = splittableRandom.ints(streamSize, min, max);
|
||||
return limitedIntStreamWithinARangeWithSplittableRandom;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithSecureRandom() {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
int randomWithSecureRandom = secureRandom.nextInt();
|
||||
return randomWithSecureRandom;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithSecureRandomWithinARange(int min, int max) {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
int randomWithSecureRandomWithinARange = secureRandom.nextInt(max - min) + min;
|
||||
return randomWithSecureRandomWithinARange;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithRandomDataGenerator(int min, int max) {
|
||||
RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
|
||||
int randomWithRandomDataGenerator = randomDataGenerator.nextInt(min, max);
|
||||
return randomWithRandomDataGenerator;
|
||||
}
|
||||
|
||||
public Integer generateRandomWithXoRoShiRo128PlusRandom(int min, int max) {
|
||||
XoRoShiRo128PlusRandom xoroRandom = new XoRoShiRo128PlusRandom();
|
||||
int randomWithXoRoShiRo128PlusRandom = xoroRandom.nextInt(max - min) + min;
|
||||
return randomWithXoRoShiRo128PlusRandom;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.abstractnumber;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AbstractNumberUnitTest {
|
||||
|
||||
private final static double DOUBLE_VALUE = 9999.999;
|
||||
private final static float FLOAT_VALUE = 101.99F;
|
||||
private final static long LONG_VALUE = 1000L;
|
||||
private final static int INTEGER_VALUE = 100;
|
||||
private final static short SHORT_VALUE = 127;
|
||||
private final static byte BYTE_VALUE = 120;
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenShortValueUsed_thenShortValueReturned() {
|
||||
Double doubleValue = Double.valueOf(DOUBLE_VALUE);
|
||||
assertEquals(9999, doubleValue.shortValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFloatValue_whenByteValueUsed_thenByteValueReturned() {
|
||||
Float floatValue = Float.valueOf(FLOAT_VALUE);
|
||||
assertEquals(101, floatValue.byteValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLongValue_whenInitValueUsed_thenInitValueReturned() {
|
||||
Long longValue = Long.valueOf(LONG_VALUE);
|
||||
assertEquals(1000, longValue.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerValue_whenLongValueUsed_thenLongValueReturned() {
|
||||
Integer integerValue = Integer.valueOf(INTEGER_VALUE);
|
||||
assertEquals(100, integerValue.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenShortValue_whenFloatValueUsed_thenFloatValueReturned() {
|
||||
Short shortValue = Short.valueOf(SHORT_VALUE);
|
||||
assertEquals(127.0F, shortValue.floatValue(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteValue_whenDoubleValueUsed_thenDoubleValueReturned() {
|
||||
Byte byteValue = Byte.valueOf(BYTE_VALUE);
|
||||
assertEquals(120.0, byteValue.doubleValue(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.divisionbyzero;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class DivisionByZeroUnitTest {
|
||||
|
||||
@Test
|
||||
void givenInt_whenDividedByZero_thenThrowException() {
|
||||
assertThrows(ArithmeticException.class, () -> {
|
||||
int result = 12 / 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDividingIntZeroByZero_thenThrowException() {
|
||||
assertThrows(ArithmeticException.class, () -> {
|
||||
int result = 0 / 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDividingFloatingNumberByZero_thenNoExceptionIsThrown() {
|
||||
assertDoesNotThrow(() -> {
|
||||
float result = 0f / 0;
|
||||
});
|
||||
assertDoesNotThrow(() -> {
|
||||
double result = 0d / 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPositiveFloatingNumber_whenDividedByZero_thenReturnPositiveInfinity() {
|
||||
assertEquals(Float.POSITIVE_INFINITY, 12f / 0);
|
||||
assertEquals(Double.POSITIVE_INFINITY, 12d / 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNegativeFloatingNumber_whenDividedByZero_thenReturnNegativeInfinity() {
|
||||
assertEquals(Float.NEGATIVE_INFINITY, -12f / 0);
|
||||
assertEquals(Double.NEGATIVE_INFINITY, -12d / 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPositiveFloatingNumber_whenDividedByNegativeZero_thenReturnNegativeInfinity() {
|
||||
assertEquals(Float.NEGATIVE_INFINITY, 12f / -0f);
|
||||
assertEquals(Double.NEGATIVE_INFINITY, 12f / -0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDividingFloatingNumberZeroByZero_thenReturnNaN() {
|
||||
assertEquals(Float.NaN, 0f / 0);
|
||||
assertEquals(Double.NaN, 0d / 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenABitRepresentationWithAllExponentBitsZeroesAndAllFractionBitsZeroes_whenTransformingItToFloat_thenReturnPositiveZero() {
|
||||
assertEquals(0f, Float.intBitsToFloat(0b00000000000000000000000000000000));
|
||||
assertEquals(-0f, Float.intBitsToFloat(0b10000000000000000000000000000000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenABitRepresentationWithAllExponentBitsOnesAndAllFractionBitsZeroes_whenTransformingItToFloat_thenReturnInfinity() {
|
||||
assertEquals(Float.POSITIVE_INFINITY, Float.intBitsToFloat(0b01111111100000000000000000000000));
|
||||
assertEquals(Float.NEGATIVE_INFINITY, Float.intBitsToFloat(0b11111111100000000000000000000000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenABitRepresentationWithAllExponentBitsOnesAndNotAllFractionBitsZeroes_whenTransformingItToFloat_thenReturnNan() {
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000010000000000000000));
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000011000000000100000));
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000011100000000000000));
|
||||
assertEquals(Float.NaN, Float.intBitsToFloat(0b11111111100000011110000000000000));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.doubletolong;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DoubleToLongUnitTest {
|
||||
|
||||
final static double VALUE = 9999.999;
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenLongValueCalled_thenLongValueReturned() {
|
||||
Assert.assertEquals(9999L, Double.valueOf(VALUE).longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathRoundUsed_thenRoundUp() {
|
||||
Assert.assertEquals(10000L, Math.round(VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathRoundUsed_thenRoundDown() {
|
||||
Assert.assertEquals(9999L, Math.round(9999.444));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathRoundUsed_thenSameValueReturned() {
|
||||
Assert.assertEquals(9999L, Math.round(9999.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathCeilUsed_thenLongValueReturned() {
|
||||
Assert.assertEquals(10000L, Math.ceil(VALUE), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathCeilUsed_thenSameValueReturned() {
|
||||
Assert.assertEquals(9999L, Math.ceil(9999.0), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathCeilUsed_thenDifferentThanRound() {
|
||||
Assert.assertEquals(10000L, Math.ceil(9999.444), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathFloorUsed_thenLongValueReturned() {
|
||||
Assert.assertEquals(9999L, Math.floor(VALUE), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathFloorUsed_thenSameValueReturned() {
|
||||
Assert.assertEquals(9999L, Math.floor(9999.0), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenMathFloorUsed_thenDifferentThanCeil() {
|
||||
Assert.assertEquals(9999L, Math.floor(9999.444), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValue_whenTypeCasted_thenLongValueReturned() {
|
||||
Assert.assertEquals(9999L, (long) VALUE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.fibonacci;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FibonacciSeriesUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTermToCalculate_thenReturnThatTermUsingRecursion() {
|
||||
int term = 10;
|
||||
int expectedValue = 55;
|
||||
assertEquals(FibonacciSeriesUtils.nthFibonacciTermRecursiveMethod(term), expectedValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTermToCalculate_thenReturnThatTermUsingIteration() {
|
||||
int term = 10;
|
||||
int expectedValue = 55;
|
||||
assertEquals(FibonacciSeriesUtils.nthFibonacciTermIterativeMethod(term), expectedValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTermToCalculate_thenReturnThatTermUsingBinetsFormula() {
|
||||
int term = 10;
|
||||
int expectedValue = 55;
|
||||
assertEquals(FibonacciSeriesUtils.nthFibonacciTermUsingBinetsFormula(term), expectedValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.formatNumber;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static com.baeldung.formatNumber.FormatNumber.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class FormatNumberUnitTest {
|
||||
private static final double D = 4.2352989244d;
|
||||
private static final double F = 8.6994540927d;
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithBigDecimal_thenGetExpectedResult() {
|
||||
assertThat(withBigDecimal(D, 2)).isEqualTo(4.24);
|
||||
assertThat(withBigDecimal(D, 3)).isEqualTo(4.235);
|
||||
assertThat(withBigDecimal(F, 2)).isEqualTo(8.7);
|
||||
assertThat(withBigDecimal(F, 3)).isEqualTo(8.699);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithDecimalFormat_thenGetExpectedResult() {
|
||||
assertThat(withDecimalFormatLocal(D)).isEqualTo(4.235);
|
||||
assertThat(withDecimalFormatLocal(F)).isEqualTo(8.699);
|
||||
|
||||
assertThat(withDecimalFormatPattern(D, 2)).isEqualTo(4.24);
|
||||
assertThat(withDecimalFormatPattern(D, 3)).isEqualTo(4.235);
|
||||
assertThat(withDecimalFormatPattern(F, 2)).isEqualTo(8.7);
|
||||
assertThat(withDecimalFormatPattern(F, 3)).isEqualTo(8.699);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithStringFormat_thenGetExpectedResult() {
|
||||
assertThat(withStringFormat(D, 2)).isEqualTo("4.24");
|
||||
assertThat(withStringFormat(D, 3)).isEqualTo("4.235");
|
||||
assertThat(withStringFormat(F, 2)).isEqualTo("8.70");
|
||||
assertThat(withStringFormat(F, 3)).isEqualTo("8.699");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberWithMathRound_thenGetExpectedResult() {
|
||||
assertThat(withMathRound(D, 2)).isEqualTo(4.24);
|
||||
assertThat(withMathRound(D, 3)).isEqualTo(4.235);
|
||||
assertThat(withMathRound(F, 2)).isEqualTo(8.7);
|
||||
assertThat(withMathRound(F, 3)).isEqualTo(8.699);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerNumber_whenFormatNumberByPaddingOutZeros_thenGetExpectedResult() {
|
||||
int value = 1;
|
||||
assertThat(byPaddingZeros(value, 3)).isEqualTo("001");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerNumber_whenFormatNumberWithTwoDecimalPlaces_thenGetExpectedResult() {
|
||||
int value = 12;
|
||||
assertThat(withTwoDecimalPlaces(value)).isEqualTo(12.00);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerNumber_whenFormatNumberWithLargeIntegers_thenGetExpectedResult() {
|
||||
int value = 123456789;
|
||||
assertThat(withLargeIntegers(value)).isEqualTo("123,456,789");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDecimalNumber_whenFormatNumberForPercentages_thenGetExpectedResult() {
|
||||
double value = 25f / 100f;
|
||||
assertThat(forPercentages(value, new Locale("en", "US"))).isEqualTo("25%");
|
||||
assertThat(forPercentages(value, new Locale("pl", "PL"))).isEqualTo("25%");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrency_whenFormatNumberCurrencyWithChosenLocalisation_thenGetExpectedResult() {
|
||||
double value = 23_500;
|
||||
assertThat(currencyWithChosenLocalisation(value, new Locale("en", "US"))).isEqualTo("$23,500.00");
|
||||
assertThat(currencyWithChosenLocalisation(value, new Locale("zh", "CN"))).isEqualTo("¥23,500.00");
|
||||
assertThat(currencyWithChosenLocalisation(value, new Locale("pl", "PL"))).isEqualTo("23 500 zł");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.integerToBinary;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IntegerToBinaryUnitTest {
|
||||
@Test
|
||||
public void givenAnInteger_whenConvertToBinary_thenGetBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = IntegerToBinary.convertIntegerToBinary(n);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenToBinaryStringCalled_thenGetBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = Integer.toBinaryString(n);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnInteger_whenToStringCalled_thenGetBinaryString() {
|
||||
int n = 7;
|
||||
String binaryString = Integer.toString(n, 2);
|
||||
assertEquals("111", binaryString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.numbersinrange;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NumbersInARangeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingForLoop_thenExpectCorrectResult() {
|
||||
NumbersInARange numbersInARange = new NumbersInARange();
|
||||
List<Integer> numbers = numbersInARange.getNumbersInRange(1, 10);
|
||||
|
||||
assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9), numbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingIntStreamRange_thenExpectCorrectResult() {
|
||||
NumbersInARange numbersInARange = new NumbersInARange();
|
||||
List<Integer> numbers = numbersInARange.getNumbersUsingIntStreamRange(1, 10);
|
||||
|
||||
assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9), numbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingIntStreamRangeClosed_thenExpectCorrectResult() {
|
||||
NumbersInARange numbersInARange = new NumbersInARange();
|
||||
List<Integer> numbers = numbersInARange.getNumbersUsingIntStreamRangeClosed(1, 10);
|
||||
|
||||
assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9,10), numbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingIntStreamIterate_thenExpectCorrectResult() {
|
||||
NumbersInARange numbersInARange = new NumbersInARange();
|
||||
List<Integer> numbers = numbersInARange.getNumbersUsingIntStreamIterate(1, 10);
|
||||
|
||||
assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9,10), numbers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.numbersinrange;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RandomNumbersInARangeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingMathRandom_thenExpectCorrectResult() {
|
||||
RandomNumbersInARange randomNumbersInARange = new RandomNumbersInARange();
|
||||
int number = randomNumbersInARange.getRandomNumber(1, 10);
|
||||
|
||||
assertTrue(number >= 1);
|
||||
assertTrue(number < 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingRandomInts_thenExpectCorrectResult() {
|
||||
RandomNumbersInARange randomNumbersInARange = new RandomNumbersInARange();
|
||||
int number = randomNumbersInARange.getRandomNumberUsingInts(1, 10);
|
||||
|
||||
assertTrue(number >= 1);
|
||||
assertTrue(number < 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTheRange1To10_andUsingRandomNextInt_thenExpectCorrectResult() {
|
||||
RandomNumbersInARange randomNumbersInARange = new RandomNumbersInARange();
|
||||
int number = randomNumbersInARange.getRandomNumberUsingNextInt(1, 10);
|
||||
|
||||
assertTrue(number >= 1);
|
||||
assertTrue(number < 10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.baeldung.parsedouble;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.primitives.Doubles;
|
||||
|
||||
import io.vavr.control.Try;
|
||||
|
||||
public class StringToDoubleParserUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenParseStringToDouble_thenDefaultNaNValueIsReturned() {
|
||||
assertThat(parseStringToDouble(null)).isNaN();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenParseStringToDouble_thenDefaultNaNValueIsReturned() {
|
||||
assertThat(parseStringToDouble("")).isNaN();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringValue_whenParseStringToDouble_thenDoubleValueIsReturned() {
|
||||
assertThat(parseStringToDouble("1")).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringValue_whenParseStringToDoubleWithDefault_thenDoubleValueIsReturned() {
|
||||
assertThat(parseStringToDouble("1", 2.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenParseStringToDoubleWithDefault_thenDefaultValueIsReturned() {
|
||||
assertThat(parseStringToDouble("", 1.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenParseStringToDoubleWithDefault_thenDefaultValueIsReturned() {
|
||||
assertThat(parseStringToDouble(null, 1.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringValue_whenParseStringToOptionalDouble_thenOptionalValueIsReturned() {
|
||||
assertThat(parseStringToOptionalDouble("1")).isEqualTo(Optional.of(1.0d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenParseStringToOptionalDouble_thenOptionalValueIsEmpty() {
|
||||
assertThat(parseStringToOptionalDouble(null)).isEqualTo(Optional.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenParseStringToOptionalDouble_thenOptionalValueIsEmpty() {
|
||||
assertThat(parseStringToOptionalDouble("")).isEqualTo(Optional.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenParseStringToOptionalDouble_thenDefaulOptionalValueIsReturned() {
|
||||
assertThat(parseStringToOptionalDouble("").orElse(1.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenParseStringToOptionalDouble_thenDefaulOptionalValueIsReturned() {
|
||||
assertThat(parseStringToOptionalDouble(null).orElse(1.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringValue_whenTryStringToDouble_thenDoubleValueIsReturned() {
|
||||
assertThat(tryStringToDouble("1", 2.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenTryStringToDoubleWithDefault_thenDoubleValueIsReturned() {
|
||||
assertThat(tryStringToDouble(null, 2.0d)).isEqualTo(2.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenTryStringToDoubleWithDefault_thenDoubleValueIsReturned() {
|
||||
assertThat(tryStringToDouble("", 2.0d)).isEqualTo(2.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoStringValues_whenTryParseFirstNonNull_thenDoubleValueIsReturned() {
|
||||
assertThat(Doubles.tryParse(MoreObjects.firstNonNull("1.0", "2.0"))).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullStringValue_whenTryParseFirstNonNull_thenSecondDoubleValueIsReturned() {
|
||||
assertThat(Doubles.tryParse(MoreObjects.firstNonNull(null, "2.0"))).isEqualTo(2.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenTryParseFirstNonNull_thenNullIsReturned() {
|
||||
assertThat(Doubles.tryParse(MoreObjects.firstNonNull("", "2.0"))).isEqualTo(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringValue_whenToDouble_thenDoubleValueIsReturned() {
|
||||
assertThat(NumberUtils.toDouble("1.0")).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenToDouble_thenLibraryDefaultDoubleValueIsReturned() {
|
||||
String nullString = null;
|
||||
assertThat(NumberUtils.toDouble(nullString)).isEqualTo(0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenToDouble_thenLibraryDefaultDoubleValueIsReturned() {
|
||||
assertThat(NumberUtils.toDouble("")).isEqualTo(0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyStringValue_whenToDoubleWithDefault_thenDoubleValueIsReturned() {
|
||||
assertThat(NumberUtils.toDouble("", 2.0d)).isEqualTo(2.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullValue_whenToDoubleWithDefault_thenDoubleValueIsReturned() {
|
||||
String nullString = null;
|
||||
assertThat(NumberUtils.toDouble(nullString, 2.0d)).isEqualTo(2.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringValue_whenToDoubleWithDefault_thenDoubleValueIsReturned() {
|
||||
assertThat(NumberUtils.toDouble("1.0", 2.0d)).isEqualTo(1.0d);
|
||||
}
|
||||
|
||||
private static Optional<Double> parseStringToOptionalDouble(String value) {
|
||||
return value == null || value.isEmpty() ? Optional.empty() : Optional.of(Double.valueOf(value));
|
||||
}
|
||||
|
||||
private static double parseStringToDouble(String value) {
|
||||
return value == null || value.isEmpty() ? Double.NaN : Double.parseDouble(value);
|
||||
}
|
||||
|
||||
private static double parseStringToDouble(String value, double defaultValue) {
|
||||
return value == null || value.isEmpty() ? defaultValue : Double.parseDouble(value);
|
||||
}
|
||||
|
||||
private static double tryStringToDouble(String value, double defaultValue) {
|
||||
return Try.of(() -> Double.parseDouble(value)).getOrElse(defaultValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.baeldung.randomnumbers;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RandomNumbersGeneratorUnitTest {
|
||||
|
||||
private static final int MIN_RANGE = 1;
|
||||
private static final int MAX_RANGE = 10;
|
||||
private static final int MIN_RANGE_NEGATIVE = -10;
|
||||
private static final int ITERATIONS = 50;
|
||||
private static final long STREAM_SIZE = 50;
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithMathRandom_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumer = generator.generateRandomWithMathRandom(MIN_RANGE, MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumer, MIN_RANGE, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithNextInt_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithNextInt();
|
||||
assertTrue(isInRange(randomNumber, Integer.MIN_VALUE, Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithNextIntWithinARange_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithNextIntWithinARange(MIN_RANGE, MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomUnlimitedIntStream_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
IntStream stream = generator.generateRandomUnlimitedIntStream();
|
||||
assertNotNull(stream);
|
||||
Integer randomNumber = stream.findFirst()
|
||||
.getAsInt();
|
||||
assertNotNull(randomNumber);
|
||||
assertTrue(isInRange(randomNumber, Integer.MIN_VALUE, Integer.MAX_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomLimitedIntStream_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
generator.generateRandomLimitedIntStream(STREAM_SIZE)
|
||||
.forEach(randomNumber -> assertTrue(isInRange(randomNumber, Integer.MIN_VALUE, Integer.MAX_VALUE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomLimitedIntStreamWithinARange_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
generator.generateRandomLimitedIntStreamWithinARange(MIN_RANGE, MAX_RANGE, STREAM_SIZE)
|
||||
.forEach(randomNumber -> assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithThreadLocalRandom_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithThreadLocalRandom();
|
||||
assertTrue(isInRange(randomNumber, Integer.MIN_VALUE, Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithThreadLocalRandomInARange_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithThreadLocalRandomInARange(MIN_RANGE, MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithThreadLocalRandomFromZero_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithThreadLocalRandomFromZero(MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumber, 0, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithSplittableRandom_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithSplittableRandom(MIN_RANGE_NEGATIVE, MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumber, MIN_RANGE_NEGATIVE, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithSplittableRandomLimitedIntStreamWithinARange_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
generator.generateRandomWithSplittableRandomLimitedIntStreamWithinARange(MIN_RANGE, MAX_RANGE, STREAM_SIZE)
|
||||
.forEach(randomNumber -> assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithSecureRandom_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithSecureRandom();
|
||||
assertTrue(isInRange(randomNumber, Integer.MIN_VALUE, Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithSecureRandomWithinARange_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithSecureRandomWithinARange(MIN_RANGE, MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithRandomDataGenerator_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithRandomDataGenerator(MIN_RANGE, MAX_RANGE);
|
||||
// RandomDataGenerator top is inclusive
|
||||
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGenerateRandomWithXoRoShiRo128PlusRandom_returnsSuccessfully() {
|
||||
RandomNumbersGenerator generator = new RandomNumbersGenerator();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
int randomNumber = generator.generateRandomWithXoRoShiRo128PlusRandom(MIN_RANGE, MAX_RANGE);
|
||||
assertTrue(isInRange(randomNumber, MIN_RANGE, MAX_RANGE));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInRange(int number, int min, int max) {
|
||||
return min <= number && number < max;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user