Implement toString for IntOperator and ReversePolishStack
Co-Authored-By: td1223
This commit is contained in:
parent
ff6d33770f
commit
855ce9fd55
|
@ -4,10 +4,12 @@ import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class IntOperator {
|
public class IntOperator {
|
||||||
|
private final String symbol;
|
||||||
private final int operands;
|
private final int operands;
|
||||||
private final Function<List<Integer>, Integer> operator;
|
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.operands = operands;
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
@ -24,4 +26,9 @@ public class IntOperator {
|
||||||
}
|
}
|
||||||
return operator.apply(arguments);
|
return operator.apply(arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return symbol;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ic.doc;
|
package ic.doc;
|
||||||
|
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ReversePolishStack {
|
public class ReversePolishStack {
|
||||||
private final Stack<IntOperator> stack = new Stack<>();
|
private final Stack<IntOperator> stack = new Stack<>();
|
||||||
|
@ -34,7 +35,12 @@ public class ReversePolishStack {
|
||||||
if (!stack.isEmpty()) {
|
if (!stack.isEmpty()) {
|
||||||
throw new ArithmeticException("Invalid notation, too many operators");
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return stack.stream().map(IntOperator::toString).collect(Collectors.joining(" "));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,13 +11,13 @@ import static org.junit.Assert.fail;
|
||||||
public class IntOperatorTest {
|
public class IntOperatorTest {
|
||||||
@Test
|
@Test
|
||||||
public void intOperatorCanEvaluate() {
|
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));
|
assertThat(intOperator.evaluate(List.of(1, 2)), is(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void intOperatorThrowsOnInvalidArguments() {
|
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 {
|
try {
|
||||||
intOperator.evaluate(List.of(1));
|
intOperator.evaluate(List.of(1));
|
||||||
fail("Expected IllegalArgumentException to be thrown on invalid number of arguments");
|
fail("Expected IllegalArgumentException to be thrown on invalid number of arguments");
|
||||||
|
@ -28,7 +28,13 @@ public class IntOperatorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canGetOperatorOperandsCount() {
|
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));
|
assertThat(intOperator.getOperands(), is(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void operatorToStringReturnsSymbol() {
|
||||||
|
IntOperator intOperator = new IntOperator("symbol", 0, args -> 0);
|
||||||
|
assertThat(intOperator.toString(), is("symbol"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,25 +16,25 @@ public class ReversePolishStackTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canPushToReversePolishStack() {
|
public void canPushToReversePolishStack() {
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 1));
|
reversePolishStack.push(new IntOperator("1", 0, args -> 1));
|
||||||
assertThat(reversePolishStack.getSize(), is(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));
|
assertThat(reversePolishStack.getSize(), is(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canEvaluateReversePolishStack() {
|
public void canEvaluateReversePolishStack() {
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 10));
|
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 25));
|
reversePolishStack.push(new IntOperator("25", 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.evaluate(), is(35));
|
assertThat(reversePolishStack.evaluate(), is(35));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void evaluatePushesBack() {
|
public void evaluatePushesBack() {
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 10));
|
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 25));
|
reversePolishStack.push(new IntOperator("25", 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)));
|
||||||
reversePolishStack.evaluate();
|
reversePolishStack.evaluate();
|
||||||
assertThat(reversePolishStack.getSize(), is(1));
|
assertThat(reversePolishStack.getSize(), is(1));
|
||||||
assertThat(reversePolishStack.evaluate(), is(35));
|
assertThat(reversePolishStack.evaluate(), is(35));
|
||||||
|
@ -42,8 +42,8 @@ public class ReversePolishStackTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void evaluateThrowsOnInvalidNotation() {
|
public void evaluateThrowsOnInvalidNotation() {
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 10));
|
reversePolishStack.push(new IntOperator("10", 0, args -> 10));
|
||||||
reversePolishStack.push(new IntOperator(0, args -> 25));
|
reversePolishStack.push(new IntOperator("25", 0, args -> 25));
|
||||||
try {
|
try {
|
||||||
reversePolishStack.evaluate();
|
reversePolishStack.evaluate();
|
||||||
fail("Expected ArithmeticException to be thrown when too many operators");
|
fail("Expected ArithmeticException to be thrown when too many operators");
|
||||||
|
@ -51,4 +51,16 @@ public class ReversePolishStackTest {
|
||||||
// good
|
// 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 - *"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue