GCD: Unterschied zwischen den Versionen

Aus JACK Wiki
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Zeile 20: Zeile 20:
<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("20"));
  placeFillInVariable(3, OMParser.parse("10"));
  placeFillInVariable(4, OMParser.parse("-5"));
  placeFillInVariable(5, OMParser.parse("5"));
  placeExerciseVariable("a", OMParser.parse("0"));
fillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
  placeExerciseVariable("b", OMParser.parse("20"));
fillInVariableMap.put(2, OMConverter.toObject("<OMOBJ><OMI>20</OMI></OMOBJ>"));
  placeExerciseVariable("c", OMParser.parse("10"));
fillInVariableMap.put(3, OMConverter.toObject("<OMOBJ><OMI>10</OMI></OMOBJ>"));
  placeExerciseVariable("d", OMParser.parse("-5"));
fillInVariableMap.put(4, OMConverter.toObject("<OMOBJ><OMI>-5</OMI></OMOBJ>"));
  placeExerciseVariable("e", OMParser.parse("5"));
fillInVariableMap.put(5, OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));
 
exerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
exerciseVariableMap.put("b", OMConverter.toObject("<OMOBJ><OMI>20</OMI></OMOBJ>"));
exerciseVariableMap.put("c", OMConverter.toObject("<OMOBJ><OMI>10</OMI></OMOBJ>"));
exerciseVariableMap.put("d", OMConverter.toObject("<OMOBJ><OMI>-5</OMI></OMOBJ>"));
exerciseVariableMap.put("e", OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));
}
 
@Test
public void testGCD1() {
assertEquals(5, Evaluator.getNumberResult("gcd('5', '5')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCD2() {
assertEquals(3, Evaluator.getNumberResult("gcd('3', '-9')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCD3() {
assertEquals(3, Evaluator.getNumberResult("gcd('-3', '9')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCD4() {
assertEquals(5, Evaluator.getNumberResult("gcd('-5', '-5')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCD5() {
assertNotEquals(5, Evaluator.getNumberResult("gcd('5', '9')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCD6() {
assertNotEquals(3, Evaluator.getNumberResult("gcd('-3', '7')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCDAtDefinition1() {
assertEquals(55, Evaluator.getNumberResult("gcd('0', '55')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCDAtDefinition2() {
assertEquals(11, Evaluator.getNumberResult("gcd('-11', '0')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCDAtDefinition3() {
assertEquals(0, Evaluator.getNumberResult("gcd('0', '0')", exerciseVariableMap, fillInVariableMap), 0.0);
}
 
@Test
public void testGCDWithInput1() {
assertEquals(10, Evaluator.getNumberResult("gcd('[pos=2]', '[pos=3]')", exerciseVariableMap, fillInVariableMap),
0.0);
}
 
@Test
public void testGCDWithInput2() {
assertEquals(5, Evaluator.getNumberResult("gcd('[pos=3]', '[pos=4]')", exerciseVariableMap, fillInVariableMap),
0.0);
}
 
@Test
public void testGCDWithInput3() {
assertEquals(5, Evaluator.getNumberResult("gcd('[pos=1]', '[pos=4]')", exerciseVariableMap, fillInVariableMap),
0.0);
}
}


@Test  
@Test
public void testGCD(){
public void testGCDWithVariables1() {
  assertTrue(5 == getEvaluator().getNumberResult("gcd('5', '5')"));
assertEquals(10, Evaluator.getNumberResult("gcd('[var=b]', '[var=c]')", exerciseVariableMap, fillInVariableMap),
  assertTrue(3 == getEvaluator().getNumberResult("gcd('3', '-9')"));
0.0);
  assertTrue(3 == getEvaluator().getNumberResult("gcd('-3', '9')"));
  assertTrue(5 == getEvaluator().getNumberResult("gcd('-5', '-5')"));
  assertFalse(5 == getEvaluator().getNumberResult("gcd('5', '9')"));
  assertFalse(3 == getEvaluator().getNumberResult("gcd('-3', '7')"));
}
}


@Test
@Test
public void testGCDAtDefinition(){
public void testGCDWithVariables2() {
  assertTrue(55 == getEvaluator().getNumberResult("gcd('0', '55')"));
assertEquals(5, Evaluator.getNumberResult("gcd('[var=c]', '[var=d]')", exerciseVariableMap, fillInVariableMap),
  assertTrue(11 ==getEvaluator().getNumberResult("gcd('-11', '0')"));
0.0);
  assertTrue(0 ==getEvaluator().getNumberResult("gcd('0', '0')"));
}
}
 
@Test
public void testGCDWithVariables3() {
assertEquals(5, Evaluator.getNumberResult("gcd('[var=a]', '[var=d]')", exerciseVariableMap, fillInVariableMap),
0.0);
}
 
@Test
@Test
public void testGCDWithInput(){
public void testGCDWithExpressions1() {
  assertTrue(10 == getEvaluator().getNumberResult("gcd('[pos=2]', '[pos=3]')"));
assertEquals(5,
  assertTrue(5 == getEvaluator().getNumberResult("gcd('[pos=3]', '[pos=4]')"));
Evaluator.getNumberResult("gcd('gcd(5, [var=c])', '[var=c]')", exerciseVariableMap, fillInVariableMap),
  assertTrue(5 == getEvaluator().getNumberResult("gcd('[pos=1]', '[pos=4]')"));
0.0);
}
}


@Test
@Test
public void testGCDWithVariables(){
public void testGCDWithExpressions2() {
  assertTrue(10 == getEvaluator().getNumberResult("gcd('[var=b]', '[var=c]')"));
assertEquals(5, Evaluator.getNumberResult("gcd('[var=c]', '(gcd([var=d], 5))')", exerciseVariableMap,
  assertTrue(5 == getEvaluator().getNumberResult("gcd('[var=c]', '[var=d]')"));
fillInVariableMap), 0.0);
  assertTrue(5 == getEvaluator().getNumberResult("gcd('[var=a]', '[var=d]')"));
}
}


@Test
@Test
public void testGCDWithExpressions(){
public void testGCDWithExpressions3() {
  assertTrue(5 == getEvaluator().getNumberResult("gcd('gcd(5, [var=c])', '[var=c]')"));
assertEquals(5, Evaluator.getNumberResult("gcd(gcd('[var=e]',gcd('[var=e]','5')),gcd('[var=b]','[pos=5]'))",
  assertTrue(5 == getEvaluator().getNumberResult("gcd('[var=c]', '(gcd([var=d], 5))')"));
exerciseVariableMap, fillInVariableMap), 0.0);
  assertTrue(5 ==getEvaluator().getNumberResult("gcd('(gcd([var=e],gcd([var=e], 5])))', '(gcd([var=b],[pos=5]))')"));
}
}


@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testGCDWithWrongInputPointNumber(){
public void testGCDWithWrongInputPointNumber() {
  getEvaluator().getNumberResult("gcd('5.5','4')");
Evaluator.getNumberResult("gcd('5.5','4')", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testGCDWithWrongInputCharacter(){
public void testGCDWithWrongInputCharacter() {
  getEvaluator().getNumberResult("gcd('a', '2')");
Evaluator.getNumberResult("gcd('a', '2')", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test(expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testGCDWithOneArgument(){
public void testGCDWithOneArgument() {
  getEvaluator().getNumberResult("gcd('7')");
Evaluator.getNumberResult("gcd('7')", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test(expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testGCDWithThreeArguments(){
public void testGCDWithThreeArguments() {
  getEvaluator().getNumberResult("gcd('4', '5', '7')");
Evaluator.getNumberResult("gcd('4', '5', '7')", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test(expected=UndefinedExerciseVariableException.class)
@Test(expected = UndefinedExerciseVariableException.class)
public void testGCDWithMissingExerciseVariable(){
public void testGCDWithMissingExerciseVariable() {
  getEvaluator().getNumberResult("gcd('[var=j]', '3')");
Evaluator.getNumberResult("gcd('[var=j]', '3')", exerciseVariableMap, fillInVariableMap);
fail();
}
}


@Test(expected=UndefinedFillInVariableException.class)
@Test(expected = UndefinedFillInVariableException.class)
public void testGCDWithMissingInput(){
public void testGCDWithMissingInput() {
  getEvaluator().getNumberResult("gcd('[pos=42]', '3')");
Evaluator.getNumberResult("gcd('[pos=42]', '3')", exerciseVariableMap, fillInVariableMap);
fail();
}
}
</syntaxhighlight>
</syntaxhighlight>

Version vom 18. September 2017, 11:21 Uhr

Beschreibung

Die Funktion gcd berechnet den größten gemeinsamen Teiler zweier ganzer Zahlen. Die Funktion erwartet zwei Zahlen und gibt eine Zahl zurück.

Syntax

gcd(int zahl1, int zahl2)

Parameter

  • zahl1 - erste Zahl aus der Menge der ganzen Zahlen
  • zahl2 - zweite Zahl aus der Menge der ganzen Zahlen

Return Value

  • Gibt eine Ganzzahl zurück

Beispiele

Standardfälle

gcd('5','20')   --> returns 5
gcd('-5','0')   --> returns 5

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() {
	
		fillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
		fillInVariableMap.put(2, OMConverter.toObject("<OMOBJ><OMI>20</OMI></OMOBJ>"));
		fillInVariableMap.put(3, OMConverter.toObject("<OMOBJ><OMI>10</OMI></OMOBJ>"));
		fillInVariableMap.put(4, OMConverter.toObject("<OMOBJ><OMI>-5</OMI></OMOBJ>"));
		fillInVariableMap.put(5, OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));

		exerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
		exerciseVariableMap.put("b", OMConverter.toObject("<OMOBJ><OMI>20</OMI></OMOBJ>"));
		exerciseVariableMap.put("c", OMConverter.toObject("<OMOBJ><OMI>10</OMI></OMOBJ>"));
		exerciseVariableMap.put("d", OMConverter.toObject("<OMOBJ><OMI>-5</OMI></OMOBJ>"));
		exerciseVariableMap.put("e", OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));
	
}

@Test
public void testGCD1() {
	assertEquals(5, Evaluator.getNumberResult("gcd('5', '5')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCD2() {
	assertEquals(3, Evaluator.getNumberResult("gcd('3', '-9')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCD3() {
	assertEquals(3, Evaluator.getNumberResult("gcd('-3', '9')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCD4() {
	assertEquals(5, Evaluator.getNumberResult("gcd('-5', '-5')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCD5() {
	assertNotEquals(5, Evaluator.getNumberResult("gcd('5', '9')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCD6() {
	assertNotEquals(3, Evaluator.getNumberResult("gcd('-3', '7')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCDAtDefinition1() {
	assertEquals(55, Evaluator.getNumberResult("gcd('0', '55')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCDAtDefinition2() {
	assertEquals(11, Evaluator.getNumberResult("gcd('-11', '0')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCDAtDefinition3() {
	assertEquals(0, Evaluator.getNumberResult("gcd('0', '0')", exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test
public void testGCDWithInput1() {
	assertEquals(10, Evaluator.getNumberResult("gcd('[pos=2]', '[pos=3]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithInput2() {
	assertEquals(5, Evaluator.getNumberResult("gcd('[pos=3]', '[pos=4]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithInput3() {
	assertEquals(5, Evaluator.getNumberResult("gcd('[pos=1]', '[pos=4]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithVariables1() {
	assertEquals(10, Evaluator.getNumberResult("gcd('[var=b]', '[var=c]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithVariables2() {
	assertEquals(5, Evaluator.getNumberResult("gcd('[var=c]', '[var=d]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithVariables3() {
	assertEquals(5, Evaluator.getNumberResult("gcd('[var=a]', '[var=d]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithExpressions1() {
	assertEquals(5,
			Evaluator.getNumberResult("gcd('gcd(5, [var=c])', '[var=c]')", exerciseVariableMap, fillInVariableMap),
			0.0);
}

@Test
public void testGCDWithExpressions2() {
	assertEquals(5, Evaluator.getNumberResult("gcd('[var=c]', '(gcd([var=d], 5))')", exerciseVariableMap,
			fillInVariableMap), 0.0);
}

@Test
public void testGCDWithExpressions3() {
	assertEquals(5, Evaluator.getNumberResult("gcd(gcd('[var=e]',gcd('[var=e]','5')),gcd('[var=b]','[pos=5]'))",
			exerciseVariableMap, fillInVariableMap), 0.0);
}

@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testGCDWithWrongInputPointNumber() {
	Evaluator.getNumberResult("gcd('5.5','4')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testGCDWithWrongInputCharacter() {
	Evaluator.getNumberResult("gcd('a', '2')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testGCDWithOneArgument() {
	Evaluator.getNumberResult("gcd('7')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testGCDWithThreeArguments() {
	Evaluator.getNumberResult("gcd('4', '5', '7')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = UndefinedExerciseVariableException.class)
public void testGCDWithMissingExerciseVariable() {
	Evaluator.getNumberResult("gcd('[var=j]', '3')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = UndefinedFillInVariableException.class)
public void testGCDWithMissingInput() {
	Evaluator.getNumberResult("gcd('[pos=42]', '3')", exerciseVariableMap, fillInVariableMap);
	fail();
}

Hinweise