forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchesTest.php
147 lines (130 loc) · 4.36 KB
/
SearchesTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Searches/BinarySearch.php';
require_once __DIR__ . '/../../Searches/FibonacciSearch.php';
require_once __DIR__ . '/../../Searches/LinearSearch.php';
require_once __DIR__ . '/../../Searches/LowerBound.php';
require_once __DIR__ . '/../../Searches/UpperBound.php';
require_once __DIR__ . '/../../Searches/JumpSearch.php';
class SearchesTest extends TestCase
{
public function testBinarySearchIterative()
{
$list = [0, 5, 7, 10, 15];
$target = 0;
$result = binarySearchIterative($list, $target);
assertEquals(0, $result);
$target = 15;
$result = binarySearchIterative($list, $target);
assertEquals(4, $result);
$target = 5;
$result = binarySearchIterative($list, $target);
assertEquals(1, $result);
$target = 6;
$result = binarySearchIterative($list, $target);
assertEquals(null, $result);
}
public function testBinarySearchByRecursion()
{
$list = [0, 5, 7, 10, 15];
$target = 0;
$result = binarySearchByRecursion($list, $target, 0, 4);
assertEquals(0, $result);
$target = 15;
$result = binarySearchByRecursion($list, $target, 0, 4);
assertEquals(4, $result);
$target = 5;
$result = binarySearchByRecursion($list, $target, 0, 4);
assertEquals(1, $result);
$target = 6;
$result = binarySearchByRecursion($list, $target, 0, 4);
assertEquals(null, $result);
}
public function testBinarySearchByRecursionWithEmptyList()
{
$list = [];
$target = 0;
$result = binarySearchByRecursion($list, $target, 0, 0);
assertEquals(null, $result);
}
public function testBinarySearchByRecursionWithOneElementList()
{
$list = [0];
$target = 0;
$result = binarySearchByRecursion($list, $target, 0, 0);
assertEquals(0, $result);
$target = 1;
$result = binarySearchByRecursion($list, $target, 0, 0);
assertEquals(null, $result);
}
public function testBinarySearchByRecursionWithTwoElementList()
{
$list = [0, 1];
$target = 0;
$result = binarySearchByRecursion($list, $target, 0, 1);
assertEquals(0, $result);
$target = 1;
$result = binarySearchByRecursion($list, $target, 0, 1);
assertEquals(1, $result);
$target = 2;
$result = binarySearchByRecursion($list, $target, 0, 1);
assertEquals(null, $result);
}
public function testBinarySearchByRecursionWithThreeElementList()
{
$list = [0, 1, 2];
$target = 0;
$result = binarySearchByRecursion($list, $target, 0, 2);
assertEquals(0, $result);
$target = 1;
$result = binarySearchByRecursion($list, $target, 0, 2);
assertEquals(1, $result);
$target = 2;
$result = binarySearchByRecursion($list, $target, 0, 2);
assertEquals(2, $result);
$target = 3;
$result = binarySearchByRecursion($list, $target, 0, 2);
assertEquals(null, $result);
}
public function testFibonacciSearch()
{
$test1 = fibonacciPosition(6);
assertEquals(8, $test1);
$test2 = fibonacciPosition(9);
assertEquals(34, $test2);
$test3 = fibonacciPosition(60);
assertEquals(1548008755920, $test3);
}
public function testLinearSearch()
{
$list = [5, 7, 8, 11, 12, 15, 17, 18, 20];
$target = 15;
$result = linearSearch($list, $target);
assertEquals(6, $result);
}
public function testLowerBound()
{
$list = [1, 2, 3, 3, 3, 4, 5, 9];
$target = 3;
$result = lowerBound($list, $target);
assertEquals(2, $result);
}
public function testUpperBound()
{
$list = [1, 2, 3, 3, 3, 4, 5, 9];
$target = 3;
$result = upperBound($list, $target);
assertEquals(5, $result);
}
public function testJumpSearch()
{
$list = array( 3,5,6,7,9,10,12,20,22,24);
$target = 12;
$result = jumpSearch($list, $target);
assertEquals(6, $result);
}
}