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    *      http://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.hamcrest.core.IsEqual.equalTo;
20  import static org.junit.Assert.assertThat;
21  
22  import java.util.Arrays;
23  
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.junit.runners.Parameterized;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  /**
30   * Unit tests for {@link org.apache.commons.text.similarity.LevenshteinDistance}.
31   */
32  @RunWith(Parameterized.class)
33  public class ParameterizedLevenshteinDistanceTest {
34  
35      private final Integer distance;
36      private final CharSequence left;
37      private final CharSequence right;
38      private final Integer threshold;
39  
40      public ParameterizedLevenshteinDistanceTest(
41          final Integer threshold,
42          final CharSequence left, final CharSequence right,
43          final Integer distance) {
44  
45          this.threshold = threshold;
46          this.left = left;
47          this.right = right;
48          this.distance = distance;
49      }
50  
51      @Parameters
52      public static Iterable<Object[]> parameters() {
53          return Arrays.asList( new Object[][] {
54  
55              /* empty strings */
56              { 0, "", "", 0 },
57              { 8, "aaapppp", "", 7 },
58              { 7, "aaapppp", "", 7 },
59              { 6, "aaapppp", "", -1 },
60  
61              /* unequal strings, zero threshold */
62              { 0, "b", "a", -1 },
63              { 0, "a", "b", -1 },
64  
65              /* equal strings */
66              { 0, "aa", "aa", 0 },
67              { 2, "aa", "aa", 0 },
68  
69              /* same length */
70              { 2, "aaa", "bbb", -1 },
71              { 3, "aaa", "bbb", 3 },
72  
73              /* big stripe */
74              { 10, "aaaaaa", "b", 6 },
75  
76              /* distance less than threshold */
77              { 8, "aaapppp", "b", 7 },
78              { 4, "a", "bbb", 3 },
79  
80              /* distance equal to threshold */
81              { 7, "aaapppp", "b", 7 },
82              { 3, "a", "bbb", 3 },
83  
84              /* distance greater than threshold */
85              { 2, "a", "bbb", -1 },
86              { 2, "bbb", "a", -1 },
87              { 6, "aaapppp", "b", -1 },
88  
89              /* stripe runs off array, strings not similar */
90              { 1, "a", "bbb", -1 },
91              { 1, "bbb", "a", -1 },
92  
93              /* stripe runs off array, strings are similar */
94              { 1, "12345", "1234567", -1 },
95              { 1, "1234567", "12345", -1 },
96  
97             /* old getLevenshteinDistance test cases */
98              { 1, "frog", "fog", 1 },
99              { 3, "fly", "ant", 3 },
100             { 7, "elephant", "hippo", 7 },
101             { 6, "elephant", "hippo", -1 },
102             { 7, "hippo", "elephant", 7 },
103             { 6, "hippo", "elephant", -1 },
104             { 8, "hippo", "zzzzzzzz", 8 },
105             { 8, "zzzzzzzz", "hippo", 8 },
106             { 1, "hello", "hallo", 1 },
107 
108             { Integer.MAX_VALUE, "frog", "fog", 1 },
109             { Integer.MAX_VALUE, "fly", "ant", 3 },
110             { Integer.MAX_VALUE, "elephant", "hippo", 7 },
111             { Integer.MAX_VALUE, "hippo", "elephant", 7 },
112             { Integer.MAX_VALUE, "hippo", "zzzzzzzz", 8 },
113             { Integer.MAX_VALUE, "zzzzzzzz", "hippo", 8 },
114             { Integer.MAX_VALUE, "hello", "hallo", 1 }
115 
116         } );
117     }
118 
119     @Test
120     public void test() {
121         final LevenshteinDistance metric = new LevenshteinDistance(threshold);
122         assertThat(metric.apply(left, right), equalTo(distance));
123     }
124 
125 }