Completed
Push — master ( 49952d...105986 )
by Mischa ter
02:18
created

DamerauLevenshteinTest::testDisplayMatrix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
namespace Oefenweb\DamerauLevenshtein\Test;
3
4
use Oefenweb\DamerauLevenshtein\DamerauLevenshtein;
5
6
class DamerauLevenshteinTest extends \PHPUnit_Framework_TestCase
7
{
8
9
    /**
10
     * Tests `getSimilarity`.
11
     *
12
     * @return void
13
     */
14
    public function testGetSimilarity()
15
    {
16
        $inputs = [
17
            ['foo', 'foo'],
18
            ['foo', 'fooo'],
19
            ['foo', 'bar'],
20
21
            ['123', '12'],
22
            ['qwe', 'qwa'],
23
            ['awe', 'qwe'],
24
            ['фыв', 'фыа'],
25
            ['vvvqw', 'vvvwq'],
26
            ['qw', 'wq'],
27
            ['qq', 'ww'],
28
            ['qw', 'qw'],
29
            ['пионер', 'плеер'],
30
            ['пионер', 'пионеер'],
31
            ['пионер', 'поинер'],
32
            ['pioner', 'poner'],
33
            ['пионер', 'понер'],
34
        ];
35
        $outputs = [
36
            0,
37
            1,
38
            3,
39
40
            1,
41
            1,
42
            1,
43
            1,
44
            1,
45
            1,
46
            2,
47
            0,
48
            3,
49
            1,
50
            1,
51
            1,
52
            1,
53
        ];
54
55
        foreach ($inputs as $i => $input) {
56
            $DamerauLevenshtein = new DamerauLevenshtein($input[0], $input[1]);
57
            $result = $DamerauLevenshtein->getSimilarity();
58
            $expected = $outputs[$i];
59
60
            $this->assertSame($expected, $result);
61
        }
62
    }
63
64
    /**
65
     * Tests `getInsCost`.
66
     *
67
     * @return void
68
     */
69 View Code Duplication
    public function testGetInsCost()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        list($firstString, $secondString) = $this->getDefaultStrings();
72
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
73
74
        // Default insert cost
75
76
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
77
        $result = $DamerauLevenshtein->getInsCost();
78
        $expected = $insCost;
79
80
        $this->assertSame($expected, $result);
81
82
        // Non-default insert cost
83
84
        $insCost = 2;
85
86
        $DamerauLevenshtein = new DamerauLevenshtein(
87
            $firstString,
88
            $secondString,
89
            $insCost,
90
            $delCost,
91
            $subCost,
92
            $transCost
93
        );
94
        $result = $DamerauLevenshtein->getInsCost();
95
        $expected = $insCost;
96
97
        $this->assertSame($expected, $result);
98
    }
99
100
    /**
101
     * Tests `getDelCost`.
102
     *
103
     * @return void
104
     */
105 View Code Duplication
    public function testGetDelCost()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        list($firstString, $secondString) = $this->getDefaultStrings();
108
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
109
110
        // Default delete cost
111
112
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
113
        $result = $DamerauLevenshtein->getDelCost();
114
        $expected = $delCost;
115
116
        $this->assertSame($expected, $result);
117
118
        // Non-default delete cost
119
120
        $delCost = 2;
121
122
        $DamerauLevenshtein = new DamerauLevenshtein(
123
            $firstString,
124
            $secondString,
125
            $insCost,
126
            $delCost,
127
            $subCost,
128
            $transCost
129
        );
130
        $result = $DamerauLevenshtein->getDelCost();
131
        $expected = $delCost;
132
133
        $this->assertSame($expected, $result);
134
    }
135
136
    /**
137
     * Tests `getSubCost`.
138
     *
139
     * @return void
140
     */
141 View Code Duplication
    public function testGetSubCost()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        list($firstString, $secondString) = $this->getDefaultStrings();
144
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
145
146
        // Default substitution cost
147
148
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
149
        $result = $DamerauLevenshtein->getSubCost();
150
        $expected = $subCost;
151
152
        $this->assertSame($expected, $result);
153
154
        // Non-default substitution cost
155
156
        $subCost = 2;
157
158
        $DamerauLevenshtein = new DamerauLevenshtein(
159
            $firstString,
160
            $secondString,
161
            $insCost,
162
            $delCost,
163
            $subCost,
164
            $transCost
165
        );
166
        $result = $DamerauLevenshtein->getSubCost();
167
        $expected = $subCost;
168
169
        $this->assertSame($expected, $result);
170
    }
171
172
    /**
173
     * Tests `getTransCost`.
174
     *
175
     * @return void
176
     */
177 View Code Duplication
    public function testGetTransCost()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        list($firstString, $secondString) = $this->getDefaultStrings();
180
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
181
182
        // Default transposition cost
183
184
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
185
        $result = $DamerauLevenshtein->getTransCost();
186
        $expected = $transCost;
187
188
        $this->assertSame($expected, $result);
189
190
        // Non-default transposition cost
191
192
        $transCost = 2;
193
194
        $DamerauLevenshtein = new DamerauLevenshtein(
195
            $firstString,
196
            $secondString,
197
            $insCost,
198
            $delCost,
199
            $subCost,
200
            $transCost
201
        );
202
        $result = $DamerauLevenshtein->getTransCost();
203
        $expected = $transCost;
204
205
        $this->assertSame($expected, $result);
206
    }
207
208
    /**
209
     * Tests `getRelativeDistance`.
210
     *
211
     * @return void
212
     */
213
    public function testGetRelativeDistance()
214
    {
215
        $delta = pow(10, -4);
216
217
        $firstString = 'O\'Callaghan';
218
        $secondString = 'OCallaghan';
219
220
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
221
        $result = $DamerauLevenshtein->getRelativeDistance();
222
        $expected = 0.90909090909091;
223
        $this->assertEquals($expected, $result, '', $delta);
224
225
        $firstString = 'Thom';
226
        $secondString = 'Mira';
227
228
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
229
        $result = $DamerauLevenshtein->getRelativeDistance();
230
        $expected = 0.0;
231
        $this->assertEquals($expected, $result, '', $delta);
232
233
        $firstString = 'Oldeboom';
234
        $secondString = 'Ven';
235
236
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
237
        $result = $DamerauLevenshtein->getRelativeDistance();
238
        $expected = 0.125;
239
        $this->assertEquals($expected, $result, '', $delta);
240
241
        $firstString = 'ven';
242
        $secondString = 'Ven';
243
244
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
245
        $result = $DamerauLevenshtein->getRelativeDistance();
246
        $expected = 0.66666666666667;
247
        $this->assertEquals($expected, $result, '', $delta);
248
249
        $firstString = 'enV';
250
        $secondString = 'Ven';
251
252
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
253
        $result = $DamerauLevenshtein->getRelativeDistance();
254
        $expected = 0.33333333333333;
255
        $this->assertEquals($expected, $result, '', $delta);
256
    }
257
258
    /**
259
     * Tests `getMatrix`.
260
     *
261
     * @return void
262
     */
263
    public function testGetMatrix()
264
    {
265
        list($firstString, $secondString) = $this->getDefaultStrings();
266
267
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
268
        $actual = $DamerauLevenshtein->getMatrix();
269
        $expected = [
270
            [0, 1, 2, 3],
271
            [1, 1, 2, 3],
272
            [2, 2, 2, 3],
273
            [3, 3, 3, 3]
274
        ];
275
        $this->assertSame($expected, $actual);
276
    }
277
278
    /**
279
     * Tests `displayMatrix`.
280
     *
281
     * @return void
282
     */
283
    public function testDisplayMatrix()
284
    {
285
        list($firstString, $secondString) = $this->getDefaultStrings();
286
287
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
288
        $actual = $DamerauLevenshtein->displayMatrix();
289
        $expected = implode('', [
290
            "  foo\n",
291
            " 0123\n",
292
            "b1123\n",
293
            "a2223\n",
294
            "r3333\n",
295
        ]);
296
        $this->assertSame($expected, $actual);
297
    }
298
299
    /**
300
     * Returns the default costs.
301
     *
302
     * @return array Costs (insert, delete, substitution, transposition)
303
     */
304
    protected function getDefaultCosts()
305
    {
306
        $insCost = 1;
307
        $delCost = 1;
308
        $subCost = 1;
309
        $transCost = 1;
310
311
        return [$insCost, $delCost, $subCost, $transCost];
312
    }
313
314
    /**
315
     * Returns the default strings.
316
     *
317
     * @return array Strings (foo, bar)
318
     */
319
    protected function getDefaultStrings()
320
    {
321
        $firstString = 'foo';
322
        $secondString = 'bar';
323
324
        return [$firstString, $secondString];
325
    }
326
}
327