EvalEq: Unterschied zwischen den Versionen
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"> | ||
@ | @Test | ||
public void | 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 | @Test | ||
public void | 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 | @Test | ||
public void | 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 | @Test | ||
public void | 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 | @Test | ||
public void testEvalEqWithONECharacter() { | public void testEvalEqWithONECharacter() { | ||
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMI(0)), | |||
Evaluator.evaluate("evalEq(a, a)", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test(expected= | @Test(expected = FunctionInvalidArgumentException.class) | ||
public void testEvalEqWithEmptyArguments() { | public void testEvalEqWithEmptyArguments() { | ||
Evaluator.evaluate("evalEq('' , '')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected=UndefinedExerciseVariableException.class) | @Test(expected = UndefinedExerciseVariableException.class) | ||
public void testEvalEqWithoutExerciseVariable() { | public void testEvalEqWithoutExerciseVariable() { | ||
Evaluator.evaluate("evalEq([var=j], [var=j])", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected=UndefinedFillInVariableException.class) | @Test(expected = UndefinedFillInVariableException.class) | ||
public void testEvalEqWithoutInput() { | public void testEvalEqWithoutInput() { | ||
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