BAEL-640: Guide to Mathematical Operations with Guava (#1754)
* Added the Spring Core dependency Injection methods source code * Added test cases for dependency Injection type demo * Added Guava Math operation test * reverted the evaluation article code * reverted evaluation article * Removed evaluation article code
This commit is contained in:
parent
a2b327cc9d
commit
4aff9d33d9
|
@ -0,0 +1,112 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.math.BigIntegerMath;
|
||||
|
||||
public class GuavaBigIntegerMathTest {
|
||||
|
||||
@Test
|
||||
public void whenPerformBinomialOnTwoIntValues_shouldReturnResult() {
|
||||
BigInteger result = BigIntegerMath.binomial(6, 3);
|
||||
assertEquals(new BigInteger("20"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenProformCeilPowOfTwoBigIntegerValues_shouldReturnResult() {
|
||||
BigInteger result = BigIntegerMath.ceilingPowerOfTwo(new BigInteger("20"));
|
||||
assertEquals(new BigInteger("32"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoBigIntegerValues_shouldDivideThemAndReturnTheResultForCeilingRounding() {
|
||||
BigInteger result = BigIntegerMath.divide(new BigInteger("10"), new BigInteger("3"), RoundingMode.CEILING);
|
||||
assertEquals(new BigInteger("4"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoBigIntegerValues_shouldDivideThemAndReturnTheResultForFloorRounding() {
|
||||
BigInteger result = BigIntegerMath.divide(new BigInteger("10"), new BigInteger("3"), RoundingMode.FLOOR);
|
||||
assertEquals(new BigInteger("3"), result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenDivideTwoBigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
BigIntegerMath.divide(new BigInteger("10"), new BigInteger("3"), RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorailInteger_shouldFactorialThemAndReturnTheResultIfInIntRange() {
|
||||
BigInteger result = BigIntegerMath.factorial(5);
|
||||
assertEquals(new BigInteger("120"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFloorPowerOfInteger_shouldReturnValue() {
|
||||
BigInteger result = BigIntegerMath.floorPowerOfTwo(new BigInteger("30"));
|
||||
assertEquals(new BigInteger("16"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowOfInteger_shouldReturnTrueIfPowerOfTwo() {
|
||||
boolean result = BigIntegerMath.isPowerOfTwo(new BigInteger("16"));
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10BigIntegerValues_shouldLog10ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = BigIntegerMath.log10(new BigInteger("30"), RoundingMode.CEILING);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10BigIntegerValues_shouldog10ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = BigIntegerMath.log10(new BigInteger("30"), RoundingMode.FLOOR);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog10BigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
BigIntegerMath.log10(new BigInteger("30"), RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2BigIntegerValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = BigIntegerMath.log2(new BigInteger("30"), RoundingMode.CEILING);
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2BigIntegerValues_shouldog2ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = BigIntegerMath.log2(new BigInteger("30"), RoundingMode.FLOOR);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog2BigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
BigIntegerMath.log2(new BigInteger("30"), RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrtBigIntegerValues_shouldSqrtThemAndReturnTheResultForCeilingRounding() {
|
||||
BigInteger result = BigIntegerMath.sqrt(new BigInteger("30"), RoundingMode.CEILING);
|
||||
assertEquals(new BigInteger("6"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrtBigIntegerValues_shouldSqrtThemAndReturnTheResultForFloorRounding() {
|
||||
BigInteger result = BigIntegerMath.sqrt(new BigInteger("30"), RoundingMode.FLOOR);
|
||||
assertEquals(new BigInteger("5"), result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenSqrtBigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
BigIntegerMath.sqrt(new BigInteger("30"), RoundingMode.UNNECESSARY);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.math.DoubleMath;
|
||||
import com.google.common.math.IntMath;
|
||||
|
||||
public class GuavaDoubleMathTest {
|
||||
|
||||
@Test
|
||||
public void whenFactorailDouble_shouldFactorialThemAndReturnTheResultIfInDoubleRange() {
|
||||
double result = DoubleMath.factorial(5);
|
||||
assertEquals(120, result, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorailDouble_shouldFactorialThemAndReturnDoubkeInfIfNotInDoubletRange() {
|
||||
double result = DoubleMath.factorial(Integer.MAX_VALUE);
|
||||
assertEquals(Double.POSITIVE_INFINITY, result, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFuzzyCompareDouble_shouldReturnZeroIfInRange() {
|
||||
int result = DoubleMath.fuzzyCompare(4, 4.05, 0.6);
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFuzzyCompareDouble_shouldReturnNonZeroIfNotInRange() {
|
||||
int result = DoubleMath.fuzzyCompare(4, 5, 0.1);
|
||||
assertEquals(-1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFuzzyEqualDouble_shouldReturnZeroIfInRange() {
|
||||
boolean result = DoubleMath.fuzzyEquals(4, 4.05, 0.6);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFuzzyEqualDouble_shouldReturnNonZeroIfNotInRange() {
|
||||
boolean result = DoubleMath.fuzzyEquals(4, 5, 0.1);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMathematicalIntDouble_shouldReturnTrueIfInRange() {
|
||||
boolean result = DoubleMath.isMathematicalInteger(5);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMathematicalIntDouble_shouldReturnFalseIfNotInRange() {
|
||||
boolean result = DoubleMath.isMathematicalInteger(5.2);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowerOfTwoDouble_shouldReturnTrueIfIsPowerOfTwo() {
|
||||
boolean result = DoubleMath.isMathematicalInteger(4);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowerOfTwoDouble_shouldReturnFalseIsNotPowerOfTwoe() {
|
||||
boolean result = DoubleMath.isMathematicalInteger(5.2);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2Double_shouldReturnResult() {
|
||||
double result = DoubleMath.log2(4);
|
||||
assertEquals(2, result, 0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenLog2DoubleValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = DoubleMath.log2(30, RoundingMode.CEILING);
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2DoubleValues_shouldog2ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = DoubleMath.log2(30, RoundingMode.FLOOR);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog2DoubleValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
DoubleMath.log2(30, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,274 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import com.google.common.math.IntMath;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GuavaIntMathTest {
|
||||
|
||||
@Test
|
||||
public void whenPerformBinomialOnTwoIntegerValues_shouldReturnResultIfUnderInt() {
|
||||
int result = IntMath.binomial(6, 3);
|
||||
assertEquals(20, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPerformBinomialOnTwoIntegerValues_shouldReturnIntMaxIfUnderInt() {
|
||||
int result = IntMath.binomial(Integer.MAX_VALUE, 3);
|
||||
assertEquals(Integer.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenProformCeilPowOfTwoIntegerValues_shouldReturnResult() {
|
||||
int result = IntMath.ceilingPowerOfTwo(20);
|
||||
assertEquals(32, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedAddTwoIntegerValues_shouldAddThemAndReturnTheSumIfNotOverflow() {
|
||||
int result = IntMath.checkedAdd(1, 2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void gwhenCheckedAddTwoIntegerValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
IntMath.checkedAdd(Integer.MAX_VALUE, 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnTheResultIfNotOverflow() {
|
||||
int result = IntMath.checkedMultiply(1, 2);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void gwhenCheckedMultiplyTwoIntegerValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
IntMath.checkedMultiply(Integer.MAX_VALUE, 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedPowTwoIntegerValues_shouldPowThemAndReturnTheResultIfNotOverflow() {
|
||||
int result = IntMath.checkedPow(2, 3);
|
||||
assertEquals(8, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void gwhenCheckedPowTwoIntegerValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
IntMath.checkedPow(Integer.MAX_VALUE, 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedSubstractTwoIntegerValues_shouldSubstractThemAndReturnTheResultIfNotOverflow() {
|
||||
int result = IntMath.checkedSubtract(4, 1);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void gwhenCheckedSubstractTwoIntegerValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
IntMath.checkedSubtract(Integer.MAX_VALUE, -100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoIntegerValues_shouldDivideThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = IntMath.divide(10, 3, RoundingMode.CEILING);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoIntegerValues_shouldDivideThemAndReturnTheResultForFloorRounding() {
|
||||
int result = IntMath.divide(10, 3, RoundingMode.FLOOR);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenDivideTwoIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
IntMath.divide(10, 3, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorailInteger_shouldFactorialThemAndReturnTheResultIfInIntRange() {
|
||||
int result = IntMath.factorial(5);
|
||||
assertEquals(120, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorailInteger_shouldFactorialThemAndReturnIntMaxIfNotInIntRange() {
|
||||
int result = IntMath.factorial(Integer.MAX_VALUE);
|
||||
assertEquals(Integer.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFloorPowerOfInteger_shouldReturnValue() {
|
||||
int result = IntMath.floorPowerOfTwo(30);
|
||||
assertEquals(16, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGcdOfTwoIntegers_shouldReturnValue() {
|
||||
int result = IntMath.gcd(30, 40);
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowOfInteger_shouldReturnTrueIfPowerOfTwo() {
|
||||
boolean result = IntMath.isPowerOfTwo(16);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowOfInteger_shouldReturnFalseeIfNotPowerOfTwo() {
|
||||
boolean result = IntMath.isPowerOfTwo(20);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPrineOfInteger_shouldReturnFalseeIfNotPrime() {
|
||||
boolean result = IntMath.isPrime(20);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10IntegerValues_shouldLog10ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = IntMath.log10(30, RoundingMode.CEILING);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10IntegerValues_shouldog10ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = IntMath.log10(30, RoundingMode.FLOOR);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog10IntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
IntMath.log10(30, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2IntegerValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = IntMath.log2(30, RoundingMode.CEILING);
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2IntegerValues_shouldog2ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = IntMath.log2(30, RoundingMode.FLOOR);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog2IntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
IntMath.log2(30, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMeanTwoIntegerValues_shouldMeanThemAndReturnTheResult() {
|
||||
int result = IntMath.mean(30, 20);
|
||||
assertEquals(25, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModTwoIntegerValues_shouldModThemAndReturnTheResult() {
|
||||
int result = IntMath.mod(30, 4);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPowTwoIntegerValues_shouldPowThemAndReturnTheResult() {
|
||||
int result = IntMath.pow(6, 4);
|
||||
assertEquals(1296, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedAddTwoIntegerValues_shouldAddThemAndReturnTheResult() {
|
||||
int result = IntMath.saturatedAdd(6, 4);
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedAddTwoIntegerValues_shouldAddThemAndReturnIntMaxIfOverflow() {
|
||||
int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 1000);
|
||||
assertEquals(Integer.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedAddTwoIntegerValues_shouldAddThemAndReturnIntMinIfUnderflow() {
|
||||
int result = IntMath.saturatedAdd(Integer.MIN_VALUE, -1000);
|
||||
assertEquals(Integer.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnTheResult() {
|
||||
int result = IntMath.saturatedMultiply(6, 4);
|
||||
assertEquals(24, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnIntMaxIfOverflow() {
|
||||
int result = IntMath.saturatedMultiply(Integer.MAX_VALUE, 1000);
|
||||
assertEquals(Integer.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedMultiplyTwoIntegerValues_shouldMultiplyThemAndReturnIntMinIfUnderflow() {
|
||||
int result = IntMath.saturatedMultiply(Integer.MIN_VALUE, 1000);
|
||||
assertEquals(Integer.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedPowTwoIntegerValues_shouldPowThemAndReturnTheResult() {
|
||||
int result = IntMath.saturatedPow(6, 2);
|
||||
assertEquals(36, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedPowTwoIntegerValues_shouldPowThemAndReturnIntMaxIfOverflow() {
|
||||
int result = IntMath.saturatedPow(Integer.MAX_VALUE, 2);
|
||||
assertEquals(Integer.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedPowTwoIntegerValues_shouldPowThemAndReturnIntMinIfUnderflow() {
|
||||
int result = IntMath.saturatedPow(Integer.MIN_VALUE, 3);
|
||||
assertEquals(Integer.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedSubstractTwoIntegerValues_shouldSubstractThemAndReturnTheResult() {
|
||||
int result = IntMath.saturatedSubtract(6, 2);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedSubstractTwoIntegerValues_shouldSubstractwThemAndReturnIntMaxIfOverflow() {
|
||||
int result = IntMath.saturatedSubtract(Integer.MAX_VALUE, -2);
|
||||
assertEquals(Integer.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedSubstractTwoIntegerValues_shouldSubstractThemAndReturnIntMinIfUnderflow() {
|
||||
int result = IntMath.saturatedSubtract(Integer.MIN_VALUE, 3);
|
||||
assertEquals(Integer.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrtIntegerValues_shouldSqrtThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = IntMath.sqrt(30, RoundingMode.CEILING);
|
||||
assertEquals(6, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrtIntegerValues_shouldSqrtThemAndReturnTheResultForFloorRounding() {
|
||||
int result = IntMath.sqrt(30, RoundingMode.FLOOR);
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenSqrtIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
IntMath.sqrt(30, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,268 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import com.google.common.math.LongMath;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GuavaLongMathTest {
|
||||
|
||||
@Test
|
||||
public void whenPerformBinomialOnTwoLongValues_shouldReturnResultIfUnderLong() {
|
||||
long result = LongMath.binomial(6, 3);
|
||||
assertEquals(20L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenProformCeilPowOfTwoLongValues_shouldReturnResult() {
|
||||
long result = LongMath.ceilingPowerOfTwo(20L);
|
||||
assertEquals(32L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedAddTwoLongValues_shouldAddThemAndReturnTheSumIfNotOverflow() {
|
||||
long result = LongMath.checkedAdd(1L, 2L);
|
||||
assertEquals(3L, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenCheckedAddTwoLongValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
LongMath.checkedAdd(Long.MAX_VALUE, 100L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedMultiplyTwoLongValues_shouldMultiplyThemAndReturnTheResultIfNotOverflow() {
|
||||
long result = LongMath.checkedMultiply(3L, 2L);
|
||||
assertEquals(6L, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenCheckedMultiplyTwoLongValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
LongMath.checkedMultiply(Long.MAX_VALUE, 100L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedPowTwoLongValues_shouldPowThemAndReturnTheResultIfNotOverflow() {
|
||||
long result = LongMath.checkedPow(2L, 3);
|
||||
assertEquals(8L, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void gwhenCheckedPowTwoLongValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
LongMath.checkedPow(Long.MAX_VALUE, 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckedSubstractTwoLongValues_shouldSubstractThemAndReturnTheResultIfNotOverflow() {
|
||||
long result = LongMath.checkedSubtract(4L, 1L);
|
||||
assertEquals(3L, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void gwhenCheckedSubstractTwoLongValues_shouldThrowArithmeticExceptionIfOverflow() {
|
||||
LongMath.checkedSubtract(Long.MAX_VALUE, -100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoLongValues_shouldDivideThemAndReturnTheResultForCeilingRounding() {
|
||||
long result = LongMath.divide(10L, 3L, RoundingMode.CEILING);
|
||||
assertEquals(4L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideTwoLongValues_shouldDivideThemAndReturnTheResultForFloorRounding() {
|
||||
long result = LongMath.divide(10L, 3L, RoundingMode.FLOOR);
|
||||
assertEquals(3L, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenDivideTwoLongValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
LongMath.divide(10L, 3L, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorailLong_shouldFactorialThemAndReturnTheResultIfInIntRange() {
|
||||
long result = LongMath.factorial(5);
|
||||
assertEquals(120L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorailLong_shouldFactorialThemAndReturnIntMaxIfNotInIntRange() {
|
||||
long result = LongMath.factorial(Integer.MAX_VALUE);
|
||||
assertEquals(Long.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFloorPowerOfLong_shouldReturnValue() {
|
||||
long result = LongMath.floorPowerOfTwo(30L);
|
||||
assertEquals(16L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGcdOfTwoLongs_shouldReturnValue() {
|
||||
long result = LongMath.gcd(30L, 40L);
|
||||
assertEquals(10L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowOfLong_shouldReturnTrueIfPowerOfTwo() {
|
||||
boolean result = LongMath.isPowerOfTwo(16L);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPowOfLong_shouldReturnFalseeIfNotPowerOfTwo() {
|
||||
boolean result = LongMath.isPowerOfTwo(20L);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIsPrineOfLong_shouldReturnFalseeIfNotPrime() {
|
||||
boolean result = LongMath.isPrime(20L);
|
||||
assertFalse(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10LongValues_shouldLog10ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = LongMath.log10(30L, RoundingMode.CEILING);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10LongValues_shouldog10ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = LongMath.log10(30L, RoundingMode.FLOOR);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog10LongValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
LongMath.log10(30L, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2LongValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() {
|
||||
int result = LongMath.log2(30L, RoundingMode.CEILING);
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog2LongValues_shouldog2ThemAndReturnTheResultForFloorRounding() {
|
||||
int result = LongMath.log2(30L, RoundingMode.FLOOR);
|
||||
assertEquals(4, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenLog2LongValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
LongMath.log2(30L, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMeanTwoLongValues_shouldMeanThemAndReturnTheResult() {
|
||||
long result = LongMath.mean(30L, 20L);
|
||||
assertEquals(25L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModLongAndIntegerValues_shouldModThemAndReturnTheResult() {
|
||||
int result = LongMath.mod(30L, 4);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModTwoLongValues_shouldModThemAndReturnTheResult() {
|
||||
long result = LongMath.mod(30L, 4L);
|
||||
assertEquals(2L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPowTwoLongValues_shouldPowThemAndReturnTheResult() {
|
||||
long result = LongMath.pow(6L, 4);
|
||||
assertEquals(1296L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedAddTwoLongValues_shouldAddThemAndReturnTheResult() {
|
||||
long result = LongMath.saturatedAdd(6L, 4L);
|
||||
assertEquals(10L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedAddTwoLongValues_shouldAddThemAndReturnIntMaxIfOverflow() {
|
||||
long result = LongMath.saturatedAdd(Long.MAX_VALUE, 1000L);
|
||||
assertEquals(Long.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedAddTwoLongValues_shouldAddThemAndReturnIntMinIfUnderflow() {
|
||||
long result = LongMath.saturatedAdd(Long.MIN_VALUE, -1000);
|
||||
assertEquals(Long.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedMultiplyTwoLongValues_shouldMultiplyThemAndReturnTheResult() {
|
||||
long result = LongMath.saturatedMultiply(6L, 4L);
|
||||
assertEquals(24L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedMultiplyTwoLongValues_shouldMultiplyThemAndReturnIntMaxIfOverflow() {
|
||||
long result = LongMath.saturatedMultiply(Long.MAX_VALUE, 1000L);
|
||||
assertEquals(Long.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedPowTwoLongValues_shouldPowThemAndReturnTheResult() {
|
||||
long result = LongMath.saturatedPow(6L, 2);
|
||||
assertEquals(36L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedPowTwoLongValues_shouldPowThemAndReturnIntMaxIfOverflow() {
|
||||
long result = LongMath.saturatedPow(Long.MAX_VALUE, 2);
|
||||
assertEquals(Long.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedPowTwoLongValues_shouldPowThemAndReturnIntMinIfUnderflow() {
|
||||
long result = LongMath.saturatedPow(Long.MIN_VALUE, 3);
|
||||
assertEquals(Long.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedSubstractTwoLongValues_shouldSubstractThemAndReturnTheResult() {
|
||||
long result = LongMath.saturatedSubtract(6L, 2L);
|
||||
assertEquals(4L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedSubstractTwoLongValues_shouldSubstractwThemAndReturnIntMaxIfOverflow() {
|
||||
long result = LongMath.saturatedSubtract(Long.MAX_VALUE, -2L);
|
||||
assertEquals(Long.MAX_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSaturatedSubstractTwoLongValues_shouldSubstractThemAndReturnIntMinIfUnderflow() {
|
||||
long result = LongMath.saturatedSubtract(Long.MIN_VALUE, 3L);
|
||||
assertEquals(Long.MIN_VALUE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrtLongValues_shouldSqrtThemAndReturnTheResultForCeilingRounding() {
|
||||
long result = LongMath.sqrt(30L, RoundingMode.CEILING);
|
||||
assertEquals(6L, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrtLongValues_shouldSqrtThemAndReturnTheResultForFloorRounding() {
|
||||
long result = LongMath.sqrt(30L, RoundingMode.FLOOR);
|
||||
assertEquals(5L, result);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenSqrtLongValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
|
||||
LongMath.sqrt(30L, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue