CompareTo: 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"> | ||
@ | @BeforeClass | ||
public void beforeTest() { | public static void beforeTest() { | ||
compareToFillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>")); | |||
compareToExerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>")); | |||
} | } | ||
@Test | @Test | ||
public void | public void testCompareTo1() { | ||
assertEquals(0, Evaluator.getNumberResult("compareTo('Hallo','Hallo')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testCompareTo2() { | |||
assertEquals(0, Evaluator.getNumberResult("compareTo('HAlLo','HAlLo')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap), 0.0); | |||
} | |||
@Test | |||
public void testCompareTo3() { | |||
assertTrue(0 < Evaluator.getNumberResult("compareTo('jack','JACK')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap)); | |||
} | |||
@Test | |||
public void testCompareTo4() { | |||
assertTrue(0 < Evaluator.getNumberResult("compareTo('paluno','JACK')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap)); | |||
} | |||
@Test | |||
public void testCompareTo5() { | |||
assertTrue(0 > Evaluator.getNumberResult("compareTo('JACK','paluno')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap)); | |||
} | |||
@Test | |||
public void testCompareTo6() { | |||
assertNotEquals(0, Evaluator.getNumberResult("compareTo('2*x+2*y','2*(x+y)')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void testCompareToWithInput() { | public void testCompareToWithInput() { | ||
assertEquals(0, Evaluator.getNumberResult("compareTo('[pos=1]','test')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap), 0.0); | |||
} | } | ||
@Test | @Test | ||
public void testCompareToWithVariables() { | public void testCompareToWithVariables() { | ||
assertEquals(0, Evaluator.getNumberResult("compareTo('[var=a]','test')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap), 0.0); | |||
} | } | ||
@Test (expected= | @Test(expected = ParserException.class) | ||
public void testCompareToWithWrongInputCharacter() { | public void testCompareToWithWrongInputCharacter() { | ||
Evaluator.evaluate("compareTo(ab,'test')", compareToExerciseVariableMap, compareToFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testCompareToWithOneArgument() { | public void testCompareToWithOneArgument() { | ||
Evaluator.evaluate("compareTo('Hallo')", compareToExerciseVariableMap, compareToFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test | @Test | ||
public void testCompareToWithOneRationalAndOneTextArgument() { | public void testCompareToWithOneRationalAndOneTextArgument() { | ||
assertNotEquals(Evaluator.getNumberResult("compareTo(12, 'rational')", compareToExerciseVariableMap, compareToFillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void testCompareToWithTwoRationalArguments() { | public void testCompareToWithTwoRationalArguments() { | ||
assertNotEquals(0, Evaluator.getNumberResult("compareTo(1, 2)", compareToExerciseVariableMap, compareToFillInVariableMap),0.0); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testCompareToWithThreeArguments() { | public void testCompareToWithThreeArguments() { | ||
Evaluator.evaluate("compareTo('hallo', 'hello', 'hillo')", compareToExerciseVariableMap, | |||
compareToFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedExerciseVariableException.class) | @Test(expected = UndefinedExerciseVariableException.class) | ||
public void testCompareToWithMissingExcerciseVariable() { | public void testCompareToWithMissingExcerciseVariable() { | ||
Evaluator.evaluate("compareTo('[var=j]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedFillInVariableException.class) | @Test(expected = UndefinedFillInVariableException.class) | ||
public void testCompareToWithMissingInput() { | public void testCompareToWithMissingInput() { | ||
Evaluator.evaluate("compareTo('[pos=42]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Hinweise === | === Hinweise === | ||
* Die Funktion unterscheidet zwischen Groß- und Kleinschreibung. | * Die Funktion unterscheidet zwischen Groß- und Kleinschreibung. |
Version vom 18. September 2017, 10:37 Uhr
Beschreibung
Die Funktion compareTo bestimmt wie sehr zwei Zeichenketten lexikografisch voneinander abweichen. Die Funktion erwartet zwei Strings und gibt eine Zahl zurück.
Syntax
compareTo(String zeichenkette1, String zeichenkette2)
Parameter
- zeichenkette1 - erste zu vergleichende Zeichenkette
- zeichenkette2 - zweite zu vergleichende Zeichenkette
Return Value
- Gibt eine Zahl zurück
Beispiele
compareTo('hallo','hallo') --> returns 0 compareTo('hallo','JACK') --> returns 30 compareTo('JACK','jack') --> returns -32
JUnit Tests
[Anzeigen]
@BeforeClass
public static void beforeTest() {
compareToFillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
compareToExerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
}
@Test
public void testCompareTo1() {
assertEquals(0, Evaluator.getNumberResult("compareTo('Hallo','Hallo')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
@Test
public void testCompareTo2() {
assertEquals(0, Evaluator.getNumberResult("compareTo('HAlLo','HAlLo')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
@Test
public void testCompareTo3() {
assertTrue(0 < Evaluator.getNumberResult("compareTo('jack','JACK')", compareToExerciseVariableMap,
compareToFillInVariableMap));
}
@Test
public void testCompareTo4() {
assertTrue(0 < Evaluator.getNumberResult("compareTo('paluno','JACK')", compareToExerciseVariableMap,
compareToFillInVariableMap));
}
@Test
public void testCompareTo5() {
assertTrue(0 > Evaluator.getNumberResult("compareTo('JACK','paluno')", compareToExerciseVariableMap,
compareToFillInVariableMap));
}
@Test
public void testCompareTo6() {
assertNotEquals(0, Evaluator.getNumberResult("compareTo('2*x+2*y','2*(x+y)')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
@Test
public void testCompareToWithInput() {
assertEquals(0, Evaluator.getNumberResult("compareTo('[pos=1]','test')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
@Test
public void testCompareToWithVariables() {
assertEquals(0, Evaluator.getNumberResult("compareTo('[var=a]','test')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
@Test(expected = ParserException.class)
public void testCompareToWithWrongInputCharacter() {
Evaluator.evaluate("compareTo(ab,'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCompareToWithOneArgument() {
Evaluator.evaluate("compareTo('Hallo')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
@Test
public void testCompareToWithOneRationalAndOneTextArgument() {
assertNotEquals(Evaluator.getNumberResult("compareTo(12, 'rational')", compareToExerciseVariableMap, compareToFillInVariableMap),0.0);
}
@Test
public void testCompareToWithTwoRationalArguments() {
assertNotEquals(0, Evaluator.getNumberResult("compareTo(1, 2)", compareToExerciseVariableMap, compareToFillInVariableMap),0.0);
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCompareToWithThreeArguments() {
Evaluator.evaluate("compareTo('hallo', 'hello', 'hillo')", compareToExerciseVariableMap,
compareToFillInVariableMap);
fail();
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testCompareToWithMissingExcerciseVariable() {
Evaluator.evaluate("compareTo('[var=j]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testCompareToWithMissingInput() {
Evaluator.evaluate("compareTo('[pos=42]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
Hinweise
- Die Funktion unterscheidet zwischen Groß- und Kleinschreibung.
- Steht der erste String lexikografisch vor dem zweiten String, ist die Rückgabe negativ. Umgekehrt ist die Rückgabe entsprechend positiv.