Compare commits
2 Commits
ab9f73ba22
...
802a916b92
| Author | SHA1 | Date | |
|---|---|---|---|
| 802a916b92 | |||
| 0ab786b24a |
@@ -8,6 +8,9 @@ public class DivideFunction implements CalcFunction {
|
|||||||
}
|
}
|
||||||
double result = input[0];
|
double result = input[0];
|
||||||
for (int i = 1; i < input.length; i++) {
|
for (int i = 1; i < input.length; i++) {
|
||||||
|
if (input[i] == 0) {
|
||||||
|
throw new ArithmeticException("Division by zero");
|
||||||
|
}
|
||||||
result /= input[i];
|
result /= input[i];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.example.util;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
class DivideFunctionTest {
|
class DivideFunctionTest {
|
||||||
|
|
||||||
@@ -27,4 +28,9 @@ class DivideFunctionTest {
|
|||||||
void dividesNegativeValues() {
|
void dividesNegativeValues() {
|
||||||
assertEquals(-2.0, divideFunction.calc(4.0, -2.0));
|
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