Skip to content

testing: improving PostfixEvaluatorTest #6405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2025
Merged
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
Next Next commit
testing: improving PostfixEvaluatorTest
  • Loading branch information
alxkm committed Jul 17, 2025
commit ef82cad5d648f40023eca44314e927edf4164026
Original file line number Diff line number Diff line change
Expand Up @@ -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(""));
}
}
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