http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/JavaUnicodeEscaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/JavaUnicodeEscaperTest.java b/src/test/java/org/apache/commons/text/beta/translate/JavaUnicodeEscaperTest.java deleted file mode 100644 index 9e30f73..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/JavaUnicodeEscaperTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.UnsupportedEncodingException; - -import static org.junit.Assert.assertEquals; - -/** - * Unit tests for {@link JavaUnicodeEscaper}. - */ -public class JavaUnicodeEscaperTest { - - @Test - public void testBelow() { - final JavaUnicodeEscaper jue = JavaUnicodeEscaper.below('F'); - - final String input = "ADFGZ"; - final String result = jue.translate(input); - assertEquals("Failed to escape Unicode characters via the below method", "\\u0041\\u0044FGZ", result); - } - - @Test - public void testBetween() { - final JavaUnicodeEscaper jue = JavaUnicodeEscaper.between('F', 'L'); - - final String input = "ADFGZ"; - final String result = jue.translate(input); - assertEquals("Failed to escape Unicode characters via the between method", "AD\\u0046\\u0047Z", result); - } - - @Test - public void testAbove() { - final JavaUnicodeEscaper jue = JavaUnicodeEscaper.above('F'); - - final String input = "ADFGZ"; - final String result = jue.translate(input); - assertEquals("Failed to escape Unicode characters via the above method", "ADF\\u0047\\u005A", result); - } - - @Test - public void testToUtf16Escape() throws UnsupportedEncodingException { - final JavaUnicodeEscaper jue = JavaUnicodeEscaper.below('F'); - // According to https://en.wikipedia.org/wiki/UTF-16#Code_points_U.2B10000..U.2B10FFFF, - // Character ?, U+24B62, Binary Code Point 0010 0100 1011 0110 0010, Binary UTF-167 1101 1000 0101 0010 1101 1111 0110 0010, UTF-16 Hex Code Units D852 DF62 - final String encoding = jue.toUtf16Escape(Integer.parseInt("024B62", 16)); - assertEquals("\\uD852\\uDF62",encoding); - } -}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/LookupTranslatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/LookupTranslatorTest.java b/src/test/java/org/apache/commons/text/beta/translate/LookupTranslatorTest.java deleted file mode 100644 index 384f58e..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/LookupTranslatorTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 LookupTranslator}. - */ -public class LookupTranslatorTest { - - @Test - public void testBasicLookup() throws IOException { - final Map<CharSequence, CharSequence> translatorMap = new HashMap<>(); - translatorMap.put("one", "two"); - final LookupTranslator lt = new LookupTranslator(translatorMap); - final StringWriter out = new StringWriter(); - final int result = lt.translate("one", 0, out); - assertEquals("Incorrect codepoint consumption", 3, result); - assertEquals("Incorrect value", "two", out.toString()); - } - - // Tests: https://issues.apache.org/jira/browse/LANG-882 - @Test - public void testLang882() throws IOException { - final Map<CharSequence, CharSequence> translatorMap = new HashMap<>(); - translatorMap.put(new StringBuffer("one"), new StringBuffer("two")); - final LookupTranslator lt = new LookupTranslator(translatorMap); - final StringWriter out = new StringWriter(); - final int result = lt.translate(new StringBuffer("one"), 0, out); - assertEquals("Incorrect codepoint consumption", 3, result); - assertEquals("Incorrect value", "two", out.toString()); - } - -} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/NumericEntityEscaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/NumericEntityEscaperTest.java b/src/test/java/org/apache/commons/text/beta/translate/NumericEntityEscaperTest.java deleted file mode 100644 index 9918885..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/NumericEntityEscaperTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 static org.junit.Assert.assertEquals; - -/** - * Unit tests for {@link NumericEntityEscaper}. - */ -public class NumericEntityEscaperTest { - - @Test - public void testBelow() { - final NumericEntityEscaper nee = NumericEntityEscaper.below('F'); - - final String input = "ADFGZ"; - final String result = nee.translate(input); - assertEquals("Failed to escape numeric entities via the below method", "ADFGZ", result); - } - - @Test - public void testBetween() { - final NumericEntityEscaper nee = NumericEntityEscaper.between('F', 'L'); - - final String input = "ADFGZ"; - final String result = nee.translate(input); - assertEquals("Failed to escape numeric entities via the between method", "ADFGZ", result); - } - - @Test - public void testAbove() { - final NumericEntityEscaper nee = NumericEntityEscaper.above('F'); - - final String input = "ADFGZ"; - final String result = nee.translate(input); - assertEquals("Failed to escape numeric entities via the above method", "ADFGZ", result); - } - - // See LANG-617 - @Test - public void testSupplementary() { - final NumericEntityEscaper nee = new NumericEntityEscaper(); - final String input = "\uD803\uDC22"; - final String expected = "𐰢"; - - final String result = nee.translate(input); - assertEquals("Failed to escape numeric entities supplementary characters", expected, result); - - } - -} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/NumericEntityUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/NumericEntityUnescaperTest.java b/src/test/java/org/apache/commons/text/beta/translate/NumericEntityUnescaperTest.java deleted file mode 100644 index ba5b0ce..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/NumericEntityUnescaperTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * Unit tests for {@link NumericEntityUnescaper}. - */ -public class NumericEntityUnescaperTest { - - @Test - public void testSupplementaryUnescaping() { - final NumericEntityUnescaper neu = new NumericEntityUnescaper(); - final String input = "𐰢"; - final String expected = "\uD803\uDC22"; - - final String result = neu.translate(input); - assertEquals("Failed to unescape numeric entities supplementary characters", expected, result); - } - - @Test - public void testOutOfBounds() { - final NumericEntityUnescaper neu = new NumericEntityUnescaper(); - - assertEquals("Failed to ignore when last character is &", "Test &", neu.translate("Test &")); - assertEquals("Failed to ignore when last character is &", "Test &#", neu.translate("Test &#")); - assertEquals("Failed to ignore when last character is &", "Test &#x", neu.translate("Test &#x")); - assertEquals("Failed to ignore when last character is &", "Test &#X", neu.translate("Test &#X")); - } - - @Test - public void testUnfinishedEntity() { - // parse it - NumericEntityUnescaper neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.semiColonOptional); - String input = "Test 0 not test"; - String expected = "Test \u0030 not test"; - - String result = neu.translate(input); - assertEquals("Failed to support unfinished entities (i.e. missing semi-colon)", expected, result); - - // ignore it - neu = new NumericEntityUnescaper(); - input = "Test 0 not test"; - expected = input; - - result = neu.translate(input); - assertEquals("Failed to ignore unfinished entities (i.e. missing semi-colon)", expected, result); - - // fail it - neu = new NumericEntityUnescaper(NumericEntityUnescaper.OPTION.errorIfNoSemiColon); - input = "Test 0 not test"; - - try { - result = neu.translate(input); - fail("IllegalArgumentException expected"); - } catch(final IllegalArgumentException iae) { - // expected - } - } - -} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/OctalUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/OctalUnescaperTest.java b/src/test/java/org/apache/commons/text/beta/translate/OctalUnescaperTest.java deleted file mode 100644 index 9f6442f..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/OctalUnescaperTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 static org.junit.Assert.assertEquals; - -/** - * Unit tests for {@link OctalUnescaper}. - */ -public class OctalUnescaperTest { - - @Test - public void testBetween() { - final OctalUnescaper oue = new OctalUnescaper(); //.between("1", "377"); - - String input = "\\45"; - String result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\45", result); - - input = "\\377"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\377", result); - - input = "\\377 and"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\377 and", result); - - input = "\\378 and"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\37" + "8 and", result); - - input = "\\378"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\37" + "8", result); - - input = "\\1"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\1", result); - - input = "\\036"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\036", result); - - input = "\\0365"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\036" + "5", result); - - input = "\\003"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\003", result); - - input = "\\0003"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\000" + "3", result); - - input = "\\279"; - result = oue.translate(input); - assertEquals("Failed to unescape octal characters via the between method", "\279", result); - - input = "\\999"; - result = oue.translate(input); - assertEquals("Failed to ignore an out of range octal character via the between method", "\\999", result); - } - -} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/SinglePassTranslatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/SinglePassTranslatorTest.java b/src/test/java/org/apache/commons/text/beta/translate/SinglePassTranslatorTest.java deleted file mode 100644 index 6a77350..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/SinglePassTranslatorTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.Before; -import org.junit.Test; - -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; - -import static org.junit.Assert.assertEquals; - -/** - * Unit test for {@link SinglePassTranslator} - */ -public class SinglePassTranslatorTest { - - private final SinglePassTranslator dummyTranslator = new SinglePassTranslator() { - @Override - void translateWhole(final CharSequence input, final Writer out) throws IOException { - } - }; - - private StringWriter out; - - @Before - public void before() { - out = new StringWriter(); - } - - @Test - public void codePointsAreReturned() throws Exception { - assertEquals(0, dummyTranslator.translate("", 0, out)); - assertEquals(3, dummyTranslator.translate("abc", 0, out)); - assertEquals(7, dummyTranslator.translate("abcdefg", 0, out)); - } - - @Test(expected = IllegalArgumentException.class) - public void indexIsValidated() throws Exception { - dummyTranslator.translate("abc", 1, out); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/UnicodeEscaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/UnicodeEscaperTest.java b/src/test/java/org/apache/commons/text/beta/translate/UnicodeEscaperTest.java deleted file mode 100644 index 7fe28ea..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/UnicodeEscaperTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 static org.junit.Assert.assertEquals; - -/** - * Unit tests for {@link UnicodeEscaper}. - */ -public class UnicodeEscaperTest { - - @Test - public void testBelow() { - final UnicodeEscaper ue = UnicodeEscaper.below('F'); - - final String input = "ADFGZ"; - final String result = ue.translate(input); - assertEquals("Failed to escape Unicode characters via the below method", "\\u0041\\u0044FGZ", result); - } - - @Test - public void testBetween() { - final UnicodeEscaper ue = UnicodeEscaper.between('F', 'L'); - - final String input = "ADFGZ"; - final String result = ue.translate(input); - assertEquals("Failed to escape Unicode characters via the between method", "AD\\u0046\\u0047Z", result); - } - - @Test - public void testAbove() { - final UnicodeEscaper ue = UnicodeEscaper.above('F'); - - final String input = "ADFGZ"; - final String result = ue.translate(input); - assertEquals("Failed to escape Unicode characters via the above method", "ADF\\u0047\\u005A", result); - } -} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnescaperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnescaperTest.java b/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnescaperTest.java deleted file mode 100644 index 9a74b30..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnescaperTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * Unit tests for {@link UnicodeEscaper}. - */ -public class UnicodeUnescaperTest { - - // Requested in LANG-507 - @Test - public void testUPlus() { - final UnicodeUnescaper uu = new UnicodeUnescaper(); - - final String input = "\\u+0047"; - assertEquals("Failed to unescape Unicode characters with 'u+' notation", "G", uu.translate(input)); - } - - @Test - public void testUuuuu() { - final UnicodeUnescaper uu = new UnicodeUnescaper(); - - final String input = "\\uuuuuuuu0047"; - final String result = uu.translate(input); - assertEquals("Failed to unescape Unicode characters with many 'u' characters", "G", result); - } - - @Test - public void testLessThanFour() { - final UnicodeUnescaper uu = new UnicodeUnescaper(); - - final String input = "\\0047\\u006"; - try { - uu.translate(input); - fail("A lack of digits in a Unicode escape sequence failed to throw an exception"); - } catch(final IllegalArgumentException iae) { - // expected - } - } -} http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnpairedSurrogateRemoverTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnpairedSurrogateRemoverTest.java b/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnpairedSurrogateRemoverTest.java deleted file mode 100644 index 3d30cee..0000000 --- a/src/test/java/org/apache/commons/text/beta/translate/UnicodeUnpairedSurrogateRemoverTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.CharArrayWriter; -import java.io.IOException; - -import static org.junit.Assert.assertEquals; - -/** - * Unit tests for {@link UnicodeUnpairedSurrogateRemover}. - */ -public class UnicodeUnpairedSurrogateRemoverTest { - final UnicodeUnpairedSurrogateRemover subject = new UnicodeUnpairedSurrogateRemover(); - final CharArrayWriter writer = new CharArrayWriter(); // nothing is ever written to it - - @Test - public void testValidCharacters() throws IOException { - assertEquals(false, subject.translate(0xd7ff, writer)); - assertEquals(false, subject.translate(0xe000, writer)); - assertEquals(0, writer.size()); - } - - @Test - public void testInvalidCharacters() throws IOException { - assertEquals(true, subject.translate(0xd800, writer)); - assertEquals(true, subject.translate(0xdfff, writer)); - assertEquals(0, writer.size()); - } -} - http://git-wip-us.apache.org/repos/asf/commons-text/blob/c7cf533d/src/test/java/org/apache/commons/text/diff/ReplacementsFinderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/diff/ReplacementsFinderTest.java b/src/test/java/org/apache/commons/text/diff/ReplacementsFinderTest.java new file mode 100644 index 0000000..eea6f9f --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java b/src/test/java/org/apache/commons/text/diff/StringsComparatorTest.java new file mode 100644 index 0000000..ddf0f2f --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/CosineDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/CosineDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/CosineDistanceTest.java new file mode 100644 index 0000000..cd39e84 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java b/src/test/java/org/apache/commons/text/similarity/FuzzyScoreTest.java new file mode 100644 index 0000000..22621f2 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/HammingDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/HammingDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/HammingDistanceTest.java new file mode 100644 index 0000000..93c00a7 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/JaccardDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/JaccardDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/JaccardDistanceTest.java new file mode 100644 index 0000000..47be780 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java b/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java new file mode 100644 index 0000000..dddfe13 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java new file mode 100644 index 0000000..84276f1 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java new file mode 100644 index 0000000..93eea39 --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java new file mode 100644 index 0000000..d40b8cf --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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/c7cf533d/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistanceTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceDistanceTest.java new file mode 100644 index 0000000..4c9ff6f --- /dev/null +++ b/src/test/java/org/apache/commons/text/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.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"); + } + +}