CompareTo: 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("test"));
compareToFillInVariableMap.put(1, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));


  placeExerciseVariable("a", OMParser.parse("test"));
compareToExerciseVariableMap.put("a", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
}
}
 
@Test  
@Test
public void testCompareTo() {
public void testCompareTo1() {
  assertTrue(0 == getEvaluator().getNumberResult("compareTo('Hallo','Hallo')"));
assertEquals(0, Evaluator.getNumberResult("compareTo('Hallo','Hallo')", compareToExerciseVariableMap,
  assertTrue(0 == getEvaluator().getNumberResult("compareTo('HAlLo','HAlLo')"));
compareToFillInVariableMap), 0.0);
  assertTrue(0 < getEvaluator().getNumberResult("compareTo('jack','JACK')"));
}
  assertTrue(0 < getEvaluator().getNumberResult("compareTo('paluno','JACK')"));
 
  assertTrue(0 > getEvaluator().getNumberResult("compareTo('JACK','paluno')"));
@Test
  assertTrue(0 != getEvaluator().getNumberResult("compareTo('2x+2y','2*(x+y)')"));
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  
@Test
public void testCompareToWithInput() {
public void testCompareToWithInput() {
  assertTrue(0 == getEvaluator().getNumberResult("compareTo('[pos=1]','test')"));
assertEquals(0, Evaluator.getNumberResult("compareTo('[pos=1]','test')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
}
 
@Test  
@Test
public void testCompareToWithVariables() {
public void testCompareToWithVariables() {
  assertTrue(0 == getEvaluator().getNumberResult("compareTo('[var=a]','test')"));
assertEquals(0, Evaluator.getNumberResult("compareTo('[var=a]','test')", compareToExerciseVariableMap,
compareToFillInVariableMap), 0.0);
}
}
 
@Test (expected=InvalidEvaluatorFunctionArgumentException.class)
@Test(expected = ParserException.class)
public void testCompareToWithWrongInputCharacter() {
public void testCompareToWithWrongInputCharacter() {
  getEvaluator().getNumberResult("compareTo(a,'test')");
Evaluator.evaluate("compareTo(ab,'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
}
 
@Test (expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCompareToWithOneArgument() {
public void testCompareToWithOneArgument() {
  getEvaluator().getNumberResult("compareTo('Hallo')");
Evaluator.evaluate("compareTo('Hallo')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
}
 
@Test (expected=UnsupportedEvaluatorFunctionException.class)
@Test
public void testCompareToWithOneRationalAndOneTextArgument() {
public void testCompareToWithOneRationalAndOneTextArgument() {
  getEvaluator().getNumberResult("compareTo(12, 'rational')");
assertNotEquals(Evaluator.getNumberResult("compareTo(12, 'rational')", compareToExerciseVariableMap, compareToFillInVariableMap),0.0);
}
}
 
@Test (expected=UnsupportedEvaluatorFunctionException.class)
@Test
public void testCompareToWithTwoRationalArguments() {
public void testCompareToWithTwoRationalArguments() {
  getEvaluator().getNumberResult("compareTo(1, 2)");
assertNotEquals(0, Evaluator.getNumberResult("compareTo(1, 2)", compareToExerciseVariableMap, compareToFillInVariableMap),0.0);
}
}
 
@Test (expected=UnsupportedEvaluatorFunctionException.class)
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testCompareToWithThreeArguments() {
public void testCompareToWithThreeArguments() {
  getEvaluator().getNumberResult("compareTo('hallo', 'hello', 'hillo')");
Evaluator.evaluate("compareTo('hallo', 'hello', 'hillo')", compareToExerciseVariableMap,
compareToFillInVariableMap);
fail();
}
}
 
@Test (expected=UndefinedExerciseVariableException.class)
@Test(expected = UndefinedExerciseVariableException.class)
public void testCompareToWithMissingExcerciseVariable() {
public void testCompareToWithMissingExcerciseVariable() {
  getEvaluator().getNumberResult("compareTo('[var=j]', 'test')");
Evaluator.evaluate("compareTo('[var=j]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
}
 
@Test (expected=UndefinedFillInVariableException.class)
@Test(expected = UndefinedFillInVariableException.class)
public void testCompareToWithMissingInput() {
public void testCompareToWithMissingInput() {
  getEvaluator().getNumberResult("compareTo('[pos=42]', 'test')");
Evaluator.evaluate("compareTo('[pos=42]', 'test')", compareToExerciseVariableMap, compareToFillInVariableMap);
fail();
}
}
</syntaxhighlight>
</syntaxhighlight>
=== Hinweise ===
=== Hinweise ===
* Die Funktion unterscheidet zwischen Groß- und Kleinschreibung.
* Die Funktion unterscheidet zwischen Groß- und Kleinschreibung.

Version vom 18. September 2017, 10:37 Uhr

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(String zeichenkette1, String zeichenkette2)

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.