From d463251ff1d28e7e47892b9f17c7c458e05d99c7 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 5 Nov 2024 19:05:33 +0000 Subject: [PATCH] Fix reverse polish stack argument order and add non-symmetric evaluation tests Co-Authored-By: td1223 --- src/main/java/ic/doc/ReversePolishStack.java | 4 ++-- src/test/java/ic/doc/IntOperatorTest.java | 6 ++++++ src/test/java/ic/doc/ReversePolishStackTest.java | 16 ++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/ic/doc/ReversePolishStack.java b/src/main/java/ic/doc/ReversePolishStack.java index 0a2f164..5f8f75e 100644 --- a/src/main/java/ic/doc/ReversePolishStack.java +++ b/src/main/java/ic/doc/ReversePolishStack.java @@ -26,12 +26,12 @@ public class ReversePolishStack { } IntOperator intOperator = stack.pop(); - Stack args = new Stack<>(); + List 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); } diff --git a/src/test/java/ic/doc/IntOperatorTest.java b/src/test/java/ic/doc/IntOperatorTest.java index 027bdb6..691be9b 100644 --- a/src/test/java/ic/doc/IntOperatorTest.java +++ b/src/test/java/ic/doc/IntOperatorTest.java @@ -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)); diff --git a/src/test/java/ic/doc/ReversePolishStackTest.java b/src/test/java/ic/doc/ReversePolishStackTest.java index ec4419f..c54d5be 100644 --- a/src/test/java/ic/doc/ReversePolishStackTest.java +++ b/src/test/java/ic/doc/ReversePolishStackTest.java @@ -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 - *"));