|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
5 | 5 |
|
6 | 6 | import java.util.EmptyStackException;
|
| 7 | +import org.junit.jupiter.api.DisplayName; |
7 | 8 | import org.junit.jupiter.api.Test;
|
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.CsvSource; |
8 | 11 |
|
9 | 12 | public class PostfixEvaluatorTest {
|
10 | 13 |
|
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)); |
16 | 19 | }
|
17 | 20 |
|
18 | 21 | @Test
|
19 |
| - public void testInvalidExpression() { |
| 22 | + @DisplayName("Should throw EmptyStackException for incomplete expression") |
| 23 | + void testInvalidExpression() { |
20 | 24 | assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("5 +"));
|
21 | 25 | }
|
22 | 26 |
|
23 | 27 | @Test
|
24 |
| - public void testMoreThanOneStackSizeAfterEvaluation() { |
| 28 | + @DisplayName("Should throw IllegalArgumentException for extra operands") |
| 29 | + void testExtraOperands() { |
25 | 30 | assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 6 + 2 * 3"));
|
26 | 31 | }
|
| 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 | + } |
27 | 44 | }
|
0 commit comments