Plus
Zur Navigation springen
Zur Suche springen
Beschreibung
Bei der Addition werden zwei Zahlen miteinander addiert.
Syntax
a + b
Parameter
- a - double
- b - double
Return Value
- Gibt eine expression zurück.
Beispiele
<math>3+5</math> --> returns 8
<math>\frac{1}{2} + \frac{3}{2}</math> --> returns 2
JUnit Tests
@Before
public void beforeTest() {
placeFillInVariable(1, OMParser.parse("3"));
placeFillInVariable(2, OMParser.parse("10"));
placeFillInVariable(3, OMParser.parse("3.5"));
placeExerciseVariable("a", OMParser.parse("3"));
placeExerciseVariable("b", OMParser.parse("10"));
placeExerciseVariable("c", OMParser.parse("3.5"));
}
@Test
public void testAddition(){
assertTrue(10 == getEvaluator().getNumberResult("6+4"));
assertTrue(10.2 == getEvaluator().getNumberResult("7.3+2.9"));
assertTrue(1 == getEvaluator().getNumberResult("0.5 + 0.5"));
assertTrue(11 == getEvaluator().getNumberResult("0.5 + 0.5 + 10"));
}
@Test
public void testAdditionWithNegativeNumber(){
assertTrue(4.4 == getEvaluator().getNumberResult("7.3+(-2.9)"));
assertTrue(-5.3 == getEvaluator().getNumberResult("-7.3+2"));
assertTrue(-8 == getEvaluator().getNumberResult("-2+(-6)"));
}
@Test
public void testAdditionWithVariables(){
assertTrue(9 == getEvaluator().getNumberResult("6+[var=a]"));
assertTrue(12.9 == getEvaluator().getNumberResult("[var=b]+2.9"));
assertTrue(10.5 == getEvaluator().getNumberResult("[var=c]+7"));
}
@Test
public void testAdditionWithInput(){
assertTrue(9 == getEvaluator().getNumberResult("6+[pos=1]"));
assertTrue(12.9 == getEvaluator().getNumberResult("[pos=2]+2.9"));
assertTrue(10.5 == getEvaluator().getNumberResult("[pos=3]+7"));
}
@Test
public void testAdditionWithEncapsulation(){
assertTrue(3 == getEvaluator().getNumberResult("1 + (1 + 1)"));
assertTrue(4 == getEvaluator().getNumberResult("1 + (1 + (1 + 1))"));
assertTrue(6 == getEvaluator().getNumberResult("1 + (1 + (1 + (1 + (1 + 1))))"));
assertTrue(6 == getEvaluator().getNumberResult("((((1 + 1) + 1) + 1) + 1) + 1"));
}
@Test(expected=ParserMismatchException.class)
public void testAdditionWithWrongInputCharacter() {
getEvaluator().getNumberResult("6 + a");
}
@Test(expected=UnsupportedBinaryOperatorException.class)
public void testAdditionWithWrongInputString() {
getEvaluator().getNumberResult("6 + 'a'");
}
@Test (expected=UndefinedExerciseVariableException.class)
public void testAdditionWithMissingExcerciseVariable() {
getEvaluator().getNumberResult("[var=j] + 1");
}
@Test (expected=UndefinedFillInVariableException.class)
public void testAdditionWithMissingInput() {
getEvaluator().getNumberResult("[pos=42] + 1");
}
Hinweise
- Mit equal ("==") lässt sich das Ergebnis anschließend vergleichen.