Ifthenelse: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 27: | Zeile 27: | ||
<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"> | ||
@ | @BeforeClass | ||
public void beforeTest() { | public static void beforeTest() { | ||
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>")); | |||
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>")); | |||
} | |||
@Test | |||
public void testIfthenelse1() { | |||
assertEquals(ExpressionParser.parse("'else'", null, null), | |||
Evaluator.evaluate("ifthenelse(0, 'then', 'else')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testIfthenelse2() { | |||
assertEquals(ExpressionParser.parse("'then'", null, null), | |||
Evaluator.evaluate("ifthenelse(1, 'then', 'else')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testIfthenelse3() { | |||
assertEquals(ExpressionParser.parse("'five'", null, null), | |||
Evaluator.evaluate("ifthenelse(5.7, 'five', 'notFive')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testIfthenelseWithInput1() { | |||
assertEquals(ExpressionParser.parse("'else'", null, null), | |||
Evaluator.evaluate("ifthenelse([pos=1], 'then', 'else')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testIfthenelseWithInput2() { | |||
assertEquals(ExpressionParser.parse("'test'", null, null), | |||
Evaluator.evaluate("ifthenelse(1, '[pos=7]', 'else')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testIfthenelseWithInput3() { | |||
assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator | |||
.evaluate("ifthenelse([pos=2], '[pos=7]', 'notFive')", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | |||
public void testIfthenelseWithVariables1() { | |||
assertEquals(ExpressionParser.parse("'else'", null, null), | |||
Evaluator.evaluate("ifthenelse([var=a], 'then', 'else')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIfthenelseWithVariables2() { | ||
assertEquals(ExpressionParser.parse("'test'", null, null), | |||
Evaluator.evaluate("ifthenelse(1, '[var=g]', 'else')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIfthenelseWithVariables3() { | ||
assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator | |||
.evaluate("ifthenelse([var=b], '[var=g]', 'notFive')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIfthenelseWithExpressions1() { | ||
assertEquals(ExpressionParser.parse("a", null, null), | |||
Evaluator.evaluate("ifthenelse('5*2 +5 == 5*3', 'a', 'b')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIfthenelseWithExpressions2() { | ||
assertEquals(ExpressionParser.parse("b", null, null), Evaluator | |||
.evaluate("ifthenelse(ifthenelse(0, '1', '0'), 'a', 'b')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test(expected= | @Test(expected = ParserException.class) | ||
public void testIfthenelseWithWrongInputCharacter(){ | public void testIfthenelseWithWrongInputCharacter() { | ||
Evaluator.evaluate("ifthenelse(ab, 'a', 'b')", exerciseVariableMap, fillInVariableMap); | |||
} | } | ||
@Test | @Test | ||
public void testIfthenelseWithTwoRationalAndOneTextArgument(){ | public void testIfthenelseWithTwoRationalAndOneTextArgument() { | ||
assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 'b')", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void testIfthenelseWithThreeRationalArguments(){ | public void testIfthenelseWithThreeRationalArguments() { | ||
assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 7)", exerciseVariableMap, fillInVariableMap)); | |||
} | } | ||
@Test(expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testIfthenelseWithOneArgument(){ | public void testIfthenelseWithOneArgument() { | ||
Evaluator.evaluate("ifthenelse(1)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testIfthenelseWithTwoArguments(){ | public void testIfthenelseWithTwoArguments() { | ||
Evaluator.evaluate("ifthenelse(0, 'this')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected=UndefinedExerciseVariableException.class) | @Test(expected = UndefinedExerciseVariableException.class) | ||
public void testIfthenelseWithMissingExerciseVariable(){ | public void testIfthenelseWithMissingExerciseVariable() { | ||
Evaluator.evaluate("ifthenelse('[var=j]', 'a', 'b')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected=UndefinedFillInVariableException.class) | @Test(expected = UndefinedFillInVariableException.class) | ||
public void testIfthenelseWithMissingInput(){ | public void testIfthenelseWithMissingInput() { | ||
Evaluator.evaluate("ifthenelse('[pos=42]', 'a', 'b')", exerciseVariableMap, fillInVariableMap); | |||
} | fail(); | ||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Hinweise === | === Hinweise === | ||
* Zahlen ungleich 0 werden zu ''wahr'' ausgewertet. | * Zahlen ungleich 0 werden zu ''wahr'' ausgewertet. | ||
[[Kategorie:Evaluatorfunktion]] | [[Kategorie:Evaluatorfunktion]] |
Version vom 18. September 2017, 12:01 Uhr
Beschreibung
Die Funktion ifthenelse wählt zwischen zwei Zeichenketten, abhängig vom logischen Wahrheitsgehalt einer gegebenen Aussage. Als Aussage sind rationale Zahlen, sowie mathematische Terme und Un-/Gleichungen zulässig. Zahlen ungleich 0 werden als wahr, 0 wird zu falsch ausgewertet.
Die Funktion erwartet eine Aussage, die sich zu wahr oder falsch auswerten lässt, einen String, der zurückgegeben wird, falls die Aussage wahr ist, und einen String, der zurückgegeben wird, falls die Aussage falsch ist. Es wird ein String zurückgegeben.
Syntax
ifthenelse(String aussage, String wahrAussage, String falschAussage)
Parameter
- aussage - logisch auzuwertende Aussage
- wahrAussage - String, der im Falle einer wahren Aussage zurückgegeben wird
- falschAussage - String, der im Falle einer falschen Aussage zurückgegeben wird
Return Value
- Gibt einen String zurück
Beispiele
Standardfälle
ifthenelse('1','wahr','falsch') --> returns wahr ifthenelse('5*2+5==5*3','a','b') --> returns a
Benutzung von Variablen
ifthenelse('[pos=1]','then','else') ifthenelse('15','[var=a]','text')
JUnit Tests
[Anzeigen]
@BeforeClass
public static void beforeTest() {
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
}
@Test
public void testIfthenelse1() {
assertEquals(ExpressionParser.parse("'else'", null, null),
Evaluator.evaluate("ifthenelse(0, 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelse2() {
assertEquals(ExpressionParser.parse("'then'", null, null),
Evaluator.evaluate("ifthenelse(1, 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelse3() {
assertEquals(ExpressionParser.parse("'five'", null, null),
Evaluator.evaluate("ifthenelse(5.7, 'five', 'notFive')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithInput1() {
assertEquals(ExpressionParser.parse("'else'", null, null),
Evaluator.evaluate("ifthenelse([pos=1], 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithInput2() {
assertEquals(ExpressionParser.parse("'test'", null, null),
Evaluator.evaluate("ifthenelse(1, '[pos=7]', 'else')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithInput3() {
assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator
.evaluate("ifthenelse([pos=2], '[pos=7]', 'notFive')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithVariables1() {
assertEquals(ExpressionParser.parse("'else'", null, null),
Evaluator.evaluate("ifthenelse([var=a], 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithVariables2() {
assertEquals(ExpressionParser.parse("'test'", null, null),
Evaluator.evaluate("ifthenelse(1, '[var=g]', 'else')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithVariables3() {
assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator
.evaluate("ifthenelse([var=b], '[var=g]', 'notFive')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithExpressions1() {
assertEquals(ExpressionParser.parse("a", null, null),
Evaluator.evaluate("ifthenelse('5*2 +5 == 5*3', 'a', 'b')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithExpressions2() {
assertEquals(ExpressionParser.parse("b", null, null), Evaluator
.evaluate("ifthenelse(ifthenelse(0, '1', '0'), 'a', 'b')", exerciseVariableMap, fillInVariableMap));
}
@Test(expected = ParserException.class)
public void testIfthenelseWithWrongInputCharacter() {
Evaluator.evaluate("ifthenelse(ab, 'a', 'b')", exerciseVariableMap, fillInVariableMap);
}
@Test
public void testIfthenelseWithTwoRationalAndOneTextArgument() {
assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 'b')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testIfthenelseWithThreeRationalArguments() {
assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 7)", exerciseVariableMap, fillInVariableMap));
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testIfthenelseWithOneArgument() {
Evaluator.evaluate("ifthenelse(1)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testIfthenelseWithTwoArguments() {
Evaluator.evaluate("ifthenelse(0, 'this')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testIfthenelseWithMissingExerciseVariable() {
Evaluator.evaluate("ifthenelse('[var=j]', 'a', 'b')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testIfthenelseWithMissingInput() {
Evaluator.evaluate("ifthenelse('[pos=42]', 'a', 'b')", exerciseVariableMap, fillInVariableMap);
fail();
}
Hinweise
- Zahlen ungleich 0 werden zu wahr ausgewertet.