Failed Conditions
Push — master ( e5bf92...5d5105 )
by Philippe
64:27
created

SortedNumericArrayNearestValueFinderTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testFindLowerOrHigher() 0 64 1
A testFindInNonIntArray() 0 8 1
A testFindEmptyArray() 0 8 1
A testFindInMixedTypedArray() 0 8 1
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