Skip to content

Commit 865cb9f

Browse files
authored
Update linked-list tests (#587)
1 parent c1860c8 commit 865cb9f

File tree

4 files changed

+226
-72
lines changed

4 files changed

+226
-72
lines changed

exercises/practice/linked-list/.meta/example.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ end
1515
function LinkedList:pop()
1616
local v = self._head.v
1717
self._head = self._head.next
18-
if not self._head then
18+
if self._head then
19+
self._head.prev = nil
20+
else
1921
self._tail = nil
2022
end
2123
return v
@@ -37,6 +39,8 @@ function LinkedList:shift()
3739
self._tail = self._tail.prev
3840
if self._tail then
3941
self._tail.next = nil
42+
else
43+
self._head = nil
4044
end
4145
return v
4246
end
@@ -52,21 +56,21 @@ function LinkedList:count()
5256
end
5357

5458
function LinkedList:delete(v)
59+
if self._head.v == v then
60+
self:pop()
61+
return
62+
end
63+
5564
local current = self._head
5665
while current do
5766
if current.v == v then
58-
if self._head == current then
59-
self._head = current.next
60-
end
61-
if self._tail == current then
62-
self._tail = current.prev
63-
end
6467
if current.prev then
6568
current.prev.next = current.next
6669
end
6770
if current.next then
6871
current.next.prev = current.prev
6972
end
73+
return
7074
end
7175
current = current.next
7276
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
return {
2+
module_name = 'LinkedList',
3+
4+
generate_test = function(case)
5+
local lines = { 'local list = LinkedList()' }
6+
7+
for _, operation in ipairs(case.input.operations) do
8+
if operation.value then
9+
table.insert(lines, ('list:%s(%s)'):format(operation.operation, operation.value))
10+
elseif operation.expected then
11+
table.insert(lines, ('assert.equal(%s, list:%s())'):format(operation.expected, operation.operation))
12+
else
13+
table.insert(lines, ('list:%s()'):format(operation.operation))
14+
end
15+
end
16+
17+
return table.concat(lines, '\n')
18+
end
19+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[7f7e3987-b954-41b8-8084-99beca08752c]
13+
description = "pop gets element from the list"
14+
15+
[c3f67e5d-cfa2-4c3e-a18f-7ce999c3c885]
16+
description = "push/pop respectively add/remove at the end of the list"
17+
18+
[00ea24ce-4f5c-4432-abb4-cc6e85462657]
19+
description = "shift gets an element from the list"
20+
21+
[37962ee0-3324-4a29-b588-5a4c861e6564]
22+
description = "shift gets first element from the list"
23+
24+
[30a3586b-e9dc-43fb-9a73-2770cec2c718]
25+
description = "unshift adds element at start of the list"
26+
27+
[042f71e4-a8a7-4cf0-8953-7e4f3a21c42d]
28+
description = "pop, push, shift, and unshift can be used in any order"
29+
30+
[88f65c0c-4532-4093-8295-2384fb2f37df]
31+
description = "count an empty list"
32+
33+
[fc055689-5cbe-4cd9-b994-02e2abbb40a5]
34+
description = "count a list with items"
35+
36+
[8272cef5-130d-40ea-b7f6-5ffd0790d650]
37+
description = "count is correct after mutation"
38+
39+
[229b8f7a-bd8a-4798-b64f-0dc0bb356d95]
40+
description = "popping to empty doesn't break the list"
41+
42+
[4e1948b4-514e-424b-a3cf-a1ebbfa2d1ad]
43+
description = "shifting to empty doesn't break the list"
44+
45+
[e8f7c600-d597-4f79-949d-8ad8bae895a6]
46+
description = "deletes the only element"
47+
48+
[fd65e422-51f3-45c0-9fd0-c33da638f89b]
49+
description = "deletes the element with the specified value from the list"
50+
51+
[59db191a-b17f-4ab7-9c5c-60711ec1d013]
52+
description = "deletes the element with the specified value from the list, re-assigns tail"
53+
54+
[58242222-5d39-415b-951d-8128247f8993]
55+
description = "deletes the element with the specified value from the list, re-assigns head"
56+
57+
[ee3729ee-3405-4bd2-9bad-de0d4aa5d647]
58+
description = "deletes the first of two elements"
59+
60+
[47e3b3b4-b82c-4c23-8c1a-ceb9b17cb9fb]
61+
description = "deletes the second of two elements"
62+
63+
[7b420958-f285-4922-b8f9-10d9dcab5179]
64+
description = "delete does not modify the list if the element is not found"
65+
66+
[7e04828f-6082-44e3-a059-201c63252a76]
67+
description = "deletes only the first occurrence"
Lines changed: 129 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,177 @@
11
local LinkedList = require('linked-list')
22

33
describe('linked-list', function()
4-
it('should be able to pop pushed elements', function()
4+
it('pop gets element from the list', function()
55
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())
108
end)
119

12-
it('should be able to shift pushed elements', function()
10+
it('push/pop respectively add/remove at the end of the list', function()
1311
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())
1816
end)
1917

20-
it('should be able to shift unshifted elements', function()
18+
it('shift gets an element from the list', function()
2119
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())
2622
end)
2723

28-
it('should be able to pop unshifted elements', function()
24+
it('shift gets first element from the list', function()
2925
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())
3430
end)
3531

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()
3755
local list = LinkedList()
3856
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)
4358

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())
4464
end)
4565

46-
it('should count correctly after a shift', function()
66+
it('count is correct after mutation', function()
4767
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())
5072
list:shift()
5173
assert.equal(1, list:count())
74+
list:pop()
75+
assert.equal(0, list:count())
5276
end)
5377

54-
it('should count correctly after a pop', function()
78+
it('popping to empty doesn\'t break the list', function()
5579
local list = LinkedList()
56-
list:push(10)
57-
list:push(20)
80+
list:push(41)
81+
list:push(59)
5882
list:pop()
83+
list:pop()
84+
list:push(47)
5985
assert.equal(1, list:count())
86+
assert.equal(47, list:pop())
6087
end)
6188

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()
6390
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())
71105
end)
72106

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()
74108
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)
79113
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())
82116
end)
83117

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()
85119
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)
90124
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())
93127
end)
94128

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()
96130
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)
102135
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())
105138
end)
106139

107-
it('should be able to delete the only element', function()
140+
it('deletes the first of two elements', function()
108141
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())
112176
end)
113177
end)

0 commit comments

Comments
 (0)