From 2a585bfd18d81be046a36166733ac448c3f6fe67 Mon Sep 17 00:00:00 2001 From: charrinton Date: Mon, 12 Mar 2018 15:23:33 +0100 Subject: [PATCH 1/3] BAEL-1546: Java 8 Math additions --- .../baeldung/math/MathNewMethodsTests.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java diff --git a/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java b/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java new file mode 100644 index 000000000000..3c31d390c84f --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java @@ -0,0 +1,65 @@ +package com.baeldung.math; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class MathNewMethodsTests { + + @Test(expected = ArithmeticException.class) + public void whenAddToMaxInteger_thenThrowsArithmeticException() { + assertEquals(150, Math.addExact(100, 50)); // Returns 150 + Math.addExact(Integer.MAX_VALUE, 1); // Throws ArithmeticException + } + + @Test(expected = ArithmeticException.class) + public void whenDecrementMinInteger_thenThrowsArithmeticException() { + assertEquals(99, Math.decrementExact(100)); // Returns 99 + Math.decrementExact(Integer.MIN_VALUE); // Throws ArithmeticException + } + + @Test(expected = ArithmeticException.class) + public void whenIncrementMaxLong_thenThrowsArithmeticException() { + assertEquals(101, Math.incrementExact(100)); // Returns 101 + Math.incrementExact(Long.MAX_VALUE); // Throws ArithmeticException + } + + @Test(expected = ArithmeticException.class) + public void whenMultiplyMaxLong_thenThrowsArithmeticException() { + assertEquals(500, Math.multiplyExact(100, 5)); // Returns 500 + Math.multiplyExact(Long.MAX_VALUE, 2); // Throws ArithmeticException + } + + @Test(expected = ArithmeticException.class) + public void whenNegateMinInteger_thenThrowsArithmeticException() { + assertEquals(-100, Math.negateExact(100)); // Returns -100 + Math.negateExact(Integer.MIN_VALUE); // MinInt value: −2.147.483.648, but MaxInt Value: 2.147.483.647 => Throws ArithmeticException + } + + @Test(expected = ArithmeticException.class) + public void whenSubstractFromMinInteger_thenThrowsArithmeticException() { + assertEquals(50, Math.subtractExact(100, 50)); // Returns 50 + Math.subtractExact(Integer.MIN_VALUE, 1); + } + + @Test + public void whenFloorDivTwoIntegers_thenExpectCorrectArithmeticResult() { + assertEquals(3, Math.floorDiv(7, 2)); // Exact quotient is 3.5 so floor(3.5) == 3 + assertEquals(-4, Math.floorDiv(-7, 2)); // Exact quotient is -3.5 so floor(-3.5) == -4 + } + + @Test + public void whenModDivTwoIntegers_thenExpectCorrectArithmeticResult() { + assertEquals(2, Math.floorMod(5, 3)); // Returns 2: floorMod for positive numbers returns the same as % operator + 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 + } + + @Test + public void whenNextDownOfDouble_thenExpectCorrectNumber() { + double number = 3.0; + double expected = 2.999999999999; + double delta = 0.00000001; + assertEquals(expected, Math.nextDown(number), delta); // The delta defines the accepted error range + } + +} From 42856db8bc084bfb8af64a17fd8edea8bb6d5176 Mon Sep 17 00:00:00 2001 From: charrinton Date: Mon, 12 Mar 2018 21:05:49 +0100 Subject: [PATCH 2/3] Applied feedback to Unit Tests --- ...Tests.java => MathNewMethodsUnitTest.java} | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) rename core-java-8/src/test/java/com/baeldung/math/{MathNewMethodsTests.java => MathNewMethodsUnitTest.java} (83%) diff --git a/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java b/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java similarity index 83% rename from core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java rename to core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java index 3c31d390c84f..db9a72bfca29 100644 --- a/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsTests.java +++ b/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java @@ -4,41 +4,59 @@ import org.junit.Test; -public class MathNewMethodsTests { +public class MathNewMethodsUnitTest { + public void whenAddExactToInteger_thenExpectCorrectArithmeticResult() { + assertEquals(150, Math.addExact(100, 50)); // Returns 150 + } + + public void whenSubstractExactFromInteger_thenExpectCorrectArithmeticResult() { + assertEquals(50, Math.subtractExact(100, 50)); // Returns 50 + } + + public void whenDecrementExactInteger_thenExpectCorrectArithmeticResult() { + assertEquals(99, Math.decrementExact(100)); // Returns 99 + } + + public void whenIncrementExactToInteger_thenExpectCorrectArithmeticResult() { + assertEquals(101, Math.incrementExact(100)); // Returns 101 + } + + public void whenMultiplyExactTwoIntegers_thenExpectCorrectArithmeticResult() { + assertEquals(500, Math.multiplyExact(100, 5)); // Returns 500 + } + + public void whenNegateExactInteger_thenExpectCorrectArithmeticResult() { + assertEquals(-100, Math.negateExact(100)); // Returns -100 + } + @Test(expected = ArithmeticException.class) public void whenAddToMaxInteger_thenThrowsArithmeticException() { - assertEquals(150, Math.addExact(100, 50)); // Returns 150 Math.addExact(Integer.MAX_VALUE, 1); // Throws ArithmeticException } @Test(expected = ArithmeticException.class) public void whenDecrementMinInteger_thenThrowsArithmeticException() { - assertEquals(99, Math.decrementExact(100)); // Returns 99 Math.decrementExact(Integer.MIN_VALUE); // Throws ArithmeticException } @Test(expected = ArithmeticException.class) public void whenIncrementMaxLong_thenThrowsArithmeticException() { - assertEquals(101, Math.incrementExact(100)); // Returns 101 Math.incrementExact(Long.MAX_VALUE); // Throws ArithmeticException } @Test(expected = ArithmeticException.class) public void whenMultiplyMaxLong_thenThrowsArithmeticException() { - assertEquals(500, Math.multiplyExact(100, 5)); // Returns 500 Math.multiplyExact(Long.MAX_VALUE, 2); // Throws ArithmeticException } @Test(expected = ArithmeticException.class) public void whenNegateMinInteger_thenThrowsArithmeticException() { - assertEquals(-100, Math.negateExact(100)); // Returns -100 Math.negateExact(Integer.MIN_VALUE); // MinInt value: −2.147.483.648, but MaxInt Value: 2.147.483.647 => Throws ArithmeticException } @Test(expected = ArithmeticException.class) public void whenSubstractFromMinInteger_thenThrowsArithmeticException() { - assertEquals(50, Math.subtractExact(100, 50)); // Returns 50 Math.subtractExact(Integer.MIN_VALUE, 1); } From bbcf521a8ad8bef49d9d3b7784b2ceb5aac5c9e0 Mon Sep 17 00:00:00 2001 From: Predrag Maric Date: Tue, 13 Mar 2018 07:25:53 +0100 Subject: [PATCH 3/3] BAEL-1546 Added missing test annotations --- .../baeldung/math/MathNewMethodsUnitTest.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java b/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java index db9a72bfca29..da963760094f 100644 --- a/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/math/MathNewMethodsUnitTest.java @@ -6,26 +6,32 @@ public class MathNewMethodsUnitTest { + @Test public void whenAddExactToInteger_thenExpectCorrectArithmeticResult() { assertEquals(150, Math.addExact(100, 50)); // Returns 150 } - + + @Test public void whenSubstractExactFromInteger_thenExpectCorrectArithmeticResult() { assertEquals(50, Math.subtractExact(100, 50)); // Returns 50 } - + + @Test public void whenDecrementExactInteger_thenExpectCorrectArithmeticResult() { assertEquals(99, Math.decrementExact(100)); // Returns 99 } - + + @Test public void whenIncrementExactToInteger_thenExpectCorrectArithmeticResult() { assertEquals(101, Math.incrementExact(100)); // Returns 101 } - + + @Test public void whenMultiplyExactTwoIntegers_thenExpectCorrectArithmeticResult() { assertEquals(500, Math.multiplyExact(100, 5)); // Returns 500 } - + + @Test public void whenNegateExactInteger_thenExpectCorrectArithmeticResult() { assertEquals(-100, Math.negateExact(100)); // Returns -100 }