linked-list 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/CHANGELOG.md +12 -1
- data/README.md +22 -8
- data/lib/linked-list/list.rb +143 -12
- data/lib/linked-list/version.rb +1 -1
- data/linked-list.gemspec +3 -3
- data/test/list_test.rb +269 -3
- data/test/test_helper.rb +4 -2
- metadata +12 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d11a48bdb387744768bf129513fce5e6f31001988e8cf45bf8b974009938e98
|
4
|
+
data.tar.gz: 9ba15ea4897065b9a111e6add68d6a60dac5ab61b9e3aba0ab83c7b42e91f8b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13f2b676935b4b18a9302e9a484f888c9c4730e81cab6fb6a891e4da5cc721fd0adf07d3f62b7a490fe985d32ed6143fdd9e01f586904214824c29c1cc8ed6f6
|
7
|
+
data.tar.gz: 5f844ebe95b710614b30ebc6a0a732c361987882ea661cc30e3773432f1702c347a2cf8979f63c6b50e6011cdd348adff1f458d0a81d22cab67aaf3b15528c36
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,20 @@
|
|
1
|
-
# 0.0.
|
1
|
+
# 0.0.12 / 2018-09-04
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
- Added `insert`, `insert_before` and `insert_after` methods (mpospelov in [#3](https://github.com/spectator/linked-list/pull/3))
|
6
|
+
- Added `reverse_each` and `reverse_each_node` methods (mpospelov in [#4](https://github.com/spectator/linked-list/pull/4))
|
7
|
+
|
8
|
+
[Compare v0.0.11...v0.0.12](https://github.com/spectator/linked-list/compare/v0.0.11...v0.0.12)
|
9
|
+
|
10
|
+
# 0.0.11 / 2018-08-23
|
2
11
|
|
3
12
|
## Added
|
4
13
|
|
5
14
|
- Added `delete` and `delete_all` methods (mpospelov in [#2](https://github.com/spectator/linked-list/pull/2))
|
6
15
|
|
16
|
+
[Compare v0.0.10...v0.0.11](https://github.com/spectator/linked-list/compare/v0.0.10...v0.0.11)
|
17
|
+
|
7
18
|
# 0.0.10 / 2018-04-02
|
8
19
|
|
9
20
|
## Fixed
|
data/README.md
CHANGED
@@ -41,20 +41,38 @@ list.unshift(object)
|
|
41
41
|
list.pop
|
42
42
|
list.shift
|
43
43
|
|
44
|
+
list.insert(object, before: object)
|
45
|
+
list.insert(object, before: ->(n) { n == 'foo' })
|
46
|
+
|
47
|
+
list.insert(object, after: object)
|
48
|
+
list.insert(object, after: ->(n) { n == 'foo' })
|
49
|
+
|
50
|
+
list.insert_before(object, node)
|
51
|
+
list.insert_after(object, node)
|
52
|
+
|
44
53
|
list.reverse
|
45
54
|
list.reverse!
|
46
55
|
|
47
56
|
list.delete(object)
|
57
|
+
list.delete { |n| n == 'foo' }
|
58
|
+
|
48
59
|
list.delete_all(object)
|
60
|
+
list.delete_all { |n| n == 'foo' }
|
49
61
|
|
50
|
-
list.each
|
62
|
+
list.each # Enumerator object
|
51
63
|
list.each { |e| puts e }
|
52
64
|
|
53
|
-
list.
|
54
|
-
list.
|
65
|
+
list.reverse_each # Enumerator object
|
66
|
+
list.reverse_each { |e| puts e }
|
67
|
+
|
68
|
+
list.reverse_each_node # Enumerator object
|
69
|
+
list.reverse_each_node { |node| puts node.data }
|
70
|
+
|
71
|
+
list.first # head of the list
|
72
|
+
list.last # tail of the list
|
55
73
|
|
56
74
|
list.length
|
57
|
-
list.size
|
75
|
+
list.size # same as `length`
|
58
76
|
|
59
77
|
list.to_a
|
60
78
|
```
|
@@ -79,10 +97,6 @@ List([object, object]) # will return new `List` object with two `Node` objects
|
|
79
97
|
Please see `LinkedList::List`, `LinkedList::Node`, and
|
80
98
|
`LinkedList::Conversions` for details.
|
81
99
|
|
82
|
-
## TODO
|
83
|
-
|
84
|
-
* Insert in the middle
|
85
|
-
|
86
100
|
## Tests
|
87
101
|
|
88
102
|
Run test with
|
data/lib/linked-list/list.rb
CHANGED
@@ -69,6 +69,65 @@ module LinkedList
|
|
69
69
|
self
|
70
70
|
end
|
71
71
|
|
72
|
+
# Inserts after or before first matched node.data from the the list by passed block or value.
|
73
|
+
#
|
74
|
+
# == Returns:
|
75
|
+
# Inserted node data
|
76
|
+
#
|
77
|
+
def insert(to_add, after: nil, before: nil)
|
78
|
+
if after && before || !after && !before
|
79
|
+
raise ArgumentError, 'either :after or :before keys should be passed'
|
80
|
+
end
|
81
|
+
matcher = after || before
|
82
|
+
matcher_proc = if matcher.is_a?(Proc)
|
83
|
+
__to_matcher(&matcher)
|
84
|
+
else
|
85
|
+
__to_matcher(matcher)
|
86
|
+
end
|
87
|
+
node = each_node.find(&matcher_proc)
|
88
|
+
return unless node
|
89
|
+
new_node = after ? insert_after_node(to_add, node) : insert_before_node(to_add, node)
|
90
|
+
new_node.data
|
91
|
+
end
|
92
|
+
|
93
|
+
# Inserts data after first matched node.data.
|
94
|
+
#
|
95
|
+
# == Returns:
|
96
|
+
# Inserted node
|
97
|
+
#
|
98
|
+
def insert_after_node(data, node)
|
99
|
+
Node(data).tap do |new_node|
|
100
|
+
new_node.prev = node
|
101
|
+
new_node.next = node.next
|
102
|
+
if node.next
|
103
|
+
node.next.prev = new_node
|
104
|
+
else
|
105
|
+
@tail = new_node
|
106
|
+
end
|
107
|
+
node.next = new_node
|
108
|
+
@length += 1
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Inserts data before first matched node.data.
|
113
|
+
#
|
114
|
+
# == Returns:
|
115
|
+
# Inserted node
|
116
|
+
#
|
117
|
+
def insert_before_node(data, node)
|
118
|
+
Node(data).tap do |new_node|
|
119
|
+
new_node.next = node
|
120
|
+
new_node.prev = node.prev
|
121
|
+
if node.prev
|
122
|
+
node.prev.next = new_node
|
123
|
+
else
|
124
|
+
@head = new_node
|
125
|
+
end
|
126
|
+
node.prev = new_node
|
127
|
+
@length += 1
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
72
131
|
# Removes first matched node.data from the the list by passed block or value.
|
73
132
|
#
|
74
133
|
# == Returns:
|
@@ -76,8 +135,8 @@ module LinkedList
|
|
76
135
|
#
|
77
136
|
def delete(val = nil, &block)
|
78
137
|
each_node.find(&__to_matcher(val, &block)).tap do |node_to_delete|
|
79
|
-
return
|
80
|
-
|
138
|
+
return unless node_to_delete
|
139
|
+
__unlink_node(node_to_delete)
|
81
140
|
end.data
|
82
141
|
end
|
83
142
|
|
@@ -88,8 +147,8 @@ module LinkedList
|
|
88
147
|
#
|
89
148
|
def delete_all(val = nil, &block)
|
90
149
|
each_node.select(&__to_matcher(val, &block)).each do |node_to_delete|
|
91
|
-
next
|
92
|
-
|
150
|
+
next unless node_to_delete
|
151
|
+
__unlink_node(node_to_delete)
|
93
152
|
end.map(&:data)
|
94
153
|
end
|
95
154
|
|
@@ -171,6 +230,30 @@ module LinkedList
|
|
171
230
|
__each { |node| yield(node) }
|
172
231
|
end
|
173
232
|
|
233
|
+
|
234
|
+
# Iterates over nodes from bottom to top passing node data to the block if
|
235
|
+
# given. If no block given, returns +Enumerator+.
|
236
|
+
#
|
237
|
+
# == Returns:
|
238
|
+
# +Enumerator+ or yields data to the block stored in every node on the
|
239
|
+
# list.
|
240
|
+
#
|
241
|
+
def reverse_each
|
242
|
+
return to_enum(__callee__) unless block_given?
|
243
|
+
__reverse_each { |node| yield(node.data) }
|
244
|
+
end
|
245
|
+
|
246
|
+
# Iterates over nodes from bottom to top passing node(LinkedList::Node instance)
|
247
|
+
# to the block if given. If no block given, returns +Enumerator+.
|
248
|
+
#
|
249
|
+
# == Returns:
|
250
|
+
# +Enumerator+ or yields list nodes to the block
|
251
|
+
#
|
252
|
+
def reverse_each_node
|
253
|
+
return to_enum(__callee__) unless block_given?
|
254
|
+
__reverse_each { |node| yield(node) }
|
255
|
+
end
|
256
|
+
|
174
257
|
# Converts list to array.
|
175
258
|
#
|
176
259
|
def to_a
|
@@ -191,19 +274,52 @@ module LinkedList
|
|
191
274
|
self
|
192
275
|
end
|
193
276
|
|
194
|
-
|
277
|
+
# Inserts data after passed node.
|
278
|
+
#
|
279
|
+
# == Returns:
|
280
|
+
# Inserted node
|
281
|
+
#
|
282
|
+
def insert_after_node(data, node)
|
283
|
+
Node(data).tap do |new_node|
|
284
|
+
new_node.prev = node
|
285
|
+
new_node.next = node.next
|
286
|
+
if node.next
|
287
|
+
node.next.prev = new_node
|
288
|
+
else
|
289
|
+
@tail = new_node
|
290
|
+
end
|
291
|
+
node.next = new_node
|
292
|
+
@length += 1
|
293
|
+
end
|
294
|
+
end
|
195
295
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
296
|
+
|
297
|
+
# Inserts data before passed node.
|
298
|
+
#
|
299
|
+
# == Returns:
|
300
|
+
# Inserted node
|
301
|
+
#
|
302
|
+
def insert_before_node(data, node)
|
303
|
+
Node(data).tap do |new_node|
|
304
|
+
new_node.next = node
|
305
|
+
new_node.prev = node.prev
|
306
|
+
if node.prev
|
307
|
+
node.prev.next = new_node
|
308
|
+
else
|
309
|
+
@head = new_node
|
310
|
+
end
|
311
|
+
node.prev = new_node
|
312
|
+
@length += 1
|
313
|
+
end
|
200
314
|
end
|
201
315
|
|
202
|
-
|
203
|
-
|
316
|
+
private
|
317
|
+
|
318
|
+
def __unlink_node(node)
|
319
|
+
if node.prev.nil?
|
204
320
|
node.next.prev = nil if node.next
|
205
321
|
@head = node.next
|
206
|
-
elsif node.next.
|
322
|
+
elsif node.next.nil?
|
207
323
|
node.prev.next = nil if node.prev
|
208
324
|
@tail = node.prev
|
209
325
|
else
|
@@ -212,6 +328,13 @@ module LinkedList
|
|
212
328
|
@length -= 1
|
213
329
|
end
|
214
330
|
|
331
|
+
|
332
|
+
def __to_matcher(val = nil, &block)
|
333
|
+
raise ArgumentError, 'either value or block should be passed' if val && block_given?
|
334
|
+
block = ->(e) { e == val } unless block_given?
|
335
|
+
->(node) { block.call(node.data) }
|
336
|
+
end
|
337
|
+
|
215
338
|
def __shift
|
216
339
|
head = @head
|
217
340
|
@head = @head.next
|
@@ -226,6 +349,14 @@ module LinkedList
|
|
226
349
|
tail
|
227
350
|
end
|
228
351
|
|
352
|
+
def __reverse_each
|
353
|
+
curr_node = @tail
|
354
|
+
while(curr_node)
|
355
|
+
yield curr_node
|
356
|
+
curr_node = curr_node.prev
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
229
360
|
def __each
|
230
361
|
curr_node = @head
|
231
362
|
while(curr_node)
|
data/lib/linked-list/version.rb
CHANGED
data/linked-list.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_development_dependency 'bundler', '>= 1.3', '<= 2.0'
|
22
|
-
spec.add_development_dependency 'coveralls'
|
23
|
-
spec.add_development_dependency 'm', '~> 1.5.0'
|
22
|
+
spec.add_development_dependency 'coveralls', '~> 0'
|
23
|
+
spec.add_development_dependency 'm', '~> 1.5', '>= 1.5.0'
|
24
24
|
spec.add_development_dependency 'minitest', '>= 5.0', '<= 6.0'
|
25
|
-
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rake', '~> 0'
|
26
26
|
end
|
data/test/list_test.rb
CHANGED
@@ -134,6 +134,229 @@ describe LinkedList::List do
|
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
|
+
describe '#insert' do
|
138
|
+
it 'raises error if after and before are passed' do
|
139
|
+
err = assert_raises ArgumentError do
|
140
|
+
assert_nil list.insert(1, after: 'x', before: 'y')
|
141
|
+
end
|
142
|
+
assert_equal err.message, 'either :after or :before keys should be passed'
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'raises error if no after nore before are passed' do
|
146
|
+
err = assert_raises ArgumentError do
|
147
|
+
assert_nil list.insert(1)
|
148
|
+
end
|
149
|
+
assert_equal err.message, 'either :after or :before keys should be passed'
|
150
|
+
end
|
151
|
+
|
152
|
+
describe 'after:' do
|
153
|
+
describe 'by block' do
|
154
|
+
it 'does not add value if insert after not found' do
|
155
|
+
list.push('foo')
|
156
|
+
assert_nil list.insert(1, after: ->(d) { d == 'foo1' })
|
157
|
+
assert_equal ['foo'], list.to_a
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'inserts value after first matching node by block' do
|
161
|
+
list.push('foo')
|
162
|
+
list.push('bar')
|
163
|
+
assert_equal 1, list.insert(1, after: ->(d) { d == 'foo' })
|
164
|
+
assert_equal ['foo', 1, 'bar'], list.to_a
|
165
|
+
assert_equal 3, list.length
|
166
|
+
end
|
167
|
+
|
168
|
+
describe 'position edge cases' do
|
169
|
+
before do
|
170
|
+
list.push(0)
|
171
|
+
list.push(1)
|
172
|
+
list.push(2)
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'inserts after in the middle' do
|
176
|
+
list.insert('foo', after: ->(d) { d == 0 })
|
177
|
+
assert_equal [0, 'foo', 1, 2], list.to_a
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'inserts after the tail' do
|
181
|
+
list.insert('foo', after: ->(d) { d == 2 })
|
182
|
+
assert_equal [0, 1, 2, 'foo'], list.to_a
|
183
|
+
assert_equal 'foo', list.last
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe 'by value' do
|
189
|
+
it 'does not add value if insert after not found' do
|
190
|
+
list.push('foo')
|
191
|
+
assert_nil list.insert(1, after: 'foo1')
|
192
|
+
assert_equal ['foo'], list.to_a
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'inserts value after first matching node by block' do
|
196
|
+
list.push('foo')
|
197
|
+
list.push('bar')
|
198
|
+
assert_equal 1, list.insert(1, after: 'foo')
|
199
|
+
assert_equal ['foo', 1, 'bar'], list.to_a
|
200
|
+
assert_equal 3, list.length
|
201
|
+
end
|
202
|
+
|
203
|
+
describe 'position edge cases' do
|
204
|
+
before do
|
205
|
+
list.push(0)
|
206
|
+
list.push(1)
|
207
|
+
list.push(2)
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'inserts after in the middle' do
|
211
|
+
list.insert('foo', after: 0)
|
212
|
+
assert_equal [0, 'foo', 1, 2], list.to_a
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'inserts after the tail' do
|
216
|
+
list.insert('foo', after: 2)
|
217
|
+
assert_equal [0, 1, 2, 'foo'], list.to_a
|
218
|
+
assert_equal 'foo', list.last
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
describe ':before' do
|
225
|
+
describe 'by block' do
|
226
|
+
it 'does not add value if insert before not found' do
|
227
|
+
list.push('foo')
|
228
|
+
assert_nil list.insert(1, before: ->(d) { d == 'foo1' })
|
229
|
+
assert_equal ['foo'], list.to_a
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'inserts value before first matching node by block' do
|
233
|
+
list.push('foo')
|
234
|
+
list.push('bar')
|
235
|
+
assert_equal 1, list.insert(1, before: ->(d) { d == 'foo' })
|
236
|
+
assert_equal [1, 'foo', 'bar'], list.to_a
|
237
|
+
assert_equal 3, list.length
|
238
|
+
end
|
239
|
+
|
240
|
+
describe 'position edge cases' do
|
241
|
+
before do
|
242
|
+
list.push(0)
|
243
|
+
list.push(1)
|
244
|
+
list.push(2)
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'inserts before head' do
|
248
|
+
list.insert('foo', before: ->(d) { d == 0 })
|
249
|
+
assert_equal ['foo', 0, 1, 2], list.to_a
|
250
|
+
assert_equal 'foo', list.first
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'inserts before in the middle' do
|
254
|
+
list.insert('foo', before: ->(d) { d == 2 })
|
255
|
+
assert_equal [0, 1, 'foo', 2], list.to_a
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
describe 'by value' do
|
261
|
+
it 'does not add value if insert before not found' do
|
262
|
+
list.push('foo')
|
263
|
+
assert_nil list.insert(1, before: 'foo1')
|
264
|
+
assert_equal ['foo'], list.to_a
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'inserts value before first' do
|
268
|
+
list.push('foo')
|
269
|
+
list.push('bar')
|
270
|
+
assert_equal 1, list.insert(1, before: 'foo')
|
271
|
+
assert_equal [1, 'foo', 'bar'], list.to_a
|
272
|
+
assert_equal 3, list.length
|
273
|
+
end
|
274
|
+
|
275
|
+
describe 'position edge cases' do
|
276
|
+
before do
|
277
|
+
list.push(0)
|
278
|
+
list.push(1)
|
279
|
+
list.push(2)
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'inserts before head' do
|
283
|
+
list.insert('foo', before: 0)
|
284
|
+
assert_equal ['foo', 0, 1, 2], list.to_a
|
285
|
+
assert_equal 'foo', list.first
|
286
|
+
end
|
287
|
+
|
288
|
+
it 'inserts before in the middle' do
|
289
|
+
list.insert('foo', before: 2)
|
290
|
+
assert_equal [0, 1, 'foo', 2], list.to_a
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
describe '#insert_after_node' do
|
298
|
+
it 'inserts value after passed node' do
|
299
|
+
list.push('foo')
|
300
|
+
list.push('bar')
|
301
|
+
node = list.each_node.find { |n| n.data == 'foo' }
|
302
|
+
assert_equal 1, list.insert_after_node(1, node).data
|
303
|
+
assert_equal ['foo', 1, 'bar'], list.to_a
|
304
|
+
assert_equal 3, list.length
|
305
|
+
end
|
306
|
+
|
307
|
+
describe 'position edge cases' do
|
308
|
+
before do
|
309
|
+
list.push(0)
|
310
|
+
list.push(1)
|
311
|
+
list.push(2)
|
312
|
+
end
|
313
|
+
|
314
|
+
it 'inserts after in the middle' do
|
315
|
+
node = list.each_node.find { |n| n.data == 0 }
|
316
|
+
list.insert_after_node('foo', node)
|
317
|
+
assert_equal [0, 'foo', 1, 2], list.to_a
|
318
|
+
end
|
319
|
+
|
320
|
+
it 'inserts after the tail' do
|
321
|
+
node = list.each_node.find { |n| n.data == 2 }
|
322
|
+
list.insert_after_node('foo', node)
|
323
|
+
assert_equal [0, 1, 2, 'foo'], list.to_a
|
324
|
+
assert_equal 'foo', list.last
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
describe '#insert_after_node' do
|
330
|
+
it 'inserts value before first' do
|
331
|
+
list.push('foo')
|
332
|
+
list.push('bar')
|
333
|
+
node = list.each_node.find { |n| n.data == 'foo' }
|
334
|
+
assert_equal 1, list.insert_before_node(1, node).data
|
335
|
+
assert_equal [1, 'foo', 'bar'], list.to_a
|
336
|
+
assert_equal 3, list.length
|
337
|
+
end
|
338
|
+
|
339
|
+
describe 'position edge cases' do
|
340
|
+
before do
|
341
|
+
list.push(0)
|
342
|
+
list.push(1)
|
343
|
+
list.push(2)
|
344
|
+
end
|
345
|
+
|
346
|
+
it 'inserts before head' do
|
347
|
+
node = list.each_node.find { |n| n.data == 0 }
|
348
|
+
list.insert_before_node('foo', node)
|
349
|
+
assert_equal ['foo', 0, 1, 2], list.to_a
|
350
|
+
assert_equal 'foo', list.first
|
351
|
+
end
|
352
|
+
|
353
|
+
it 'inserts before in the middle' do
|
354
|
+
node = list.each_node.find { |n| n.data == 2 }
|
355
|
+
list.insert_before_node('foo', node)
|
356
|
+
assert_equal [0, 1, 'foo', 2], list.to_a
|
357
|
+
end
|
358
|
+
end
|
359
|
+
end
|
137
360
|
|
138
361
|
describe '#delete' do
|
139
362
|
it 'raises error if block and value are passed' do
|
@@ -149,12 +372,14 @@ describe LinkedList::List do
|
|
149
372
|
end
|
150
373
|
|
151
374
|
it 'deletes value in first matching node' do
|
375
|
+
calls_count = 0
|
152
376
|
list.push('foo')
|
153
377
|
list.push('foo')
|
154
378
|
list.push('bar')
|
155
379
|
list.push('foo')
|
156
|
-
list.delete { |d| d == '
|
157
|
-
assert_equal ['foo', '
|
380
|
+
list.delete { |d| calls_count += 1;d == 'bar' }
|
381
|
+
assert_equal ['foo', 'foo', 'foo'], list.to_a
|
382
|
+
assert_equal 3, calls_count
|
158
383
|
end
|
159
384
|
|
160
385
|
it 'returns deleted value' do
|
@@ -189,7 +414,6 @@ describe LinkedList::List do
|
|
189
414
|
assert_equal [0, 2], list.to_a
|
190
415
|
end
|
191
416
|
|
192
|
-
|
193
417
|
it 'deletes value from tail' do
|
194
418
|
list.delete { |d| d == 2 }
|
195
419
|
assert_equal [0, 1], list.to_a
|
@@ -416,6 +640,48 @@ describe LinkedList::List do
|
|
416
640
|
end
|
417
641
|
end
|
418
642
|
|
643
|
+
describe '#each_node' do
|
644
|
+
it 'returns enumerator if no block given' do
|
645
|
+
assert_instance_of Enumerator, list.each
|
646
|
+
end
|
647
|
+
|
648
|
+
it 'pass each node data to the block' do
|
649
|
+
list.push(node_1)
|
650
|
+
list.push(node_2)
|
651
|
+
nodes = []
|
652
|
+
list.each_node { |e| nodes << e }
|
653
|
+
assert_equal %w(foo bar), nodes.map(&:data)
|
654
|
+
end
|
655
|
+
end
|
656
|
+
|
657
|
+
describe '#reverse_each' do
|
658
|
+
it 'returns enumerator if no block given' do
|
659
|
+
assert_instance_of Enumerator, list.each
|
660
|
+
end
|
661
|
+
|
662
|
+
it 'pass each node data to the block' do
|
663
|
+
list.push(node_1)
|
664
|
+
list.push(node_2)
|
665
|
+
nodes = []
|
666
|
+
list.reverse_each { |e| nodes << e }
|
667
|
+
assert_equal %w(bar foo), nodes
|
668
|
+
end
|
669
|
+
end
|
670
|
+
|
671
|
+
describe '#reverse_each_node' do
|
672
|
+
it 'returns enumerator if no block given' do
|
673
|
+
assert_instance_of Enumerator, list.each
|
674
|
+
end
|
675
|
+
|
676
|
+
it 'pass each node data to the block' do
|
677
|
+
list.push(node_1)
|
678
|
+
list.push(node_2)
|
679
|
+
nodes = []
|
680
|
+
list.reverse_each_node { |e| nodes << e }
|
681
|
+
assert_equal %w(bar foo), nodes.map(&:data)
|
682
|
+
end
|
683
|
+
end
|
684
|
+
|
419
685
|
describe '#inspect' do
|
420
686
|
it 'includes class name' do
|
421
687
|
assert_match(/LinkedList::List/, list.inspect)
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linked-list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Velikanau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -34,14 +34,14 @@ dependencies:
|
|
34
34
|
name: coveralls
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,9 @@ dependencies:
|
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.5'
|
54
|
+
- - ">="
|
52
55
|
- !ruby/object:Gem::Version
|
53
56
|
version: 1.5.0
|
54
57
|
type: :development
|
@@ -56,6 +59,9 @@ dependencies:
|
|
56
59
|
version_requirements: !ruby/object:Gem::Requirement
|
57
60
|
requirements:
|
58
61
|
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '1.5'
|
64
|
+
- - ">="
|
59
65
|
- !ruby/object:Gem::Version
|
60
66
|
version: 1.5.0
|
61
67
|
- !ruby/object:Gem::Dependency
|
@@ -82,14 +88,14 @@ dependencies:
|
|
82
88
|
name: rake
|
83
89
|
requirement: !ruby/object:Gem::Requirement
|
84
90
|
requirements:
|
85
|
-
- - "
|
91
|
+
- - "~>"
|
86
92
|
- !ruby/object:Gem::Version
|
87
93
|
version: '0'
|
88
94
|
type: :development
|
89
95
|
prerelease: false
|
90
96
|
version_requirements: !ruby/object:Gem::Requirement
|
91
97
|
requirements:
|
92
|
-
- - "
|
98
|
+
- - "~>"
|
93
99
|
- !ruby/object:Gem::Version
|
94
100
|
version: '0'
|
95
101
|
description: Ruby implementation of Doubly Linked List, following some Ruby idioms.
|