-
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 2 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 NoSuchElementException("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,228 @@ | ||||||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||||
|
|
||||||||
| import java.util.NoSuchElementException; | ||||||||
| import org.junit.jupiter.api.Test; | ||||||||
|
|
||||||||
| class CalculateTest { | ||||||||
|
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 about |
||||||||
| private static final Calculate CALCULATOR = new Calculate(); | ||||||||
|
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 dont think we should make calculator as constant. Lets make it as private static variable and initialize it in @BeforeAll method |
||||||||
| private double actual; | ||||||||
| private double answer; | ||||||||
|
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.
Suggested change
|
||||||||
|
|
||||||||
| @Test | ||||||||
| void additionWithTwoPositiveOperands_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 |
||||||||
| actual = CALCULATOR.calculate(6, 8, Calculate.PLUS); | ||||||||
| answer = 14; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void additionWithTwoNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, -8, Calculate.PLUS); | ||||||||
| answer = -14; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void additionWithPositiveAndNegativeOperands_Ok() { | ||||||||
| double actual = CALCULATOR.calculate(-6, 8, Calculate.PLUS); | ||||||||
| double answer = 2; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void additionWithZeroInDifferentPlaces_Ok() { | ||||||||
| actual = CALCULATOR.calculate(0, 8, Calculate.PLUS); | ||||||||
| answer = 8; | ||||||||
| assertEquals(answer,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 |
||||||||
| actual = CALCULATOR.calculate(6, 0, Calculate.PLUS); | ||||||||
| answer = 6; | ||||||||
| assertEquals(answer,actual); | ||||||||
|
|
||||||||
| actual = CALCULATOR.calculate(0, 0, Calculate.PLUS); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void subtractionWithTwoPositiveOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(6, 8, Calculate.MINUS); | ||||||||
| answer = -2; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void subtractionWithTwoNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, -8, Calculate.MINUS); | ||||||||
| answer = 2; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void subtractionWithPositiveAndNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, 8, Calculate.MINUS); | ||||||||
| answer = -14; | ||||||||
| assertEquals(answer, 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. we can add to this test check for first negative and second positive operands |
||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void subtractionWithZeroInDifferentPlaces_Ok() { | ||||||||
| actual = CALCULATOR.calculate(0, 8, Calculate.MINUS); | ||||||||
| answer = -8; | ||||||||
| assertEquals(answer,actual); | ||||||||
|
|
||||||||
| actual = CALCULATOR.calculate(6, 0, Calculate.MINUS); | ||||||||
| answer = 6; | ||||||||
| assertEquals(answer,actual); | ||||||||
|
|
||||||||
| actual = CALCULATOR.calculate(0, 0, Calculate.MINUS); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void divisionWithTwoPositiveOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(6, 2, Calculate.DIVISION); | ||||||||
| answer = 3; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void divisionWithTwoNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, -2, Calculate.DIVISION); | ||||||||
| answer = 3; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void divisionWithPositiveAndNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, 2, Calculate.DIVISION); | ||||||||
| answer = -3; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void divisionWithZeroInStart_Ok() { | ||||||||
| actual = CALCULATOR.calculate(0, 8, Calculate.DIVISION); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void divisionWithZeroInEnd_notOk() { | ||||||||
| assertThrows(ArithmeticException.class, () -> { | ||||||||
| CALCULATOR.calculate(6, 0, Calculate.DIVISION); | ||||||||
| }); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void divisionWithZeroInStartAndEnd_notOk() { | ||||||||
| assertThrows(ArithmeticException.class, () -> { | ||||||||
| CALCULATOR.calculate(0, 0, Calculate.DIVISION); | ||||||||
| }); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void multiplicationWithTwoPositiveOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(6, 8, Calculate.MULTIPLICATION); | ||||||||
| answer = 48; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void multiplicationWithTwoNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, -8, Calculate.MULTIPLICATION); | ||||||||
| answer = 48; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void multiplicationWithPositiveAndNegativeOperands_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-6, 8, Calculate.MULTIPLICATION); | ||||||||
| answer = -48; | ||||||||
| assertEquals(answer, actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void multiplicationWithZeroInDifferentPlaces_Ok() { | ||||||||
| actual = CALCULATOR.calculate(0, 8, Calculate.MULTIPLICATION); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer,actual); | ||||||||
|
|
||||||||
| actual = CALCULATOR.calculate(6, 0, Calculate.MULTIPLICATION); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer,actual); | ||||||||
|
|
||||||||
| actual = CALCULATOR.calculate(0, 0, Calculate.MULTIPLICATION); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingPositiveValueToThePositivePower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(5, 2, Calculate.EXPONENTIATION); | ||||||||
| answer = 25; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingNegativeValueToThePositivePower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-5, 2, Calculate.EXPONENTIATION); | ||||||||
| answer = 25; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingPositiveValueToTheNegativePower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(10, -3, Calculate.EXPONENTIATION); | ||||||||
| answer = 0.001; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingNegativeValueToTheNegativePower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-10, -3, Calculate.EXPONENTIATION); | ||||||||
| answer = -0.001; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingPositiveValueToZeroPower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(5, 0, Calculate.EXPONENTIATION); | ||||||||
| answer = 1; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingNegativeValueToZeroPower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(-5, 0, Calculate.EXPONENTIATION); | ||||||||
| answer = 1; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingZeroToPower_Ok() { | ||||||||
| actual = CALCULATOR.calculate(0, 0, Calculate.EXPONENTIATION); | ||||||||
| answer = 1; | ||||||||
| assertEquals(answer,actual); | ||||||||
|
|
||||||||
| actual = CALCULATOR.calculate(0, 5, Calculate.EXPONENTIATION); | ||||||||
| answer = 0; | ||||||||
| assertEquals(answer,actual); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void raisingZeroToNegativePower_notOk() { | ||||||||
| assertThrows(ArithmeticException.class, () -> { | ||||||||
| CALCULATOR.calculate(0, -5, Calculate.EXPONENTIATION); | ||||||||
| }); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void illegalOperation_notOk() { | ||||||||
| assertThrows(NoSuchElementException.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.
Maybe IllegalArgumentException would be better here?
