diff --git a/src/Datastructure/Lists/ArrayLists/StringBuilder.php b/src/Datastructure/Lists/ArrayLists/StringBuilder.php index b58c2af..766c704 100644 --- a/src/Datastructure/Lists/ArrayLists/StringBuilder.php +++ b/src/Datastructure/Lists/ArrayLists/StringBuilder.php @@ -162,7 +162,7 @@ private function arrayListToStringBuilder(ArrayList $arrayList, bool $reverse = } } while (!$stack->isEmpty()) { - $stringBuilder->append($stack->peek()); + $stringBuilder->append($stack->pop()); } } else { $queue = new Queue(); diff --git a/src/Datastructure/Stackqueue/Stack.php b/src/Datastructure/Stackqueue/Stack.php index f5af238..078787f 100644 --- a/src/Datastructure/Stackqueue/Stack.php +++ b/src/Datastructure/Stackqueue/Stack.php @@ -90,7 +90,7 @@ protected function isValid(): bool { * */ public function peek() { - if (!$this->isValid()) { + if (null === $this->stack) { return null; } if (0 === $this->stackSize()) { @@ -113,9 +113,8 @@ public function stackSize(): int { /** * pop() removes an item from the top of the stack. * - * @return bool */ - public function pop(): bool { + public function pop() { if ($this->stack === null) { return false; } @@ -128,7 +127,7 @@ public function pop(): bool { * of the array. */ $return = array_pop($this->stack); - return $return !== null; + return $return; } /** diff --git a/src/Datastructure/Stackqueue/StackSet.php b/src/Datastructure/Stackqueue/StackSet.php index 4faeb37..875db6f 100644 --- a/src/Datastructure/Stackqueue/StackSet.php +++ b/src/Datastructure/Stackqueue/StackSet.php @@ -37,16 +37,29 @@ class StackSet { private $counter = 0; private $stackList = null; + /** + * StackSet constructor. + * + * @param int $maxSize + */ public function __construct(int $maxSize = 128) { $this->maxSize = $maxSize; $this->stackList = new ArrayList(); } + /** + * @param $element + * @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException + */ public function push($element) { $stack = $this->getLastStack(); $this->addToStack($stack, $element); } + /** + * @return Stack + * @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException + */ private function getLastStack(): Stack { $modulo = $this->counter % $this->maxSize; if (0 === $modulo) { @@ -57,6 +70,11 @@ private function getLastStack(): Stack { return $stack; } + /** + * @param Stack $stack + * @param $element + * @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException + */ private function addToStack(Stack $stack, $element) { $stack->push($element); $this->counter++; @@ -68,11 +86,15 @@ private function addToStack(Stack $stack, $element) { } } + /** + * @return mixed|null + * @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException + */ public function pop() { $index = $this->stackList->length(); /** @var Stack $stack */ $stack = $this->stackList->get($index - 1); - $element = $stack->peek(); + $element = $stack->pop(); $this->counter--; if (0 === $stack->stackSize()) { $this->stackList->remove($index - 1); @@ -82,6 +104,9 @@ public function pop() { return $element; } + /** + * @return int + */ public function stackCount(): int { return $this->stackList->length(); } diff --git a/tests/StackQueue/FixedStackTest.php b/tests/StackQueue/FixedStackTest.php index 7aed93f..6798737 100644 --- a/tests/StackQueue/FixedStackTest.php +++ b/tests/StackQueue/FixedStackTest.php @@ -44,11 +44,11 @@ public function testStack() { $this->assertTrue($added === false); $this->assertTrue($stack->stackSize() === 2); - $class = $stack->peek(); + $class = $stack->pop(); $this->assertTrue($class instanceof Exception); $this->assertTrue($stack->isEmpty() == false); - $class = $stack->peek(); + $class = $stack->pop(); $this->assertTrue($class instanceof stdClass); $this->assertTrue($stack->isEmpty() == true); } diff --git a/tests/StackQueue/StackSetTest.php b/tests/StackQueue/StackSetTest.php index ff22741..489328f 100644 --- a/tests/StackQueue/StackSetTest.php +++ b/tests/StackQueue/StackSetTest.php @@ -1,12 +1,42 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ namespace StackQueue; - use doganoo\PHPAlgorithms\Datastructure\Stackqueue\StackSet; +use PHPUnit\Framework\TestCase; -class StackSetTest extends \PHPUnit\Framework\TestCase { +/** + * Class StackSetTest + * + * @package StackQueue + */ +class StackSetTest extends TestCase { + /** + * @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException + */ public function testStackSet() { $stackSet = new StackSet(2); $stackSet->push("Hallo"); @@ -19,6 +49,9 @@ public function testStackSet() { $this->assertTrue($stackSet->stackCount() === 1); } + /** + * @throws \doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException + */ public function testHugeStackSet() { $setSize = 1024; $factor = 4; diff --git a/tests/StackQueue/StackTest.php b/tests/StackQueue/StackTest.php index fbde209..2ff94c2 100644 --- a/tests/StackQueue/StackTest.php +++ b/tests/StackQueue/StackTest.php @@ -25,11 +25,12 @@ use doganoo\PHPAlgorithms\Datastructure\Stackqueue\Queue; use doganoo\PHPAlgorithms\Datastructure\Stackqueue\Stack; +use PHPUnit\Framework\TestCase; /** * Class StackQueueTest class testing Stacks and Queues */ -class StackTest extends \PHPUnit\Framework\TestCase { +class StackTest extends TestCase { /** * Stack class test */ @@ -37,13 +38,13 @@ public function testStack() { $stack = new Stack(); $stack->push(new stdClass()); $stack->push(new Exception()); - $this->assertTrue($stack->isEmpty() == false); + $this->assertTrue(false === $stack->isEmpty()); - $class = $stack->peek(); + $class = $stack->pop(); $this->assertTrue($class instanceof Exception); $this->assertTrue($stack->isEmpty() == false); - $class = $stack->peek(); + $class = $stack->pop(); $this->assertTrue($class instanceof stdClass); $this->assertTrue($stack->isEmpty() == true); } @@ -64,7 +65,5 @@ public function testQueue() { $class = $queue->dequeue(); $this->assertTrue($class instanceof stdClass); $this->assertTrue($queue->isEmpty() == true); - - } } \ No newline at end of file