NumberOfVariables
Zur Navigation springen
Zur Suche springen
Beschreibung
Die Funktion numberOfVariables zählt die Variablen in einem Ausdruck. Sie erwartet einen String und gibt einen numerischen Ausdruck zurück.
Syntax
numberOfVariables(Expression term)
Parameter
- term - der Ausdruck dessen Variablen gezählt werden sollen
Return Value
- Gibt einen numerischen Ausdruck zurück.
Beispiele
numberOfVariables('2x+3a-2b') --> returns 3
JUnit Tests
@Before
public void beforeTest() {
placeFillInVariable(1, OMParser.parse("0"));
placeFillInVariable(2, OMParser.parse("3"));
placeFillInVariable(5, OMParser.parse("x"));
placeExerciseVariable("a", OMParser.parse("x"));
placeExerciseVariable("b", OMParser.parse("3"));
}
@Test
public void testNumberOfVariables() {
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2x+3a-x+2b')"));
assertTrue(1 == getEvaluator().getNumberResult("numberOfVariables('2x+3x-x+2x')"));
assertTrue(4 == getEvaluator().getNumberResult("numberOfVariables('sin(i)*E^(e)+ q/sqrt(s)')"));
assertTrue(0 == getEvaluator().getNumberResult("numberOfVariables('5')"));
}
@Test
public void testNumberOfVariablesWithInput() {
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2x+3a-x+2b')"));
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2[pos=5]+3a-[pos=5]+2b')"));
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2[pos=5]+3a-[pos=5]+2b')"));
assertTrue(2 == getEvaluator().getNumberResult("numberOfVariables('2[pos=5]+3[pos=5]-[pos=5]+2b')"));
}
@Test
public void testNumberOfVariablesWithVariables() {
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2x+3a-x+2b')"));
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2[var=a]+3a-[var=a]+2b')"));
assertTrue(3 == getEvaluator().getNumberResult("numberOfVariables('2[var=a]+3a-[var=a]+2b')"));
assertTrue(2 == getEvaluator().getNumberResult("numberOfVariables('2[var=a]+3[var=a]-[var=a]+2b')"));
}
@Test
public void testNumberOfVariablesWithONECharacter(){
assertTrue(1 == getEvaluator().getNumberResult("numberOfVariables(a)"));
}
@Test(expected=InvalidCASFunctionArgumentException.class)
public void testNumberOfVariablesWithEmptyStringArguments(){
getEvaluator().getNumberResult("numberOfVariables('')");
}
@Test(expected=InvalidCASFunctionArgumentException.class)
public void testNumberOfVariablesWithEmptyArgument(){
getEvaluator().getNumberResult("numberOfVariables()");
}
@Test(expected=UndefinedExerciseVariableException.class)
public void testNumberOfVariablesWithMissingExerciseVariable(){
getEvaluator().getNumberResult("numberOfVariables('[var=j]')");
}
@Test(expected=UndefinedFillInVariableException.class)
public void testNumberOfVariablesWithMissingInput(){
getEvaluator().getNumberResult("numberOfVariables('[pos=42]')");
}