fix: throw ArithmeticException on divide by zero (#9)
CI / build-and-test (pull_request) Successful in 16s
CI / build-and-test (pull_request) Successful in 16s
This commit is contained in:
@@ -8,6 +8,9 @@ public class DivideFunction implements CalcFunction {
|
||||
}
|
||||
double result = input[0];
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
if (input[i] == 0) {
|
||||
throw new ArithmeticException("Division by zero");
|
||||
}
|
||||
result /= input[i];
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.example.util;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class DivideFunctionTest {
|
||||
|
||||
@@ -27,4 +28,9 @@ class DivideFunctionTest {
|
||||
void dividesNegativeValues() {
|
||||
assertEquals(-2.0, divideFunction.calc(4.0, -2.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsOnDivideByZero() {
|
||||
assertThrows(ArithmeticException.class, () -> divideFunction.calc(5.0, 0.0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user