Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAEL-1546: Java 8 Math additions #3805

Merged
merged 3 commits into from
Mar 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.baeldung.math;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

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
}

@Test(expected = ArithmeticException.class)
public void whenAddToMaxInteger_thenThrowsArithmeticException() {
Math.addExact(Integer.MAX_VALUE, 1); // Throws ArithmeticException
}

@Test(expected = ArithmeticException.class)
public void whenDecrementMinInteger_thenThrowsArithmeticException() {
Math.decrementExact(Integer.MIN_VALUE); // Throws ArithmeticException
}

@Test(expected = ArithmeticException.class)
public void whenIncrementMaxLong_thenThrowsArithmeticException() {
Math.incrementExact(Long.MAX_VALUE); // Throws ArithmeticException
}

@Test(expected = ArithmeticException.class)
public void whenMultiplyMaxLong_thenThrowsArithmeticException() {
Math.multiplyExact(Long.MAX_VALUE, 2); // Throws ArithmeticException
}

@Test(expected = ArithmeticException.class)
public void whenNegateMinInteger_thenThrowsArithmeticException() {
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() {
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
}

}