fix: DivideFunction throws on divide by zero (#9) #10

Merged
pitfriedrich merged 1 commits from ai/issue-9-divide-by-zero into main 2026-07-25 09:16:23 +00:00
2 changed files with 9 additions and 0 deletions
Showing only changes of commit 0ab786b24a - Show all commits
@@ -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));
}
}