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
|
local LinkedList = require "telescope.algos.linked_list"
describe("LinkedList", function()
it("can create a list", function()
local l = LinkedList:new()
assert.are.same(0, l.size)
end)
it("can add a single entry to the list", function()
local l = LinkedList:new()
l:append "hello"
assert.are.same(1, l.size)
end)
it("can iterate over one item", function()
local l = LinkedList:new()
l:append "hello"
for val in l:iter() do
assert.are.same("hello", val)
end
end)
it("iterates in order", function()
local l = LinkedList:new()
l:append "hello"
l:append "world"
local x = {}
for val in l:iter() do
table.insert(x, val)
end
assert.are.same({ "hello", "world" }, x)
end)
it("iterates in order, for prepend", function()
local l = LinkedList:new()
l:prepend "world"
l:prepend "hello"
local x = {}
for val in l:iter() do
table.insert(x, val)
end
assert.are.same({ "hello", "world" }, x)
end)
it("iterates in order, for combo", function()
local l = LinkedList:new()
l:prepend "world"
l:prepend "hello"
l:append "last"
l:prepend "first"
local x = {}
for val in l:iter() do
table.insert(x, val)
end
assert.are.same({ "first", "hello", "world", "last" }, x)
assert.are.same(#x, l.size)
end)
it("has ipairs", function()
local l = LinkedList:new()
l:prepend "world"
l:prepend "hello"
l:append "last"
l:prepend "first"
local x = {}
for v in l:iter() do
table.insert(x, v)
end
assert.are.same({ "first", "hello", "world", "last" }, x)
local expected = {}
for i, v in ipairs(x) do
table.insert(expected, { i, v })
end
local actual = {}
for i, v in l:ipairs() do
table.insert(actual, { i, v })
end
assert.are.same(expected, actual)
end)
describe("track_at", function()
it("should update tracked when only appending", function()
local l = LinkedList:new { track_at = 2 }
l:append "first"
l:append "second"
l:append "third"
assert.are.same("second", l.tracked)
end)
it("should update tracked when first some prepend and then append", function()
local l = LinkedList:new { track_at = 2 }
l:prepend "first"
l:append "second"
l:append "third"
assert.are.same("second", l.tracked)
end)
it("should update when only prepending", function()
local l = LinkedList:new { track_at = 2 }
l:prepend "third"
l:prepend "second"
l:prepend "first"
assert.are.same("second", l.tracked)
end)
it("should update when lots of prepend and append", function()
local l = LinkedList:new { track_at = 2 }
l:prepend "third"
l:prepend "second"
l:prepend "first"
l:append "fourth"
l:prepend "zeroth"
assert.are.same("first", l.tracked)
end)
end)
end)
|