Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Calculate {
double calculate(double a, double b, char c);
}
25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax;

public class Calculator implements Calculate {

@Override
public double calculate(double firstParameter, double secondParameter, char operation) {
switch (operation) {
case '+':
return firstParameter + secondParameter;
case '-':
return firstParameter - secondParameter;
case '*':
return firstParameter * secondParameter;
case '^':
return Math.pow(firstParameter, secondParameter);
Comment on lines +14 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а что если первый операнд 0, а второй -2?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

добавлена проверка Double.POSITIVE_INFINITY

case '/':
if (secondParameter == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return firstParameter / secondParameter;
default:
throw new IllegalArgumentException("Invalid operation");
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

146 changes: 146 additions & 0 deletions src/test/java/core/basesyntax/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package core.basesyntax;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class CalculatorTest {
private static final double DELTA = 0.0001;
private Calculate calculator = new Calculator();
private double expected;
private double actual;

@Test
void calculate_additionWithTwoPositiveOperands_Ok() {
expected = 100.0;
actual = calculator.calculate(90.0, 10.0, '+');
assertEquals(expected, actual);
}

@Test
void calculate_additionWithTwoNegativeOperands_Ok() {
expected = - 150.0;
actual = calculator.calculate(-100.0, -50.0, '+');
assertEquals(expected, actual);
}

@Test
void calculate_additionWithPositiveAndNegativeOperands_Ok() {
expected = - 150.0;
actual = calculator.calculate(-200.0, 50.0, '+');
assertEquals(expected, actual);
}

@Test
void calculate_additionWithFirstOperandIsZero_Ok() {
expected = 50.0;
actual = calculator.calculate(0.0, 50.0, '+');
assertEquals(expected, actual);
}

@Test
void calculate_additionWithSecondOperandIsZero_Ok() {
expected = 60.0;
actual = calculator.calculate(60.0, 0.0, '+');
assertEquals(expected, actual);
}

@Test
void calculate_additionWithMinAndMaxDoubleValues_Ok() {
expected = Double.MAX_VALUE + Double.MIN_VALUE;
actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+');
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_subtractionWithMinAndMaxDoubleValues_Ok() {
expected = Double.MAX_VALUE - Double.MIN_VALUE;
actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-');
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_multiplicationWithMinAndMaxDoubleValues_Ok() {
expected = Double.MAX_VALUE * Double.MIN_VALUE;
actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*');
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_divisionWithMinAndMaxDoubleValues_Ok() {
expected = Double.MAX_VALUE / Double.MIN_VALUE;
actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '/');
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_divisionByZero_NotOk() {
assertThrows(ArithmeticException.class, () -> {
calculator.calculate(2.0, 0.0, '/');
});
}

@Test
void calculate_raisingPositiveValueToThePositivePower_Ok() {
expected = 64.0;
actual = calculator.calculate(8.0, 2.0, '^');
assertEquals(expected, actual);
}

@Test
void calculate_raisingNegativeValueToThePositivePower_Ok() {
expected = 64.0;
actual = calculator.calculate(-8.0, 2.0, '^');
assertEquals(expected, actual);
}

@Test
void calculate_raisingPositiveValueToTheNegativePower_Ok() {
expected = 0.015625;
actual = calculator.calculate(8.0, -2.0, '^');
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_raisingNegativeValueToTheNegativePower_Ok() {
expected = 0.015625;
actual = calculator.calculate(-8.0, -2.0, '^');
assertEquals(expected, actual, DELTA);
}

@Test
void calculate_raisingPositiveValueToZeroPower_Ok() {
expected = 1.0;
actual = calculator.calculate(8.0, 0.0, '^');
assertEquals(expected, actual);
}

@Test
void calculate_raisingNegativeValueToZeroPower_Ok() {
expected = 1.0;
actual = calculator.calculate(-8.0, 0.0, '^');
assertEquals(expected, actual);
}

@Test
void calculate_raisingZeroToPower_Ok() {
expected = 0.0;
actual = calculator.calculate(0.0, 10.0, '^');
assertEquals(expected, actual);
}

@Test
void calculate_raisingZeroToNegativePower() {
expected = Double.POSITIVE_INFINITY;
actual = calculator.calculate(0, -2, '^');
assertEquals(expected, actual);
}

@Test
void illegalOperation_NotOk() {
assertThrows(IllegalArgumentException.class, () -> {
calculator.calculate(2.0, 3.0, '#');
});
}
}
8 changes: 0 additions & 8 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.