Skip to content

Commit

Permalink
gs-cv: LetterPairSimilarity: added a test class and checked for null/…
Browse files Browse the repository at this point in the history
…empty strings
  • Loading branch information
plassalas committed Oct 12, 2017
1 parent a38f958 commit 9b8b548
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public static void main(String[] args) {
}

public static double compareStrings(String string1, String string2) {
if (null == string1 || null == string2)
throw new IllegalArgumentException("LetterPairSimilarity requires two not null strings");
if (string1.equals(string2))
return 1d;
if (string1.isEmpty())
return 0;
if (string2.isEmpty())
return 0;

List<String> pairs1 = wordLetterPairs(string1.toLowerCase());
List<String> pairs2 = wordLetterPairs(string2.toLowerCase());
int intersection = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.genericsystem.cv.utils;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class LetterPairSimilarityTest {

@Test
public void compareStrings() {
String ref = "healed";
assertEquals(LetterPairSimilarity.compareStrings("", ""), 1d, 0.0);
assertEquals(LetterPairSimilarity.compareStrings("kitten", ""), 0, 0.0);
assertEquals(LetterPairSimilarity.compareStrings("", "kitten"), 0, 0.0);
assertEquals(LetterPairSimilarity.compareStrings(ref, ref), 1d, 0.0);
assertEquals(LetterPairSimilarity.compareStrings(ref, "sealed"), 0.800, 0.001);
assertEquals(LetterPairSimilarity.compareStrings(ref, "healthy"), 0.545, 0.001);
assertEquals(LetterPairSimilarity.compareStrings(ref, "heard"), 0.444, 0.001);
assertEquals(LetterPairSimilarity.compareStrings(ref, "herded"), 0.400, 0.001);
assertEquals(LetterPairSimilarity.compareStrings(ref, "help"), 0.250, 0.001);
assertEquals(LetterPairSimilarity.compareStrings(ref, "sold"), 0, 0.001);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void compareStringsNullString() {
LetterPairSimilarity.compareStrings("kitten", null);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void compareStringsNullString2() {
LetterPairSimilarity.compareStrings(null, "kitten");
}
}

0 comments on commit 9b8b548

Please sign in to comment.