Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Changes from all commits
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
18 changes: 11 additions & 7 deletions 18 src/test/java/com/thealgorithms/stacks/PrefixEvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@
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 PrefixEvaluatorTest {

@Test
public void testValidExpressions() {
assertEquals(10, PrefixEvaluator.evaluatePrefix("+ * 2 3 4"));
assertEquals(5, PrefixEvaluator.evaluatePrefix("- + 7 3 5"));
assertEquals(6, PrefixEvaluator.evaluatePrefix("/ * 3 2 1"));
@ParameterizedTest(name = "Expression: \"{0}\" → Result: {1}")
@CsvSource({"'+ * 2 3 4', 10", "'- + 7 3 5', 5", "'/ * 3 2 1', 6"})
void testValidExpressions(String expression, int expected) {
assertEquals(expected, PrefixEvaluator.evaluatePrefix(expression));
}

@Test
public void testInvalidExpression() {
@DisplayName("Should throw EmptyStackException for incomplete expression")
void testInvalidExpression() {
assertThrows(EmptyStackException.class, () -> PrefixEvaluator.evaluatePrefix("+ 3"));
}

@Test
public void testMoreThanOneStackSizeAfterEvaluation() {
@DisplayName("Should throw IllegalArgumentException if stack not reduced to one result")
void testMoreThanOneStackSizeAfterEvaluation() {
assertThrows(IllegalArgumentException.class, () -> PrefixEvaluator.evaluatePrefix("+ 3 4 5"));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.