Length

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

Beschreibung

Die Funktion length gibt die Länge einer gegebenen Zeichenkette zurück. Die Funktion erwartet einen String und gibt eine Ganzzahl zurück.

Syntax

length(Zeichenkette zeichenkette)

Parameter

  • zeichenkette - Zeichenkette deren Länge bestimmt werden soll

Return Value

Beispiele

Standardfälle

length('Hello World!')   --> returns 12
length('a')   --> returns 1

Benutzung von Variablen

length([pos=1])
length([var=a])

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() {
	
		fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>Haveaniceday</OMSTR></OMOBJ>"));
		fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>stillworking</OMSTR></OMOBJ>"));
		
		exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>Haveaniceday</OMSTR></OMOBJ>"));
		exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>stillworking</OMSTR></OMOBJ>"));
	
}

@Test
public void testLength1() {
	assertEquals(5, Evaluator.getNumberResult("length('abcde')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLength2() {
	assertEquals(14, Evaluator.getNumberResult("length('another string')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLength3() {
	assertEquals(0, Evaluator.getNumberResult("length('')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLength4() {
	assertEquals(2, Evaluator.getNumberResult("length('  ')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLengthWithInput1() {
	assertEquals(12, Evaluator.getNumberResult("length('[pos=7]')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLengthWithInput2() {
	assertEquals(12, Evaluator.getNumberResult("length('[pos=8]')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLengthWithVariables1() {
	assertEquals(12, Evaluator.getNumberResult("length('[var=g]')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testLengthWithVariables2() {
	assertEquals(12, Evaluator.getNumberResult("length('[var=h]')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test(expected=ParserException.class)
public void testLengthWithWrongInputCharacter() {
	Evaluator.getNumberResult("length(ab)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testLengthWithTwoArguments() {
	Evaluator.getNumberResult("length('abab', 'abab')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testLengthWithThreeArguments() {
	Evaluator.getNumberResult("length('abab', 'ab', 'ab')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=UndefinedExerciseVariableException.class)
public void testLengthWithMissingExerciseVariable() {
	Evaluator.getNumberResult("length('[var=j]')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=UndefinedFillInVariableException.class)
public void testLengthWithMissingInput() {
	Evaluator.getNumberResult("length('[pos=42]')", exerciseVariableMap, fillInVariableMap);
	fail();
}

Hinweise