CharAt: Unterschied zwischen den Versionen

Aus JACK Wiki
Zur Navigation springen Zur Suche springen
KKeine Bearbeitungszusammenfassung
Zeile 16: Zeile 16:
=== Beispiele ===
=== Beispiele ===
  charAt('hallo',0)  --> returns h
  charAt('hallo',0)  --> returns h
 
  charAt('JACK',3)  --> returns K
  charAt('JACK',3)  --> returns K
====JUnit Tests====
<span class="mw-customtoggle-myDivision">[Anzeigen]</span>
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
@BeforeClass
public static void beforeTest() {
charAtFillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMSTR>W</OMSTR></OMOBJ>"));
charAtExerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMSTR>W</OMSTR></OMOBJ>"));
}
@Test
public void testCharAt1() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("o")),
Evaluator.evaluate("charAt('Hello World!1',4)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test
public void testCharAt2() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("W")),
Evaluator.evaluate("charAt('Hello World!1',6)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test
public void testCharAt3() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR(" ")),
Evaluator.evaluate("charAt('Hello World!1',5)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test
public void testCharAt4() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("!")),
Evaluator.evaluate("charAt('Hello World!1',11)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test
public void testCharAt5() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("1")),
Evaluator.evaluate("charAt('Hello World!1',12)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test(expected = FunctionInvalidArgumentException.class)
public void testCharAtOutOfBoundsIndexNegative() {
Evaluator.evaluate("charAt('Hello World!1',-1)", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidArgumentException.class)
public void testCharAtOutOfBoundsIndexToHigh() {
Evaluator.evaluate("charAt('Hello World!1',55)", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
@Test
public void testCharAtWithInput() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("W")),
Evaluator.evaluate("charAt('Hello [pos=1]orld!1',6)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test
public void testCharAtWithVariables() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("W")),
Evaluator.evaluate("charAt('Hello [var=a]orld!1',6)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test
public void testCharAtWithExpressions1() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("b")),
Evaluator.evaluate("charAt(charAt('abc', 1), 0)", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test(expected = FunctionInvalidArgumentTypeException.class) //Throws Error, because carAt returns a String! This should not work
public void testCharAtWithExpressions2() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
Evaluator.evaluate("charAt('abc', charAt('00', 0))", charAtExerciseVariableMap, charAtFillInVariableMap));
}
@Test (expected=FunctionInvalidArgumentTypeException.class)
public void testCharAtWithWrongInputCharacter() {
Evaluator.evaluate("charAt('abcd', a)", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCharAtWithOneArgument() {
Evaluator.evaluate("charAt('abcd')", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCharAtWithThreeArguments() {
Evaluator.evaluate("charAt('abcd', 0, 2)", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidArgumentException.class)
public void testCharAtWithTwoRationalArguments() {
Evaluator.evaluate("charAt(5, 12)", charAtExerciseVariableMap, charAtFillInVariableMap);
}
@Test
public void testCharAtWithTwoTextArguments() {
Evaluator.evaluate("charAt('abc', '1')", charAtExerciseVariableMap, charAtFillInVariableMap);
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testCharAtWithMissingExcerciseVariable() {
Evaluator.evaluate("charAt('[var=j]', 2)", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testCharAtWithMissingInput() {
Evaluator.evaluate("charAt('[pos=42]', 2)", charAtExerciseVariableMap, charAtFillInVariableMap);
fail();
}
</syntaxhighlight>


=== Hinweise ===
=== Hinweise ===

Version vom 24. September 2018, 12:47 Uhr

Beschreibung

Die Funktion charAt gibt das Zeichen an einer beliebigen Stelle innerhalb einer Zeichenkette zurück. Das erste Zeichen hat dabei den Index 0, das zweite Zeichen den Index 1 usw.

Die Funktion erwartet eine Zeichenkette und eine natürliche Zahl und gibt das Zeichen an der entsprechenden Stelle zurück.

Syntax

charAt(Zeichenkette zeichenkette, Ganzzahl stelle)

Parameter

  • zeichenkette - die Zeichenkette, aus der das Zeichen bestimmt wird
  • stelle - das wievielte Zeichen zurückgegeben werden soll

Return Value

  • Gibt eine Character zurück.

Beispiele

charAt('hallo',0)   --> returns h

charAt('JACK',3)   --> returns K

Hinweise

  • Die angegebene Zahl darf nicht kleiner als 0 und nicht größer als die Länge der Zeichenkette -1 sein.