Implement toString for IntOperator and ReversePolishStack

Co-Authored-By: td1223
This commit is contained in:
Gleb Koval 2024-11-05 18:26:50 +00:00
parent ff6d33770f
commit 855ce9fd55
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
4 changed files with 46 additions and 15 deletions

View File

@ -4,10 +4,12 @@ import java.util.List;
import java.util.function.Function;
public class IntOperator {
private final String symbol;
private final int operands;
private final Function<List<Integer>, Integer> operator;
IntOperator(int operands, Function<List<Integer>, Integer> operator) {
IntOperator(String symbol, int operands, Function<List<Integer>, Integer> operator) {
this.symbol = symbol;
this.operands = operands;
this.operator = operator;
}
@ -24,4 +26,9 @@ public class IntOperator {
}
return operator.apply(arguments);
}
@Override
public String toString() {
return symbol;
}
}

View File

@ -1,6 +1,7 @@
package ic.doc;
import java.util.Stack;
import java.util.stream.Collectors;
public class ReversePolishStack {
private final Stack<IntOperator> stack = new Stack<>();
@ -34,7 +35,12 @@ public class ReversePolishStack {
if (!stack.isEmpty()) {
throw new ArithmeticException("Invalid notation, too many operators");
}
stack.push(new IntOperator(0, args -> result));
stack.push(new IntOperator(Integer.toString(result), 0, args -> result));
return result;
}
@Override
public String toString() {
return stack.stream().map(IntOperator::toString).collect(Collectors.joining(" "));
}
}

View File

@ -11,13 +11,13 @@ import static org.junit.Assert.fail;
public class IntOperatorTest {
@Test
public void intOperatorCanEvaluate() {
IntOperator intOperator = new IntOperator(2, arguments -> arguments.get(0) + arguments.get(1));
IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1));
assertThat(intOperator.evaluate(List.of(1, 2)), is(3));
}
@Test
public void intOperatorThrowsOnInvalidArguments() {
IntOperator intOperator = new IntOperator(2, arguments -> arguments.get(0) + arguments.get(1));
IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1));
try {
intOperator.evaluate(List.of(1));
fail("Expected IllegalArgumentException to be thrown on invalid number of arguments");
@ -28,7 +28,13 @@ public class IntOperatorTest {
@Test
public void canGetOperatorOperandsCount() {
IntOperator intOperator = new IntOperator(2, arguments -> arguments.get(0) + arguments.get(1));
IntOperator intOperator = new IntOperator("+", 2, args -> args.get(0) + args.get(1));
assertThat(intOperator.getOperands(), is(2));
}
@Test
public void operatorToStringReturnsSymbol() {
IntOperator intOperator = new IntOperator("symbol", 0, args -> 0);
assertThat(intOperator.toString(), is("symbol"));
}
}

View File

@ -16,25 +16,25 @@ public class ReversePolishStackTest {
@Test
public void canPushToReversePolishStack() {
reversePolishStack.push(new IntOperator(0, args -> 1));
reversePolishStack.push(new IntOperator("1", 0, args -> 1));
assertThat(reversePolishStack.getSize(), is(1));
reversePolishStack.push(new IntOperator(0, args -> 2));
reversePolishStack.push(new IntOperator("2", 0, args -> 2));
assertThat(reversePolishStack.getSize(), is(2));
}
@Test
public void canEvaluateReversePolishStack() {
reversePolishStack.push(new IntOperator(0, args -> 10));
reversePolishStack.push(new IntOperator(0, args -> 25));
reversePolishStack.push(new IntOperator(2, args -> args.get(0) + args.get(1)));
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)));
assertThat(reversePolishStack.evaluate(), is(35));
}
@Test
public void evaluatePushesBack() {
reversePolishStack.push(new IntOperator(0, args -> 10));
reversePolishStack.push(new IntOperator(0, args -> 25));
reversePolishStack.push(new IntOperator(2, args -> args.get(0) + args.get(1)));
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.evaluate();
assertThat(reversePolishStack.getSize(), is(1));
assertThat(reversePolishStack.evaluate(), is(35));
@ -42,8 +42,8 @@ public class ReversePolishStackTest {
@Test
public void evaluateThrowsOnInvalidNotation() {
reversePolishStack.push(new IntOperator(0, args -> 10));
reversePolishStack.push(new IntOperator(0, args -> 25));
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
reversePolishStack.push(new IntOperator("25", 0, args -> 25));
try {
reversePolishStack.evaluate();
fail("Expected ArithmeticException to be thrown when too many operators");
@ -51,4 +51,16 @@ public class ReversePolishStackTest {
// good
}
}
@Test
public void testReversePolishStackString() {
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("-", 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 - *"));
}
}