IndexOf: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
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>str</OMSTR></OMOBJ>")); | |||
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>anotherstring</OMSTR></OMOBJ>")); | |||
fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>")); | |||
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>str</OMSTR></OMOBJ>")); | |||
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>anotherstring</OMSTR></OMOBJ>")); | |||
exerciseVariableMap.put("i", OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>")); | |||
} | |||
@Test | |||
public void testIndexOf1() { | |||
assertEquals(3, Evaluator.getNumberResult("indexOf('abcdef','def', 0)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testIndexOf2() { | |||
assertEquals(4, Evaluator.getNumberResult("indexOf('abc def','def', 2)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testIndexOf3() { | |||
assertEquals(4, Evaluator.getNumberResult("indexOf('abc_def','def', 3)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testIndexOf4() { | |||
assertEquals(4, Evaluator.getNumberResult("indexOf('2 c 4e6','4e6', 1)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test (expected=FunctionInvalidArgumentException.class) | |||
public void testIndexOf5() { | |||
assertEquals(-1, Evaluator.getNumberResult("indexOf('2c4e 6','4e6', -1)", exerciseVariableMap, fillInVariableMap),0.0); | |||
fail(); | |||
} | } | ||
@Test | |||
public void testIndexOfWithInput1() { | |||
assertEquals(7, Evaluator.getNumberResult("indexOf('[pos=8]','str', 3)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testIndexOfWithInput2() { | |||
assertEquals(7, Evaluator.getNumberResult("indexOf('[pos=8]','[pos=7]', 2)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | |||
public void testIndexOfWithInput3() { | |||
assertEquals(7, Evaluator.getNumberResult("indexOf('[pos=8]','[pos=7]', [pos=9])", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | |||
@Test | @Test | ||
public void | public void testIndexOfWithVariables1() { | ||
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','str', 3)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIndexOfWithVariables2() { | ||
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','[var=g]', 2)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIndexOfWithVariables3() { | ||
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','[var=g]', [var=i])", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void | public void testIndexOfWithArgumentAsInputPointNumber() { | ||
assertEquals(OMCreator.createOMI(-1),Evaluator.evaluate("indexOf('abcdr5', 5.1, 1)", exerciseVariableMap, fillInVariableMap).getOMI()); | |||
} | } | ||
@Test | |||
public void testIndexOfWithWrongInputCharacter() { | |||
assertEquals(-1, Evaluator.getNumberResult("indexOf('abcd5', a, 1)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | |||
public void testIndexOfWithTwoRationalAndOneTextArguments() { | |||
assertEquals(4, Evaluator.getNumberResult("indexOf('abcd5', 5, 1)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void testIndexOfWithThreeRationalArguments(){ | public void testIndexOfWithThreeRationalArguments() { | ||
assertEquals(0,Evaluator.getNumberResult("indexOf(5, 5, 0)", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test | @Test | ||
public void testIndexOfWithThreeTextArguments(){ | public void testIndexOfWithThreeTextArguments() { | ||
assertEquals(3.0 , Evaluator.getNumberResult("indexOf('abcstr', 'str', '0')", exerciseVariableMap, fillInVariableMap),0.0); | |||
} | } | ||
@Test(expected= | @Test(expected=FunctionInvalidNumberOfArgumentsException.class) | ||
public void testIndexOfWithOneArgument(){ | public void testIndexOfWithOneArgument() { | ||
Evaluator.getNumberResult("indexOf(1)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test | @Test | ||
public void testIndexOfWithTwoArguments(){ | public void testIndexOfWithTwoArguments() { | ||
assertEquals(OMCreator.createOMI(-1),Evaluator.evaluate("indexOf('string', 1)", exerciseVariableMap, fillInVariableMap).getOMI()); | |||
} | } | ||
@Test(expected=UndefinedExerciseVariableException.class) | @Test(expected=UndefinedExerciseVariableException.class) | ||
public void testIndexOfWithMissingExerciseVariable(){ | public void testIndexOfWithMissingExerciseVariable() { | ||
Evaluator.getNumberResult("indexOf('[var=j]', 'str', 2)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected=UndefinedFillInVariableException.class) | @Test(expected=UndefinedFillInVariableException.class) | ||
public void testIndexOfWithMissingInput(){ | public void testIndexOfWithMissingInput() { | ||
Evaluator.getNumberResult("indexOf('[pos=42]', 'str', 2)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 18. September 2017, 12:02 Uhr
Beschreibung
Die Funktion indexOf bestimmt die Position, an der eine Zeichenkette oder ein einzelnes Zeichen innerhalb einer anderen Zeichenkette das erste Mal vorkommt. Außerdem muss angegeben werden, ab welcher Stelle gesucht werden soll. Die Funktion erwartet zwei Strings, die zu durchsuchende und die zu suchende Zeichenkette, und eine Ganzzahl, ab welcher Stelle die Suche beginnen soll. Es wird eine Ganzzahl zurückgegeben.
Syntax
indexOf(String zeichenkette, String muster, int position)
Parameter
- zeichenkette - Zeichenkette in dem gesucht wird
- muster - Zeichenkette, die innnerhalb der ersten gesucht wird
- position - Startpositition der Suche
Return Value
- Gibt eine Ganzzahl zurück
Beispiele
Standardfälle
indexOf('Hello World!','o',0) --> returns 4 indexOf('Hello World!','o',6) --> returns 7
Benutzung von Variablen
indexOf('[pos=1]','x',0) indexOf('text','[var=a]',0) indexOf('[var=b]','x',[pos=2])
JUnit Tests
[Anzeigen]
@BeforeClass
public static void beforeTest() {
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>str</OMSTR></OMOBJ>"));
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>anotherstring</OMSTR></OMOBJ>"));
fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>"));
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>str</OMSTR></OMOBJ>"));
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>anotherstring</OMSTR></OMOBJ>"));
exerciseVariableMap.put("i", OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>"));
}
@Test
public void testIndexOf1() {
assertEquals(3, Evaluator.getNumberResult("indexOf('abcdef','def', 0)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOf2() {
assertEquals(4, Evaluator.getNumberResult("indexOf('abc def','def', 2)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOf3() {
assertEquals(4, Evaluator.getNumberResult("indexOf('abc_def','def', 3)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOf4() {
assertEquals(4, Evaluator.getNumberResult("indexOf('2 c 4e6','4e6', 1)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test (expected=FunctionInvalidArgumentException.class)
public void testIndexOf5() {
assertEquals(-1, Evaluator.getNumberResult("indexOf('2c4e 6','4e6', -1)", exerciseVariableMap, fillInVariableMap),0.0);
fail();
}
@Test
public void testIndexOfWithInput1() {
assertEquals(7, Evaluator.getNumberResult("indexOf('[pos=8]','str', 3)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithInput2() {
assertEquals(7, Evaluator.getNumberResult("indexOf('[pos=8]','[pos=7]', 2)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithInput3() {
assertEquals(7, Evaluator.getNumberResult("indexOf('[pos=8]','[pos=7]', [pos=9])", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithVariables1() {
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','str', 3)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithVariables2() {
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','[var=g]', 2)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithVariables3() {
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','[var=g]', [var=i])", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithArgumentAsInputPointNumber() {
assertEquals(OMCreator.createOMI(-1),Evaluator.evaluate("indexOf('abcdr5', 5.1, 1)", exerciseVariableMap, fillInVariableMap).getOMI());
}
@Test
public void testIndexOfWithWrongInputCharacter() {
assertEquals(-1, Evaluator.getNumberResult("indexOf('abcd5', a, 1)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithTwoRationalAndOneTextArguments() {
assertEquals(4, Evaluator.getNumberResult("indexOf('abcd5', 5, 1)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithThreeRationalArguments() {
assertEquals(0,Evaluator.getNumberResult("indexOf(5, 5, 0)", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test
public void testIndexOfWithThreeTextArguments() {
assertEquals(3.0 , Evaluator.getNumberResult("indexOf('abcstr', 'str', '0')", exerciseVariableMap, fillInVariableMap),0.0);
}
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testIndexOfWithOneArgument() {
Evaluator.getNumberResult("indexOf(1)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test
public void testIndexOfWithTwoArguments() {
assertEquals(OMCreator.createOMI(-1),Evaluator.evaluate("indexOf('string', 1)", exerciseVariableMap, fillInVariableMap).getOMI());
}
@Test(expected=UndefinedExerciseVariableException.class)
public void testIndexOfWithMissingExerciseVariable() {
Evaluator.getNumberResult("indexOf('[var=j]', 'str', 2)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected=UndefinedFillInVariableException.class)
public void testIndexOfWithMissingInput() {
Evaluator.getNumberResult("indexOf('[pos=42]', 'str', 2)", 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.