mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
feat: Add PostfixEvaluator
new algorithm with Junit tests (#5754)
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
package com.thealgorithms.stacks;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.EmptyStackException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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 +"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidExpression() {
|
||||
assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("5 +"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoreThanOneStackSizeAfterEvaluation() {
|
||||
assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 6 + 2 * 3"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user