|
1 | 1 | local LinkedList = require('linked-list')
|
2 | 2 |
|
3 | 3 | describe('linked-list', function()
|
4 |
| - it('should be able to pop pushed elements', function() |
| 4 | + it('pop gets element from the list', function() |
5 | 5 | local list = LinkedList()
|
6 |
| - list:push(10) |
7 |
| - list:push(20) |
8 |
| - assert.equal(20, list:pop()) |
9 |
| - assert.equal(10, list:pop()) |
| 6 | + list:push(7) |
| 7 | + assert.equal(7, list:pop()) |
10 | 8 | end)
|
11 | 9 |
|
12 |
| - it('should be able to shift pushed elements', function() |
| 10 | + it('push/pop respectively add/remove at the end of the list', function() |
13 | 11 | local list = LinkedList()
|
14 |
| - list:push(10) |
15 |
| - list:push(20) |
16 |
| - assert.equal(10, list:shift()) |
17 |
| - assert.equal(20, list:shift()) |
| 12 | + list:push(11) |
| 13 | + list:push(13) |
| 14 | + assert.equal(13, list:pop()) |
| 15 | + assert.equal(11, list:pop()) |
18 | 16 | end)
|
19 | 17 |
|
20 |
| - it('should be able to shift unshifted elements', function() |
| 18 | + it('shift gets an element from the list', function() |
21 | 19 | local list = LinkedList()
|
22 |
| - list:unshift(10) |
23 |
| - list:unshift(20) |
24 |
| - assert.equal(20, list:shift()) |
25 |
| - assert.equal(10, list:shift()) |
| 20 | + list:push(17) |
| 21 | + assert.equal(17, list:shift()) |
26 | 22 | end)
|
27 | 23 |
|
28 |
| - it('should be able to pop unshifted elements', function() |
| 24 | + it('shift gets first element from the list', function() |
29 | 25 | local list = LinkedList()
|
30 |
| - list:unshift(10) |
31 |
| - list:unshift(20) |
32 |
| - assert.equal(10, list:pop()) |
33 |
| - assert.equal(20, list:pop()) |
| 26 | + list:push(23) |
| 27 | + list:push(5) |
| 28 | + assert.equal(23, list:shift()) |
| 29 | + assert.equal(5, list:shift()) |
34 | 30 | end)
|
35 | 31 |
|
36 |
| - it('should be able to count its elements', function() |
| 32 | + it('unshift adds element at start of the list', function() |
| 33 | + local list = LinkedList() |
| 34 | + list:unshift(23) |
| 35 | + list:unshift(5) |
| 36 | + assert.equal(5, list:shift()) |
| 37 | + assert.equal(23, list:shift()) |
| 38 | + end) |
| 39 | + |
| 40 | + it('pop, push, shift, and unshift can be used in any order', function() |
| 41 | + local list = LinkedList() |
| 42 | + list:push(1) |
| 43 | + list:push(2) |
| 44 | + assert.equal(2, list:pop()) |
| 45 | + list:push(3) |
| 46 | + assert.equal(1, list:shift()) |
| 47 | + list:unshift(4) |
| 48 | + list:push(5) |
| 49 | + assert.equal(4, list:shift()) |
| 50 | + assert.equal(5, list:pop()) |
| 51 | + assert.equal(3, list:shift()) |
| 52 | + end) |
| 53 | + |
| 54 | + it('count an empty list', function() |
37 | 55 | local list = LinkedList()
|
38 | 56 | assert.equal(0, list:count())
|
39 |
| - list:push(10) |
40 |
| - assert.equal(1, list:count()) |
41 |
| - list:push(20) |
42 |
| - assert.equal(2, list:count()) |
| 57 | + end) |
43 | 58 |
|
| 59 | + it('count a list with items', function() |
| 60 | + local list = LinkedList() |
| 61 | + list:push(37) |
| 62 | + list:push(1) |
| 63 | + assert.equal(2, list:count()) |
44 | 64 | end)
|
45 | 65 |
|
46 |
| - it('should count correctly after a shift', function() |
| 66 | + it('count is correct after mutation', function() |
47 | 67 | local list = LinkedList()
|
48 |
| - list:push(10) |
49 |
| - list:push(20) |
| 68 | + list:push(31) |
| 69 | + assert.equal(1, list:count()) |
| 70 | + list:unshift(43) |
| 71 | + assert.equal(2, list:count()) |
50 | 72 | list:shift()
|
51 | 73 | assert.equal(1, list:count())
|
| 74 | + list:pop() |
| 75 | + assert.equal(0, list:count()) |
52 | 76 | end)
|
53 | 77 |
|
54 |
| - it('should count correctly after a pop', function() |
| 78 | + it('popping to empty doesn\'t break the list', function() |
55 | 79 | local list = LinkedList()
|
56 |
| - list:push(10) |
57 |
| - list:push(20) |
| 80 | + list:push(41) |
| 81 | + list:push(59) |
58 | 82 | list:pop()
|
| 83 | + list:pop() |
| 84 | + list:push(47) |
59 | 85 | assert.equal(1, list:count())
|
| 86 | + assert.equal(47, list:pop()) |
60 | 87 | end)
|
61 | 88 |
|
62 |
| - it('should be able to delete from the beginning of the list', function() |
| 89 | + it('shifting to empty doesn\'t break the list', function() |
63 | 90 | local list = LinkedList()
|
64 |
| - list:push(10) |
65 |
| - list:push(20) |
66 |
| - list:push(30) |
67 |
| - list:delete(30) |
68 |
| - assert.equal(2, list:count()) |
69 |
| - assert.equal(20, list:pop()) |
70 |
| - assert.equal(10, list:shift()) |
| 91 | + list:push(41) |
| 92 | + list:push(59) |
| 93 | + list:shift() |
| 94 | + list:shift() |
| 95 | + list:push(47) |
| 96 | + assert.equal(1, list:count()) |
| 97 | + assert.equal(47, list:shift()) |
| 98 | + end) |
| 99 | + |
| 100 | + it('deletes the only element', function() |
| 101 | + local list = LinkedList() |
| 102 | + list:push(61) |
| 103 | + list:delete(61) |
| 104 | + assert.equal(0, list:count()) |
71 | 105 | end)
|
72 | 106 |
|
73 |
| - it('should be able to delete from the middle of the list', function() |
| 107 | + it('deletes the element with the specified value from the list', function() |
74 | 108 | local list = LinkedList()
|
75 |
| - list:push(10) |
76 |
| - list:push(20) |
77 |
| - list:push(30) |
78 |
| - list:delete(20) |
| 109 | + list:push(71) |
| 110 | + list:push(83) |
| 111 | + list:push(79) |
| 112 | + list:delete(83) |
79 | 113 | assert.equal(2, list:count())
|
80 |
| - assert.equal(30, list:pop()) |
81 |
| - assert.equal(10, list:shift()) |
| 114 | + assert.equal(79, list:pop()) |
| 115 | + assert.equal(71, list:shift()) |
82 | 116 | end)
|
83 | 117 |
|
84 |
| - it('should be able to delete from the end of the list', function() |
| 118 | + it('deletes the element with the specified value from the list, re-assigns tail', function() |
85 | 119 | local list = LinkedList()
|
86 |
| - list:push(10) |
87 |
| - list:push(20) |
88 |
| - list:push(30) |
89 |
| - list:delete(10) |
| 120 | + list:push(71) |
| 121 | + list:push(83) |
| 122 | + list:push(79) |
| 123 | + list:delete(83) |
90 | 124 | assert.equal(2, list:count())
|
91 |
| - assert.equal(30, list:pop()) |
92 |
| - assert.equal(20, list:shift()) |
| 125 | + assert.equal(79, list:pop()) |
| 126 | + assert.equal(71, list:pop()) |
93 | 127 | end)
|
94 | 128 |
|
95 |
| - it('should delete all elements with the matching value', function() |
| 129 | + it('deletes the element with the specified value from the list, re-assigns head', function() |
96 | 130 | local list = LinkedList()
|
97 |
| - list:push(10) |
98 |
| - list:push(20) |
99 |
| - list:push(20) |
100 |
| - list:push(30) |
101 |
| - list:delete(20) |
| 131 | + list:push(71) |
| 132 | + list:push(83) |
| 133 | + list:push(79) |
| 134 | + list:delete(83) |
102 | 135 | assert.equal(2, list:count())
|
103 |
| - assert.equal(30, list:pop()) |
104 |
| - assert.equal(10, list:shift()) |
| 136 | + assert.equal(71, list:shift()) |
| 137 | + assert.equal(79, list:shift()) |
105 | 138 | end)
|
106 | 139 |
|
107 |
| - it('should be able to delete the only element', function() |
| 140 | + it('deletes the first of two elements', function() |
108 | 141 | local list = LinkedList()
|
109 |
| - list:push(10) |
110 |
| - list:delete(10) |
111 |
| - assert.equal(0, list:count()) |
| 142 | + list:push(97) |
| 143 | + list:push(101) |
| 144 | + list:delete(97) |
| 145 | + assert.equal(1, list:count()) |
| 146 | + assert.equal(101, list:pop()) |
| 147 | + end) |
| 148 | + |
| 149 | + it('deletes the second of two elements', function() |
| 150 | + local list = LinkedList() |
| 151 | + list:push(97) |
| 152 | + list:push(101) |
| 153 | + list:delete(101) |
| 154 | + assert.equal(1, list:count()) |
| 155 | + assert.equal(97, list:pop()) |
| 156 | + end) |
| 157 | + |
| 158 | + it('delete does not modify the list if the element is not found', function() |
| 159 | + local list = LinkedList() |
| 160 | + list:push(89) |
| 161 | + list:delete(103) |
| 162 | + assert.equal(1, list:count()) |
| 163 | + end) |
| 164 | + |
| 165 | + it('deletes only the first occurrence', function() |
| 166 | + local list = LinkedList() |
| 167 | + list:push(73) |
| 168 | + list:push(9) |
| 169 | + list:push(9) |
| 170 | + list:push(107) |
| 171 | + list:delete(9) |
| 172 | + assert.equal(3, list:count()) |
| 173 | + assert.equal(107, list:pop()) |
| 174 | + assert.equal(9, list:pop()) |
| 175 | + assert.equal(73, list:pop()) |
112 | 176 | end)
|
113 | 177 | end)
|
0 commit comments