From f2276f0e588b566a81c8075f511f6df2a2fe81c1 Mon Sep 17 00:00:00 2001 From: vadim9482 Date: Wed, 6 Oct 2021 22:18:53 +0300 Subject: [PATCH 1/3] Init commit --- .../core/basesyntax/{HelloWorld.java => Calculator.java} | 2 +- src/test/java/core/basesyntax/CalculatorTest.java | 7 +++++++ src/test/java/core/basesyntax/HelloWorldTest.java | 8 -------- 3 files changed, 8 insertions(+), 9 deletions(-) rename src/main/java/core/basesyntax/{HelloWorld.java => Calculator.java} (77%) create mode 100644 src/test/java/core/basesyntax/CalculatorTest.java delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/Calculator.java similarity index 77% rename from src/main/java/core/basesyntax/HelloWorld.java rename to src/main/java/core/basesyntax/Calculator.java index 97db782b..1fd1a0a9 100644 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -3,6 +3,6 @@ /** * Feel free to remove this class and create your own. */ -public class HelloWorld { +public class Calculator { } diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java new file mode 100644 index 00000000..b12f8cde --- /dev/null +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +import static org.junit.jupiter.api.Assertions.*; + +class CalculatorTest { + +} \ No newline at end of file diff --git a/src/test/java/core/basesyntax/HelloWorldTest.java b/src/test/java/core/basesyntax/HelloWorldTest.java deleted file mode 100644 index 9a90d822..00000000 --- a/src/test/java/core/basesyntax/HelloWorldTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorldTest { - -} From 521b77d2b2c6a884ad2c29a77d855746bad4f187 Mon Sep 17 00:00:00 2001 From: vadim9482 Date: Sat, 9 Oct 2021 23:00:03 +0300 Subject: [PATCH 2/3] Class calculator was created throw Interfac, some test were applied --- .../java/core/basesyntax/Calculating.java | 5 + src/main/java/core/basesyntax/Calculator.java | 36 +- .../exceptions/OperatorException.java | 7 + .../basesyntax/exceptions/ValueException.java | 7 + .../java/core/basesyntax/CalculatorTest.java | 328 +++++++++++++++++- 5 files changed, 377 insertions(+), 6 deletions(-) create mode 100644 src/main/java/core/basesyntax/Calculating.java create mode 100644 src/main/java/core/basesyntax/exceptions/OperatorException.java create mode 100644 src/main/java/core/basesyntax/exceptions/ValueException.java diff --git a/src/main/java/core/basesyntax/Calculating.java b/src/main/java/core/basesyntax/Calculating.java new file mode 100644 index 00000000..b820033d --- /dev/null +++ b/src/main/java/core/basesyntax/Calculating.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Calculating { + public double calculate(double a, double b, char operation); +} diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java index 1fd1a0a9..8b8bb0fa 100644 --- a/src/main/java/core/basesyntax/Calculator.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -1,8 +1,36 @@ package core.basesyntax; -/** - * Feel free to remove this class and create your own. - */ -public class Calculator { +import core.basesyntax.exceptions.OperatorException; +import core.basesyntax.exceptions.ValueException; +public class Calculator implements Calculating { + + @Override + public double calculate(double a, double b, char operation) { + double result = 0; + switch (operation) { + case '+': + result = a + b; + break; + case '-': + result = a - b; + break; + case '*': + result = a * b; + break; + case '/': + if (b == 0.00) { + throw new ValueException("Division by zero"); + } + result = a / b; + break; + case '^': + result = Math.pow(a, b); + break; + default: + throw new OperatorException("Wrong operator"); + } + System.out.println(a + " " + operation + " " + b + " = " + result); + return result; + } } diff --git a/src/main/java/core/basesyntax/exceptions/OperatorException.java b/src/main/java/core/basesyntax/exceptions/OperatorException.java new file mode 100644 index 00000000..19b02be6 --- /dev/null +++ b/src/main/java/core/basesyntax/exceptions/OperatorException.java @@ -0,0 +1,7 @@ +package core.basesyntax.exceptions; + +public class OperatorException extends RuntimeException { + public OperatorException(String message) { + super(message); + } +} diff --git a/src/main/java/core/basesyntax/exceptions/ValueException.java b/src/main/java/core/basesyntax/exceptions/ValueException.java new file mode 100644 index 00000000..079e6c20 --- /dev/null +++ b/src/main/java/core/basesyntax/exceptions/ValueException.java @@ -0,0 +1,7 @@ +package core.basesyntax.exceptions; + +public class ValueException extends RuntimeException { + public ValueException(String message) { + super(message); + } +} diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index b12f8cde..d72196eb 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -1,7 +1,331 @@ package core.basesyntax; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import core.basesyntax.exceptions.OperatorException; +import core.basesyntax.exceptions.ValueException; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; class CalculatorTest { + private static final Double MIN_DOUBLE_VALUE = Double.MIN_VALUE; + private static final Double MAX_DOUBLE_VALUE = Double.MAX_VALUE; + private static final Double POSITIVE_INFINITY = Double.POSITIVE_INFINITY; + private static final Double NEGATIVE_INFINITY = Double.NEGATIVE_INFINITY; + private static final char ILLEGAL_EXPRESSION = '?'; + private static Calculator calculator; + private double firstValue; + private double secondValue; + private double expected; + private double actual; + + @BeforeAll + static void beforeAll() { + calculator = new Calculator(); + } + + @Test + void calculate_additionForMinAndMaxDoubleValues() { + firstValue = MIN_DOUBLE_VALUE; + secondValue = MIN_DOUBLE_VALUE; + expected = 1E-323; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = POSITIVE_INFINITY; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + firstValue = MIN_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = POSITIVE_INFINITY; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + } + + @Test + void calculate_additionWithPositiveAndNegativeOperands_OK() { + firstValue = -100; + secondValue = 200; + expected = 100; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + } + + @Test + void calculate_additionWithTwoNegativeOperands_OK() { + firstValue = -100.00; + secondValue = -200.00; + expected = -300; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + } + + @Test + void calculate_additionWithTwoPositiveOperands_OK() { + firstValue = 125; + secondValue = 225; + expected = 350; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + + } + + @Test + void calculate_additionWithZeroInDifferentPlaces_OK() { + firstValue = 0; + secondValue = 5; + expected = 5; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + firstValue = -5; + secondValue = 0; + expected = -5; + actual = calculator.calculate(firstValue, secondValue, '+'); + assertEquals(expected, actual); + } + + @Test + void calculate_divisionForMinAndMaxDoubleValues_OK() { + firstValue = MIN_DOUBLE_VALUE; + secondValue = MIN_DOUBLE_VALUE; + expected = 1; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = 1; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + firstValue = MIN_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MIN_DOUBLE_VALUE; + expected = POSITIVE_INFINITY; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + } + + @Test + void calculate_divisionWithPositiveAndNegativeOperands_OK() { + firstValue = -25; + secondValue = 5; + expected = -5; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + } + + @Test + void calculate_divisionWithTwoNegativeOperands_OK() { + firstValue = -15; + secondValue = -5; + expected = 3; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + } + + @Test + void calculate_divisionWithTwoPositiveOperands_OK() { + firstValue = 75; + secondValue = 15; + expected = 5; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + } + + @Test + void calculate_divisionWithZeroInDifferentPlaces_OK() { + firstValue = 0; + secondValue = 125; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual); + firstValue = 125; + secondValue = 0; + assertThrows(ValueException.class, () -> + calculator.calculate(firstValue, secondValue, '/')); + } + + @Test + void calculate_illegalOperation_NotOK() { + assertThrows(OperatorException.class, () -> + calculator.calculate(firstValue, secondValue, ILLEGAL_EXPRESSION)); + } + + @Test + void calculate_multiplicationForMinAndMaxDoubleValues() { + firstValue = MIN_DOUBLE_VALUE; + secondValue = MIN_DOUBLE_VALUE; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = POSITIVE_INFINITY; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + firstValue = MIN_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + } + + @Test + void calculate_multiplicationWithPositiveAndNegativeOperands_OK() { + firstValue = -5; + secondValue = 5; + expected = -25; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + } + + @Test + void calculate_multiplicationWithTwoNegativeOperands_OK() { + firstValue = -25; + secondValue = -15; + expected = 375; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + } + + @Test + void calculate_multiplicationWithTwoPositiveOperands_OK() { + firstValue = 5; + secondValue = 5; + expected = 25; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + } + + @Test + void calculate_multiplicationWithZeroInDifferentPlaces_OK() { + firstValue = 0; + secondValue = 5; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + firstValue = 5; + secondValue = 0; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '*'); + assertEquals(expected, actual); + } + + @Test + void calculate_raisingNegativePositiveValueToTheNegativePower_OK() { + firstValue = -5; + secondValue = -4; + expected = 0; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual); + } + + @Test + void calculate_raisingNegativePositiveValueToThePositivePower_OK() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual); + } + + @Test + void calculate_raisingNegativePositiveValueToTheZeroPower_OK() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual); + } + + @Test + void calculate_raisingZeroToPower_OK() { + firstValue = 0; + secondValue = 5; + expected = -1; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual); + } + + @Test + void calculate_subtractionForMinAndMaxDoubleValues() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual); + } + + @Test + void calculate_subtractionWithPositiveAndNegativeOperands_OK() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual); + } + @Test + void calculate_subtractionWithTwoNegativeOperands_OK() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual); + } + @Test + void calculate_subtractionWithTwoPositiveOperands_OK() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual); + } + + @Test + void calculate_subtractionWithZeroInDifferentPlaces_OK() { + firstValue = -5; + secondValue = 5; + expected = firstValue + secondValue; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual); + } + + /* ; + addition for min and max double values; + addition with positive and negative operands; + addition with 2 negative operands; + addition with 2 positive operands; + addition with zero in different places; + + (check division by 0); + division for min and max double values; + division with positive and negative operands; + division with 2 negative operands; + division with 2 positive operands; + division with zero in different places; + + illegal operation; + + multiplication for min and max double values; + multiplication with positive and negative operands; + multiplication with 2 negative operands; + multiplication with 2 positive operands; + multiplication with zero in different places; + + raising positive/negative value to the negative power; + raising positive/negative value to the positive power; + raising positive/negative value to zero power; + raising zero to power -} \ No newline at end of file + subtraction for min and max double values; + subtraction with positive and negative operands; + subtraction with 2 negative operands; + subtraction with 2 positive operands; + subtraction with zero in different places; + */ +} From 5bcc892234c3780e20417215f1731456b73acd80 Mon Sep 17 00:00:00 2001 From: vadim9482 Date: Tue, 12 Oct 2021 23:19:27 +0300 Subject: [PATCH 3/3] All tests were implemented --- src/main/java/core/basesyntax/Calculator.java | 4 +- .../java/core/basesyntax/CalculatorTest.java | 335 ++++++++++-------- 2 files changed, 183 insertions(+), 156 deletions(-) diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java index 8b8bb0fa..6b727d17 100644 --- a/src/main/java/core/basesyntax/Calculator.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -25,12 +25,14 @@ public double calculate(double a, double b, char operation) { result = a / b; break; case '^': + if (a == 0 && b < 0) { + throw new ValueException("Zero cannot powered to negative value"); + } result = Math.pow(a, b); break; default: throw new OperatorException("Wrong operator"); } - System.out.println(a + " " + operation + " " + b + " = " + result); return result; } } diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index d72196eb..a7bb613e 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -9,10 +9,10 @@ import org.junit.jupiter.api.Test; class CalculatorTest { + private static final double DELTA = 1e-13; private static final Double MIN_DOUBLE_VALUE = Double.MIN_VALUE; private static final Double MAX_DOUBLE_VALUE = Double.MAX_VALUE; private static final Double POSITIVE_INFINITY = Double.POSITIVE_INFINITY; - private static final Double NEGATIVE_INFINITY = Double.NEGATIVE_INFINITY; private static final char ILLEGAL_EXPRESSION = '?'; private static Calculator calculator; private double firstValue; @@ -29,123 +29,126 @@ static void beforeAll() { void calculate_additionForMinAndMaxDoubleValues() { firstValue = MIN_DOUBLE_VALUE; secondValue = MIN_DOUBLE_VALUE; - expected = 1E-323; + expected = 1.0E-323; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); - firstValue = MAX_DOUBLE_VALUE; + assertEquals(expected, actual, DELTA); + firstValue = MIN_DOUBLE_VALUE; secondValue = MAX_DOUBLE_VALUE; - expected = POSITIVE_INFINITY; + expected = MAX_DOUBLE_VALUE; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); - firstValue = MIN_DOUBLE_VALUE; + assertEquals(expected, actual, DELTA); + firstValue = MAX_DOUBLE_VALUE; secondValue = MAX_DOUBLE_VALUE; expected = POSITIVE_INFINITY; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_additionWithPositiveAndNegativeOperands_OK() { - firstValue = -100; - secondValue = 200; - expected = 100; + firstValue = -100.1; + secondValue = 200.2; + expected = 100.1; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_additionWithTwoNegativeOperands_OK() { - firstValue = -100.00; - secondValue = -200.00; - expected = -300; + firstValue = -100.10; + secondValue = -200.20; + expected = -300.3; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_additionWithTwoPositiveOperands_OK() { - firstValue = 125; - secondValue = 225; - expected = 350; + firstValue = 0.1; + secondValue = 0.2; + expected = 0.3; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); - + assertEquals(expected, actual, DELTA); } @Test void calculate_additionWithZeroInDifferentPlaces_OK() { - firstValue = 0; - secondValue = 5; - expected = 5; + firstValue = 0.0; + secondValue = 5.0; + expected = 5.0; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); - firstValue = -5; - secondValue = 0; - expected = -5; + assertEquals(expected, actual, DELTA); + firstValue = -5.0; + secondValue = 0.0; + expected = -5.0; actual = calculator.calculate(firstValue, secondValue, '+'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_divisionForMinAndMaxDoubleValues_OK() { firstValue = MIN_DOUBLE_VALUE; secondValue = MIN_DOUBLE_VALUE; - expected = 1; - actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); - firstValue = MAX_DOUBLE_VALUE; - secondValue = MAX_DOUBLE_VALUE; - expected = 1; + expected = 1.0; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); firstValue = MIN_DOUBLE_VALUE; secondValue = MAX_DOUBLE_VALUE; - expected = 0; + expected = 0.0; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); firstValue = MAX_DOUBLE_VALUE; secondValue = MIN_DOUBLE_VALUE; expected = POSITIVE_INFINITY; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = 1; + actual = calculator.calculate(firstValue, secondValue, '/'); + assertEquals(expected, actual, DELTA); } @Test void calculate_divisionWithPositiveAndNegativeOperands_OK() { - firstValue = -25; - secondValue = 5; - expected = -5; + firstValue = -25.5; + secondValue = 5.1; + expected = -5.0; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_divisionWithTwoNegativeOperands_OK() { - firstValue = -15; - secondValue = -5; + firstValue = -15.15; + secondValue = -5.05; expected = 3; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_divisionWithTwoPositiveOperands_OK() { - firstValue = 75; - secondValue = 15; - expected = 5; + firstValue = 75.5; + secondValue = 15.1; + expected = 5.0; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test - void calculate_divisionWithZeroInDifferentPlaces_OK() { - firstValue = 0; - secondValue = 125; - expected = 0; + void calculate_divisionZero_OK() { + firstValue = 0.0; + secondValue = 125.0; + expected = 0.0; actual = calculator.calculate(firstValue, secondValue, '/'); - assertEquals(expected, actual); - firstValue = 125; - secondValue = 0; + assertEquals(expected, actual, DELTA); + } + + @Test + void calculate_divisionByZero_NotOK() { + firstValue = 125.0; + secondValue = 0.0; assertThrows(ValueException.class, () -> calculator.calculate(firstValue, secondValue, '/')); } @@ -160,172 +163,194 @@ void calculate_illegalOperation_NotOK() { void calculate_multiplicationForMinAndMaxDoubleValues() { firstValue = MIN_DOUBLE_VALUE; secondValue = MIN_DOUBLE_VALUE; - expected = 0; + expected = 0.00; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); - firstValue = MAX_DOUBLE_VALUE; + assertEquals(expected, actual, DELTA); + firstValue = MIN_DOUBLE_VALUE; secondValue = MAX_DOUBLE_VALUE; - expected = POSITIVE_INFINITY; + expected = 8.881784197001251E-16; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); - firstValue = MIN_DOUBLE_VALUE; + assertEquals(expected, actual, DELTA); + firstValue = MAX_DOUBLE_VALUE; secondValue = MAX_DOUBLE_VALUE; - expected = 0; + expected = POSITIVE_INFINITY; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_multiplicationWithPositiveAndNegativeOperands_OK() { - firstValue = -5; - secondValue = 5; - expected = -25; + firstValue = -5.05; + secondValue = 5.05; + expected = -25.5025; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_multiplicationWithTwoNegativeOperands_OK() { - firstValue = -25; - secondValue = -15; - expected = 375; + firstValue = -25.5; + secondValue = -15.5; + expected = 395.25; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_multiplicationWithTwoPositiveOperands_OK() { - firstValue = 5; - secondValue = 5; - expected = 25; + firstValue = 0.1; + secondValue = 0.1; + expected = 0.01; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_multiplicationWithZeroInDifferentPlaces_OK() { - firstValue = 0; - secondValue = 5; - expected = 0; + firstValue = 0.0; + secondValue = 5.0; + expected = 0.0; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); - firstValue = 5; - secondValue = 0; - expected = 0; + assertEquals(expected, actual, DELTA); + firstValue = -5.0; + secondValue = 0.0; + expected = 0.0; actual = calculator.calculate(firstValue, secondValue, '*'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_raisingNegativePositiveValueToTheNegativePower_OK() { - firstValue = -5; - secondValue = -4; - expected = 0; + firstValue = -5.0; + secondValue = -4.0; + expected = 0.0016; actual = calculator.calculate(firstValue, secondValue, '^'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); + firstValue = 5.0; + secondValue = -4.0; + expected = 0.0016; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual, DELTA); } @Test void calculate_raisingNegativePositiveValueToThePositivePower_OK() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = -5.0; + secondValue = 5.0; + expected = -3125.0; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual, DELTA); + firstValue = 5.0; + secondValue = 2.0; + expected = 25.0; actual = calculator.calculate(firstValue, secondValue, '^'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_raisingNegativePositiveValueToTheZeroPower_OK() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = -5.0; + secondValue = 0.0; + expected = 1.0; + actual = calculator.calculate(firstValue, secondValue, '^'); + assertEquals(expected, actual, DELTA); + firstValue = -5.0; + secondValue = 0.0; + expected = 1.0; actual = calculator.calculate(firstValue, secondValue, '^'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); + } + + @Test + void calculate_raisingZeroToNegativePower_NotOK() { + firstValue = 0.0; + secondValue = -5.0; + assertThrows(ValueException.class, + () -> calculator.calculate(firstValue, secondValue, '^')); } @Test - void calculate_raisingZeroToPower_OK() { - firstValue = 0; - secondValue = 5; - expected = -1; + void calculate_raisingZeroToPositivePower_OK() { + firstValue = 0.0; + secondValue = 5.0; + expected = 0.0; actual = calculator.calculate(firstValue, secondValue, '^'); - assertEquals(expected, actual); } @Test void calculate_subtractionForMinAndMaxDoubleValues() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = MIN_DOUBLE_VALUE; + secondValue = MIN_DOUBLE_VALUE; + expected = 0.0; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual, DELTA); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = 0.0; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual, DELTA); + firstValue = MIN_DOUBLE_VALUE; + secondValue = MAX_DOUBLE_VALUE; + expected = -MAX_DOUBLE_VALUE; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual, DELTA); + firstValue = MAX_DOUBLE_VALUE; + secondValue = MIN_DOUBLE_VALUE; + expected = MAX_DOUBLE_VALUE; actual = calculator.calculate(firstValue, secondValue, '-'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_subtractionWithPositiveAndNegativeOperands_OK() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = -0.3; + secondValue = 0.2; + expected = -0.5; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual, DELTA); + firstValue = 5.0; + secondValue = -5.0; + expected = 10.0; actual = calculator.calculate(firstValue, secondValue, '-'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } + @Test void calculate_subtractionWithTwoNegativeOperands_OK() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = -25.0; + secondValue = -5.0; + expected = -20.0; actual = calculator.calculate(firstValue, secondValue, '-'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } + @Test void calculate_subtractionWithTwoPositiveOperands_OK() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = 0.3; + secondValue = 0.2; + expected = 0.1; actual = calculator.calculate(firstValue, secondValue, '-'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } @Test void calculate_subtractionWithZeroInDifferentPlaces_OK() { - firstValue = -5; - secondValue = 5; - expected = firstValue + secondValue; + firstValue = -5.0; + secondValue = 0.0; + expected = -5.0; + actual = calculator.calculate(firstValue, secondValue, '-'); + assertEquals(expected, actual, DELTA); + firstValue = 0.0; + secondValue = 5.0; + expected = -5.0; actual = calculator.calculate(firstValue, secondValue, '-'); - assertEquals(expected, actual); + assertEquals(expected, actual, DELTA); } - /* ; - addition for min and max double values; - addition with positive and negative operands; - addition with 2 negative operands; - addition with 2 positive operands; - addition with zero in different places; - - (check division by 0); - division for min and max double values; - division with positive and negative operands; - division with 2 negative operands; - division with 2 positive operands; - division with zero in different places; - - illegal operation; - - multiplication for min and max double values; - multiplication with positive and negative operands; - multiplication with 2 negative operands; - multiplication with 2 positive operands; - multiplication with zero in different places; - - raising positive/negative value to the negative power; - raising positive/negative value to the positive power; - raising positive/negative value to zero power; - raising zero to power - - subtraction for min and max double values; - subtraction with positive and negative operands; - subtraction with 2 negative operands; - subtraction with 2 positive operands; - subtraction with zero in different places; - */ + @Test + void doubleComparing0andZero() { + firstValue = MIN_DOUBLE_VALUE; + secondValue = 0.0; + assertEquals(firstValue, secondValue, DELTA); + } }