package com.educoder.bridge;

import com.educoder.bridge.service.CalculatorService;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @Author: youys
 * @Date: 2023/2/27
 * @Description:
 */
public class CalculatorServiceTest {

    @Test
    public void testAdd() {
        CalculatorService calculator = new CalculatorService();
        int result = calculator.add(1, 2);
        assertEquals(3, result);
    }

    @Test
    public void testSubtract() {
        CalculatorService calculator = new CalculatorService();
        int result = calculator.subtract(5, 2);
        assertEquals(3, result);
    }

    @Test
    public void testMultiply() {
        CalculatorService calculator = new CalculatorService();
        int result = calculator.multiply(3, 4);
        assertEquals(12, result);
    }

    @Test
    public void testDivide() {
        CalculatorService calculator = new CalculatorService();
        double result = calculator.divide(10, 2);
        assertEquals(5.0, result, 0);

        try {
            calculator.divide(10, 0);
            fail("Expected an IllegalArgumentException to be thrown");
        } catch (IllegalArgumentException e) {
            assertEquals("Cannot divide by zero!", e.getMessage());
        }
    }

    @Test
    public void testSqrt() {
        CalculatorService calculator = new CalculatorService();
        double result = calculator.sqrt(16);
        assertEquals(4.0, result, 0);

        try {
            calculator.sqrt(-1);
            fail("Expected an IllegalArgumentException to be thrown");
        } catch (IllegalArgumentException e) {
            assertEquals("Cannot calculate square root of negative number!", e.getMessage());
        }
    }

}
