CompareTo

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

Beschreibung

Die Funktion compareTo bestimmt wie sehr zwei Zeichenketten lexikografisch voneinander abweichen. Die Funktion erwartet zwei Strings und gibt eine Zahl zurück.

Syntax

compareTo(Zeichenkette text1, Zeichenkette text2)

Parameter

  • zeichenkette1 - erste zu vergleichende Zeichenkette
  • zeichenkette2 - zweite zu vergleichende Zeichenkette

Return Value

  • Gibt eine Zahl zurück

Beispiele

compareTo('hallo','hallo')   --> returns 0
compareTo('hallo','JACK')   --> returns 30
compareTo('JACK','jack')   --> returns -32

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() {
	
		compareToFillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));

		compareToExerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
	
}

@Test
public void testCompareTo1() {
	assertEquals(0, Evaluator.getNumberResult("compareTo('Hallo','Hallo')", compareToExerciseVariableMap,
			compareToFillInVariableMap), 0.0);
}

@Test
public void testCompareTo2() {
	assertEquals(0, Evaluator.getNumberResult("compareTo('HAlLo','HAlLo')", compareToExerciseVariableMap,
			compareToFillInVariableMap), 0.0);
}

@Test
public void testCompareTo3() {
	assertTrue(0 < Evaluator.getNumberResult("compareTo('jack','JACK')", compareToExerciseVariableMap,
			compareToFillInVariableMap));
}

@Test
public void testCompareTo4() {
	assertTrue(0 < Evaluator.getNumberResult("compareTo('paluno','JACK')", compareToExerciseVariableMap,
			compareToFillInVariableMap));
}

@Test
public void testCompareTo5() {
	assertTrue(0 > Evaluator.getNumberResult("compareTo('JACK','paluno')", compareToExerciseVariableMap,
			compareToFillInVariableMap));
}

@Test
public void testCompareTo6() {
	assertNotEquals(0, Evaluator.getNumberResult("compareTo('2*x+2*y','2*(x+y)')", compareToExerciseVariableMap,
			compareToFillInVariableMap), 0.0);
}

@Test
public void testCompareToWithInput() {
	assertEquals(0, Evaluator.getNumberResult("compareTo('[pos=1]','test')", compareToExerciseVariableMap,
			compareToFillInVariableMap), 0.0);
}

@Test
public void testCompareToWithVariables() {
	assertEquals(0, Evaluator.getNumberResult("compareTo('[var=a]','test')", compareToExerciseVariableMap,
			compareToFillInVariableMap), 0.0);
}

@Test(expected = ParserException.class)
public void testCompareToWithWrongInputCharacter() {
	Evaluator.evaluate("compareTo(ab,'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCompareToWithOneArgument() {
	Evaluator.evaluate("compareTo('Hallo')", compareToExerciseVariableMap, compareToFillInVariableMap);
	fail();
}

@Test
public void testCompareToWithOneRationalAndOneTextArgument() {
	assertNotEquals(Evaluator.getNumberResult("compareTo(12, 'rational')", compareToExerciseVariableMap, compareToFillInVariableMap),0.0);
}

@Test
public void testCompareToWithTwoRationalArguments() {
	assertNotEquals(0, Evaluator.getNumberResult("compareTo(1, 2)", compareToExerciseVariableMap, compareToFillInVariableMap),0.0);
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCompareToWithThreeArguments() {
	Evaluator.evaluate("compareTo('hallo', 'hello', 'hillo')", compareToExerciseVariableMap,
			compareToFillInVariableMap);
	fail();
}

@Test(expected = UndefinedExerciseVariableException.class)
public void testCompareToWithMissingExcerciseVariable() {
	Evaluator.evaluate("compareTo('[var=j]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
	fail();
}

@Test(expected = UndefinedFillInVariableException.class)
public void testCompareToWithMissingInput() {
	Evaluator.evaluate("compareTo('[pos=42]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
	fail();
}

Hinweise

  • Die Funktion unterscheidet zwischen Groß- und Kleinschreibung.
  • Steht der erste String lexikografisch vor dem zweiten String, ist die Rückgabe negativ. Umgekehrt ist die Rückgabe entsprechend positiv.