skn_utils 3.3.4 → 3.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/bench_linklists.rb +74 -0
- data/bin/{benchmark.rb → bench_nested_result.rb} +15 -51
- data/lib/skn_utils/lists/circular_linked_list.rb +99 -46
- data/lib/skn_utils/lists/doubly_linked_list.rb +42 -8
- data/lib/skn_utils/lists/link_node.rb +8 -8
- data/lib/skn_utils/lists/linked_list.rb +34 -12
- data/lib/skn_utils/version.rb +1 -1
- data/spec/lib/skn_utils/lists/Circular_linked_list_spec.rb +165 -258
- data/spec/lib/skn_utils/lists/doubly_linked_list_spec.rb +165 -260
- data/spec/lib/skn_utils/lists/linked_list_spec.rb +145 -260
- data/spec/spec_helper.rb +1 -0
- data/spec/support/shared_examples_for_linked_list.rb +156 -0
- metadata +6 -3
@@ -2,268 +2,153 @@
|
|
2
2
|
# spec/lib/skn_utils/linked_list_spec.rb
|
3
3
|
#
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
context "Initialization" do
|
8
|
-
it "can be initialized without params" do
|
9
|
-
expect(subject).to be
|
10
|
-
end
|
11
|
-
it "can insert the first value" do
|
12
|
-
expect(subject.empty?).to be true
|
13
|
-
expect(subject.insert(101)).to eq(1)
|
14
|
-
end
|
15
|
-
it "can be cleared" do
|
16
|
-
subject.insert(101)
|
17
|
-
expect(subject.clear).to eq(1)
|
18
|
-
end
|
19
|
-
it "can be initialized with one or more initial values" do
|
20
|
-
list = described_class.new(10,100,100)
|
21
|
-
expect(list.current).to eq(10)
|
22
|
-
end
|
23
|
-
it "can be initialized with multiple values" do
|
24
|
-
list = described_class.new(10, 100, 1000)
|
25
|
-
expect(list.current).to eq(10)
|
26
|
-
end
|
27
|
-
it "can not be initialized with any array as the value." do
|
28
|
-
list = described_class.new([10, 100, 1000])
|
29
|
-
expect(list.current).to eq([10, 100, 1000])
|
30
|
-
end
|
31
|
-
it "is initially empty?" do
|
32
|
-
expect(subject.empty?).to be true
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "Navigation" do
|
37
|
-
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
38
|
-
|
39
|
-
it "#first returns the first value" do
|
40
|
-
expect(list.first).to eq(10)
|
41
|
-
end
|
42
|
-
it "#next returns the second value" do
|
43
|
-
expect(list.first).to eq(10)
|
44
|
-
expect(list.next).to eq(20)
|
45
|
-
end
|
46
|
-
it "#current returns the last value as a side-effect of initialization via new" do
|
47
|
-
expect(list.current).to eq(10)
|
48
|
-
end
|
49
|
-
it "#last returns the last value" do
|
50
|
-
expect(list.last).to eq(100)
|
51
|
-
end
|
52
|
-
it "#nth(6) returns the sixth value" do
|
53
|
-
expect(list.first).to eq(10)
|
54
|
-
expect(list.nth(6)).to eq(60)
|
55
|
-
end
|
56
|
-
it "#at_index(6) returns the sixth value" do
|
57
|
-
expect(list.at_index(6)).to eq(60)
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
context "Insertions" do
|
63
|
-
it "#insert(value) indicates a value was added" do
|
64
|
-
bsize = subject.size
|
65
|
-
expect(subject.insert(110)).to eq(bsize + 1)
|
66
|
-
end
|
67
|
-
it "#prepend(value) indicates a value was added" do
|
68
|
-
bsize = subject.size
|
69
|
-
expect(subject.prepend(110)).to eq(bsize + 1)
|
70
|
-
end
|
71
|
-
it "#append(value) indicates a value was added" do
|
72
|
-
bsize = subject.size
|
73
|
-
expect(subject.append(110)).to eq(bsize + 1)
|
74
|
-
end
|
75
|
-
it "#insert_before(pvalue,value) indicates a value was added" do
|
76
|
-
subject.insert(120)
|
77
|
-
bsize = subject.size
|
78
|
-
expect(subject.insert_before(120, 110)).to eq(bsize + 1)
|
79
|
-
expect(subject.to_a).to eq([110,120])
|
80
|
-
end
|
81
|
-
it "#insert_after(value) indicates a value was added" do
|
82
|
-
subject.insert(120)
|
83
|
-
bsize = subject.size
|
84
|
-
expect(subject.insert_after(120, 125)).to eq(bsize + 1)
|
85
|
-
expect(subject.to_a).to eq([120,125])
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context "Removals" do
|
90
|
-
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
91
|
-
|
92
|
-
it "#remove(value) removes first occurance of that value" do
|
93
|
-
bsize = list.size
|
94
|
-
expect(list.remove(30)).to eq(bsize - 1)
|
95
|
-
expect(list.to_a).to eq([10,20, 40, 50, 60, 70, 80, 90, 100])
|
96
|
-
end
|
97
|
-
|
98
|
-
it "#clear removes all elements from list" do
|
99
|
-
expect(list.clear).to eq(10)
|
100
|
-
expect(list.empty?).to be true
|
101
|
-
end
|
102
|
-
end
|
5
|
+
require "support/shared_examples_for_linked_list"
|
103
6
|
|
104
|
-
|
105
|
-
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
106
|
-
it "#each works as expected when block is provided" do
|
107
|
-
x = []
|
108
|
-
list.each {|r| x << r}
|
109
|
-
expect(x).to be_a(Array)
|
110
|
-
expect(x).to eq([10,20, 30, 40, 50, 60, 70, 80, 90, 100])
|
111
|
-
end
|
112
|
-
it "#each works as expected when no block is offered" do
|
113
|
-
expect(list.each).to be_a(Enumerator)
|
114
|
-
expect(list.each.first).to eq(10)
|
115
|
-
end
|
116
|
-
it "#to_a returns the contents of linkedlist as an Array" do
|
117
|
-
base = list.to_a
|
118
|
-
expect(base).to be_a(Array)
|
119
|
-
expect(base).to eq([10,20, 30, 40, 50, 60, 70, 80, 90, 100])
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
context "Edge cases " do
|
124
|
-
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
125
|
-
|
126
|
-
it "#at_index(-999) fails and returns the current element. " do
|
127
|
-
expect(list.at_index(-999)).to eq(10)
|
128
|
-
end
|
129
|
-
it "#at_index(0) fails and returns the current element. " do
|
130
|
-
expect(list.at_index(0)).to eq(10)
|
131
|
-
end
|
132
|
-
it "#at_index(999) fails and returns the current element. " do
|
133
|
-
expect(list.at_index(999)).to eq(10)
|
134
|
-
end
|
135
|
-
it "#at_index(n) returns the proper element. " do
|
136
|
-
expect(list.at_index(1)).to eq(10)
|
137
|
-
expect(list.at_index(list.size / 2)).to eq(50)
|
138
|
-
expect(list.at_index(list.size)).to eq(100)
|
139
|
-
end
|
140
|
-
it "#at_index(n) returns the proper element for linkedlist with one element. " do
|
141
|
-
only = described_class.new(55)
|
142
|
-
expect(only.at_index(1)).to eq(55)
|
143
|
-
expect(only.at_index(10)).to eq(55)
|
144
|
-
expect(only.at_index(-10)).to eq(55)
|
145
|
-
end
|
146
|
-
|
147
|
-
it "#nth(-999) returns first value because backward movement is not supported" do
|
148
|
-
expect(list.nth(-999)).to eq(10)
|
149
|
-
end
|
150
|
-
it "#nth(0) returns current value, or last initialization value." do
|
151
|
-
expect(list.nth(0)).to eq(10)
|
152
|
-
end
|
153
|
-
it "#nth(999) returns last initialization value." do
|
154
|
-
expect(list.nth(999)).to eq(100)
|
155
|
-
end
|
156
|
-
it "#current equals last initialization value." do
|
157
|
-
expect(list.current).to eq(10)
|
158
|
-
end
|
159
|
-
it "#next after initialization returns proper order. " do
|
160
|
-
expect(list.next).to eq(20)
|
161
|
-
expect(list.next).to eq(30)
|
162
|
-
expect(list.next).to eq(40)
|
163
|
-
end
|
164
|
-
it "#first, #next, #current, #prev, #nth, and #last return same value after initialization with one value. " do
|
165
|
-
only = described_class.new(55)
|
166
|
-
expect(only.first).to eq(55)
|
167
|
-
expect(only.next).to eq(55)
|
168
|
-
expect(only.last).to eq(55)
|
169
|
-
expect(only.current).to eq(55)
|
170
|
-
expect(only.nth(1)).to eq(55)
|
171
|
-
expect(only.nth(11)).to eq(55)
|
172
|
-
end
|
173
|
-
it "#first, #next, #current, #prev, #nth, and #last return same value after initialization with no values. " do
|
174
|
-
only = described_class.new
|
175
|
-
expect(only.first).to be nil
|
176
|
-
expect(only.next).to be nil
|
177
|
-
expect(only.last).to be nil
|
178
|
-
expect(only.current).to be nil
|
179
|
-
expect(only.nth(1)).to be nil
|
180
|
-
expect(only.nth(-1)).to be nil
|
181
|
-
end
|
182
|
-
it "#prepend enables navigation methods normal operations. " do
|
183
|
-
only = described_class.new
|
184
|
-
only.prepend(55)
|
185
|
-
expect(only.first).to eq(55)
|
186
|
-
expect(only.next).to eq(55)
|
187
|
-
expect(only.last).to eq(55)
|
188
|
-
expect(only.current).to eq(55)
|
189
|
-
expect(only.nth(1)).to eq(55)
|
190
|
-
expect(only.nth(11)).to eq(55)
|
191
|
-
end
|
192
|
-
it "#append enables navigation methods normal operations. " do
|
193
|
-
only = described_class.new
|
194
|
-
only.append(55)
|
195
|
-
expect(only.first).to eq(55)
|
196
|
-
expect(only.next).to eq(55)
|
197
|
-
expect(only.last).to eq(55)
|
198
|
-
expect(only.current).to eq(55)
|
199
|
-
expect(only.nth(1)).to eq(55)
|
200
|
-
expect(only.nth(11)).to eq(55)
|
201
|
-
end
|
202
|
-
it "#insert_before enables navigation methods normal operations. " do
|
203
|
-
only = described_class.new
|
204
|
-
only.insert_before(nil, 55)
|
205
|
-
expect(only.first).to eq(55)
|
206
|
-
expect(only.next).to eq(55)
|
207
|
-
expect(only.last).to eq(55)
|
208
|
-
expect(only.current).to eq(55)
|
209
|
-
expect(only.nth(1)).to eq(55)
|
210
|
-
expect(only.nth(11)).to eq(55)
|
211
|
-
end
|
212
|
-
it "#insert_after enables navigation methods normal operations. " do
|
213
|
-
only = described_class.new
|
214
|
-
only.insert_after(nil, 55)
|
215
|
-
expect(only.first).to eq(55)
|
216
|
-
expect(only.next).to eq(55)
|
217
|
-
expect(only.last).to eq(55)
|
218
|
-
expect(only.current).to eq(55)
|
219
|
-
expect(only.nth(1)).to eq(55)
|
220
|
-
expect(only.nth(11)).to eq(55)
|
221
|
-
end
|
222
|
-
it "#remove does not make navigation methods unstable if only element. " do
|
223
|
-
only = described_class.new(55)
|
224
|
-
only.remove(55)
|
225
|
-
expect(only.first).to be nil
|
226
|
-
expect(only.next).to be nil
|
227
|
-
expect(only.last).to be nil
|
228
|
-
expect(only.current).to be nil
|
229
|
-
expect(only.nth(1)).to be nil
|
230
|
-
expect(only.nth(-1)).to be nil
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
context "Sort Feature" do
|
235
|
-
let(:num_list) { described_class.new(100, 50, 10, 40, 80, 30, 60, 90, 70, 20, 110) }
|
236
|
-
let(:alpha_list) { described_class.new('Z', 'K', 'S', 'n', 's', 'z', 'k', 'N', 'o', 'A') }
|
237
|
-
let(:hash_list) { described_class.new({key: 'Z'}, {key: 'K'}, {key: 'S'}, {key: 'n'}, {key: 's'},
|
238
|
-
{key: 'z'}, {key: 'k'}, {key: 'N'}, {key: 'o'}, {key: 'A'}
|
239
|
-
) {|a| a[:key]}
|
240
|
-
}
|
7
|
+
RSpec.describe SknUtils::Lists::LinkedList, "Singular LinkedList " do
|
241
8
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
9
|
+
it_behaves_like "a linked list", 'single'
|
10
|
+
|
11
|
+
context "LinkedList Specific" do
|
12
|
+
|
13
|
+
context "Edge cases " do
|
14
|
+
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
15
|
+
|
16
|
+
it "#at_index(-999) fails and returns the current element. " do
|
17
|
+
expect(list.at_index(-999)).to eq(10)
|
18
|
+
end
|
19
|
+
it "#at_index(0) fails and returns the current element. " do
|
20
|
+
expect(list.at_index(0)).to eq(10)
|
21
|
+
end
|
22
|
+
it "#at_index(999) fails and returns the current element. " do
|
23
|
+
expect(list.at_index(999)).to eq(10)
|
24
|
+
end
|
25
|
+
it "#at_index(n) returns the proper element. " do
|
26
|
+
expect(list.at_index(1)).to eq(10)
|
27
|
+
expect(list.at_index(list.size / 2)).to eq(50)
|
28
|
+
expect(list.at_index(list.size)).to eq(100)
|
29
|
+
end
|
30
|
+
it "#at_index(n) returns the proper element for linkedlist with one element. " do
|
31
|
+
only = described_class.new(55)
|
32
|
+
expect(only.at_index(1)).to eq(55)
|
33
|
+
expect(only.at_index(10)).to eq(55)
|
34
|
+
expect(only.at_index(-10)).to eq(55)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "#nth(-9) returns first value because backward movement is not supported" do
|
38
|
+
expect(list.nth(-9)).to eq(10)
|
39
|
+
end
|
40
|
+
it "#nth(0) returns current value." do
|
41
|
+
expect(list.nth(0)).to eq(10)
|
42
|
+
end
|
43
|
+
it "#nth(999) returns last value." do
|
44
|
+
expect(list.nth(999)).to eq(100)
|
45
|
+
end
|
46
|
+
it "#current equals first value." do
|
47
|
+
expect(list.current).to eq(10)
|
48
|
+
end
|
49
|
+
it "#next after initialization returns proper order. " do
|
50
|
+
expect(list.next).to eq(20)
|
51
|
+
expect(list.next).to eq(30)
|
52
|
+
expect(list.next).to eq(40)
|
53
|
+
end
|
54
|
+
it "#first, #next, #current, #nth, and #last return same value when size eq 1. " do
|
55
|
+
only = described_class.new(55)
|
56
|
+
expect(only.first).to eq(55)
|
57
|
+
expect(only.next).to eq(55)
|
58
|
+
expect(only.last).to eq(55)
|
59
|
+
expect(only.current).to eq(55)
|
60
|
+
expect(only.nth(2)).to eq(55)
|
61
|
+
expect(only.at_index(2)).to eq(55)
|
62
|
+
end
|
63
|
+
it "#first, #next, #current, #nth, #at_index, and #last return same value when size eq 0. " do
|
64
|
+
only = described_class.new
|
65
|
+
expect(only.first).to be nil
|
66
|
+
expect(only.next).to be nil
|
67
|
+
expect(only.last).to be nil
|
68
|
+
expect(only.current).to be nil
|
69
|
+
expect(only.nth(2)).to be nil
|
70
|
+
expect(only.at_index(2)).to be nil
|
71
|
+
end
|
72
|
+
it "#prepend enables navigation methods normal operations. " do
|
73
|
+
only = described_class.new
|
74
|
+
only.prepend(55)
|
75
|
+
expect(only.first).to eq(55)
|
76
|
+
expect(only.next).to eq(55)
|
77
|
+
expect(only.last).to eq(55)
|
78
|
+
expect(only.current).to eq(55)
|
79
|
+
expect(only.nth(2)).to eq(55)
|
80
|
+
expect(only.at_index(2)).to eq(55)
|
81
|
+
end
|
82
|
+
it "#append enables navigation methods normal operations. " do
|
83
|
+
only = described_class.new
|
84
|
+
only.append(55)
|
85
|
+
expect(only.first).to eq(55)
|
86
|
+
expect(only.next).to eq(55)
|
87
|
+
expect(only.last).to eq(55)
|
88
|
+
expect(only.current).to eq(55)
|
89
|
+
expect(only.nth(2)).to eq(55)
|
90
|
+
expect(only.at_index(2)).to eq(55)
|
91
|
+
end
|
92
|
+
it "#insert_before enables navigation methods normal operations. " do
|
93
|
+
only = described_class.new
|
94
|
+
only.insert_before(nil, 55)
|
95
|
+
expect(only.first).to eq(55)
|
96
|
+
expect(only.next).to eq(55)
|
97
|
+
expect(only.last).to eq(55)
|
98
|
+
expect(only.current).to eq(55)
|
99
|
+
expect(only.nth(2)).to eq(55)
|
100
|
+
expect(only.at_index(2)).to eq(55)
|
101
|
+
end
|
102
|
+
it "#insert_after enables navigation methods normal operations. " do
|
103
|
+
only = described_class.new
|
104
|
+
only.insert_after(nil, 55)
|
105
|
+
expect(only.first).to eq(55)
|
106
|
+
expect(only.next).to eq(55)
|
107
|
+
expect(only.last).to eq(55)
|
108
|
+
expect(only.current).to eq(55)
|
109
|
+
expect(only.nth(2)).to eq(55)
|
110
|
+
expect(only.at_index(2)).to eq(55)
|
111
|
+
end
|
112
|
+
it "#remove does not make navigation methods unstable when removing 2:2. " do
|
113
|
+
only = described_class.new(55,60)
|
114
|
+
only.remove(60)
|
115
|
+
expect(only.first).to eq(55)
|
116
|
+
expect(only.next).to eq(55)
|
117
|
+
expect(only.last).to eq(55)
|
118
|
+
expect(only.current).to eq(55)
|
119
|
+
expect(only.nth(2)).to eq(55)
|
120
|
+
expect(only.at_index(2)).to eq(55)
|
121
|
+
end
|
122
|
+
it "#remove does not make navigation methods unstable when removing 1:2. " do
|
123
|
+
only = described_class.new(55,60)
|
124
|
+
only.remove(55)
|
125
|
+
expect(only.first).to eq(60)
|
126
|
+
expect(only.next).to eq(60)
|
127
|
+
expect(only.last).to eq(60)
|
128
|
+
expect(only.current).to eq(60)
|
129
|
+
expect(only.nth(2)).to eq(60)
|
130
|
+
expect(only.at_index(2)).to eq(60)
|
131
|
+
end
|
132
|
+
it "#remove does not make navigation methods unstable if empty?. " do
|
133
|
+
only = described_class.new(55)
|
134
|
+
only.remove(55)
|
135
|
+
expect(only.first).to be nil
|
136
|
+
expect(only.next).to be nil
|
137
|
+
expect(only.last).to be nil
|
138
|
+
expect(only.current).to be nil
|
139
|
+
expect(only.nth(2)).to be nil
|
140
|
+
expect(only.at_index(2)).to be nil
|
141
|
+
end
|
142
|
+
it "#remove does not make navigation methods unstable when target not found. " do
|
143
|
+
only = described_class.new(55)
|
144
|
+
only.remove(20)
|
145
|
+
expect(only.first).to eq(55)
|
146
|
+
expect(only.next).to eq(55)
|
147
|
+
expect(only.last).to eq(55)
|
148
|
+
expect(only.current).to eq(55)
|
149
|
+
expect(only.nth(2)).to eq(55)
|
150
|
+
expect(only.at_index(2)).to eq(55)
|
151
|
+
end
|
267
152
|
end
|
268
153
|
end
|
269
154
|
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,156 @@
|
|
1
|
+
|
2
|
+
RSpec.shared_examples "a linked list" do |list_type|
|
3
|
+
|
4
|
+
context "Initialization" do
|
5
|
+
it "can be initialized without params" do
|
6
|
+
expect(subject).to be
|
7
|
+
end
|
8
|
+
it "can insert the first value" do
|
9
|
+
expect(subject.empty?).to be true
|
10
|
+
expect(subject.insert(101)).to eq(1)
|
11
|
+
end
|
12
|
+
it "can be cleared" do
|
13
|
+
subject.insert(101)
|
14
|
+
expect(subject.clear).to eq(1)
|
15
|
+
end
|
16
|
+
it "can be initialized with one or more initial values" do
|
17
|
+
list = described_class.new(10,100,100)
|
18
|
+
expect(list.size).to eq(3)
|
19
|
+
end
|
20
|
+
it "positions to first element after being initialized with multiple values" do
|
21
|
+
list = described_class.new(10, 100, 1000)
|
22
|
+
expect(list.current).to eq(10)
|
23
|
+
end
|
24
|
+
it "can not be initialized with any array as the single value." do
|
25
|
+
list = described_class.new([10, 100, 1000])
|
26
|
+
expect(list.current).to eq([10, 100, 1000])
|
27
|
+
expect(list.size).to eq(1)
|
28
|
+
end
|
29
|
+
it { is_expected.to be_empty }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "Navigation" do
|
33
|
+
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
34
|
+
|
35
|
+
it "#first returns the first value" do
|
36
|
+
expect(list.first).to eq(10)
|
37
|
+
end
|
38
|
+
it "#next returns the second value" do
|
39
|
+
expect(list.next).to eq(20)
|
40
|
+
end
|
41
|
+
it "#current returns the last value accessed" do
|
42
|
+
expect(list.current).to eq(10)
|
43
|
+
expect(list.next).to eq(20)
|
44
|
+
expect(list.current).to eq(20)
|
45
|
+
end
|
46
|
+
it "#last returns the last value" do
|
47
|
+
expect(list.last).to eq(100)
|
48
|
+
end
|
49
|
+
it "#nth(6) returns the sixth value" do
|
50
|
+
expect(list.nth(6)).to eq(60)
|
51
|
+
end
|
52
|
+
it "#at_index(6) returns the sixth value" do
|
53
|
+
expect(list.at_index(6)).to eq(60)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "Insertions" do
|
58
|
+
it "#insert(value) indicates a value was added" do
|
59
|
+
bsize = subject.size
|
60
|
+
expect(subject.insert(110)).to eq(bsize + 1)
|
61
|
+
end
|
62
|
+
it "#prepend(value) indicates a value was added" do
|
63
|
+
bsize = subject.size
|
64
|
+
expect(subject.prepend(110)).to eq(bsize + 1)
|
65
|
+
end
|
66
|
+
it "#append(value) indicates a value was added" do
|
67
|
+
bsize = subject.size
|
68
|
+
expect(subject.append(110)).to eq(bsize + 1)
|
69
|
+
end
|
70
|
+
it "#insert_before(pvalue,value) indicates a value was added" do
|
71
|
+
subject.insert(120)
|
72
|
+
bsize = subject.size
|
73
|
+
expect(subject.insert_before(120, 110)).to eq(bsize + 1)
|
74
|
+
expect(subject.to_a).to eq([110,120])
|
75
|
+
end
|
76
|
+
it "#insert_after(pvalue, value) indicates a value was added" do
|
77
|
+
subject.insert(120)
|
78
|
+
bsize = subject.size
|
79
|
+
expect(subject.insert_after(120, 125)).to eq(bsize + 1)
|
80
|
+
expect(subject.to_a).to eq([120,125])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "Removals" do
|
85
|
+
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
86
|
+
|
87
|
+
it "#remove(value) removes first occurance of that value" do
|
88
|
+
bsize = list.size
|
89
|
+
expect(list.remove(30)).to eq(bsize - 1)
|
90
|
+
expect(list.to_a).to eq([10,20, 40, 50, 60, 70, 80, 90, 100])
|
91
|
+
end
|
92
|
+
it "#clear removes all elements from list" do
|
93
|
+
expect(list.clear).to eq(10)
|
94
|
+
expect(list.empty?).to be true
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "Enumeration" do
|
99
|
+
let(:list) { described_class.new(10,20, 30, 40, 50, 60, 70, 80, 90, 100) }
|
100
|
+
|
101
|
+
it "#each works as expected when block is provided" do
|
102
|
+
x = []
|
103
|
+
list.each {|r| x << r}
|
104
|
+
expect(x).to be_a(Array)
|
105
|
+
expect(x).to eq([10,20, 30, 40, 50, 60, 70, 80, 90, 100])
|
106
|
+
end
|
107
|
+
it "#each works as expected when no block is offered" do
|
108
|
+
base = list.each
|
109
|
+
expect(base).to be_a(Enumerator)
|
110
|
+
8.times { base.next }
|
111
|
+
expect(base.next).to eq(90)
|
112
|
+
end
|
113
|
+
it "#to_a returns the contents of linkedlist as an Array" do
|
114
|
+
base = list.to_a
|
115
|
+
expect(base).to be_a(Array)
|
116
|
+
expect(base).to eq([10,20, 30, 40, 50, 60, 70, 80, 90, 100])
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "Sort Feature" do
|
121
|
+
let(:num_list) { described_class.new(100, 50, 10, 40, 80, 30, 60, 90, 70, 20, 110) }
|
122
|
+
let(:alpha_list) { described_class.new('Z', 'K', 'S', 'n', 's', 'z', 'k', 'N', 'o', 'A') }
|
123
|
+
let(:hash_list) { described_class.new({key: 'Z'}, {key: 'K'}, {key: 'S'}, {key: 'n'}, {key: 's'},
|
124
|
+
{key: 'z'}, {key: 'k'}, {key: 'N'}, {key: 'o'}, {key: 'A'}
|
125
|
+
) {|a| a[:key]}
|
126
|
+
}
|
127
|
+
|
128
|
+
it "#sort! redefines numeric list in asending order" do
|
129
|
+
expect(num_list.sort!).to eq(11)
|
130
|
+
expect(num_list.to_a).to eq([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110])
|
131
|
+
end
|
132
|
+
it "#sort!(:desc) redefines numeric list in descending order" do
|
133
|
+
expect(num_list.sort!(:desc)).to eq(11)
|
134
|
+
expect(num_list.to_a).to eq([110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10])
|
135
|
+
end
|
136
|
+
it "#sort! redefines alpha numeric list in asending order" do
|
137
|
+
expect(alpha_list.sort!).to eq(10)
|
138
|
+
expect(alpha_list.to_a).to eq(["A", "K", "N", "S", "Z", "k", "n", "o", "s", "z"])
|
139
|
+
end
|
140
|
+
it "#sort!(:desc) redefines alpha numeric list in descending order" do
|
141
|
+
expect(alpha_list.sort!(:desc)).to eq(10)
|
142
|
+
expect(alpha_list.to_a).to eq(["z", "s", "o", "n", "k", "Z", "S", "N", "K", "A"])
|
143
|
+
end
|
144
|
+
it "#sort!() redefines hash object values in default order" do
|
145
|
+
expect(hash_list.sort!).to eq(10)
|
146
|
+
expect(hash_list.to_a).to eq([{:key=>"A"}, {:key=>"K"}, {:key=>"N"}, {:key=>"S"}, {:key=>"Z"},
|
147
|
+
{:key=>"k"}, {:key=>"n"}, {:key=>"o"}, {:key=>"s"}, {:key=>"z"}])
|
148
|
+
end
|
149
|
+
it "#sort!() lambda overrides sort_condifiton and sorts hash object values in custom order" do
|
150
|
+
expect(hash_list.sort!() {|a,b| a[:key] <= b[:key] }).to eq(10)
|
151
|
+
expect(hash_list.to_a).to eq([{:key=>"z"}, {:key=>"s"}, {:key=>"o"}, {:key=>"n"}, {:key=>"k"},
|
152
|
+
{:key=>"Z"}, {:key=>"S"}, {:key=>"N"}, {:key=>"K"}, {:key=>"A"}])
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: skn_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.3.
|
4
|
+
version: 3.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Scott Jr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: deep_merge
|
@@ -128,7 +128,8 @@ files:
|
|
128
128
|
- README.rdoc
|
129
129
|
- Rakefile
|
130
130
|
- _config.yml
|
131
|
-
- bin/
|
131
|
+
- bin/bench_linklists.rb
|
132
|
+
- bin/bench_nested_result.rb
|
132
133
|
- bin/console
|
133
134
|
- lib/skn_hash.rb
|
134
135
|
- lib/skn_settings.rb
|
@@ -162,6 +163,7 @@ files:
|
|
162
163
|
- spec/lib/skn_utils/notifier_base_spec.rb
|
163
164
|
- spec/lib/skn_utils/null_object_spec.rb
|
164
165
|
- spec/spec_helper.rb
|
166
|
+
- spec/support/shared_examples_for_linked_list.rb
|
165
167
|
homepage: https://github.com/skoona/skn_utils
|
166
168
|
licenses:
|
167
169
|
- MIT
|
@@ -206,3 +208,4 @@ test_files:
|
|
206
208
|
- spec/lib/skn_utils/notifier_base_spec.rb
|
207
209
|
- spec/lib/skn_utils/null_object_spec.rb
|
208
210
|
- spec/spec_helper.rb
|
211
|
+
- spec/support/shared_examples_for_linked_list.rb
|