Skip to content

Commit 3fb4e03

Browse files
Diane Duanadamd1985
authored andcommitted
BAEL-640: Guide to Mathematical Operations with Guava (BDD naming) (eugenp#1599)
* Guava IntMath tests * Guava DoubleMath test * break down testDoubleMath() * bdd ut naming * bdd ut naming 2 * break down round unit test * rename unit tests
1 parent c75daff commit 3fb4e03

1 file changed

Lines changed: 143 additions & 115 deletions

File tree

‎guava/src/test/java/org/baeldung/guava/GuavaMathTest.java‎

Lines changed: 143 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,195 @@
11
package org.baeldung.guava;
22

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;
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
import static org.junit.Assert.*;
85

96
import java.math.BigInteger;
107
import java.math.RoundingMode;
118

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;
9+
import org.junit.Test;
1610

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-
}
11+
import com.google.common.math.DoubleMath;
12+
import com.google.common.math.IntMath;
2613

27-
try {
28-
IntMath.checkedAdd(Integer.MIN_VALUE, -1);
29-
assertTrue(false);
30-
} catch (ArithmeticException e) {
31-
assertTrue(true);
32-
}
14+
public class GuavaMathTest {
15+
@Test(expected = ArithmeticException.class)
16+
public void whenSumOverflow_thenThrowException() {
17+
IntMath.checkedAdd(Integer.MAX_VALUE, 1);
18+
}
3319

34-
int result1 = IntMath.checkedAdd(2, 1);
35-
assertThat(result1, equalTo(3));
20+
@Test(expected = ArithmeticException.class)
21+
public void whenSumUnderflow_thenThrowException() {
22+
IntMath.checkedAdd(Integer.MIN_VALUE, -1);
23+
}
3624

37-
int result2 = IntMath.saturatedAdd(Integer.MAX_VALUE, 100);
38-
assertThat(result2, equalTo(Integer.MAX_VALUE));
25+
@Test
26+
public void should_calculate_sum() {
27+
int result = IntMath.checkedAdd(2, 1);
28+
assertThat(result, equalTo(3));
29+
}
3930

40-
int result3 = IntMath.saturatedAdd(Integer.MIN_VALUE, -100);
41-
assertThat(result3, equalTo(Integer.MIN_VALUE));
31+
@Test
32+
public void whenSumOverflow_thenReturnMaxInteger() {
33+
int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 100);
34+
assertThat(result, equalTo(Integer.MAX_VALUE));
4235
}
4336

4437
@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-
}
38+
public void whenSumUnderflow_thenReturnMinInteger() {
39+
int result = IntMath.saturatedAdd(Integer.MIN_VALUE, -100);
40+
assertThat(result, equalTo(Integer.MIN_VALUE));
41+
}
5242

53-
try {
54-
IntMath.checkedSubtract(Integer.MAX_VALUE, -1);
55-
assertTrue(false);
56-
} catch (ArithmeticException e) {
57-
assertTrue(true);
58-
};
43+
@Test(expected = ArithmeticException.class)
44+
public void whenDifferenceOverflow_thenThrowException() {
45+
IntMath.checkedSubtract(Integer.MAX_VALUE, -1);
46+
}
5947

60-
int result1 = IntMath.checkedSubtract(200, 100);
61-
assertThat(result1, equalTo(100));
48+
@Test(expected = ArithmeticException.class)
49+
public void whenDifferenceUnderflow_thenThrowException() {
50+
IntMath.checkedSubtract(Integer.MIN_VALUE, 1);
51+
}
6252

63-
int result2 = IntMath.saturatedSubtract(Integer.MIN_VALUE, 1);
64-
assertThat(result2, equalTo(Integer.MIN_VALUE));
53+
@Test
54+
public void should_calculate_difference() {
55+
int result = IntMath.checkedSubtract(200, 100);
56+
assertThat(result, equalTo(100));
57+
}
6558

66-
int result3 = IntMath.saturatedSubtract(Integer.MAX_VALUE, -1);
67-
assertThat(result3, equalTo(Integer.MAX_VALUE));
59+
@Test
60+
public void whenDifferenceOverflow_thenReturnMaxInteger() {
61+
int result = IntMath.saturatedSubtract(Integer.MAX_VALUE, -1);
62+
assertThat(result, equalTo(Integer.MAX_VALUE));
6863
}
6964

7065
@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-
}
66+
public void whenDifferenceUnderflow_thenReturnMinInteger() {
67+
int result = IntMath.saturatedSubtract(Integer.MIN_VALUE, 1);
68+
assertThat(result, equalTo(Integer.MIN_VALUE));
69+
}
7870

79-
try {
80-
IntMath.checkedMultiply(Integer.MIN_VALUE, 2);
81-
assertTrue(false);
82-
} catch (ArithmeticException e) {
83-
assertTrue(true);
84-
}
71+
@Test(expected = ArithmeticException.class)
72+
public void whenProductOverflow_thenThrowException() {
73+
IntMath.checkedMultiply(Integer.MAX_VALUE, 2);
74+
}
8575

86-
int result1 = IntMath.checkedMultiply(21, 3);
87-
assertThat(result1, equalTo(63));
76+
@Test(expected = ArithmeticException.class)
77+
public void whenProductUnderflow_thenThrowException() {
78+
IntMath.checkedMultiply(Integer.MIN_VALUE, 2);
79+
}
8880

89-
int result2 = IntMath.saturatedMultiply(Integer.MAX_VALUE, 2);
90-
assertThat(result2, equalTo(Integer.MAX_VALUE));
81+
@Test
82+
public void should_calculate_product() {
83+
int result = IntMath.checkedMultiply(21, 3);
84+
assertThat(result, equalTo(63));
85+
}
9186

92-
int result3 = IntMath.saturatedMultiply(Integer.MIN_VALUE, 2);
93-
assertThat(result3, equalTo(Integer.MIN_VALUE));
87+
@Test
88+
public void whenProductOverflow_thenReturnMaxInteger() {
89+
int result = IntMath.saturatedMultiply(Integer.MAX_VALUE, 2);
90+
assertThat(result, equalTo(Integer.MAX_VALUE));
9491
}
9592

9693
@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-
}
94+
public void whenProductUnderflow_thenReturnMinInteger() {
95+
int result = IntMath.saturatedMultiply(Integer.MIN_VALUE, 2);
96+
assertThat(result, equalTo(Integer.MIN_VALUE));
97+
}
10498

105-
try {
106-
IntMath.checkedPow(Integer.MIN_VALUE, 3);
107-
assertTrue(false);
108-
} catch (ArithmeticException e) {
109-
assertTrue(true);
110-
}
99+
@Test(expected = ArithmeticException.class)
100+
public void whenPowerOverflow_thenThrowException() {
101+
IntMath.checkedPow(Integer.MAX_VALUE, 2);
102+
}
103+
104+
@Test(expected = ArithmeticException.class)
105+
public void whenPowerUnderflow_thenThrowException() {
106+
IntMath.checkedPow(Integer.MIN_VALUE, 3);
107+
}
111108

112-
int result1 = IntMath.saturatedPow(3, 3);
113-
assertThat(result1, equalTo(27));
109+
@Test
110+
public void should_calculate_power() {
111+
int result = IntMath.saturatedPow(3, 3);
112+
assertThat(result, equalTo(27));
113+
}
114114

115-
int result2 = IntMath.saturatedPow(Integer.MAX_VALUE, 2);
116-
assertThat(result2, equalTo(Integer.MAX_VALUE));
115+
@Test
116+
public void whenPowerOverflow_thenReturnMaxInteger() {
117+
int result = IntMath.saturatedPow(Integer.MAX_VALUE, 2);
118+
assertThat(result, equalTo(Integer.MAX_VALUE));
119+
}
117120

118-
int result3 = IntMath.saturatedPow(Integer.MIN_VALUE, 3);
119-
assertThat(result3, equalTo(Integer.MIN_VALUE));
121+
@Test
122+
public void whenPowerUnderflow_thenReturnMinInteger() {
123+
int result = IntMath.saturatedPow(Integer.MIN_VALUE, 3);
124+
assertThat(result, equalTo(Integer.MIN_VALUE));
120125
}
121126

122127
@Test
123-
public void testIntMathRound() {
128+
public void should_round_divide_result() {
124129
int result1 = IntMath.divide(3, 2, RoundingMode.DOWN);
125130
assertThat(result1, equalTo(1));
131+
126132
int result2 = IntMath.divide(3, 2, RoundingMode.UP);
127133
assertThat(result2, equalTo(2));
134+
}
128135

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));
136+
@Test
137+
public void should_round_log2_result() {
138+
int result1 = IntMath.log2(5, RoundingMode.FLOOR);
139+
assertThat(result1, equalTo(2));
133140

134-
int result5 = IntMath.log10(11, RoundingMode.HALF_UP);
135-
assertThat(result5, equalTo(1));
141+
int result2 = IntMath.log2(5, RoundingMode.CEILING);
142+
assertThat(result2, equalTo(3));
143+
}
136144

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+
@Test
146+
public void should_round_log10_result() {
147+
int result = IntMath.log10(11, RoundingMode.HALF_UP);
148+
assertThat(result, equalTo(1));
145149
}
146150

147151
@Test
148-
public void testIntMathAdditionalFunctions() {
149-
int result1 = IntMath.gcd(15, 20);
150-
assertThat(result1, equalTo(5));
152+
public void should_round_sqrt_result() {
153+
int result = IntMath.sqrt(4, RoundingMode.UNNECESSARY);
154+
assertThat(result, equalTo(2));
155+
}
151156

152-
int result2 = IntMath.mod(8, 3);
153-
assertThat(result2, equalTo(2));
157+
@Test(expected = ArithmeticException.class)
158+
public void whenNeedRounding_thenThrowException() {
159+
IntMath.sqrt(5, RoundingMode.UNNECESSARY);
160+
}
161+
162+
@Test
163+
public void should_calculate_gcd() {
164+
int result = IntMath.gcd(15, 20);
165+
assertThat(result, equalTo(5));
166+
}
167+
168+
@Test
169+
public void should_calculate_mod() {
170+
int result = IntMath.mod(8, 3);
171+
assertThat(result, equalTo(2));
172+
}
154173

155-
boolean result3 = IntMath.isPowerOfTwo(8);
156-
assertTrue(result3);
157-
boolean result4 = IntMath.isPowerOfTwo(9);
158-
assertFalse(result4);
174+
@Test
175+
public void should_test_if_is_power_of_two() {
176+
boolean result1 = IntMath.isPowerOfTwo(8);
177+
assertTrue(result1);
159178

160-
int result5 = IntMath.factorial(4);
161-
assertThat(result5, equalTo(24));
179+
boolean result2 = IntMath.isPowerOfTwo(9);
180+
assertFalse(result2);
181+
}
162182

163-
int result6 = IntMath.binomial(7, 3);
164-
assertThat(result6, equalTo(35));
183+
@Test
184+
public void should_calculate_factorial() {
185+
int result = IntMath.factorial(4);
186+
assertThat(result, equalTo(24));
187+
}
188+
189+
@Test
190+
public void should_calculate_binomial() {
191+
int result = IntMath.binomial(7, 3);
192+
assertThat(result, equalTo(35));
165193
}
166194

167195
@Test

0 commit comments

Comments
 (0)