|
| 1 | +package org.baeldung.guava; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.math.BigInteger; |
| 7 | +import java.math.RoundingMode; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.google.common.math.BigIntegerMath; |
| 12 | + |
| 13 | +public class GuavaBigIntegerMathTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void whenPerformBinomialOnTwoIntValues_shouldReturnResult() { |
| 17 | + BigInteger result = BigIntegerMath.binomial(6, 3); |
| 18 | + assertEquals(new BigInteger("20"), result); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void whenProformCeilPowOfTwoBigIntegerValues_shouldReturnResult() { |
| 23 | + BigInteger result = BigIntegerMath.ceilingPowerOfTwo(new BigInteger("20")); |
| 24 | + assertEquals(new BigInteger("32"), result); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenDivideTwoBigIntegerValues_shouldDivideThemAndReturnTheResultForCeilingRounding() { |
| 29 | + BigInteger result = BigIntegerMath.divide(new BigInteger("10"), new BigInteger("3"), RoundingMode.CEILING); |
| 30 | + assertEquals(new BigInteger("4"), result); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenDivideTwoBigIntegerValues_shouldDivideThemAndReturnTheResultForFloorRounding() { |
| 35 | + BigInteger result = BigIntegerMath.divide(new BigInteger("10"), new BigInteger("3"), RoundingMode.FLOOR); |
| 36 | + assertEquals(new BigInteger("3"), result); |
| 37 | + } |
| 38 | + |
| 39 | + @Test(expected = ArithmeticException.class) |
| 40 | + public void whenDivideTwoBigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() { |
| 41 | + BigIntegerMath.divide(new BigInteger("10"), new BigInteger("3"), RoundingMode.UNNECESSARY); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void whenFactorailInteger_shouldFactorialThemAndReturnTheResultIfInIntRange() { |
| 46 | + BigInteger result = BigIntegerMath.factorial(5); |
| 47 | + assertEquals(new BigInteger("120"), result); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenFloorPowerOfInteger_shouldReturnValue() { |
| 52 | + BigInteger result = BigIntegerMath.floorPowerOfTwo(new BigInteger("30")); |
| 53 | + assertEquals(new BigInteger("16"), result); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void whenIsPowOfInteger_shouldReturnTrueIfPowerOfTwo() { |
| 58 | + boolean result = BigIntegerMath.isPowerOfTwo(new BigInteger("16")); |
| 59 | + assertTrue(result); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenLog10BigIntegerValues_shouldLog10ThemAndReturnTheResultForCeilingRounding() { |
| 64 | + int result = BigIntegerMath.log10(new BigInteger("30"), RoundingMode.CEILING); |
| 65 | + assertEquals(2, result); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void whenLog10BigIntegerValues_shouldog10ThemAndReturnTheResultForFloorRounding() { |
| 70 | + int result = BigIntegerMath.log10(new BigInteger("30"), RoundingMode.FLOOR); |
| 71 | + assertEquals(1, result); |
| 72 | + } |
| 73 | + |
| 74 | + @Test(expected = ArithmeticException.class) |
| 75 | + public void whenLog10BigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() { |
| 76 | + BigIntegerMath.log10(new BigInteger("30"), RoundingMode.UNNECESSARY); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void whenLog2BigIntegerValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() { |
| 81 | + int result = BigIntegerMath.log2(new BigInteger("30"), RoundingMode.CEILING); |
| 82 | + assertEquals(5, result); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void whenLog2BigIntegerValues_shouldog2ThemAndReturnTheResultForFloorRounding() { |
| 87 | + int result = BigIntegerMath.log2(new BigInteger("30"), RoundingMode.FLOOR); |
| 88 | + assertEquals(4, result); |
| 89 | + } |
| 90 | + |
| 91 | + @Test(expected = ArithmeticException.class) |
| 92 | + public void whenLog2BigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() { |
| 93 | + BigIntegerMath.log2(new BigInteger("30"), RoundingMode.UNNECESSARY); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void whenSqrtBigIntegerValues_shouldSqrtThemAndReturnTheResultForCeilingRounding() { |
| 98 | + BigInteger result = BigIntegerMath.sqrt(new BigInteger("30"), RoundingMode.CEILING); |
| 99 | + assertEquals(new BigInteger("6"), result); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void whenSqrtBigIntegerValues_shouldSqrtThemAndReturnTheResultForFloorRounding() { |
| 104 | + BigInteger result = BigIntegerMath.sqrt(new BigInteger("30"), RoundingMode.FLOOR); |
| 105 | + assertEquals(new BigInteger("5"), result); |
| 106 | + } |
| 107 | + |
| 108 | + @Test(expected = ArithmeticException.class) |
| 109 | + public void whenSqrtBigIntegerValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() { |
| 110 | + BigIntegerMath.sqrt(new BigInteger("30"), RoundingMode.UNNECESSARY); |
| 111 | + } |
| 112 | +} |
0 commit comments