GCD
Zur Navigation springen
Zur Suche springen
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();
}