Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

public class Calculator {

public double calculate(double value1, double value2, char operation) {
Comment thread
taras-oleksiuk marked this conversation as resolved.
Outdated
switch (operation) {
case '+': return value1 + value2;
case '-': return value1 - value2;
case '*': return value1 * value2;
Comment thread
taras-oleksiuk marked this conversation as resolved.
Outdated
case '/':
if (value2 == 0) {
throw new ArithmeticException("Division by zero");
}
return value1 / value2;
case '^':
return Math.pow(value1, value2);
default:
throw new IllegalArgumentException("Illegal argument operation");
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

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

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class CalculatorTest {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

redundant empty line

private static Calculator calculator;
private final double firstPositiveOperand = 11.13;
private final double secondPositiveOperand = 12.14;
private final double firstNegativeOperand = -21.23;
private final double secondNegativeOperand = -22.24;
private final int zeroOperand = 0;
Comment thread
taras-oleksiuk marked this conversation as resolved.
Outdated

private final char addition = '+';
private final char subtraction = '-';
private final char division = '/';
private final char multiplication = '*';
private final char raisingToPower = '^';
private final char illegalOperation = '$';
Comment thread
taras-oleksiuk marked this conversation as resolved.
Outdated

@BeforeAll
static void beforeAll() {
calculator = new Calculator();
}

@Test
void additionPositiveOperands() {
Comment thread
taras-oleksiuk marked this conversation as resolved.
Outdated
double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, addition);
double expected = firstPositiveOperand + secondPositiveOperand;
assertEquals(expected, actual, "Test failed! Addition of two positive operands should be "
+ expected + " but was " + actual);
}

@Test
void additionNegativeOperands() {
double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, addition);
double expected = firstNegativeOperand + secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Addition of two negative operands should be "
+ expected + " but was " + actual);
}

@Test
void additionPositiveAndNegativeOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, addition);
double expected = firstPositiveOperand + secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative"
+ " operand should be " + expected + " but was " + actual);
}

@Test
void additionOperationWithOneZeroOperand() {
double actual = calculator.calculate(zeroOperand, secondNegativeOperand, addition);
double expected = zeroOperand + secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Addition of a zero operand and a negative"
+ " operand should be " + expected + " but was " + actual);

actual = calculator.calculate(firstPositiveOperand, zeroOperand, addition);
expected = firstPositiveOperand + zeroOperand;
assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero"
+ " operand should be " + expected + " but was " + actual);
}

@Test
void additionOperationWithMinOrMaxOperand() {
double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, addition);
double expected = Double.MIN_VALUE + Double.MAX_VALUE;
assertEquals(expected, actual, "Test failed! Addition of min value and max value"
+ " should be " + expected + " but was " + actual);
}

@Test
void subtractionPositiveOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand,
subtraction);
double expected = firstPositiveOperand - secondPositiveOperand;
assertEquals(expected, actual, "Test failed! Subtraction of two positive operands"
+ " should be " + expected + " but was " + actual);
}

@Test
void subtractionNegativeOperands() {
double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand,
subtraction);
double expected = firstNegativeOperand - secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Subtraction of two negative operands"
+ " should be " + expected + " but was " + actual);
}

@Test
void subtractionPositiveAndNegativeOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand,
subtraction);
double expected = firstPositiveOperand - secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a "
+ "negative operand should be " + expected + " but was " + actual);
}

@Test
void subtractionOperationWithOneZeroOperand() {
double actual = calculator.calculate(zeroOperand, secondNegativeOperand, subtraction);
double expected = zeroOperand - secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a negative"
+ " operand should be " + expected + " but was " + actual);

actual = calculator.calculate(firstPositiveOperand, zeroOperand, subtraction);
expected = firstPositiveOperand - zeroOperand;
assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a zero"
+ " operand should be " + expected + " but was " + actual);
}

@Test
void subtractionOperationWithMinOrMaxOperand() {
double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, subtraction);
double expected = Double.MIN_VALUE - Double.MAX_VALUE;
assertEquals(expected, actual, "Test failed! Subtraction of min value and max value"
+ " should be " + expected + " but was " + actual);
}

@Test
void divisionPositiveOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand, division);
double expected = firstPositiveOperand / secondPositiveOperand;
assertEquals(expected, actual, "Test failed! Division of two positive operands should be "
+ expected + " but was " + actual);
}

@Test
void divisionNegativeOperands() {
double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand, division);
double expected = firstNegativeOperand / secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Division of two negative operands should be "
+ expected + " but was " + actual);
}

@Test
void divisionPositiveAndNegativeOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand, division);
double expected = firstPositiveOperand / secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative"
+ " operand should be " + expected + " but was " + actual);
}

@Test
void divisionOperationWithOneZeroOperand() {
double actual = calculator.calculate(zeroOperand, secondNegativeOperand, division);
double expected = zeroOperand / secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Division of a zero operand and a negative"
+ " operand should be " + expected + " but was " + actual);

assertThrows(ArithmeticException.class, () ->
calculator.calculate(firstPositiveOperand, zeroOperand, division),
"Test failed! Division by zero must throw the Arithmetic exception.");
}

@Test
void divisionOperationWithMinOrMaxOperand() {
double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, division);
double expected = Double.MIN_VALUE / Double.MAX_VALUE;
assertEquals(expected, actual, "Test failed! Division of min value and max value"
+ " should be " + expected + " but was " + actual);
}

@Test
void multiplicationPositiveOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand,
multiplication);
double expected = firstPositiveOperand * secondPositiveOperand;
assertEquals(expected, actual, "Test failed! Multiplication of two positive operands"
+ " should be " + expected + " but was " + actual);
}

@Test
void multiplicationNegativeOperands() {
double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand,
multiplication);
double expected = firstNegativeOperand * secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Multiplication of two negative operands"
+ " should be " + expected + " but was " + actual);
}

@Test
void multiplicationPositiveAndNegativeOperands() {
double actual = calculator.calculate(firstPositiveOperand, secondNegativeOperand,
multiplication);
double expected = firstPositiveOperand * secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and"
+ " a negative operand should be " + expected + " but was " + actual);
}

@Test
void multiplicationOperationWithOneZeroOperand() {
double actual = calculator.calculate(zeroOperand, secondNegativeOperand, multiplication);
double expected = zeroOperand * secondNegativeOperand;
assertEquals(expected, actual, "Test failed! Multiplication of a zero operand and"
+ " a negative operand should be " + expected + " but was " + actual);

actual = calculator.calculate(firstPositiveOperand, zeroOperand, multiplication);
expected = firstPositiveOperand * zeroOperand;
assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and"
+ " a zero operand should be " + expected + " but was " + actual);
}

@Test
void multiplicationOperationWithMinOrMaxOperand() {
double actual = calculator.calculate(Double.MIN_VALUE, Double.MAX_VALUE, multiplication);
double expected = Double.MIN_VALUE * Double.MAX_VALUE;
assertEquals(expected, actual, "Test failed! Multiplication of min value and max value"
+ " should be " + expected + " but was " + actual);
}

@Test
void raisingToPositivePower() {
double actual = calculator.calculate(firstPositiveOperand, secondPositiveOperand,
raisingToPower);
double expected = Math.pow(firstPositiveOperand, secondPositiveOperand);
assertEquals(expected, actual, "Test failed! Raising to positive power should be "
+ expected + " but was " + actual);
}

@Test
void raisingToNegativePower() {
double actual = calculator.calculate(firstNegativeOperand, secondNegativeOperand,
raisingToPower);
double expected = Math.pow(firstNegativeOperand, secondNegativeOperand);
assertEquals(expected, actual, "Test failed! Raising to negative power should be "
+ expected + " but was " + actual);
}

@Test
void raisingToZeroPower() {
double actual = calculator.calculate(firstPositiveOperand, zeroOperand, raisingToPower);
double expected = Math.pow(firstPositiveOperand, zeroOperand);
assertEquals(expected, actual, "Test failed! Raising to zero power"
+ " should be " + expected + " but was " + actual);
}

@Test
void raisingZeroToPower() {
double actual = calculator.calculate(zeroOperand, secondNegativeOperand, raisingToPower);
double expected = Math.pow(zeroOperand, secondNegativeOperand);
assertEquals(expected, actual, "Test failed! Raising zero to negative power "
+ " should be " + expected + " but was " + actual);
}

@Test
void calculate_illegalOperation_notOk() {
assertThrows(IllegalArgumentException.class, () ->
calculator.calculate(firstPositiveOperand, secondPositiveOperand,
illegalOperation));
}
}
8 changes: 0 additions & 8 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.