Skip to content

Commit 0e9be57

Browse files
authored
testing: improving PostfixEvaluatorTest (#6405)
* testing: improving PostfixEvaluatorTest * testing: redundant cases
1 parent 31bf130 commit 0e9be57

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,41 @@
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

66
import java.util.EmptyStackException;
7+
import org.junit.jupiter.api.DisplayName;
78
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.CsvSource;
811

912
public class PostfixEvaluatorTest {
1013

11-
@Test
12-
public void testValidExpressions() {
13-
assertEquals(22, PostfixEvaluator.evaluatePostfix("5 6 + 2 *"));
14-
assertEquals(27, PostfixEvaluator.evaluatePostfix("7 2 + 3 *"));
15-
assertEquals(3, PostfixEvaluator.evaluatePostfix("10 5 / 1 +"));
14+
@ParameterizedTest(name = "Expression: \"{0}\" → Result: {1}")
15+
@CsvSource({"'5 6 + 2 *', 22", "'7 2 + 3 *', 27", "'10 5 / 1 +', 3", "'8', 8", "'3 4 +', 7"})
16+
@DisplayName("Valid postfix expressions")
17+
void testValidExpressions(String expression, int expected) {
18+
assertEquals(expected, PostfixEvaluator.evaluatePostfix(expression));
1619
}
1720

1821
@Test
19-
public void testInvalidExpression() {
22+
@DisplayName("Should throw EmptyStackException for incomplete expression")
23+
void testInvalidExpression() {
2024
assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("5 +"));
2125
}
2226

2327
@Test
24-
public void testMoreThanOneStackSizeAfterEvaluation() {
28+
@DisplayName("Should throw IllegalArgumentException for extra operands")
29+
void testExtraOperands() {
2530
assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 6 + 2 * 3"));
2631
}
32+
33+
@Test
34+
@DisplayName("Should throw ArithmeticException for division by zero")
35+
void testDivisionByZero() {
36+
assertThrows(ArithmeticException.class, () -> PostfixEvaluator.evaluatePostfix("1 0 /"));
37+
}
38+
39+
@Test
40+
@DisplayName("Should throw IllegalArgumentException for invalid characters")
41+
void testInvalidToken() {
42+
assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("1 a +"));
43+
}
2744
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy