Laget to varianter til og la til flere tester
This commit is contained in:
@@ -25,6 +25,7 @@ public class Calc {
|
||||
|
||||
/**
|
||||
* Pushes a new operand onto top of the stack.
|
||||
*
|
||||
* @param d the new operand
|
||||
*/
|
||||
public void pushOperand(double d) {
|
||||
@@ -52,6 +53,7 @@ public class Calc {
|
||||
|
||||
/**
|
||||
* Removes and returns the top operand.
|
||||
*
|
||||
* @return the top operand
|
||||
* @throws IllegalStateException if the stack is empty
|
||||
*/
|
||||
@@ -65,6 +67,7 @@ public class Calc {
|
||||
/**
|
||||
* Performs the provided operation in the top operand, and
|
||||
* replaces it with the result.
|
||||
*
|
||||
* @param op the operation to perform
|
||||
* @return the result of performing the operation
|
||||
* @throws IllegalStateException if the operand stack is empty
|
||||
@@ -77,6 +80,7 @@ public class Calc {
|
||||
/**
|
||||
* Performs the provided operation in the two topmost operands, and
|
||||
* replaces them with the result.
|
||||
*
|
||||
* @param op the operation to perform
|
||||
* @return the result of performing the operation
|
||||
* @throws IllegalStateException if the operand count is less than two
|
||||
@@ -94,6 +98,8 @@ public class Calc {
|
||||
|
||||
/**
|
||||
* Swaps the two topmost operands.
|
||||
*
|
||||
* @throws IllegalStateException if the operand count is less than two
|
||||
*/
|
||||
public void swap() {
|
||||
// TODO
|
||||
@@ -101,6 +107,8 @@ public class Calc {
|
||||
|
||||
/**
|
||||
* Duplicates the top operand.
|
||||
*
|
||||
* @throws IllegalStateException if the operand stack is empty
|
||||
*/
|
||||
public void dup() {
|
||||
// TODO
|
||||
|
||||
@@ -28,6 +28,21 @@ public class CalcTest {
|
||||
checkCalc(calc, 3.14, 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeekOperand() {
|
||||
Calc calc = new Calc(1.0, 3.14);
|
||||
Assertions.assertEquals(3.14, calc.peekOperand());
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> new Calc().peekOperand());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeekOperandN() {
|
||||
Calc calc = new Calc(1.0, 3.14);
|
||||
Assertions.assertEquals(3.14, calc.peekOperand(0));
|
||||
Assertions.assertEquals(1.0, calc.peekOperand(1));
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> calc.peekOperand(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopOperand() {
|
||||
Calc calc = new Calc(1.0, 3.14);
|
||||
@@ -36,4 +51,64 @@ public class CalcTest {
|
||||
Assertions.assertEquals(1.0, calc.popOperand());
|
||||
checkCalc(calc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPopOperand_emptyStack() {
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().popOperand());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformOperation1() {
|
||||
Calc calc = new Calc(1.0);
|
||||
Assertions.assertEquals(-1.0, calc.performOperation(n -> -n));
|
||||
checkCalc(calc, -1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformOperation1_emptyOperandStack() {
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().performOperation(n -> -n));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testPerformOperation2() {
|
||||
Calc calc = new Calc(1.0, 3.0);
|
||||
Assertions.assertEquals(-2.0, calc.performOperation((n1, n2) -> n1 - n2));
|
||||
checkCalc(calc, -2.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerformOperation2_lessThanTwoOperands() {
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc(1.0).performOperation((n1, n2) -> n1 - n2));
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().performOperation((n1, n2) -> n1 - n2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwap() {
|
||||
Calc calc = new Calc(1.0, 3.14);
|
||||
calc.swap();
|
||||
checkCalc(calc, 3.14, 1.0);
|
||||
calc.swap();
|
||||
checkCalc(calc, 1.0, 3.14);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwap_lessThanTwoOperands() {
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc(1.0).swap());
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().swap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDup() {
|
||||
Calc calc = new Calc(1.0, 3.14);
|
||||
Assertions.assertEquals(3.14, calc.popOperand());
|
||||
checkCalc(calc, 1.0);
|
||||
Assertions.assertEquals(1.0, calc.popOperand());
|
||||
checkCalc(calc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDup_emptyOperandStack() {
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().dup());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user