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
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax;

public class Calculator {

public double calculate(double a, double b, char c) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

use descriptive parameter and variable names.

switch (c) {
case '+':
return a + b;
case '-':
return a - b;
case '/':
if (b == 0) {
throw new RuntimeException("Error, division by 0");
}
return a / b;
case '*':
return a * b;
case '^':
if (a < 0 && b != (long) b) {
throw new RuntimeException("Error");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no need to throw Exception. We can raise any number to any power from a mathematical point of view.

return Math.pow(a, b);
default:
throw new RuntimeException("Error, unknown operation type");
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

302 changes: 302 additions & 0 deletions src/test/java/core/basesyntax/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
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;

public class CalculatorTest {
private static Calculator calculator;

@BeforeAll
static void beforeAll() {
calculator = new Calculator();
}

/*
Tests for additions.
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't use comments. Your code should be understandable without them


@Test
void additionWithTwoPositiveOperands_isOk() {
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.

what method do we test?

double a = 23.4;
double b = 700.5;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

frequently used variables can be translated into constants.

double actual = calculator.calculate(a, b, '+');
double expected = 723.9;
assertEquals(expected, actual);
}

@Test
void additionWithTwoNegativeOperands_isOk() {
double a = -23.4;
double b = -700.5;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

frequently used variables can be translated into constants.

double expected = -723.9;
double actual = calculator.calculate(a, b, '+');
assertEquals(expected, actual);
}

@Test
void additionWithPositiveAndNegativeOperands_isOk() {
double a = -23.4;
double b = 700.5;
double expected = 677.1;
double actual = calculator.calculate(a, b, '+');
assertEquals(expected, actual);
}

@Test
void additionWithZeroInDifferentPlaces_isOk() {
double a = 0;
double b = 700.5;
double expected = 700.5;
double actual = calculator.calculate(a, b, '+');
assertEquals(expected, actual);
a = 321.45;
b = 0;
expected = 321.45;
actual = calculator.calculate(a, b, '+');
assertEquals(expected, actual);
}

@Test
void additionForMinAndMaxDoubleValues_isOk() {
double a = Double.MAX_VALUE;
double b = Double.MIN_VALUE;
double delta = 0.0001;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

the delta variable must be declared constant. Check Common mistakes

double actual = calculator.calculate(a, b, '+');
assertEquals(a, actual, delta);
}

/*
Tests for subtractions.
*/

@Test
void subtractionWithTwoPositiveOperands_isOk() {
double a = 23.4;
double b = 700.5;
double actual = calculator.calculate(a, b, '-');
double expected = -677.1;
assertEquals(expected, actual);
}

@Test
void subtractionWithTwoNegativeOperands_isOk() {
double a = -23.4;
double b = -700.5;
double expected = 677.1;
double actual = calculator.calculate(a, b, '-');
assertEquals(expected, actual);
}

@Test
void subtractionWithPositiveAndNegativeOperands_isOk() {
double a = -23.4;
double b = 700.5;
double expected = -723.9;
double actual = calculator.calculate(a, b, '-');
assertEquals(expected, actual);
}

@Test
void subtractionWithZeroInDifferentPlaces_isOk() {
double a = 0;
double b = 700.5;
double expected = -700.5;
double actual = calculator.calculate(a, b, '-');
assertEquals(expected, actual);
a = 321.45;
b = 0;
expected = 321.45;
actual = calculator.calculate(a, b, '-');
assertEquals(expected, actual);
}

@Test
void subtractionForMinAndMaxDoubleValues_isOk() {
double a = Double.MAX_VALUE;
double b = Double.MIN_VALUE;
double actual = calculator.calculate(a, b, '-');
double expected = Double.MAX_VALUE;
double delta = 0.0001;
assertEquals(expected, actual, delta);
}

/*
Tests for multiplication.
*/

@Test
void multiplicationWithTwoPositiveOperands_isOk() {
double a = 23.4;
double b = 700.5;
double actual = calculator.calculate(a, b, '*');
double expected = 16391.7;
assertEquals(expected, actual);
}

@Test
void multiplicationWithTwoNegativeOperands_isOk() {
double a = -23.4;
double b = -700.5;
double expected = 16391.7;
double actual = calculator.calculate(a, b, '*');
assertEquals(expected, actual);
}

@Test
void multiplicationWithPositiveAndNegativeOperands_isOk() {
double a = -23.4;
double b = 700.5;
double expected = -16391.7;
double actual = calculator.calculate(a, b, '*');
assertEquals(expected, actual);
}

@Test
void multiplicationWithZeroInDifferentPlaces_isOk() {
double a = 0;
double b = 700.5;
double expected = 0;
double actual = calculator.calculate(a, b, '*');
assertEquals(expected, actual);
a = 321.45;
b = 0;
actual = calculator.calculate(a, b, '*');
assertEquals(expected, actual);
}

@Test
void multiplicationForMinAndMaxDoubleValues_isOk() {
double a = Double.MAX_VALUE;
double b = Double.MIN_VALUE;
double actual = calculator.calculate(a, b, '*');
double delta = 0.0001;
assertEquals(0, actual, delta);
}

/*
Tests for division.
*/

@Test
void divisionWithTwoPositiveOperands_isOk() {
double a = 8;
double b = 25;
double expected = 0.32;
double actual = calculator.calculate(a, b, '/');
assertEquals(expected, actual);
}

@Test
void divisionWithTwoNegativeOperands_isOk() {
double a = -8;
double b = -25;
double expected = 0.32;
double actual = calculator.calculate(a, b, '/');
assertEquals(expected, actual);
}

@Test
void divisionWithPositiveAndNegativeOperands_isOk() {
double a = 8;
double b = -25;
double expected = -0.32;
double actual = calculator.calculate(a, b, '/');
assertEquals(expected, actual);
}

@Test
void divisionWithZeroInDifferentPlaces() {
double a = 0;
double b = 25;
double expected = 0;
double actual = calculator.calculate(a, b, '/');
assertEquals(expected, actual);
double anotherA = 8;
double anotherB = 0;
assertThrows(RuntimeException.class, () -> calculator.calculate(anotherA, anotherB, '/'));
}

/*
Tests for ^ operation.
*/

@Test
void raisingForMinAndMaxDoubleValues() {
double a = Double.MAX_VALUE;
double b = Double.MIN_VALUE;
double expected = 1;
double delta = 0.0001;
double actual = calculator.calculate(a, b, '^');
assertEquals(expected, actual, delta);
a = Double.MIN_VALUE;
b = Double.MAX_VALUE;
actual = calculator.calculate(a, b, '^');
expected = 0;
assertEquals(expected, actual, delta);
}

@Test
void raisingPositiveOrNegativeValueToThePositivePower() {
final double b = 2.5;
double a = 4.5;
double expected = 42.956737;
double delta = 0.0001;
double actual = calculator.calculate(a, b, '^');
assertEquals(expected, actual, delta);
double negativeA = -4.5;
assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^'));

}

@Test
void raisingPositiveOrNegativeValueToTheNegativePower() {
double a = 4.5;
double b = -2.2;
double expected = 0.03655;
double delta = 0.0001;
double actual = calculator.calculate(a, b, '^');
assertEquals(expected, actual, delta);
double negativeA = -4.5;
assertThrows(RuntimeException.class, () -> calculator.calculate(negativeA, b, '^'));
}

@Test
void raisingPositiveOrNegativeValueToTheZeroPower() {
double a = 4.5;
double b = 0;
double expected = 1;
double actual = calculator.calculate(a, b, '^');
assertEquals(expected, actual);
a = -4.5;
expected = 1;
actual = calculator.calculate(a, b, '^');
assertEquals(expected, actual);
}

@Test
void raisingZeroToPower() {
double a = 0;
double b = 4.5;
double expected = 0;
double actual = calculator.calculate(a, b, '^');
assertEquals(expected, actual);
}

/*
Tests for illegal operation.
*/

@Test
void illegalOperations_ExceptionOk() {
double finalA = 24;
double finalB = -8;
assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '='));
assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '!'));
assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '$'));
assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '%'));
assertThrows(RuntimeException.class, () -> calculator.calculate(finalA, finalB, '('));
}
}
8 changes: 0 additions & 8 deletions src/test/java/core/basesyntax/HelloWorldTest.java

This file was deleted.