Abs: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 23: | Zeile 23: | ||
====JUnit Tests==== | ====JUnit Tests==== | ||
<div class="mw-collapsible mw-collapsed"> | <div class="mw-collapsible mw-collapsed"> | ||
abs(a) | @Test public void testAbs() { | ||
abs(-2.2, 3.3) | assertTrue(1 == getEvaluator().getNumberResult("abs(1)")); | ||
abs('[var=j]') | assertTrue(1 == getEvaluator().getNumberResult("abs('-1')")); | ||
abs('[pos=42]') | assertTrue(0.5 == getEvaluator().getNumberResult("abs(-0.5)")); | ||
assertTrue(0 == getEvaluator().getNumberResult("abs(-0)")); | |||
} | |||
@Test public void testAbsWithInput() { | |||
assertTrue(1 == getEvaluator().getNumberResult("abs('[pos=2]')")); | |||
assertTrue(8.43 == getEvaluator().getNumberResult("abs('[pos=1]')")); | |||
} | |||
@Test public void testAbsWithVariables() { | |||
assertTrue(1 == getEvaluator().getNumberResult("abs('[var=b]')")); | |||
assertTrue(8.43 == getEvaluator().getNumberResult("abs('[var=a]')")); | |||
} | |||
@Test public void testAbsWithExpressions() { | |||
assertTrue(5 == getEvaluator().getNumberResult("abs(-abs(5))")); | |||
assertTrue(5 == getEvaluator().getNumberResult("abs(-abs(abs(-5)))")); | |||
} | |||
@Test (expected=ParserMismatchException.class) | |||
public void testAbsWithWrongInputCharacter() { | |||
getEvaluator().getNumberResult("abs(a)"); | |||
} | |||
@Test (expected=UnsupportedEvaluatorFunctionException.class) | |||
public void testAbsWithTwoArguments() { | |||
getEvaluator().getNumberResult("abs(-2.2, 3.3)"); | |||
} | |||
@Test (expected=UnsupportedEvaluatorFunctionException.class) | |||
public void testAbsWithThreeArguments() { | |||
getEvaluator().getNumberResult("abs(2.123, -3.55, -1)"); | |||
} | |||
@Test (expected=UndefinedExerciseVariableException.class) | |||
public void testAbsWithMissingExcerciseVariable() { | |||
getEvaluator().getNumberResult("abs('[var=j]')"); | |||
} | |||
@Test (expected=UndefinedFillInVariableException.class) | |||
public void testAbsWithMissingInput() { | |||
getEvaluator().getNumberResult("abs('[pos=42]')"); | |||
} | |||
</div> | </div> | ||
Version vom 20. Juni 2017, 09:05 Uhr
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
@Test public void testAbs() { assertTrue(1 == getEvaluator().getNumberResult("abs(1)")); assertTrue(1 == getEvaluator().getNumberResult("abs('-1')")); assertTrue(0.5 == getEvaluator().getNumberResult("abs(-0.5)")); assertTrue(0 == getEvaluator().getNumberResult("abs(-0)")); } @Test public void testAbsWithInput() { assertTrue(1 == getEvaluator().getNumberResult("abs('[pos=2]')")); assertTrue(8.43 == getEvaluator().getNumberResult("abs('[pos=1]')")); } @Test public void testAbsWithVariables() { assertTrue(1 == getEvaluator().getNumberResult("abs('[var=b]')")); assertTrue(8.43 == getEvaluator().getNumberResult("abs('[var=a]')")); } @Test public void testAbsWithExpressions() { assertTrue(5 == getEvaluator().getNumberResult("abs(-abs(5))")); assertTrue(5 == getEvaluator().getNumberResult("abs(-abs(abs(-5)))")); } @Test (expected=ParserMismatchException.class) public void testAbsWithWrongInputCharacter() { getEvaluator().getNumberResult("abs(a)"); } @Test (expected=UnsupportedEvaluatorFunctionException.class) public void testAbsWithTwoArguments() { getEvaluator().getNumberResult("abs(-2.2, 3.3)"); } @Test (expected=UnsupportedEvaluatorFunctionException.class) public void testAbsWithThreeArguments() { getEvaluator().getNumberResult("abs(2.123, -3.55, -1)"); } @Test (expected=UndefinedExerciseVariableException.class) public void testAbsWithMissingExcerciseVariable() { getEvaluator().getNumberResult("abs('[var=j]')"); } @Test (expected=UndefinedFillInVariableException.class) public void testAbsWithMissingInput() { getEvaluator().getNumberResult("abs('[pos=42]')"); }