Fix reverse polish stack argument order and add non-symmetric evaluation tests
Co-Authored-By: td1223
This commit is contained in:
parent
38e373bdfc
commit
d463251ff1
|
@ -26,12 +26,12 @@ public class ReversePolishStack {
|
|||
}
|
||||
|
||||
IntOperator intOperator = stack.pop();
|
||||
Stack<Integer> args = new Stack<>();
|
||||
List<Integer> args = new LinkedList<>();
|
||||
while (args.size() < intOperator.getOperands()) {
|
||||
if (stack.isEmpty()) {
|
||||
throw new ArithmeticException("Not enough arguments to evaluate operator");
|
||||
}
|
||||
args.push(evaluateNext());
|
||||
args.addFirst(evaluateNext());
|
||||
}
|
||||
return intOperator.evaluate(args);
|
||||
}
|
||||
|
|
|
@ -15,6 +15,12 @@ public class IntOperatorTest {
|
|||
assertThat(intOperator.evaluate(List.of(1, 2)), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intOperatorCanEvaluateNonSymmetric() {
|
||||
IntOperator intOperator = new IntOperator("-", 2, args -> args.get(0) - args.get(1));
|
||||
assertThat(intOperator.evaluate(List.of(1, 2)), is(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intOperatorThrowsOnInvalidArguments() {
|
||||
IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1));
|
||||
|
|
|
@ -30,6 +30,18 @@ public class ReversePolishStackTest {
|
|||
assertThat(reversePolishStack.evaluate(), is(35));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEvaluateReversePolishStackNonSymmetric() {
|
||||
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
|
||||
reversePolishStack.push(new IntOperator("25", 0, args -> 25));
|
||||
reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1)));
|
||||
reversePolishStack.push(new IntOperator("9", 0, args -> 9));
|
||||
reversePolishStack.push(new IntOperator("1", 0, args -> 1));
|
||||
reversePolishStack.push(new IntOperator("-", 2, args -> args.get(0) - args.get(1)));
|
||||
reversePolishStack.push(new IntOperator("*", 2, args -> args.get(0) * args.get(1)));
|
||||
assertThat(reversePolishStack.evaluate(), is(280));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void evaluatePushesBack() {
|
||||
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
|
||||
|
@ -57,8 +69,8 @@ public class ReversePolishStackTest {
|
|||
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
|
||||
reversePolishStack.push(new IntOperator("25", 0, args -> 25));
|
||||
reversePolishStack.push(new IntOperator("+", 2, args -> args.get(0) + args.get(1)));
|
||||
reversePolishStack.push(new IntOperator("1", 0, args -> 10));
|
||||
reversePolishStack.push(new IntOperator("9", 0, args -> 25));
|
||||
reversePolishStack.push(new IntOperator("1", 0, args -> 1));
|
||||
reversePolishStack.push(new IntOperator("9", 0, args -> 9));
|
||||
reversePolishStack.push(new IntOperator("-", 2, args -> args.get(0) - args.get(1)));
|
||||
reversePolishStack.push(new IntOperator("*", 2, args -> args.get(0) * args.get(1)));
|
||||
assertThat(reversePolishStack.toString(), is("10 25 + 1 9 - *"));
|
||||
|
|
Loading…
Reference in New Issue