-
Notifications
You must be signed in to change notification settings - Fork 257
done #306
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
alexpogrebniy
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
alexpogrebniy:1_branch
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
done #306
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,24 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
| public double calculate(double numOne, double numTwo, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return numOne + numTwo; | ||
| case '-': | ||
| return numOne - numTwo; | ||
| case '*': | ||
| return numOne * numTwo; | ||
| case '/': | ||
| if (numTwo == 0) { | ||
| throw new ArithmeticException("You can't divide by zero"); | ||
| } | ||
| return numOne / numTwo; | ||
| case '^': | ||
| return Math.pow(numOne, numTwo); | ||
| default: | ||
| throw new RuntimeException("Wrong operator" + operation); | ||
| } | ||
| } | ||
| } | ||
|
|
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,293 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class CalculatorTest { | ||
| private static final Calculator calculator = new Calculator(); | ||
| private static final char ADD = '+'; | ||
| private static final char MINUS = '-'; | ||
| private static final char MULTIPLY = '*'; | ||
| private static final char DIVIDE = '/'; | ||
| private static final char POW = '^'; | ||
| private static final char ILLEGAL_OPERATION = '%'; | ||
| 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_DIFF_OF_MIN_AND_MAX_VALUES = 1.7976931348623157E308; | ||
| private static final double NEGATIVE_DIFF_OF_MIN_AND_MAX_VALUES = -1.7976931348623157E308; | ||
| private static final double SUM_OF_TWO_MIN_VALUES = 1.0E-323; | ||
| private static final double MULTIPLY_MIN_BY_MAX = 8.881784197001251E-16; | ||
| private static final double FIVE_POSITIVE = 5.1; | ||
| private static final double TEN_POSITIVE = 10.4; | ||
| private static final double FIVE_NEGATIVE = -5.1; | ||
| private static final double TEN_NEGATIVE = -10.4; | ||
|
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. ці змінні разом з actual / expceted краще винести в методи і їх не потрібно робити константами |
||
| private static final double DELTA = 0.000000000000001; | ||
| private double actual; | ||
| private double expected; | ||
|
|
||
| @Test | ||
| void calculate_addingTwoPositive_ok() { | ||
| expected = 15.5; | ||
| actual = calculator.calculate(FIVE_POSITIVE, TEN_POSITIVE, ADD); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addingTwoNegative_ok() { | ||
| expected = -15.5; | ||
| actual = calculator.calculate(FIVE_NEGATIVE, TEN_NEGATIVE, ADD); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addingPositiveToNegative_ok() { | ||
| expected = -5.3; | ||
| actual = calculator.calculate(TEN_NEGATIVE, FIVE_POSITIVE, ADD); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addingZero_ok() { | ||
| expected = 10.4; | ||
| actual = calculator.calculate(0, TEN_POSITIVE, ADD); | ||
| assertEquals(expected, actual, DELTA); | ||
|
|
||
| expected = -10.4; | ||
| actual = calculator.calculate(0, TEN_NEGATIVE, ADD); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addingMinAndMaxValues_ok() { | ||
| actual = calculator.calculate(MAX_DOUBLE_VALUE, MAX_DOUBLE_VALUE, ADD); | ||
| assertEquals(Double.POSITIVE_INFINITY, actual); | ||
|
|
||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MAX_DOUBLE_VALUE, ADD); | ||
| assertEquals(POSITIVE_DIFF_OF_MIN_AND_MAX_VALUES, actual); | ||
|
|
||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MIN_DOUBLE_VALUE, ADD); | ||
| assertEquals(SUM_OF_TWO_MIN_VALUES, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractingTwoPositive_ok() { | ||
| expected = -5.3; | ||
| double actual = calculator.calculate(FIVE_POSITIVE, TEN_POSITIVE, MINUS); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractingTwoNegative_ok() { | ||
| expected = 5.3; | ||
| actual = calculator.calculate(FIVE_NEGATIVE, TEN_NEGATIVE, MINUS); | ||
| assertEquals(expected, actual, DELTA); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void calculate_negativeMinusPositive_ok() { | ||
| expected = -15.5; | ||
| actual = calculator.calculate(TEN_NEGATIVE, FIVE_POSITIVE, MINUS); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_positiveMinusNegative_ok() { | ||
| expected = 15.5; | ||
| actual = calculator.calculate(TEN_POSITIVE, FIVE_NEGATIVE, MINUS); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractingZero_ok() { | ||
| expected = 5.1; | ||
| actual = calculator.calculate(FIVE_POSITIVE, 0.0, MINUS); | ||
| assertEquals(expected, actual); | ||
|
|
||
| expected = -5.1; | ||
| actual = calculator.calculate(0.0, FIVE_POSITIVE, MINUS); | ||
| assertEquals(expected, actual); | ||
|
|
||
| actual = calculator.calculate(FIVE_NEGATIVE, 0.0, MINUS); | ||
| assertEquals(expected, actual); | ||
|
|
||
| expected = 5.1; | ||
| actual = calculator.calculate(0.0, FIVE_NEGATIVE, MINUS); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractingMinAndMaxValues_ok() { | ||
| actual = calculator.calculate(MAX_DOUBLE_VALUE, MIN_DOUBLE_VALUE, MINUS); | ||
| assertEquals(POSITIVE_DIFF_OF_MIN_AND_MAX_VALUES, actual); | ||
|
|
||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MAX_DOUBLE_VALUE, MINUS); | ||
| assertEquals(NEGATIVE_DIFF_OF_MIN_AND_MAX_VALUES, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplyTwoPositive_ok() { | ||
| expected = 53.04; | ||
| actual = calculator.calculate(TEN_POSITIVE, FIVE_POSITIVE, MULTIPLY); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplyTwoNegative_ok() { | ||
| expected = 53.04; | ||
| actual = calculator.calculate(TEN_NEGATIVE, FIVE_NEGATIVE, MULTIPLY); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplyPositiveAndNegative_ok() { | ||
| expected = -53.04; | ||
| actual = calculator.calculate(TEN_NEGATIVE, FIVE_POSITIVE, MULTIPLY); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplyByZero_ok() { | ||
| expected = -0.0; | ||
| actual = calculator.calculate(TEN_NEGATIVE, 0.0, MULTIPLY); | ||
| assertEquals(expected, actual); | ||
|
|
||
| expected = 0.0; | ||
| actual = calculator.calculate(0.0, FIVE_POSITIVE, MULTIPLY); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplyMinAndMaxValues_ok() { | ||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MAX_DOUBLE_VALUE, MULTIPLY); | ||
| assertEquals(MULTIPLY_MIN_BY_MAX, actual); | ||
|
|
||
| expected = 0.0; | ||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MIN_DOUBLE_VALUE, MULTIPLY); | ||
| assertEquals(expected, actual); | ||
|
|
||
| actual = calculator.calculate(MAX_DOUBLE_VALUE, MAX_DOUBLE_VALUE, MULTIPLY); | ||
| assertEquals(Double.POSITIVE_INFINITY, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideTwoPositive_ok() { | ||
| expected = 2.03921568627451; | ||
| actual = calculator.calculate(TEN_POSITIVE, FIVE_POSITIVE, DIVIDE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideTwoNegative_ok() { | ||
| expected = 2.03921568627451; | ||
| actual = calculator.calculate(TEN_NEGATIVE, FIVE_NEGATIVE, DIVIDE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_dividePositiveByNegative_ok() { | ||
| expected = -2.03921568627451; | ||
| actual = calculator.calculate(TEN_POSITIVE, FIVE_NEGATIVE, DIVIDE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideNegativeByPositive_ok() { | ||
| expected = -2.03921568627451; | ||
| actual = calculator.calculate(TEN_NEGATIVE, FIVE_POSITIVE, DIVIDE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideByNumberItself_ok() { | ||
| expected = 1.0; | ||
| actual = calculator.calculate(FIVE_POSITIVE, FIVE_POSITIVE, DIVIDE); | ||
| assertEquals(expected, actual); | ||
|
|
||
| expected = 1.0; | ||
| actual = calculator.calculate(FIVE_NEGATIVE, FIVE_NEGATIVE, DIVIDE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideByZero_notOk() { | ||
| assertThrows(ArithmeticException.class, | ||
| () -> calculator.calculate(FIVE_POSITIVE, 0, DIVIDE)); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideZero_ok() { | ||
| actual = calculator.calculate(0, FIVE_POSITIVE, DIVIDE); | ||
| assertEquals(0.0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divideMinAndMaxValues_ok() { | ||
| actual = calculator.calculate(MAX_DOUBLE_VALUE, MIN_DOUBLE_VALUE, DIVIDE); | ||
| assertEquals(Double.POSITIVE_INFINITY, actual); | ||
|
|
||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MAX_DOUBLE_VALUE, DIVIDE); | ||
| assertEquals(0.0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisePositiveToPositive_ok() { | ||
| expected = 153769.43917747992; | ||
| actual = calculator.calculate(TEN_POSITIVE, FIVE_POSITIVE, POW); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raiseNegativeToNegative_ok() { | ||
| expected = -0.09615384615384615; | ||
| actual = calculator.calculate(TEN_NEGATIVE, -1, POW); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisePositiveToNegative_ok() { | ||
| expected = 0.19607843137254904; | ||
| actual = calculator.calculate(FIVE_POSITIVE, -1, POW); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raiseNegativeToPositive_ok() { | ||
| expected = -0.19607843137254904; | ||
| actual = calculator.calculate(FIVE_NEGATIVE, -1, POW); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raiseToZero_ok() { | ||
| actual = calculator.calculate(FIVE_POSITIVE, 0, POW); | ||
| assertEquals(1.0, actual); | ||
|
|
||
| actual = calculator.calculate(FIVE_NEGATIVE, 0, POW); | ||
| assertEquals(1.0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raiseMinAndMaxValues_ok() { | ||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MAX_DOUBLE_VALUE, POW); | ||
| assertEquals(0.0, actual); | ||
|
|
||
| actual = calculator.calculate(MAX_DOUBLE_VALUE, MIN_DOUBLE_VALUE, POW); | ||
| assertEquals(1.0, actual); | ||
|
|
||
| actual = calculator.calculate(MAX_DOUBLE_VALUE, MAX_DOUBLE_VALUE, POW); | ||
| assertEquals(Double.POSITIVE_INFINITY, actual); | ||
|
|
||
| actual = calculator.calculate(MIN_DOUBLE_VALUE, MIN_DOUBLE_VALUE, POW); | ||
| assertEquals(1.0, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_WithIllegalOperator_notOk() { | ||
| assertThrows(RuntimeException.class, | ||
| () -> calculator.calculate(FIVE_POSITIVE, FIVE_POSITIVE, ILLEGAL_OPERATION)); | ||
| } | ||
| } | ||
|
|
||
This file was deleted.
Oops, something went wrong.
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.
нам тут не потрібно створювати нові константи, що дублюють існуючі. Можна одразу використовувати Double.MIN_VALUE;