-
Notifications
You must be signed in to change notification settings - Fork 257
Created a calculator and wrote tests for it #291
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
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,52 @@ | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class Calculate { | ||
| public static final char PLUS = '+'; | ||
| public static final char MINUS = '-'; | ||
| public static final char DIVISION = '/'; | ||
| public static final char MULTIPLICATION = '*'; | ||
| public static final char EXPONENTIATION = '^'; | ||
|
|
||
| public double calculate(double firstNumber, double secondNumber, char operation) { | ||
| switch (operation) { | ||
| case PLUS: | ||
| return getTheAmountResult(firstNumber, secondNumber); | ||
| case MINUS: | ||
| return getTheSubtractionResult(firstNumber, secondNumber); | ||
| case DIVISION: | ||
| return getTheDivisionResult(firstNumber, secondNumber); | ||
| case MULTIPLICATION: | ||
| return getTheMultiplicationResult(firstNumber, secondNumber); | ||
| case EXPONENTIATION: | ||
| return getTheExponentiationResult(firstNumber, secondNumber); | ||
| default: | ||
| throw new IllegalArgumentException("Incorrect operation"); | ||
| } | ||
| } | ||
|
|
||
| private double getTheAmountResult(double firstNumber, double secondNumber) { | ||
| return firstNumber + secondNumber; | ||
| } | ||
|
|
||
| private double getTheSubtractionResult(double firstNumber, double secondNumber) { | ||
| return firstNumber - secondNumber; | ||
| } | ||
|
|
||
| private double getTheDivisionResult(double firstNumber, double secondNumber) { | ||
| if (secondNumber == 0) { | ||
| throw new ArithmeticException("We can't divide by zero"); | ||
| } | ||
| return firstNumber / secondNumber; | ||
| } | ||
|
|
||
| private double getTheMultiplicationResult(double firstNumber, double secondNumber) { | ||
| return firstNumber * secondNumber; | ||
| } | ||
|
|
||
| private double getTheExponentiationResult(double firstNumber, double secondNumber) { | ||
| if (firstNumber == 0 && secondNumber < 0) { | ||
| throw new ArithmeticException("We can't divide by zero"); | ||
| } | ||
| return Math.pow(firstNumber, secondNumber); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| 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 CalculateTest { | ||
| private static final double DELTA = 0.0001; | ||
| private double actual; | ||
| private double expected; | ||
| private static Calculate calculator; | ||
|
|
||
| @BeforeAll | ||
| static void beforeAll() { | ||
| calculator = new Calculate(); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithTwoPositiveOperands_Ok() { | ||
| actual = calculator.calculate(6, 8, Calculate.PLUS); | ||
| expected = 14; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithTwoNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, -8, Calculate.PLUS); | ||
| expected = -14; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithPositiveAndNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, 8, Calculate.PLUS); | ||
| expected = 2; | ||
| 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. I don't think that it is good to use whitespaces between tests in one method. Maybe is not a mistake, and that is more readable, better to ask a mentor. 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. Agree, lets remove redundant empty lines |
||
| @Test | ||
| void calculate_additionWithZeroInDifferentPlaces_Ok() { | ||
| actual = calculator.calculate(0, 8, Calculate.PLUS); | ||
| expected = 8; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(6, 0, Calculate.PLUS); | ||
| expected = 6; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(0, 0, Calculate.PLUS); | ||
| expected = 0; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithMaxValues_OK() { | ||
| actual = calculator.calculate(Double.MAX_VALUE, Double.MAX_VALUE, Calculate.PLUS); | ||
| expected = Double.POSITIVE_INFINITY; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_additionWithMinValue_OK() { | ||
| actual = calculator.calculate(Double.MIN_VALUE, Double.MIN_VALUE, Calculate.PLUS); | ||
| expected = 0; | ||
| assertEquals(expected, actual, DELTA); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithTwoPositiveOperands_Ok() { | ||
| actual = calculator.calculate(6, 8, Calculate.MINUS); | ||
| expected = -2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithTwoNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, -8, Calculate.MINUS); | ||
| expected = 2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithFirstPositiveAndSecondNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, 8, Calculate.MINUS); | ||
| expected = -14; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithFirstNegativeAndSecondPositiveOperands_Ok() { | ||
| actual = calculator.calculate(6, -8, Calculate.MINUS); | ||
| expected = 14; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_subtractionWithZeroInDifferentPlaces_Ok() { | ||
| actual = calculator.calculate(0, 8, Calculate.MINUS); | ||
| expected = -8; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(6, 0, Calculate.MINUS); | ||
| expected = 6; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(0, 0, Calculate.MINUS); | ||
| expected = 0; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithTwoPositiveOperands_Ok() { | ||
| actual = calculator.calculate(6, 2, Calculate.DIVISION); | ||
| expected = 3; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithTwoNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, -2, Calculate.DIVISION); | ||
| expected = 3; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithPositiveAndNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, 2, Calculate.DIVISION); | ||
| expected = -3; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithZeroInStart_Ok() { | ||
| actual = calculator.calculate(0, 8, Calculate.DIVISION); | ||
| expected = 0; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithZeroInEnd_notOk() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculator.calculate(6, 0, Calculate.DIVISION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_divisionWithZeroInStartAndEnd_notOk() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculator.calculate(0, 0, Calculate.DIVISION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithTwoPositiveOperands_Ok() { | ||
| actual = calculator.calculate(6, 8, Calculate.MULTIPLICATION); | ||
| expected = 48; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithTwoNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, -8, Calculate.MULTIPLICATION); | ||
| expected = 48; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithPositiveAndNegativeOperands_Ok() { | ||
| actual = calculator.calculate(-6, 8, Calculate.MULTIPLICATION); | ||
| expected = -48; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_multiplicationWithZeroInDifferentPlaces_Ok() { | ||
| actual = calculator.calculate(0, 8, Calculate.MULTIPLICATION); | ||
| expected = 0; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(6, 0, Calculate.MULTIPLICATION); | ||
| expected = 0; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(0, 0, Calculate.MULTIPLICATION); | ||
| expected = 0; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueToThePositivePower_Ok() { | ||
| actual = calculator.calculate(5, 2, Calculate.EXPONENTIATION); | ||
| expected = 25; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueToThePositivePower_Ok() { | ||
| actual = calculator.calculate(-5, 2, Calculate.EXPONENTIATION); | ||
| expected = 25; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueToTheNegativePower_Ok() { | ||
| actual = calculator.calculate(10, -3, Calculate.EXPONENTIATION); | ||
| expected = 0.001; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueToTheNegativePower_Ok() { | ||
| actual = calculator.calculate(-10, -3, Calculate.EXPONENTIATION); | ||
| expected = -0.001; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingPositiveValueToZeroPower_Ok() { | ||
| actual = calculator.calculate(5, 0, Calculate.EXPONENTIATION); | ||
| expected = 1; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingNegativeValueToZeroPower_Ok() { | ||
| actual = calculator.calculate(-5, 0, Calculate.EXPONENTIATION); | ||
| expected = 1; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingZeroToPower_Ok() { | ||
| actual = calculator.calculate(0, 0, Calculate.EXPONENTIATION); | ||
| expected = 1; | ||
| assertEquals(expected,actual); | ||
| actual = calculator.calculate(0, 5, Calculate.EXPONENTIATION); | ||
| expected = 0; | ||
| assertEquals(expected,actual); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_raisingZeroToNegativePower_notOk() { | ||
| assertThrows(ArithmeticException.class, () -> { | ||
| calculator.calculate(0, -5, Calculate.EXPONENTIATION); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void calculate_illegalOperation_notOk() { | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| calculator.calculate(0, 0, '&'); | ||
| }); | ||
| } | ||
| } | ||
This file was deleted.
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 about
addition for min and max double values;?