-
Notifications
You must be signed in to change notification settings - Fork 257
calculator implemented, tests added #295
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
StasShepet
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
StasShepet:hw-lambda-calc
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 2 commits
Commits
Show all changes
4 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,26 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Calculator { | ||
| public double calculate(double operand1, double operand2, char operation) { | ||
| switch (operation) { | ||
| case '+': | ||
| return operand1 + operand2; | ||
| case '-': | ||
| return operand1 - operand2; | ||
| case '*': | ||
| return operand1 * operand2; | ||
| case '/': | ||
| if (operand2 == 0) { | ||
| throw new IllegalArgumentException(); | ||
|
StasShepet marked this conversation as resolved.
Outdated
|
||
| } | ||
| return operand1 / operand2; | ||
| case '^': | ||
| if (operand1 == 0 && operand2 <= 0) { | ||
| throw new IllegalArgumentException(); | ||
|
StasShepet marked this conversation as resolved.
Outdated
|
||
| } | ||
| return Math.pow(operand1, operand2); | ||
| default: | ||
| throw new IllegalArgumentException(); | ||
|
StasShepet marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
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,269 @@ | ||
| 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; | ||
|
|
||
| class CalculatorTest { | ||
| private static final double MAX_VALUE = Double.MAX_VALUE; | ||
| private static final double MIN_VALUE = Double.MIN_VALUE; | ||
|
StasShepet marked this conversation as resolved.
Outdated
|
||
| private static final double ZERO = 0; | ||
| private static final double FIRST_POS_OPERAND = 5; | ||
| private static final double SECOND_POS_OPERAND = 10; | ||
| private static final double FIRST_NEG_OPERAND = -5; | ||
| private static final double SECOND_NEG_OPERAND = -10; | ||
| private static final double SQUARE = 2; | ||
| private static final double NEG_SQUARE = -2; | ||
| private static final double DELTA = 0.0001; | ||
| 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_POWER = '^'; | ||
| private static final char ILLEGA_OPERATION = 'a'; | ||
| private static Calculator calculator; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithTwoPos_Ok() { | ||
| double expected = 15; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_POS_OPERAND, ADDITION); | ||
| assertEquals(expected, actual, "Test failed! Addition of two positive operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithTwoNeg_Ok() { | ||
| double expected = -15; | ||
| double actual = calculator.calculate(FIRST_NEG_OPERAND, SECOND_NEG_OPERAND, ADDITION); | ||
| assertEquals(expected, actual,"Test failed! Addition of two negative operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithOnePosOneNeg_Ok() { | ||
| double expected = -5; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_NEG_OPERAND, ADDITION); | ||
| assertEquals(expected, actual, "Test failed! Addition of a positive operand and a negative" | ||
| + "operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithZero_Ok() { | ||
| double expected = 5; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, ZERO, ADDITION); | ||
| assertEquals(expected, actual, "Test failed! Addition of a positive operand and a zero" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| actual = calculator.calculate(ZERO, FIRST_POS_OPERAND, ADDITION); | ||
| assertEquals(expected, actual, "Test failed! Addition of a zero operand and a positive" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithMaxAndMin_Ok() { | ||
| double expected = MAX_VALUE + MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, ADDITION); | ||
| assertEquals(expected, actual, DELTA, "Test failed! Addition of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithTwoPos_Ok() { | ||
| double expected = -5; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_POS_OPERAND, SUBTRACTION); | ||
| assertEquals(expected, actual, "Test failed! Subtraction of two positive operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithTwoNeg_Ok() { | ||
| double expected = 5; | ||
| double actual = calculator.calculate(FIRST_NEG_OPERAND, SECOND_NEG_OPERAND, SUBTRACTION); | ||
| assertEquals(expected, actual, "Test failed! Subtraction of two negative operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithOnePosOneNeg_Ok() { | ||
| double expected = 15; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_NEG_OPERAND, SUBTRACTION); | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a positive operand and a " | ||
| + "negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithZero_Ok() { | ||
| double expected = 5; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, ZERO, SUBTRACTION); | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a positive" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| actual = calculator.calculate(ZERO, FIRST_NEG_OPERAND, SUBTRACTION); | ||
| assertEquals(expected, actual, "Test failed! Subtraction of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithMaxAndMin_Ok() { | ||
| double expected = MAX_VALUE - MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, SUBTRACTION); | ||
| assertEquals(expected, actual, DELTA, "Test failed! Subtraction of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithTwoPos_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_POS_OPERAND, MULTIPLICATION); | ||
| assertEquals(expected, actual, "Test failed! Multiplication of two positive operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithTwoNeg_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(FIRST_NEG_OPERAND, SECOND_NEG_OPERAND, MULTIPLICATION); | ||
| assertEquals(expected, actual, "Test failed! Multiplication of two negative operands" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithOnePosOneNeg_Ok() { | ||
| double expected = -50; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_NEG_OPERAND, MULTIPLICATION); | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" | ||
| + " a negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithZero_Ok() { | ||
| double expected = 0; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, ZERO, MULTIPLICATION); | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a positive operand and" | ||
| + " a zero operand should be " + expected + " but was " + actual); | ||
| expected = -0.0; | ||
| actual = calculator.calculate(ZERO, SECOND_NEG_OPERAND, MULTIPLICATION); | ||
| assertEquals(expected, actual, "Test failed! Multiplication of a zero operand and" | ||
| + " a negative operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithMaxAndMin_Ok() { | ||
| double expected = MAX_VALUE * MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, MULTIPLICATION); | ||
| assertEquals(expected, actual, DELTA, "Test failed! Multiplication of min value and " | ||
| + "max value should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithTwoPos_Ok() { | ||
| double expected = 0.5; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_POS_OPERAND, DIVISION); | ||
| assertEquals(expected, actual, "Test failed! Division of two positive operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithTwoNeg_Ok() { | ||
| double expected = 0.5; | ||
| double actual = calculator.calculate(FIRST_NEG_OPERAND, SECOND_NEG_OPERAND, DIVISION); | ||
| assertEquals(expected, actual, "Test failed! Division of two negative operands should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithOnePosOneNeg_Ok() { | ||
| double expected = -0.5; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SECOND_NEG_OPERAND, DIVISION); | ||
| assertEquals(expected, actual, "Test failed! Division of a positive operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithZero_Ok() { | ||
| double expected = -0.0; | ||
| double actual = calculator.calculate(ZERO, SECOND_NEG_OPERAND, DIVISION); | ||
| assertEquals(expected, actual, "Test failed! Division of a zero operand and a negative" | ||
| + " operand should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithZero_notOk() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> calculator.calculate(FIRST_POS_OPERAND, ZERO, DIVISION), | ||
| "Test failed! Division by zero must throw the Arithmetic exception."); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divMinAndMax_Ok() { | ||
| double expected = MAX_VALUE / MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, DIVISION); | ||
| assertEquals(expected, actual, DELTA, "Test failed! Division of min value and max value" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powWithPos_Ok() { | ||
| double expected = 25; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, SQUARE, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising to positive power should be " | ||
| + expected + " but was " + actual); | ||
| actual = calculator.calculate(FIRST_NEG_OPERAND, SQUARE, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising to positive power should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powWithNeg_Ok() { | ||
| double expected = 0.04; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, NEG_SQUARE, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising to negative power should be " | ||
| + expected + " but was " + actual); | ||
| actual = calculator.calculate(FIRST_NEG_OPERAND, NEG_SQUARE, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising to negative power should be " | ||
| + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powWithZero_Ok() { | ||
| double expected = 1; | ||
| double actual = calculator.calculate(FIRST_POS_OPERAND, ZERO, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising to zero power" | ||
| + " should be " + expected + " but was " + actual); | ||
| actual = calculator.calculate(FIRST_NEG_OPERAND, ZERO, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising to zero power" | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powZeroPow_Ok() { | ||
| double expected = 0; | ||
| double actual = calculator.calculate(ZERO, SQUARE, RAISING_TO_POWER); | ||
| assertEquals(expected, actual, "Test failed! Raising zero to positive power " | ||
| + " should be " + expected + " but was " + actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powZeroPow_notOk() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> calculator.calculate(ZERO, NEG_SQUARE, RAISING_TO_POWER), | ||
| "Test failed! Rasing zero to negetive power must throw the Arithmetic exception."); | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> calculator.calculate(ZERO, ZERO, RAISING_TO_POWER), | ||
| "Test failed! Raising zero to zero power must throw the Arithmetic exception."); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_notOk() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> calculator.calculate(FIRST_POS_OPERAND, | ||
| SECOND_POS_OPERAND, | ||
| ILLEGA_OPERATION), | ||
| "Test failed! Illegal operation must throw the Arithmetic exception."); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.