1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpSpellCheck\Tests\Utils; |
6
|
|
|
|
7
|
|
|
use PhpSpellCheck\Utils\SortedNumericArrayNearestValueFinder; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class SortedNumericArrayNearestValueFinderTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public function testFindLowerOrHigher() |
14
|
|
|
{ |
15
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
16
|
|
|
1337, |
17
|
|
|
[1000, 2000, 3000], |
18
|
|
|
SortedNumericArrayNearestValueFinder::FIND_LOWER |
19
|
|
|
); |
20
|
|
|
$this->assertSame(0, $foundNearestLowerIndex); |
21
|
|
|
|
22
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
23
|
|
|
1337, |
24
|
|
|
[1000, 2000, 3000], |
25
|
|
|
SortedNumericArrayNearestValueFinder::FIND_HIGHER |
26
|
|
|
); |
27
|
|
|
$this->assertSame(1, $foundNearestLowerIndex); |
28
|
|
|
|
29
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
30
|
|
|
2000, |
31
|
|
|
[1000, 2000, 3000], |
32
|
|
|
SortedNumericArrayNearestValueFinder::FIND_LOWER |
33
|
|
|
); |
34
|
|
|
$this->assertSame(1, $foundNearestLowerIndex); |
35
|
|
|
|
36
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
37
|
|
|
1500, |
38
|
|
|
[1000, 2000, 3000], |
39
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
40
|
|
|
); |
41
|
|
|
$this->assertSame(1, $foundNearestLowerIndex); |
42
|
|
|
|
43
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
44
|
|
|
1337, |
45
|
|
|
[1000, 2000, 3000], |
46
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
47
|
|
|
); |
48
|
|
|
$this->assertSame(1, $foundNearestLowerIndex); |
49
|
|
|
|
50
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
51
|
|
|
4000, |
52
|
|
|
[1000, 2000, 3000], |
53
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
54
|
|
|
); |
55
|
|
|
$this->assertSame(2, $foundNearestLowerIndex); |
56
|
|
|
|
57
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
58
|
|
|
4000, |
59
|
|
|
[1000, 2000, 3000], |
60
|
|
|
SortedNumericArrayNearestValueFinder::FIND_HIGHER |
61
|
|
|
); |
62
|
|
|
$this->assertSame(2, $foundNearestLowerIndex); |
63
|
|
|
|
64
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
65
|
|
|
50, |
66
|
|
|
[1000, 2000, 3000], |
67
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
68
|
|
|
); |
69
|
|
|
$this->assertSame(0, $foundNearestLowerIndex); |
70
|
|
|
|
71
|
|
|
$foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex( |
72
|
|
|
50, |
73
|
|
|
[1000, 2000, 3000], |
74
|
|
|
SortedNumericArrayNearestValueFinder::FIND_LOWER |
75
|
|
|
); |
76
|
|
|
$this->assertSame(0, $foundNearestLowerIndex); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testFindEmptyArray() |
80
|
|
|
{ |
81
|
|
|
$this->expectException(\InvalidArgumentException::class); |
82
|
|
|
|
83
|
|
|
SortedNumericArrayNearestValueFinder::findIndex( |
84
|
|
|
1500, |
85
|
|
|
[], |
86
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testFindInNonIntArray() |
91
|
|
|
{ |
92
|
|
|
$this->expectException(\InvalidArgumentException::class); |
93
|
|
|
|
94
|
|
|
SortedNumericArrayNearestValueFinder::findIndex( |
95
|
|
|
1500, |
96
|
|
|
['foo'], |
97
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testFindInMixedTypedArray() |
102
|
|
|
{ |
103
|
|
|
$this->expectException(\InvalidArgumentException::class); |
104
|
|
|
|
105
|
|
|
SortedNumericArrayNearestValueFinder::findIndex( |
106
|
|
|
1500, |
107
|
|
|
['foo', 1, 'bar'], |
108
|
|
|
SortedNumericArrayNearestValueFinder::FIND_DEFAULT |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|