|
1 | 1 | package org.baeldung.guava; |
2 | 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; |
| 3 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 4 | +import static org.junit.Assert.*; |
8 | 5 |
|
9 | 6 | import java.math.BigInteger; |
10 | 7 | import java.math.RoundingMode; |
11 | 8 |
|
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; |
16 | 10 |
|
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; |
26 | 13 |
|
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 | + } |
33 | 19 |
|
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 | + } |
36 | 24 |
|
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 | + } |
39 | 30 |
|
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)); |
42 | 35 | } |
43 | 36 |
|
44 | 37 | @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 | + } |
52 | 42 |
|
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 | + } |
59 | 47 |
|
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 | + } |
62 | 52 |
|
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 | + } |
65 | 58 |
|
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)); |
68 | 63 | } |
69 | 64 |
|
70 | 65 | @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 | + } |
78 | 70 |
|
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 | + } |
85 | 75 |
|
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 | + } |
88 | 80 |
|
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 | + } |
91 | 86 |
|
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)); |
94 | 91 | } |
95 | 92 |
|
96 | 93 | @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 | + } |
104 | 98 |
|
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 | + } |
111 | 108 |
|
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 | + } |
114 | 114 |
|
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 | + } |
117 | 120 |
|
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)); |
120 | 125 | } |
121 | 126 |
|
122 | 127 | @Test |
123 | | - public void testIntMathRound() { |
| 128 | + public void should_round_divide_result() { |
124 | 129 | int result1 = IntMath.divide(3, 2, RoundingMode.DOWN); |
125 | 130 | assertThat(result1, equalTo(1)); |
| 131 | + |
126 | 132 | int result2 = IntMath.divide(3, 2, RoundingMode.UP); |
127 | 133 | assertThat(result2, equalTo(2)); |
| 134 | + } |
128 | 135 |
|
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)); |
133 | 140 |
|
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 | + } |
136 | 144 |
|
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)); |
145 | 149 | } |
146 | 150 |
|
147 | 151 | @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 | + } |
151 | 156 |
|
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 | + } |
154 | 173 |
|
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); |
159 | 178 |
|
160 | | - int result5 = IntMath.factorial(4); |
161 | | - assertThat(result5, equalTo(24)); |
| 179 | + boolean result2 = IntMath.isPowerOfTwo(9); |
| 180 | + assertFalse(result2); |
| 181 | + } |
162 | 182 |
|
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)); |
165 | 193 | } |
166 | 194 |
|
167 | 195 | @Test |
|
0 commit comments