Skip to content
Original file line number Diff line number Diff line change
@@ -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<Integer> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ void testApplyThrowsIllegalArgumentExceptionAndCreatesLevenshteinDetailedDistanc
void testApplyWithNullSimilarityInput() {
assertThrows(IllegalArgumentException.class,
() -> new LevenshteinDetailedDistance(0).apply((SimilarityInput<Object>) null, (SimilarityInput<Object>) 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class LevenshteinDistanceTest {
@Test
void testApplyThrowsIllegalArgumentExceptionSimilarityInput() {
assertThrows(IllegalArgumentException.class, () -> new LevenshteinDistance(0).apply((SimilarityInput<Object>) null, (SimilarityInput<Object>) 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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> similarityScoreFrom;

@BeforeEach
void doBeforeEachTest() {
longestCommonSubsequence = new LongestCommonSubsequence();
similarityScoreFrom = new SimilarityScoreFrom<>(longestCommonSubsequence, "asdf");
}

@Test
void testApply() {
final LongestCommonSubsequence longestCommonSubsequence = new LongestCommonSubsequence();
final SimilarityScoreFrom<Integer> 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, ""));
Expand Down