ConvertToBinary: Unterschied zwischen den Versionen

Aus JACK Wiki
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">
@Before
@BeforeClass
public void beforeTest() {
public static void beforeTest() {
  placeFillInVariable(1, OMParser.parse("0"));
  placeFillInVariable(2, OMParser.parse("7"));
fillInVariableMap.put(2, OMConverter.toObject("<OMOBJ><OMI>7</OMI></OMOBJ>"));
 
 
  placeExerciseVariable("a", OMParser.parse("0"));
exerciseVariableMap.put("b", OMConverter.toObject("<OMOBJ><OMI>7</OMI></OMOBJ>"));
  placeExerciseVariable("b", OMParser.parse("7"));
}
 
@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 testConvertToBinary() {
public void testConvertToBinaryWithInput2() {
  assertTrue(10 == getEvaluator().getNumberResult("convertToBinary(2)"));
assertEquals(OMCreator.createOMSTR("0"),
  assertTrue(1101101111 == getEvaluator().getNumberResult("convertToBinary(879)"));
Evaluator.evaluate("convertToBinary([pos=1])", exerciseVariableMap, fillInVariableMap).getOMSTR());
  assertFalse(-11 == getEvaluator().getNumberResult("convertToBinary(-3)"));
}
}


@Test  
@Test
public void testConvertToBinaryWithInput() {
public void testConvertToBinaryWithVariables1() {
  assertTrue(111 == getEvaluator().getNumberResult("convertToBinary([pos=2])"));
assertEquals(OMCreator.createOMSTR("0"),
  assertTrue(0 == getEvaluator().getNumberResult("convertToBinary([pos=1])"));
Evaluator.evaluate("convertToBinary([var=a])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
}


@Test  
@Test
public void testConvertToBinaryWithVariables() {
public void testConvertToBinaryWithVariables2() {
  assertTrue(0 == getEvaluator().getNumberResult("convertToBinary([var=a])"));
assertEquals(OMCreator.createOMSTR("111"),
  assertTrue(111 == getEvaluator().getNumberResult("convertToBinary([var=b])"));
Evaluator.evaluate("convertToBinary([var=b])", exerciseVariableMap, fillInVariableMap).getOMSTR());
}
}


@Test (expected=InvalidEvaluatorFunctionArgumentException.class)
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testConvertToBinaryWithWrongInputCharacter() {
public void testConvertToBinaryWithWrongInputCharacter() {
  getEvaluator().getNumberResult("convertToBinary(a)");
Evaluator.evaluate("convertToBinary(a)", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test (expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testConvertToBinaryWithTwoArguments() {
public void testConvertToBinaryWithTwoArguments() {
  getEvaluator().getNumberResult("convertToBinary(3, 2)");
Evaluator.evaluate("convertToBinary(3, 2)", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test (expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testConvertToBinaryWithThreeArguments() {
public void testConvertToBinaryWithThreeArguments() {
  getEvaluator().getNumberResult("convertToBinary(1, 2, 3)");
Evaluator.evaluate("convertToBinary(1, 2, 3)", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test (expected=UndefinedExerciseVariableException.class)
@Test(expected = UndefinedExerciseVariableException.class)
public void testConvertToBinaryWithMissingExcerciseVariable() {
public void testConvertToBinaryWithMissingExcerciseVariable() {
  getEvaluator().getNumberResult("convertToBinary([var=j])");
Evaluator.evaluate("convertToBinary([var=j])", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test (expected=UndefinedFillInVariableException.class)
@Test(expected = UndefinedFillInVariableException.class)
public void testConvertToBinaryWithMissingInput() {
public void testConvertToBinaryWithMissingInput() {
  getEvaluator().getNumberResult("convertToBinary([pos=42])");
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.