ConvertToBinary: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 22: | Zeile 22: | ||
<span class="mw-customtoggle-myDivision">[Anzeigen]</span> | <span class="mw-customtoggle-myDivision">[Anzeigen]</span> | ||
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision"> | <syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision"> | ||
@ | @BeforeClass | ||
public void beforeTest() { | 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 | @Test | ||
public void | public void testConvertToBinaryWithInput2() { | ||
assertEquals(OMCreator.createOMSTR("0"), | |||
Evaluator.evaluate("convertToBinary([pos=1])", exerciseVariableMap, fillInVariableMap).getOMSTR()); | |||
} | } | ||
@Test | @Test | ||
public void | public void testConvertToBinaryWithVariables1() { | ||
assertEquals(OMCreator.createOMSTR("0"), | |||
Evaluator.evaluate("convertToBinary([var=a])", exerciseVariableMap, fillInVariableMap).getOMSTR()); | |||
} | } | ||
@Test | @Test | ||
public void | public void testConvertToBinaryWithVariables2() { | ||
assertEquals(OMCreator.createOMSTR("111"), | |||
Evaluator.evaluate("convertToBinary([var=b])", exerciseVariableMap, fillInVariableMap).getOMSTR()); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidArgumentTypeException.class) | ||
public void testConvertToBinaryWithWrongInputCharacter() { | public void testConvertToBinaryWithWrongInputCharacter() { | ||
Evaluator.evaluate("convertToBinary(a)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testConvertToBinaryWithTwoArguments() { | public void testConvertToBinaryWithTwoArguments() { | ||
Evaluator.evaluate("convertToBinary(3, 2)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected= | @Test(expected = FunctionInvalidNumberOfArgumentsException.class) | ||
public void testConvertToBinaryWithThreeArguments() { | public void testConvertToBinaryWithThreeArguments() { | ||
Evaluator.evaluate("convertToBinary(1, 2, 3)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedExerciseVariableException.class) | @Test(expected = UndefinedExerciseVariableException.class) | ||
public void testConvertToBinaryWithMissingExcerciseVariable() { | public void testConvertToBinaryWithMissingExcerciseVariable() { | ||
Evaluator.evaluate("convertToBinary([var=j])", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test (expected=UndefinedFillInVariableException.class) | @Test(expected = UndefinedFillInVariableException.class) | ||
public void testConvertToBinaryWithMissingInput() { | public void testConvertToBinaryWithMissingInput() { | ||
Evaluator.evaluate("convertToBinary([pos=42])", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Version vom 18. September 2017, 10:38 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
[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.