-
Notifications
You must be signed in to change notification settings - Fork 257
added calculator solution #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator implements CalculatorService { | ||
| @Override | ||
| public double calculate(double firstValue, double secondValue, char operationType) { | ||
| switch (operationType) { | ||
| case '+' : { | ||
| return firstValue + secondValue; | ||
| } | ||
| case '-' : { | ||
| return firstValue - secondValue; | ||
| } | ||
| case '*' : { | ||
| return firstValue * secondValue; | ||
| } | ||
| case '/' : { | ||
| if (firstValue != 0.0d && secondValue == 0) { | ||
| throw new IllegalArgumentException("Can't divide by zero!"); | ||
| } | ||
| return firstValue / secondValue; | ||
| } | ||
| case '^' : { | ||
| return Math.pow(firstValue, secondValue); | ||
| } | ||
| default: { | ||
| throw new IllegalArgumentException("Illegal operation type"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public interface CalculatorService { | ||
| double calculate(double valueA, double valueB, char operationType); | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,301 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CalculatorTest { | ||
| private static final double DELTA = 0.00001; | ||
| private static CalculatorService calculatorService; | ||
| private double actual; | ||
| private double expected; | ||
|
|
||
| @BeforeAll | ||
| private static void beforeAll() { | ||
| calculatorService = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void addTwoPositiveNumbers_Ok() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what method co we test?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| actual = calculatorService.calculate(10, 5, '+'); | ||
| expected = 15; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addTwoNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-5, -10, '+'); | ||
| expected = -15; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addPositiveAndNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-30, 50, '+'); | ||
| expected = 20; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void additionWithZero_Ok() { | ||
| double actualFirstZero = calculatorService.calculate(0, 5, '+'); | ||
| double actualSecondZero = calculatorService.calculate(50, 0, '+'); | ||
| double actualBothZero = calculatorService.calculate(0, 0, '+'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with actual
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| double expectedFirstZero = 5; | ||
| double expectedSecondZero = 50; | ||
| double expectedBothZero = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| assertEquals(expectedFirstZero, actualFirstZero); | ||
| assertEquals(expectedSecondZero, actualSecondZero); | ||
| assertEquals(expectedBothZero, actualBothZero); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure that we should use few assertions in one test.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As i can see multiple assertions are used in tests from previous homeworks, so I decided to use them too. |
||
| } | ||
|
|
||
| @Test | ||
| void addTwoZeroNumbers_NotOk() { | ||
| actual = calculatorService.calculate(0, 0, '+'); | ||
| expected = 55; | ||
| assertNotEquals(expected, actual); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this test? how should it fail?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| } | ||
|
|
||
| @Test | ||
| void addTwoMaxNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '+'); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addTwoMinNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '+'); | ||
| expected = 1.0E-323; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractTwoPositiveNumbers_Ok() { | ||
| actual = calculatorService.calculate(200, 5, '-'); | ||
| expected = 195; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractTwoNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-50, -10, '-'); | ||
| expected = -40; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractPositiveAndNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-30, 50, '-'); | ||
| expected = -80; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractionWithZero_Ok() { | ||
| double actualFirstZero = calculatorService.calculate(0, 5, '-'); | ||
| double actualSecondZero = calculatorService.calculate(50, 0, '-'); | ||
| double actualBothZero = calculatorService.calculate(0, 0, '-'); | ||
| double expectedFirstZero = -5; | ||
| double expectedSecondZero = 50; | ||
| double expectedBothZero = 0; | ||
| assertEquals(expectedFirstZero, actualFirstZero); | ||
| assertEquals(expectedSecondZero, actualSecondZero); | ||
| assertEquals(expectedBothZero, actualBothZero); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractTwoZeroNumbers_NotOk() { | ||
| actual = calculatorService.calculate(0, 0, '-'); | ||
| expected = 5555; | ||
| assertNotEquals(expected, actual); | ||
| } | ||
|
Comment on lines
+110
to
+114
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| @Test | ||
| void subtractTwoMaxNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '-'); | ||
| expected = 0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtractTwoMinNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '-'); | ||
| expected = 0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplyTwoPositiveNumbers_Ok() { | ||
| actual = calculatorService.calculate(100, 100, '*'); | ||
| expected = 10000; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplyTwoNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-8, -20, '*'); | ||
| expected = 160; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplyPositiveAndNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-40, 40, '*'); | ||
| expected = -1600; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplicationWithZero_Ok() { | ||
| double actualFirstZero = calculatorService.calculate(0, 5, '*'); | ||
| double actualSecondZero = calculatorService.calculate(50, 0, '*'); | ||
| double actualBothZero = calculatorService.calculate(0, 0, '*'); | ||
| double expectedFirstZero = 0; | ||
| double expectedSecondZero = 0; | ||
| double expectedBothZero = 0; | ||
| assertEquals(expectedFirstZero, actualFirstZero); | ||
| assertEquals(expectedSecondZero, actualSecondZero); | ||
| assertEquals(expectedBothZero, actualBothZero); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplyTwoZeroNumbers_NotOk() { | ||
| actual = calculatorService.calculate(0, 0, '*'); | ||
| expected = 10; | ||
| assertNotEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplyTwoMaxNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '*'); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplyTwoMinNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '*'); | ||
| expected = 0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divideTwoPositiveNumbers_Ok() { | ||
| actual = calculatorService.calculate(100, 10, '/'); | ||
| expected = 10; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divideTwoNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-8, -2, '/'); | ||
| expected = 4; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void dividePositiveAndNegativeNumbers_Ok() { | ||
| actual = calculatorService.calculate(-160, 4, '/'); | ||
| expected = -40; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divideWithZero_Ok() { | ||
| actual = calculatorService.calculate(0, 5, '/'); | ||
| expected = 0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divideWithZero_NotOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> calculatorService.calculate(50, 0, '/')); | ||
| } | ||
|
|
||
| @Test | ||
| void divideWithTwoZeroValues_Ok() { | ||
| actual = calculatorService.calculate(0.0d, 0, '/'); | ||
| expected = Double.NaN; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divideTwoMaxNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '/'); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divideTwoMinNumbers_Ok() { | ||
| actual = calculatorService.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '/'); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raisePositiveNumberToPositivePower_Ok() { | ||
| actual = calculatorService.calculate(2, 5, '^'); | ||
| expected = 32; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseNegativeNumberToPositivePower_Ok() { | ||
| actual = calculatorService.calculate(-5, 2, '^'); | ||
| expected = 25; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseNegativeNumberToNegativePower_Ok() { | ||
| actual = calculatorService.calculate(-4, -2, '^'); | ||
| expected = 0.0625; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingToPowerWithZero_Ok() { | ||
| double actualFirstZero = calculatorService.calculate(0, 5, '^'); | ||
| double actualSecondZero = calculatorService.calculate(50, 0, '^'); | ||
| double actualBothZero = calculatorService.calculate(0, 0, '^'); | ||
| double expectedFirstZero = 0; | ||
| double expectedSecondZero = 1; | ||
| double expectedBothZero = 1; | ||
| assertEquals(expectedFirstZero, actualFirstZero); | ||
| assertEquals(expectedSecondZero, actualSecondZero); | ||
| assertEquals(expectedBothZero, actualBothZero); | ||
| } | ||
|
|
||
| @Test | ||
| void raisingNegativeToZeroPower_Ok() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added new test |
||
| actual = calculatorService.calculate(-80, 0, '^'); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseMaxNumberToMaxPower_Ok() { | ||
| actual = calculatorService.calculate(Double.MAX_VALUE, Double.MAX_VALUE, '^'); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raiseMinNumberToMinPower_Ok() { | ||
| actual = calculatorService.calculate(Double.MIN_VALUE, Double.MIN_VALUE, '^'); | ||
| expected = 1; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void illegalOperationType_NotOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> calculatorService.calculate(50, 0, '$')); | ||
| assertThrows(IllegalArgumentException.class, () -> calculatorService.calculate(50, 0, '4')); | ||
| assertThrows(IllegalArgumentException.class, () -> calculatorService.calculate(50, 0, 'h')); | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed