11
11
class SinglyLinkedListTest extends TestCase
12
12
{
13
13
/**
14
- * Provider of non-existing IPs
14
+ * Create a SinglyLinkedList node
15
15
*/
16
- public function provideNodes ()
16
+ private function createNode ( string $ string ): SinglyLinkedList
17
17
{
18
- $ string = 'hannah ' ;
19
- $ nodeIs = new SinglyLinkedList ($ string [0 ]);
18
+ $ node = new SinglyLinkedList ($ string [0 ]);
20
19
21
20
for ($ i = 1 ; $ i < strlen ($ string ); $ i ++) {
22
- $ nodeIs ->append ($ string [$ i ]);
21
+ $ node ->append ($ string [$ i ]);
23
22
}
24
23
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
+ }
31
26
27
+ /**
28
+ * Provider of SinglyLinkedList nodes
29
+ */
30
+ public function provideNodes ()
31
+ {
32
32
return [
33
33
'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
36
40
],
37
41
'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
40
48
],
41
49
];
42
50
}
43
51
44
52
/**
45
- * Test the if a singly linked list is a palindrome
53
+ * Test the if a SinglyLinkedList is a palindrome
46
54
*
47
55
* @dataProvider provideNodes
48
56
*/
49
57
public function testIsPalindrome ($ node , $ expected ): void
50
58
{
51
- $ this ->assertSame ($ expected , $ this ->isPalindrome ($ node ));
59
+ $ this ->assertEquals ($ expected , $ this ->isPalindrome ($ node ));
52
60
}
53
61
54
62
/**
55
- * Supporting methods for testing if a linked list is palindrome
63
+ * Supporting method for testing if a linked list is palindrome
56
64
*/
57
65
private function isPalindrome (SinglyLinkedList $ node ): bool
58
66
{
@@ -71,6 +79,9 @@ private function isPalindrome(SinglyLinkedList $node): bool
71
79
return $ pairs == floor ($ length / 2 );
72
80
}
73
81
82
+ /**
83
+ * Supporting method for testing if a linked list is palindrome
84
+ */
74
85
private function hasPair ($ node , string $ data ): bool
75
86
{
76
87
if (! $ node instanceof SinglyLinkedList) {
@@ -90,6 +101,9 @@ private function hasPair($node, string $data): bool
90
101
return false ;
91
102
}
92
103
104
+ /**
105
+ * Supporting method for testing if a linked list is palindrome
106
+ */
93
107
private function length (SinglyLinkedList $ node ): int
94
108
{
95
109
$ curr = $ node ;
@@ -102,4 +116,20 @@ private function length(SinglyLinkedList $node): int
102
116
103
117
return $ counter ;
104
118
}
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
+ }
105
135
}
0 commit comments