From ef82cad5d648f40023eca44314e927edf4164026 Mon Sep 17 00:00:00 2001 From: alxkm Date: Thu, 17 Jul 2025 22:53:22 +0200 Subject: [PATCH 1/2] testing: improving PostfixEvaluatorTest --- .../stacks/PostfixEvaluatorTest.java | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java b/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java index 882fe644ccd5..8afd8617239c 100644 --- a/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java +++ b/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java @@ -4,24 +4,47 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.EmptyStackException; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; public class PostfixEvaluatorTest { - @Test - public void testValidExpressions() { - assertEquals(22, PostfixEvaluator.evaluatePostfix("5 6 + 2 *")); - assertEquals(27, PostfixEvaluator.evaluatePostfix("7 2 + 3 *")); - assertEquals(3, PostfixEvaluator.evaluatePostfix("10 5 / 1 +")); + @ParameterizedTest(name = "Expression: \"{0}\" → Result: {1}") + @CsvSource({"'5 6 + 2 *', 22", "'7 2 + 3 *', 27", "'10 5 / 1 +', 3", "'8', 8", "'3 4 +', 7"}) + @DisplayName("Valid postfix expressions") + void testValidExpressions(String expression, int expected) { + assertEquals(expected, PostfixEvaluator.evaluatePostfix(expression)); } @Test - public void testInvalidExpression() { + @DisplayName("Should throw EmptyStackException for incomplete expression") + void testInvalidExpression() { assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("5 +")); } @Test - public void testMoreThanOneStackSizeAfterEvaluation() { + @DisplayName("Should throw IllegalArgumentException for extra operands") + void testExtraOperands() { assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 6 + 2 * 3")); } + + @Test + @DisplayName("Should throw ArithmeticException for division by zero") + void testDivisionByZero() { + assertThrows(ArithmeticException.class, () -> PostfixEvaluator.evaluatePostfix("5 0 /")); + } + + @Test + @DisplayName("Should throw IllegalArgumentException for invalid characters") + void testInvalidToken() { + assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 a +")); + } + + @Test + @DisplayName("Should throw EmptyStackException for empty input") + void testEmptyInput() { + assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("")); + } } From bc2d6b95b3ba8211c441b690c6e7d6dffcbb79d0 Mon Sep 17 00:00:00 2001 From: alxkm Date: Thu, 17 Jul 2025 22:57:28 +0200 Subject: [PATCH 2/2] testing: redundant cases --- .../com/thealgorithms/stacks/PostfixEvaluatorTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java b/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java index 8afd8617239c..682240acd752 100644 --- a/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java +++ b/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java @@ -33,18 +33,12 @@ void testExtraOperands() { @Test @DisplayName("Should throw ArithmeticException for division by zero") void testDivisionByZero() { - assertThrows(ArithmeticException.class, () -> PostfixEvaluator.evaluatePostfix("5 0 /")); + assertThrows(ArithmeticException.class, () -> PostfixEvaluator.evaluatePostfix("1 0 /")); } @Test @DisplayName("Should throw IllegalArgumentException for invalid characters") void testInvalidToken() { - assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 a +")); - } - - @Test - @DisplayName("Should throw EmptyStackException for empty input") - void testEmptyInput() { - assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("")); + assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("1 a +")); } }