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..32d85752aa --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/EditDistanceFromTest.java @@ -0,0 +1,45 @@ +/* + * 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 + * + * https://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 org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class EditDistanceFromTest { + + private LongestCommonSubsequenceDistance longestCommonSubsequenceDistance; + private EditDistanceFrom editDistanceFrom; + + @BeforeEach + void doBeforeEachTest() { + longestCommonSubsequenceDistance = new LongestCommonSubsequenceDistance(); + editDistanceFrom = new EditDistanceFrom<>(longestCommonSubsequenceDistance, "asdf"); + } + + @Test + void testGetLeft() { + assertEquals("asdf", editDistanceFrom.getLeft()); + } + + @Test + void testGetSimilarityScore() { + assertEquals(longestCommonSubsequenceDistance, editDistanceFrom.getEditDistance()); + } + +} diff --git a/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java b/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java index 334ea748fc..f7ef2ab75f 100644 --- a/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java +++ b/src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java @@ -89,4 +89,14 @@ void testGettingJaccardSimilarityNullString() { void testGettingJaccardSimilarityStringNull() { assertThrows(IllegalArgumentException.class, () -> classBeingTested.apply(" ", null)); } + + @Test + void testGettingJaccardSimilarityNullSimilarityInput() { + assertThrows(IllegalArgumentException.class, () -> classBeingTested.apply(null, new SimilarityCharacterInput("asdf"))); + } + + @Test + void testGettingJaccardSimilaritySimilarityInputNull() { + assertThrows(IllegalArgumentException.class, () -> classBeingTested.apply(new SimilarityCharacterInput("asdf"), null)); + } } diff --git a/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java index 509d58bb59..7df3a10338 100644 --- a/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -105,4 +106,9 @@ void testGetJaroWinklerDistance_StringString() { assertEquals(1 - 0.51111d, distance.apply("foo", " foo"), 0.00001d); } + @Test + void testMatches() { + assertArrayEquals(new int[]{2, 0, 2}, distance.matches("ab", "aba")); + } + } diff --git a/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java index 9ba1baf753..f80cbc7915 100644 --- a/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java @@ -41,6 +41,14 @@ void testApplyThrowsIllegalArgumentExceptionAndCreatesLevenshteinDetailedDistanc void testApplyWithNullSimilarityInput() { assertThrows(IllegalArgumentException.class, () -> new LevenshteinDetailedDistance(0).apply((SimilarityInput) null, (SimilarityInput) null)); + assertThrows(IllegalArgumentException.class, + () -> new LevenshteinDetailedDistance(0).apply(new SimilarityCharacterInput("asdf"), (SimilarityCharacterInput) null)); + assertThrows(IllegalArgumentException.class, + () -> new LevenshteinDetailedDistance(0).apply((SimilarityCharacterInput) null, new SimilarityCharacterInput("asdf"))); + assertThrows(IllegalArgumentException.class, + () -> new LevenshteinDetailedDistance(null).apply(new SimilarityCharacterInput("asdf"), (SimilarityCharacterInput) null)); + assertThrows(IllegalArgumentException.class, + () -> new LevenshteinDetailedDistance(null).apply((SimilarityCharacterInput) null, new SimilarityCharacterInput("asdf"))); } @Test diff --git a/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java b/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java index 99dd5e72b5..8aa3960d72 100644 --- a/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java @@ -34,6 +34,10 @@ class LevenshteinDistanceTest { @Test void testApplyThrowsIllegalArgumentExceptionSimilarityInput() { assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply((SimilarityInput) null, (SimilarityInput) null)); + assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply(new SimilarityCharacterInput("asdf"), + (SimilarityCharacterInput) null)); + assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply((SimilarityCharacterInput) null, + new SimilarityCharacterInput("asdf"))); } @Test @@ -85,6 +89,12 @@ void testGetLevenshteinDistance_StringNullInt(final Class cls) { assertThrows(IllegalArgumentException.class, () -> UNLIMITED_DISTANCE.apply(SimilarityInputTest.build(cls, "a"), SimilarityInputTest.build(cls, null))); } + @Test + void testGetLevenshteinDistance_EmptyStringString() { + assertEquals(-1, new LevenshteinDistance(0).apply(new SimilarityCharacterInput(""), + new SimilarityCharacterInput("asdf"))); + } + @ParameterizedTest @MethodSource("org.apache.commons.text.similarity.SimilarityInputTest#similarityInputs()") void testGetLevenshteinDistance_StringString(final Class cls) { diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java index b280361a1f..e5208a2011 100644 --- a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -133,4 +134,10 @@ void testLongestCommonSubsequenceApply() { assertEquals(4, subject.apply("leettteft", "ritttght")); assertEquals(15, subject.apply("the same string", "the same string")); } + + @Test + void testLongestCommonSubstringLengthArray() { + assertArrayEquals(new int[][]{ {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 2, 2}}, subject.longestCommonSubstringLengthArray("ab", "abc")); + } + } diff --git a/src/test/java/org/apache/commons/text/similarity/SimilarityScoreFromTest.java b/src/test/java/org/apache/commons/text/similarity/SimilarityScoreFromTest.java index 9f141f7e4e..cd037b4dc5 100644 --- a/src/test/java/org/apache/commons/text/similarity/SimilarityScoreFromTest.java +++ b/src/test/java/org/apache/commons/text/similarity/SimilarityScoreFromTest.java @@ -20,17 +20,35 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class SimilarityScoreFromTest { + private LongestCommonSubsequence longestCommonSubsequence; + private SimilarityScoreFrom similarityScoreFrom; + + @BeforeEach + void doBeforeEachTest() { + longestCommonSubsequence = new LongestCommonSubsequence(); + similarityScoreFrom = new SimilarityScoreFrom<>(longestCommonSubsequence, "asdf"); + } + @Test void testApply() { - final LongestCommonSubsequence longestCommonSubsequence = new LongestCommonSubsequence(); - final SimilarityScoreFrom similarityScoreFrom = new SimilarityScoreFrom<>(longestCommonSubsequence, "asdf"); assertEquals(1, similarityScoreFrom.apply("s")); } + @Test + void testGetLeft() { + assertEquals("asdf", similarityScoreFrom.getLeft()); + } + + @Test + void testGetSimilarityScore() { + assertEquals(longestCommonSubsequence, similarityScoreFrom.getSimilarityScore()); + } + @Test void testFailsToCreateSimilarityScoreFromThrowsIllegalArgumentException() { assertThrows(IllegalArgumentException.class, () -> new SimilarityScoreFrom<>(null, ""));