@@ -24,6 +24,11 @@ public function testDistance(int $expected, string $a, string $b, int $maxDistan
24
24
$ this ->assertSame ($ expected - 1 , DamerauLevenshtein::distance ($ a , $ b , $ expected - 1 ));
25
25
$ this ->assertSame ($ expected - 1 , DamerauLevenshtein::distance ($ b , $ a , $ expected - 1 ));
26
26
}
27
+
28
+ $ this ->assertSame ($ expected * 2 , DamerauLevenshtein::distance ($ a , $ b , $ maxDistance === PHP_INT_MAX ? PHP_INT_MAX : $ maxDistance * 2 , 2 , 2 , 2 , 2 ));
29
+ $ this ->assertSame ($ expected * 2 , DamerauLevenshtein::distance ($ b , $ a , $ maxDistance === PHP_INT_MAX ? PHP_INT_MAX : $ maxDistance * 2 , 2 , 2 , 2 , 2 ));
30
+ $ this ->assertSame ($ expected * 4 , DamerauLevenshtein::distance ($ a , $ b , $ maxDistance === PHP_INT_MAX ? PHP_INT_MAX : $ maxDistance * 4 , 4 , 4 , 4 , 4 ));
31
+ $ this ->assertSame ($ expected * 4 , DamerauLevenshtein::distance ($ b , $ a , $ maxDistance === PHP_INT_MAX ? PHP_INT_MAX : $ maxDistance * 4 , 4 , 4 , 4 , 4 ));
27
32
}
28
33
29
34
public static function distanceProvider (): \Generator {
@@ -123,12 +128,63 @@ public static function distanceProvider(): \Generator {
123
128
yield [4 , 'стул ' , 'вода ' ];
124
129
125
130
yield [1 , 'aaäaa ' , 'aaöaa ' ];
126
- yield [1 , 'prefix\xF0\x9F\x92\xA9 ' , 'prefix\xF0\x9F\x92\xAF ' ];
127
- yield [1 , 'prefix\xF0\x9F\x92\xA9 ' , 'prefix\xF0\x9F\x93\xA9 ' ];
128
- yield [1 , '\xF0\x9F\x92\xA9suffix ' , '\xF0\x9F\x92\xAFsuffix ' ];
129
- yield [1 , '\xF0\x9F\x92\xA9suffix ' , '\xF0\x9F\x93\xA9suffix ' ];
130
- yield [1 , 'prefix\xF0\x9F\x92\xA9suffix ' , 'prefix\xF0\x9F\x92\xAFsuffix ' ];
131
- yield [1 , 'prefix\xF0\x9F\x92\xA9suffix ' , 'prefix\xF0\x9F\x93\xA9suffix ' ];
131
+ yield [1 , "prefix \xF0\x9F\x92\xA9" , "prefix \xF0\x9F\x92\xAF" ];
132
+ yield [1 , "prefix \xF0\x9F\x92\xA9" , "prefix \xF0\x9F\x93\xA9" ];
133
+ yield [1 , "\xF0\x9F\x92\xA9suffix " , "\xF0\x9F\x92\xAFsuffix " ];
134
+ yield [1 , "\xF0\x9F\x92\xA9suffix " , "\xF0\x9F\x93\xA9suffix " ];
135
+ yield [1 , "prefix \xF0\x9F\x92\xA9suffix " , "prefix \xF0\x9F\x92\xAFsuffix " ];
136
+ yield [1 , "prefix \xF0\x9F\x92\xA9suffix " , "prefix \xF0\x9F\x93\xA9suffix " ];
137
+
138
+ }
139
+
140
+ /**
141
+ * @dataProvider costsProvider
142
+ */
143
+ public function testDifferentCosts (int $ expected , string $ a , string $ b , int $ insertionCost = 1 , int $ replacementCost = 1 , int $ deletionCost = 1 , int $ transpositionCost = 1 ): void
144
+ {
145
+ $ this ->assertSame ($ expected , DamerauLevenshtein::distance ($ a , $ b , PHP_INT_MAX , $ insertionCost , $ replacementCost , $ deletionCost , $ transpositionCost ));
146
+ $ this ->assertSame ($ expected , DamerauLevenshtein::distance ($ b , $ a , PHP_INT_MAX , $ deletionCost , $ replacementCost , $ insertionCost , $ transpositionCost ));
147
+ $ this ->assertSame ($ expected , DamerauLevenshtein::distance ($ a , $ b , $ expected , $ insertionCost , $ replacementCost , $ deletionCost , $ transpositionCost ));
148
+ $ this ->assertSame ($ expected , DamerauLevenshtein::distance ($ b , $ a , $ expected , $ deletionCost , $ replacementCost , $ insertionCost , $ transpositionCost ));
149
+ $ this ->assertSame ($ expected , DamerauLevenshtein::distance ($ a , $ b , $ expected + 1 , $ insertionCost , $ replacementCost , $ deletionCost , $ transpositionCost ));
150
+ $ this ->assertSame ($ expected , DamerauLevenshtein::distance ($ b , $ a , $ expected + 1 , $ deletionCost , $ replacementCost , $ insertionCost , $ transpositionCost ));
151
+
152
+ if ($ expected > 0 ) {
153
+ $ this ->assertSame ($ expected - 1 , DamerauLevenshtein::distance ($ a , $ b , $ expected - 1 , $ insertionCost , $ replacementCost , $ deletionCost , $ transpositionCost ));
154
+ $ this ->assertSame ($ expected - 1 , DamerauLevenshtein::distance ($ b , $ a , $ expected - 1 , $ deletionCost , $ replacementCost , $ insertionCost , $ transpositionCost ));
155
+ }
156
+ }
132
157
158
+ public static function costsProvider (): \Generator
159
+ {
160
+ yield [7 , 'abc ' , 'bcd ' , 3 , 8 , 4 ];
161
+ yield [3 , 'abc ' , 'bcd ' , 2 , 1 , 3 ];
162
+ yield [4 , 'abcd ' , 'acbd ' , 1 , 2 , 3 , 4 ];
163
+ yield [4 , 'abcd ' , 'acbd ' , 2 , 2 , 3 , 5 ];
164
+ yield [4 , 'abcd ' , 'acbd ' , 1 , 3 , 3 , 5 ];
165
+ yield [4 , 'abcd ' , 'acbd ' , 2 , 3 , 3 , 4 ];
166
+ yield [5 , 'abcd ' , 'acbd ' , 2 , 3 , 3 , 5 ];
167
+ yield [5 , 'abcd ' , 'acbd ' , 2 , 3 , 3 , 6 ];
168
+ yield [6 , 'abcd ' , 'acbd ' , 2 , 3 , 4 , 6 ];
169
+ yield [1 , 'abcd ' , 'abcde ' , 1 , 2 , 2 , 2 ];
170
+ yield [1 , 'abcd ' , 'abcde ' , 1 , 99 , 99 , 99 ];
171
+ yield [1 , 'abcd ' , 'aXcd ' , 2 , 1 , 2 , 2 ];
172
+ yield [1 , 'abcd ' , 'aXcd ' , 99 , 1 , 99 , 99 ];
173
+ yield [1 , 'abcd ' , 'abc ' , 2 , 2 , 1 , 2 ];
174
+ yield [1 , 'abcd ' , 'abc ' , 99 , 99 , 1 , 99 ];
175
+ yield [1 , 'abcd ' , 'acbd ' , 2 , 2 , 2 , 1 ];
176
+ yield [1 , 'abcd ' , 'acbd ' , 99 , 99 , 99 , 1 ];
177
+ yield [2 , 'abcd ' , 'abcde ' , 2 , 3 , 3 , 3 ];
178
+ yield [2 , 'abcd ' , 'abcde ' , 2 , 99 , 99 , 99 ];
179
+ yield [2 , 'abcd ' , 'aXcd ' , 3 , 2 , 3 , 3 ];
180
+ yield [2 , 'abcd ' , 'aXcd ' , 99 , 2 , 99 , 99 ];
181
+ yield [2 , 'abcd ' , 'abc ' , 3 , 3 , 2 , 3 ];
182
+ yield [2 , 'abcd ' , 'abc ' , 99 , 99 , 2 , 99 ];
183
+ yield [2 , 'abcd ' , 'acbd ' , 3 , 3 , 3 , 2 ];
184
+ yield [2 , 'abcd ' , 'acbd ' , 99 , 99 , 99 , 2 ];
185
+ yield [13 , 'aaaa ' , 'bbbbb ' , 1 , 99 , 2 , 99 ];
186
+ yield [14 , 'aaaaa ' , 'bbbb ' , 1 , 99 , 2 , 99 ];
187
+ yield [14 , 'aaaa ' , 'bbbbb ' , 2 , 99 , 1 , 99 ];
188
+ yield [13 , 'aaaaa ' , 'bbbb ' , 2 , 99 , 1 , 99 ];
133
189
}
134
190
}
0 commit comments