Skip to content

Commit 64ff9b2

Browse files
authored
refactor: StackPostfixNotation (#5400)
1 parent b70f077 commit 64ff9b2

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
import java.util.function.BiFunction;
66

77
/**
8-
* @brief Utility class evaluating postix expressions, cf. https://en.wikipedia.org/wiki/Reverse_Polish_notation
9-
* @details The computation is done using Integers.
8+
* Utility class for evaluating postfix expressions using integer arithmetic.
9+
* <p>
10+
* Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators follow their operands.
11+
* This class provides a method to evaluate expressions written in postfix notation.
12+
* </p>
13+
* <p>
14+
* For more information on postfix notation, refer to
15+
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation (RPN) on Wikipedia</a>.
16+
* </p>
1017
*/
1118
public final class StackPostfixNotation {
1219
private StackPostfixNotation() {
@@ -55,7 +62,7 @@ private static void consumeExpression(Stack<Integer> s, final String exp) {
5562
* @exception IllegalArgumentException exp is not a valid postix expression.
5663
*/
5764
public static int postfixEvaluate(final String exp) {
58-
Stack<Integer> s = new Stack<Integer>();
65+
Stack<Integer> s = new Stack<>();
5966
consumeExpression(s, exp);
6067
if (s.size() != 1) {
6168
throw new IllegalArgumentException("exp is not a proper postfix expression.");
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
package com.thealgorithms.stacks;
22

3-
import static java.util.Map.entry;
43
import static org.junit.jupiter.api.Assertions.assertEquals;
54
import static org.junit.jupiter.api.Assertions.assertThrows;
65

7-
import java.util.Map;
8-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
910

1011
public class StackPostfixNotationTest {
11-
@Test
12-
public void testEvaluate() {
13-
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));
14-
for (final var tc : testCases.entrySet()) {
15-
assertEquals(tc.getValue(), StackPostfixNotation.postfixEvaluate(tc.getKey()));
16-
}
17-
}
18-
19-
@Test
20-
public void testIfEvaluateThrowsExceptionForEmptyInput() {
21-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(""));
22-
}
2312

24-
@Test
25-
public void testIfEvaluateThrowsExceptionForInproperInput() {
26-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 3"));
13+
@ParameterizedTest
14+
@MethodSource("provideValidTestCases")
15+
void testEvaluate(String expression, int expected) {
16+
assertEquals(expected, StackPostfixNotation.postfixEvaluate(expression));
2717
}
2818

29-
@Test
30-
public void testIfEvaluateThrowsExceptionForInputWithUnknownOperation() {
31-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("3 3 !"));
19+
static Stream<Arguments> provideValidTestCases() {
20+
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));
3221
}
3322

34-
@Test
35-
public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsA() {
36-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("+"));
23+
@ParameterizedTest
24+
@MethodSource("provideInvalidTestCases")
25+
void testEvaluateThrowsException(String expression) {
26+
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate(expression));
3727
}
3828

39-
@Test
40-
public void testIfEvaluateThrowsExceptionForInputWithTooFewArgsB() {
41-
assertThrows(IllegalArgumentException.class, () -> StackPostfixNotation.postfixEvaluate("2 +"));
29+
static Stream<Arguments> provideInvalidTestCases() {
30+
return Stream.of(Arguments.of(""), Arguments.of("3 3 3"), Arguments.of("3 3 !"), Arguments.of("+"), Arguments.of("2 +"));
4231
}
4332
}

0 commit comments

Comments
 (0)