View Javadoc
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 java.util.stream.Stream;
22  
23  import org.junit.jupiter.params.ParameterizedTest;
24  import org.junit.jupiter.params.provider.Arguments;
25  import org.junit.jupiter.params.provider.MethodSource;
26  
27  /**
28   * Tests {@link LevenshteinDistance}.
29   */
30  class ParameterizedLevenshteinDistanceTest {
31  
32      public static Stream<Arguments> parameters() {
33          // @formatter:off
34          return Stream.of(
35              /* empty strings */
36              Arguments.of(0, "", "", 0),
37              Arguments.of(8, "aaapppp", "", 7),
38              Arguments.of(7, "aaapppp", "", 7),
39              Arguments.of(6, "aaapppp", "", -1),
40  
41              /* unequal strings, zero threshold */
42              Arguments.of(0, "b", "a", -1),
43              Arguments.of(0, "a", "b", -1),
44  
45              /* equal strings */
46              Arguments.of(0, "aa", "aa", 0),
47              Arguments.of(2, "aa", "aa", 0),
48  
49              /* same length */
50              Arguments.of(2, "aaa", "bbb", -1),
51              Arguments.of(3, "aaa", "bbb", 3),
52  
53              /* big stripe */
54              Arguments.of(10, "aaaaaa", "b", 6),
55  
56              /* distance less than threshold */
57              Arguments.of(8, "aaapppp", "b", 7),
58              Arguments.of(4, "a", "bbb", 3),
59  
60              /* distance equal to threshold */
61              Arguments.of(7, "aaapppp", "b", 7),
62              Arguments.of(3, "a", "bbb", 3),
63  
64              /* distance greater than threshold */
65              Arguments.of(2, "a", "bbb", -1),
66              Arguments.of(2, "bbb", "a", -1),
67              Arguments.of(6, "aaapppp", "b", -1),
68  
69              /* stripe runs off array, strings not similar */
70              Arguments.of(1, "a", "bbb", -1),
71              Arguments.of(1, "bbb", "a", -1),
72  
73              /* stripe runs off array, strings are similar */
74              Arguments.of(1, "12345", "1234567", -1),
75              Arguments.of(1, "1234567", "12345", -1),
76  
77              /* old getLevenshteinDistance test cases */
78              Arguments.of(1, "frog", "fog", 1),
79              Arguments.of(3, "fly", "ant", 3),
80              Arguments.of(7, "elephant", "hippo", 7),
81              Arguments.of(6, "elephant", "hippo", -1),
82              Arguments.of(7, "hippo", "elephant", 7),
83              Arguments.of(6, "hippo", "elephant", -1),
84              Arguments.of(8, "hippo", "zzzzzzzz", 8),
85              Arguments.of(8, "zzzzzzzz", "hippo", 8),
86              Arguments.of(1, "hello", "hallo", 1),
87  
88              Arguments.of(Integer.MAX_VALUE, "frog", "fog", 1),
89              Arguments.of(Integer.MAX_VALUE, "fly", "ant", 3),
90              Arguments.of(Integer.MAX_VALUE, "elephant", "hippo", 7),
91              Arguments.of(Integer.MAX_VALUE, "hippo", "elephant", 7),
92              Arguments.of(Integer.MAX_VALUE, "hippo", "zzzzzzzz", 8),
93              Arguments.of(Integer.MAX_VALUE, "zzzzzzzz", "hippo", 8),
94              Arguments.of(Integer.MAX_VALUE, "hello", "hallo", 1));
95          // @formatter:on
96      }
97  
98      @ParameterizedTest
99      @MethodSource("parameters")
100     void test(final Integer threshold, final CharSequence left, final CharSequence right, final Integer distance) {
101         assertEquals(distance, new LevenshteinDistance(threshold).apply(left, right));
102     }
103 
104 }