forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedListTest.php
135 lines (114 loc) · 3.19 KB
/
SinglyLinkedListTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../DataStructures/SinglyLinkedList.php';
/**
* Tests for the Singly Linked Lists class
*/
class SinglyLinkedListTest extends TestCase
{
/**
* Create a SinglyLinkedList node
*/
private function createNode(string $string): SinglyLinkedList
{
$node = new SinglyLinkedList($string[0]);
for ($i = 1; $i < strlen($string); $i++) {
$node->append($string[$i]);
}
return $node;
}
/**
* Provider of SinglyLinkedList nodes
*/
public function provideNodes()
{
return [
'IsPalindrome' => [
'node' => $this->createNode('hannah'),
'expected' => true
],
'IsPalindrome2' => [
'node' => $this->createNode('hanah'),
'expected' => true
],
'IsNotPalindrome' => [
'node' => $this->createNode('hanbah'),
'expected' => false
],
'IsNotPalindrome2' => [
'node' => $this->createNode('han1h'),
'expected' => false
],
];
}
/**
* Test the if a SinglyLinkedList is a palindrome
*
* @dataProvider provideNodes
*/
public function testIsPalindrome($node, $expected): void
{
$this->assertEquals($expected, $this->isPalindrome($node));
}
/**
* Supporting method for testing if a linked list is palindrome
*/
private function isPalindrome(SinglyLinkedList $node): bool
{
$length = $this->length($node);
$pairs = 0;
$curr = $node;
while ($curr instanceof SinglyLinkedList) {
if ($this->hasPair($curr->next, $curr->data)) {
$pairs++;
}
$curr = $curr->next;
}
return $pairs == floor($length / 2);
}
/**
* Supporting method for testing if a linked list is palindrome
*/
private function hasPair($node, string $data): bool
{
if (! $node instanceof SinglyLinkedList) {
return false;
}
$curr = $node;
while ($curr instanceof SinglyLinkedList) {
if ($curr->data == $data) {
return true;
}
$curr = $curr->next;
}
return false;
}
/**
* Supporting method for testing if a linked list is palindrome
*/
private function length(SinglyLinkedList $node): int
{
$curr = $node;
$counter = 0;
while ($curr instanceof SinglyLinkedList) {
$counter++;
$curr = $curr->next;
}
return $counter;
}
/**
* Test SinglyLinkedList's delete functionality
*/
public function testDelete(): void
{
$this->assertEquals(
$this->createNode('hanah'), // expected node
$this->createNode('hannah')->delete('n'), // actual node
);
$this->assertNotEquals(
$this->createNode('hanah'), // expected node
$this->createNode('hannah')->delete('h'), // actual node
);
}
}