Skip to content

Commit f8a776c

Browse files
authored
Add Deque Interface and Corresponding Tests (#12)
- Created Deque interface extending Queue interface to define additional methods for double-ended queues. - Added methods: addFirst, removeLast, and peekLast to the Deque interface. - Implemented unit tests for Deque interface to ensure proper contract implementation. Interface: - KaririCode\Contract\DataStructure\Deque Tests: - KaririCode\Contract\Tests\DataStructure\DequeTest
1 parent 83ff17c commit f8a776c

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/DataStructure/Deque.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\DataStructure;
6+
7+
/**
8+
* Interface Deque.
9+
*
10+
* Extends the Queue interface to define the contract for double-ended queue data structures,
11+
* which allow elements to be added and removed from both ends.
12+
*
13+
* @category Interfaces
14+
*
15+
* @author Walmir Silva <walmir.silva@kariricode.org>
16+
* @license MIT
17+
*
18+
* @see https://kariricode.org/
19+
*/
20+
interface Deque extends Queue
21+
{
22+
/**
23+
* Adds an element to the front of the deque.
24+
*
25+
* @param mixed $element the element to add
26+
*/
27+
public function addFirst(mixed $element): void;
28+
29+
/**
30+
* Removes and returns the element at the end of the deque.
31+
*
32+
* @return mixed the removed element or null if the deque is empty
33+
*/
34+
public function removeLast(): mixed;
35+
36+
/**
37+
* Returns the element at the end of the deque without removing it.
38+
*
39+
* @return mixed the last element or null if the deque is empty
40+
*/
41+
public function peekLast(): mixed;
42+
}

src/DataStructure/DequeTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\Tests\DataStructure;
6+
7+
use KaririCode\Contract\DataStructure\Deque;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class DequeTest extends TestCase
11+
{
12+
public function testEnqueue(): void
13+
{
14+
$mock = $this->createMock(Deque::class);
15+
$mock->expects($this->once())->method('enqueue')->with('element');
16+
17+
$mock->enqueue('element');
18+
}
19+
20+
public function testDequeue(): void
21+
{
22+
$mock = $this->createMock(Deque::class);
23+
$mock->method('dequeue')->willReturn('element');
24+
25+
$this->assertSame('element', $mock->dequeue());
26+
}
27+
28+
public function testPeek(): void
29+
{
30+
$mock = $this->createMock(Deque::class);
31+
$mock->method('peek')->willReturn('element');
32+
33+
$this->assertSame('element', $mock->peek());
34+
}
35+
36+
public function testAddFirst(): void
37+
{
38+
$mock = $this->createMock(Deque::class);
39+
$mock->expects($this->once())->method('addFirst')->with('element');
40+
41+
$mock->addFirst('element');
42+
}
43+
44+
public function testRemoveLast(): void
45+
{
46+
$mock = $this->createMock(Deque::class);
47+
$mock->method('removeLast')->willReturn('element');
48+
49+
$this->assertSame('element', $mock->removeLast());
50+
}
51+
52+
public function testPeekLast(): void
53+
{
54+
$mock = $this->createMock(Deque::class);
55+
$mock->method('peekLast')->willReturn('element');
56+
57+
$this->assertSame('element', $mock->peekLast());
58+
}
59+
60+
public function testIsEmpty(): void
61+
{
62+
63+
$mock = $this->createMock(Deque::class);
64+
$mock->method('isEmpty')->willReturn(true);
65+
66+
$this->assertTrue($mock->isEmpty());
67+
}
68+
69+
public function testSize(): void
70+
{
71+
$mock = $this->createMock(Deque::class);
72+
$mock->method('size')->willReturn(0);
73+
74+
$this->assertSame(0, $mock->size());
75+
}
76+
}

0 commit comments

Comments
 (0)