Skip to content

Commit 4597595

Browse files
committed
Fix: updated SinglyLinkedList's constructor | Test: added a test for delete functionality
1 parent e8111c1 commit 4597595

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

DataStructures/SinglyLinkedList.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
class SinglyLinkedList
77
{
88
public ?SinglyLinkedList $next = null;
9+
public $data;
910

10-
public function __construct(public $data)
11+
public function __construct($data)
1112
{
13+
$this->data = $data;
1214
}
1315

1416
public function append($data): void
@@ -22,7 +24,7 @@ public function append($data): void
2224
$current->next = new SinglyLinkedList($data);
2325
}
2426

25-
public function delete($data)
27+
public function delete($data): SinglyLinkedList
2628
{
2729
$current = $this;
2830

tests/DataStructures/SinglyLinkedListTest.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,56 @@
1111
class SinglyLinkedListTest extends TestCase
1212
{
1313
/**
14-
* Provider of non-existing IPs
14+
* Create a SinglyLinkedList node
1515
*/
16-
public function provideNodes()
16+
private function createNode(string $string): SinglyLinkedList
1717
{
18-
$string = 'hannah';
19-
$nodeIs = new SinglyLinkedList($string[0]);
18+
$node = new SinglyLinkedList($string[0]);
2019

2120
for ($i = 1; $i < strlen($string); $i++) {
22-
$nodeIs->append($string[$i]);
21+
$node->append($string[$i]);
2322
}
2423

25-
$string = 'hanbah';
26-
$nodeIsNot = new SinglyLinkedList($string[0]);
27-
28-
for ($i = 1; $i < strlen($string); $i++) {
29-
$nodeIsNot->append($string[$i]);
30-
}
24+
return $node;
25+
}
3126

27+
/**
28+
* Provider of SinglyLinkedList nodes
29+
*/
30+
public function provideNodes()
31+
{
3232
return [
3333
'IsPalindrome' => [
34-
'node' => $nodeIs,
35-
'expected' => true,
34+
'node' => $this->createNode('hannah'),
35+
'expected' => true
36+
],
37+
'IsPalindrome2' => [
38+
'node' => $this->createNode('hanah'),
39+
'expected' => true
3640
],
3741
'IsNotPalindrome' => [
38-
'node' => $nodeIsNot,
39-
'expected' => false,
42+
'node' => $this->createNode('hanbah'),
43+
'expected' => false
44+
],
45+
'IsNotPalindrome2' => [
46+
'node' => $this->createNode('han1h'),
47+
'expected' => false
4048
],
4149
];
4250
}
4351

4452
/**
45-
* Test the if a singly linked list is a palindrome
53+
* Test the if a SinglyLinkedList is a palindrome
4654
*
4755
* @dataProvider provideNodes
4856
*/
4957
public function testIsPalindrome($node, $expected): void
5058
{
51-
$this->assertSame($expected, $this->isPalindrome($node));
59+
$this->assertEquals($expected, $this->isPalindrome($node));
5260
}
5361

5462
/**
55-
* Supporting methods for testing if a linked list is palindrome
63+
* Supporting method for testing if a linked list is palindrome
5664
*/
5765
private function isPalindrome(SinglyLinkedList $node): bool
5866
{
@@ -71,6 +79,9 @@ private function isPalindrome(SinglyLinkedList $node): bool
7179
return $pairs == floor($length / 2);
7280
}
7381

82+
/**
83+
* Supporting method for testing if a linked list is palindrome
84+
*/
7485
private function hasPair($node, string $data): bool
7586
{
7687
if (! $node instanceof SinglyLinkedList) {
@@ -90,6 +101,9 @@ private function hasPair($node, string $data): bool
90101
return false;
91102
}
92103

104+
/**
105+
* Supporting method for testing if a linked list is palindrome
106+
*/
93107
private function length(SinglyLinkedList $node): int
94108
{
95109
$curr = $node;
@@ -102,4 +116,20 @@ private function length(SinglyLinkedList $node): int
102116

103117
return $counter;
104118
}
119+
120+
/**
121+
* Test SinglyLinkedList's delete functionality
122+
*/
123+
public function testDelete(): void
124+
{
125+
$this->assertEquals(
126+
$this->createNode('hanah'), // expected node
127+
$this->createNode('hannah')->delete('n'), // actual node
128+
);
129+
130+
$this->assertNotEquals(
131+
$this->createNode('hanah'), // expected node
132+
$this->createNode('hannah')->delete('h'), // actual node
133+
);
134+
}
105135
}

0 commit comments

Comments
 (0)