|
| 1 | +package com.baeldung.java.math; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +public class Java8MathUnitTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + public void whenAddExactToInteger_thenExpectCorrectArithmeticResult() { |
| 11 | + assertEquals(150, Math.addExact(100, 50)); // Returns 150 |
| 12 | + } |
| 13 | + |
| 14 | + @Test |
| 15 | + public void whenSubstractExactFromInteger_thenExpectCorrectArithmeticResult() { |
| 16 | + assertEquals(50, Math.subtractExact(100, 50)); // Returns 50 |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenDecrementExactInteger_thenExpectCorrectArithmeticResult() { |
| 21 | + assertEquals(99, Math.decrementExact(100)); // Returns 99 |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void whenIncrementExactToInteger_thenExpectCorrectArithmeticResult() { |
| 26 | + assertEquals(101, Math.incrementExact(100)); // Returns 101 |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void whenMultiplyExactTwoIntegers_thenExpectCorrectArithmeticResult() { |
| 31 | + assertEquals(500, Math.multiplyExact(100, 5)); // Returns 500 |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenNegateExactInteger_thenExpectCorrectArithmeticResult() { |
| 36 | + assertEquals(-100, Math.negateExact(100)); // Returns -100 |
| 37 | + } |
| 38 | + |
| 39 | + @Test(expected = ArithmeticException.class) |
| 40 | + public void whenAddToMaxInteger_thenThrowsArithmeticException() { |
| 41 | + Math.addExact(Integer.MAX_VALUE, 1); // Throws ArithmeticException |
| 42 | + } |
| 43 | + |
| 44 | + @Test(expected = ArithmeticException.class) |
| 45 | + public void whenDecrementMinInteger_thenThrowsArithmeticException() { |
| 46 | + Math.decrementExact(Integer.MIN_VALUE); // Throws ArithmeticException |
| 47 | + } |
| 48 | + |
| 49 | + @Test(expected = ArithmeticException.class) |
| 50 | + public void whenIncrementMaxLong_thenThrowsArithmeticException() { |
| 51 | + Math.incrementExact(Long.MAX_VALUE); // Throws ArithmeticException |
| 52 | + } |
| 53 | + |
| 54 | + @Test(expected = ArithmeticException.class) |
| 55 | + public void whenMultiplyMaxLong_thenThrowsArithmeticException() { |
| 56 | + Math.multiplyExact(Long.MAX_VALUE, 2); // Throws ArithmeticException |
| 57 | + } |
| 58 | + |
| 59 | + @Test(expected = ArithmeticException.class) |
| 60 | + public void whenNegateMinInteger_thenThrowsArithmeticException() { |
| 61 | + Math.negateExact(Integer.MIN_VALUE); // MinInt value: −2.147.483.648, but MaxInt Value: 2.147.483.647 => Throws ArithmeticException |
| 62 | + } |
| 63 | + |
| 64 | + @Test(expected = ArithmeticException.class) |
| 65 | + public void whenSubstractFromMinInteger_thenThrowsArithmeticException() { |
| 66 | + Math.subtractExact(Integer.MIN_VALUE, 1); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void whenFloorDivTwoIntegers_thenExpectCorrectArithmeticResult() { |
| 71 | + assertEquals(3, Math.floorDiv(7, 2)); // Exact quotient is 3.5 so floor(3.5) == 3 |
| 72 | + assertEquals(-4, Math.floorDiv(-7, 2)); // Exact quotient is -3.5 so floor(-3.5) == -4 |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void whenFloorModTwoIntegers_thenExpectCorrectArithmeticResult() { |
| 77 | + assertEquals(2, Math.floorMod(5, 3)); // Returns 2: floorMod for positive numbers returns the same as % operator |
| 78 | + assertEquals(1, Math.floorMod(-5, 3)); // Returns 1 and not 2 because floorDiv(-5, 3) is -2 and not -1 and (-2*3) + (1) = -5 |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenNextDownOfDouble_thenExpectCorrectNumber() { |
| 83 | + double number = 3.0; |
| 84 | + double expected = 2.999999999999; |
| 85 | + double delta = 0.00000001; |
| 86 | + assertEquals(expected, Math.nextDown(number), delta); // The delta defines the accepted error range |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments