ConvertToBinary: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Spobel (Diskussion | Beiträge) (→Syntax) |
|||
Zeile 3: | Zeile 3: | ||
=== Syntax === | === Syntax === | ||
convertToBinary( | convertToBinary([[Ganzzahl]] zahl) | ||
=== Parameter === | === Parameter === |
Version vom 4. Oktober 2017, 13:18 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(Ganzzahl 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
[Anzeigen]
@BeforeClass
public static void beforeTest() {
fillInVariableMap.put(2, OMConverter.toObject("<OMOBJ><OMI>7</OMI></OMOBJ>"));
exerciseVariableMap.put("b", OMConverter.toObject("<OMOBJ><OMI>7</OMI></OMOBJ>"));
}
@Test
public void testConvertToBinary1() {
assertEquals(OMCreator.createOMSTR("10"),
Evaluator.evaluate("convertToBinary(2)", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinary2() {
assertEquals(OMCreator.createOMSTR("1101101111"),
Evaluator.evaluate("convertToBinary(879)", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinary3() {
assertNotEquals(OMCreator.createOMSTR("-11"),
Evaluator.evaluate("convertToBinary(-3)", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithInput1() {
assertEquals(OMCreator.createOMSTR("111"),
Evaluator.evaluate("convertToBinary([pos=2])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithInput2() {
assertEquals(OMCreator.createOMSTR("0"),
Evaluator.evaluate("convertToBinary([pos=1])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithVariables1() {
assertEquals(OMCreator.createOMSTR("0"),
Evaluator.evaluate("convertToBinary([var=a])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test
public void testConvertToBinaryWithVariables2() {
assertEquals(OMCreator.createOMSTR("111"),
Evaluator.evaluate("convertToBinary([var=b])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testConvertToBinaryWithWrongInputCharacter() {
Evaluator.evaluate("convertToBinary(a)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testConvertToBinaryWithTwoArguments() {
Evaluator.evaluate("convertToBinary(3, 2)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testConvertToBinaryWithThreeArguments() {
Evaluator.evaluate("convertToBinary(1, 2, 3)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testConvertToBinaryWithMissingExcerciseVariable() {
Evaluator.evaluate("convertToBinary([var=j])", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testConvertToBinaryWithMissingInput() {
Evaluator.evaluate("convertToBinary([pos=42])", exerciseVariableMap, fillInVariableMap);
fail();
}
Hinweise
- Für negative Zahlen wird das Zweierkomplement zurückgegeben.