-
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
913bdeb
2dac81e
f75d9a9
067a1b2
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,6 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public interface Calculator { | ||
|
|
||
| double calculate(double firstNumber, char operationType, double secondNumber); | ||
| } |
This file was deleted.
| 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; | ||
| } | ||
| } |
| 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.
| 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 Calculator 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 double actual; | ||
| private double expected; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| myCalculatorTest = new MyCalculator(); | ||
| } | ||
|
|
||
| @Test | ||
| void addition_PositiveNumbers_Ok() { | ||
|
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. Let's name our methods based on the pattern |
||
| expected = 155.5; | ||
| actual = myCalculatorTest.calculate(90.5, ADDITION, 65.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addition_NegativeNumbers_Ok() { | ||
| expected = -241.1; | ||
| actual = myCalculatorTest.calculate(-177.6, ADDITION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addition_PositiveAndNegativeNumbers_Ok() { | ||
| expected = 136.5; | ||
| actual = myCalculatorTest.calculate(200.0, ADDITION, -63.5); | ||
| assertEquals(expected, actual); | ||
|
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 will be if values are 2.0 and 1.1? |
||
| } | ||
|
|
||
| @Test | ||
| void addition_ZeroFirstNumberPosition_Ok() { | ||
| expected = -77; | ||
| actual = myCalculatorTest.calculate(0, ADDITION, -77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addition_ZeroSecondNumberPosition_Ok() { | ||
| expected = 200; | ||
| actual = myCalculatorTest.calculate(200.0, ADDITION, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void addition_MaxValues_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(MAX_VALUE, ADDITION, MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| void addition_MinValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, ADDITION, MIN_VALUE); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_PositiveNumbers_Ok() { | ||
| expected = 25.5; | ||
| actual = myCalculatorTest.calculate(90.5, SUBTRACTION, 65.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_NegativeNumbers_Ok() { | ||
| expected = -114.1; | ||
| actual = myCalculatorTest.calculate(-177.6, SUBTRACTION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_PositiveAndNegativeNumbers_Ok() { | ||
| expected = 263.5; | ||
| actual = myCalculatorTest.calculate(200.0, SUBTRACTION, -63.5); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_ZeroFirstNumberPosition_Ok() { | ||
| expected = 77.0; | ||
| actual = myCalculatorTest.calculate(0, SUBTRACTION, -77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_ZeroSecondNumberPosition_Ok() { | ||
| expected = 200; | ||
| actual = myCalculatorTest.calculate(200.0, SUBTRACTION, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_MaxValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MAX_VALUE, SUBTRACTION, MAX_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void subtraction_MinValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, SUBTRACTION, MIN_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_PositiveNumbers_Ok() { | ||
| expected = 50.0; | ||
| actual = myCalculatorTest.calculate(10.0, MULTIPLICATION, 5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_NegativeNumbers_Ok() { | ||
| expected = 50.0; | ||
| actual = myCalculatorTest.calculate(-10.0, MULTIPLICATION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_PositiveAndNegativeNumbers_Ok() { | ||
| expected = -1000.0; | ||
| actual = myCalculatorTest.calculate(200.0, MULTIPLICATION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_ZeroFirstNumberPosition_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(0, MULTIPLICATION, 77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_ZeroSecondNumberPosition_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(200.0, MULTIPLICATION, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_MaxValues_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(MAX_VALUE, MULTIPLICATION, MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| void multiplication_MinValues_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, MULTIPLICATION, MIN_VALUE); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void division_PositiveNumbers_Ok() { | ||
| expected = 1.3923; | ||
| actual = myCalculatorTest.calculate(90.5, DIVISION, 65.0); | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void division_NegativeNumbers_Ok() { | ||
| expected = 4.0; | ||
| actual = myCalculatorTest.calculate(-20.0, DIVISION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void division_PositiveAndNegativeNumbers_Ok() { | ||
| expected = -4.0; | ||
| actual = myCalculatorTest.calculate(20.0, DIVISION, -5.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void division_ZeroFirstNumberPosition_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(0, DIVISION, 77.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void division_ZeroSecondNumberPosition_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(200.0, DIVISION, 0)); | ||
| } | ||
|
|
||
| @Test | ||
| void division_MaxValues_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(MAX_VALUE, DIVISION, MAX_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void division_MinValues_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(MIN_VALUE, DIVISION, MIN_VALUE); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_PositiveValue_PositivePower_Ok() { | ||
| expected = 400.0; | ||
| actual = myCalculatorTest.calculate(20.0, RAISING_TO_A_POWER, 2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_NegativeValue_PositivePower_Ok() { | ||
| expected = 400.0; | ||
| actual = myCalculatorTest.calculate(-20.0, RAISING_TO_A_POWER, 2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_PositiveValue_NegativePower_Ok() { | ||
| expected = 0.0025; | ||
| actual = myCalculatorTest.calculate(20.0, RAISING_TO_A_POWER, -2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_NegativeValue_NegativePower_Ok() { | ||
| expected = 0.0025; | ||
| actual = myCalculatorTest.calculate(-20.0, RAISING_TO_A_POWER, -2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_PositiveValue_ZeroPower_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(20.0, RAISING_TO_A_POWER, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_NegativeValue_ZeroPower_Ok() { | ||
| expected = 1.0; | ||
| actual = myCalculatorTest.calculate(-20.0, RAISING_TO_A_POWER, 0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void raising_ZeroValue_PositivePower_Ok() { | ||
| expected = 0; | ||
| actual = myCalculatorTest.calculate(0, RAISING_TO_A_POWER, 2.0); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void illegalOperation_NotOk() { | ||
| assertThrows(OperationTypeException.class, () -> | ||
| myCalculatorTest.calculate(65.0, '!', 50.0)); | ||
| } | ||
| } | ||
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.
constants should be declare before non-constant fields