Eval: Unterschied zwischen den Versionen

Aus JACK Wiki
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">
@Before
@Test
public void beforeTest() {
public void testEval1() {
  placeFillInVariable(1, OMParser.parse("0"));
assertEquals(2, Evaluator.getNumberResult("eval('1+1')", exerciseVariableMap, fillInVariableMap), 0.0);
  placeFillInVariable(2, OMParser.parse("1"));
  placeExerciseVariable("a", OMParser.parse("0"));
  placeExerciseVariable("b", OMParser.parse("1"));
}
}
 
@Test  
@Test
public void testEval() {
public void testEval2() {
  assertTrue(2  == getEvaluator().getNumberResult("eval('1+1')"));
assertEquals(2.2, Evaluator.getNumberResult("eval('1.2+1')", exerciseVariableMap, fillInVariableMap), 0.0);
  assertTrue(2.2 == getEvaluator().getNumberResult("eval('1.2+1')"));
  assertTrue(5  == getEvaluator().getNumberResult("eval(|-5|)"));
  assertTrue(12  == getEvaluator().getNumberResult("eval('2+5*2')"));
  assertTrue(16  == getEvaluator().getNumberResult("eval('2*(5+3)')"));
  assertTrue(1  == getEvaluator().getNumberResult("eval(5%2)"));
  assertFalse(1  == getEvaluator().getNumberResult("eval('2+2')"));
}
}
 
@Test  
@Test
public void testEvalWithInput() {
public void testEval3() {
  assertTrue(0 == getEvaluator().getNumberResult("eval('[pos=1]')"));
assertEquals(5, Evaluator.getNumberResult("eval(abs(-5))", exerciseVariableMap, fillInVariableMap), 0.0);
  assertTrue(0 == getEvaluator().getNumberResult("eval('[pos=2]-1')"));
}
}
 
@Test  
@Test
public void testEvalWithVariables() {
public void testEval4() {
  assertTrue(0 == getEvaluator().getNumberResult("eval('[var=a]')"));
assertEquals(12, Evaluator.getNumberResult("eval('2+5*2')", exerciseVariableMap, fillInVariableMap), 0.0);
  assertTrue(0 == getEvaluator().getNumberResult("eval('[var=b]-1')"));
}
}
 
@Test  
@Test
public void testEvalWithExpressions() {
public void testEval5() {
  assertTrue(6 == getEvaluator().getNumberResult("eval(eval('2+2') + eval('1+1'))"));
assertEquals(16, Evaluator.getNumberResult("eval('2*(5+3)')", exerciseVariableMap, fillInVariableMap), 0.0);
  assertTrue(1 == getEvaluator().getNumberResult("eval(eval(1))"));
}
}
 
@Test  
@Test
public void testEvalWithFunction() {
public void testEval6() {
  assertFalse("5x".equals(getEvaluator().getResultAsEvaluatorString("eval('2x+3x')")));
assertEquals(1, Evaluator.getNumberResult("eval(5%2)", exerciseVariableMap, fillInVariableMap), 0.0);
  assertFalse(1 == getEvaluator().getNumberResult("eval('2x=2')"));
}
}
 
@Test (expected=InvalidEvaluatorFunctionArgumentException.class)
@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() {
  getEvaluator().getNumberResult("eval('')");
Evaluator.evaluate("eval('')", exerciseVariableMap, fillInVariableMap);
}
}
 
@Test (expected=InvalidEvaluatorFunctionArgumentException.class)
@Test
public void testEvalWithWrongInputCharacter() {
public void testEvalWithWrongInputCharacter() {
  getEvaluator().getNumberResult("eval('a')");
OMOBJ expected = new OMOBJ();
expected.setOMV(OMCreator.createOMV("a"));
assertEquals(expected, Evaluator.evaluate("eval('a')", exerciseVariableMap, fillInVariableMap));
}
}
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testEvalWithTwoArguments(){
public void testEvalWithTwoArguments() {
  getEvaluator().getNumberResult("eval(0, 1)");
Evaluator.getNumberResult("eval(0, 1)", exerciseVariableMap, fillInVariableMap);
fail();
}
}
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testEvalWithThreeArguments(){
public void testEvalWithThreeArguments() {
  getEvaluator().getNumberResult("eval(1, 0, 0)");
Evaluator.getNumberResult("eval(1, 0, 0)", exerciseVariableMap, fillInVariableMap);
fail();
}
}
 
@Test (expected=UndefinedExerciseVariableException.class)
@Test(expected = UndefinedExerciseVariableException.class)
public void testEvalWithMissingExerciseVariable() {
public void testEvalWithMissingExerciseVariable() {
  getEvaluator().getNumberResult("eval('[var=j]')");
Evaluator.getNumberResult("eval('[var=j]')", exerciseVariableMap, fillInVariableMap);
fail();
}
}
 
@Test (expected=UndefinedFillInVariableException.class)
@Test(expected = UndefinedFillInVariableException.class)
public void testEvalWithMissingInput() {
public void testEvalWithMissingInput() {
  getEvaluator().getNumberResult("eval('[pos=42]')");
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.