-
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 1 commit
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,215 @@ | ||
| 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 Calculator calculator; | ||
| 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 DELTA = 0.0001; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithTwoPos_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(15.5, 34.5, '+'); | ||
| assertEquals(expected, actual); | ||
|
StasShepet marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithTwoNeg_Ok() { | ||
| double expected = -50; | ||
| double actual = calculator.calculate(-15.5, -34.5, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithOnePosOneNeg_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(100, -50, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithZero_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(50, 0, '+'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(0, 50, '+'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_addWithMaxAndMin_Ok() { | ||
| double expected = MAX_VALUE + MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, '+'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithTwoPos_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(100, 50, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithTwoNeg_Ok() { | ||
| double expected = -50; | ||
| double actual = calculator.calculate(-100, -50, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithOnePosOneNeg_Ok() { | ||
| double expected = 150; | ||
| double actual = calculator.calculate(100, -50, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithZero_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(50, 0, '-'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(0, -50, '-'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subWithMaxAndMin_Ok() { | ||
| double expected = MAX_VALUE - MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, '-'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithTwoPos_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(10, 5, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithTwoNeg_Ok() { | ||
| double expected = 50; | ||
| double actual = calculator.calculate(-10, -5, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithOnePosOneNeg_Ok() { | ||
| double expected = -50; | ||
| double actual = calculator.calculate(10, -5, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithZero_Ok() { | ||
| double expected = 0; | ||
| double actual = calculator.calculate(50, 0, '*'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(0, 50, '*'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multWithMaxAndMin_Ok() { | ||
| double expected = MAX_VALUE * MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, '*'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithTwoPos_Ok() { | ||
| double expected = 10; | ||
| double actual = calculator.calculate(50, 5, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithTwoNeg_Ok() { | ||
| double expected = 10; | ||
| double actual = calculator.calculate(-50, -5, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithOnePosOneNeg_Ok() { | ||
| double expected = -2; | ||
| double actual = calculator.calculate(10, -5, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithZero_Ok() { | ||
| double expected = 0; | ||
| double actual = calculator.calculate(0, 5, '/'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divWithZero_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> calculator.calculate(5, 0, '/')); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divMinAndMax_Ok() { | ||
| double expected = MAX_VALUE / MIN_VALUE; | ||
| double actual = calculator.calculate(MAX_VALUE, MIN_VALUE, '/'); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powWithPos_Ok() { | ||
| double expected = 100; | ||
| double actual = calculator.calculate(10, 2, '^'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(-10, 2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powWithNeg_Ok() { | ||
| double expected = 0.01; | ||
| double actual = calculator.calculate(10, -2, '^'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(-10, -2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powWithZero_Ok() { | ||
| double expected = 1; | ||
| double actual = calculator.calculate(10, 0, '^'); | ||
| assertEquals(expected, actual); | ||
| actual = calculator.calculate(-10, 0, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powZeroPow_Ok() { | ||
| double expected = 0; | ||
| double actual = calculator.calculate(0, 2, '^'); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_powZeroPow_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> calculator.calculate(0, -2, '^')); | ||
| assertThrows(IllegalArgumentException.class, () -> calculator.calculate(0, 0, '^')); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> calculator.calculate(0, 0, 'a')); | ||
| } | ||
| } | ||
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.