Abs

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

Beschreibung

Die Funktion abs berechnet den Betrag einer Zahl. Die Funktion erwartet eine Zahl als Parameter und gibt eine Zahl zurück.

Syntax

abs(float zahl)

Parameter

  • zahl - eine rationale Zahl, deren Betrag berechnet werden soll

Return Value

  • Gibt eine Ganzzahl zurück

Beispiele

Standardfälle

abs(1)         --> returns 1
abs(-2.5)      --> returns 2.5
abs(-abs(5))   --> returns 5

Benutzung mit Variablen

abs('[pos=1]')
abs('[var=a]')

JUnit Tests

[Anzeigen]

@Test
public void testAbs1() {
	assertEquals(1, Evaluator.getNumberResult("abs(1)", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbs2() {
	assertEquals(1, Evaluator.getNumberResult("abs('-1')", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbs3() {
	assertEquals(0.5, Evaluator.getNumberResult("abs(-0.5)", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbs4() {
	assertEquals(0, Evaluator.getNumberResult("abs(-0)", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbsWithInput1() {
	assertEquals(1, Evaluator.getNumberResult("abs('[pos=2]')", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbsWithInput2() {
	assertEquals(0, Evaluator.getNumberResult("abs('[pos=1]')", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbsWithVariables1() {
	assertEquals(1, Evaluator.getNumberResult("abs('[var=b]')", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbsWithVariables2() {
	assertEquals(0, Evaluator.getNumberResult("abs('[var=a]')", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbsWithExpressions1() {
	assertEquals(5, Evaluator.getNumberResult("abs(-abs(5))", exerciseVariableMap, fillInVariableMap), 0);
}

@Test
public void testAbsWithExpressions2() {
	assertEquals(5, Evaluator.getNumberResult("abs(-abs(abs(-5)))", exerciseVariableMap, fillInVariableMap), 0);
}

@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testAbsWithWrongInputCharacter1() {
	Evaluator.getNumberResult("abs(a)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testAbsWithTwoArguments1() {
	Evaluator.getNumberResult("abs(-2.2, 3.3)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testAbsWithThreeArguments1() {
	Evaluator.getNumberResult("abs(2.123, -3.55, -1)", exerciseVariableMap, fillInVariableMap);
	fail();
}

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

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

Hinweise