Eval

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

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.