Skip to content

Commit ebf719b

Browse files
committed
closes #1: bugfix
1 parent e5349e2 commit ebf719b

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/BinarySearch/BinarySearch.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function search(): void
3535
throw new InvalidArgumentException('The $directionCallback attribute must be instance of the type callback. Set attribute using by $class->setDirectionClass(fn($a, $b) => $a > $b)');
3636
}
3737

38+
$this->foundIndex = false;
39+
$this->foundValue = null;
40+
3841
$this->loop();
3942
}
4043

src/SearchAbstract.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ abstract class SearchAbstract implements SearchInterface
2121
/**
2222
* The found data in which index
2323
*/
24-
protected int|string $foundIndex = -1;
24+
protected int|string|bool $foundIndex = false;
2525

2626
/**
2727
* The found data
2828
*/
2929
protected mixed $foundValue = null;
3030

3131
/**
32-
* @return int|string
32+
* @return int|string|bool
3333
*/
34-
public function getFoundIndex(): int|string
34+
public function getFoundIndex(): int|string|bool
3535
{
3636
return $this->foundIndex;
3737
}

src/SearchInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function setSearchValue(int|string $searchValue): self;
1212

1313
public function getSearchValue(): null|int|string;
1414

15-
public function getFoundIndex(): null|int|string;
15+
public function getFoundIndex(): null|int|string|bool;
1616

1717
public function getFoundValue(): mixed;
1818

tests/BinarySearchTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ public function testBinarySearchLoopNormalArray(): void
7676

7777
$this->assertSame(98588, $search->getFoundIndex());
7878
$this->assertSame(98589, $search->getFoundValue());
79+
}
80+
81+
public function testBinarysearchNotFoundData(): void
82+
{
83+
$array = [];
84+
85+
for ($index = 0; $index < 100 * 10000; $index++) {
86+
$array[] = $index + 1;
87+
}
7988

80-
/**
81-
* There is not found data
82-
*/
8389
$search = new BinarySearch();
8490
$search
8591
->setCompareCallback(fn ($current, $searchValue) => $current === $searchValue)
@@ -88,7 +94,9 @@ public function testBinarySearchLoopNormalArray(): void
8894
->setSearchValue(999999999)
8995
->search();
9096

91-
$this->assertSame(-1, $search->getFoundIndex());
97+
$foundIndex = $search->getFoundIndex();
98+
99+
$this->assertFalse($foundIndex);
92100
}
93101

94102
public function testBinarySearchLoopArraysInArray(): void

0 commit comments

Comments
 (0)