refactor: StackPostfixNotation
(#5400)
This commit is contained in:
parent
b70f077343
commit
64ff9b2efe
@ -5,8 +5,15 @@ import java.util.Stack;
|
|||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Utility class evaluating postix expressions, cf. https://en.wikipedia.org/wiki/Reverse_Polish_notation
|
* Utility class for evaluating postfix expressions using integer arithmetic.
|
||||||
* @details The computation is done using Integers.
|
* <p>
|
||||||
|
* Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators follow their operands.
|
||||||
|
* This class provides a method to evaluate expressions written in postfix notation.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* For more information on postfix notation, refer to
|
||||||
|
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation (RPN) on Wikipedia</a>.
|
||||||
|
* </p>
|
||||||
*/
|
*/
|
||||||
public final class StackPostfixNotation {
|
public final class StackPostfixNotation {
|
||||||
private StackPostfixNotation() {
|
private StackPostfixNotation() {
|
||||||
@ -55,7 +62,7 @@ public final class StackPostfixNotation {
|
|||||||
* @exception IllegalArgumentException exp is not a valid postix expression.
|
* @exception IllegalArgumentException exp is not a valid postix expression.
|
||||||
*/
|
*/
|
||||||
public static int postfixEvaluate(final String exp) {
|
public static int postfixEvaluate(final String exp) {
|
||||||
Stack<Integer> s = new Stack<Integer>();
|
Stack<Integer> s = new Stack<>();
|
||||||
consumeExpression(s, exp);
|
consumeExpression(s, exp);
|
||||||
if (s.size() != 1) {
|
if (s.size() != 1) {
|
||||||
throw new IllegalArgumentException("exp is not a proper postfix expression.");
|
throw new IllegalArgumentException("exp is not a proper postfix expression.");
|
||||||
|
@ -1,43 +1,32 @@
|
|||||||
package com.thealgorithms.stacks;
|
package com.thealgorithms.stacks;
|
||||||
|
|
||||||
import static java.util.Map.entry;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.stream.Stream;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
public class StackPostfixNotationTest {
|
public class StackPostfixNotationTest {
|
||||||
@Test
|
|
||||||
public void testEvaluate() {
|
@ParameterizedTest
|
||||||
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));
|
@MethodSource("provideValidTestCases")
|
||||||
for (final var tc : testCases.entrySet()) {
|
void testEvaluate(String expression, int expected) {
|
||||||
assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey()));
|
assertEquals(expected, StackPostfixNotation.postfixEvaluate(expression));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
static Stream<Arguments> provideValidTestCases() {
|
||||||
public void testIfEvaluateThrowsExceptionForEmptyInput() {
|
return Stream.of(Arguments.of("1 1 +", 2), Arguments.of("2 3 *", 6), Arguments.of("6 2 /", 3), Arguments.of("-5 -2 -", -3), Arguments.of("5 2 + 3 *", 21), Arguments.of("-5", -5));
|
||||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@ParameterizedTest
|
||||||
public void testIfEvaluateThrowsExceptionForInproperInput() {
|
@MethodSource("provideInvalidTestCases")
|
||||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3"));
|
void testEvaluateThrowsException(String expression) {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
static Stream<Arguments> provideInvalidTestCases() {
|
||||||
public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
|
return Stream.of(Arguments.of(""), Arguments.of("3 3 3"), Arguments.of("3 3 !"), Arguments.of("+"), Arguments.of("2 +"));
|
||||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsA() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("+"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsB() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("2 +"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user