forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculatorUsingStackTest.java
More file actions
42 lines (33 loc) · 1.19 KB
/
BasicCalculatorUsingStackTest.java
File metadata and controls
42 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class BasicCalculatorUsingStackTest {
@Test
void testSimpleAddition() {
assertEquals(5, BasicCalculatorUsingStack.evaluate("2 + 3"));
}
@Test
void testSimpleSubtraction() {
assertEquals(1, BasicCalculatorUsingStack.evaluate("3 - 2"));
}
@Test
void testWithParentheses() {
assertEquals(23, BasicCalculatorUsingStack.evaluate("(1 + (4 + 5 + 2) - 3) + (6 + 8)"));
}
@Test
void testUnaryMinus() {
assertEquals(-2, BasicCalculatorUsingStack.evaluate("-2"));
assertEquals(1, BasicCalculatorUsingStack.evaluate("1 - (-2)"));
}
@Test
void testSpacesInExpression() {
assertEquals(3, BasicCalculatorUsingStack.evaluate(" 2 + 1 "));
}
@Test
void testInvalidExpression() {
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> BasicCalculatorUsingStack.evaluate(""));
assertEquals("Expression must not be null or empty.", exception.getMessage());
}
}