1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
56 { 0, "", "", 0 },
57 { 8, "aaapppp", "", 7 },
58 { 7, "aaapppp", "", 7 },
59 { 6, "aaapppp", "", -1 },
60
61
62 { 0, "b", "a", -1 },
63 { 0, "a", "b", -1 },
64
65
66 { 0, "aa", "aa", 0 },
67 { 2, "aa", "aa", 0 },
68
69
70 { 2, "aaa", "bbb", -1 },
71 { 3, "aaa", "bbb", 3 },
72
73
74 { 10, "aaaaaa", "b", 6 },
75
76
77 { 8, "aaapppp", "b", 7 },
78 { 4, "a", "bbb", 3 },
79
80
81 { 7, "aaapppp", "b", 7 },
82 { 3, "a", "bbb", 3 },
83
84
85 { 2, "a", "bbb", -1 },
86 { 2, "bbb", "a", -1 },
87 { 6, "aaapppp", "b", -1 },
88
89
90 { 1, "a", "bbb", -1 },
91 { 1, "bbb", "a", -1 },
92
93
94 { 1, "12345", "1234567", -1 },
95 { 1, "1234567", "12345", -1 },
96
97
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 }