2020-10-07 10:35:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\DataStructure;
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-10-07 10:35:06 +02:00
|
|
|
use Psl;
|
|
|
|
use Psl\DataStructure\Stack;
|
|
|
|
|
|
|
|
final class StackTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testPushAndPop(): void
|
|
|
|
{
|
|
|
|
$stack = new Stack();
|
|
|
|
$stack->push('hello');
|
|
|
|
$stack->push('hey');
|
|
|
|
$stack->push('hi');
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertCount(3, $stack);
|
2020-10-07 10:35:06 +02:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('hi', $stack->peek());
|
2020-10-07 10:35:06 +02:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('hi', $stack->pop());
|
|
|
|
static::assertSame('hey', $stack->pop());
|
|
|
|
static::assertSame('hello', $stack->pop());
|
2020-10-07 10:35:06 +02:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertNull($stack->pull());
|
2020-10-07 10:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPeek(): void
|
|
|
|
{
|
|
|
|
$stack = new Stack();
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertNull($stack->peek());
|
2020-10-07 10:35:06 +02:00
|
|
|
|
|
|
|
$stack->push('hello');
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertNotNull($stack->peek());
|
|
|
|
static::assertSame('hello', $stack->peek());
|
2020-10-07 10:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPopThrowsForEmptyStack(): void
|
|
|
|
{
|
|
|
|
$stack = new Stack();
|
|
|
|
$stack->push('hello');
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('hello', $stack->pop());
|
2020-10-07 10:35:06 +02:00
|
|
|
|
|
|
|
$this->expectException(Psl\Exception\InvariantViolationException::class);
|
|
|
|
$this->expectExceptionMessage('Cannot pop an item from an empty Stack.');
|
|
|
|
|
|
|
|
$stack->pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPullReturnsNullForEmptyStack(): void
|
|
|
|
{
|
|
|
|
$stack = new Stack();
|
|
|
|
$stack->push('hello');
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('hello', $stack->pull());
|
|
|
|
static::assertNull($stack->pull());
|
2020-10-07 10:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCount(): void
|
|
|
|
{
|
|
|
|
$stack = new Stack();
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame(0, $stack->count());
|
2020-10-07 10:35:06 +02:00
|
|
|
|
|
|
|
$stack->push('hello');
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame(1, $stack->count());
|
2020-10-07 10:35:06 +02:00
|
|
|
}
|
|
|
|
}
|