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;
import com.google.common.math.DoubleMath;
import com.google.common.math.IntMath;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.math.RoundingMode;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.google.common.math.DoubleMath;
import com.google.common.math.IntMath;
public class GuavaMathTest {
@Test
public void testIntMathAdd() {
try {
IntMath.checkedAdd(Integer.MAX_VALUE, 1);
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
@Test(expected = ArithmeticException.class)
public void whenSumOverflow_thenThrowException() {
IntMath.checkedAdd(Integer.MAX_VALUE, 1);
}
try {
IntMath.checkedAdd(Integer.MIN_VALUE, -1);
assertTrue(false);
} 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(expected = ArithmeticException.class)
public void whenSumUnderflow_thenThrowException() {
IntMath.checkedAdd(Integer.MIN_VALUE, -1);
}
@Test
public void testIntMathSubtract() {
try {
IntMath.checkedSubtract(Integer.MIN_VALUE, 1);
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));
public void should_calculate_sum() {
int result = IntMath.checkedAdd(2, 1);
assertThat(result, equalTo(3));
}
@Test
public void testIntMathMultiply() {
try {
IntMath.checkedMultiply(Integer.MAX_VALUE, 2);
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));
public void whenSumOverflow_thenReturnMaxInteger() {
int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 100);
assertThat(result, equalTo(Integer.MAX_VALUE));
}
@Test
public void testIntMathPow() {
try {
IntMath.checkedPow(Integer.MAX_VALUE, 2);
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
public void whenSumUnderflow_thenReturnMinInteger() {
int result = IntMath.saturatedAdd(Integer.MIN_VALUE, -100);
assertThat(result, equalTo(Integer.MIN_VALUE));
}
try {
IntMath.checkedPow(Integer.MIN_VALUE, 3);
assertTrue(false);
} catch (ArithmeticException e) {
assertTrue(true);
}
@Test(expected = ArithmeticException.class)
public void whenDifferenceOverflow_thenThrowException() {
IntMath.checkedSubtract(Integer.MAX_VALUE, -1);
}
int result1 = IntMath.saturatedPow(3, 3);
assertThat(result1, equalTo(27));
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(expected = ArithmeticException.class)
public void whenDifferenceUnderflow_thenThrowException() {
IntMath.checkedSubtract(Integer.MIN_VALUE, 1);
}
@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);
assertThat(result1, equalTo(1));
int result2 = IntMath.divide(3, 2, RoundingMode.UP);
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
public void testIntMathAdditionalFunctions() {
int result1 = IntMath.gcd(15, 20);
assertThat(result1, equalTo(5));
public void should_round_log2_result() {
int result1 = IntMath.log2(5, RoundingMode.FLOOR);
assertThat(result1, equalTo(2));
int result2 = IntMath.mod(8, 3);
assertThat(result2, equalTo(2));
int result2 = IntMath.log2(5, RoundingMode.CEILING);
assertThat(result2, equalTo(3));
}
boolean result3 = IntMath.isPowerOfTwo(8);
assertTrue(result3);
boolean result4 = IntMath.isPowerOfTwo(9);
assertFalse(result4);
@Test
public void should_round_log10_result() {
int result = IntMath.log10(11, RoundingMode.HALF_UP);
assertThat(result, equalTo(1));
}
int result5 = IntMath.factorial(4);
assertThat(result5, equalTo(24));
@Test
public void should_round_sqrt_result() {
int result = IntMath.sqrt(4, RoundingMode.UNNECESSARY);
assertThat(result, equalTo(2));
}
int result6 = IntMath.binomial(7, 3);
assertThat(result6, equalTo(35));
@Test(expected = ArithmeticException.class)
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