-
Notifications
You must be signed in to change notification settings - Fork 257
Created MyCalculator and MyCalculatorTest class. Test coverage 100% #285
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
Open
VladyslavLekhter
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
VladyslavLekhter:hw-lambda_calculator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
913bdeb
Created MyCalculator and MyCalculatorTest class. Test coverage 100%
VladyslavLekhter 2dac81e
Added constants and checkResult method
VladyslavLekhter f75d9a9
Added constants and checkResult method.
VladyslavLekhter 067a1b2
Changed place of constants and name of test methods.
VladyslavLekhter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public interface Calculator { | ||
|
|
||
| double calculate(double firstNumber, char operationType, double secondNumber); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class MyCalculator implements Calculator { | ||
| private static final char ADDITION = '+'; | ||
| private static final char SUBTRACTION = '-'; | ||
| private static final char DIVISION = '/'; | ||
| private static final char MULTIPLICATION = '*'; | ||
| private static final char RAISING_TO_A_POWER = '^'; | ||
|
|
||
| @Override | ||
| public double calculate(double firstNumber, char operationType, double secondNumber) { | ||
| switch (operationType) { | ||
| case ADDITION: | ||
| return checkResult(firstNumber + secondNumber); | ||
| case SUBTRACTION: | ||
| return checkResult(firstNumber - secondNumber); | ||
| case MULTIPLICATION: | ||
| return checkResult(firstNumber * secondNumber); | ||
| case DIVISION: | ||
| if (secondNumber == 0) { | ||
| throw new OperationTypeException("You can't divide by zero"); | ||
| } | ||
| return checkResult(firstNumber / secondNumber); | ||
| case RAISING_TO_A_POWER: | ||
| return checkResult(Math.pow(firstNumber, secondNumber)); | ||
| default: | ||
| throw new OperationTypeException("This type of operation is not supported"); | ||
| } | ||
| } | ||
|
|
||
| private double checkResult(double result) { | ||
| if (result == Double.POSITIVE_INFINITY || result == Double.NEGATIVE_INFINITY) { | ||
| throw new OperationTypeException("Too large or small value"); | ||
| } | ||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class OperationTypeException extends RuntimeException { | ||
| public OperationTypeException(String message) { | ||
| super(message); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static java.lang.Double.MAX_VALUE; | ||
| import static java.lang.Double.MIN_VALUE; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class MyCalculatorTest { | ||
| private static final double DELTA = 0.00001; | ||
| private static final char ADDITION = '+'; | ||
| private static final char SUBTRACTION = '-'; | ||
| private static final char MULTIPLICATION = '*'; | ||
| private static final char DIVISION = '/'; | ||
| private static final char RAISING_TO_A_POWER = '^'; | ||
| private static Calculator myCalculatorTest; | ||
| private double actual; | ||
| private double expected; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| myCalculatorTest = new MyCalculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addPositiveNumbers_Ok() { | ||
| expected = 155.5; | ||
| actual = myCalculatorTest.calculate(90.5, ADDITION, 65.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addNegativeNumbers_Ok() { | ||
| expected = -241.1; | ||
| actual = myCalculatorTest.calculate(-177.6, ADDITION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addPositiveAndNegativeNumbers_Ok() { | ||
| expected = 136.5; | ||
| actual = myCalculatorTest.calculate(200.0, ADDITION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addZeroFirstNumberPosition_Ok() { | ||
| expected = -77; | ||
| actual = myCalculatorTest.calculate(0, ADDITION, -77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addZeroSecondNumberPosition_Ok() { | ||
| expected = 200; | ||
| actual = myCalculatorTest.calculate(200.0, ADDITION, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addMaxValues_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(MAX_VALUE, ADDITION, MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addMinValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, ADDITION, MIN_VALUE); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionPositiveNumbers_Ok() { | ||
| expected = 25.5; | ||
| actual = myCalculatorTest.calculate(90.5, SUBTRACTION, 65.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionNegativeNumbers_Ok() { | ||
| expected = -114.1; | ||
| actual = myCalculatorTest.calculate(-177.6, SUBTRACTION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionPositiveAndNegativeNumbers_Ok() { | ||
| expected = 263.5; | ||
| actual = myCalculatorTest.calculate(200.0, SUBTRACTION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionZeroFirstNumberPosition_Ok() { | ||
| expected = 77.0; | ||
| actual = myCalculatorTest.calculate(0, SUBTRACTION, -77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionZeroSecondNumberPosition_Ok() { | ||
| expected = 200; | ||
| actual = myCalculatorTest.calculate(200.0, SUBTRACTION, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionMaxValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MAX_VALUE, SUBTRACTION, MAX_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionMinValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, SUBTRACTION, MIN_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationPositiveNumbers_Ok() { | ||
| expected = 50.0; | ||
| actual = myCalculatorTest.calculate(10.0, MULTIPLICATION, 5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationNegativeNumbers_Ok() { | ||
| expected = 50.0; | ||
| actual = myCalculatorTest.calculate(-10.0, MULTIPLICATION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationPositiveAndNegativeNumbers_Ok() { | ||
| expected = -1000.0; | ||
| actual = myCalculatorTest.calculate(200.0, MULTIPLICATION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationZeroFirstNumberPosition_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(0, MULTIPLICATION, 77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationZeroSecondNumberPosition_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(200.0, MULTIPLICATION, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationMaxValues_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(MAX_VALUE, MULTIPLICATION, MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationMinValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, MULTIPLICATION, MIN_VALUE); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionPositiveNumbers_Ok() { | ||
| expected = 1.3923; | ||
| actual = myCalculatorTest.calculate(90.5, DIVISION, 65.0); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionNegativeNumbers_Ok() { | ||
| expected = 4.0; | ||
| actual = myCalculatorTest.calculate(-20.0, DIVISION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionPositiveAndNegativeNumbers_Ok() { | ||
| expected = -4.0; | ||
| actual = myCalculatorTest.calculate(20.0, DIVISION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionZeroFirstNumberPosition_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(0, DIVISION, 77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionZeroSecondNumberPosition_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(200.0, DIVISION, 0)); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionMaxValues_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(MAX_VALUE, DIVISION, MAX_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionMinValues_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, DIVISION, MIN_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValuePositivePower_Ok() { | ||
| expected = 400.0; | ||
| actual = myCalculatorTest.calculate(20.0, RAISING_TO_A_POWER, 2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValuePositivePower_Ok() { | ||
| expected = 400.0; | ||
| actual = myCalculatorTest.calculate(-20.0, RAISING_TO_A_POWER, 2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueNegativePower_Ok() { | ||
| expected = 0.0025; | ||
| actual = myCalculatorTest.calculate(20.0, RAISING_TO_A_POWER, -2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueNegativePower_Ok() { | ||
| expected = 0.0025; | ||
| actual = myCalculatorTest.calculate(-20.0, RAISING_TO_A_POWER, -2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueZeroPower_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(20.0, RAISING_TO_A_POWER, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueZeroPower_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(-20.0, RAISING_TO_A_POWER, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingZeroValuePositivePower_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(0, RAISING_TO_A_POWER, 2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(65.0, '!', 50.0)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What will be if values are 2.0 and 1.1?
Will the test be passed?