Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dob't remember adding this dependecy, and it still worked ok with org.junit.jupiter.api.Assertions

</dependencies>
<build>
<plugins>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Calculator {
public double calculator(double firstNumber, double secondNumber, char operation) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public double calculator(double firstNumber, double secondNumber, char operation) {
public double calculate(double firstNumber, double secondNumber, char operation) {

switch (operation) {
case '+':
return firstNumber + secondNumber;
case '-':
return firstNumber - secondNumber;
case '*':
return firstNumber * secondNumber;
case '/':
if (secondNumber == 0) {
throw new ArithmeticException("You can't divide by 0!");
}
return firstNumber / secondNumber;
case '^':
return Math.pow(firstNumber, secondNumber);
default:
throw new IllegalArgumentException("This operation " + operation
+ " doesn't exist");
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

236 changes: 236 additions & 0 deletions src/test/java/core/basesyntax/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package core.basesyntax;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {
private static final double DELTA = 0.000001;
private static Calculator calc;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculator calculator

private double expected;
private double actual;

@Before
public void setUp() {
calc = new Calculator();
}

@Test
public void positiveAddition() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong test methods names, read README.md more carefully. For this task use such convention: ; For example, if we are testing the method calculate with an illegal character passed as an operation the test method name should be calculate_illegalOperation_notOk. notOk is because the test expects the calculate method to throw an exception.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the same to everything.

expected = 8;
actual = calc.calculator(1, 7, '+');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create private static variable and use them instead of raw numbers.

assertEquals(expected, actual);
}

@Test
public void negativeAddition() {
expected = -8;
actual = calc.calculator(-1, -7, '+');
assertEquals(expected, actual);
}

@Test
public void positiveNegativeAddition() {
expected = 6;
actual = calc.calculator(-1, 7, '+');
assertEquals(expected, actual);
expected = -6;
actual = calc.calculator(1, -7, '+');
assertEquals(expected, actual);
}

@Test
public void differentPlacesZeroAddition() {
expected = 1;
actual = calc.calculator(1, 0, '+');
assertEquals(expected, actual);
actual = calc.calculator(0, 1, '+');
assertEquals(expected, actual);
}

@Test
public void maxMinValuesAddition() {
expected = Double.POSITIVE_INFINITY;
actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '+');
assertEquals(expected, actual, DELTA);
expected = 0;
actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '+');
assertEquals(expected, actual, DELTA);
}

@Test
public void positiveSubtraction() {
expected = -6;
actual = calc.calculator(1, 7, '-');
assertEquals(expected, actual);
}

@Test
public void negativeSubtraction() {
expected = 6;
actual = calc.calculator(-1, -7, '-');
assertEquals(expected, actual);
}

@Test
public void positiveNegativeSubtraction() {
expected = 8;
actual = calc.calculator(1, -7, '-');
assertEquals(expected, actual);
expected = -8;
actual = calc.calculator(-1, 7, '-');
assertEquals(expected, actual);
}

@Test
public void differentPlacesZeroSubtraction() {
expected = 1;
actual = calc.calculator(1, 0, '-');
assertEquals(expected, actual);
expected = -1;
actual = calc.calculator(0, 1, '-');
assertEquals(expected, actual);
}

@Test
public void maxMinValuesSubtraction() {
expected = 0;
actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '-');
assertEquals(expected, actual, DELTA);
expected = 0;
actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '-');
assertEquals(expected, actual, DELTA);
}

@Test
public void positiveMultiplication() {
expected = 7;
actual = calc.calculator(1, 7, '*');
assertEquals(expected, actual);
}

@Test
public void negativeMultiplication() {
expected = 7;
actual = calc.calculator(-1, -7, '*');
assertEquals(expected, actual);
}

@Test
public void positiveNegativeMultiplication() {
expected = -7;
actual = calc.calculator(-1, 7, '*');
assertEquals(expected, actual);
expected = -7;
actual = calc.calculator(1, -7, '*');
assertEquals(expected, actual);
}

@Test
public void differentPlacesZeroMultiplication() {
expected = 0;
actual = calc.calculator(1, 0, '*');
assertEquals(expected, actual);
expected = 0;
actual = calc.calculator(0, 7, '*');
assertEquals(expected, actual);
}

@Test
public void maxMinValuesMultiplication() {
expected = Double.POSITIVE_INFINITY;
actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '*');
assertEquals(expected, actual);
expected = 0;
actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '*');
assertEquals(expected, actual);
}

@Test
public void positiveDivision() {
expected = 2;
actual = calc.calculator(4,2, '/');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calc.calculator(4, 2, '/');
missed whitespace 😈

assertEquals(expected, actual);
}

@Test
public void negativeDivision() {
expected = 2;
actual = calc.calculator(-4,-2, '/');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😈

assertEquals(expected, actual);
}

@Test
public void positiveNegativeDivision() {
expected = -2;
actual = calc.calculator(-4, 2, '/');
assertEquals(expected, actual);
expected = -2;
actual = calc.calculator(4, -2, '/');
assertEquals(expected, actual);
}

@Test
public void firstPlacesZeroDivision() {
expected = 0;
actual = calc.calculator(0, 2, '/');
assertEquals(expected, actual);
}

@Test
public void secondPlacesZeroDivision() {
assertThrows(ArithmeticException.class,
() -> calc.calculator(2, 0, '/'));
}

@Test
public void maxMinValueDivision() {
expected = 1;
actual = calc.calculator(Double.MAX_VALUE, Double.MAX_VALUE, '/');
assertEquals(expected, actual);
expected = 1;
actual = calc.calculator(Double.MIN_VALUE, Double.MIN_VALUE, '/');
assertEquals(expected, actual);
}

@Test
public void raisingPositiveToPositiveOrNegative() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raisingToPower, but I doubt it critical

expected = 64;
actual = calc.calculator(4, 3, '^');
assertEquals(expected, actual);
expected = 0.0625;
actual = calc.calculator(4, -2, '^');
assertEquals(expected, actual, DELTA);
}

@Test
public void raisingNegativeToPositiveOrNegative() {
expected = 16;
actual = calc.calculator(-4, 2, '^');
assertEquals(expected, actual);
expected = 0.0625;
actual = calc.calculator(-4, -2, '^');
assertEquals(expected, actual, DELTA);
}

@Test
public void raisingValueToZero() {
expected = 1;
actual = calc.calculator(2, 0, '^');
assertEquals(expected, actual, DELTA);
}

@Test
public void raisingZeroToPower() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was commented that it must be tested with positive and negative.

expected = 0;
actual = calc.calculator(0, 2, '^');
assertEquals(expected, actual);
}

@Test
public void illegalOperation() {
assertThrows(IllegalArgumentException.class, () -> calc.calculator(1, 2, '&'));
}
}
8 changes: 0 additions & 8 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.