ConvertToBinary: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 18: | Zeile 18: | ||
convertToBinary('[pos=1]') | convertToBinary('[pos=1]') | ||
convertToBinary('[var=a]') | convertToBinary('[var=a]') | ||
====JUnit Tests==== | |||
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed"> | |||
@Before | |||
public void beforeTest() { | |||
placeFillInVariable(1, OMParser.parse("0")); | |||
placeFillInVariable(2, OMParser.parse("7")); | |||
placeExerciseVariable("a", OMParser.parse("0")); | |||
placeExerciseVariable("b", OMParser.parse("7")); | |||
} | |||
@Test | |||
public void testConvertToBinary() { | |||
assertTrue(10 == getEvaluator().getNumberResult("convertToBinary(2)")); | |||
assertTrue(1101101111 == getEvaluator().getNumberResult("convertToBinary(879)")); | |||
assertFalse(-11 == getEvaluator().getNumberResult("convertToBinary(-3)")); | |||
} | |||
@Test | |||
public void testConvertToBinaryWithInput() { | |||
assertTrue(111 == getEvaluator().getNumberResult("convertToBinary([pos=2])")); | |||
assertTrue(0 == getEvaluator().getNumberResult("convertToBinary([pos=1])")); | |||
} | |||
@Test | |||
public void testConvertToBinaryWithVariables() { | |||
assertTrue(0 == getEvaluator().getNumberResult("convertToBinary([var=a])")); | |||
assertTrue(111 == getEvaluator().getNumberResult("convertToBinary([var=b])")); | |||
} | |||
@Test (expected=InvalidEvaluatorFunctionArgumentException.class) | |||
public void testConvertToBinaryWithWrongInputCharacter() { | |||
getEvaluator().getNumberResult("convertToBinary(a)"); | |||
} | |||
@Test (expected=UnsupportedEvaluatorFunctionException.class) | |||
public void testConvertToBinaryWithTwoArguments() { | |||
getEvaluator().getNumberResult("convertToBinary(3, 2)"); | |||
} | |||
@Test (expected=UnsupportedEvaluatorFunctionException.class) | |||
public void testConvertToBinaryWithThreeArguments() { | |||
getEvaluator().getNumberResult("convertToBinary(1, 2, 3)"); | |||
} | |||
@Test (expected=UndefinedExerciseVariableException.class) | |||
public void testConvertToBinaryWithMissingExcerciseVariable() { | |||
getEvaluator().getNumberResult("convertToBinary([var=j])"); | |||
} | |||
@Test (expected=UndefinedFillInVariableException.class) | |||
public void testConvertToBinaryWithMissingInput() { | |||
getEvaluator().getNumberResult("convertToBinary([pos=42])"); | |||
} | |||
</syntaxhighlight> | |||
=== Hinweise === | === Hinweise === |
Version vom 20. Juni 2017, 10:39 Uhr
Beschreibung
Die Funktion convertToBinary wandelt eine Ganzzahl in ihre entsprechende Binärdarstellung um. Die Funktion erwartet eine Zahl und gibt eine Zahl zurück.
Syntax
convertToBinary(int zahl)
Parameter
- zahl - die Dezimaldarstellung der Zahl, die in Binär umgewandelt werden soll
Return Value
- Gibt eine Ganzzahl zurück
Beispiele
Standardfälle
convertToBinary('15') --> returns 1111
Benutzung von Variablen
convertToBinary('[pos=1]') convertToBinary('[var=a]')
JUnit Tests
@Before
public void beforeTest() {
placeFillInVariable(1, OMParser.parse("0"));
placeFillInVariable(2, OMParser.parse("7"));
placeExerciseVariable("a", OMParser.parse("0"));
placeExerciseVariable("b", OMParser.parse("7"));
}
@Test
public void testConvertToBinary() {
assertTrue(10 == getEvaluator().getNumberResult("convertToBinary(2)"));
assertTrue(1101101111 == getEvaluator().getNumberResult("convertToBinary(879)"));
assertFalse(-11 == getEvaluator().getNumberResult("convertToBinary(-3)"));
}
@Test
public void testConvertToBinaryWithInput() {
assertTrue(111 == getEvaluator().getNumberResult("convertToBinary([pos=2])"));
assertTrue(0 == getEvaluator().getNumberResult("convertToBinary([pos=1])"));
}
@Test
public void testConvertToBinaryWithVariables() {
assertTrue(0 == getEvaluator().getNumberResult("convertToBinary([var=a])"));
assertTrue(111 == getEvaluator().getNumberResult("convertToBinary([var=b])"));
}
@Test (expected=InvalidEvaluatorFunctionArgumentException.class)
public void testConvertToBinaryWithWrongInputCharacter() {
getEvaluator().getNumberResult("convertToBinary(a)");
}
@Test (expected=UnsupportedEvaluatorFunctionException.class)
public void testConvertToBinaryWithTwoArguments() {
getEvaluator().getNumberResult("convertToBinary(3, 2)");
}
@Test (expected=UnsupportedEvaluatorFunctionException.class)
public void testConvertToBinaryWithThreeArguments() {
getEvaluator().getNumberResult("convertToBinary(1, 2, 3)");
}
@Test (expected=UndefinedExerciseVariableException.class)
public void testConvertToBinaryWithMissingExcerciseVariable() {
getEvaluator().getNumberResult("convertToBinary([var=j])");
}
@Test (expected=UndefinedFillInVariableException.class)
public void testConvertToBinaryWithMissingInput() {
getEvaluator().getNumberResult("convertToBinary([pos=42])");
}
Hinweise
- Für negative Zahlen wird das Zweierkomplement zurückgegeben.