JAVA-12730: Rename java-numbers-4 to core-java-numbers-4
This commit is contained in:
13
core-java-modules/core-java-numbers-4/README.md
Normal file
13
core-java-modules/core-java-numbers-4/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Probability in Java](https://www.baeldung.com/java-probability)
|
||||
- [Understanding the & 0xff Value in Java](https://www.baeldung.com/java-and-0xff)
|
||||
- [Determine if an Integer’s Square Root Is an Integer in Java](https://www.baeldung.com/java-find-if-square-root-is-integer)
|
||||
- [Guide to Java BigInteger](https://www.baeldung.com/java-biginteger)
|
||||
- [Automorphic Numbers in Java](https://www.baeldung.com/java-automorphic-numbers)
|
||||
- [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size)
|
||||
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
|
||||
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
|
||||
- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number)
|
||||
- [Check if BigDecimal Value Is Zero](https://www.baeldung.com/java-bigdecimal-zero)
|
||||
- More articles: [[<-- prev]](../java-numbers-3)
|
||||
47
core-java-modules/core-java-numbers-4/pom.xml
Normal file
47
core-java-modules/core-java-numbers-4/pom.xml
Normal file
@@ -0,0 +1,47 @@
|
||||
<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-4</artifactId>
|
||||
<name>core-java-numbers-4</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>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>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-numbers-4</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<vavr.version>0.10.2</vavr.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.automorphicnumber;
|
||||
|
||||
public class AutomorphicNumber {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isAutomorphicUsingLoop(76));
|
||||
System.out.println(isAutomorphicUsingMath(76));
|
||||
}
|
||||
|
||||
public static boolean isAutomorphicUsingMath(int number) {
|
||||
int square = number * number;
|
||||
|
||||
int numberOfDigits = (int) Math.floor(Math.log10(number) + 1);
|
||||
int lastDigits = (int) (square % (Math.pow(10, numberOfDigits)));
|
||||
|
||||
return number == lastDigits;
|
||||
}
|
||||
|
||||
public static boolean isAutomorphicUsingLoop(int number) {
|
||||
int square = number * number;
|
||||
|
||||
while (number > 0) {
|
||||
if (number % 10 != square % 10) {
|
||||
return false;
|
||||
}
|
||||
number /= 10;
|
||||
square /= 10;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.booleantoint;
|
||||
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
|
||||
public class BooleanToInt {
|
||||
public static int booleanPrimitiveToInt(boolean foo) {
|
||||
int bar = 0;
|
||||
if (foo) {
|
||||
bar = 1;
|
||||
}
|
||||
return bar;
|
||||
}
|
||||
|
||||
public static int booleanPrimitiveToIntTernary(boolean foo) {
|
||||
return (foo) ? 1 : 0;
|
||||
}
|
||||
|
||||
public static int booleanObjectToInt(boolean foo) {
|
||||
return Boolean.compare(foo, false);
|
||||
}
|
||||
|
||||
public static int booleanObjectToIntInverse(boolean foo) {
|
||||
return Boolean.compare(foo, true) + 1;
|
||||
}
|
||||
|
||||
public static int booleanObjectMethodToInt(Boolean foo) {
|
||||
return foo.compareTo(false);
|
||||
}
|
||||
|
||||
public static int booleanObjectMethodToIntInverse(Boolean foo) {
|
||||
return foo.compareTo(true) + 1;
|
||||
}
|
||||
|
||||
public static int booleanUtilsToInt(Boolean foo) {
|
||||
return BooleanUtils.toInteger(foo);
|
||||
}
|
||||
|
||||
public static int bitwiseBooleanToInt(Boolean foo) {
|
||||
return (Boolean.hashCode(foo) >> 1) & 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.convertLongToInt;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
public class ConvertLongToInt {
|
||||
|
||||
static Function<Long, Integer> convert = val -> Optional.ofNullable(val)
|
||||
.map(Long::intValue)
|
||||
.orElse(null);
|
||||
|
||||
public static int longToIntCast(long number) {
|
||||
return (int) number;
|
||||
}
|
||||
|
||||
public static int longToIntJavaWithMath(long number) {
|
||||
return Math.toIntExact(number);
|
||||
}
|
||||
|
||||
public static int longToIntJavaWithLambda(long number) {
|
||||
return convert.apply(number);
|
||||
}
|
||||
|
||||
public static int longToIntBoxingValues(long number) {
|
||||
return Long.valueOf(number)
|
||||
.intValue();
|
||||
}
|
||||
|
||||
public static int longToIntGuava(long number) {
|
||||
return Ints.checkedCast(number);
|
||||
}
|
||||
|
||||
public static int longToIntGuavaSaturated(long number) {
|
||||
return Ints.saturatedCast(number);
|
||||
}
|
||||
|
||||
public static int longToIntWithBigDecimal(long number) {
|
||||
return new BigDecimal(number).intValueExact();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.humanreadablebytes;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class FileSizeFormatUtil {
|
||||
private static final long BYTE = 1L;
|
||||
private static final long KB = BYTE << 10;
|
||||
private static final long MB = KB << 10;
|
||||
private static final long GB = MB << 10;
|
||||
private static final long TB = GB << 10;
|
||||
private static final long PB = TB << 10;
|
||||
private static final long EB = PB << 10;
|
||||
private static final DecimalFormat DEC_FORMAT = new DecimalFormat("#.##");
|
||||
|
||||
public static String toHumanReadable(long size) {
|
||||
if (size < 0)
|
||||
throw new IllegalArgumentException("Invalid file size: " + size);
|
||||
if (size >= EB) return formatSize(size, EB, "EB");
|
||||
if (size >= PB) return formatSize(size, PB, "PB");
|
||||
if (size >= TB) return formatSize(size, TB, "TB");
|
||||
if (size >= GB) return formatSize(size, GB, "GB");
|
||||
if (size >= MB) return formatSize(size, MB, "MB");
|
||||
if (size >= KB) return formatSize(size, KB, "KB");
|
||||
return formatSize(size, BYTE, "Bytes");
|
||||
}
|
||||
|
||||
private static String formatSize(long size, long divider, String unitName) {
|
||||
return DEC_FORMAT.format((double) size / divider) + " " + unitName;
|
||||
}
|
||||
|
||||
public static String toHumanReadableWithEnum(long size) {
|
||||
final List<SizeUnit> units = SizeUnit.unitsInDescending();
|
||||
if (size < 0)
|
||||
throw new IllegalArgumentException("Invalid file size: " + size);
|
||||
String result = null;
|
||||
for (SizeUnit unit : units) {
|
||||
if (size >= unit.getUnitBase()) {
|
||||
result = formatSize(size, unit.getUnitBase(), unit.name());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result == null ? formatSize(size, SizeUnit.Bytes.getUnitBase(), SizeUnit.Bytes.name()) : result;
|
||||
}
|
||||
|
||||
public static String toHumanReadableByNumOfLeadingZeros(long size) {
|
||||
if (size < 0)
|
||||
throw new IllegalArgumentException("Invalid file size: " + size);
|
||||
if (size < 1024) return size + " Bytes";
|
||||
int unitIdx = (63 - Long.numberOfLeadingZeros(size)) / 10;
|
||||
return formatSize(size, 1L << (unitIdx * 10), " KMGTPE".charAt(unitIdx) + "B");
|
||||
}
|
||||
|
||||
enum SizeUnit {
|
||||
Bytes(1L),
|
||||
KB(Bytes.unitBase << 10),
|
||||
MB(KB.unitBase << 10),
|
||||
GB(MB.unitBase << 10),
|
||||
TB(GB.unitBase << 10),
|
||||
PB(TB.unitBase << 10),
|
||||
EB(PB.unitBase << 10);
|
||||
|
||||
private final Long unitBase;
|
||||
|
||||
public static List<SizeUnit> unitsInDescending() {
|
||||
List<SizeUnit> list = Arrays.asList(values());
|
||||
Collections.reverse(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
public Long getUnitBase() {
|
||||
return unitBase;
|
||||
}
|
||||
|
||||
SizeUnit(long unitBase) {
|
||||
this.unitBase = unitBase;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.number_0xff;
|
||||
|
||||
public class Number0xff {
|
||||
|
||||
public static int getRedColor(int rgba) {
|
||||
return rgba >> 24 & 0xff;
|
||||
}
|
||||
|
||||
public static int getGreenColor(int rgba) {
|
||||
return rgba >> 16 & 0xff;
|
||||
}
|
||||
|
||||
public static int getBlueColor(int rgba) {
|
||||
return rgba >> 8 & 0xff;
|
||||
}
|
||||
|
||||
public static int getAlfa(int rgba) {
|
||||
return rgba & 0xff;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.perfectsquare;
|
||||
|
||||
public class PerfectSquareUtil {
|
||||
|
||||
public static boolean isPerfectSquareByUsingSqrt(long n) {
|
||||
if (n <= 0)
|
||||
return false;
|
||||
double perfectSquare = Math.sqrt(n);
|
||||
long tst = (long)(perfectSquare + 0.5);
|
||||
return tst*tst == n;
|
||||
}
|
||||
|
||||
public static boolean isPerfectSquareByUsingBinarySearch(long low, long high, long number) {
|
||||
long check = (low + high) / 2L;
|
||||
if (high < low)
|
||||
return false;
|
||||
if (number == check * check) {
|
||||
return true;
|
||||
} else if (number < check * check) {
|
||||
high = check - 1L;
|
||||
return isPerfectSquareByUsingBinarySearch(low, high, number);
|
||||
} else {
|
||||
low = check + 1L;
|
||||
return isPerfectSquareByUsingBinarySearch(low, high, number);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPerfectSquareByUsingNewtonMethod(long n) {
|
||||
long x1 = n;
|
||||
long x2 = 1L;
|
||||
while (x1 > x2) {
|
||||
x1 = (x1 + x2) / 2L;
|
||||
x2 = n / x1;
|
||||
}
|
||||
return x1 == x2 && n % x1 == 0L;
|
||||
}
|
||||
|
||||
public static boolean isPerfectSquareWithOptimization(long n) {
|
||||
if (n < 0)
|
||||
return false;
|
||||
switch ((int) (n & 0xF)) {
|
||||
case 0: case 1: case 4: case 9:
|
||||
long tst = (long) Math.sqrt(n);
|
||||
return tst * tst == n;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.probability;
|
||||
|
||||
import org.apache.commons.math3.distribution.NormalDistribution;
|
||||
|
||||
public class MaleHeightGenerator {
|
||||
private static final double MEAN_HEIGHT = 176.02;
|
||||
private static final double STANDARD_DEVIATION = 7.11;
|
||||
private static NormalDistribution distribution = new NormalDistribution(MEAN_HEIGHT, STANDARD_DEVIATION);
|
||||
|
||||
public static double generateNormalHeight() {
|
||||
return distribution.sample();
|
||||
}
|
||||
|
||||
public static double probabilityOfHeightBetween(double heightLowerExclusive, double heightUpperInclusive) {
|
||||
return distribution.probability(heightLowerExclusive, heightUpperInclusive);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.probability;
|
||||
|
||||
import io.vavr.Lazy;
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class RandomInvoker {
|
||||
private final Lazy<SplittableRandom> random = Lazy.of(SplittableRandom::new);
|
||||
|
||||
public <T> T withProbability(Supplier<T> positiveCase, Supplier<T> negativeCase, int probability) {
|
||||
SplittableRandom random = this.random.get();
|
||||
if (random.nextInt(1, 101) <= probability) {
|
||||
return positiveCase.get();
|
||||
} else {
|
||||
return negativeCase.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.reversenumber;
|
||||
|
||||
public class ReverseNumber {
|
||||
|
||||
public static int reverseNumberWhileLoop(int number) {
|
||||
int reversedNumber = 0;
|
||||
int numberToReverse = Math.abs(number);
|
||||
|
||||
while (numberToReverse > 0) {
|
||||
int mod = numberToReverse % 10;
|
||||
reversedNumber = reversedNumber * 10 + mod;
|
||||
numberToReverse /= 10;
|
||||
}
|
||||
|
||||
return number < 0 ? reversedNumber * -1 : reversedNumber;
|
||||
}
|
||||
|
||||
public static int reverseNumberForLoop(int number) {
|
||||
int reversedNumber = 0;
|
||||
int numberToReverse = Math.abs(number);
|
||||
|
||||
for (; numberToReverse > 0; numberToReverse /= 10) {
|
||||
int mod = numberToReverse % 10;
|
||||
reversedNumber = reversedNumber * 10 + mod;
|
||||
}
|
||||
|
||||
return number < 0 ? reversedNumber * -1 : reversedNumber;
|
||||
}
|
||||
|
||||
public static int reverseNumberRecWrapper(int number) {
|
||||
int output = reverseNumberRec(Math.abs(number), 0);
|
||||
return number < 0 ? output * -1 : output;
|
||||
}
|
||||
private static int reverseNumberRec(int numberToReverse, int recursiveReversedNumber) {
|
||||
|
||||
if (numberToReverse > 0) {
|
||||
int mod = numberToReverse % 10;
|
||||
recursiveReversedNumber = recursiveReversedNumber * 10 + mod;
|
||||
numberToReverse /= 10;
|
||||
return reverseNumberRec(numberToReverse, recursiveReversedNumber);
|
||||
}
|
||||
|
||||
return recursiveReversedNumber;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.automorphicnumber;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AutomorphicNumberUnitTest {
|
||||
|
||||
@Test
|
||||
void givenANumber_whenPassed_thenShouldDetermineAutomorphicOrNot() {
|
||||
int number1 = 76; // automorphic
|
||||
int number2 = 16; // not automorphic
|
||||
assertTrue(AutomorphicNumber.isAutomorphicUsingLoop(number1));
|
||||
assertFalse(AutomorphicNumber.isAutomorphicUsingLoop(number2));
|
||||
assertTrue(AutomorphicNumber.isAutomorphicUsingMath(number1));
|
||||
assertFalse(AutomorphicNumber.isAutomorphicUsingMath(number2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.bigdecimalzero;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BigDecimalZeroChkUnitTest {
|
||||
private static final BigDecimal BD1 = new BigDecimal("0");
|
||||
private static final BigDecimal BD2 = new BigDecimal("0.0000");
|
||||
|
||||
@Test
|
||||
void givenBD_whenCheckedWithEquals_shouldCheckedAsZero() {
|
||||
assertThat(BigDecimal.ZERO.equals(BD1)).isTrue();
|
||||
|
||||
// in the article, we show the failure of the assertion below
|
||||
// assertThat(BigDecimal.ZERO.equals(BD2)).isTrue();
|
||||
|
||||
assertThat(BigDecimal.ZERO.equals(BD2)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBD_whenCheckedWithCompareTo_shouldCheckedAsZero() {
|
||||
assertThat(BigDecimal.ZERO.compareTo(BD1)).isSameAs(0);
|
||||
assertThat(BigDecimal.ZERO.compareTo(BD2)).isSameAs(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBD_whenCheckedWithSignum_shouldCheckedAsZero() {
|
||||
assertThat(BD1.signum()).isSameAs(0);
|
||||
assertThat(BD2.signum()).isSameAs(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.biginteger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class BigIntegerUnitTest {
|
||||
|
||||
@Test
|
||||
void givenPositiveAndNegativeAndZeroBigInteger_whenGetSigNumValue_shouldReturnOneAndMinusOneAndZero() {
|
||||
assertEquals(1, BigInteger.TEN.signum());
|
||||
assertEquals(-1, BigInteger.TEN.negate().signum());
|
||||
assertEquals(0, BigInteger.ZERO.signum());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenByteArrays_whenCreateBigInteger_shouldTranslateToTwosComplementBinary() {
|
||||
assertEquals(new BigInteger("1"), new BigInteger(new byte[]{0b1}));
|
||||
assertEquals(new BigInteger("2"), new BigInteger(new byte[]{0b10}));
|
||||
assertEquals(new BigInteger("4"), new BigInteger(new byte[]{0b100}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSingleByte_whenCreateBigIntegerAndDifferentSigNum_thenOppositeValues() {
|
||||
byte[] bytes = { (byte) -128 }; // 0b1000_0000
|
||||
|
||||
BigInteger positive = new BigInteger(1, bytes);
|
||||
BigInteger negative = new BigInteger(-1, bytes);
|
||||
|
||||
assertEquals(new BigInteger("128"), positive);
|
||||
assertEquals("10000000", positive.toString(2));
|
||||
|
||||
assertEquals(new BigInteger("-128"), negative);
|
||||
assertEquals("-10000000", negative.toString(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenZeroBigInteger_whenCheckMagnitude_thenIsEmpty() {
|
||||
assertEquals(0, BigInteger.ZERO.bitCount());
|
||||
assertEquals(BigInteger.ZERO, new BigInteger(0, new byte[]{}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void given63rdBitSet_whenCreateBigInteger_thenIsLargerThanLongByOne() {
|
||||
// first
|
||||
BigInteger bi1 = BigInteger.ZERO.setBit(63);
|
||||
String str1 = bi1.toString(2);
|
||||
|
||||
// second
|
||||
byte[] bytes = ByteBuffer.allocate(Long.BYTES).putLong(Long.MIN_VALUE).array();
|
||||
BigInteger bi2 = new BigInteger(1, bytes);
|
||||
String str2 = bi2.toString(2);
|
||||
|
||||
largerThanLongAssertionSet(bi1, str1);
|
||||
|
||||
assertEquals(bi1, bi2);
|
||||
|
||||
largerThanLongAssertionSet(bi2, str2);
|
||||
|
||||
}
|
||||
|
||||
private static void largerThanLongAssertionSet(BigInteger bi, String str)
|
||||
{
|
||||
assertEquals(64, bi.bitLength());
|
||||
assertEquals(1, bi.signum());
|
||||
assertEquals("9223372036854775808", bi.toString());
|
||||
assertEquals(BigInteger.ONE, bi.subtract(BigInteger.valueOf(Long.MAX_VALUE)));
|
||||
|
||||
assertEquals(64, str.length());
|
||||
assertTrue(str.matches("^10{63}$")); // 1000 0000
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.booleantoint;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class BooleanToIntUnitTest {
|
||||
@Test
|
||||
void givenBooleanPrimitiveValue_ThenReturnInt() {
|
||||
assertEquals(1, BooleanToInt.booleanPrimitiveToInt(true));
|
||||
assertEquals(0, BooleanToInt.booleanPrimitiveToInt(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBooleanPrimitiveValue_ThenReturnIntTernary() {
|
||||
assertEquals(1, BooleanToInt.booleanPrimitiveToIntTernary(true));
|
||||
assertEquals(0, BooleanToInt.booleanPrimitiveToIntTernary(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBooleanObject_ThenReturnInt() {
|
||||
assertEquals(0, BooleanToInt.booleanObjectToInt(false));
|
||||
assertEquals(1, BooleanToInt.booleanObjectToInt(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBooleanObject_ThenReturnIntInverse() {
|
||||
assertEquals(0, BooleanToInt.booleanObjectToIntInverse(false));
|
||||
assertEquals(1, BooleanToInt.booleanObjectToIntInverse(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBooleanObject_ThenReturnIntUsingClassMethod() {
|
||||
assertEquals(0, BooleanToInt.booleanObjectMethodToInt(false));
|
||||
assertEquals(1, BooleanToInt.booleanObjectMethodToInt(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBooleanObject_ThenReturnIntUsingClassMethodInverse() {
|
||||
assertEquals(0, BooleanToInt.booleanObjectMethodToIntInverse(false));
|
||||
assertEquals(1, BooleanToInt.booleanObjectMethodToIntInverse(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBoolean_ThenReturnIntUsingBooleanUtils() {
|
||||
assertEquals(0, BooleanToInt.booleanUtilsToInt(false));
|
||||
assertEquals(1, BooleanToInt.booleanUtilsToInt(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenBoolean_ThenReturnIntUsingBitwiseOperators() {
|
||||
assertEquals(0, BooleanToInt.bitwiseBooleanToInt(false));
|
||||
assertEquals(1, BooleanToInt.bitwiseBooleanToInt(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.convertLongToInt;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ConvertLongToIntUnitTest {
|
||||
|
||||
@Test
|
||||
void longToInt() {
|
||||
long number = 186762L;
|
||||
int expected = 186762;
|
||||
|
||||
assertEquals(expected, ConvertLongToInt.longToIntCast(number));
|
||||
assertEquals(expected, ConvertLongToInt.longToIntJavaWithMath(number));
|
||||
assertEquals(expected, ConvertLongToInt.longToIntJavaWithLambda(number));
|
||||
assertEquals(expected, ConvertLongToInt.longToIntBoxingValues(number));
|
||||
assertEquals(expected, ConvertLongToInt.longToIntGuava(number));
|
||||
assertEquals(expected, ConvertLongToInt.longToIntGuavaSaturated(number));
|
||||
assertEquals(expected, ConvertLongToInt.longToIntWithBigDecimal(number));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.humanreadablebytes;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FileSizeFormatUtilUnitTest {
|
||||
private final static Map<Long, String> DATA_MAP = new HashMap<Long, String>() {{
|
||||
put(0L, "0 Bytes");
|
||||
put(1023L, "1023 Bytes");
|
||||
put(1024L, "1 KB");
|
||||
put(12_345L, "12.06 KB");
|
||||
put(10_123_456L, "9.65 MB");
|
||||
put(10_123_456_798L, "9.43 GB");
|
||||
put(1_777_777_777_777_777_777L, "1.54 EB");
|
||||
}};
|
||||
|
||||
@Test
|
||||
public void givenBytes_whenCalltoHumanReadableMethod_thenGetExpectedResults() {
|
||||
DATA_MAP.forEach((in, expected) -> Assert.assertEquals(expected, FileSizeFormatUtil.toHumanReadable(in)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBytes_whenCalltoHumanReadableWithEnumMethod_thenGetExpectedResults() {
|
||||
DATA_MAP.forEach((in, expected) -> Assert.assertEquals(expected, FileSizeFormatUtil.toHumanReadableWithEnum(in)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBytes_whenCalltoHumanReadableByLeadingZeros_thenGetExpectedResults() {
|
||||
DATA_MAP.forEach((in, expected) -> Assert.assertEquals(expected, FileSizeFormatUtil.toHumanReadableByNumOfLeadingZeros(in)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBytes_whenCalltoHumanReadableByFileUtils_thenOutputExpectedResults() {
|
||||
DATA_MAP.forEach((in, expected) -> System.out.println(in + " bytes -> " + FileUtils.byteCountToDisplaySize(in)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.number_0xff;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class Number0xffUnitTest {
|
||||
|
||||
@Test
|
||||
public void test0xFFAssignedToInteger() {
|
||||
int x = 0xff;
|
||||
int expectedValue = 255;
|
||||
assertEquals(expectedValue, x);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test0xFFAssignedToByte() {
|
||||
byte y = (byte) 0xff;
|
||||
int expectedValue = -1;
|
||||
assertEquals(expectedValue, y);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenColor_whenGetRedColor_thenExtractRedColor() {
|
||||
int rgba = 272214023;
|
||||
int expectedValue = 16;
|
||||
assertEquals(expectedValue, Number0xff.getRedColor(rgba));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenColor_whenGetGreenColor_thenExtractGreenColor() {
|
||||
int rgba = 272214023;
|
||||
int expectedValue = 57;
|
||||
assertEquals(expectedValue, Number0xff.getGreenColor(rgba));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenColor_whenGetBlueColor_thenExtractBlueColor() {
|
||||
int rgba = 272214023;
|
||||
int expectedValue = 168;
|
||||
assertEquals(expectedValue, Number0xff.getBlueColor(rgba));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenColor_whenGetAlfa_thenExtractAlfa() {
|
||||
int rgba = 272214023;
|
||||
int expectedValue = 7;
|
||||
assertEquals(expectedValue, Number0xff.getAlfa(rgba));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.perfectsquare;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PerfectSquareUnitTest {
|
||||
|
||||
@Test
|
||||
public void testIsNumberPerfectSquare() {
|
||||
long n = 18676209273604L; // 18676209273604 = 43621598 * 43621598
|
||||
boolean expectedValue = true;
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareWithOptimization(n));
|
||||
|
||||
n = 549790047707L; // prime number
|
||||
expectedValue = false;
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareWithOptimization(n));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.probability;
|
||||
|
||||
import org.assertj.core.data.Offset;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RandomInvokerUnitTest {
|
||||
@Test
|
||||
public void givenProbability_whenInvoked_invokeWithProbability() {
|
||||
RandomInvoker randomInvoker = new RandomInvoker();
|
||||
|
||||
int numberOfSamples = 1_000_000;
|
||||
int probability = 10;
|
||||
int howManyTimesInvoked = Stream.generate(() -> randomInvoker.withProbability(() -> 1, () -> 0, probability))
|
||||
.limit(numberOfSamples)
|
||||
.mapToInt(e -> e).sum();
|
||||
int monteCarloProbability = (howManyTimesInvoked * 100) / numberOfSamples;
|
||||
|
||||
assertThat(monteCarloProbability).isCloseTo(probability, Offset.offset(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.reversenumber;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ReverseNumberUnitTest {
|
||||
|
||||
private static final int ORIGINAL_NUMBER = 123456789;
|
||||
private static final int REVERSED_NUMBER = 987654321;
|
||||
|
||||
@Test
|
||||
void whenReverseNumberWhileLoop_thenOriginalEqualToReverse() {
|
||||
Assertions.assertThat(ReverseNumber.reverseNumberWhileLoop(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReverseNumberForLoop_thenOriginalEqualToReverse() {
|
||||
Assertions.assertThat(ReverseNumber.reverseNumberForLoop(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReverseNumberRec_thenOriginalEqualToReverse() {
|
||||
Assertions.assertThat(ReverseNumber.reverseNumberRecWrapper(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReverseNegativeNumber_thenNumberShouldReverse() {
|
||||
Assertions.assertThat(ReverseNumber.reverseNumberWhileLoop(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1);
|
||||
Assertions.assertThat(ReverseNumber.reverseNumberForLoop(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1);
|
||||
Assertions.assertThat(ReverseNumber.reverseNumberRecWrapper(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user