Skip to content

Commit f99e435

Browse files
TheRealHauiTheRealHauigarydgregory
authored
Added new tests for the main Levenshtein algorithm implementations (#728)
* Added new tests for the main Levenshtein algorithm implementation classes * Added new tests for the main Levenshtein algorithm implementation classes * Added tests * Remove extra vertical whitespace * Added tests --------- Co-authored-by: TheRealHaui <[email protected]> Co-authored-by: Gary Gregory <[email protected]>
1 parent 4229741 commit f99e435

File tree

7 files changed

+106
-2
lines changed

7 files changed

+106
-2
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.text.similarity;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
class EditDistanceFromTest {
25+
26+
private LongestCommonSubsequenceDistance longestCommonSubsequenceDistance;
27+
private EditDistanceFrom<Integer> editDistanceFrom;
28+
29+
@BeforeEach
30+
void doBeforeEachTest() {
31+
longestCommonSubsequenceDistance = new LongestCommonSubsequenceDistance();
32+
editDistanceFrom = new EditDistanceFrom<>(longestCommonSubsequenceDistance, "asdf");
33+
}
34+
35+
@Test
36+
void testGetLeft() {
37+
assertEquals("asdf", editDistanceFrom.getLeft());
38+
}
39+
40+
@Test
41+
void testGetSimilarityScore() {
42+
assertEquals(longestCommonSubsequenceDistance, editDistanceFrom.getEditDistance());
43+
}
44+
45+
}

src/test/java/org/apache/commons/text/similarity/JaccardSimilarityTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ void testGettingJaccardSimilarityNullString() {
8989
void testGettingJaccardSimilarityStringNull() {
9090
assertThrows(IllegalArgumentException.class, () -> classBeingTested.apply(" ", null));
9191
}
92+
93+
@Test
94+
void testGettingJaccardSimilarityNullSimilarityInput() {
95+
assertThrows(IllegalArgumentException.class, () -> classBeingTested.apply(null, new SimilarityCharacterInput("asdf")));
96+
}
97+
98+
@Test
99+
void testGettingJaccardSimilaritySimilarityInputNull() {
100+
assertThrows(IllegalArgumentException.class, () -> classBeingTested.apply(new SimilarityCharacterInput("asdf"), null));
101+
}
92102
}

src/test/java/org/apache/commons/text/similarity/JaroWinklerDistanceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2122

2223
import org.junit.jupiter.api.BeforeAll;
2324
import org.junit.jupiter.api.Test;
@@ -105,4 +106,9 @@ void testGetJaroWinklerDistance_StringString() {
105106
assertEquals(1 - 0.51111d, distance.apply("foo", " foo"), 0.00001d);
106107
}
107108

109+
@Test
110+
void testMatches() {
111+
assertArrayEquals(new int[]{2, 0, 2}, distance.matches("ab", "aba"));
112+
}
113+
108114
}

src/test/java/org/apache/commons/text/similarity/LevenshteinDetailedDistanceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ void testApplyThrowsIllegalArgumentExceptionAndCreatesLevenshteinDetailedDistanc
4141
void testApplyWithNullSimilarityInput() {
4242
assertThrows(IllegalArgumentException.class,
4343
() -> new LevenshteinDetailedDistance(0).apply((SimilarityInput<Object>) null, (SimilarityInput<Object>) null));
44+
assertThrows(IllegalArgumentException.class,
45+
() -> new LevenshteinDetailedDistance(0).apply(new SimilarityCharacterInput("asdf"), (SimilarityCharacterInput) null));
46+
assertThrows(IllegalArgumentException.class,
47+
() -> new LevenshteinDetailedDistance(0).apply((SimilarityCharacterInput) null, new SimilarityCharacterInput("asdf")));
48+
assertThrows(IllegalArgumentException.class,
49+
() -> new LevenshteinDetailedDistance(null).apply(new SimilarityCharacterInput("asdf"), (SimilarityCharacterInput) null));
50+
assertThrows(IllegalArgumentException.class,
51+
() -> new LevenshteinDetailedDistance(null).apply((SimilarityCharacterInput) null, new SimilarityCharacterInput("asdf")));
4452
}
4553

4654
@Test

src/test/java/org/apache/commons/text/similarity/LevenshteinDistanceTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class LevenshteinDistanceTest {
3434
@Test
3535
void testApplyThrowsIllegalArgumentExceptionSimilarityInput() {
3636
assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply((SimilarityInput<Object>) null, (SimilarityInput<Object>) null));
37+
assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply(new SimilarityCharacterInput("asdf"),
38+
(SimilarityCharacterInput) null));
39+
assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply((SimilarityCharacterInput) null,
40+
new SimilarityCharacterInput("asdf")));
3741
}
3842

3943
@Test
@@ -85,6 +89,12 @@ void testGetLevenshteinDistance_StringNullInt(final Class<?> cls) {
8589
assertThrows(IllegalArgumentException.class, () -> UNLIMITED_DISTANCE.apply(SimilarityInputTest.build(cls, "a"), SimilarityInputTest.build(cls, null)));
8690
}
8791

92+
@Test
93+
void testGetLevenshteinDistance_EmptyStringString() {
94+
assertEquals(-1, new LevenshteinDistance(0).apply(new SimilarityCharacterInput(""),
95+
new SimilarityCharacterInput("asdf")));
96+
}
97+
8898
@ParameterizedTest
8999
@MethodSource("org.apache.commons.text.similarity.SimilarityInputTest#similarityInputs()")
90100
void testGetLevenshteinDistance_StringString(final Class<?> cls) {

src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2122

2223
import org.junit.jupiter.api.BeforeAll;
2324
import org.junit.jupiter.api.Test;
@@ -133,4 +134,10 @@ void testLongestCommonSubsequenceApply() {
133134
assertEquals(4, subject.apply("leettteft", "ritttght"));
134135
assertEquals(15, subject.apply("the same string", "the same string"));
135136
}
137+
138+
@Test
139+
void testLongestCommonSubstringLengthArray() {
140+
assertArrayEquals(new int[][]{ {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 2, 2}}, subject.longestCommonSubstringLengthArray("ab", "abc"));
141+
}
142+
136143
}

src/test/java/org/apache/commons/text/similarity/SimilarityScoreFromTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,35 @@
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222

23+
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425

2526
class SimilarityScoreFromTest {
2627

28+
private LongestCommonSubsequence longestCommonSubsequence;
29+
private SimilarityScoreFrom<Integer> similarityScoreFrom;
30+
31+
@BeforeEach
32+
void doBeforeEachTest() {
33+
longestCommonSubsequence = new LongestCommonSubsequence();
34+
similarityScoreFrom = new SimilarityScoreFrom<>(longestCommonSubsequence, "asdf");
35+
}
36+
2737
@Test
2838
void testApply() {
29-
final LongestCommonSubsequence longestCommonSubsequence = new LongestCommonSubsequence();
30-
final SimilarityScoreFrom<Integer> similarityScoreFrom = new SimilarityScoreFrom<>(longestCommonSubsequence, "asdf");
3139
assertEquals(1, similarityScoreFrom.apply("s"));
3240
}
3341

42+
@Test
43+
void testGetLeft() {
44+
assertEquals("asdf", similarityScoreFrom.getLeft());
45+
}
46+
47+
@Test
48+
void testGetSimilarityScore() {
49+
assertEquals(longestCommonSubsequence, similarityScoreFrom.getSimilarityScore());
50+
}
51+
3452
@Test
3553
void testFailsToCreateSimilarityScoreFromThrowsIllegalArgumentException() {
3654
assertThrows(IllegalArgumentException.class, () -> new SimilarityScoreFrom<>(null, ""));

0 commit comments

Comments
 (0)