diff --git a/src/test/java/org/apache/commons/text/diff/CommandIntegrationTest.java b/src/test/java/org/apache/commons/text/diff/CommandIntegrationTest.java new file mode 100644 index 0000000000..769afaa13c --- /dev/null +++ b/src/test/java/org/apache/commons/text/diff/CommandIntegrationTest.java @@ -0,0 +1,105 @@ +/* + * 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.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class CommandIntegrationTest { + + //Create Visitor through using CommandVisitor + private static class myVisitor implements CommandVisitor { + private final List visits = new ArrayList<>(); + + public List getVisits() { + return visits; + } + + @Override + public void visitInsertCommand(T object) { + visits.add("Insert: " + object); + } + @Override + public void visitDeleteCommand(T object) { + visits.add("Delete: " + object); + } + + @Override + public void visitKeepCommand(T object) { + visits.add("Keep: " + object); + } + } + + private myVisitor visitor; + + @BeforeEach + void InitializeVisitor(){ + visitor = new myVisitor<>(); + } + + //Test 1 - Test all commands together with edit command + @Test + void testAllCommands() { + List> commands = Arrays.asList( + new DeleteCommand<>("A"), + new KeepCommand<>("B"), + new InsertCommand<>("C") + ); + + for (EditCommand commandList : commands) { + commandList.accept(visitor); + } + + List expectedVisits = Arrays.asList( + "Delete: A", + "Keep: B", + "Insert: C" + ); + + assertEquals(expectedVisits, visitor.getVisits()); + } + + //Test 2,3,4 - Test individual commands + @Test + void testDeleteCommand() { + DeleteCommand delete = new DeleteCommand<>("D"); + delete.accept(visitor); + assertEquals(Arrays.asList("Delete: D"), visitor.getVisits()); + } + + @Test + void testInsertCommand() { + InsertCommand insert = new InsertCommand<>("E"); + insert.accept(visitor); + assertEquals(Arrays.asList("Insert: E"), visitor.getVisits()); + } + + @Test + void testKeepCommand() { + KeepCommand keep = new KeepCommand<>("F"); + keep.accept(visitor); + assertEquals(Arrays.asList("Keep: F"), visitor.getVisits()); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherCosineDistTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherCosineDistTest.java new file mode 100644 index 0000000000..eb36053f2f --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherCosineDistTest.java @@ -0,0 +1,95 @@ +/* +* 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + + +public class AnotherCosineDistTest { + + private final CosineDistance cosineDistance = new CosineDistance(); + + //Test 1: Testing empty strings - should return error + @Test + void testEmptyStrings() { + assertThrows(IllegalArgumentException.class, () -> { + double distance = cosineDistance.apply("", ""); + }); + assertThrows(IllegalArgumentException.class, () -> { + double distance = cosineDistance.apply("", "a"); + }); + assertThrows(IllegalArgumentException.class, () -> { + double distance = cosineDistance.apply("a", ""); + }); + } + + //Test 2: Testing identical strings - should return distance 0.0 + @Test + void testIdenticalStrings(){ + double distance = cosineDistance.apply("testing", "testing"); + assertEquals(0.0, distance); + } + + //Test 3: Testing different strings - should return distance 1.0 + @Test + void testDifferentStrings(){ + double distance = cosineDistance.apply("testing", "success"); + assertEquals(1.0, distance); + } + + //Test 4: Testing repeated word - result should be 0.0 + @Test + void testOneRepeatedWord(){ + double distance = cosineDistance.apply("testing", "testing testing"); + assertEquals(0.0, distance); + } + + //Test 5: Testing word with subword- result should be between 0.0 and 1.0 + @Test + void testSubword(){ + double distance = cosineDistance.apply("testing", "test"); + assertTrue(distance != 0.0); + } + + //Test 6: Testing very large identical strings - result should be 0.0 + @Test + void testLargeStrings(){ + + String word1 = "abc"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 9999; i++) { + sb.append(word1).append("abc"); + } + String word2 = word1; + + double distance = cosineDistance.apply(word1, word2); + assertEquals(0.0, distance); + } + + //SPECIAL CASE TEST + //Test 7: Testing case sensitivity- result should not be 0.0 + @Test + void testCaseSens(){ + double distance = cosineDistance.apply("test", "Test"); + assertTrue(distance != 0.0); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherCosineSimilarityTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherCosineSimilarityTest.java new file mode 100644 index 0000000000..8e8cbb5518 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherCosineSimilarityTest.java @@ -0,0 +1,87 @@ +/* +* 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 java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + +public class AnotherCosineSimilarityTest { + + //Test 1: Null values - Should throw exception + @Test + public void testNullVectors() { + assertThrows(IllegalArgumentException.class, () -> { + new CosineSimilarity().cosineSimilarity(null, new HashMap<>()); + }); + assertThrows(IllegalArgumentException.class, () -> { + new CosineSimilarity().cosineSimilarity(new HashMap<>(), null); + }); + assertThrows(IllegalArgumentException.class, () -> { + new CosineSimilarity().cosineSimilarity(null, null); + }); + } + + //Test 2: Empty vectors - Should return 0.0 + @Test + public void testEmptyVectors() { + double similar = new CosineSimilarity().cosineSimilarity(new HashMap<>(), new HashMap<>()); + assertEquals(0.0, similar); + } + + //Test 3: Identical Vectors - Should return ~1.0 + @Test + public void testIdenticalVec(){ + final Map vector = new HashMap<>(); + vector.put("a", 1); + vector.put("b", 2); + double similar = new CosineSimilarity().cosineSimilarity(vector, vector); + assertEquals(1.0, similar, 0.00001); + } + + //Test 4: Different Vectors - Should return 0.0 + @Test + public void testDiffVec(){ + Map vector1 = new HashMap<>(); + Map vector2 = new HashMap<>(); + vector1.put("a", 1); + vector1.put("b", 2); + vector2.put("c", 1); + vector2.put("d", 2); + double similar = new CosineSimilarity().cosineSimilarity(vector1, vector2); + assertEquals(0.0, similar); + } + + //SPECIAL CASE TEST + //Test 5: Testing case sensitivity- result should be 0.0 + @Test + void testCaseSens(){ + Map vector1 = new HashMap<>(); + Map vector2 = new HashMap<>(); + vector1.put("a", 1); + vector1.put("b", 2); + vector2.put("A", 1); + vector2.put("B", 2); + double similar = new CosineSimilarity().cosineSimilarity(vector1, vector2); + assertEquals(0.0, similar); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherJaccardDistTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherJaccardDistTest.java new file mode 100644 index 0000000000..12171d87bc --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherJaccardDistTest.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 static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + +public class AnotherJaccardDistTest { + + private final JaccardDistance jacDist = new JaccardDistance(); + + //Test 1 - Null inputs should throw exceptions + @Test + public void testNullInput(){ + assertThrows(IllegalArgumentException.class, () -> { + jacDist.apply(null, "test"); + }); + assertThrows(IllegalArgumentException.class, () -> { + jacDist.apply("test", null); + }); + } + + //Test 2 - Empty strings should return 0 + @Test + public void testEmptyStrings(){ + assertEquals(0.0, jacDist.apply("", "")); + } + + //Test 3 - Test regular identical values + @Test + public void identValues(){ + assertEquals(0.0, jacDist.apply("abcd", "abcd")); + assertEquals(0.0, jacDist.apply("testing", "testing")); + + } + + //Test 4 - test regular different values + @Test + public void testRegularValues(){ + assertEquals(1.0, jacDist.apply("testing", "qwry")); + assertEquals(1.0, jacDist.apply("abcd", "efgh")); + assertEquals(1.0, jacDist.apply("hamburger", "pizza"), 0.1); + } + + //Test 5 - test regular similar values + @Test + public void testSimilarValues(){ + assertEquals(0.0, jacDist.apply("abc", "acb")); + assertEquals(0.5, jacDist.apply("test", "testing")); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherJaccardSimilarityTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherJaccardSimilarityTest.java new file mode 100644 index 0000000000..e83476612f --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherJaccardSimilarityTest.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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + +public class AnotherJaccardSimilarityTest { + + private final JaccardSimilarity jacSim = new JaccardSimilarity(); + + //Test 1 - Null inputs should throw exceptions + @Test + public void testNullInput(){ + assertThrows(IllegalArgumentException.class, () -> { + jacSim.apply(null, "test"); + }); + assertThrows(IllegalArgumentException.class, () -> { + jacSim.apply("test", null); + }); + } + + //Test 2 - Empty strings should return 0 + @Test + public void testEmptyStrings(){ + assertEquals(1.0, jacSim.apply("", "")); + } + + //Test 3 - Test regular identical values + @Test + public void identValues(){ + assertEquals(1.0, jacSim.apply("abcd", "abcd")); + assertEquals(1.0, jacSim.apply("testing", "testing")); + + } + + //Test 4 - test regular different values + @Test + public void testRegularValues(){ + assertEquals(0.0, jacSim.apply("testing", "qwry")); + assertEquals(0.0, jacSim.apply("abcd", "efgh")); + assertEquals(0.0, jacSim.apply("hamburger", "pizza"), 0.1); + } + + //Test 5 - test regular similar values + @Test + public void testSimilarValues(){ + assertEquals(1.0, jacSim.apply("abc", "acb")); + assertEquals(0.5, jacSim.apply("test", "testing")); + } +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherLCSDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherLCSDistanceTest.java new file mode 100644 index 0000000000..28cfcf608f --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherLCSDistanceTest.java @@ -0,0 +1,103 @@ +/* +* 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + + +public class AnotherLCSDistanceTest { + + private final LongestCommonSubsequenceDistance lcsDist = new LongestCommonSubsequenceDistance(); + + //Test 1 - Test null values to throw exception + @Test + public void testNullInput(){ + assertThrows(IllegalArgumentException.class, () -> { + lcsDist.apply(null, "test"); + }); + assertThrows(IllegalArgumentException.class, () -> { + lcsDist.apply("test", null); + }); + } + + //Test 2 - Empty inputs + @Test + public void testEmptyInput(){ + assertEquals(0, lcsDist.apply("","")); + + assertEquals(4, lcsDist.apply("test","")); + + assertEquals(4, lcsDist.apply("","test")); + } + + //Test 3 - Typical case with identical strings + @Test + public void testIdentStrings(){ + assertEquals(0, lcsDist.apply("abc","abc")); + + assertEquals(0, lcsDist.apply("testing","testing")); + + } + + //Test 4 - Typical case with competely different strings + @Test + public void testDiffStrings(){ + assertEquals(6, lcsDist.apply("abc","def")); + + assertEquals(10, lcsDist.apply("hello","pizza")); + + } + + //Test 5 - Typical case with some similarities and some differences + @Test + public void testTypicalCases(){ + assertEquals(3, lcsDist.apply("test","testing")); + + assertEquals(3, lcsDist.apply("testing","test")); + + assertEquals(6, lcsDist.apply("abcdef","defghi")); + + } + + //Test 6 - Extremely long strings + @Test + public void testLongStrings(){ + String word1 = "abc"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 9999; i++) { + sb.append(word1).append("abc"); + } + word1 = sb.toString(); + String word2 = word1; + + assertEquals(0, lcsDist.apply(word1,word2)); + + String word3 = "def"; + StringBuilder sb2 = new StringBuilder(); + for (int i = 0; i < 9999; i++) { + sb2.append(word3).append("def"); + } + word3 = sb2.toString(); + + assertEquals(119988, lcsDist.apply(word1,word3)); + + } + +} \ No newline at end of file diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherLCSTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherLCSTest.java new file mode 100644 index 0000000000..65fb7adf34 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherLCSTest.java @@ -0,0 +1,115 @@ +/* +* 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + +public class AnotherLCSTest { + + private final LongestCommonSubsequence lcs = new LongestCommonSubsequence(); + + //Test 1 - Null inputs should throw exception + @Test + public void testNullInput(){ + assertThrows(IllegalArgumentException.class, () -> { + lcs.apply(null, "test"); + }); + assertThrows(IllegalArgumentException.class, () -> { + lcs.apply("test", null); + }); + } + + //Test 2 - Empty inputs + @Test + public void testEmptyInput(){ + assertEquals(0, lcs.apply("","")); + assertEquals("", lcs.longestCommonSubsequence("", "")); + + assertEquals(0, lcs.apply("test","")); + assertEquals("", lcs.longestCommonSubsequence("test", "")); + + assertEquals(0, lcs.apply("","test")); + assertEquals("", lcs.longestCommonSubsequence("", "test")); + } + + //Test 3 - Typical case with identical strings + @Test + public void testIdentStrings(){ + assertEquals(3, lcs.apply("abc","abc")); + assertEquals("abc", lcs.longestCommonSubsequence("abc", "abc")); + + assertEquals(7, lcs.apply("testing","testing")); + assertEquals("testing", lcs.longestCommonSubsequence("testing", "testing")); + + } + + //Test 4 - Typical case with competely different strings + @Test + public void testDiffStrings(){ + assertEquals(0, lcs.apply("abc","def")); + assertEquals("", lcs.longestCommonSubsequence("abc", "def")); + + assertEquals(0, lcs.apply("hello","pizza")); + assertEquals("", lcs.longestCommonSubsequence("hello", "pizza")); + + } + + //Test 5 - Typical case with some similarities and some differences + @Test + public void testTypicalCases(){ + assertEquals(4, lcs.apply("test","testing")); + assertEquals("test", lcs.longestCommonSubsequence("test", "testing")); + + assertEquals(4, lcs.apply("testing","test")); + assertEquals("test", lcs.longestCommonSubsequence("testing", "test")); + + assertEquals(3, lcs.apply("abcdef","defghi")); + assertEquals("def", lcs.longestCommonSubsequence("abcdef", "defghi")); + + } + + //Test 6 - Extremely long strings + @Test + public void testLongStrings(){ + String word1 = "abc"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 9999; i++) { + sb.append(word1).append("abc"); + } + word1 = sb.toString(); + String word2 = word1; + + String word3 = "def"; + + StringBuilder sb2 = new StringBuilder(); + for (int i = 0; i < 9999; i++) { + sb2.append(word3).append("def"); + } + word3 = sb2.toString(); + + assertEquals(59994, lcs.apply(word1,word2)); + //value is too large to assertEquals with longestCommonSubsequence + + assertEquals(0, lcs.apply(word1, word3)); + assertEquals("", lcs.longestCommonSubsequence(word1, word3)); + + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherLevenshteinDetailDistTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherLevenshteinDetailDistTest.java new file mode 100644 index 0000000000..71172b7b93 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherLevenshteinDetailDistTest.java @@ -0,0 +1,135 @@ +/* +* 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + +public class AnotherLevenshteinDetailDistTest { + + private final LevenshteinDetailedDistance distance = LevenshteinDetailedDistance.getDefaultInstance(); + + //Test 1: Null values - Should throw exceptions + @Test + public void testNullValues() { + assertThrows(IllegalArgumentException.class, () -> { + new LevenshteinDetailedDistance().apply(null, "test"); + }); + assertThrows(IllegalArgumentException.class, () -> { + new LevenshteinDetailedDistance().apply("test", null); + }); + } + + //Test 2: Empty Strings - Should return values + @Test + public void testEmptyStrings(){ + LevenshteinResults result = distance.apply("", ""); + assertEquals(0, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + + result = distance.apply("", "test"); + assertEquals(4, result.getDistance()); + assertEquals(4, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + + result = distance.apply("test", ""); + assertEquals(4, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(4, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + + } + + //Test 3: Different Strings/ Regular Values + @Test + public void testRegularValues(){ + LevenshteinResults result = distance.apply("a", "b"); + assertEquals(1, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(1, result.getSubstituteCount()); + + result = distance.apply("qwerty", "erty"); + assertEquals(2, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(2, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + + result = distance.apply("erty", "qwerty"); + assertEquals(2, result.getDistance()); + assertEquals(2, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + + result = distance.apply("aaa", "bbb"); + assertEquals(3, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(3, result.getSubstituteCount()); + + result = distance.apply("abc", "cde"); + assertEquals(3, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(3, result.getSubstituteCount()); + + result = distance.apply("testing", "test"); + assertEquals(3, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(3, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + } + + //Test 4: Long Values + @Test + public void testLongValues(){ + LevenshteinDetailedDistance distance = LevenshteinDetailedDistance.getDefaultInstance(); + String word1 = "abc"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 999; i++) { + sb.append(word1).append("abc"); + } + word1 = sb.toString(); + String word2 = word1; + + String word3 = "def"; + StringBuilder sb2 = new StringBuilder(); + for (int i = 0; i < 999; i++) { + sb2.append(word3).append("def"); + } + word3 = sb2.toString(); + + + LevenshteinResults result = distance.apply(word1, word2); + assertEquals(0, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(0, result.getSubstituteCount()); + + result = distance.apply(word1, word3); + assertEquals(5994, result.getDistance()); + assertEquals(0, result.getInsertCount()); + assertEquals(0, result.getDeleteCount()); + assertEquals(5994, result.getSubstituteCount()); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/AnotherLevenshteinDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/AnotherLevenshteinDistanceTest.java new file mode 100644 index 0000000000..9679a1e02d --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/AnotherLevenshteinDistanceTest.java @@ -0,0 +1,82 @@ +/* +* 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +public class AnotherLevenshteinDistanceTest { + + //Test 1: Null values - Should throw exceptions + @Test + public void testNullValues() { + assertThrows(IllegalArgumentException.class, () -> { + new LevenshteinDistance().apply(null, "test"); + }); + assertThrows(IllegalArgumentException.class, () -> { + new LevenshteinDistance().apply("test", null); + }); + } + + //Test 2: Empty Strings - Should return values + @Test + public void testEmptyStrings(){ + LevenshteinDistance distance = LevenshteinDistance.getDefaultInstance(); + assertEquals(0, distance.apply("", "").intValue()); + assertEquals(1, distance.apply("", "a").intValue()); + assertEquals(1, distance.apply("a", "").intValue()); + } + + //Test 3: Different Strings/ Regular Values + @Test + public void testRegularValues(){ + LevenshteinDistance distance = LevenshteinDistance.getDefaultInstance(); + assertEquals(1, distance.apply("a", "b").intValue()); + assertEquals(2, distance.apply("qwerty", "erty").intValue()); + assertEquals(2, distance.apply("erty", "qwerty").intValue()); + assertEquals(3, distance.apply("aaa", "bbb").intValue()); + assertEquals(3, distance.apply("abc", "cde").intValue()); + assertEquals(3, distance.apply("testing", "test").intValue()); + } + + //Test 4: Long Values + @Test + public void testLongValues(){ + LevenshteinDistance distance = LevenshteinDistance.getDefaultInstance(); + String word1 = "abc"; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 999; i++) { + sb.append(word1).append("abc"); + } + word1 = sb.toString(); + String word2 = word1; + + String word3 = "def"; + StringBuilder sb2 = new StringBuilder(); + for (int i = 0; i < 999; i++) { + sb2.append(word3).append("def"); + } + word3 = sb2.toString(); + + assertEquals(0, distance.apply(word1, word2).intValue()); + assertTrue(distance.apply(word1, word3) != 0.0); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/EditDistanceFromTest.java b/src/test/java/org/apache/commons/text/similarity/EditDistanceFromTest.java new file mode 100644 index 0000000000..f817086498 --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/EditDistanceFromTest.java @@ -0,0 +1,132 @@ +/* + * 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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; + + +public class EditDistanceFromTest { + + private final EditDistance levenshteinDist = LevenshteinDistance.getDefaultInstance(); + + //Testing using robust boundary value testing and special-case testing + + //First Test: Identical strings + @Test + public void testIdenticalStrings() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, "text"); + assertEquals(0, distanceFrom.apply("text")); + //Should have 0 distance since they are identical strings (no changes are made) + } + + //Second Test: Empty Left string + @Test + public void testEmptyLeftString() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, ""); + assertEquals(4, distanceFrom.apply("text")); + //Should have a distance of 4 since 4 insertions are made + } + + //Third Test: Empty Right String + @Test + public void testEmptyRightString() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, "text"); + assertEquals(4, distanceFrom.apply("")); // 4 deletions + //Should have a distance of 4 since 4 deletions are made + } + + //Fourth Test: Both Strings are empty + @Test + public void testEmptyStrings() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, ""); + assertEquals(0, distanceFrom.apply("")); + //Should have a distance of 0 since they are empty (and identical) strings + } + + //Fifth Test: One character difference + @Test + public void testOneCharaDiff() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, "test"); + assertEquals(1, distanceFrom.apply("text")); + //Should have a distance of 1 since they are one character off + } + + //Sixth Test: Uses special characters + @Test + public void testApply_withSpecialCharacters() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, "Sp3c1@l!"); + assertEquals(4, distanceFrom.apply("Special")); + //Should have a distance of 4 since there are 4 special characters + } + + //Seventh Test: Test maximum value (extremely long strings) + @Test + public void testApply_veryLongStrings() { + StringBuilder longStr = new StringBuilder(); + for (int i = 0; i < 1000; i++) { + longStr.append("a"); + } + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, longStr.toString()); + + //adjusting single character at end + StringBuilder oneCharaChange = new StringBuilder(longStr); + oneCharaChange.setCharAt(999, 'b'); + + assertEquals(1, distanceFrom.apply(oneCharaChange.toString())); + } + + //Eighth Test: Tests Typical Case + @Test + public void testTypicalCase() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, "typical"); + + int distance = distanceFrom.apply("typing"); + + assertEquals(3, distance); + } + + + //The following test cases are special cases (null) in which exceptions should be thrown: + + //Nineth Test: null editDistance + @Test + public void testConstructor_nullEditDistance_throwsException() { + assertThrows(IllegalArgumentException.class, () -> { + new EditDistanceFrom(null, "test"); + }); + } + + //Tenth Test: null Left String + @Test + public void testNullLeftStringException() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, null); + + assertThrows(IllegalArgumentException.class, () -> distanceFrom.apply("right")); + } + + //Eleventh Test: null Right String + @Test + public void testNullRightStringException() { + EditDistanceFrom distanceFrom = new EditDistanceFrom<>(levenshteinDist, "left"); + + assertThrows(IllegalArgumentException.class, () -> distanceFrom.apply(null)); + } + +}