1+ package org .baeldung .guava ;
2+
3+ import com .google .common .math .DoubleMath ;
4+ import com .google .common .math .IntMath ;
5+ import org .junit .Rule ;
6+ import org .junit .Test ;
7+ import org .junit .rules .ExpectedException ;
8+
9+ import java .math .BigInteger ;
10+ import java .math .RoundingMode ;
11+
12+ import static org .hamcrest .core .IsEqual .equalTo ;
13+ import static org .junit .Assert .assertFalse ;
14+ import static org .junit .Assert .assertThat ;
15+ import static org .junit .Assert .assertTrue ;
16+
17+ public class GuavaMathTest {
18+ @ Test
19+ public void testIntMathAdd () {
20+ try {
21+ IntMath .checkedAdd (Integer .MAX_VALUE , 1 );
22+ assertTrue (false );
23+ } catch (ArithmeticException e ) {
24+ assertTrue (true );
25+ }
26+
27+ try {
28+ IntMath .checkedAdd (Integer .MIN_VALUE , -1 );
29+ assertTrue (false );
30+ } catch (ArithmeticException e ) {
31+ assertTrue (true );
32+ }
33+
34+ int result1 = IntMath .checkedAdd (2 , 1 );
35+ assertThat (result1 , equalTo (3 ));
36+
37+ int result2 = IntMath .saturatedAdd (Integer .MAX_VALUE , 100 );
38+ assertThat (result2 , equalTo (Integer .MAX_VALUE ));
39+
40+ int result3 = IntMath .saturatedAdd (Integer .MIN_VALUE , -100 );
41+ assertThat (result3 , equalTo (Integer .MIN_VALUE ));
42+ }
43+
44+ @ Test
45+ public void testIntMathSubtract () {
46+ try {
47+ IntMath .checkedSubtract (Integer .MIN_VALUE , 1 );
48+ assertTrue (false );
49+ } catch (ArithmeticException e ) {
50+ assertTrue (true );
51+ }
52+
53+ try {
54+ IntMath .checkedSubtract (Integer .MAX_VALUE , -1 );
55+ assertTrue (false );
56+ } catch (ArithmeticException e ) {
57+ assertTrue (true );
58+ };
59+
60+ int result1 = IntMath .checkedSubtract (200 , 100 );
61+ assertThat (result1 , equalTo (100 ));
62+
63+ int result2 = IntMath .saturatedSubtract (Integer .MIN_VALUE , 1 );
64+ assertThat (result2 , equalTo (Integer .MIN_VALUE ));
65+
66+ int result3 = IntMath .saturatedSubtract (Integer .MAX_VALUE , -1 );
67+ assertThat (result3 , equalTo (Integer .MAX_VALUE ));
68+ }
69+
70+ @ Test
71+ public void testIntMathMultiply () {
72+ try {
73+ IntMath .checkedMultiply (Integer .MAX_VALUE , 2 );
74+ assertTrue (false );
75+ } catch (ArithmeticException e ) {
76+ assertTrue (true );
77+ }
78+
79+ try {
80+ IntMath .checkedMultiply (Integer .MIN_VALUE , 2 );
81+ assertTrue (false );
82+ } catch (ArithmeticException e ) {
83+ assertTrue (true );
84+ }
85+
86+ int result1 = IntMath .checkedMultiply (21 , 3 );
87+ assertThat (result1 , equalTo (63 ));
88+
89+ int result2 = IntMath .saturatedMultiply (Integer .MAX_VALUE , 2 );
90+ assertThat (result2 , equalTo (Integer .MAX_VALUE ));
91+
92+ int result3 = IntMath .saturatedMultiply (Integer .MIN_VALUE , 2 );
93+ assertThat (result3 , equalTo (Integer .MIN_VALUE ));
94+ }
95+
96+ @ Test
97+ public void testIntMathPow () {
98+ try {
99+ IntMath .checkedPow (Integer .MAX_VALUE , 2 );
100+ assertTrue (false );
101+ } catch (ArithmeticException e ) {
102+ assertTrue (true );
103+ }
104+
105+ try {
106+ IntMath .checkedPow (Integer .MIN_VALUE , 3 );
107+ assertTrue (false );
108+ } catch (ArithmeticException e ) {
109+ assertTrue (true );
110+ }
111+
112+ int result1 = IntMath .saturatedPow (3 , 3 );
113+ assertThat (result1 , equalTo (27 ));
114+
115+ int result2 = IntMath .saturatedPow (Integer .MAX_VALUE , 2 );
116+ assertThat (result2 , equalTo (Integer .MAX_VALUE ));
117+
118+ int result3 = IntMath .saturatedPow (Integer .MIN_VALUE , 3 );
119+ assertThat (result3 , equalTo (Integer .MIN_VALUE ));
120+ }
121+
122+ @ Test
123+ public void testIntMathRound () {
124+ int result1 = IntMath .divide (3 , 2 , RoundingMode .DOWN );
125+ assertThat (result1 , equalTo (1 ));
126+ int result2 = IntMath .divide (3 , 2 , RoundingMode .UP );
127+ assertThat (result2 , equalTo (2 ));
128+
129+ int result3 = IntMath .log2 (5 , RoundingMode .FLOOR );
130+ assertThat (result3 , equalTo (2 ));
131+ int result4 = IntMath .log2 (5 , RoundingMode .CEILING );
132+ assertThat (result4 , equalTo (3 ));
133+
134+ int result5 = IntMath .log10 (11 , RoundingMode .HALF_UP );
135+ assertThat (result5 , equalTo (1 ));
136+
137+ int result6 = IntMath .sqrt (4 , RoundingMode .UNNECESSARY );
138+ assertThat (result6 , equalTo (2 ));
139+ try {
140+ IntMath .sqrt (5 , RoundingMode .UNNECESSARY );
141+ assertTrue (false );
142+ } catch (ArithmeticException e ) {
143+ assertTrue (true );
144+ }
145+ }
146+
147+ @ Test
148+ public void testIntMathAdditionalFunctions () {
149+ int result1 = IntMath .gcd (15 , 20 );
150+ assertThat (result1 , equalTo (5 ));
151+
152+ int result2 = IntMath .mod (8 , 3 );
153+ assertThat (result2 , equalTo (2 ));
154+
155+ boolean result3 = IntMath .isPowerOfTwo (8 );
156+ assertTrue (result3 );
157+ boolean result4 = IntMath .isPowerOfTwo (9 );
158+ assertFalse (result4 );
159+
160+ int result5 = IntMath .factorial (4 );
161+ assertThat (result5 , equalTo (24 ));
162+
163+ int result6 = IntMath .binomial (7 , 3 );
164+ assertThat (result6 , equalTo (35 ));
165+ }
166+
167+ @ Test
168+ public void should_detect_integer () {
169+ boolean result1 = DoubleMath .isMathematicalInteger (2.0 );
170+ assertThat (result1 , equalTo (true ));
171+ boolean result2 = DoubleMath .isMathematicalInteger (2.1 );
172+ assertThat (result2 , equalTo (false ));
173+ }
174+
175+ @ Test
176+ public void should_round_to_integer_types () {
177+ int result3 = DoubleMath .roundToInt (2.5 , RoundingMode .DOWN );
178+ assertThat (result3 , equalTo (2 ));
179+
180+ long result4 = DoubleMath .roundToLong (2.5 , RoundingMode .HALF_UP );
181+ assertThat (result4 , equalTo (3L ));
182+
183+ BigInteger result5 = DoubleMath .roundToBigInteger (2.5 , RoundingMode .UP );
184+ assertThat (result5 , equalTo (new BigInteger ("3" )));
185+ }
186+
187+ @ Test
188+ public void should_calculate_log_2 () {
189+ int result6 = DoubleMath .log2 (10 , RoundingMode .UP );
190+ assertThat (result6 , equalTo (4 ));
191+ }
192+ }
0 commit comments