EvalEq

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

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(Ausdruck term1, Ausdruck 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','3*x')    --> returns 'x^2-3*x+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