IndexOf: Unterschied zwischen den Versionen

Aus JACK Wiki
Wechseln zu: Navigation, Suche
(JUnit Tests)
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">
@Before
+
@BeforeClass
public void beforeTest() {
+
public static void beforeTest() {
  placeFillInVariable(1, OMParser.parse("str"));
+
  placeFillInVariable(2, OMParser.parse("anotherstring"));
+
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>str</OMSTR></OMOBJ>"));
  placeFillInVariable(3, OMParser.parse("2"));
+
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>anotherstring</OMSTR></OMOBJ>"));
 +
fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>"));
 
 
  placeExerciseVariable("a", OMParser.parse("str"));
+
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>str</OMSTR></OMOBJ>"));
  placeExerciseVariable("b", OMParser.parse("anotherstring"));
+
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>anotherstring</OMSTR></OMOBJ>"));
  placeExerciseVariable("c", OMParser.parse("2"));
+
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 testIndexOf(){
+
public void testIndexOfWithVariables1() {
  assertTrue(3 == getEvaluator().getNumberResult("indexOf('abcdef','def', 0)"));
+
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','str', 3)", exerciseVariableMap, fillInVariableMap),0.0);
  assertTrue(4 == getEvaluator().getNumberResult("indexOf('abc def','def', 2)"));
 
  assertTrue(4 == getEvaluator().getNumberResult("indexOf('abc_def','def', 3)"));
 
  assertTrue(4 == getEvaluator().getNumberResult("indexOf('2 c 4e6','4e6', 1)"));
 
  assertTrue(-1 == getEvaluator().getNumberResult("indexOf('2c4e 6','4e6', -1)"));
 
 
}
 
}
  
 
@Test
 
@Test
public void testIndexOfWithInput(){
+
public void testIndexOfWithVariables2() {
  assertTrue(7 == getEvaluator().getNumberResult("indexOf('[pos=2]','str', 3)"));
+
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','[var=g]', 2)", exerciseVariableMap, fillInVariableMap),0.0);
  assertTrue(7 == getEvaluator().getNumberResult("indexOf('[pos=2]','[pos=1]', 2)"));
 
  assertTrue(7 == getEvaluator().getNumberResult("indexOf('[pos=2]','[pos=1]', [pos=3])"));
 
 
}
 
}
+
 
 
@Test
 
@Test
public void testIndexOfWithVariables(){
+
public void testIndexOfWithVariables3() {
  assertTrue(7 == getEvaluator().getNumberResult("indexOf('[var=b]','str', 3)"));
+
assertEquals(7, Evaluator.getNumberResult("indexOf('[var=h]','[var=g]', [var=i])", exerciseVariableMap, fillInVariableMap),0.0);
  assertTrue(7 == getEvaluator().getNumberResult("indexOf('[var=b]','[var=a]', 2)"));
 
  assertTrue(7 == getEvaluator().getNumberResult("indexOf('[var=b]','[var=a]', [var=c])"));
 
 
}
 
}
  
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test
public void testIndexOfWithWrongInputPointNumber(){
+
public void testIndexOfWithArgumentAsInputPointNumber() {
  getEvaluator().getNumberResult("indexOf('abcd5', 5.1, 1)");
+
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(expected=InvalidEvaluatorFunctionArgumentException.class)
 
public void testIndexOfWithWrongInputCharacter(){
 
  getEvaluator().getNumberResult("indexOf('abcd5', a, 1)");
 
 
}
 
}
 +
 +
@Test
 +
public void testIndexOfWithTwoRationalAndOneTextArguments() {
 +
assertEquals(4, Evaluator.getNumberResult("indexOf('abcd5', 5, 1)", exerciseVariableMap, fillInVariableMap),0.0);
 
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
 
public void testIndexOfWithTwoRationalAndOneTextArguments(){
 
  getEvaluator().getNumberResult("indexOf('abcd5', 5, 1)");
 
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test
public void testIndexOfWithThreeRationalArguments(){
+
public void testIndexOfWithThreeRationalArguments() {
  getEvaluator().getNumberResult("indexOf(5, 5, 0)");
+
assertEquals(0,Evaluator.getNumberResult("indexOf(5, 5, 0)", exerciseVariableMap, fillInVariableMap),0.0);
 +
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test
public void testIndexOfWithThreeTextArguments(){
+
public void testIndexOfWithThreeTextArguments() {
  getEvaluator().getNumberResult("indexOf('str', 'str', '0')");
+
assertEquals(3.0 , Evaluator.getNumberResult("indexOf('abcstr', 'str', '0')", exerciseVariableMap, fillInVariableMap),0.0);
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testIndexOfWithOneArgument(){
+
public void testIndexOfWithOneArgument() {
getEvaluator().getNumberResult("indexOf(1)");
+
Evaluator.getNumberResult("indexOf(1)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test
public void testIndexOfWithTwoArguments(){
+
public void testIndexOfWithTwoArguments() {
  getEvaluator().getNumberResult("indexOf('string', 1)");
+
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() {
  getEvaluator().getNumberResult("indexOf('[var=j]', 'str', 2)");
+
Evaluator.getNumberResult("indexOf('[var=j]', 'str', 2)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
 
@Test(expected=UndefinedFillInVariableException.class)
 
@Test(expected=UndefinedFillInVariableException.class)
public void testIndexOfWithMissingInput(){
+
public void testIndexOfWithMissingInput() {
  getEvaluator().getNumberResult("indexOf('[pos=42]', 'str', 2)");
+
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.