CharAt: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 22: | Zeile 22: | ||
<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() { | ||
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 | @Test | ||
public void | 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= | @Test(expected = FunctionInvalidArgumentException.class) | ||
public void testCharAtOutOfBoundsIndexNegative() { | public void testCharAtOutOfBoundsIndexNegative() { | ||
Evaluator.evaluate("charAt('Hello World!1',-1)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidArgumentException.class) | ||
public void testCharAtOutOfBoundsIndexToHigh() { | public void testCharAtOutOfBoundsIndexToHigh() { | ||
Evaluator.evaluate("charAt('Hello World!1',55)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test | @Test | ||
public void testCharAtWithInput() { | public void testCharAtWithInput() { | ||
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("W")), | |||
Evaluator.evaluate("charAt('Hello [pos=1]orld!1',6)", charAtExerciseVariableMap, charAtFillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void testCharAtWithVariables() { | public void testCharAtWithVariables() { | ||
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("W")), | |||
Evaluator.evaluate("charAt('Hello [var=a]orld!1',6)", charAtExerciseVariableMap, charAtFillInVariableMap)); | |||
} | } | ||
@Test | @Test | ||
public void | 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= | @Test (expected=FunctionInvalidArgumentTypeException.class) | ||
public void testCharAtWithWrongInputCharacter() { | public void testCharAtWithWrongInputCharacter() { | ||
Evaluator.evaluate("charAt('abcd', a)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testCharAtWithOneArgument() { | public void testCharAtWithOneArgument() { | ||
Evaluator.evaluate("charAt('abcd')", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testCharAtWithThreeArguments() { | public void testCharAtWithThreeArguments() { | ||
Evaluator.evaluate("charAt('abcd', 0, 2)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidArgumentException.class) | ||
public void testCharAtWithTwoRationalArguments() { | public void testCharAtWithTwoRationalArguments() { | ||
Evaluator.evaluate("charAt(5, 12)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
} | } | ||
@Test | @Test | ||
public void testCharAtWithTwoTextArguments() { | public void testCharAtWithTwoTextArguments() { | ||
Evaluator.evaluate("charAt('abc', '1')", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
} | } | ||
@Test (expected=UndefinedExerciseVariableException.class) | @Test(expected = UndefinedExerciseVariableException.class) | ||
public void testCharAtWithMissingExcerciseVariable() { | public void testCharAtWithMissingExcerciseVariable() { | ||
Evaluator.evaluate("charAt('[var=j]', 2)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedFillInVariableException.class) | @Test(expected = UndefinedFillInVariableException.class) | ||
public void testCharAtWithMissingInput() { | public void testCharAtWithMissingInput() { | ||
Evaluator.evaluate("charAt('[pos=42]', 2)", charAtExerciseVariableMap, charAtFillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 18. September 2017, 10:36 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(String zeichenkette, int 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
JUnit Tests
[Anzeigen]
@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();
}
Hinweise
- Die angegebene Zahl darf nicht kleiner als 0 und nicht größer als die Länge der Zeichenkette -1 sein.