Completed
Push — master ( 4b3449...8131a6 )
by Siro Díaz
01:20
created

SimpleLinkedListTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 64.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 90
loc 140
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace DataStructures\Tests\Lists;
4
5
use DataStructures\Lists\SimpleLinkedList;
6
use PHPUnit\Framework\TestCase;
7
8
class SimpleLinkedListTest extends TestCase {
9
    private $list;
10
11
    public function setUp() {
12
        $this->list = new SimpleLinkedList();
13
    }
14
15
    public function testSize() {
16
        $this->assertEquals($this->list->size(), 0);
17
    }
18
19
    public function testEmpty() {
20
        $this->assertTrue($this->list->empty());
21
    }
22
23
    public function testPush() {
24
        $this->list->push(20);
25
        $this->assertEquals(1, $this->list->size());
26
        $this->assertEquals(20, $this->list->get(0));
27
        $this->list->push(true);
28
        $this->assertEquals(2, $this->list->size());
29
        $this->assertTrue($this->list->get(1));
30
        $this->list->push(30);
31
        $this->assertEquals(30, $this->list->get(2));
32
    }
33
34
    public function testInsert() {
35
        $this->list->insert(0, 100);
36
        $this->assertEquals(100, $this->list->get(0));
37
        $this->assertEquals(1, $this->list->size());
38
39
        $this->list->insert(0, 200);
40
        $this->assertEquals(2, $this->list->size());
41
        $this->assertEquals(200, $this->list->get(0));
42
        $this->assertEquals(100, $this->list->get(1));
43
44
        $this->list->insert(1, 300);
45
        $this->assertEquals(3, $this->list->size());
46
        $this->assertEquals(200, $this->list->get(0));
47
        $this->assertEquals(300, $this->list->get(1));
48
        $this->assertEquals(100, $this->list->get(2));
49
        
50
        /*
51
        for($i = 0; $i < 100; $i++) {
52
            $this->list->insert($i, $i + 1);
53
            $this->assertEquals($i + 1, $this->list->get($i));
54
        }
55
        */
56
    }
57
    
58
    public function testDelete() {
59
        $this->assertNull($this->list->delete(10));
60
        $this->list->push(20);
61
        $this->list->push(true);
62
        $this->list->push(15);
63
        $this->list->push(3.14);
64
        $this->list->push("string");
65
        $this->assertEquals(3.14, $this->list->delete(3));
66
        $this->assertEquals(4, $this->list->size());
67
        $this->assertEquals(true, $this->list->delete(1));
68
        $this->assertEquals(false, $this->list->empty());
69
        $this->assertEquals(20, $this->list->delete(0));
70
    }
71
72
    public function testShift() {
73
        $this->assertNull($this->list->shift());
74
        $this->list->push(20);
75
        $this->list->push(true);
76
        $this->assertEquals(20, $this->list->shift());
77
        $this->assertEquals(1, $this->list->size());
78
        $this->assertEquals(true, $this->list->shift());
79
        $this->assertEquals(true, $this->list->empty());
80
        $this->assertNull($this->list->shift());
81
    }
82
83
    public function testPop() {
84
        //TODO 
85
        $this->assertTrue(true);
86
    }
87
88
    public function testUnshift() {
89
        $this->list->unshift(999);
90
        $this->assertEquals(1, $this->list->size());
91
        $this->assertEquals(999, $this->list->get(0));
92
        $this->list->unshift(888);
93
        $this->assertEquals(2, $this->list->size());
94
        $this->assertEquals(888, $this->list->get(0));
95
        $this->assertEquals(999, $this->list->get(1));
96
        $this->list->unshift(777);
97
        $this->assertEquals(3, $this->list->size());
98
        $this->assertEquals(777, $this->list->get(0));
99
        $this->assertEquals(888, $this->list->get(1));
100
        $this->assertEquals(999, $this->list->get(2));
101
    }
102
103
    public function testGet() {
104
        $this->list->push(20);
105
        $this->list->push(true);
106
        $this->list->push(15);
107
        $this->list->push(3.14);
108
        $this->list->push("string");
109
        $this->assertTrue(true);
110
    }
111
112
    public function testGetAll() {
113
        $this->list->push(20);
114
        $this->list->push(true);
115
        $this->list->push(15);
116
        $this->list->push(3.14);
117
        $this->list->push("string");
118
        $data = [];
119
        foreach($this->list->getAll() as $node) {
120
            $data[] = $node;
121
        }
122
        $this->assertCount(5, $data);
123
        $this->assertSame([20, true, 15, 3.14, "string"], $data);
124
    }
125
126
    public function testToArray() {
127
        $this->list->push(20);
128
        $this->list->push(true);
129
        $this->list->push(15);
130
        $this->list->push(3.14);
131
        $this->list->push("string");
132
        $this->list->pop();
133
        $nodes = $this->list->toArray();
134
        $this->assertSame([20, true, 15, 3.14], $nodes);
135
    }
136
137
    public function testClear() {
138
        $this->list->push(20);
139
        $this->list->push(true);
140
        $this->list->push(15);
141
        $this->list->push(3.14);
142
        $this->list->push("string");
143
        $this->list->clear();
144
        $this->assertEmpty($this->list->toArray());
145
        $this->assertEquals($this->list->size(), 0);
146
    }
147
}