LastIndexOf: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Zeile 26: | Zeile 26: | ||
<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>oneString</OMSTR></OMOBJ>")); | |||
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>")); | |||
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>oneString</OMSTR></OMOBJ>")); | |||
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>")); | |||
} | |||
@Test | |||
public void testLastIndexOf1() { | |||
assertEquals(4, Evaluator.getNumberResult("lastIndexOf('abbbb','b',5)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testLastIndexOf2() { | |||
assertEquals(0, Evaluator.getNumberResult("lastIndexOf('cabade','c',10)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testLastIndexOf3() { | |||
assertEquals(-1, Evaluator.getNumberResult("lastIndexOf('cabade','test',10)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testLastIndexOf4() { | |||
assertEquals(6, Evaluator.getNumberResult("lastIndexOf('he llo','lo',7)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testLastIndexOf5() { | |||
assertEquals(0, Evaluator.getNumberResult("lastIndexOf('','',0)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testLastIndexOfWithInput1() { | |||
assertEquals(3, Evaluator.getNumberResult("lastIndexOf('[pos=7]','S',7)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testLastIndexOfWithInput2() { | ||
assertEquals(4, Evaluator.getNumberResult("lastIndexOf('abbbb','[pos=8]',5)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testLastIndexOfWithVariable1() { | ||
assertEquals(3, Evaluator.getNumberResult("lastIndexOf('[var=g]','S',7)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testLastIndexOfWithVariable2() { | ||
assertEquals(4, Evaluator.getNumberResult("lastIndexOf('abbbb','[var=h]',5)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test(expected= | @Test(expected=ParserException.class) | ||
public void testLastIndexOfWithWrongInputCharacter(){ | public void testLastIndexOfWithWrongInputCharacter() { | ||
Evaluator.getNumberResult("lastIndexOf(ab, ab, ab)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected= | @Test(expected=FunctionInvalidNumberOfArgumentsException.class) | ||
public void testLastIndexOfWithOneArgument(){ | public void testLastIndexOfWithOneArgument() { | ||
Evaluator.getNumberResult("lastIndexOf('abab')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test | @Test | ||
public void testLastIndexOfWithTwoArguments(){ | public void testLastIndexOfWithTwoArguments() { | ||
assertEquals(2, Evaluator.getNumberResult("lastIndexOf('abab', 'ab')", exerciseVariableMap, fillInVariableMap), 0.0); | |||
} | } | ||
@Test(expected=UndefinedExerciseVariableException.class) | @Test(expected=UndefinedExerciseVariableException.class) | ||
public void testLastIndexOfWithMissingExerciseVariable(){ | public void testLastIndexOfWithMissingExerciseVariable() { | ||
Evaluator.getNumberResult("lastIndexOf('[var=j]', '[var=j]', '[var=j]')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected=UndefinedFillInVariableException.class) | @Test(expected=UndefinedFillInVariableException.class) | ||
public void testLastIndexOfWithMissingInput(){ | public void testLastIndexOfWithMissingInput() { | ||
Evaluator.getNumberResult("lastIndexOf('[pos=42]', '[pos=42]', '[pos=42]')", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 19. September 2017, 07:29 Uhr
Beschreibung
Die Funktion lastIndexOf bestimmt die Position an der eine Zeichenkette oder ein einzelnes Zeichen das letzte Mal innerhalb einer anderen Zeichenkette vorkommt. Außerdem muss angegeben werden, ab welcher Stelle innerhalb der durchsuchten Zeichenkette gesucht werden soll. Die Funktion erwartet zwei Strings, die zu durchsuchende und die zu suchende Zeichenkette, und eine Ganzzahl, bis zu welcher Stelle die Suche durchgeführt werden soll. Es wird eine Ganzzahl zurückgegeben.
Syntax
lastIndexOf(String zeichenkette, String muster, int position)
Parameter
- zeichenkette - Zeichenkette in dem gesucht wird
- muster - Zeichenkette, die innnerhalb der ersten gesucht wird
- position - Endpositition der Suche
Return Value
- Gibt eine Ganzzahl zurück
Beispiele
Standardfälle
lastIndexOf('Hello World!','o',0) --> returns -1 lastIndexOf('Hello World!','o',6) --> returns 4
Benutzung von Variablen
lastIndexOf('[pos=1]','x',10) lastIndexOf('text','[var=a]',4) lastIndexOf('[var=b]','x',[pos=2])
JUnit Tests
[Anzeigen]
@BeforeClass
public static void beforeTest() {
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>oneString</OMSTR></OMOBJ>"));
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>oneString</OMSTR></OMOBJ>"));
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
}
@Test
public void testLastIndexOf1() {
assertEquals(4, Evaluator.getNumberResult("lastIndexOf('abbbb','b',5)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOf2() {
assertEquals(0, Evaluator.getNumberResult("lastIndexOf('cabade','c',10)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOf3() {
assertEquals(-1, Evaluator.getNumberResult("lastIndexOf('cabade','test',10)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOf4() {
assertEquals(6, Evaluator.getNumberResult("lastIndexOf('he llo','lo',7)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOf5() {
assertEquals(0, Evaluator.getNumberResult("lastIndexOf('','',0)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOfWithInput1() {
assertEquals(3, Evaluator.getNumberResult("lastIndexOf('[pos=7]','S',7)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOfWithInput2() {
assertEquals(4, Evaluator.getNumberResult("lastIndexOf('abbbb','[pos=8]',5)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOfWithVariable1() {
assertEquals(3, Evaluator.getNumberResult("lastIndexOf('[var=g]','S',7)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testLastIndexOfWithVariable2() {
assertEquals(4, Evaluator.getNumberResult("lastIndexOf('abbbb','[var=h]',5)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test(expected=ParserException.class)
public void testLastIndexOfWithWrongInputCharacter() {
Evaluator.getNumberResult("lastIndexOf(ab, ab, ab)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testLastIndexOfWithOneArgument() {
Evaluator.getNumberResult("lastIndexOf('abab')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test
public void testLastIndexOfWithTwoArguments() {
assertEquals(2, Evaluator.getNumberResult("lastIndexOf('abab', 'ab')", exerciseVariableMap, fillInVariableMap), 0.0);
}
@Test(expected=UndefinedExerciseVariableException.class)
public void testLastIndexOfWithMissingExerciseVariable() {
Evaluator.getNumberResult("lastIndexOf('[var=j]', '[var=j]', '[var=j]')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected=UndefinedFillInVariableException.class)
public void testLastIndexOfWithMissingInput() {
Evaluator.getNumberResult("lastIndexOf('[pos=42]', '[pos=42]', '[pos=42]')", exerciseVariableMap, fillInVariableMap);
fail();
}
Hinweise
- Die Nummerierung innerhalb der Zeichenkette beginnt mit 0.
- Sollte die gesuchte Zeichenkette nicht gefunden werden, gibt die Funktion -1 zurück.