mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
testing: refactor to ParameterizedTest PrefixEvaluatorTest (#6415)
testing: refactor to ParameterizedTest PrefixEvaluatorTest Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
cfd784105b
commit
ceead5eccd
@@ -4,24 +4,28 @@ 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.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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user