From ec4432647e541832de5db6358748a915b6e470b3 Mon Sep 17 00:00:00 2001 From: Nazar Hetun Date: Tue, 14 Sep 2021 12:16:36 +0300 Subject: [PATCH 1/3] implement calculator --- src/main/java/core/basesyntax/Calculator.java | 27 ++ src/main/java/core/basesyntax/HelloWorld.java | 8 - .../java/core/basesyntax/CalculatorTest.java | 302 ++++++++++++++++++ .../java/core/basesyntax/HelloWorldTest.java | 8 - 4 files changed, 329 insertions(+), 16 deletions(-) create mode 100644 src/main/java/core/basesyntax/Calculator.java delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java 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/Calculator.java b/src/main/java/core/basesyntax/Calculator.java new file mode 100644 index 00000000..a388db2c --- /dev/null +++ b/src/main/java/core/basesyntax/Calculator.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class Calculator { + + public double calculate(double a, double b, char c) { + switch (c) { + case '+': + return a + b; + case '-': + return a - b; + case '/': + if (b == 0) { + throw new RuntimeException("Error, division by 0"); + } + return a / b; + case '*': + return a * b; + case '^': + if (a < 0 && b != (long) b) { + throw new RuntimeException("Error"); + } + return Math.pow(a, b); + default: + throw new RuntimeException("Error, unknown operation type"); + } + } +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 97db782b..00000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - -} diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java new file mode 100644 index 00000000..c5731081 --- /dev/null +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -0,0 +1,302 @@ +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; + +public class CalculatorTest { + private static Calculator calculator; + + @BeforeAll + static void beforeAll() { + calculator = new Calculator(); + } + + /* + Tests for additions. + */ + + @Test + void additionWithTwoPositiveOperands_isOk() { + double a = 23.4; + double b = 700.5; + double actual = calculator.calculate(a, b, '+'); + double expected = 723.9; + assertEquals(expected, actual); + } + + @Test + void additionWithTwoNegativeOperands_isOk() { + double a = -23.4; + double b = -700.5; + double expected = -723.9; + double actual = calculator.calculate(a, b, '+'); + assertEquals(expected, actual); + } + + @Test + void additionWithPositiveAndNegativeOperands_isOk() { + double a = -23.4; + double b = 700.5; + double expected = 677.1; + double actual = calculator.calculate(a, b, '+'); + assertEquals(expected, actual); + } + + @Test + void additionWithZeroInDifferentPlaces_isOk() { + double a = 0; + double b = 700.5; + double expected = 700.5; + double actual = calculator.calculate(a, b, '+'); + assertEquals(expected, actual); + a = 321.45; + b = 0; + expected = 321.45; + actual = calculator.calculate(a, b, '+'); + assertEquals(expected, actual); + } + + @Test + void additionForMinAndMaxDoubleValues_isOk() { + double a = Double.MAX_VALUE; + double b = Double.MIN_VALUE; + double delta = 0.0001; + double actual = calculator.calculate(a, b, '+'); + assertEquals(a, actual, delta); + } + + /* + Tests for subtractions. + */ + + @Test + void subtractionWithTwoPositiveOperands_isOk() { + double a = 23.4; + double b = 700.5; + double actual = calculator.calculate(a, b, '-'); + double expected = -677.1; + assertEquals(expected, actual); + } + + @Test + void subtractionWithTwoNegativeOperands_isOk() { + double a = -23.4; + double b = -700.5; + double expected = 677.1; + double actual = calculator.calculate(a, b, '-'); + assertEquals(expected, actual); + } + + @Test + void subtractionWithPositiveAndNegativeOperands_isOk() { + double a = -23.4; + double b = 700.5; + double expected = -723.9; + double actual = calculator.calculate(a, b, '-'); + assertEquals(expected, actual); + } + + @Test + void subtractionWithZeroInDifferentPlaces_isOk() { + double a = 0; + double b = 700.5; + double expected = -700.5; + double actual = calculator.calculate(a, b, '-'); + assertEquals(expected, actual); + a = 321.45; + b = 0; + expected = 321.45; + actual = calculator.calculate(a, b, '-'); + assertEquals(expected, actual); + } + + @Test + void subtractionForMinAndMaxDoubleValues_isOk() { + double a = Double.MAX_VALUE; + double b = Double.MIN_VALUE; + double actual = calculator.calculate(a, b, '-'); + double expected = Double.MAX_VALUE; + double delta = 0.0001; + assertEquals(expected, actual, delta); + } + + /* + Tests for multiplication. + */ + + @Test + void multiplicationWithTwoPositiveOperands_isOk() { + double a = 23.4; + double b = 700.5; + double actual = calculator.calculate(a, b, '*'); + double expected = 16391.7; + assertEquals(expected, actual); + } + + @Test + void multiplicationWithTwoNegativeOperands_isOk() { + double a = -23.4; + double b = -700.5; + double expected = 16391.7; + double actual = calculator.calculate(a, b, '*'); + assertEquals(expected, actual); + } + + @Test + void multiplicationWithPositiveAndNegativeOperands_isOk() { + double a = -23.4; + double b = 700.5; + double expected = -16391.7; + double actual = calculator.calculate(a, b, '*'); + assertEquals(expected, actual); + } + + @Test + void multiplicationWithZeroInDifferentPlaces_isOk() { + double a = 0; + double b = 700.5; + double expected = 0; + double actual = calculator.calculate(a, b, '*'); + assertEquals(expected, actual); + a = 321.45; + b = 0; + actual = calculator.calculate(a, b, '*'); + assertEquals(expected, actual); + } + + @Test + void multiplicationForMinAndMaxDoubleValues_isOk() { + double a = Double.MAX_VALUE; + double b = Double.MIN_VALUE; + double actual = calculator.calculate(a, b, '*'); + double delta = 0.0001; + assertEquals(0, actual, delta); + } + + /* + Tests for division. + */ + + @Test + void divisionWithTwoPositiveOperands_isOk() { + double a = 8; + double b = 25; + double expected = 0.32; + double actual = calculator.calculate(a, b, '/'); + assertEquals(expected, actual); + } + + @Test + void divisionWithTwoNegativeOperands_isOk() { + double a = -8; + double b = -25; + double expected = 0.32; + double actual = calculator.calculate(a, b, '/'); + assertEquals(expected, actual); + } + + @Test + void divisionWithPositiveAndNegativeOperands_isOk() { + double a = 8; + double b = -25; + double expected = -0.32; + double actual = calculator.calculate(a, b, '/'); + assertEquals(expected, actual); + } + + @Test + void divisionWithZeroInDifferentPlaces() { + double a = 0; + double b = 25; + double expected = 0; + double actual = calculator.calculate(a, b, '/'); + assertEquals(expected, actual); + double anotherA = 8; + double anotherB = 0; + assertThrows(RuntimeException.class, () -> calculator.calculate(anotherA, anotherB, '/')); + } + + /* + Tests for ^ operation. + */ + + @Test + void raisingForMinAndMaxDoubleValues() { + double a = Double.MAX_VALUE; + double b = Double.MIN_VALUE; + double expected = 1; + double delta = 0.0001; + double actual = calculator.calculate(a, b, '^'); + assertEquals(expected, actual, delta); + a = Double.MIN_VALUE; + b = Double.MAX_VALUE; + actual = calculator.calculate(a, b, '^'); + expected = 0; + assertEquals(expected, actual, delta); + } + + @Test + void raisingPositiveOrNegativeValueToThePositivePower() { + final double b = 2.5; + double a = 4.5; + double expected = 42.956737; + double delta = 0.0001; + double actual = calculator.calculate(a, b, '^'); + assertEquals(expected, actual, delta); + double negativeA = -4.5; + assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^')); + + } + + @Test + void raisingPositiveOrNegativeValueToTheNegativePower() { + double a = 4.5; + double b = -2.2; + double expected = 0.03655; + double delta = 0.0001; + double actual = calculator.calculate(a, b, '^'); + assertEquals(expected, actual, delta); + double negativeA = -4.5; + assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^')); + } + + @Test + void raisingPositiveOrNegativeValueToTheZeroPower() { + double a = 4.5; + double b = 0; + double expected = 1; + double actual = calculator.calculate(a, b, '^'); + assertEquals(expected, actual); + a = -4.5; + expected = 1; + actual = calculator.calculate(a, b, '^'); + assertEquals(expected, actual); + } + + @Test + void raisingZeroToPower() { + double a = 0; + double b = 4.5; + double expected = 0; + double actual = calculator.calculate(a, b, '^'); + assertEquals(expected, actual); + } + + /* + Tests for illegal operation. + */ + + @Test + void illegalOperations_ExceptionOk() { + double finalA = 24; + double finalB = -8; + assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '=')); + assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '!')); + assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '$')); + assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '%')); + assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '(')); + } +} 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 552c0f9554c7c3df1b8e6c2092dee37ad1d1c815 Mon Sep 17 00:00:00 2001 From: Nazar Hetun Date: Tue, 14 Sep 2021 13:49:43 +0300 Subject: [PATCH 2/3] Add constants and other minor improvements --- src/main/java/core/basesyntax/Calculator.java | 18 +- .../java/core/basesyntax/CalculatorTest.java | 257 +++++++----------- 2 files changed, 103 insertions(+), 172 deletions(-) diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java index a388db2c..c0480d4c 100644 --- a/src/main/java/core/basesyntax/Calculator.java +++ b/src/main/java/core/basesyntax/Calculator.java @@ -2,24 +2,24 @@ public class Calculator { - public double calculate(double a, double b, char c) { - switch (c) { + public double calculate(double firstValue, double secondValue, char operation) { + switch (operation) { case '+': - return a + b; + return firstValue + secondValue; case '-': - return a - b; + return firstValue - secondValue; case '/': - if (b == 0) { + if (secondValue == 0) { throw new RuntimeException("Error, division by 0"); } - return a / b; + return firstValue / secondValue; case '*': - return a * b; + return firstValue * secondValue; case '^': - if (a < 0 && b != (long) b) { + if (firstValue < 0 && secondValue != (long) secondValue) { throw new RuntimeException("Error"); } - return Math.pow(a, b); + return Math.pow(firstValue, secondValue); default: throw new RuntimeException("Error, unknown operation type"); } diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index c5731081..b9316b8e 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -8,295 +8,226 @@ public class CalculatorTest { private static Calculator calculator; + private static final double DELTA = 0.0001; + private static final double POSITIVE_NUMBER_1 = 23.4; + private static final double POSITIVE_NUMBER_2 = 700.5; + private static final double POSITIVE_NUMBER_3 = 321.45; + private static final double POSITIVE_NUMBER_4 = 8; + private static final double POSITIVE_NUMBER_5 = 25; + private static final double POSITIVE_NUMBER_6 = 2.5; + private static final double POSITIVE_NUMBER_7 = 4.5; + private static final double NEGATIVE_NUMBER_1 = -23.4; + private static final double NEGATIVE_NUMBER_2 = -700.5; + private static final double NEGATIVE_NUMBER_3 = -8; + private static final double NEGATIVE_NUMBER_4 = -25; + private static final double NEGATIVE_NUMBER_5 = -2.5; + private static final double ZERO_NUMBER = 0; + private double actual; + private double expected; @BeforeAll static void beforeAll() { calculator = new Calculator(); } - /* - Tests for additions. - */ - @Test void additionWithTwoPositiveOperands_isOk() { - double a = 23.4; - double b = 700.5; - double actual = calculator.calculate(a, b, '+'); - double expected = 723.9; + actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '+'); + expected = 723.9; assertEquals(expected, actual); } @Test void additionWithTwoNegativeOperands_isOk() { - double a = -23.4; - double b = -700.5; - double expected = -723.9; - double actual = calculator.calculate(a, b, '+'); + expected = -723.9; + actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '+'); assertEquals(expected, actual); } @Test void additionWithPositiveAndNegativeOperands_isOk() { - double a = -23.4; - double b = 700.5; - double expected = 677.1; - double actual = calculator.calculate(a, b, '+'); + expected = 677.1; + actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '+'); assertEquals(expected, actual); } @Test void additionWithZeroInDifferentPlaces_isOk() { - double a = 0; - double b = 700.5; - double expected = 700.5; - double actual = calculator.calculate(a, b, '+'); + expected = POSITIVE_NUMBER_2; + actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '+'); assertEquals(expected, actual); - a = 321.45; - b = 0; - expected = 321.45; - actual = calculator.calculate(a, b, '+'); + expected = POSITIVE_NUMBER_3; + actual = calculator.calculate(POSITIVE_NUMBER_3, ZERO_NUMBER, '+'); assertEquals(expected, actual); } @Test void additionForMinAndMaxDoubleValues_isOk() { - double a = Double.MAX_VALUE; - double b = Double.MIN_VALUE; - double delta = 0.0001; - double actual = calculator.calculate(a, b, '+'); - assertEquals(a, actual, delta); + expected = Double.MAX_VALUE; + actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+'); + assertEquals(expected, actual, DELTA); } - /* - Tests for subtractions. - */ - @Test void subtractionWithTwoPositiveOperands_isOk() { - double a = 23.4; - double b = 700.5; - double actual = calculator.calculate(a, b, '-'); - double expected = -677.1; + actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '-'); + expected = -677.1; assertEquals(expected, actual); } @Test void subtractionWithTwoNegativeOperands_isOk() { - double a = -23.4; - double b = -700.5; - double expected = 677.1; - double actual = calculator.calculate(a, b, '-'); + expected = 677.1; + actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '-'); assertEquals(expected, actual); } @Test void subtractionWithPositiveAndNegativeOperands_isOk() { - double a = -23.4; - double b = 700.5; - double expected = -723.9; - double actual = calculator.calculate(a, b, '-'); + expected = -723.9; + actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '-'); assertEquals(expected, actual); } @Test void subtractionWithZeroInDifferentPlaces_isOk() { - double a = 0; - double b = 700.5; - double expected = -700.5; - double actual = calculator.calculate(a, b, '-'); + expected = NEGATIVE_NUMBER_2; + actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '-'); assertEquals(expected, actual); - a = 321.45; - b = 0; - expected = 321.45; - actual = calculator.calculate(a, b, '-'); + expected = POSITIVE_NUMBER_3; + actual = calculator.calculate(POSITIVE_NUMBER_3, ZERO_NUMBER, '-'); assertEquals(expected, actual); } @Test void subtractionForMinAndMaxDoubleValues_isOk() { - double a = Double.MAX_VALUE; - double b = Double.MIN_VALUE; - double actual = calculator.calculate(a, b, '-'); - double expected = Double.MAX_VALUE; - double delta = 0.0001; - assertEquals(expected, actual, delta); + actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-'); + expected = Double.MAX_VALUE; + assertEquals(expected, actual, DELTA); } - /* - Tests for multiplication. - */ - @Test void multiplicationWithTwoPositiveOperands_isOk() { - double a = 23.4; - double b = 700.5; - double actual = calculator.calculate(a, b, '*'); - double expected = 16391.7; + actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '*'); + expected = 16391.7; assertEquals(expected, actual); } @Test void multiplicationWithTwoNegativeOperands_isOk() { - double a = -23.4; - double b = -700.5; - double expected = 16391.7; - double actual = calculator.calculate(a, b, '*'); + expected = 16391.7; + actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '*'); assertEquals(expected, actual); } @Test void multiplicationWithPositiveAndNegativeOperands_isOk() { - double a = -23.4; - double b = 700.5; - double expected = -16391.7; - double actual = calculator.calculate(a, b, '*'); + expected = -16391.7; + actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '*'); assertEquals(expected, actual); } @Test void multiplicationWithZeroInDifferentPlaces_isOk() { - double a = 0; - double b = 700.5; - double expected = 0; - double actual = calculator.calculate(a, b, '*'); + expected = ZERO_NUMBER; + actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '*'); assertEquals(expected, actual); - a = 321.45; - b = 0; - actual = calculator.calculate(a, b, '*'); + actual = calculator.calculate(POSITIVE_NUMBER_3, ZERO_NUMBER, '*'); assertEquals(expected, actual); } @Test void multiplicationForMinAndMaxDoubleValues_isOk() { - double a = Double.MAX_VALUE; - double b = Double.MIN_VALUE; - double actual = calculator.calculate(a, b, '*'); - double delta = 0.0001; - assertEquals(0, actual, delta); + expected = ZERO_NUMBER; + actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*'); + assertEquals(expected, actual, DELTA); } - /* - Tests for division. - */ - @Test void divisionWithTwoPositiveOperands_isOk() { - double a = 8; - double b = 25; - double expected = 0.32; - double actual = calculator.calculate(a, b, '/'); + expected = 0.32; + actual = calculator.calculate(POSITIVE_NUMBER_4, POSITIVE_NUMBER_5, '/'); assertEquals(expected, actual); } @Test void divisionWithTwoNegativeOperands_isOk() { - double a = -8; - double b = -25; - double expected = 0.32; - double actual = calculator.calculate(a, b, '/'); + expected = 0.32; + actual = calculator.calculate(NEGATIVE_NUMBER_3, NEGATIVE_NUMBER_4, '/'); assertEquals(expected, actual); } @Test void divisionWithPositiveAndNegativeOperands_isOk() { - double a = 8; - double b = -25; - double expected = -0.32; - double actual = calculator.calculate(a, b, '/'); + expected = -0.32; + actual = calculator.calculate(POSITIVE_NUMBER_4, NEGATIVE_NUMBER_4, '/'); assertEquals(expected, actual); } @Test void divisionWithZeroInDifferentPlaces() { - double a = 0; - double b = 25; - double expected = 0; - double actual = calculator.calculate(a, b, '/'); + expected = ZERO_NUMBER; + actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_5, '/'); assertEquals(expected, actual); - double anotherA = 8; - double anotherB = 0; - assertThrows(RuntimeException.class, () -> calculator.calculate(anotherA, anotherB, '/')); + assertThrows(RuntimeException.class, + () -> calculator.calculate(POSITIVE_NUMBER_4, ZERO_NUMBER, '/')); } - /* - Tests for ^ operation. - */ - @Test void raisingForMinAndMaxDoubleValues() { - double a = Double.MAX_VALUE; - double b = Double.MIN_VALUE; - double expected = 1; - double delta = 0.0001; - double actual = calculator.calculate(a, b, '^'); - assertEquals(expected, actual, delta); - a = Double.MIN_VALUE; - b = Double.MAX_VALUE; - actual = calculator.calculate(a, b, '^'); - expected = 0; - assertEquals(expected, actual, delta); + expected = 1; + actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '^'); + assertEquals(expected, actual, DELTA); + actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '^'); + assertEquals(expected, actual, DELTA); } @Test void raisingPositiveOrNegativeValueToThePositivePower() { - final double b = 2.5; - double a = 4.5; - double expected = 42.956737; - double delta = 0.0001; - double actual = calculator.calculate(a, b, '^'); - assertEquals(expected, actual, delta); - double negativeA = -4.5; - assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^')); + expected = 61.763235; + actual = calculator.calculate(POSITIVE_NUMBER_6, POSITIVE_NUMBER_7, '^'); + assertEquals(expected, actual, DELTA); + assertThrows(RuntimeException.class, + () -> calculator.calculate(NEGATIVE_NUMBER_5, POSITIVE_NUMBER_7, '^')); } @Test void raisingPositiveOrNegativeValueToTheNegativePower() { - double a = 4.5; - double b = -2.2; - double expected = 0.03655; - double delta = 0.0001; - double actual = calculator.calculate(a, b, '^'); - assertEquals(expected, actual, delta); - double negativeA = -4.5; - assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^')); + expected = 0.02327; + actual = calculator.calculate(POSITIVE_NUMBER_7, NEGATIVE_NUMBER_5, '^'); + assertEquals(expected, actual, DELTA); + assertThrows(RuntimeException.class, + () -> calculator.calculate(NEGATIVE_NUMBER_5, NEGATIVE_NUMBER_5, '^')); } @Test void raisingPositiveOrNegativeValueToTheZeroPower() { - double a = 4.5; - double b = 0; - double expected = 1; - double actual = calculator.calculate(a, b, '^'); - assertEquals(expected, actual); - a = -4.5; expected = 1; - actual = calculator.calculate(a, b, '^'); + actual = calculator.calculate(POSITIVE_NUMBER_7, ZERO_NUMBER, '^'); + assertEquals(expected, actual); + actual = calculator.calculate(NEGATIVE_NUMBER_5, ZERO_NUMBER, '^'); assertEquals(expected, actual); } @Test void raisingZeroToPower() { - double a = 0; - double b = 4.5; - double expected = 0; - double actual = calculator.calculate(a, b, '^'); + expected = ZERO_NUMBER; + actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_7, '^'); assertEquals(expected, actual); } - /* - Tests for illegal operation. - */ - @Test void illegalOperations_ExceptionOk() { - double finalA = 24; - double finalB = -8; - assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '=')); - assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '!')); - assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '$')); - assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '%')); - assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '(')); + assertThrows(RuntimeException.class, + () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '=')); + assertThrows(RuntimeException.class, + () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '!')); + assertThrows(RuntimeException.class, + () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '$')); + assertThrows(RuntimeException.class, + () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '%')); + assertThrows(RuntimeException.class, + () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '(')); } } From 4b92f89890dbc64d824a01f4239a891899599195 Mon Sep 17 00:00:00 2001 From: Nazar Hetun Date: Tue, 14 Sep 2021 15:18:56 +0300 Subject: [PATCH 3/3] rename tests names, rewrite calculate_raisingZeroToPower test --- .../java/core/basesyntax/CalculatorTest.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/test/java/core/basesyntax/CalculatorTest.java b/src/test/java/core/basesyntax/CalculatorTest.java index b9316b8e..dc47302f 100644 --- a/src/test/java/core/basesyntax/CalculatorTest.java +++ b/src/test/java/core/basesyntax/CalculatorTest.java @@ -31,28 +31,28 @@ static void beforeAll() { } @Test - void additionWithTwoPositiveOperands_isOk() { + void calculate_additionWithTwoPositiveOperands_isOk() { actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '+'); expected = 723.9; assertEquals(expected, actual); } @Test - void additionWithTwoNegativeOperands_isOk() { + void calculate_additionWithTwoNegativeOperands_isOk() { expected = -723.9; actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '+'); assertEquals(expected, actual); } @Test - void additionWithPositiveAndNegativeOperands_isOk() { + void calculate_additionWithPositiveAndNegativeOperands_isOk() { expected = 677.1; actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '+'); assertEquals(expected, actual); } @Test - void additionWithZeroInDifferentPlaces_isOk() { + void calculate_additionWithZeroInDifferentPlaces_isOk() { expected = POSITIVE_NUMBER_2; actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '+'); assertEquals(expected, actual); @@ -62,35 +62,35 @@ void additionWithZeroInDifferentPlaces_isOk() { } @Test - void additionForMinAndMaxDoubleValues_isOk() { + void calculate_additionForMinAndMaxDoubleValues_isOk() { expected = Double.MAX_VALUE; actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '+'); assertEquals(expected, actual, DELTA); } @Test - void subtractionWithTwoPositiveOperands_isOk() { + void calculate_subtractionWithTwoPositiveOperands_isOk() { actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '-'); expected = -677.1; assertEquals(expected, actual); } @Test - void subtractionWithTwoNegativeOperands_isOk() { + void calculate_subtractionWithTwoNegativeOperands_isOk() { expected = 677.1; actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '-'); assertEquals(expected, actual); } @Test - void subtractionWithPositiveAndNegativeOperands_isOk() { + void calculate_subtractionWithPositiveAndNegativeOperands_isOk() { expected = -723.9; actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '-'); assertEquals(expected, actual); } @Test - void subtractionWithZeroInDifferentPlaces_isOk() { + void calculate_subtractionWithZeroInDifferentPlaces_isOk() { expected = NEGATIVE_NUMBER_2; actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '-'); assertEquals(expected, actual); @@ -100,35 +100,35 @@ void subtractionWithZeroInDifferentPlaces_isOk() { } @Test - void subtractionForMinAndMaxDoubleValues_isOk() { + void calculate_subtractionForMinAndMaxDoubleValues_isOk() { actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '-'); expected = Double.MAX_VALUE; assertEquals(expected, actual, DELTA); } @Test - void multiplicationWithTwoPositiveOperands_isOk() { + void calculate_multiplicationWithTwoPositiveOperands_isOk() { actual = calculator.calculate(POSITIVE_NUMBER_1, POSITIVE_NUMBER_2, '*'); expected = 16391.7; assertEquals(expected, actual); } @Test - void multiplicationWithTwoNegativeOperands_isOk() { + void calculate_multiplicationWithTwoNegativeOperands_isOk() { expected = 16391.7; actual = calculator.calculate(NEGATIVE_NUMBER_1, NEGATIVE_NUMBER_2, '*'); assertEquals(expected, actual); } @Test - void multiplicationWithPositiveAndNegativeOperands_isOk() { + void calculate_multiplicationWithPositiveAndNegativeOperands_isOk() { expected = -16391.7; actual = calculator.calculate(NEGATIVE_NUMBER_1, POSITIVE_NUMBER_2, '*'); assertEquals(expected, actual); } @Test - void multiplicationWithZeroInDifferentPlaces_isOk() { + void calculate_multiplicationWithZeroInDifferentPlaces_isOk() { expected = ZERO_NUMBER; actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_2, '*'); assertEquals(expected, actual); @@ -137,35 +137,35 @@ void multiplicationWithZeroInDifferentPlaces_isOk() { } @Test - void multiplicationForMinAndMaxDoubleValues_isOk() { + void calculate_multiplicationForMinAndMaxDoubleValues_isOk() { expected = ZERO_NUMBER; actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '*'); assertEquals(expected, actual, DELTA); } @Test - void divisionWithTwoPositiveOperands_isOk() { + void calculate_divisionWithTwoPositiveOperands_isOk() { expected = 0.32; actual = calculator.calculate(POSITIVE_NUMBER_4, POSITIVE_NUMBER_5, '/'); assertEquals(expected, actual); } @Test - void divisionWithTwoNegativeOperands_isOk() { + void calculate_divisionWithTwoNegativeOperands_isOk() { expected = 0.32; actual = calculator.calculate(NEGATIVE_NUMBER_3, NEGATIVE_NUMBER_4, '/'); assertEquals(expected, actual); } @Test - void divisionWithPositiveAndNegativeOperands_isOk() { + void calculate_divisionWithPositiveAndNegativeOperands_isOk() { expected = -0.32; actual = calculator.calculate(POSITIVE_NUMBER_4, NEGATIVE_NUMBER_4, '/'); assertEquals(expected, actual); } @Test - void divisionWithZeroInDifferentPlaces() { + void calculate_divisionWithZeroInDifferentPlaces() { expected = ZERO_NUMBER; actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_5, '/'); assertEquals(expected, actual); @@ -174,7 +174,7 @@ void divisionWithZeroInDifferentPlaces() { } @Test - void raisingForMinAndMaxDoubleValues() { + void calculate_raisingForMinAndMaxDoubleValues() { expected = 1; actual = calculator.calculate(Double.MAX_VALUE, Double.MIN_VALUE, '^'); assertEquals(expected, actual, DELTA); @@ -183,7 +183,7 @@ void raisingForMinAndMaxDoubleValues() { } @Test - void raisingPositiveOrNegativeValueToThePositivePower() { + void calculate_raisingPositiveOrNegativeValueToThePositivePower() { expected = 61.763235; actual = calculator.calculate(POSITIVE_NUMBER_6, POSITIVE_NUMBER_7, '^'); assertEquals(expected, actual, DELTA); @@ -193,7 +193,7 @@ void raisingPositiveOrNegativeValueToThePositivePower() { } @Test - void raisingPositiveOrNegativeValueToTheNegativePower() { + void calculate_raisingPositiveOrNegativeValueToTheNegativePower() { expected = 0.02327; actual = calculator.calculate(POSITIVE_NUMBER_7, NEGATIVE_NUMBER_5, '^'); assertEquals(expected, actual, DELTA); @@ -202,7 +202,7 @@ void raisingPositiveOrNegativeValueToTheNegativePower() { } @Test - void raisingPositiveOrNegativeValueToTheZeroPower() { + void calculate_raisingPositiveOrNegativeValueToTheZeroPower() { expected = 1; actual = calculator.calculate(POSITIVE_NUMBER_7, ZERO_NUMBER, '^'); assertEquals(expected, actual); @@ -211,14 +211,17 @@ void raisingPositiveOrNegativeValueToTheZeroPower() { } @Test - void raisingZeroToPower() { + void calculate_raisingZeroToPower() { expected = ZERO_NUMBER; actual = calculator.calculate(ZERO_NUMBER, POSITIVE_NUMBER_7, '^'); assertEquals(expected, actual); + expected = Double.POSITIVE_INFINITY; + actual = calculator.calculate(ZERO_NUMBER, NEGATIVE_NUMBER_5, '^'); + assertEquals(expected, actual); } @Test - void illegalOperations_ExceptionOk() { + void calculate_illegalOperations_ExceptionOk() { assertThrows(RuntimeException.class, () -> calculator.calculate(ZERO_NUMBER, ZERO_NUMBER, '=')); assertThrows(RuntimeException.class,