From 40c3041eea24cfda34ee583f395d2f780bf5ffaf Mon Sep 17 00:00:00 2001 From: Denys Stefanko Date: Sun, 12 Sep 2021 20:45:48 +0300 Subject: [PATCH 1/3] first version --- src/main/java/core/basesyntax/Calculator.java | 5 ++++ src/main/java/core/basesyntax/HelloWorld.java | 8 ----- .../java/core/basesyntax/MyCalculator.java | 29 +++++++++++++++++++ .../basesyntax/UnknownOperationException.java | 7 +++++ .../java/core/basesyntax/HelloWorldTest.java | 8 ----- .../core/basesyntax/MyCalculatorTest.java | 29 +++++++++++++++++++ 6 files changed, 70 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/main/java/core/basesyntax/MyCalculator.java create mode 100644 src/main/java/core/basesyntax/UnknownOperationException.java delete mode 100644 src/test/java/core/basesyntax/HelloWorldTest.java create mode 100644 src/test/java/core/basesyntax/MyCalculatorTest.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..c531f8f3 --- /dev/null +++ b/src/main/java/core/basesyntax/Calculator.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Calculator { + double calculate(double firstValue, double secondValue, char operation); +} 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/main/java/core/basesyntax/MyCalculator.java b/src/main/java/core/basesyntax/MyCalculator.java new file mode 100644 index 00000000..e354265a --- /dev/null +++ b/src/main/java/core/basesyntax/MyCalculator.java @@ -0,0 +1,29 @@ +package core.basesyntax; + +public class MyCalculator implements Calculator{ + public static final char ADDITION = '+'; + public static final char SUBTRACTION = '-'; + public static final char DIVISION = '/'; + public static final char MULTIPLICATION = '*'; + public static final char RISING_TO_POWER = '^'; + @Override + public double calculate(double firstValue, double secondValue, char operation) { + switch (operation) { + case ADDITION : + return firstValue + secondValue; + case SUBTRACTION : + return firstValue - secondValue; + case DIVISION : + if (secondValue == 0) { + throw new ArithmeticException("Second value can't be 0!"); + } + return firstValue / secondValue; + case MULTIPLICATION : + return firstValue * secondValue; + case RISING_TO_POWER : + return Math.pow(firstValue, secondValue); + default: + throw new UnknownOperationException("Unknown operation"); + } + } +} diff --git a/src/main/java/core/basesyntax/UnknownOperationException.java b/src/main/java/core/basesyntax/UnknownOperationException.java new file mode 100644 index 00000000..7ccaa034 --- /dev/null +++ b/src/main/java/core/basesyntax/UnknownOperationException.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public class UnknownOperationException extends RuntimeException { + public UnknownOperationException (String message) { + super(message); + } +} 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 { - -} diff --git a/src/test/java/core/basesyntax/MyCalculatorTest.java b/src/test/java/core/basesyntax/MyCalculatorTest.java new file mode 100644 index 00000000..07545742 --- /dev/null +++ b/src/test/java/core/basesyntax/MyCalculatorTest.java @@ -0,0 +1,29 @@ +package core.basesyntax; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MyCalculatorTest { + public static final char ADDITION = '+'; + public static final char SUBTRACTION = '-'; + public static final char DIVISION = '/'; + public static final char MULTIPLICATION = '*'; + public static final char RISING_TO_POWER = '^'; + Calculator calculator = new MyCalculator(); + + @Test + void additionTwoPositiveOperands_OK() { + double actual = calculator.calculate(10, 5, ADDITION); + double expected = 15; + assertEquals(actual,expected); + } + + @Test + void additionTwoNegativeOperands_OK() { + double actual = calculator.calculate(-10, -5, ADDITION); + double expected = -15; + assertEquals(expected, actual); + } + +} \ No newline at end of file From a7074bcdf8c02371bd884223bf2f0fc3794cde53 Mon Sep 17 00:00:00 2001 From: Denys Stefanko Date: Mon, 13 Sep 2021 19:06:51 +0300 Subject: [PATCH 2/3] made all necessary tests --- .../java/core/basesyntax/MyCalculator.java | 3 +- .../basesyntax/UnknownOperationException.java | 2 +- .../core/basesyntax/MyCalculatorTest.java | 243 +++++++++++++++++- 3 files changed, 240 insertions(+), 8 deletions(-) diff --git a/src/main/java/core/basesyntax/MyCalculator.java b/src/main/java/core/basesyntax/MyCalculator.java index e354265a..63689cd8 100644 --- a/src/main/java/core/basesyntax/MyCalculator.java +++ b/src/main/java/core/basesyntax/MyCalculator.java @@ -1,11 +1,12 @@ package core.basesyntax; -public class MyCalculator implements Calculator{ +public class MyCalculator implements Calculator { public static final char ADDITION = '+'; public static final char SUBTRACTION = '-'; public static final char DIVISION = '/'; public static final char MULTIPLICATION = '*'; public static final char RISING_TO_POWER = '^'; + @Override public double calculate(double firstValue, double secondValue, char operation) { switch (operation) { diff --git a/src/main/java/core/basesyntax/UnknownOperationException.java b/src/main/java/core/basesyntax/UnknownOperationException.java index 7ccaa034..2214fbac 100644 --- a/src/main/java/core/basesyntax/UnknownOperationException.java +++ b/src/main/java/core/basesyntax/UnknownOperationException.java @@ -1,7 +1,7 @@ package core.basesyntax; public class UnknownOperationException extends RuntimeException { - public UnknownOperationException (String message) { + public UnknownOperationException(String message) { super(message); } } diff --git a/src/test/java/core/basesyntax/MyCalculatorTest.java b/src/test/java/core/basesyntax/MyCalculatorTest.java index 07545742..872de0c1 100644 --- a/src/test/java/core/basesyntax/MyCalculatorTest.java +++ b/src/test/java/core/basesyntax/MyCalculatorTest.java @@ -1,22 +1,30 @@ package core.basesyntax; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; class MyCalculatorTest { public static final char ADDITION = '+'; public static final char SUBTRACTION = '-'; public static final char DIVISION = '/'; public static final char MULTIPLICATION = '*'; - public static final char RISING_TO_POWER = '^'; - Calculator calculator = new MyCalculator(); + public static final char RISING = '^'; + private static final double DELTA = 0.00001; + private static Calculator calculator; + + @BeforeAll + static void beforeAll() { + calculator = new MyCalculator(); + } @Test void additionTwoPositiveOperands_OK() { double actual = calculator.calculate(10, 5, ADDITION); double expected = 15; - assertEquals(actual,expected); + assertEquals(expected,actual); } @Test @@ -26,4 +34,227 @@ void additionTwoNegativeOperands_OK() { assertEquals(expected, actual); } -} \ No newline at end of file + @Test + void additionNegativeAndPositiveOperands_OK() { + double actual = calculator.calculate(-10,5, ADDITION); + double expected = -5; + assertEquals(expected,actual); + } + + @Test + void additionWithZero_OK() { + double actual = calculator.calculate(0, 10, ADDITION); + double expected = 10; + assertEquals(expected, actual); + actual = calculator.calculate(10, 0, ADDITION); + assertEquals(expected, actual); + } + + @Test + void additionWithMaxValues_OK() { + double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, ADDITION); + double expected = Double.POSITIVE_INFINITY; + assertEquals(expected, actual, DELTA); + } + + @Test + void additionWithMinValue_OK() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, ADDITION); + double expected = 0; + assertEquals(expected,actual,DELTA); + } + + @Test + void subtractionWithTwoPositiveOperands_OK() { + double actual = calculator.calculate(10, 5, SUBTRACTION); + double expected = 5; + assertEquals(expected,actual); + } + + @Test + void subtractionWithTwoNegativeOperands_OK() { + double actual = calculator.calculate(-10, -5, SUBTRACTION); + double expected = -5; + assertEquals(expected, actual); + } + + @Test + void subtractionWithNegativeAndPositiveOperands_OK() { + double actual = calculator.calculate(-5, 10, SUBTRACTION); + double expected = -15; + assertEquals(expected, actual); + } + + @Test + void subtractionWithZeroOperands_OK() { + double actual = calculator.calculate(10, 0, SUBTRACTION); + double expected = 10; + assertEquals(expected, actual); + expected = -10; + actual = calculator.calculate(0, 10, SUBTRACTION); + assertEquals(expected, actual); + } + + @Test + void subtractionWithMaxValues_OK() { + double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, SUBTRACTION); + double expected = 0; + assertEquals(expected, actual, DELTA); + } + + @Test + void subtractionWithMinValues_OK() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, SUBTRACTION); + double expected = 0; + assertEquals(expected, actual, DELTA); + } + + @Test + void divisionWithTwoPositiveOperands_OK() { + double actual = calculator.calculate(10, 5, DIVISION); + double expected = 2; + assertEquals(expected,actual); + } + + @Test + void divisionWithTwoNegativeOperands_OK() { + double actual = calculator.calculate(-10, -5, DIVISION); + double expected = 2; + assertEquals(expected, actual); + } + + @Test + void divisionWithNegativeAndPositiveOperands_OK() { + double actual = calculator.calculate(-10, 2, DIVISION); + double expected = -5; + assertEquals(expected, actual); + } + + @Test + void divisionWithFirstZeroOperands_OK() { + double actual = calculator.calculate(0, 10, DIVISION); + double expected = 0; + assertEquals(expected, actual); + } + + @Test + void divisionOnZero_NotOK() { + assertThrows(ArithmeticException.class, () -> { + calculator.calculate(10, 0, DIVISION); + }); + } + + @Test + void divisionWithMaxValues_OK() { + double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, DIVISION); + double expected = 1; + assertEquals(expected, actual, DELTA); + } + + @Test + void divisionWithMinValues_OK() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, DIVISION); + double expected = 1; + assertEquals(expected, actual, DELTA); + } + + @Test + void multiplicationTwoPositiveOperands_OK() { + double actual = calculator.calculate(10, 5, MULTIPLICATION); + double expected = 50; + assertEquals(expected,actual); + } + + @Test + void multiplicationTwoNegativeOperands_OK() { + double actual = calculator.calculate(-10, -5, MULTIPLICATION); + double expected = 50; + assertEquals(expected, actual); + } + + @Test + void multiplicationNegativeAndPositiveOperands_OK() { + double actual = calculator.calculate(-10,5, MULTIPLICATION); + double expected = -50; + assertEquals(expected,actual); + } + + @Test + void multiplicationWithZero_OK() { + double actual = calculator.calculate(0, 10, MULTIPLICATION); + double expected = 0; + assertEquals(expected, actual); + actual = calculator.calculate(10, 0, MULTIPLICATION); + assertEquals(expected, actual); + } + + @Test + void multiplicationWithMaxValues_OK() { + double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, MULTIPLICATION); + double expected = Double.POSITIVE_INFINITY; + assertEquals(expected, actual, DELTA); + } + + @Test + void multiplicationWithMinValue_OK() { + double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, MULTIPLICATION); + double expected = 0; + assertEquals(expected,actual,DELTA); + } + + @Test + void raisingPositiveToPositivePower_OK() { + double expected = 32; + double actual = calculator.calculate(2, 5, RISING); + assertEquals(expected, actual); + } + + @Test + void raisingPositiveToNegativePower_OK() { + double expected = 0.125; + double actual = calculator.calculate(2, -3, RISING); + assertEquals(expected, actual); + } + + @Test + void raisingNegativeToNegativePower_OK() { + double expected = -0.125; + double actual = calculator.calculate(-2, -3, RISING); + assertEquals(expected, actual); + } + + @Test + void raisingNegativeToPositivePower_OK() { + double expected = -27; + double actual = calculator.calculate(-3, 3, RISING); + assertEquals(expected, actual); + } + + @Test + void raisingPositiveValueToZeroPower_OK() { + double expected = 1; + double actual = calculator.calculate(5, 0, RISING); + assertEquals(expected, actual); + } + + @Test + void raisingNegativeValueToZeroPower_OK() { + double expected = 1; + double actual = calculator.calculate(-5, 0, RISING); + assertEquals(expected, actual); + } + + @Test + void raisingZeroToPower_OK() { + double expected = 0; + double actual = calculator.calculate(0, 5, RISING); + assertEquals(expected, actual); + } + + @Test + void unknownOperation_OK() { + assertThrows(UnknownOperationException.class, () -> { + calculator.calculate(10, 10, '.'); + }); + } +} From d95a846978d7386f453cdb2165ae590fe6fb7e0b Mon Sep 17 00:00:00 2001 From: Denys Stefanko Date: Tue, 14 Sep 2021 11:30:00 +0300 Subject: [PATCH 3/3] added fixes after review --- .../core/basesyntax/MyCalculatorTest.java | 73 ++++++++++--------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/src/test/java/core/basesyntax/MyCalculatorTest.java b/src/test/java/core/basesyntax/MyCalculatorTest.java index 872de0c1..b83740da 100644 --- a/src/test/java/core/basesyntax/MyCalculatorTest.java +++ b/src/test/java/core/basesyntax/MyCalculatorTest.java @@ -21,28 +21,28 @@ static void beforeAll() { } @Test - void additionTwoPositiveOperands_OK() { + void calculate_additionTwoPositiveOperands_OK() { double actual = calculator.calculate(10, 5, ADDITION); double expected = 15; assertEquals(expected,actual); } @Test - void additionTwoNegativeOperands_OK() { + void calculate_additionTwoNegativeOperands_OK() { double actual = calculator.calculate(-10, -5, ADDITION); double expected = -15; assertEquals(expected, actual); } @Test - void additionNegativeAndPositiveOperands_OK() { + void calculate_additionNegativeAndPositiveOperands_OK() { double actual = calculator.calculate(-10,5, ADDITION); double expected = -5; assertEquals(expected,actual); } @Test - void additionWithZero_OK() { + void calculate_additionWithZero_OK() { double actual = calculator.calculate(0, 10, ADDITION); double expected = 10; assertEquals(expected, actual); @@ -51,42 +51,42 @@ void additionWithZero_OK() { } @Test - void additionWithMaxValues_OK() { + void calculate_additionWithMaxValues_OK() { double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, ADDITION); double expected = Double.POSITIVE_INFINITY; assertEquals(expected, actual, DELTA); } @Test - void additionWithMinValue_OK() { + void calculate_additionWithMinValue_OK() { double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, ADDITION); double expected = 0; assertEquals(expected,actual,DELTA); } @Test - void subtractionWithTwoPositiveOperands_OK() { + void calculate_subtractionWithTwoPositiveOperands_OK() { double actual = calculator.calculate(10, 5, SUBTRACTION); double expected = 5; assertEquals(expected,actual); } @Test - void subtractionWithTwoNegativeOperands_OK() { + void calculate_subtractionWithTwoNegativeOperands_OK() { double actual = calculator.calculate(-10, -5, SUBTRACTION); double expected = -5; assertEquals(expected, actual); } @Test - void subtractionWithNegativeAndPositiveOperands_OK() { + void calculate_subtractionWithNegativeAndPositiveOperands_OK() { double actual = calculator.calculate(-5, 10, SUBTRACTION); double expected = -15; assertEquals(expected, actual); } @Test - void subtractionWithZeroOperands_OK() { + void calculate_subtractionWithZeroOperands_OK() { double actual = calculator.calculate(10, 0, SUBTRACTION); double expected = 10; assertEquals(expected, actual); @@ -96,91 +96,91 @@ void subtractionWithZeroOperands_OK() { } @Test - void subtractionWithMaxValues_OK() { + void calculate_subtractionWithMaxValues_OK() { double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, SUBTRACTION); double expected = 0; assertEquals(expected, actual, DELTA); } @Test - void subtractionWithMinValues_OK() { + void calculate_subtractionWithMinValues_OK() { double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, SUBTRACTION); double expected = 0; assertEquals(expected, actual, DELTA); } @Test - void divisionWithTwoPositiveOperands_OK() { + void calculate_divisionWithTwoPositiveOperands_OK() { double actual = calculator.calculate(10, 5, DIVISION); double expected = 2; assertEquals(expected,actual); } @Test - void divisionWithTwoNegativeOperands_OK() { + void calculate_divisionWithTwoNegativeOperands_OK() { double actual = calculator.calculate(-10, -5, DIVISION); double expected = 2; assertEquals(expected, actual); } @Test - void divisionWithNegativeAndPositiveOperands_OK() { + void calculate_divisionWithNegativeAndPositiveOperands_OK() { double actual = calculator.calculate(-10, 2, DIVISION); double expected = -5; assertEquals(expected, actual); } @Test - void divisionWithFirstZeroOperands_OK() { + void calculate_divisionWithFirstZeroOperands_OK() { double actual = calculator.calculate(0, 10, DIVISION); double expected = 0; assertEquals(expected, actual); } @Test - void divisionOnZero_NotOK() { + void calculate_divisionOnZero_NotOK() { assertThrows(ArithmeticException.class, () -> { calculator.calculate(10, 0, DIVISION); }); } @Test - void divisionWithMaxValues_OK() { + void calculate_divisionWithMaxValues_OK() { double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, DIVISION); double expected = 1; assertEquals(expected, actual, DELTA); } @Test - void divisionWithMinValues_OK() { + void calculate_divisionWithMinValues_OK() { double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, DIVISION); double expected = 1; assertEquals(expected, actual, DELTA); } @Test - void multiplicationTwoPositiveOperands_OK() { + void calculate_multiplicationTwoPositiveOperands_OK() { double actual = calculator.calculate(10, 5, MULTIPLICATION); double expected = 50; assertEquals(expected,actual); } @Test - void multiplicationTwoNegativeOperands_OK() { + void calculate_multiplicationTwoNegativeOperands_OK() { double actual = calculator.calculate(-10, -5, MULTIPLICATION); double expected = 50; assertEquals(expected, actual); } @Test - void multiplicationNegativeAndPositiveOperands_OK() { + void calculate_multiplicationNegativeAndPositiveOperands_OK() { double actual = calculator.calculate(-10,5, MULTIPLICATION); double expected = -50; assertEquals(expected,actual); } @Test - void multiplicationWithZero_OK() { + void calculate_multiplicationWithZero_OK() { double actual = calculator.calculate(0, 10, MULTIPLICATION); double expected = 0; assertEquals(expected, actual); @@ -189,70 +189,77 @@ void multiplicationWithZero_OK() { } @Test - void multiplicationWithMaxValues_OK() { + void calculate_multiplicationWithMaxValues_OK() { double actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, MULTIPLICATION); double expected = Double.POSITIVE_INFINITY; assertEquals(expected, actual, DELTA); } @Test - void multiplicationWithMinValue_OK() { + void calculate_multiplicationWithMinValue_OK() { double actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, MULTIPLICATION); double expected = 0; assertEquals(expected,actual,DELTA); } @Test - void raisingPositiveToPositivePower_OK() { + void calculate_raisingPositiveToPositivePower_OK() { double expected = 32; double actual = calculator.calculate(2, 5, RISING); assertEquals(expected, actual); } @Test - void raisingPositiveToNegativePower_OK() { + void calculate_raisingPositiveToNegativePower_OK() { double expected = 0.125; double actual = calculator.calculate(2, -3, RISING); assertEquals(expected, actual); } @Test - void raisingNegativeToNegativePower_OK() { + void calculate_raisingNegativeToNegativePower_OK() { double expected = -0.125; double actual = calculator.calculate(-2, -3, RISING); assertEquals(expected, actual); } @Test - void raisingNegativeToPositivePower_OK() { + void calculate_raisingNegativeToPositivePower_OK() { double expected = -27; double actual = calculator.calculate(-3, 3, RISING); assertEquals(expected, actual); } @Test - void raisingPositiveValueToZeroPower_OK() { + void calculate_raisingPositiveValueToZeroPower_OK() { double expected = 1; double actual = calculator.calculate(5, 0, RISING); assertEquals(expected, actual); } @Test - void raisingNegativeValueToZeroPower_OK() { + void calculate_raisingNegativeValueToZeroPower_OK() { double expected = 1; double actual = calculator.calculate(-5, 0, RISING); assertEquals(expected, actual); } @Test - void raisingZeroToPower_OK() { + void calculate_raisingZeroToPower_OK() { double expected = 0; double actual = calculator.calculate(0, 5, RISING); assertEquals(expected, actual); } @Test - void unknownOperation_OK() { + void calculate_zeroToNegativePower_OK() { + double expected = Double.POSITIVE_INFINITY; + double actual = calculator.calculate(0, -3, RISING); + assertEquals(expected, actual); + } + + @Test + void calculate_unknownOperation_OK() { assertThrows(UnknownOperationException.class, () -> { calculator.calculate(10, 10, '.'); });