Skip to content
Snippets Groups Projects
Commit 14bc21b8 authored by Mahboobe Shakeri's avatar Mahboobe Shakeri
Browse files

calculator class test added

parent 3bfb9461
No related branches found
No related tags found
No related merge requests found
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
private Calculator calculator;
@BeforeEach
public void setUp() {
calculator = new Calculator();
}
@Test
void addTest() {
assertEquals(3, calculator.add(1, 2));
assertNotEquals(4, calculator.add(2, 3));
assertEquals(0, calculator.add(-2, 2), "adding -2 and 2 should be 0");
assertEquals(-7, calculator.add(-5, -2), "adding tow negetive number of -5 and -2 equals to -7 ");
assertEquals(calculator.add(3,2), calculator.add(2,3));
}
@Test
void subtract() {
assertEquals(-1, calculator.subtract(1, 2));
assertEquals(1, calculator.subtract(2, 1));
assertEquals(0, calculator.subtract(5, 5), "subtracting 5 from 5 should return 0");
}
@Test
void multiply() {
assertEquals(0, calculator.multiply(0, 2), "multiplying with 0 should return 0");
assertEquals(5, calculator.multiply(1, 5), "multiplying with 1 should return the number");
assertEquals(-8, calculator.multiply(-4, 2), "multiplying with negative number should return negative number");
assertEquals(6, calculator.multiply(-3, -2), "multiplying two negative number should be positive");
}
@Test
void testDivideWithzero() {
assertThrows(IllegalArgumentException.class, () -> {calculator.divide(7,0);});
}
@Test
void testDivide() {
assertEquals(0, calculator.divide(0, 2), "dividing by 0 should return 0");
assertEquals(1, calculator.divide(5, 5), "dividing by number itself should return 1");
assertEquals(-2, calculator.divide(-2, 1), "dividing by 1 should return number itself");
}
@Test
void testAddWithMaxValue() {
assertEquals(Integer.MAX_VALUE, calculator.add(Integer.MAX_VALUE, 0), "Adding 0 to Integer.MAX_VALUE should return Integer.MAX_VALUE");
assertEquals(Integer.MAX_VALUE, calculator.add(Integer.MAX_VALUE, 1), "Adding 1 to Integer.MAX_VALUE should return Integer.MAX_VALUE");
}
@Test void testAddWithMaxValueException() {
Exception exception = assertThrows(ArithmeticException.class, () -> {
calculator.add(Integer.MAX_VALUE, Integer.MAX_VALUE);
});
assertTrue(exception.getMessage().contains("integer overflow"));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment