Skip to content

Commit 4aff9d3

Browse files
amilabanukaadamd1985
authored andcommitted
BAEL-640: Guide to Mathematical Operations with Guava (eugenp#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
1 parent a2b327c commit 4aff9d3

4 files changed

Lines changed: 752 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.baeldung.guava;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.math.RoundingMode;
6+
7+
import org.junit.Test;
8+
9+
import com.google.common.math.DoubleMath;
10+
import com.google.common.math.IntMath;
11+
12+
public class GuavaDoubleMathTest {
13+
14+
@Test
15+
public void whenFactorailDouble_shouldFactorialThemAndReturnTheResultIfInDoubleRange() {
16+
double result = DoubleMath.factorial(5);
17+
assertEquals(120, result, 0);
18+
}
19+
20+
@Test
21+
public void whenFactorailDouble_shouldFactorialThemAndReturnDoubkeInfIfNotInDoubletRange() {
22+
double result = DoubleMath.factorial(Integer.MAX_VALUE);
23+
assertEquals(Double.POSITIVE_INFINITY, result, 0);
24+
}
25+
26+
@Test
27+
public void whenFuzzyCompareDouble_shouldReturnZeroIfInRange() {
28+
int result = DoubleMath.fuzzyCompare(4, 4.05, 0.6);
29+
assertEquals(0, result);
30+
}
31+
32+
@Test
33+
public void whenFuzzyCompareDouble_shouldReturnNonZeroIfNotInRange() {
34+
int result = DoubleMath.fuzzyCompare(4, 5, 0.1);
35+
assertEquals(-1, result);
36+
}
37+
38+
@Test
39+
public void whenFuzzyEqualDouble_shouldReturnZeroIfInRange() {
40+
boolean result = DoubleMath.fuzzyEquals(4, 4.05, 0.6);
41+
assertTrue(result);
42+
}
43+
44+
@Test
45+
public void whenFuzzyEqualDouble_shouldReturnNonZeroIfNotInRange() {
46+
boolean result = DoubleMath.fuzzyEquals(4, 5, 0.1);
47+
assertFalse(result);
48+
}
49+
50+
@Test
51+
public void whenMathematicalIntDouble_shouldReturnTrueIfInRange() {
52+
boolean result = DoubleMath.isMathematicalInteger(5);
53+
assertTrue(result);
54+
}
55+
56+
@Test
57+
public void whenMathematicalIntDouble_shouldReturnFalseIfNotInRange() {
58+
boolean result = DoubleMath.isMathematicalInteger(5.2);
59+
assertFalse(result);
60+
}
61+
62+
@Test
63+
public void whenIsPowerOfTwoDouble_shouldReturnTrueIfIsPowerOfTwo() {
64+
boolean result = DoubleMath.isMathematicalInteger(4);
65+
assertTrue(result);
66+
}
67+
68+
@Test
69+
public void whenIsPowerOfTwoDouble_shouldReturnFalseIsNotPowerOfTwoe() {
70+
boolean result = DoubleMath.isMathematicalInteger(5.2);
71+
assertFalse(result);
72+
}
73+
74+
@Test
75+
public void whenLog2Double_shouldReturnResult() {
76+
double result = DoubleMath.log2(4);
77+
assertEquals(2, result, 0);
78+
}
79+
80+
81+
@Test
82+
public void whenLog2DoubleValues_shouldLog2ThemAndReturnTheResultForCeilingRounding() {
83+
int result = DoubleMath.log2(30, RoundingMode.CEILING);
84+
assertEquals(5, result);
85+
}
86+
87+
@Test
88+
public void whenLog2DoubleValues_shouldog2ThemAndReturnTheResultForFloorRounding() {
89+
int result = DoubleMath.log2(30, RoundingMode.FLOOR);
90+
assertEquals(4, result);
91+
}
92+
93+
@Test(expected = ArithmeticException.class)
94+
public void whenLog2DoubleValues_shouldThrowArithmeticExceptionIfRoundingNotDefinedButNecessary() {
95+
DoubleMath.log2(30, RoundingMode.UNNECESSARY);
96+
}
97+
98+
}

0 commit comments

Comments
 (0)