mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-28 06:55:02 +08:00
Handle incorrect inputs in StackPostfixNotation
(#4261)
This commit is contained in:
@ -0,0 +1,33 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import static java.util.Map.entry;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StackPostfixNotationTest {
|
||||
@Test
|
||||
public void testEvaluate() {
|
||||
final Map<String, Integer> testCases = Map.ofEntries(entry("1 1 +", 2), entry("2 3 *", 6), entry("6 2 /", 3), entry("-5 -2 -", -3), entry("5 2 + 3 *", 21), entry("-5", -5));
|
||||
for (final var tc : testCases.entrySet()) {
|
||||
assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIfEvaluateThrowsExceptionForEmptyInput() {
|
||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIfEvaluateThrowsExceptionForInproperInput() {
|
||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
|
||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user