EvalEq: Unterschied zwischen den Versionen

Aus JACK Wiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Zeile 20: Zeile 20:
<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 testEvalEq1() {
  placeFillInVariable(1, OMParser.parse("0"));
OMOBJ expected = OMConverter.toObject(
  placeFillInVariable(2, OMParser.parse("1"));
"<OMOBJ><OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
  placeExerciseVariable("a", OMParser.parse("0"));
+ "<OMA>"
  placeExerciseVariable("b", OMParser.parse("1"));
+ "<OMS name=\"power\" cd=\"arith1\"/>"
+ "<OMV name=\"x\"/>"
+ "<OMI>2</OMI>"
+ "</OMA>"
+ "<OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"times\" cd=\"arith1\"/>"
+ "<OMI>-1</OMI>"
+ "<OMV name=\"x\"/>"
+ "</OMA>"
+ "<OMI>-1</OMI>"
+ "</OMA>"
+ "</OMA></OMOBJ>"); //returns x^2+(-1*x)+-1
assertEquals(expected,
Evaluator.evaluate("evalEq('x^2', 'x+1')", exerciseVariableMap, fillInVariableMap));
}
 
@Test
public void testEvalEq2() {
OMOBJ expected = OMConverter.toObject(
"<OMOBJ>"
+ "<OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"power\" cd=\"arith1\"/>"
+ "<OMV name=\"x\"/>"
+ "<OMI>2</OMI>"
+ "</OMA>"
+ "<OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"times\" cd=\"arith1\"/>"
+ "<OMI>-1</OMI>"
+ "<OMV name=\"y\"/>"
+ "</OMA>"
+ "<OMI>-1</OMI>"
+ "</OMA>"
+ "</OMA></OMOBJ>"); //returns  x^2  +  -1*y + -1
assertEquals(expected,
Evaluator.evaluate("evalEq('x^2', 'y+1')", exerciseVariableMap, fillInVariableMap));
}
}
@Test
public void testEvalEq3() {
assertEquals(OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"),
Evaluator.evaluate("evalEq('5', '5')", exerciseVariableMap, fillInVariableMap));
}
@Test
@Test
public void testEvalEq() {
public void testEvalEqWithInput1() {
  assertTrue("'-1-x+x^2'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('x^2', 'x+1')")));
OMOBJ expected = OMConverter.toObject(
  assertTrue("'x^2-(y+1)'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('x^2', 'y+1')")));
"<OMOBJ><OMA>"
  assertTrue("'5-(5)'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('5', '5')")));
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"times\" cd=\"arith1\"/>"
+ "<OMI>-1</OMI>"
+ "<OMA>"
+ "<OMS name=\"power\" cd=\"arith1\"/>"
+ "<OMV name=\"x\"/>"
+ "<OMI>2</OMI>"
+ "</OMA>"
+ "</OMA>"
+ "<OMI>-4</OMI>"
+ "</OMA></OMOBJ>"); //returns  -1*x^2 + -4 
assertEquals(expected,
Evaluator.evaluate("evalEq('[pos=1]','x^2+4')", exerciseVariableMap, fillInVariableMap));
}
}
 
@Test
public void testEvalEqWithInput2() {
OMOBJ expected = OMConverter.toObject(
"<OMOBJ><OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"times\" cd=\"arith1\"/>"
+ "<OMI>3</OMI>"
+ "<OMV name=\"x\"/>"
+ "</OMA>"
+ "<OMI>-3</OMI>"
+ "</OMA></OMOBJ>"); //returns 3*x  + -3
assertEquals(expected,
Evaluator.evaluate("evalEq('3*x-2','[pos=2]')", exerciseVariableMap, fillInVariableMap));
}
 
@Test
@Test
public void testEvalEqWithInput() {
public void testEvalEqWithVariables1() {
  assertTrue("'-4-x^2'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('[pos=1]','x^2+4')")));
OMOBJ expected = OMConverter.toObject(
  assertTrue("'-3+3*x'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('3x-2','[pos=2]')")));
"<OMOBJ><OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"times\" cd=\"arith1\"/>"
+ "<OMI>-1</OMI>"
+ "<OMA>"
+ "<OMS name=\"power\" cd=\"arith1\"/>"
+ "<OMV name=\"x\"/>"
+ "<OMI>2</OMI>"
+ "</OMA>"
+ "</OMA>"
+ "<OMI>-4</OMI>"
+ "</OMA></OMOBJ>"); //returns  -1*x^2 + -4 
assertEquals(expected,
Evaluator.evaluate("evalEq('[var=a]','x^2+4')", exerciseVariableMap, fillInVariableMap));
}
}
 
@Test
@Test
public void testEvalEqWithVariables() {
public void testEvalEqWithVariables2() {
  assertTrue("'-4-x^2'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('[var=a]','x^2+4')")));
OMOBJ expected = OMConverter.toObject(
  assertTrue("'-3+3*x'".equals(getEvaluator().getResultAsEvaluatorString("evalEq('3x-2','[var=b]')")));
"<OMOBJ><OMA>"
+ "<OMS name=\"plus\" cd=\"arith1\"/>"
+ "<OMA>"
+ "<OMS name=\"times\" cd=\"arith1\"/>"
+ "<OMI>3</OMI>"
+ "<OMV name=\"x\"/>"
+ "</OMA>"
+ "<OMI>-3</OMI>"
+ "</OMA></OMOBJ>"); //returns 3*x + -3
assertEquals(expected,
Evaluator.evaluate("evalEq('3*x-2','[var=b]')", exerciseVariableMap, fillInVariableMap));
}
}
 
@Test
@Test
public void testEvalEqWithONECharacter() {
public void testEvalEqWithONECharacter() {
  assertTrue("'a-(a)'".equals(getEvaluator().getResultAsEvaluatorString("evalEq(a, a)")));
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMI(0)),
Evaluator.evaluate("evalEq(a, a)", exerciseVariableMap, fillInVariableMap));
}
}
 
@Test(expected=InvalidCASFunctionArgumentException.class)
@Test(expected = FunctionInvalidArgumentException.class)
public void testEvalEqWithEmptyArguments() {
public void testEvalEqWithEmptyArguments() {
  getEvaluator().getResultAsEvaluatorString("evalEq('' , '')");
Evaluator.evaluate("evalEq('' , '')", exerciseVariableMap, fillInVariableMap);
fail();
}
}
 
@Test(expected=UndefinedExerciseVariableException.class)
@Test(expected = UndefinedExerciseVariableException.class)
public void testEvalEqWithoutExerciseVariable() {
public void testEvalEqWithoutExerciseVariable() {
  getEvaluator().getResultAsEvaluatorString("evalEq([var=j], [var=j])");
Evaluator.evaluate("evalEq([var=j], [var=j])", exerciseVariableMap, fillInVariableMap);
fail();
}
}
 
@Test(expected=UndefinedFillInVariableException.class)
@Test(expected = UndefinedFillInVariableException.class)
public void testEvalEqWithoutInput() {
public void testEvalEqWithoutInput() {
  getEvaluator().getResultAsEvaluatorString("evalEq([pos=42], [pos=42])");
Evaluator.evaluate("evalEq([pos=42], [pos=42])", exerciseVariableMap, fillInVariableMap);
fail();
}
}
</syntaxhighlight>
</syntaxhighlight>
===Hinweise===
===Hinweise===
* Um den Ausdruck anschließend sinnvoll vergleichen zu können kann man z.B. die Funktion [[equalsExpr]] benutzen. Ein Beispiel:
* Um den Ausdruck anschließend sinnvoll vergleichen zu können kann man z.B. die Funktion [[equalsExpr]] benutzen. Ein Beispiel:

Version vom 18. September 2017, 11:17 Uhr

Beschreibung

Die Funktion evalEq evaluiert zwei Ausdrücke miteinander. Das können z.B. zwei Fill-In Kästchen, die die linke und rechte Seite einer Gleichung darstellen, sein. evalEq erwartet zwei Strings und gibt einen String zurück.

Syntax

evalEq(Expression term1, Expression term2)

Parameter

  • term1 - der Ausdruck, der z.B. auf der linken Seite einer Gleichung steht
  • term2 - der Ausdruck, der z.B. auf der rechten Seite einer Gleichung steht

Return Value

  • Gibt einen String zurück, evaluiert aus string1-string2.

Beispiele

evalEq('x^2 + 4','3x')    --> returns 'x^2-3x+4'
evalEq('5','5')    --> returns '0'

JUnit Tests

[Anzeigen]

@Test
public void testEvalEq1() {
	OMOBJ expected = OMConverter.toObject(
		"<OMOBJ><OMA>"
			+ "<OMS name=\"plus\" cd=\"arith1\"/>"
			+ "<OMA>"
				+ "<OMS name=\"power\" cd=\"arith1\"/>"
				+ "<OMV name=\"x\"/>"
				+ "<OMI>2</OMI>"
			+ "</OMA>"
			+ "<OMA>"
				+ "<OMS name=\"plus\" cd=\"arith1\"/>"
				+ "<OMA>"
					+ "<OMS name=\"times\" cd=\"arith1\"/>"
					+ "<OMI>-1</OMI>"
					+ "<OMV name=\"x\"/>"
				+ "</OMA>"
				+ "<OMI>-1</OMI>"
			+ "</OMA>"
		+ "</OMA></OMOBJ>"); //returns x^2+(-1*x)+-1
	assertEquals(expected,
			Evaluator.evaluate("evalEq('x^2', 'x+1')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testEvalEq2() {
	OMOBJ expected = OMConverter.toObject(
		"<OMOBJ>"
		+ "<OMA>"
			+ "<OMS name=\"plus\" cd=\"arith1\"/>"
			+ "<OMA>"
				+ "<OMS name=\"power\" cd=\"arith1\"/>"
				+ "<OMV name=\"x\"/>"
				+ "<OMI>2</OMI>"
			+ "</OMA>"
			+ "<OMA>"
				+ "<OMS name=\"plus\" cd=\"arith1\"/>"
				+ "<OMA>"
					+ "<OMS name=\"times\" cd=\"arith1\"/>"
					+ "<OMI>-1</OMI>"
					+ "<OMV name=\"y\"/>"
				+ "</OMA>"
				+ "<OMI>-1</OMI>"
			+ "</OMA>"
		+ "</OMA></OMOBJ>"); //returns   x^2  +  -1*y + -1
	assertEquals(expected,
			Evaluator.evaluate("evalEq('x^2', 'y+1')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testEvalEq3() {
	assertEquals(OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"),
			Evaluator.evaluate("evalEq('5', '5')", exerciseVariableMap, fillInVariableMap));
	
}

@Test
public void testEvalEqWithInput1() {
	OMOBJ expected = OMConverter.toObject(
		"<OMOBJ><OMA>"
			+ "<OMS name=\"plus\" cd=\"arith1\"/>"
			+ "<OMA>"
				+ "<OMS name=\"times\" cd=\"arith1\"/>"
				+ "<OMI>-1</OMI>"
				+ "<OMA>"
					+ "<OMS name=\"power\" cd=\"arith1\"/>"
					+ "<OMV name=\"x\"/>"
					+ "<OMI>2</OMI>"
				+ "</OMA>"
			+ "</OMA>"
			+ "<OMI>-4</OMI>"
		+ "</OMA></OMOBJ>"); //returns  -1*x^2  + -4  
	assertEquals(expected,
			Evaluator.evaluate("evalEq('[pos=1]','x^2+4')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testEvalEqWithInput2() {
	OMOBJ expected = OMConverter.toObject(
			"<OMOBJ><OMA>"
					+ "<OMS name=\"plus\" cd=\"arith1\"/>"
					+ "<OMA>"
						+ "<OMS name=\"times\" cd=\"arith1\"/>"
						+ "<OMI>3</OMI>"
						+ "<OMV name=\"x\"/>"
					+ "</OMA>"
					+ "<OMI>-3</OMI>"
			+ "</OMA></OMOBJ>"); //returns 3*x  + -3 
	assertEquals(expected,
			Evaluator.evaluate("evalEq('3*x-2','[pos=2]')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testEvalEqWithVariables1() {
	OMOBJ expected = OMConverter.toObject(
			"<OMOBJ><OMA>"
				+ "<OMS name=\"plus\" cd=\"arith1\"/>"
				+ "<OMA>"
					+ "<OMS name=\"times\" cd=\"arith1\"/>"
					+ "<OMI>-1</OMI>"
					+ "<OMA>"
						+ "<OMS name=\"power\" cd=\"arith1\"/>"
						+ "<OMV name=\"x\"/>"
						+ "<OMI>2</OMI>"
					+ "</OMA>"
				+ "</OMA>"
				+ "<OMI>-4</OMI>"
			+ "</OMA></OMOBJ>"); //returns  -1*x^2  + -4  
	assertEquals(expected,
			Evaluator.evaluate("evalEq('[var=a]','x^2+4')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testEvalEqWithVariables2() {
	OMOBJ expected = OMConverter.toObject(
			"<OMOBJ><OMA>"
					+ "<OMS name=\"plus\" cd=\"arith1\"/>"
					+ "<OMA>"
						+ "<OMS name=\"times\" cd=\"arith1\"/>"
						+ "<OMI>3</OMI>"
						+ "<OMV name=\"x\"/>"
					+ "</OMA>"
					+ "<OMI>-3</OMI>"
			+ "</OMA></OMOBJ>"); //returns 3*x  + -3 
	assertEquals(expected,
			Evaluator.evaluate("evalEq('3*x-2','[var=b]')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testEvalEqWithONECharacter() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMI(0)),
			Evaluator.evaluate("evalEq(a, a)", exerciseVariableMap, fillInVariableMap));
}

@Test(expected = FunctionInvalidArgumentException.class)
public void testEvalEqWithEmptyArguments() {
	Evaluator.evaluate("evalEq('' , '')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = UndefinedExerciseVariableException.class)
public void testEvalEqWithoutExerciseVariable() {
	Evaluator.evaluate("evalEq([var=j], [var=j])", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = UndefinedFillInVariableException.class)
public void testEvalEqWithoutInput() {
	Evaluator.evaluate("evalEq([pos=42], [pos=42])", exerciseVariableMap, fillInVariableMap);
	fail();
}

Hinweise

  • Um den Ausdruck anschließend sinnvoll vergleichen zu können kann man z.B. die Funktion equalsExpr benutzen. Ein Beispiel:
equalsExpr(evalEq('x^2','x+1'),'x^2-x-1')    --> returns true