BAEL-640: Guide to Mathematical Operations with Guava (BDD naming) (#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
This commit is contained in:
Diane Duan 2017-04-11 16:11:58 +08:00 committed by adamd1985
parent c75daff854
commit 3fb4e03a49

View File

@ -1,167 +1,195 @@
package org.baeldung.guava; package org.baeldung.guava;
import com.google.common.math.DoubleMath; import static org.hamcrest.core.IsEqual.equalTo;
import com.google.common.math.IntMath; import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.math.BigInteger; import java.math.BigInteger;
import java.math.RoundingMode; import java.math.RoundingMode;
import static org.hamcrest.core.IsEqual.equalTo; import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import com.google.common.math.DoubleMath;
import static org.junit.Assert.assertTrue; import com.google.common.math.IntMath;
public class GuavaMathTest { public class GuavaMathTest {
@Test @Test(expected = ArithmeticException.class)
public void testIntMathAdd() { public void whenSumOverflow_thenThrowException() {
try { IntMath.checkedAdd(Integer.MAX_VALUE, 1);
IntMath.checkedAdd(Integer.MAX_VALUE, 1); }
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
try { @Test(expected = ArithmeticException.class)
IntMath.checkedAdd(Integer.MIN_VALUE, -1); public void whenSumUnderflow_thenThrowException() {
assertTrue(false); IntMath.checkedAdd(Integer.MIN_VALUE, -1);
} catch (ArithmeticException e) {
assertTrue(true);
}
int result1 = IntMath.checkedAdd(2, 1);
assertThat(result1, equalTo(3));
int result2 = IntMath.saturatedAdd(Integer.MAX_VALUE, 100);
assertThat(result2, equalTo(Integer.MAX_VALUE));
int result3 = IntMath.saturatedAdd(Integer.MIN_VALUE, -100);
assertThat(result3, equalTo(Integer.MIN_VALUE));
} }
@Test @Test
public void testIntMathSubtract() { public void should_calculate_sum() {
try { int result = IntMath.checkedAdd(2, 1);
IntMath.checkedSubtract(Integer.MIN_VALUE, 1); assertThat(result, equalTo(3));
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
try {
IntMath.checkedSubtract(Integer.MAX_VALUE, -1);
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
};
int result1 = IntMath.checkedSubtract(200, 100);
assertThat(result1, equalTo(100));
int result2 = IntMath.saturatedSubtract(Integer.MIN_VALUE, 1);
assertThat(result2, equalTo(Integer.MIN_VALUE));
int result3 = IntMath.saturatedSubtract(Integer.MAX_VALUE, -1);
assertThat(result3, equalTo(Integer.MAX_VALUE));
} }
@Test @Test
public void testIntMathMultiply() { public void whenSumOverflow_thenReturnMaxInteger() {
try { int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 100);
IntMath.checkedMultiply(Integer.MAX_VALUE, 2); assertThat(result, equalTo(Integer.MAX_VALUE));
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
try {
IntMath.checkedMultiply(Integer.MIN_VALUE, 2);
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
int result1 = IntMath.checkedMultiply(21, 3);
assertThat(result1, equalTo(63));
int result2 = IntMath.saturatedMultiply(Integer.MAX_VALUE, 2);
assertThat(result2, equalTo(Integer.MAX_VALUE));
int result3 = IntMath.saturatedMultiply(Integer.MIN_VALUE, 2);
assertThat(result3, equalTo(Integer.MIN_VALUE));
} }
@Test @Test
public void testIntMathPow() { public void whenSumUnderflow_thenReturnMinInteger() {
try { int result = IntMath.saturatedAdd(Integer.MIN_VALUE, -100);
IntMath.checkedPow(Integer.MAX_VALUE, 2); assertThat(result, equalTo(Integer.MIN_VALUE));
assertTrue(false); }
} catch (ArithmeticException e) {
assertTrue(true);
}
try { @Test(expected = ArithmeticException.class)
IntMath.checkedPow(Integer.MIN_VALUE, 3); public void whenDifferenceOverflow_thenThrowException() {
assertTrue(false); IntMath.checkedSubtract(Integer.MAX_VALUE, -1);
} catch (ArithmeticException e) { }
assertTrue(true);
}
int result1 = IntMath.saturatedPow(3, 3); @Test(expected = ArithmeticException.class)
assertThat(result1, equalTo(27)); public void whenDifferenceUnderflow_thenThrowException() {
IntMath.checkedSubtract(Integer.MIN_VALUE, 1);
int result2 = IntMath.saturatedPow(Integer.MAX_VALUE, 2);
assertThat(result2, equalTo(Integer.MAX_VALUE));
int result3 = IntMath.saturatedPow(Integer.MIN_VALUE, 3);
assertThat(result3, equalTo(Integer.MIN_VALUE));
} }
@Test @Test
public void testIntMathRound() { public void should_calculate_difference() {
int result = IntMath.checkedSubtract(200, 100);
assertThat(result, equalTo(100));
}
@Test
public void whenDifferenceOverflow_thenReturnMaxInteger() {
int result = IntMath.saturatedSubtract(Integer.MAX_VALUE, -1);
assertThat(result, equalTo(Integer.MAX_VALUE));
}
@Test
public void whenDifferenceUnderflow_thenReturnMinInteger() {
int result = IntMath.saturatedSubtract(Integer.MIN_VALUE, 1);
assertThat(result, equalTo(Integer.MIN_VALUE));
}
@Test(expected = ArithmeticException.class)
public void whenProductOverflow_thenThrowException() {
IntMath.checkedMultiply(Integer.MAX_VALUE, 2);
}
@Test(expected = ArithmeticException.class)
public void whenProductUnderflow_thenThrowException() {
IntMath.checkedMultiply(Integer.MIN_VALUE, 2);
}
@Test
public void should_calculate_product() {
int result = IntMath.checkedMultiply(21, 3);
assertThat(result, equalTo(63));
}
@Test
public void whenProductOverflow_thenReturnMaxInteger() {
int result = IntMath.saturatedMultiply(Integer.MAX_VALUE, 2);
assertThat(result, equalTo(Integer.MAX_VALUE));
}
@Test
public void whenProductUnderflow_thenReturnMinInteger() {
int result = IntMath.saturatedMultiply(Integer.MIN_VALUE, 2);
assertThat(result, equalTo(Integer.MIN_VALUE));
}
@Test(expected = ArithmeticException.class)
public void whenPowerOverflow_thenThrowException() {
IntMath.checkedPow(Integer.MAX_VALUE, 2);
}
@Test(expected = ArithmeticException.class)
public void whenPowerUnderflow_thenThrowException() {
IntMath.checkedPow(Integer.MIN_VALUE, 3);
}
@Test
public void should_calculate_power() {
int result = IntMath.saturatedPow(3, 3);
assertThat(result, equalTo(27));
}
@Test
public void whenPowerOverflow_thenReturnMaxInteger() {
int result = IntMath.saturatedPow(Integer.MAX_VALUE, 2);
assertThat(result, equalTo(Integer.MAX_VALUE));
}
@Test
public void whenPowerUnderflow_thenReturnMinInteger() {
int result = IntMath.saturatedPow(Integer.MIN_VALUE, 3);
assertThat(result, equalTo(Integer.MIN_VALUE));
}
@Test
public void should_round_divide_result() {
int result1 = IntMath.divide(3, 2, RoundingMode.DOWN); int result1 = IntMath.divide(3, 2, RoundingMode.DOWN);
assertThat(result1, equalTo(1)); assertThat(result1, equalTo(1));
int result2 = IntMath.divide(3, 2, RoundingMode.UP); int result2 = IntMath.divide(3, 2, RoundingMode.UP);
assertThat(result2, equalTo(2)); assertThat(result2, equalTo(2));
int result3 = IntMath.log2(5, RoundingMode.FLOOR);
assertThat(result3, equalTo(2));
int result4 = IntMath.log2(5, RoundingMode.CEILING);
assertThat(result4, equalTo(3));
int result5 = IntMath.log10(11, RoundingMode.HALF_UP);
assertThat(result5, equalTo(1));
int result6 = IntMath.sqrt(4, RoundingMode.UNNECESSARY);
assertThat(result6, equalTo(2));
try {
IntMath.sqrt(5, RoundingMode.UNNECESSARY);
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
} }
@Test @Test
public void testIntMathAdditionalFunctions() { public void should_round_log2_result() {
int result1 = IntMath.gcd(15, 20); int result1 = IntMath.log2(5, RoundingMode.FLOOR);
assertThat(result1, equalTo(5)); assertThat(result1, equalTo(2));
int result2 = IntMath.mod(8, 3); int result2 = IntMath.log2(5, RoundingMode.CEILING);
assertThat(result2, equalTo(2)); assertThat(result2, equalTo(3));
}
boolean result3 = IntMath.isPowerOfTwo(8); @Test
assertTrue(result3); public void should_round_log10_result() {
boolean result4 = IntMath.isPowerOfTwo(9); int result = IntMath.log10(11, RoundingMode.HALF_UP);
assertFalse(result4); assertThat(result, equalTo(1));
}
int result5 = IntMath.factorial(4); @Test
assertThat(result5, equalTo(24)); public void should_round_sqrt_result() {
int result = IntMath.sqrt(4, RoundingMode.UNNECESSARY);
assertThat(result, equalTo(2));
}
int result6 = IntMath.binomial(7, 3); @Test(expected = ArithmeticException.class)
assertThat(result6, equalTo(35)); public void whenNeedRounding_thenThrowException() {
IntMath.sqrt(5, RoundingMode.UNNECESSARY);
}
@Test
public void should_calculate_gcd() {
int result = IntMath.gcd(15, 20);
assertThat(result, equalTo(5));
}
@Test
public void should_calculate_mod() {
int result = IntMath.mod(8, 3);
assertThat(result, equalTo(2));
}
@Test
public void should_test_if_is_power_of_two() {
boolean result1 = IntMath.isPowerOfTwo(8);
assertTrue(result1);
boolean result2 = IntMath.isPowerOfTwo(9);
assertFalse(result2);
}
@Test
public void should_calculate_factorial() {
int result = IntMath.factorial(4);
assertThat(result, equalTo(24));
}
@Test
public void should_calculate_binomial() {
int result = IntMath.binomial(7, 3);
assertThat(result, equalTo(35));
} }
@Test @Test