You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ch...@apache.org on 2017/01/24 12:31:00 UTC
[19/27] [text] TEXT-61: context.xml and test packages
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/diff/ReplacementsFinderTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/diff/ReplacementsFinderTest.java b/src/test/java/org/apache/commons/text/beta/diff/ReplacementsFinderTest.java
new file mode 100644
index 0000000..f5a8277
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/diff/ReplacementsFinderTest.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.diff;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+/**
+ * Tests for the ReplacementsFinder.
+ */
+@RunWith(Parameterized.class)
+public class ReplacementsFinderTest {
+ private SimpleHandler handler = null;
+ private final String left;
+ private final String right;
+ private final int skipped;
+ private final Character[] from;
+ private final Character[] to;
+ @Before
+ public void setUp() {
+ handler = new SimpleHandler();
+ }
+ @Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(new Object[][] {
+ {
+ "branco",
+ "blanco",
+ 1,
+ new Character[] {'r'},
+ new Character[] {'l'}},
+ {
+ "test the blocks before you use it",
+ "try the blocks before you put it",
+ 25,
+ new Character[] {'e', 's', 't', 's', 'e'},
+ new Character[] {'r', 'y', 'p', 't'}
+ }
+ });
+ }
+ public ReplacementsFinderTest(final String left, final String right, final int skipped,
+ final Character[] from, final Character[] to) {
+ this.left = left;
+ this.right = right;
+ this.skipped = skipped;
+ this.from = from;
+ this.to = to;
+ }
+ @Test
+ public void testReplacementsHandler() {
+ final StringsComparator sc = new StringsComparator(left, right);
+ final ReplacementsFinder<Character> replacementFinder = new ReplacementsFinder<>(handler);
+ sc.getScript().visit(replacementFinder);
+ assertEquals("Skipped characters do not match", skipped, handler.getSkipped());
+ assertArrayEquals("From characters do not match", from,
+ handler.getFrom().toArray(new Character[0]));
+ assertArrayEquals("To characters do not match", to,
+ handler.getTo().toArray(new Character[0]));
+ }
+ // Helper RecplacementsHandler implementation for testing
+ private class SimpleHandler implements ReplacementsHandler<Character> {
+ private int skipped;
+ private final List<Character> from;
+ private final List<Character> to;
+ public SimpleHandler() {
+ skipped = 0;
+ from = new ArrayList<>();
+ to = new ArrayList<>();
+ }
+ public int getSkipped() {
+ return skipped;
+ }
+ public List<Character> getFrom() {
+ return from;
+ }
+ public List<Character> getTo() {
+ return to;
+ }
+ @Override
+ public void handleReplacement(final int skipped, final List<Character> from, final List<Character> to) {
+ this.skipped += skipped;
+ this.from.addAll(from);
+ this.to.addAll(to);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/diff/StringsComparatorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/diff/StringsComparatorTest.java b/src/test/java/org/apache/commons/text/beta/diff/StringsComparatorTest.java
new file mode 100644
index 0000000..cb0fd3f
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/diff/StringsComparatorTest.java
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.diff;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+/**
+ * Tests for the StringsComparator.
+ */
+public class StringsComparatorTest {
+ private List<String> before;
+ private List<String> after;
+ private int[] length;
+ private int[] lcs;
+ @Test
+ public void testLength() {
+ for (int i = 0; i < before.size(); ++i) {
+ final StringsComparator comparator = new StringsComparator(before.get(i), after.get(i));
+ Assert.assertEquals(length[i], comparator.getScript().getModifications());
+ }
+ }
+ @Test
+ public void testLongestCommonSubsequence() {
+ for (int i = 0; i < before.size(); ++i) {
+ final StringsComparator comparator = new StringsComparator(before.get(i), after.get(i));
+ Assert.assertEquals(lcs[i], comparator.getScript().getLCSLength());
+ }
+ }
+ @Test
+ public void testExecution() {
+ for (int i = 0; i < before.size(); ++i) {
+ final ExecutionVisitor<Character> ev = new ExecutionVisitor<>();
+ new StringsComparator(before.get(i), after.get(i)).getScript().visit(ev);
+ Assert.assertEquals(after.get(i), ev.getString());
+ }
+ }
+ private class ExecutionVisitor<T> implements CommandVisitor<T> {
+ private final StringBuilder v;
+ public ExecutionVisitor() {
+ v = new StringBuilder();
+ }
+ @Override
+ public void visitInsertCommand(final T object) {
+ v.append(object);
+ }
+ @Override
+ public void visitKeepCommand(final T object) {
+ v.append(object);
+ }
+ @Override
+ public void visitDeleteCommand(final T object) {
+ }
+ public String getString() {
+ return v.toString();
+ }
+ }
+ @Before
+ public void setUp() {
+ before = Arrays.asList(
+ "bottle",
+ "nematode knowledge",
+ "",
+ "aa",
+ "prefixed string",
+ "ABCABBA",
+ "glop glop",
+ "coq",
+ "spider-man");
+ after = Arrays.asList(
+ "noodle",
+ "empty bottle",
+ "",
+ "C",
+ "prefix",
+ "CBABAC",
+ "pas glop pas glop",
+ "ane",
+ "klingon");
+ length = new int[] {
+ 6,
+ 16,
+ 0,
+ 3,
+ 9,
+ 5,
+ 8,
+ 6,
+ 13
+ };
+ lcs = new int[] {
+ 3,
+ 7,
+ 0,
+ 0,
+ 6,
+ 4,
+ 9,
+ 0,
+ 2
+ };
+ }
+ @After
+ public void tearDown() {
+ before = null;
+ after = null;
+ length = null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/CosineDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/CosineDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/CosineDistanceTest.java
new file mode 100644
index 0000000..65cb51a
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/CosineDistanceTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link CosineSimilarity}.
+ */
+public class CosineDistanceTest {
+
+ /**
+ * Cosine distance under test.
+ */
+ private static CosineDistance cosineDistance;
+
+ /**
+ * Creates the cosine distance object used throughout the tests.
+ */
+ @BeforeClass
+ public static void setUp() {
+ cosineDistance = new CosineDistance();
+ }
+
+ /**
+ * Tests the cosine distance with several inputs.
+ */
+ @Test
+ public void testCosineDistance() {
+ assertEquals(Double.valueOf(0.5d), roundValue(cosineDistance.apply("the house", "da house")));
+ assertEquals(Double.valueOf(0.0d), roundValue(cosineDistance.apply("AB", "AB")));
+ assertEquals(Double.valueOf(1.0d), roundValue(cosineDistance.apply("AB", "BA")));
+ assertEquals(Double.valueOf(0.08d), roundValue(cosineDistance.apply(
+ "the boy was from tamana shi, kumamoto ken, and the girl was from rio de janeiro, rio",
+ "the boy was from tamana shi, kumamoto, and the boy was from rio de janeiro, rio de janeiro")));
+ }
+
+ // --- Utility methods
+
+ /**
+ * Rounds up a value.
+ *
+ * @param value a value
+ * @return rounded up value
+ */
+ private Double roundValue(final Double value) {
+ return (Double) new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/FuzzyScoreTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/FuzzyScoreTest.java b/src/test/java/org/apache/commons/text/beta/similarity/FuzzyScoreTest.java
new file mode 100644
index 0000000..e1401ca
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/FuzzyScoreTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Locale;
+
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link FuzzyScore}.
+ */
+public class FuzzyScoreTest {
+
+ private static final FuzzyScore ENGLISH_SCORE = new FuzzyScore(Locale.ENGLISH);
+
+ @Test
+ public void testGetFuzzyScore() throws Exception {
+ assertEquals(0, (int) ENGLISH_SCORE.fuzzyScore("", ""));
+ assertEquals(0, (int) ENGLISH_SCORE.fuzzyScore("Workshop", "b"));
+ assertEquals(1, (int) ENGLISH_SCORE.fuzzyScore("Room", "o"));
+ assertEquals(1, (int) ENGLISH_SCORE.fuzzyScore("Workshop", "w"));
+ assertEquals(2, (int) ENGLISH_SCORE.fuzzyScore("Workshop", "ws"));
+ assertEquals(4, (int) ENGLISH_SCORE.fuzzyScore("Workshop", "wo"));
+ assertEquals(3, (int) ENGLISH_SCORE.fuzzyScore(
+ "Apache Software Foundation", "asf"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetFuzzyScore_StringNullLocale() throws Exception {
+ ENGLISH_SCORE.fuzzyScore("not null", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetFuzzyScore_NullStringLocale() throws Exception {
+ ENGLISH_SCORE.fuzzyScore(null, "not null");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetFuzzyScore_NullNullLocale() throws Exception {
+ ENGLISH_SCORE.fuzzyScore(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testMissingLocale() throws Exception {
+ new FuzzyScore((Locale) null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/HammingDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/HammingDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/HammingDistanceTest.java
new file mode 100644
index 0000000..d38981f
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/HammingDistanceTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link HammingDistance}.
+ */
+public class HammingDistanceTest {
+
+ private static HammingDistance distance;
+
+ @BeforeClass
+ public static void setUp() {
+ distance = new HammingDistance();
+ }
+
+ @Test
+ public void testHammingDistance() {
+ assertEquals(Integer.valueOf(0), distance.apply("", ""));
+ assertEquals(Integer.valueOf(0), distance.apply("pappa", "pappa"));
+ assertEquals(Integer.valueOf(1), distance.apply("papaa", "pappa"));
+ assertEquals(Integer.valueOf(3), distance.apply("karolin", "kathrin"));
+ assertEquals(Integer.valueOf(3), distance.apply("karolin", "kerstin"));
+ assertEquals(Integer.valueOf(2), distance.apply("1011101", "1001001"));
+ assertEquals(Integer.valueOf(3), distance.apply("2173896", "2233796"));
+ assertEquals(Integer.valueOf(2), distance.apply("ATCG", "ACCC"));
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testHammingDistance_nullLeftValue() {
+ distance.apply(null, "");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testHammingDistance_nullRightValue() {
+ distance.apply("", null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/JaccardDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/JaccardDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/JaccardDistanceTest.java
new file mode 100644
index 0000000..6e74dc3
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/JaccardDistanceTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link JaccardDistance}.
+ */
+public class JaccardDistanceTest {
+
+ private static JaccardDistance classBeingTested;
+
+ @BeforeClass
+ public static void setUp() {
+ classBeingTested = new JaccardDistance();
+ }
+
+ @Test
+ public void testGettingJaccardDistance() {
+ assertEquals(1.00d, classBeingTested.apply("", ""), 0.0d);
+ assertEquals(1.00d, classBeingTested.apply("left", ""), 0.0d);
+ assertEquals(1.00d, classBeingTested.apply("", "right"), 0.0d);
+ assertEquals(0.25d, classBeingTested.apply("frog", "fog"), 0.0d);
+ assertEquals(1.00d, classBeingTested.apply("fly", "ant"), 0.0d);
+ assertEquals(0.78d, classBeingTested.apply("elephant", "hippo"), 0.0d);
+ assertEquals(0.36d, classBeingTested.apply("ABC Corporation", "ABC Corp"), 0.0d);
+ assertEquals(0.24d, classBeingTested.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."), 0.0d);
+ assertEquals(0.11d, classBeingTested.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"), 0.0d);
+ assertEquals(0.10d, classBeingTested.apply("PENNSYLVANIA", "PENNCISYLVNIA"), 0.0d);
+ assertEquals(0.87d, classBeingTested.apply("left", "right"), 0.0d);
+ assertEquals(0.87d, classBeingTested.apply("leettteft", "ritttght"), 0.0d);
+ assertEquals(0.0d, classBeingTested.apply("the same string", "the same string"), 0.0d);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingJaccardDistanceNullNull() throws Exception {
+ classBeingTested.apply(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingJaccardDistanceStringNull() throws Exception {
+ classBeingTested.apply(" ", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingJaccardDistanceNullString() throws Exception {
+ classBeingTested.apply(null, "right");
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/JaccardSimilarityTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/JaccardSimilarityTest.java b/src/test/java/org/apache/commons/text/beta/similarity/JaccardSimilarityTest.java
new file mode 100644
index 0000000..8841cfe
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/JaccardSimilarityTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link JaccardSimilarity}.
+ */
+public class JaccardSimilarityTest {
+
+ private static JaccardSimilarity classBeingTested;
+
+ @BeforeClass
+ public static void setUp() {
+ classBeingTested = new JaccardSimilarity();
+ }
+
+ @Test
+ public void testGettingJaccardSimilarity() {
+ assertEquals(0.00d, classBeingTested.apply("", ""), 0.0d);
+ assertEquals(0.00d, classBeingTested.apply("left", ""), 0.0d);
+ assertEquals(0.00d, classBeingTested.apply("", "right"), 0.0d);
+ assertEquals(0.75d, classBeingTested.apply("frog", "fog"), 0.0d);
+ assertEquals(0.00d, classBeingTested.apply("fly", "ant"), 0.0d);
+ assertEquals(0.22d, classBeingTested.apply("elephant", "hippo"), 0.0d);
+ assertEquals(0.64d, classBeingTested.apply("ABC Corporation", "ABC Corp"), 0.0d);
+ assertEquals(0.76d, classBeingTested.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."), 0.0d);
+ assertEquals(0.89d, classBeingTested.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"), 0.0d);
+ assertEquals(0.9d, classBeingTested.apply("PENNSYLVANIA", "PENNCISYLVNIA"), 0.0d);
+ assertEquals(0.13d, classBeingTested.apply("left", "right"), 0.0d);
+ assertEquals(0.13d, classBeingTested.apply("leettteft", "ritttght"), 0.0d);
+ assertEquals(1.0d, classBeingTested.apply("the same string", "the same string"), 0.0d);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingJaccardSimilarityNullNull() throws Exception {
+ classBeingTested.apply(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingJaccardSimilarityStringNull() throws Exception {
+ classBeingTested.apply(" ", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingJaccardSimilarityNullString() throws Exception {
+ classBeingTested.apply(null, "right");
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/JaroWinklerDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/JaroWinklerDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/JaroWinklerDistanceTest.java
new file mode 100644
index 0000000..2210426
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/JaroWinklerDistanceTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link JaroWinklerDistance}.
+ */
+public class JaroWinklerDistanceTest {
+
+ private static JaroWinklerDistance distance;
+
+ @BeforeClass
+ public static void setUp() {
+ distance = new JaroWinklerDistance();
+ }
+
+ @Test
+ public void testGetJaroWinklerDistance_StringString() {
+ assertEquals(0.93d, (double) distance.apply("frog", "fog"), 0.0d);
+ assertEquals(0.0d, (double) distance.apply("fly", "ant"), 0.0d);
+ assertEquals(0.44d, (double) distance.apply("elephant", "hippo"), 0.0d);
+ assertEquals(0.93d, (double) distance.apply("ABC Corporation", "ABC Corp"), 0.0d);
+ assertEquals(0.95d, (double) distance.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."), 0.0d);
+ assertEquals(0.92d, (double) distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"), 0.0d);
+ assertEquals(0.88d, (double) distance.apply("PENNSYLVANIA", "PENNCISYLVNIA"), 0.0d);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetJaroWinklerDistance_NullNull() throws Exception {
+ distance.apply(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetJaroWinklerDistance_StringNull() throws Exception {
+ distance.apply(" ", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetJaroWinklerDistance_NullString() throws Exception {
+ distance.apply(null, "clear");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDetailedDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDetailedDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDetailedDistanceTest.java
new file mode 100644
index 0000000..dc814bb
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDetailedDistanceTest.java
@@ -0,0 +1,402 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class LevenshteinDetailedDistanceTest {
+
+ private static final LevenshteinDetailedDistance UNLIMITED_DISTANCE = new LevenshteinDetailedDistance();
+
+ @Test
+ public void testGetLevenshteinDetailedDistance_StringString() {
+ LevenshteinResults result = UNLIMITED_DISTANCE.apply("", "");
+ assertEquals(0, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("", "a");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(1, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("aaapppp", "");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(7, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("frog", "fog");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(1, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("fly", "ant");
+ assertEquals(3, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(3, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("elephant", "hippo");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(3, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("hippo", "elephant");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("hippo", "zzzzzzzz");
+ assertEquals(8, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(5, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("zzzzzzzz", "hippo");
+ assertEquals(8, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(3, (int) result.getDeleteCount());
+ assertEquals(5, (int) result.getSubstituteCount());
+
+ result = UNLIMITED_DISTANCE.apply("hello", "hallo");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+ }
+
+ @Test
+ public void testEquals() {
+ final LevenshteinDetailedDistance classBeingTested = new LevenshteinDetailedDistance();
+ LevenshteinResults actualResult = classBeingTested.apply("hello", "hallo");
+ LevenshteinResults expectedResult = new LevenshteinResults(1, 0, 0, 1);
+ assertEquals(actualResult, expectedResult);
+
+ actualResult = classBeingTested.apply("zzzzzzzz", "hippo");
+ expectedResult = new LevenshteinResults(8, 0, 3, 5);
+ assertEquals(actualResult, expectedResult);
+ assertEquals(actualResult, actualResult); //intentionally added
+
+ actualResult = classBeingTested.apply("", "");
+ expectedResult = new LevenshteinResults(0, 0, 0, 0);
+ assertEquals(actualResult, expectedResult);
+ }
+
+ @Test
+ public void testHashCode() {
+ final LevenshteinDetailedDistance classBeingTested = new LevenshteinDetailedDistance();
+ LevenshteinResults actualResult = classBeingTested.apply("aaapppp", "");
+ LevenshteinResults expectedResult = new LevenshteinResults(7, 0, 7, 0);
+ assertEquals(actualResult.hashCode(), expectedResult.hashCode());
+
+ actualResult = classBeingTested.apply("frog", "fog");
+ expectedResult = new LevenshteinResults(1, 0, 1, 0);
+ assertEquals(actualResult.hashCode(), expectedResult.hashCode());
+
+ actualResult = classBeingTested.apply("elephant", "hippo");
+ expectedResult = new LevenshteinResults(7, 0, 3, 4);
+ assertEquals(actualResult.hashCode(), expectedResult.hashCode());
+ }
+
+ @Test
+ public void testToString() {
+ final LevenshteinDetailedDistance classBeingTested = new LevenshteinDetailedDistance();
+ LevenshteinResults actualResult = classBeingTested.apply("fly", "ant");
+ LevenshteinResults expectedResult = new LevenshteinResults(3, 0, 0, 3);
+ assertEquals(actualResult.toString(), expectedResult.toString());
+
+ actualResult = classBeingTested.apply("hippo", "elephant");
+ expectedResult = new LevenshteinResults(7, 3, 0, 4);
+ assertEquals(actualResult.toString(), expectedResult.toString());
+
+ actualResult = classBeingTested.apply("", "a");
+ expectedResult = new LevenshteinResults(1, 1, 0, 0);
+ assertEquals(actualResult.toString(), expectedResult.toString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDetailedDistance_NullString() throws Exception {
+ UNLIMITED_DISTANCE.apply("a", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDetailedDistance_StringNull() throws Exception {
+ UNLIMITED_DISTANCE.apply(null, "a");
+ }
+
+ @Test
+ public void testGetLevenshteinDetailedDistance_StringStringInt() {
+
+ LevenshteinResults result = new LevenshteinDetailedDistance(0).apply("", "");
+
+ assertEquals(0, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(8).apply("aaapppp", "");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(7, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(7).apply("aaapppp", "");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(7, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(6).apply("aaapppp", "");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(0).apply("b", "a");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(0).apply("a", "b");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(0).apply("aa", "aa");
+ assertEquals(0, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(2).apply("aa", "aa");
+ assertEquals(0, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(2).apply("aaa", "bbb");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(3).apply("aaa", "bbb");
+ assertEquals(3, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(3, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(10).apply("aaaaaa", "b");
+ assertEquals(6, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(5, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(8).apply("aaapppp", "b");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(6, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(4).apply("a", "bbb");
+ assertEquals(3, (int) result.getDistance());
+ assertEquals(2, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(7).apply("aaapppp", "b");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(6, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(3).apply("a", "bbb");
+ assertEquals(3, (int) result.getDistance());
+ assertEquals(2, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(2).apply("a", "bbb");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(2).apply("bbb", "a");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(6).apply("aaapppp", "b");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(1).apply("a", "bbb");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(1).apply("bbb", "a");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(1).apply("12345", "1234567");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(1).apply("1234567", "12345");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(1).apply("frog", "fog");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(1, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(3).apply("fly", "ant");
+ assertEquals(3, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(3, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(7).apply("elephant", "hippo");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(3, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(6).apply("elephant", "hippo");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(7).apply("hippo", "elephant");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(7).apply("hippo", "elephant");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(6).apply("hippo", "elephant");
+ assertEquals(-1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(8).apply("hippo", "zzzzzzzz");
+ assertEquals(8, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(5, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(8).apply("zzzzzzzz", "hippo");
+ assertEquals(8, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(3, (int) result.getDeleteCount());
+ assertEquals(5, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(1).apply("hello", "hallo");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("frog", "fog");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(1, (int) result.getDeleteCount());
+ assertEquals(0, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("fly", "ant");
+ assertEquals(3, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(3, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("elephant", "hippo");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(3, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("hippo", "elephant");
+ assertEquals(7, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(4, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("hippo", "zzzzzzzz");
+ assertEquals(8, (int) result.getDistance());
+ assertEquals(3, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(5, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("zzzzzzzz", "hippo");
+ assertEquals(8, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(3, (int) result.getDeleteCount());
+ assertEquals(5, (int) result.getSubstituteCount());
+
+ result = new LevenshteinDetailedDistance(Integer.MAX_VALUE).apply("hello", "hallo");
+ assertEquals(1, (int) result.getDistance());
+ assertEquals(0, (int) result.getInsertCount());
+ assertEquals(0, (int) result.getDeleteCount());
+ assertEquals(1, (int) result.getSubstituteCount());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDetailedDistance_NullStringInt() throws Exception {
+ UNLIMITED_DISTANCE.apply(null, "a");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDetailedDistance_StringNullInt() throws Exception {
+ UNLIMITED_DISTANCE.apply("a", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testConstructorWithNegativeThreshold() throws Exception {
+ new LevenshteinDetailedDistance(-1);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDistanceTest.java
new file mode 100644
index 0000000..cc69195
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/LevenshteinDistanceTest.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link LevenshteinDistance}.
+ */
+public class LevenshteinDistanceTest {
+
+ private static final LevenshteinDistance UNLIMITED_DISTANCE = new LevenshteinDistance();
+
+ @Test
+ public void testGetLevenshteinDistance_StringString() {
+ assertEquals(0, (int) UNLIMITED_DISTANCE.apply("", ""));
+ assertEquals(1, (int) UNLIMITED_DISTANCE.apply("", "a"));
+ assertEquals(7, (int) UNLIMITED_DISTANCE.apply("aaapppp", ""));
+ assertEquals(1, (int) UNLIMITED_DISTANCE.apply("frog", "fog"));
+ assertEquals(3, (int) UNLIMITED_DISTANCE.apply("fly", "ant"));
+ assertEquals(7, (int) UNLIMITED_DISTANCE.apply("elephant", "hippo"));
+ assertEquals(7, (int) UNLIMITED_DISTANCE.apply("hippo", "elephant"));
+ assertEquals(8, (int) UNLIMITED_DISTANCE.apply("hippo", "zzzzzzzz"));
+ assertEquals(8, (int) UNLIMITED_DISTANCE.apply("zzzzzzzz", "hippo"));
+ assertEquals(1, (int) UNLIMITED_DISTANCE.apply("hello", "hallo"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDistance_NullString() throws Exception {
+ UNLIMITED_DISTANCE.apply("a", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDistance_StringNull() throws Exception {
+ UNLIMITED_DISTANCE.apply(null, "a");
+ }
+
+ @Test
+ public void testGetLevenshteinDistance_StringStringInt() {
+ // empty strings
+ assertEquals(0, (int) new LevenshteinDistance(0).apply("", ""));
+ assertEquals(7, (int) new LevenshteinDistance(8).apply("aaapppp", ""));
+ assertEquals(7, (int) new LevenshteinDistance(7).apply("aaapppp", ""));
+ assertEquals(-1, (int) new LevenshteinDistance(6).apply("aaapppp", ""));
+
+ // unequal strings, zero threshold
+ assertEquals(-1, (int) new LevenshteinDistance(0).apply("b", "a"));
+ assertEquals(-1, (int) new LevenshteinDistance(0).apply("a", "b"));
+
+ // equal strings
+ assertEquals(0, (int) new LevenshteinDistance(0).apply("aa", "aa"));
+ assertEquals(0, (int) new LevenshteinDistance(2).apply("aa", "aa"));
+
+ // same length
+ assertEquals(-1, (int) new LevenshteinDistance(2).apply("aaa", "bbb"));
+ assertEquals(3, (int) new LevenshteinDistance(3).apply("aaa", "bbb"));
+
+ // big stripe
+ assertEquals(6, (int) new LevenshteinDistance(10).apply("aaaaaa", "b"));
+
+ // distance less than threshold
+ assertEquals(7, (int) new LevenshteinDistance(8).apply("aaapppp", "b"));
+ assertEquals(3, (int) new LevenshteinDistance(4).apply("a", "bbb"));
+
+ // distance equal to threshold
+ assertEquals(7, (int) new LevenshteinDistance(7).apply("aaapppp", "b"));
+ assertEquals(3, (int) new LevenshteinDistance(3).apply("a", "bbb"));
+
+ // distance greater than threshold
+ assertEquals(-1, (int) new LevenshteinDistance(2).apply("a", "bbb"));
+ assertEquals(-1, (int) new LevenshteinDistance(2).apply("bbb", "a"));
+ assertEquals(-1, (int) new LevenshteinDistance(6).apply("aaapppp", "b"));
+
+ // stripe runs off array, strings not similar
+ assertEquals(-1, (int) new LevenshteinDistance(1).apply("a", "bbb"));
+ assertEquals(-1, (int) new LevenshteinDistance(1).apply("bbb", "a"));
+
+ // stripe runs off array, strings are similar
+ assertEquals(-1, (int) new LevenshteinDistance(1).apply("12345", "1234567"));
+ assertEquals(-1, (int) new LevenshteinDistance(1).apply("1234567", "12345"));
+
+ // old getLevenshteinDistance test cases
+ assertEquals(1, (int) new LevenshteinDistance(1).apply("frog", "fog"));
+ assertEquals(3, (int) new LevenshteinDistance(3).apply("fly", "ant"));
+ assertEquals(7, (int) new LevenshteinDistance(7).apply("elephant", "hippo"));
+ assertEquals(-1, (int) new LevenshteinDistance(6).apply("elephant", "hippo"));
+ assertEquals(7, (int) new LevenshteinDistance(7).apply("hippo", "elephant"));
+ assertEquals(-1, (int) new LevenshteinDistance(6).apply("hippo", "elephant"));
+ assertEquals(8, (int) new LevenshteinDistance(8).apply("hippo", "zzzzzzzz"));
+ assertEquals(8, (int) new LevenshteinDistance(8).apply("zzzzzzzz", "hippo"));
+ assertEquals(1, (int) new LevenshteinDistance(1).apply("hello", "hallo"));
+
+ assertEquals(1,
+ (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("frog", "fog"));
+ assertEquals(3, (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("fly", "ant"));
+ assertEquals(7,
+ (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("elephant", "hippo"));
+ assertEquals(7,
+ (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("hippo", "elephant"));
+ assertEquals(8,
+ (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("hippo", "zzzzzzzz"));
+ assertEquals(8,
+ (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("zzzzzzzz", "hippo"));
+ assertEquals(1,
+ (int) new LevenshteinDistance(Integer.MAX_VALUE).apply("hello", "hallo"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDistance_NullStringInt() throws Exception {
+ UNLIMITED_DISTANCE.apply(null, "a");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGetLevenshteinDistance_StringNullInt() throws Exception {
+ UNLIMITED_DISTANCE.apply("a", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testConstructorWithNegativeThreshold() throws Exception {
+ new LevenshteinDistance(-1);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceDistanceTest.java
new file mode 100644
index 0000000..4de31bd
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceDistanceTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link LongestCommonSubsequenceDistance}.
+ */
+public class LongestCommonSubsequenceDistanceTest {
+
+ private static LongestCommonSubsequenceDistance subject;
+
+ @BeforeClass
+ public static void setup() {
+ subject = new LongestCommonSubsequenceDistance();
+ }
+
+ @Test
+ public void testGettingLogestCommonSubsequenceDistacne() {
+ assertEquals(Integer.valueOf(0), subject.apply("", ""));
+ assertEquals(Integer.valueOf(4), subject.apply("left", ""));
+ assertEquals(Integer.valueOf(5), subject.apply("", "right"));
+ assertEquals(Integer.valueOf(1), subject.apply("frog", "fog"));
+ assertEquals(Integer.valueOf(6), subject.apply("fly", "ant"));
+ assertEquals(Integer.valueOf(11), subject.apply("elephant", "hippo"));
+ assertEquals(Integer.valueOf(7), subject.apply("ABC Corporation", "ABC Corp"));
+ assertEquals(Integer.valueOf(4), subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
+ assertEquals(Integer.valueOf(9), subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
+ assertEquals(Integer.valueOf(3), subject.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
+ assertEquals(Integer.valueOf(7), subject.apply("left", "right"));
+ assertEquals(Integer.valueOf(9), subject.apply("leettteft", "ritttght"));
+ assertEquals(Integer.valueOf(0), subject.apply("the same string", "the same string"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceDistanceNullNull() throws Exception {
+ subject.apply(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceDistanceStringNull() throws Exception {
+ subject.apply(" ", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceDistanceNullString() throws Exception {
+ subject.apply(null, "right");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceTest.java
new file mode 100644
index 0000000..95c2d09
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/LongestCommonSubsequenceTest.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link LongestCommonSubsequence}.
+ */
+public class LongestCommonSubsequenceTest {
+
+ private static LongestCommonSubsequence subject;
+
+ @BeforeClass
+ public static void setup() {
+ subject = new LongestCommonSubsequence();
+ }
+
+ @Test
+ public void testLongestCommonSubsequenceApply() {
+ assertEquals(Integer.valueOf(0), subject.apply("", ""));
+ assertEquals(Integer.valueOf(0), subject.apply("left", ""));
+ assertEquals(Integer.valueOf(0), subject.apply("", "right"));
+ assertEquals(Integer.valueOf(3), subject.apply("frog", "fog"));
+ assertEquals(Integer.valueOf(0), subject.apply("fly", "ant"));
+ assertEquals(Integer.valueOf(1), subject.apply("elephant", "hippo"));
+ assertEquals(Integer.valueOf(8), subject.apply("ABC Corporation", "ABC Corp"));
+ assertEquals(Integer.valueOf(20), subject.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
+ assertEquals(Integer.valueOf(24), subject.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
+ assertEquals(Integer.valueOf(11), subject.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
+ assertEquals(Integer.valueOf(1), subject.apply("left", "right"));
+ assertEquals(Integer.valueOf(4), subject.apply("leettteft", "ritttght"));
+ assertEquals(Integer.valueOf(15), subject.apply("the same string", "the same string"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceApplyNullNull() throws Exception {
+ subject.apply(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceApplyStringNull() throws Exception {
+ subject.apply(" ", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceApplyNullString() throws Exception {
+ subject.apply(null, "right");
+ }
+
+ @Test
+ public void testLongestCommonSubsequence() {
+ assertEquals("", subject.logestCommonSubsequence("", ""));
+ assertEquals("", subject.logestCommonSubsequence("left", ""));
+ assertEquals("", subject.logestCommonSubsequence("", "right"));
+ assertEquals("fog", subject.logestCommonSubsequence("frog", "fog"));
+ assertEquals("", subject.logestCommonSubsequence("fly", "ant"));
+ assertEquals("h", subject.logestCommonSubsequence("elephant", "hippo"));
+ assertEquals("ABC Corp", subject.logestCommonSubsequence("ABC Corporation", "ABC Corp"));
+ assertEquals("D H Enterprises Inc", subject.logestCommonSubsequence("D N H Enterprises Inc", "D & H Enterprises, Inc."));
+ assertEquals("My Gym Childrens Fitness", subject.logestCommonSubsequence("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
+ assertEquals("PENNSYLVNIA", subject.logestCommonSubsequence("PENNSYLVANIA", "PENNCISYLVNIA"));
+ assertEquals("t", subject.logestCommonSubsequence("left", "right"));
+ assertEquals("tttt", subject.logestCommonSubsequence("leettteft", "ritttght"));
+ assertEquals("the same string", subject.logestCommonSubsequence("the same string", "the same string"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceNullNull() throws Exception {
+ subject.logestCommonSubsequence(null, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceStringNull() throws Exception {
+ subject.logestCommonSubsequence(" ", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testGettingLongestCommonSubsequenceNullString() throws Exception {
+ subject.logestCommonSubsequence(null, "right");
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedEditDistanceFromTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedEditDistanceFromTest.java b/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedEditDistanceFromTest.java
new file mode 100644
index 0000000..0d080ba
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedEditDistanceFromTest.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Unit tests for {@link EditDistanceFrom}.
+ *
+ * @param <R> The {@link EditDistance} return type.
+ */
+@RunWith(Parameterized.class)
+public class ParameterizedEditDistanceFromTest<R> {
+
+ private final EditDistance<R> editDistance;
+ private final CharSequence left;
+ private final CharSequence right;
+ private final R distance;
+
+ public ParameterizedEditDistanceFromTest(
+ final EditDistance<R> editDistance,
+ final CharSequence left, final CharSequence right,
+ final R distance) {
+
+ this.editDistance = editDistance;
+ this.left = left;
+ this.right = right;
+ this.distance = distance;
+ }
+
+ @Parameters
+ public static Iterable<Object[]> parameters() {
+ return Arrays.asList( new Object[][] {
+
+ { new HammingDistance(), "Sam I am.", "Ham I am.", 1 },
+ { new HammingDistance(), "Japtheth, Ham, Shem", "Japtheth, HAM, Shem", 2 },
+ { new HammingDistance(), "Hamming", "Hamming", 0 },
+
+ { new LevenshteinDistance(), "Apache", "a patchy", 4 },
+ { new LevenshteinDistance(), "go", "no go", 3 },
+ { new LevenshteinDistance(), "go", "go", 0 },
+
+ { new LevenshteinDistance(4), "Apache", "a patchy", 4 },
+ { new LevenshteinDistance(4), "go", "no go", 3 },
+ { new LevenshteinDistance(0), "go", "go", 0 },
+
+ {
+ new EditDistance<Boolean>() {
+ @Override
+ public Boolean apply(final CharSequence left, final CharSequence right) {
+ return left == right || (left != null && left.equals(right));
+ }
+ },
+ "Bob's your uncle.",
+ "Every good boy does fine.",
+ false
+ }
+
+ } );
+ }
+
+ @Test
+ public void test() {
+ final EditDistanceFrom<R> editDistanceFrom = new EditDistanceFrom<>(editDistance, left);
+ assertThat(editDistanceFrom.apply(right), equalTo(distance));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedLevenshteinDistanceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedLevenshteinDistanceTest.java b/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedLevenshteinDistanceTest.java
new file mode 100644
index 0000000..69a062a
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedLevenshteinDistanceTest.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Unit tests for {@link LevenshteinDistance}.
+ */
+@RunWith(Parameterized.class)
+public class ParameterizedLevenshteinDistanceTest {
+
+ private final Integer distance;
+ private final CharSequence left;
+ private final CharSequence right;
+ private final Integer threshold;
+
+ public ParameterizedLevenshteinDistanceTest(
+ final Integer threshold,
+ final CharSequence left, final CharSequence right,
+ final Integer distance) {
+
+ this.threshold = threshold;
+ this.left = left;
+ this.right = right;
+ this.distance = distance;
+ }
+
+ @Parameters
+ public static Iterable<Object[]> parameters() {
+ return Arrays.asList( new Object[][] {
+
+ /* empty strings */
+ { 0, "", "", 0 },
+ { 8, "aaapppp", "", 7 },
+ { 7, "aaapppp", "", 7 },
+ { 6, "aaapppp", "", -1 },
+
+ /* unequal strings, zero threshold */
+ { 0, "b", "a", -1 },
+ { 0, "a", "b", -1 },
+
+ /* equal strings */
+ { 0, "aa", "aa", 0 },
+ { 2, "aa", "aa", 0 },
+
+ /* same length */
+ { 2, "aaa", "bbb", -1 },
+ { 3, "aaa", "bbb", 3 },
+
+ /* big stripe */
+ { 10, "aaaaaa", "b", 6 },
+
+ /* distance less than threshold */
+ { 8, "aaapppp", "b", 7 },
+ { 4, "a", "bbb", 3 },
+
+ /* distance equal to threshold */
+ { 7, "aaapppp", "b", 7 },
+ { 3, "a", "bbb", 3 },
+
+ /* distance greater than threshold */
+ { 2, "a", "bbb", -1 },
+ { 2, "bbb", "a", -1 },
+ { 6, "aaapppp", "b", -1 },
+
+ /* stripe runs off array, strings not similar */
+ { 1, "a", "bbb", -1 },
+ { 1, "bbb", "a", -1 },
+
+ /* stripe runs off array, strings are similar */
+ { 1, "12345", "1234567", -1 },
+ { 1, "1234567", "12345", -1 },
+
+ /* old getLevenshteinDistance test cases */
+ { 1, "frog", "fog", 1 },
+ { 3, "fly", "ant", 3 },
+ { 7, "elephant", "hippo", 7 },
+ { 6, "elephant", "hippo", -1 },
+ { 7, "hippo", "elephant", 7 },
+ { 6, "hippo", "elephant", -1 },
+ { 8, "hippo", "zzzzzzzz", 8 },
+ { 8, "zzzzzzzz", "hippo", 8 },
+ { 1, "hello", "hallo", 1 },
+
+ { Integer.MAX_VALUE, "frog", "fog", 1 },
+ { Integer.MAX_VALUE, "fly", "ant", 3 },
+ { Integer.MAX_VALUE, "elephant", "hippo", 7 },
+ { Integer.MAX_VALUE, "hippo", "elephant", 7 },
+ { Integer.MAX_VALUE, "hippo", "zzzzzzzz", 8 },
+ { Integer.MAX_VALUE, "zzzzzzzz", "hippo", 8 },
+ { Integer.MAX_VALUE, "hello", "hallo", 1 }
+
+ } );
+ }
+
+ @Test
+ public void test() {
+ final LevenshteinDistance metric = new LevenshteinDistance(threshold);
+ assertThat(metric.apply(left, right), equalTo(distance));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedSimilarityScoreFromTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedSimilarityScoreFromTest.java b/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedSimilarityScoreFromTest.java
new file mode 100644
index 0000000..810f1ee
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/ParameterizedSimilarityScoreFromTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.util.Arrays;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for {@link SimilarityScoreFrom}.
+ *
+ * @param <R> The {@link SimilarityScore} return type.
+ */
+@RunWith(Parameterized.class)
+public class ParameterizedSimilarityScoreFromTest<R> {
+
+ private final SimilarityScore<R> similarityScore;
+ private final CharSequence left;
+ private final CharSequence right;
+ private final R distance;
+
+ public ParameterizedSimilarityScoreFromTest(
+ final SimilarityScore<R> similarityScore,
+ final CharSequence left, final CharSequence right,
+ final R distance) {
+
+ this.similarityScore = similarityScore;
+ this.left = left;
+ this.right = right;
+ this.distance = distance;
+ }
+
+ @Parameters
+ public static Iterable<Object[]> parameters() {
+ return Arrays.asList( new Object[][] {
+
+ { new JaroWinklerDistance(), "elephant", "hippo", 0.44 },
+ { new JaroWinklerDistance(), "hippo", "elephant", 0.44 },
+ { new JaroWinklerDistance(), "hippo", "zzzzzzzz", 0.0 },
+
+ {
+ new SimilarityScore<Boolean>() {
+ @Override
+ public Boolean apply(final CharSequence left, final CharSequence right) {
+ return left == right || (left != null && left.equals(right));
+ }
+ },
+ "Bob's your uncle.",
+ "Every good boy does fine.",
+ false
+ }
+
+ } );
+ }
+
+ @Test
+ public void test() {
+ final SimilarityScoreFrom<R> similarityScoreFrom = new SimilarityScoreFrom<>(similarityScore, left);
+ assertThat(similarityScoreFrom.apply(right), equalTo(distance));
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/similarity/StringMetricFromTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/similarity/StringMetricFromTest.java b/src/test/java/org/apache/commons/text/beta/similarity/StringMetricFromTest.java
new file mode 100644
index 0000000..f8f67da
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/similarity/StringMetricFromTest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.similarity;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link EditDistanceFrom}.
+ */
+public class StringMetricFromTest {
+
+ @Test
+ public void testEquivalence() {
+ final EditDistance<Integer> metric = new LevenshteinDistance();
+ final String left = "Apache";
+ final String right = "a patchy";
+ final Integer distance = 4;
+ final EditDistanceFrom<Integer> metricFrom = new EditDistanceFrom<>(metric, left);
+
+ assertThat(metricFrom.apply(right), equalTo(distance));
+ assertThat(metricFrom.apply(right), equalTo(metric.apply(left, right)));
+ }
+
+ @Test
+ public void testJavadocExample() {
+ final EditDistance<Integer> metric = new LevenshteinDistance();
+ final String target = "Apache";
+ final EditDistanceFrom<Integer> metricFrom =
+ new EditDistanceFrom<>(metric, target);
+ String mostSimilar = null;
+ Integer shortestDistance = null;
+
+ for (final String test : new String[] { "Appaloosa", "a patchy", "apple" }) {
+ final Integer distance = metricFrom.apply(test);
+ if (shortestDistance == null || distance < shortestDistance) {
+ shortestDistance = distance;
+ mostSimilar = test;
+ }
+ }
+ assertThat(mostSimilar, equalTo("a patchy"));
+ assertThat(shortestDistance, equalTo(4));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testMissingMetric() {
+ new EditDistanceFrom<Number>(null, "no go");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/translate/AggregateTranslatorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/translate/AggregateTranslatorTest.java b/src/test/java/org/apache/commons/text/beta/translate/AggregateTranslatorTest.java
new file mode 100644
index 0000000..cc63f5a
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/translate/AggregateTranslatorTest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.text.beta.translate;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link AggregateTranslator}.
+ */
+public class AggregateTranslatorTest {
+
+ @Test
+ public void testNullConstructor() throws Exception {
+ final String testString = "foo";
+ final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator[]) null);
+ assertEquals(testString, subject.translate(testString));
+ }
+
+ @Test
+ public void testNullVarargConstructor() throws Exception {
+ final String testString = "foo";
+ final AggregateTranslator subject = new AggregateTranslator((CharSequenceTranslator) null);
+ assertEquals(testString, subject.translate(testString));
+ }
+
+ @Test
+ public void testNonNull() throws IOException{
+ final Map<CharSequence, CharSequence> oneTwoMap = new HashMap<>();
+ oneTwoMap.put("one", "two");
+ final Map<CharSequence, CharSequence> threeFourMap = new HashMap<>();
+ threeFourMap.put("three", "four");
+ final CharSequenceTranslator translator1 = new LookupTranslator(oneTwoMap);
+ final CharSequenceTranslator translator2 = new LookupTranslator(threeFourMap);
+ final AggregateTranslator subject = new AggregateTranslator(translator1, translator2);
+ final StringWriter out1 = new StringWriter();
+ final int result1 = subject.translate(new StringBuffer("one"), 0, out1);
+ assertEquals("Incorrect codepoint consumption", 3, result1);
+ assertEquals("Incorrect value", "two", out1.toString());
+ final StringWriter out2 = new StringWriter();
+ final int result2 = subject.translate(new StringBuffer("three"), 0, out2);
+ assertEquals("Incorrect codepoint consumption", 5, result2);
+ assertEquals("Incorrect value", "four", out2.toString());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/165c8e60/src/test/java/org/apache/commons/text/beta/translate/EntityArraysTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/beta/translate/EntityArraysTest.java b/src/test/java/org/apache/commons/text/beta/translate/EntityArraysTest.java
new file mode 100644
index 0000000..2bb8d59
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/beta/translate/EntityArraysTest.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.text.beta.translate;
+
+import org.junit.Test;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link EntityArrays}.
+ */
+public class EntityArraysTest {
+
+ @Test
+ public void testConstructorExists() {
+ new EntityArrays();
+ }
+
+ // LANG-659, LANG-658 - avoid duplicate entries
+ @Test
+ public void testForDuplicatedDeclaredMapKeys() throws Exception {
+ String packageDirectory = EntityArraysTest.class.getPackage().getName().replace(".", "/");
+ try (BufferedReader br = new BufferedReader(new FileReader("src/main/java/" + packageDirectory + "/EntityArrays.java"))) {
+ String line;
+ int mapDeclarationCounter = 0;
+ while ((line = br.readLine()) != null) {
+ //Start with map declaration and count put lines
+ if (line.contains("new HashMap<>();")) {
+ mapDeclarationCounter = 0;
+ } else if (line.contains(".put(")) {
+ mapDeclarationCounter++;
+ } else if (line.contains("Collections.unmodifiableMap(initialMap);")) {
+ String mapVariableName = line.split("=")[0].trim();
+ Map<String,String> mapValue = (Map)EntityArrays.class.getDeclaredField(mapVariableName).get(EntityArrays.class);
+ // Validate that we are not inserting into the same key twice in the map declaration. If this,
+ // indeed was the case the keySet().size() would be smaller than the number of put() statements
+ assertEquals(mapDeclarationCounter, mapValue.keySet().size());
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testForDuplicateDeclaredMapValuesISO8859Map() {
+ assertEquals(EntityArrays.ISO8859_1_ESCAPE.keySet().size(),
+ EntityArrays.ISO8859_1_UNESCAPE.keySet().size());
+ }
+
+ @Test
+ public void testISO8859Map() {
+ testEscapeVsUnescapeMaps(EntityArrays.ISO8859_1_ESCAPE, EntityArrays.ISO8859_1_UNESCAPE);
+ }
+
+ @Test
+ public void testForDuplicateDeclaredMapValuesHtml40ExtendedMap() {
+ assertEquals(EntityArrays.HTML40_EXTENDED_ESCAPE.keySet().size(),
+ EntityArrays.HTML40_EXTENDED_UNESCAPE.keySet().size());
+ }
+
+ @Test
+ public void testHtml40ExtendedMap() {
+ testEscapeVsUnescapeMaps(EntityArrays.HTML40_EXTENDED_ESCAPE, EntityArrays.HTML40_EXTENDED_UNESCAPE);
+ }
+
+ @Test
+ public void testForDuplicateDeclaredMapValuesAposMap() {
+ assertEquals(EntityArrays.APOS_ESCAPE.keySet().size(),
+ EntityArrays.APOS_UNESCAPE.keySet().size());
+ }
+
+ @Test
+ public void testAposMap() {
+ testEscapeVsUnescapeMaps(EntityArrays.APOS_ESCAPE, EntityArrays.APOS_UNESCAPE);
+ }
+
+ @Test
+ public void testForDuplicateDeclaredMapValuesBasicMap() {
+ assertEquals(EntityArrays.BASIC_ESCAPE.keySet().size(),
+ EntityArrays.BASIC_UNESCAPE.keySet().size());
+ }
+
+ @Test
+ public void testBasicMap() {
+ testEscapeVsUnescapeMaps(EntityArrays.BASIC_ESCAPE, EntityArrays.BASIC_UNESCAPE);
+ }
+
+ @Test
+ public void testForDuplicateDeclaredMapValuesJavaCtrlCharsMap() {
+ assertEquals(EntityArrays.JAVA_CTRL_CHARS_ESCAPE.keySet().size(),
+ EntityArrays.JAVA_CTRL_CHARS_UNESCAPE.keySet().size());
+ }
+
+ @Test
+ public void testJavaCntrlCharsMap() {
+ testEscapeVsUnescapeMaps(EntityArrays.JAVA_CTRL_CHARS_ESCAPE, EntityArrays.JAVA_CTRL_CHARS_UNESCAPE);
+ }
+
+ private void testEscapeVsUnescapeMaps(final Map<CharSequence, CharSequence> escapeMap,
+ final Map<CharSequence, CharSequence> unescapeMap) {
+ for (final CharSequence escapeKey : escapeMap.keySet()) {
+ for (final CharSequence unescapeKey : unescapeMap.keySet()) {
+ if (escapeKey == unescapeMap.get(unescapeKey)) {
+ assertEquals(escapeMap.get(escapeKey), unescapeKey);
+ }
+ }
+ }
+ }
+
+}