Eval: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 22: | Zeile 22: | ||
<span class="mw-customtoggle-myDivision">[Anzeigen]</span> | <span class="mw-customtoggle-myDivision">[Anzeigen]</span> | ||
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision"> | <syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision"> | ||
@ | @Test | ||
public void | public void testEval1() { | ||
assertEquals(2, Evaluator.getNumberResult("eval('1+1')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testEval2() { | ||
assertEquals(2.2, Evaluator.getNumberResult("eval('1.2+1')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testEval3() { | ||
assertEquals(5, Evaluator.getNumberResult("eval(abs(-5))", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testEval4() { | ||
assertEquals(12, Evaluator.getNumberResult("eval('2+5*2')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testEval5() { | ||
assertEquals(16, Evaluator.getNumberResult("eval('2*(5+3)')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testEval6() { | ||
assertEquals(1, Evaluator.getNumberResult("eval(5%2)", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test ( | @Test | ||
public void testEval7() { | |||
assertNotEquals(1, Evaluator.getNumberResult("eval('2+2')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithInput1() { | |||
assertEquals(0, Evaluator.getNumberResult("eval('[pos=1]')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithInput2() { | |||
assertEquals(0, Evaluator.getNumberResult("eval('[pos=2]-1')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithVariables1() { | |||
assertEquals(0, Evaluator.getNumberResult("eval('[var=a]')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithVariables2() { | |||
assertEquals(0, Evaluator.getNumberResult("eval('[var=b]-1')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithExpressions1() { | |||
assertEquals(6, Evaluator.getNumberResult("eval(eval('2+2') + eval('1+1'))", exerciseVariableMap, | |||
fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithExpressions2() { | |||
assertEquals(1, Evaluator.getNumberResult("eval(eval(1))", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testEvalWithFunction1() { | |||
assertNotEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("5*x")), | |||
Evaluator.evaluate("eval('2*x+3*x')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testEvalWithFunction2() { | |||
assertNotEquals(1, Evaluator.getNumberResult("eval('2*x=2')", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testEvalWithWrongInputEmptyStringArgument() { | public void testEvalWithWrongInputEmptyStringArgument() { | ||
Evaluator.evaluate("eval('')", exerciseVariableMap, fillInVariableMap); | |||
} | } | ||
@Test | @Test | ||
public void testEvalWithWrongInputCharacter() { | public void testEvalWithWrongInputCharacter() { | ||
OMOBJ expected = new OMOBJ(); | |||
expected.setOMV(OMCreator.createOMV("a")); | |||
assertEquals(expected, Evaluator.evaluate("eval('a')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test(expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testEvalWithTwoArguments(){ | public void testEvalWithTwoArguments() { | ||
Evaluator.getNumberResult("eval(0, 1)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testEvalWithThreeArguments(){ | public void testEvalWithThreeArguments() { | ||
Evaluator.getNumberResult("eval(1, 0, 0)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedExerciseVariableException.class) | @Test(expected = UndefinedExerciseVariableException.class) | ||
public void testEvalWithMissingExerciseVariable() { | public void testEvalWithMissingExerciseVariable() { | ||
Evaluator.getNumberResult("eval('[var=j]')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedFillInVariableException.class) | @Test(expected = UndefinedFillInVariableException.class) | ||
public void testEvalWithMissingInput() { | public void testEvalWithMissingInput() { | ||
Evaluator.getNumberResult("eval('[pos=42]')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 18. September 2017, 11:09 Uhr
Beschreibung
Die Funktion eval wertet einen Term aus. Der auszuwertende Ausdruck darf nur Zahlen und die Basisoperationen Addition, Subtraktion, Multiplikation, Division, sowie Modulo und Beträge beinhalten. Die Funktion erwartet einen String, den auszuwertenden Term, und gibt das entsprechende Ergebnis zurück.
Syntax
eval(Expression term)
Parameter
- term - der auszuwertende Term
Return Value
- Gibt eine Zahl zurück
Beispiele
Standardfälle
eval('(3+2*4)%5') --> returns 1
Benutzung von Variablen
eval('3+[pos=1]-2') eval('[var=a]/2+1')
JUnit Tests
[Anzeigen]
@Test
public void testEval1() {
assertEquals(2, Evaluator.getNumberResult("eval('1+1')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEval2() {
assertEquals(2.2, Evaluator.getNumberResult("eval('1.2+1')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEval3() {
assertEquals(5, Evaluator.getNumberResult("eval(abs(-5))", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEval4() {
assertEquals(12, Evaluator.getNumberResult("eval('2+5*2')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEval5() {
assertEquals(16, Evaluator.getNumberResult("eval('2*(5+3)')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEval6() {
assertEquals(1, Evaluator.getNumberResult("eval(5%2)", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEval7() {
assertNotEquals(1, Evaluator.getNumberResult("eval('2+2')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEvalWithInput1() {
assertEquals(0, Evaluator.getNumberResult("eval('[pos=1]')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEvalWithInput2() {
assertEquals(0, Evaluator.getNumberResult("eval('[pos=2]-1')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEvalWithVariables1() {
assertEquals(0, Evaluator.getNumberResult("eval('[var=a]')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEvalWithVariables2() {
assertEquals(0, Evaluator.getNumberResult("eval('[var=b]-1')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEvalWithExpressions1() {
assertEquals(6, Evaluator.getNumberResult("eval(eval('2+2') + eval('1+1'))", exerciseVariableMap,
fillInVariableMap), 0.0);
}
@Test
public void testEvalWithExpressions2() {
assertEquals(1, Evaluator.getNumberResult("eval(eval(1))", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test
public void testEvalWithFunction1() {
assertNotEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("5*x")),
Evaluator.evaluate("eval('2*x+3*x')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testEvalWithFunction2() {
assertNotEquals(1, Evaluator.getNumberResult("eval('2*x=2')", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testEvalWithWrongInputEmptyStringArgument() {
Evaluator.evaluate("eval('')", exerciseVariableMap, fillInVariableMap);
}
@Test
public void testEvalWithWrongInputCharacter() {
OMOBJ expected = new OMOBJ();
expected.setOMV(OMCreator.createOMV("a"));
assertEquals(expected, Evaluator.evaluate("eval('a')", exerciseVariableMap, fillInVariableMap));
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testEvalWithTwoArguments() {
Evaluator.getNumberResult("eval(0, 1)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testEvalWithThreeArguments() {
Evaluator.getNumberResult("eval(1, 0, 0)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testEvalWithMissingExerciseVariable() {
Evaluator.getNumberResult("eval('[var=j]')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testEvalWithMissingInput() {
Evaluator.getNumberResult("eval('[pos=42]')", exerciseVariableMap, fillInVariableMap);
fail();
}
Hinweise
- Die Funktion berücksichtigt Punktrechnung vor Strichrechnung.
- Bei der Multiplikation mit einer Klammer muss explizit ein Malzeichen verwendet werden.