list 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -1
- data/ext/list/list.c +733 -29
- data/list.gemspec +1 -1
- data/spec/bench.rb +5 -4
- data/spec/list_spec.rb +806 -259
- data/spec/mem_spec.rb +2 -4
- metadata +2 -2
data/list.gemspec
CHANGED
data/spec/bench.rb
CHANGED
@@ -7,7 +7,7 @@ require 'list'
|
|
7
7
|
require 'benchmark'
|
8
8
|
|
9
9
|
Benchmark.bm(32) do |x|
|
10
|
-
[
|
10
|
+
[100000].each do |n|
|
11
11
|
x.report("#{n}times") do
|
12
12
|
n.times do
|
13
13
|
end
|
@@ -15,7 +15,7 @@ Benchmark.bm(32) do |x|
|
|
15
15
|
end
|
16
16
|
|
17
17
|
[Array, List].each do |obj|
|
18
|
-
[
|
18
|
+
[100000].each do |n|
|
19
19
|
x.report("#{obj}#new #{n}times") do
|
20
20
|
n.times do
|
21
21
|
obj.new
|
@@ -24,16 +24,17 @@ Benchmark.bm(32) do |x|
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
[[:push, 1], [:unshift, 1], [:pop], [:shift], [:insert, 0, 1], [:delete_at, 0]].each do |args|
|
27
|
+
[[:[], 100000], [:push, 1], [:unshift, 1], [:pop], [:shift], [:insert, 0, 1], [:delete_at, 0]].each do |args|
|
28
28
|
m = args.shift
|
29
29
|
[(0..100000).to_a, (0..100000).to_list].each do |obj|
|
30
|
-
[
|
30
|
+
[100000].each do |n|
|
31
31
|
o = obj.dup
|
32
32
|
x.report("#{o.class}##{m} #{n}times") do
|
33
33
|
n.times do
|
34
34
|
o.send(m, *args)
|
35
35
|
end
|
36
36
|
end
|
37
|
+
GC.start
|
37
38
|
end
|
38
39
|
end
|
39
40
|
end
|
data/spec/list_spec.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe List do
|
4
|
+
before :all do
|
5
|
+
@cls = List
|
6
|
+
@subcls = L
|
7
|
+
end
|
8
|
+
|
4
9
|
before :each do
|
5
10
|
GC.start
|
6
11
|
end
|
@@ -10,96 +15,104 @@ describe List do
|
|
10
15
|
end
|
11
16
|
|
12
17
|
it "class" do
|
13
|
-
expect(
|
18
|
+
expect(@cls).to be_include(Enumerable)
|
14
19
|
end
|
15
20
|
|
16
21
|
it "create" do
|
17
|
-
expect(
|
18
|
-
expect(
|
19
|
-
expect(
|
22
|
+
expect(@cls[]).to eq(@cls.new)
|
23
|
+
expect(@cls[1,2,3]).to eq(@cls.new [1,2,3])
|
24
|
+
expect(@subcls[1,2,3]).to eq(@subcls.new [1,2,3])
|
20
25
|
end
|
21
26
|
|
22
27
|
it "try_convert" do
|
23
|
-
expect(
|
24
|
-
expect(
|
25
|
-
expect(
|
28
|
+
expect(@cls.try_convert @cls[1,2,3]).to eq(@cls[1,2,3])
|
29
|
+
expect(@cls.try_convert [1,2,3]).to eq(@cls[1,2,3])
|
30
|
+
expect(@cls.try_convert "[1,2,3]").to eq(nil)
|
26
31
|
end
|
27
32
|
|
28
33
|
it "to_list" do
|
29
|
-
expect([].to_list).to eq(
|
30
|
-
expect([1,[2],3].to_list).to eq(
|
31
|
-
expect((0..5).to_list).to eq(
|
32
|
-
expect(
|
33
|
-
expect(
|
34
|
+
expect([].to_list).to eq(@cls.new)
|
35
|
+
expect([1,[2],3].to_list).to eq(@cls[1,[2],3])
|
36
|
+
expect((0..5).to_list).to eq(@cls[0,1,2,3,4,5])
|
37
|
+
expect(@cls[1,2,3].to_list).to eq(@cls[1,2,3])
|
38
|
+
expect(@subcls[1,2,3].to_list).to eq(@subcls[1,2,3])
|
39
|
+
klass = Class.new(Array)
|
40
|
+
list = klass.new.to_list
|
41
|
+
expect(list).to eq(@cls[])
|
42
|
+
expect(list.class).to eq(@cls)
|
34
43
|
end
|
35
44
|
|
36
45
|
it "initialize" do
|
37
|
-
expect(
|
38
|
-
expect(
|
39
|
-
expect(
|
40
|
-
expect(
|
41
|
-
expect(
|
42
|
-
expect(
|
43
|
-
expect(
|
44
|
-
expect
|
45
|
-
expect{
|
46
|
-
expect{
|
46
|
+
expect(@cls.new).to be_a_kind_of(@cls)
|
47
|
+
expect(@subcls.new).to be_a_kind_of(@cls)
|
48
|
+
expect(@cls.new([])).to be_a_kind_of(@cls)
|
49
|
+
expect(@cls.new([1,2,3])).to be_a_kind_of(@cls)
|
50
|
+
expect(@cls.new([1,2,3])).to eq(@cls[1,2,3])
|
51
|
+
expect(@cls.new(3)).to eq(@cls[nil,nil,nil])
|
52
|
+
expect(@cls.new(3, 0)).to eq(@cls[0,0,0])
|
53
|
+
expect(@cls.new(3){|i| "foo"}).to eq(@cls["foo", "foo", "foo"])
|
54
|
+
expect{@cls.new.instance_eval{ initialize }}.to_not raise_error
|
55
|
+
expect{@cls.new {}}.to_not raise_error
|
56
|
+
expect{@cls.new(3,0,0)}.to raise_error(ArgumentError)
|
57
|
+
expect{@cls.new(-1,1)}.to raise_error(ArgumentError)
|
58
|
+
expect{@cls.new("a")}.to raise_error(TypeError)
|
59
|
+
expect{@cls.new("a",0)}.to raise_error(TypeError)
|
47
60
|
end
|
48
61
|
|
49
62
|
it "dup and replace" do
|
50
|
-
list =
|
63
|
+
list = @cls.new
|
51
64
|
expect(list.dup).to eq(list)
|
52
|
-
list =
|
65
|
+
list = @cls[1,2,3]
|
53
66
|
expect(list.dup).to eq(list)
|
54
67
|
end
|
55
68
|
|
56
69
|
it "inspect" do
|
57
|
-
list =
|
58
|
-
expect(list.inspect).to eq("
|
70
|
+
list = @cls.new
|
71
|
+
expect(list.inspect).to eq("#<#{@cls}: []>")
|
59
72
|
list.push 1,[2],{:a=>3}
|
60
|
-
expect(list.inspect).to eq("
|
61
|
-
expect(
|
73
|
+
expect(list.inspect).to eq("#<#{@cls}: [1, [2], {:a=>3}]>")
|
74
|
+
expect(@subcls[1,2,3].inspect).to eq("#<#{@subcls}: [1, 2, 3]>")
|
62
75
|
end
|
63
76
|
|
64
77
|
it "to_a" do
|
65
|
-
list =
|
78
|
+
list = @cls.new
|
66
79
|
expect(list.to_a).to eq([])
|
67
80
|
a = (0..10).to_a
|
68
|
-
list =
|
81
|
+
list = @cls.new(a)
|
69
82
|
expect(list.to_a).to eq(a)
|
70
83
|
end
|
71
84
|
|
72
85
|
it "freeze and frozen?" do
|
73
|
-
list =
|
86
|
+
list = @cls.new
|
74
87
|
list.freeze
|
75
|
-
expect(list).to eq(
|
88
|
+
expect(list).to eq(@cls[])
|
76
89
|
expect(list.frozen?).to eq(true)
|
77
90
|
expect{ list.push 1 }.to raise_error
|
78
91
|
end
|
79
92
|
|
80
93
|
it "==" do
|
81
|
-
list =
|
82
|
-
expect(list).to eq(
|
94
|
+
list = @cls.new
|
95
|
+
expect(list).to eq(@cls.new)
|
83
96
|
expect(list).to eq(list)
|
84
97
|
list.push(1)
|
85
|
-
expect(list).to_not eq(
|
86
|
-
expect(list).to eq(
|
98
|
+
expect(list).to_not eq(@cls.new)
|
99
|
+
expect(list).to eq(@cls[1])
|
87
100
|
expect(list).to_not eq([1])
|
88
|
-
expect(
|
89
|
-
expect(
|
101
|
+
expect(@subcls[1]).to eq(@subcls[1])
|
102
|
+
expect(@subcls[1]).to eq(@cls[1])
|
90
103
|
end
|
91
104
|
|
92
105
|
it "eql?" do
|
93
|
-
list =
|
106
|
+
list = @cls.new
|
94
107
|
expect(list.eql?(list)).to be true
|
95
|
-
expect(list.eql?(
|
96
|
-
expect(list.eql?(
|
108
|
+
expect(list.eql?(@cls.new)).to be false
|
109
|
+
expect(list.eql?(@subcls.new)).to be false
|
97
110
|
end
|
98
111
|
|
99
112
|
it "hash" do
|
100
|
-
expect(
|
101
|
-
list1 =
|
102
|
-
list2 =
|
113
|
+
expect(@cls.new.hash).to eq(@cls.new.hash)
|
114
|
+
list1 = @cls[1,2,3]
|
115
|
+
list2 = @cls[1,2,3]
|
103
116
|
expect(list1.hash).to eq(list2.hash)
|
104
117
|
hash = {}
|
105
118
|
hash[list1] = 1
|
@@ -107,66 +120,74 @@ describe List do
|
|
107
120
|
end
|
108
121
|
|
109
122
|
it "[]" do
|
110
|
-
list =
|
123
|
+
list = @cls.new
|
111
124
|
expect(list[0]).to eq(nil)
|
112
125
|
list.push 1,2,3
|
113
126
|
expect(list[2]).to eq(3)
|
114
127
|
expect(list[-1]).to eq(3)
|
115
128
|
expect(list[10]).to eq(nil)
|
116
|
-
expect(list[1,1]).to eq(
|
117
|
-
expect(list[1,2]).to eq(
|
118
|
-
expect(list[1,10]).to eq(
|
129
|
+
expect(list[1,1]).to eq(@cls[2])
|
130
|
+
expect(list[1,2]).to eq(@cls[2,3])
|
131
|
+
expect(list[1,10]).to eq(@cls[2,3])
|
119
132
|
|
120
133
|
# special cases
|
121
134
|
expect(list[3]).to eq(nil)
|
122
135
|
expect(list[10,0]).to eq(nil)
|
123
|
-
expect(list[3,0]).to eq(
|
124
|
-
expect(list[3..10]).to eq(
|
136
|
+
expect(list[3,0]).to eq(@cls[])
|
137
|
+
expect(list[3..10]).to eq(@cls[])
|
138
|
+
|
139
|
+
expect{@cls[][0,0,0]}.to raise_error(ArgumentError)
|
125
140
|
end
|
126
141
|
|
127
142
|
it "[]=" do
|
128
|
-
list =
|
143
|
+
list = @cls.new
|
129
144
|
list[0] = 1
|
130
|
-
expect(list).to eq(
|
145
|
+
expect(list).to eq(@cls[1])
|
131
146
|
list[1] = 2
|
132
|
-
expect(list).to eq(
|
147
|
+
expect(list).to eq(@cls[1,2])
|
133
148
|
list[4] = 4
|
134
|
-
expect(list).to eq(
|
135
|
-
list[0,3] =
|
136
|
-
expect(list).to eq(
|
137
|
-
list[0,3] =
|
138
|
-
expect(list).to eq(
|
139
|
-
list[1..2] =
|
140
|
-
expect(list).to eq(
|
149
|
+
expect(list).to eq(@cls[1,2,nil,nil,4])
|
150
|
+
list[0,3] = @cls['a','b','c']
|
151
|
+
expect(list).to eq(@cls['a','b','c',nil,4])
|
152
|
+
list[0,3] = @cls['a','b','c']
|
153
|
+
expect(list).to eq(@cls['a','b','c',nil,4])
|
154
|
+
list[1..2] = @cls[1,2]
|
155
|
+
expect(list).to eq(@cls['a',1,2,nil,4])
|
141
156
|
list[0,2] = "?"
|
142
|
-
expect(list).to eq(
|
157
|
+
expect(list).to eq(@cls["?",2,nil,4])
|
143
158
|
list[0..2] = "A"
|
144
|
-
expect(list).to eq(
|
159
|
+
expect(list).to eq(@cls["A",4])
|
145
160
|
list[-1] = "Z"
|
146
|
-
expect(list).to eq(
|
161
|
+
expect(list).to eq(@cls["A","Z"])
|
147
162
|
list[1..-1] = nil
|
148
|
-
expect(list).to eq(
|
149
|
-
list[1..-1] =
|
150
|
-
expect(list).to eq(
|
151
|
-
list[0,0] =
|
152
|
-
expect(list).to eq(
|
163
|
+
expect(list).to eq(@cls["A",nil])
|
164
|
+
list[1..-1] = @cls[]
|
165
|
+
expect(list).to eq(@cls["A"])
|
166
|
+
list[0,0] = @cls[1,2]
|
167
|
+
expect(list).to eq(@cls[1,2,"A"])
|
153
168
|
list[3,0] = "B"
|
154
|
-
expect(list).to eq(
|
169
|
+
expect(list).to eq(@cls[1,2,"A","B"])
|
155
170
|
list[6,0] = "C"
|
156
|
-
expect(list).to eq(
|
157
|
-
expect{list["10"]}.to raise_error(TypeError)
|
171
|
+
expect(list).to eq(@cls[1,2,"A","B",nil,nil,"C"])
|
158
172
|
|
159
|
-
a =
|
173
|
+
a = @cls[1,2,3]
|
160
174
|
a[1,0] = a
|
161
|
-
expect(a).to eq(
|
175
|
+
expect(a).to eq(@cls[1,1,2,3,2,3]);
|
162
176
|
|
163
|
-
a =
|
177
|
+
a = @cls[1,2,3]
|
164
178
|
a[-1,0] = a
|
165
|
-
expect(a).to eq(
|
179
|
+
expect(a).to eq(@cls[1,2,1,2,3,3]);
|
180
|
+
|
181
|
+
expect{@cls[0][-2] = 1}.to raise_error(IndexError)
|
182
|
+
expect{@cls[0][-2,0] = nil}.to raise_error(IndexError)
|
183
|
+
expect{@cls[0][0,0,0] = 0}.to raise_error(ArgumentError)
|
184
|
+
expect{@cls[0].freeze[0,0,0] = 0}.to raise_error(ArgumentError)
|
185
|
+
expect{@cls[0][:foo] = 0}.to raise_error(TypeError)
|
186
|
+
expect{@cls[0].freeze[:foo] = 0}.to raise_error(RuntimeError)
|
166
187
|
end
|
167
188
|
|
168
189
|
it "at" do
|
169
|
-
list =
|
190
|
+
list = @cls.new
|
170
191
|
expect(list.at(0)).to eq(nil)
|
171
192
|
list.push 1,2,3
|
172
193
|
expect(list.at(0)).to eq(1)
|
@@ -176,91 +197,97 @@ describe List do
|
|
176
197
|
end
|
177
198
|
|
178
199
|
it "fetch" do
|
179
|
-
list =
|
200
|
+
list = @cls[11,22,33,44]
|
180
201
|
expect(list.fetch(1)).to eq(22)
|
181
202
|
expect(list.fetch(-1)).to eq(44)
|
182
203
|
expect(list.fetch(4, 'cat')).to eq('cat')
|
183
204
|
expect(list.fetch(100){|i| "#{i} is out of bounds" }).to eq('100 is out of bounds')
|
184
|
-
|
185
|
-
expect{
|
205
|
+
|
206
|
+
expect{@cls[0,1].fetch(100)}.to raise_error(IndexError)
|
207
|
+
expect{@cls[0,1].fetch(-100)}.to raise_error(IndexError)
|
208
|
+
expect{@cls[0,1].fetch("100")}.to raise_error(TypeError)
|
186
209
|
end
|
187
210
|
|
188
211
|
it "first" do
|
189
|
-
list =
|
212
|
+
list = @cls.new
|
190
213
|
expect(list.first).to eq(nil)
|
191
214
|
list.push 1,2,3
|
192
215
|
expect(list.first).to eq(1)
|
193
216
|
expect(list.first).to eq(1)
|
194
|
-
expect(list.first(2)).to eq(
|
195
|
-
expect(list.first(10)).to eq(
|
217
|
+
expect(list.first(2)).to eq(@cls[1,2])
|
218
|
+
expect(list.first(10)).to eq(@cls[1,2,3])
|
196
219
|
expect{list.first("1")}.to raise_error(TypeError)
|
197
220
|
expect{list.first(-1)}.to raise_error(ArgumentError)
|
198
221
|
end
|
199
222
|
|
200
223
|
it "last" do
|
201
|
-
list =
|
224
|
+
list = @cls.new
|
202
225
|
expect(list.last).to eq(nil)
|
203
226
|
list.push 1,2,3
|
204
227
|
expect(list.last).to eq(3)
|
205
228
|
expect(list.last).to eq(3)
|
206
|
-
expect(list.last(2)).to eq(
|
207
|
-
expect(list.last(10)).to eq(
|
229
|
+
expect(list.last(2)).to eq(@cls[2,3])
|
230
|
+
expect(list.last(10)).to eq(@cls[1,2,3])
|
208
231
|
expect{list.last("1")}.to raise_error(TypeError)
|
209
232
|
expect{list.last(-1)}.to raise_error(ArgumentError)
|
210
233
|
end
|
211
234
|
|
212
235
|
it "concat" do
|
213
|
-
list =
|
214
|
-
list2 =
|
215
|
-
expect(list.concat(list2)).to eq(
|
216
|
-
expect(list.concat([4,5,6])).to eq(
|
217
|
-
expect(list.concat(
|
236
|
+
list = @cls.new
|
237
|
+
list2 = @cls[1,2,3]
|
238
|
+
expect(list.concat(list2)).to eq(@cls[1,2,3])
|
239
|
+
expect(list.concat([4,5,6])).to eq(@cls[1,2,3,4,5,6])
|
240
|
+
expect(list.concat(@subcls[7,8,9])).to eq(@cls[1,2,3,4,5,6,7,8,9])
|
218
241
|
end
|
219
242
|
|
220
243
|
it "push" do
|
221
|
-
list =
|
244
|
+
list = @cls.new
|
222
245
|
expect(list.push).to eq(list)
|
223
246
|
10.times { |i|
|
224
247
|
list << i
|
225
248
|
}
|
226
|
-
expect(list).to eq(
|
227
|
-
list.push(*(10...20)
|
228
|
-
expect(list).to eq(
|
249
|
+
expect(list).to eq(@cls[*0...10])
|
250
|
+
list.push(*(10...20))
|
251
|
+
expect(list).to eq(@cls[*0...20])
|
229
252
|
end
|
230
253
|
|
231
254
|
it "pop" do
|
232
|
-
list =
|
255
|
+
list = @cls.new
|
233
256
|
expect(list.pop).to eq(nil)
|
234
257
|
list.push 1,2,3
|
235
|
-
expect(list.pop(2)
|
258
|
+
expect(list.pop(2)).to eq(@cls[2,3])
|
236
259
|
expect(list.pop).to eq(1)
|
237
260
|
expect{list.pop("1")}.to raise_error(TypeError)
|
238
261
|
end
|
239
262
|
|
240
263
|
it "shift" do
|
241
|
-
list =
|
264
|
+
list = @cls.new
|
242
265
|
expect(list.shift).to eq(nil)
|
243
266
|
list.push 1,2,3
|
244
|
-
expect(list.shift(2)
|
267
|
+
expect(list.shift(2)).to eq(@cls[1,2])
|
245
268
|
expect(list.shift).to eq(3)
|
246
269
|
expect{list.shift("1")}.to raise_error(TypeError)
|
247
270
|
end
|
248
271
|
|
249
272
|
it "unshift" do
|
250
|
-
list =
|
273
|
+
list = @cls.new
|
251
274
|
expect(list.unshift).to eq(list)
|
252
|
-
expect(list.unshift(3)
|
253
|
-
expect(list.unshift(2
|
275
|
+
expect(list.unshift(3)).to eq(@cls[3])
|
276
|
+
expect(list.unshift(1,2)).to eq(@cls[1,2,3])
|
277
|
+
expect(list.unshift(@cls["foo","bar"])).to eq(@cls[@cls["foo","bar"],1,2,3])
|
278
|
+
|
279
|
+
expect{@cls[].freeze.unshift('cat')}.to raise_error(RuntimeError)
|
280
|
+
expect{@cls[].freeze.unshift()}.to raise_error(RuntimeError)
|
254
281
|
end
|
255
282
|
|
256
283
|
it "insert" do
|
257
|
-
list =
|
258
|
-
expect(list.insert(2, 99)).to eq(
|
284
|
+
list = @cls['a','b','c','d']
|
285
|
+
expect(list.insert(2, 99)).to eq(@cls["a","b",99,"c","d"]);
|
259
286
|
expect{list.insert("a", 1)}.to raise_error(TypeError)
|
260
287
|
end
|
261
288
|
|
262
289
|
it "each" do
|
263
|
-
list =
|
290
|
+
list = @cls.new
|
264
291
|
expect(list.each).to be_a_kind_of(Enumerator)
|
265
292
|
expect(list.each{}).to eq(list)
|
266
293
|
|
@@ -278,7 +305,7 @@ describe List do
|
|
278
305
|
end
|
279
306
|
|
280
307
|
it "each_index" do
|
281
|
-
list =
|
308
|
+
list = @cls.new
|
282
309
|
expect(list.each_index).to be_a_kind_of(Enumerator)
|
283
310
|
expect(list.each_index{}).to eq(list)
|
284
311
|
|
@@ -296,7 +323,7 @@ describe List do
|
|
296
323
|
end
|
297
324
|
|
298
325
|
it "reverse_each" do
|
299
|
-
list =
|
326
|
+
list = @cls[1,2,3]
|
300
327
|
result = []
|
301
328
|
list.reverse_each do |i|
|
302
329
|
result << i
|
@@ -305,7 +332,7 @@ describe List do
|
|
305
332
|
end
|
306
333
|
|
307
334
|
it "length" do
|
308
|
-
list =
|
335
|
+
list = @cls.new
|
309
336
|
expect(list.length).to eq(0)
|
310
337
|
list.push 1,2,3
|
311
338
|
expect(list.length).to eq(3)
|
@@ -313,14 +340,14 @@ describe List do
|
|
313
340
|
end
|
314
341
|
|
315
342
|
it "empty?" do
|
316
|
-
list =
|
343
|
+
list = @cls.new
|
317
344
|
expect(list.empty?).to eq(true)
|
318
345
|
list.push 1,2,3
|
319
346
|
expect(list.empty?).to eq(false)
|
320
347
|
end
|
321
348
|
|
322
349
|
it "find_index" do
|
323
|
-
list =
|
350
|
+
list = @cls.new
|
324
351
|
expect(list.find_index(1)).to eq(nil)
|
325
352
|
list.push 1,2,3
|
326
353
|
expect(list.find_index(1)).to eq(0)
|
@@ -329,267 +356,300 @@ describe List do
|
|
329
356
|
end
|
330
357
|
|
331
358
|
it "replace" do
|
332
|
-
list =
|
333
|
-
expect(list.replace(
|
334
|
-
expect(list.replace([7,8,9])).to eq(
|
359
|
+
list = @cls[1,2,3,4,5]
|
360
|
+
expect(list.replace(@cls[4,5,6])).to eq(@cls[4,5,6])
|
361
|
+
expect(list.replace([7,8,9])).to eq(@cls[7,8,9])
|
335
362
|
expect{list.replace "1"}.to raise_error(TypeError)
|
336
363
|
end
|
337
364
|
|
338
365
|
it "clear" do
|
339
|
-
list =
|
340
|
-
expect(list.clear).to eq(
|
341
|
-
expect(list.clear).to eq(
|
366
|
+
list = @cls[1,2,3]
|
367
|
+
expect(list.clear).to eq(@cls[])
|
368
|
+
expect(list.clear).to eq(@cls[])
|
342
369
|
end
|
343
370
|
|
344
371
|
it "rindex" do
|
345
|
-
list =
|
372
|
+
list = @cls[1,2,2,2,3]
|
346
373
|
expect(list.rindex(2)).to eq(3)
|
347
374
|
expect(list.rindex(-10)).to eq(nil)
|
348
375
|
expect(list.rindex { |x| x == 2 }).to eq(3)
|
376
|
+
|
377
|
+
list = @cls[0,1]
|
378
|
+
e = list.rindex
|
379
|
+
expect(e.next).to eq(1)
|
380
|
+
list.clear
|
381
|
+
expect{e.next}.to raise_error(StopIteration)
|
382
|
+
|
383
|
+
obj = Object.new
|
384
|
+
class << obj; self; end.class_eval do
|
385
|
+
define_method(:==) {|x| list.clear; false}
|
386
|
+
end
|
387
|
+
list = @cls[nil,obj]
|
388
|
+
expect(list.rindex(0)).to eq(nil)
|
349
389
|
end
|
350
390
|
|
351
391
|
it "join" do
|
352
|
-
expect(
|
353
|
-
expect(
|
354
|
-
expect(
|
355
|
-
expect(
|
356
|
-
expect(
|
357
|
-
expect
|
392
|
+
expect(@cls.new.join).to eq("")
|
393
|
+
expect(@cls[1,2,3].join).to eq("123")
|
394
|
+
expect(@cls["a","b","c"].join).to eq("abc")
|
395
|
+
expect(@cls["a","b","c"].join("-")).to eq("a-b-c")
|
396
|
+
expect(@cls["a",@cls["b",@cls["c"]]].join("-")).to eq("a-b-c")
|
397
|
+
expect(@cls["a",["b",@cls[["c"]]]].join("-")).to eq("a-b-c")
|
398
|
+
expect{@cls["a","b","c"].join(1)}.to raise_error(TypeError)
|
358
399
|
orig_sep = $,
|
359
400
|
$, = "*"
|
360
|
-
expect(
|
401
|
+
expect(@cls["a","b","c"].join).to eq("a*b*c")
|
361
402
|
$, = orig_sep
|
403
|
+
|
404
|
+
def (a = Object.new).to_list
|
405
|
+
List[self]
|
406
|
+
end
|
407
|
+
expect{@cls[a].join}.to raise_error(ArgumentError)
|
362
408
|
end
|
363
409
|
|
364
410
|
it "reverse" do
|
365
|
-
list =
|
366
|
-
expect(list.reverse).to eq(
|
411
|
+
list = @cls.new
|
412
|
+
expect(list.reverse).to eq(@cls.new)
|
367
413
|
list.push 1,2,3
|
368
|
-
expect(list.reverse).to eq(
|
369
|
-
expect(list).to eq(
|
414
|
+
expect(list.reverse).to eq(@cls[3,2,1])
|
415
|
+
expect(list).to eq(@cls[1,2,3])
|
370
416
|
end
|
371
417
|
|
372
418
|
it "reverse!" do
|
373
|
-
list =
|
374
|
-
expect(list.reverse!).to eq(
|
419
|
+
list = @cls.new
|
420
|
+
expect(list.reverse!).to eq(@cls.new)
|
375
421
|
list.push 1,2,3
|
376
|
-
expect(list.reverse!).to eq(
|
377
|
-
expect(list).to eq(
|
422
|
+
expect(list.reverse!).to eq(@cls[3,2,1])
|
423
|
+
expect(list).to eq(@cls[3,2,1])
|
378
424
|
end
|
379
425
|
|
380
426
|
it "rotate" do
|
381
|
-
list =
|
382
|
-
expect(list.rotate).to eq(
|
427
|
+
list = @cls.new
|
428
|
+
expect(list.rotate).to eq(@cls.new)
|
383
429
|
list.push 1,2,3
|
384
|
-
expect(list.rotate).to eq(
|
385
|
-
expect(list.rotate(2)).to eq(
|
386
|
-
expect(list.rotate(-2)).to eq(
|
387
|
-
expect(list).to eq(
|
430
|
+
expect(list.rotate).to eq(@cls[2,3,1])
|
431
|
+
expect(list.rotate(2)).to eq(@cls[3,1,2])
|
432
|
+
expect(list.rotate(-2)).to eq(@cls[2,3,1])
|
433
|
+
expect(list).to eq(@cls[1,2,3])
|
388
434
|
expect{list.rotate("a")}.to raise_error(TypeError)
|
389
435
|
expect{list.rotate(1,2)}.to raise_error(ArgumentError)
|
390
436
|
end
|
391
437
|
|
392
438
|
it "rotate!" do
|
393
|
-
list =
|
394
|
-
expect(list.rotate!).to eq(
|
439
|
+
list = @cls.new
|
440
|
+
expect(list.rotate!).to eq(@cls.new)
|
395
441
|
list.push 1,2,3
|
396
|
-
expect(list.rotate!).to eq(
|
397
|
-
expect(list.rotate!(2)).to eq(
|
398
|
-
expect(list.rotate!(-2)).to eq(
|
399
|
-
expect(list).to eq(
|
442
|
+
expect(list.rotate!).to eq(@cls[2,3,1])
|
443
|
+
expect(list.rotate!(2)).to eq(@cls[1,2,3])
|
444
|
+
expect(list.rotate!(-2)).to eq(@cls[2,3,1])
|
445
|
+
expect(list).to eq(@cls[2,3,1])
|
400
446
|
expect{list.rotate!("a")}.to raise_error(TypeError)
|
401
447
|
expect{list.rotate!(1,2)}.to raise_error(ArgumentError)
|
402
448
|
end
|
403
449
|
|
404
450
|
it "sort" do
|
405
|
-
list =
|
406
|
-
expect(list.sort).to eq(
|
451
|
+
list = @cls.new
|
452
|
+
expect(list.sort).to eq(@cls.new)
|
407
453
|
list.push *[4,1,3,5,2]
|
408
|
-
expect(list.sort).to eq(
|
409
|
-
expect(list.sort{|a,b| b - a}).to eq(
|
410
|
-
expect(list).to eq(
|
454
|
+
expect(list.sort).to eq(@cls[1,2,3,4,5])
|
455
|
+
expect(list.sort{|a,b| b - a}).to eq(@cls[5,4,3,2,1])
|
456
|
+
expect(list).to eq(@cls[4,1,3,5,2])
|
411
457
|
end
|
412
458
|
|
413
459
|
it "sort!" do
|
414
|
-
list =
|
415
|
-
expect(list.sort!).to eq(
|
460
|
+
list = @cls.new
|
461
|
+
expect(list.sort!).to eq(@cls.new)
|
416
462
|
list.push *[4,1,3,5,2]
|
417
|
-
expect(list.sort!).to eq(
|
418
|
-
expect(list).to eq(
|
419
|
-
expect(list.sort!{|a,b| b - a}).to eq(
|
420
|
-
expect(list).to eq(
|
463
|
+
expect(list.sort!).to eq(@cls[1,2,3,4,5])
|
464
|
+
expect(list).to eq(@cls[1,2,3,4,5])
|
465
|
+
expect(list.sort!{|a,b| b - a}).to eq(@cls[5,4,3,2,1])
|
466
|
+
expect(list).to eq(@cls[5,4,3,2,1])
|
421
467
|
end
|
422
468
|
|
423
469
|
it "sort_by" do
|
424
|
-
list =
|
425
|
-
expect(list.sort_by{|a| a}).to eq(
|
470
|
+
list = @cls.new
|
471
|
+
expect(list.sort_by{|a| a}).to eq(@cls.new)
|
426
472
|
list.push *[4,1,3,5,2]
|
427
|
-
expect(list.sort_by{|a| a}).to eq(
|
428
|
-
expect(list.sort_by{|a| -a}).to eq(
|
429
|
-
expect(list).to eq(
|
473
|
+
expect(list.sort_by{|a| a}).to eq(@cls[1,2,3,4,5])
|
474
|
+
expect(list.sort_by{|a| -a}).to eq(@cls[5,4,3,2,1])
|
475
|
+
expect(list).to eq(@cls[4,1,3,5,2])
|
430
476
|
end
|
431
477
|
|
432
478
|
it "sort_by!" do
|
433
|
-
list =
|
434
|
-
expect(list.sort_by!{|a| a}).to eq(
|
479
|
+
list = @cls.new
|
480
|
+
expect(list.sort_by!{|a| a}).to eq(@cls.new)
|
435
481
|
list.push *[4,1,3,5,2]
|
436
|
-
expect(list.sort_by!{|a| a}).to eq(
|
437
|
-
expect(list.sort_by!{|a| -a}).to eq(
|
438
|
-
expect(list).to eq(
|
482
|
+
expect(list.sort_by!{|a| a}).to eq(@cls[1,2,3,4,5])
|
483
|
+
expect(list.sort_by!{|a| -a}).to eq(@cls[5,4,3,2,1])
|
484
|
+
expect(list).to eq(@cls[5,4,3,2,1])
|
439
485
|
end
|
440
486
|
|
441
487
|
it "collect" do
|
442
|
-
list =
|
443
|
-
expect(list.collect{|a| a}).to eq(
|
488
|
+
list = @cls.new
|
489
|
+
expect(list.collect{|a| a}).to eq(@cls.new)
|
444
490
|
list.push *[4,1,3,5,2]
|
445
|
-
expect(list.collect{|a| a * a}).to eq(
|
446
|
-
expect(list.map.each{|a| -a}).to eq(
|
447
|
-
expect(list).to eq(
|
491
|
+
expect(list.collect{|a| a * a}).to eq(@cls[16,1,9,25,4])
|
492
|
+
expect(list.map.each{|a| -a}).to eq(@cls[-4,-1,-3,-5,-2])
|
493
|
+
expect(list).to eq(@cls[4,1,3,5,2])
|
448
494
|
end
|
449
495
|
|
450
496
|
it "collect!" do
|
451
|
-
list =
|
452
|
-
expect(list.collect!{|a| a}).to eq(
|
497
|
+
list = @cls.new
|
498
|
+
expect(list.collect!{|a| a}).to eq(@cls.new)
|
453
499
|
list.push *[4,1,3,5,2]
|
454
|
-
expect(list.collect!{|a| a * a}).to eq(
|
455
|
-
expect(list.map!.each{|a| -a}).to eq(
|
500
|
+
expect(list.collect!{|a| a * a}).to eq(@cls[16,1,9,25,4])
|
501
|
+
expect(list.map!.each{|a| -a}).to eq(@cls[-16,-1,-9,-25,-4])
|
456
502
|
end
|
457
503
|
|
458
504
|
it "select" do
|
459
|
-
list =
|
460
|
-
expect(list.select{|i| i}).to eq(
|
505
|
+
list = @cls.new
|
506
|
+
expect(list.select{|i| i}).to eq(@cls.new)
|
461
507
|
list.push *[4,1,3,5,2]
|
462
|
-
expect(list.select{|i| i.even?}).to eq(
|
463
|
-
expect(list.select.each{|i| i.odd?}).to eq(
|
464
|
-
expect(list).to eq(
|
508
|
+
expect(list.select{|i| i.even?}).to eq(@cls[4,2])
|
509
|
+
expect(list.select.each{|i| i.odd?}).to eq(@cls[1,3,5])
|
510
|
+
expect(list).to eq(@cls[4,1,3,5,2])
|
465
511
|
end
|
466
512
|
|
467
513
|
it "select!" do
|
468
|
-
list =
|
514
|
+
list = @cls.new
|
469
515
|
expect(list.select!{|i| i}).to eq(nil)
|
470
516
|
list.push *[4,1,3,5,2]
|
471
|
-
expect(list.select!{|i| i.even?}).to eq(
|
472
|
-
expect(list.select!.each{|i| i % 4 == 0}).to eq(
|
517
|
+
expect(list.select!{|i| i.even?}).to eq(@cls[4,2])
|
518
|
+
expect(list.select!.each{|i| i % 4 == 0}).to eq(@cls[4])
|
473
519
|
expect(list.select!.each{|i| i % 4 == 0}).to eq(nil)
|
474
|
-
expect(list).to eq(
|
520
|
+
expect(list).to eq(@cls[4])
|
475
521
|
end
|
476
522
|
|
477
523
|
it "keep_if" do
|
478
|
-
list =
|
524
|
+
list = @cls.new
|
479
525
|
expect(list.keep_if{|i| i}).to eq(list)
|
480
526
|
list.push *[4,1,3,5,2]
|
481
|
-
expect(list.keep_if{|i| i.even?}).to eq(
|
482
|
-
expect(list.keep_if.each{|i| i % 4 == 0}).to eq(
|
527
|
+
expect(list.keep_if{|i| i.even?}).to eq(@cls[4,2])
|
528
|
+
expect(list.keep_if.each{|i| i % 4 == 0}).to eq(@cls[4])
|
483
529
|
expect(list.keep_if.each{|i| i % 4 == 0}).to eq(list)
|
484
|
-
expect(list).to eq(
|
530
|
+
expect(list).to eq(@cls[4])
|
485
531
|
end
|
486
532
|
|
487
533
|
it "values_at" do
|
488
|
-
expect(
|
489
|
-
list =
|
490
|
-
expect(list.values_at).to eq(
|
491
|
-
expect(list.values_at 1).to eq(
|
492
|
-
expect(list.values_at 1,3,10).to eq(
|
493
|
-
expect(list.values_at -1,-2).to eq(
|
494
|
-
expect(list.values_at 1..5).to eq(
|
495
|
-
expect(list.values_at -2..-1).to eq(
|
496
|
-
expect(list.values_at -1..-2).to eq(
|
497
|
-
expect(list.values_at 0,1..5).to eq(
|
498
|
-
expect(list.values_at 10..12).to eq(
|
534
|
+
expect(@cls.new.values_at(1)).to eq(@cls[nil])
|
535
|
+
list = @cls[4,1,3,5,2]
|
536
|
+
expect(list.values_at).to eq(@cls.new)
|
537
|
+
expect(list.values_at 1).to eq(@cls[1])
|
538
|
+
expect(list.values_at 1,3,10).to eq(@cls[1,5,nil])
|
539
|
+
expect(list.values_at -1,-2).to eq(@cls[2,5])
|
540
|
+
expect(list.values_at 1..5).to eq(@cls[1,3,5,2,nil])
|
541
|
+
expect(list.values_at -2..-1).to eq(@cls[5,2])
|
542
|
+
expect(list.values_at -1..-2).to eq(@cls.new)
|
543
|
+
expect(list.values_at 0,1..5).to eq(@cls[4,1,3,5,2,nil])
|
544
|
+
expect(list.values_at 10..12).to eq(@cls[nil,nil,nil])
|
499
545
|
expect{list.values_at "a"}.to raise_error(TypeError)
|
500
546
|
end
|
501
547
|
|
502
548
|
it "delete" do
|
503
|
-
expect(
|
504
|
-
list =
|
549
|
+
expect(@cls.new.delete(nil)).to eq(nil)
|
550
|
+
list = @cls[4,1,3,2,5,5]
|
505
551
|
expect(list.delete(1)).to eq(1)
|
506
|
-
expect(list).to eq(
|
552
|
+
expect(list).to eq(@cls[4,3,2,5,5])
|
507
553
|
expect(list.delete(5){"not found"}).to eq(5)
|
508
|
-
expect(list).to eq(
|
554
|
+
expect(list).to eq(@cls[4,3,2])
|
509
555
|
expect(list.delete(4)).to eq(4)
|
510
556
|
expect(list.delete(1){"not found"}).to eq("not found")
|
511
557
|
|
512
|
-
list =
|
558
|
+
list = @cls.new(1024,0)
|
513
559
|
expect(list.delete(0)).to eq(0)
|
514
|
-
expect(list).to eq(
|
560
|
+
expect(list).to eq(@cls.new)
|
515
561
|
end
|
516
562
|
|
517
563
|
it "delete_at" do
|
518
|
-
list =
|
564
|
+
list = @cls[1,2,3]
|
519
565
|
expect(list.delete_at(1)).to eq(2)
|
520
|
-
expect(list).to eq(
|
566
|
+
expect(list).to eq(@cls[1,3])
|
521
567
|
expect(list.delete_at(-1)).to eq(3)
|
522
|
-
expect(list).to eq(
|
568
|
+
expect(list).to eq(@cls[1])
|
523
569
|
expect(list.delete_at(0)).to eq(1)
|
524
|
-
expect(list).to eq(
|
570
|
+
expect(list).to eq(@cls[])
|
525
571
|
expect(list.delete_at(0)).to eq(nil)
|
526
572
|
expect{list.delete_at("a")}.to raise_error(TypeError)
|
527
573
|
end
|
528
574
|
|
529
575
|
it "delete_if" do
|
530
|
-
list =
|
531
|
-
expect(list.delete_if{|i| 3 < i}).to eq(
|
532
|
-
expect(list.delete_if.each{|i| i.even?}).to eq(
|
533
|
-
expect(list.delete_if{|i| 0 < i}).to eq(
|
534
|
-
expect(list.delete_if{|i| 0 < i}).to eq(
|
535
|
-
expect(list).to eq(
|
576
|
+
list = @cls[1,2,3,4,5]
|
577
|
+
expect(list.delete_if{|i| 3 < i}).to eq(@cls[1,2,3])
|
578
|
+
expect(list.delete_if.each{|i| i.even?}).to eq(@cls[1,3])
|
579
|
+
expect(list.delete_if{|i| 0 < i}).to eq(@cls[])
|
580
|
+
expect(list.delete_if{|i| 0 < i}).to eq(@cls[])
|
581
|
+
expect(list).to eq(@cls[])
|
536
582
|
end
|
537
583
|
|
538
584
|
it "reject" do
|
539
|
-
list =
|
540
|
-
expect(list.reject{|i| 3 < i}).to eq(
|
541
|
-
expect(list.reject.each{|i| i.even?}).to eq(
|
542
|
-
expect(list.reject{|i| 0 < i}).to eq(
|
543
|
-
expect(list).to eq(
|
585
|
+
list = @cls[1,2,3,4,5]
|
586
|
+
expect(list.reject{|i| 3 < i}).to eq(@cls[1,2,3])
|
587
|
+
expect(list.reject.each{|i| i.even?}).to eq(@cls[1,3,5])
|
588
|
+
expect(list.reject{|i| 0 < i}).to eq(@cls[])
|
589
|
+
expect(list).to eq(@cls[1,2,3,4,5])
|
544
590
|
end
|
545
591
|
|
546
592
|
it "reject!" do
|
547
|
-
list =
|
593
|
+
list = @cls[1,2,3,4,5]
|
548
594
|
expect(list.reject!{|i| i % 10 == 0}).to eq(nil)
|
549
|
-
expect(list.reject!{|i| 3 < i}).to eq(
|
550
|
-
expect(list.reject!.each{|i| i.even?}).to eq(
|
551
|
-
expect(list.reject!{|i| 0 < i}).to eq(
|
595
|
+
expect(list.reject!{|i| 3 < i}).to eq(@cls[1,2,3])
|
596
|
+
expect(list.reject!.each{|i| i.even?}).to eq(@cls[1,3])
|
597
|
+
expect(list.reject!{|i| 0 < i}).to eq(@cls[])
|
552
598
|
expect(list.reject!{|i| 0 < i}).to eq(nil)
|
553
|
-
expect(list).to eq(
|
599
|
+
expect(list).to eq(@cls[])
|
554
600
|
end
|
555
601
|
|
556
602
|
it "zip" do
|
557
|
-
a =
|
558
|
-
b =
|
603
|
+
a = @cls[4,5,6]
|
604
|
+
b = @cls[7,8,9]
|
559
605
|
ary = []
|
560
|
-
expect(a.zip(b)).to eq(
|
561
|
-
expect(
|
562
|
-
expect(
|
563
|
-
expect(a.zip(
|
564
|
-
expect(a.zip([1,2],[8])).to eq(
|
606
|
+
expect(a.zip(b)).to eq(@cls[[4,7],[5,8],[6,9]])
|
607
|
+
expect(@cls[1,2,3].zip(a,b)).to eq(@cls[[1,4,7],[2,5,8],[3,6,9]])
|
608
|
+
expect(@cls[1,2].zip(a,b)).to eq(@cls[[1,4,7],[2,5,8]])
|
609
|
+
expect(a.zip(@cls[1,2],@cls[8])).to eq(@cls[[4,1,8],[5,2,nil],[6,nil,nil]])
|
610
|
+
expect(a.zip([1,2],[8])).to eq(@cls[[4,1,8],[5,2,nil],[6,nil,nil]])
|
565
611
|
expect(a.zip([1,2],[8]){|i| ary << i}).to eq(nil)
|
566
|
-
expect(ary).to eq([
|
612
|
+
expect(ary).to eq([[4,1,8],[5,2,nil],[6,nil,nil]])
|
567
613
|
expect{a.zip("a")}.to raise_error # <= 1.9.3 NoMethodError, >= 2.0.0 TypeError
|
614
|
+
|
615
|
+
obj = Object.new
|
616
|
+
def obj.to_a; [1,2]; end
|
617
|
+
expect{@cls[*%w{a b}].zip(obj)}.to raise_error # <= 1.9.3 NoNameError, >= 2.0.0 TypeError
|
618
|
+
|
619
|
+
def obj.each; [3,4].each{|e| yield e}; end
|
620
|
+
expect(@cls[*%w{a b}].zip(obj)).to eq(@cls[['a',3],['b',4]])
|
621
|
+
|
622
|
+
def obj.to_ary; [5,6]; end
|
623
|
+
expect(@cls[*%w{a b}].zip(obj)).to eq(@cls[['a',5],['b',6]])
|
624
|
+
|
625
|
+
r = 1..1
|
626
|
+
def r.respond_to?(*); super; end
|
627
|
+
expect(@cls[42].zip(r)).to eq(@cls[[42,1]])
|
568
628
|
end
|
569
629
|
|
570
630
|
it "transpose" do
|
571
|
-
expect(
|
572
|
-
expect(
|
573
|
-
expect(
|
574
|
-
expect{
|
575
|
-
expect{
|
631
|
+
expect(@cls[].transpose).to eq(@cls[])
|
632
|
+
expect(@cls[[1,2],@cls[3,4],@cls[5,6]].transpose).to eq(@cls[[1,3,5],[2,4,6]])
|
633
|
+
expect(@cls[[1,2],[3,4],[5,6]].transpose).to eq(@cls[[1,3,5],[2,4,6]])
|
634
|
+
expect{@cls[[1],[2,3]].transpose}.to raise_error(IndexError)
|
635
|
+
expect{@cls[1].transpose}.to raise_error(TypeError)
|
576
636
|
end
|
577
637
|
|
578
638
|
it "fill" do
|
579
|
-
list =
|
580
|
-
expect(list.fill("x")).to eq(
|
581
|
-
expect(list.fill("z",2,2)).to eq(
|
582
|
-
expect(list.fill("y",0..1)).to eq(
|
583
|
-
expect(list.fill{"foo"}).to eq(
|
639
|
+
list = @cls["a","b","c","d"]
|
640
|
+
expect(list.fill("x")).to eq(@cls["x","x","x","x"])
|
641
|
+
expect(list.fill("z",2,2)).to eq(@cls["x","x","z","z"])
|
642
|
+
expect(list.fill("y",0..1)).to eq(@cls["y","y","z","z"])
|
643
|
+
expect(list.fill{"foo"}).to eq(@cls["foo","foo","foo","foo"])
|
584
644
|
expect(list[0].object_id).to_not eq(list[1].object_id)
|
585
|
-
expect(list.fill{|i| i*i}).to eq(
|
586
|
-
expect(list.fill(-2){|i| i*i*i}).to eq(
|
587
|
-
expect(list.fill("z",2)).to eq(
|
645
|
+
expect(list.fill{|i| i*i}).to eq(@cls[0,1,4,9])
|
646
|
+
expect(list.fill(-2){|i| i*i*i}).to eq(@cls[0,1,8,27])
|
647
|
+
expect(list.fill("z",2)).to eq(@cls[0,1,"z","z"])
|
588
648
|
expect{list.fill("z","a")}.to raise_error(TypeError)
|
589
649
|
end
|
590
650
|
|
591
651
|
it "include?" do
|
592
|
-
list =
|
652
|
+
list = @cls.new
|
593
653
|
expect(list.include?(1)).to eq(false)
|
594
654
|
list.push 1,2,3
|
595
655
|
expect(list.include?(1)).to eq(true)
|
@@ -597,28 +657,28 @@ describe List do
|
|
597
657
|
end
|
598
658
|
|
599
659
|
it "<=>" do
|
600
|
-
expect(
|
601
|
-
expect(
|
602
|
-
expect(
|
603
|
-
expect(
|
660
|
+
expect(@cls["a","a","c"] <=> @cls["a","b","c"]).to eq(-1)
|
661
|
+
expect(@cls[1,2,3,4,5,6] <=> @cls[1,2]).to eq(+1)
|
662
|
+
expect(@cls[1,2] <=> @cls[1,2]).to eq(0)
|
663
|
+
expect(@cls[1,2] <=> @cls[1,:two]).to eq(nil)
|
604
664
|
end
|
605
665
|
|
606
666
|
it "slice" do
|
607
|
-
list =
|
667
|
+
list = @cls.new
|
608
668
|
expect(list.slice(0)).to eq(nil)
|
609
669
|
list.push 1,2,3,4,5
|
610
670
|
expect(list.slice(0)).to eq(1)
|
611
|
-
expect(list.slice(1,3)).to eq(
|
612
|
-
expect(list.slice(1..3)).to eq(
|
671
|
+
expect(list.slice(1,3)).to eq(@cls[2,3,4])
|
672
|
+
expect(list.slice(1..3)).to eq(@cls[2,3,4])
|
613
673
|
end
|
614
674
|
|
615
675
|
it "slice!" do
|
616
|
-
list =
|
676
|
+
list = @cls.new
|
617
677
|
expect(list.slice!(0)).to eq(nil)
|
618
678
|
list.push 1,2,3,4,5
|
619
679
|
expect(list.slice!(2)).to eq(3)
|
620
|
-
expect(list.slice!(2,3)).to eq(
|
621
|
-
expect(list.slice!(0..1)).to eq(
|
680
|
+
expect(list.slice!(2,3)).to eq(@cls[4,5])
|
681
|
+
expect(list.slice!(0..1)).to eq(@cls[1,2])
|
622
682
|
list.push 1,2,3,4,5
|
623
683
|
expect(list.slice!(-1)).to eq(5)
|
624
684
|
expect(list.slice!(100..110)).to eq(nil)
|
@@ -628,13 +688,500 @@ describe List do
|
|
628
688
|
expect{list.slice!("a")}.to raise_error(TypeError)
|
629
689
|
expect{list.slice!(1,"a")}.to raise_error(TypeError)
|
630
690
|
expect{list.slice!(1,2,3)}.to raise_error(ArgumentError)
|
631
|
-
expect(list).to eq(
|
691
|
+
expect(list).to eq(@cls[1,2,3,4])
|
692
|
+
end
|
693
|
+
|
694
|
+
it "assoc" do
|
695
|
+
expect(@cls[].assoc "foo").to eq(nil)
|
696
|
+
list = @cls[@cls["foo",1,2],["bar",1,2],[1,"baz"]]
|
697
|
+
expect(list.assoc "foo").to eq(@cls["foo",1,2])
|
698
|
+
expect(list.assoc "bar").to eq(@cls["bar",1,2])
|
699
|
+
expect(list.assoc "baz").to eq(nil)
|
700
|
+
end
|
701
|
+
|
702
|
+
it "rassoc" do
|
703
|
+
expect(@cls[].rassoc "foo").to eq(nil)
|
704
|
+
list = @cls[@cls["foo",1,2],["bar",1,2],[1,"baz"]]
|
705
|
+
expect(list.rassoc "foo").to eq(nil)
|
706
|
+
expect(list.rassoc 1).to eq(@cls["foo",1,2])
|
707
|
+
expect(list.rassoc "baz").to eq(@cls[1,"baz"])
|
708
|
+
end
|
709
|
+
|
710
|
+
it "+" do
|
711
|
+
expect(@cls[] + @cls[]).to eq(@cls[])
|
712
|
+
expect(@cls[1] + @cls[]).to eq(@cls[1])
|
713
|
+
expect(@cls[] + @cls[1]).to eq(@cls[1])
|
714
|
+
expect(@cls[1] + @cls[1]).to eq(@cls[1,1])
|
715
|
+
expect(@cls[1] + @subcls[1]).to eq(@cls[1,1])
|
716
|
+
expect(@subcls[1] + @cls[1]).to eq(@subcls[1,1])
|
717
|
+
expect(@cls[1,2,3] + @cls["a","b","c"]).to eq(@cls[1,2,3,"a","b","c"])
|
718
|
+
expect{@cls[] + 1}.to raise_error(TypeError)
|
719
|
+
end
|
720
|
+
|
721
|
+
it "*" do
|
722
|
+
expect(@cls[] * 3).to eq(@cls[])
|
723
|
+
expect(@cls[1] * 3).to eq(@cls[1,1,1])
|
724
|
+
expect(@cls[1,2] * 3).to eq(@cls[1,2,1,2,1,2])
|
725
|
+
expect(@cls[1,2,3] * 0).to eq(@cls[])
|
726
|
+
expect{@cls[1,2,3] * (-3)}.to raise_error(ArgumentError)
|
727
|
+
|
728
|
+
expect(@cls[1,2,3] * "-").to eq("1-2-3")
|
729
|
+
expect(@cls[1,2,3] * "").to eq("123")
|
730
|
+
end
|
731
|
+
|
732
|
+
it "-" do
|
733
|
+
expect(@cls[1] - @cls[1]).to eq(@cls[])
|
734
|
+
expect(@cls[1,2,3,4,5] - @cls[2,3,4,5]).to eq(@cls[1])
|
735
|
+
expect(@cls[1,2,1,3,1,4,1,5] - @cls[2,3,4,5]).to eq(@cls[1,1,1,1])
|
736
|
+
list = @cls[]
|
737
|
+
1000.times { list << 1 }
|
738
|
+
expect(list - @cls[2]).to eq(@cls[1] * 1000)
|
739
|
+
expect(@cls[1,2,1] - @cls[2]).to eq(@cls[1,1])
|
740
|
+
expect(@cls[1,2,3] - @cls[4,5,6]).to eq(@cls[1,2,3])
|
741
|
+
end
|
742
|
+
|
743
|
+
it "&" do
|
744
|
+
expect(@cls[1,1,3,5] & @cls[1,2,3]).to eq(@cls[1,3])
|
745
|
+
expect(@cls[1,1,3,5] & @cls[]).to eq(@cls[])
|
746
|
+
expect(@cls[] & @cls[1,2,3]).to eq(@cls[])
|
747
|
+
expect(@cls[1,2,3] & @cls[4,5,6]).to eq(@cls[])
|
748
|
+
end
|
749
|
+
|
750
|
+
it "|" do
|
751
|
+
expect(@cls[ ] | @cls[ ]).to eq(@cls[ ])
|
752
|
+
expect(@cls[ ] | @cls[1]).to eq(@cls[1])
|
753
|
+
expect(@cls[1] | @cls[ ]).to eq(@cls[1])
|
754
|
+
expect(@cls[1] | @cls[1]).to eq(@cls[1])
|
755
|
+
|
756
|
+
expect(@cls[1] | @cls[2]).to eq(@cls[1,2])
|
757
|
+
expect(@cls[1,1] | @cls[2,2]).to eq(@cls[1,2])
|
758
|
+
expect(@cls[1,2] | @cls[1,2]).to eq(@cls[1,2])
|
759
|
+
|
760
|
+
a = @cls[*%w{a b c}]
|
761
|
+
b = @cls[*%w{a b c d e}]
|
762
|
+
c = a | b
|
763
|
+
expect(b).to eq(c)
|
764
|
+
expect(b.eql?(c)).to eq false
|
765
|
+
end
|
766
|
+
|
767
|
+
it "uniq" do
|
768
|
+
a = @cls[]
|
769
|
+
b = a.uniq
|
770
|
+
expect(a).to eq(@cls[])
|
771
|
+
expect(b).to eq(@cls[])
|
772
|
+
expect(a.eql?(b)).to eq false
|
773
|
+
|
774
|
+
a = @cls[1]
|
775
|
+
b = a.uniq
|
776
|
+
expect(a).to eq(@cls[1])
|
777
|
+
expect(b).to eq(@cls[1])
|
778
|
+
expect(a.eql?(b)).to eq false
|
779
|
+
|
780
|
+
a = @cls[1,1]
|
781
|
+
b = a.uniq
|
782
|
+
expect(a).to eq(@cls[1,1])
|
783
|
+
expect(b).to eq(@cls[1])
|
784
|
+
expect(a.eql?(b)).to eq false
|
785
|
+
|
786
|
+
a = @cls[1,2]
|
787
|
+
b = a.uniq
|
788
|
+
expect(a).to eq(@cls[1,2])
|
789
|
+
expect(b).to eq(@cls[1,2])
|
790
|
+
expect(a.eql?(b)).to eq false
|
791
|
+
|
792
|
+
a = @cls[1,2,3,2,1,2,3,4,nil]
|
793
|
+
b = a.dup
|
794
|
+
expect(a.uniq).to eq(@cls[1,2,3,4,nil])
|
795
|
+
expect(a).to eq(b)
|
796
|
+
|
797
|
+
c = @cls["a:def","a:xyz","b:abc","b:xyz","c:jkl"]
|
798
|
+
d = c.dup
|
799
|
+
expect(c.uniq{|s| s[/^\w+/]}).to eq(@cls["a:def","b:abc","c:jkl"])
|
800
|
+
expect(c).to eq(d)
|
801
|
+
|
802
|
+
a = @cls[*%w{a a}]
|
803
|
+
b = a.uniq
|
804
|
+
expect(a).to eq(@cls[*%w{a a}])
|
805
|
+
expect(a.none?(&:frozen?)).to eq true
|
806
|
+
expect(b).to eq(@cls[*%w{a}])
|
807
|
+
expect(b.none?(&:frozen?)).to eq true
|
808
|
+
|
809
|
+
b = "abc"
|
810
|
+
list = @cls[b,b.dup,b.dup]
|
811
|
+
expect(list.uniq.size).to eq(1)
|
812
|
+
expect(list.uniq[0]).to eq(b)
|
813
|
+
|
814
|
+
a = @cls[]
|
815
|
+
b = a.uniq {|v| v.even? }
|
816
|
+
expect(a).to eq(@cls[])
|
817
|
+
expect(b).to eq(@cls[])
|
818
|
+
expect(a.eql?(b)).to eq false
|
819
|
+
|
820
|
+
a = @cls[1]
|
821
|
+
b = a.uniq {|v| v.even? }
|
822
|
+
expect(a).to eq(@cls[1])
|
823
|
+
expect(b).to eq(@cls[1])
|
824
|
+
expect(a.eql?(b)).to eq false
|
825
|
+
|
826
|
+
a = @cls[1,3]
|
827
|
+
b = a.uniq {|v| v.even? }
|
828
|
+
expect(a).to eq(@cls[1,3])
|
829
|
+
expect(b).to eq(@cls[1])
|
830
|
+
expect(a.eql?(b)).to eq false
|
831
|
+
|
832
|
+
a = @cls[*%w{a a}]
|
833
|
+
b = a.uniq {|v| v }
|
834
|
+
expect(a).to eq(@cls[*%w{a a}])
|
835
|
+
expect(a.none?(&:frozen?)).to eq true
|
836
|
+
expect(b).to eq(@cls[*%w{a}])
|
837
|
+
expect(b.none?(&:frozen?)).to eq true
|
838
|
+
end
|
839
|
+
|
840
|
+
it "uniq!" do
|
841
|
+
a = @cls[]
|
842
|
+
b = a.uniq!
|
843
|
+
expect(b).to eq(nil)
|
844
|
+
|
845
|
+
a = @cls[1]
|
846
|
+
b = a.uniq!
|
847
|
+
expect(b).to eq(nil)
|
848
|
+
|
849
|
+
a = @cls[1,1]
|
850
|
+
b = a.uniq!
|
851
|
+
expect(a).to eq(@cls[1])
|
852
|
+
expect(b).to eq(@cls[1])
|
853
|
+
expect(b).to eq(a)
|
854
|
+
|
855
|
+
a = @cls[1,2]
|
856
|
+
b = a.uniq!
|
857
|
+
expect(a).to eq(@cls[1,2])
|
858
|
+
expect(b).to eq(nil)
|
859
|
+
|
860
|
+
a = @cls[1,2,3,2,1,2,3,4,nil]
|
861
|
+
expect(a.uniq!).to eq(@cls[1,2,3,4,nil])
|
862
|
+
expect(a).to eq(@cls[1,2,3,4,nil])
|
863
|
+
|
864
|
+
c = @cls["a:def","a:xyz","b:abc","b:xyz","c:jkl"]
|
865
|
+
d = c.dup
|
866
|
+
expect(c.uniq!{|s| s[/^\w+/]}).to eq(@cls["a:def","b:abc","c:jkl"])
|
867
|
+
expect(c).to eq(@cls["a:def","b:abc","c:jkl"])
|
868
|
+
|
869
|
+
c = @cls["a:def","b:abc","c:jkl"]
|
870
|
+
expect(c.uniq!{|s| s[/^\w+/]}).to eq(nil)
|
871
|
+
expect(c).to eq(@cls["a:def","b:abc","c:jkl"])
|
872
|
+
|
873
|
+
expect(@cls[1,2,3].uniq!).to eq(nil)
|
874
|
+
|
875
|
+
f = a.dup.freeze
|
876
|
+
expect{a.uniq!(1)}.to raise_error(ArgumentError)
|
877
|
+
expect{f.uniq!(1)}.to raise_error(ArgumentError)
|
878
|
+
expect{f.uniq!}.to raise_error(RuntimeError)
|
879
|
+
|
880
|
+
expect{
|
881
|
+
a = @cls[ {c: "b"}, {c: "r"}, {c: "w"}, {c: "g"}, {c: "g"} ]
|
882
|
+
a.sort_by!{|e| e[:c]}
|
883
|
+
a.uniq! {|e| e[:c]}
|
884
|
+
}.to_not raise_error
|
885
|
+
|
886
|
+
a = @cls[]
|
887
|
+
b = a.uniq! {|v| v.even? }
|
888
|
+
expect(b).to eq(nil)
|
889
|
+
|
890
|
+
a = @cls[1]
|
891
|
+
b = a.uniq! {|v| v.even? }
|
892
|
+
expect(b).to eq(nil)
|
893
|
+
|
894
|
+
a = @cls[1,3]
|
895
|
+
b = a.uniq! {|v| v.even? }
|
896
|
+
expect(a).to eq(@cls[1])
|
897
|
+
expect(b).to eq(@cls[1])
|
898
|
+
expect(a.eql?(b)).to eq true
|
899
|
+
|
900
|
+
a = @cls[*%w{a a}]
|
901
|
+
b = a.uniq! {|v| v }
|
902
|
+
expect(b).to eq(@cls[*%w{a}])
|
903
|
+
expect(a.eql?(b)).to eq true
|
904
|
+
expect(b.none?(&:frozen?)).to eq true
|
905
|
+
|
906
|
+
list = @cls[1,2]
|
907
|
+
orig = list.dup
|
908
|
+
expect{list.uniq! {|v| list.freeze; 1}}.to raise_error(RuntimeError)
|
909
|
+
expect(list).to eq(orig)
|
910
|
+
end
|
911
|
+
|
912
|
+
it "compact" do
|
913
|
+
expect(@cls[1,nil,nil,2,3,nil,4].compact).to eq(@cls[1,2,3,4])
|
914
|
+
expect(@subcls[1,nil,nil,2,3,nil,4].compact).to eq(@subcls[1,2,3,4])
|
915
|
+
expect(@cls[nil,1,nil,2,3,nil,4].compact).to eq(@cls[1,2,3,4])
|
916
|
+
expect(@cls[1,nil,nil,2,3,nil,4,nil].compact).to eq(@cls[1,2,3,4])
|
917
|
+
expect(@cls[1,2,3,4].compact).to eq(@cls[1,2,3,4])
|
918
|
+
end
|
919
|
+
|
920
|
+
it "compact!" do
|
921
|
+
a = @cls[1,nil,nil,2,3,nil,4]
|
922
|
+
expect(a.compact!).to eq(@cls[1,2,3,4])
|
923
|
+
expect(a).to eq(@cls[1,2,3,4])
|
924
|
+
|
925
|
+
a = @cls[nil,1,nil,2,3,nil,4]
|
926
|
+
expect(a.compact!).to eq(@cls[1,2,3,4])
|
927
|
+
expect(a).to eq(@cls[1,2,3,4])
|
928
|
+
|
929
|
+
a = @cls[1,nil,nil,2,3,nil,4,nil]
|
930
|
+
expect(a.compact!).to eq(@cls[1,2,3,4])
|
931
|
+
expect(a).to eq(@cls[1,2,3,4])
|
932
|
+
|
933
|
+
a = @cls[1,2,3,4]
|
934
|
+
expect(a.compact!).to eq(nil)
|
935
|
+
expect(a).to eq(@cls[1,2,3,4])
|
936
|
+
end
|
937
|
+
|
938
|
+
it "flatten" do
|
939
|
+
a1 = @cls[1,2,3]
|
940
|
+
a2 = @cls[5,6]
|
941
|
+
a3 = @cls[4,a2]
|
942
|
+
a4 = @cls[a1,a3]
|
943
|
+
expect(a4.flatten).to eq(@cls[1,2,3,4,5,6])
|
944
|
+
expect(a4.flatten(1)).to eq(@cls[1,2,3,4,@cls[5,6]])
|
945
|
+
expect(a4).to eq(@cls[a1,a3])
|
946
|
+
|
947
|
+
a5 = @cls[a1,@cls[],a3]
|
948
|
+
expect(a5.flatten).to eq(@cls[1,2,3,4,5,6])
|
949
|
+
expect(@cls[].flatten).to eq(@cls[])
|
950
|
+
expect(@cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten).to eq(@cls[])
|
951
|
+
|
952
|
+
expect{@cls[@cls[]].flatten("")}.to raise_error(TypeError)
|
953
|
+
|
954
|
+
a6 = @cls[@cls[1,2],3]
|
955
|
+
a6.taint
|
956
|
+
a7 = a6.flatten
|
957
|
+
expect(a7.tainted?).to eq true
|
958
|
+
|
959
|
+
a8 = @cls[@cls[1,2],3]
|
960
|
+
a9 = a8.flatten(0)
|
961
|
+
expect(a9).to eq(a8)
|
962
|
+
expect(a9.eql?(a8)).to eq false
|
963
|
+
|
964
|
+
f = [].freeze
|
965
|
+
expect{f.flatten!(1,2)}.to raise_error(ArgumentError)
|
966
|
+
expect{f.flatten!}.to raise_error(RuntimeError)
|
967
|
+
expect{f.flatten!(:foo)}.to raise_error(RuntimeError)
|
968
|
+
end
|
969
|
+
|
970
|
+
it "flatten!" do
|
971
|
+
a1 = @cls[1,2,3]
|
972
|
+
a2 = @cls[5,6]
|
973
|
+
a3 = @cls[4,a2]
|
974
|
+
a4 = @cls[a1,a3]
|
975
|
+
expect(a4.flatten!).to eq(@cls[1,2,3,4,5,6])
|
976
|
+
expect(a4).to eq(@cls[1,2,3,4,5,6])
|
977
|
+
|
978
|
+
a5 = @cls[a1,@cls[],a3]
|
979
|
+
expect(a5.flatten!).to eq(@cls[1,2,3,4,5,6])
|
980
|
+
expect(a5.flatten!(0)).to eq(nil)
|
981
|
+
expect(a5).to eq(@cls[1,2,3,4,5,6])
|
982
|
+
|
983
|
+
expect(@cls[].flatten!).to eq(nil)
|
984
|
+
expect(@cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten!).to eq(@cls[])
|
985
|
+
|
986
|
+
expect(@cls[].flatten!(0)).to eq(nil)
|
987
|
+
end
|
988
|
+
|
989
|
+
it "count" do
|
990
|
+
a = @cls[1,2,3,1,2]
|
991
|
+
expect(a.count).to eq(5)
|
992
|
+
expect(a.count(1)).to eq(2)
|
993
|
+
expect(a.count {|x| x % 2 == 1}).to eq(3)
|
994
|
+
expect(a.count(1) {|x| x % 2 == 1}).to eq(2)
|
995
|
+
expect{a.count(0,1)}.to raise_error(ArgumentError)
|
996
|
+
end
|
997
|
+
|
998
|
+
it "shuffle" do
|
999
|
+
100.times do
|
1000
|
+
expect(@cls[2,1,0].shuffle.sort).to eq(@cls[0,1,2])
|
1001
|
+
end
|
1002
|
+
|
1003
|
+
gen = Random.new(0)
|
1004
|
+
expect{@cls[1,2,3].shuffle(1, random: gen)}.to raise_error(ArgumentError)
|
1005
|
+
srand(0)
|
1006
|
+
100.times do
|
1007
|
+
expect(@cls[0,1,2].shuffle(random: gen)).to eq(@cls[0,1,2].shuffle)
|
1008
|
+
end
|
1009
|
+
|
1010
|
+
# FIXME v1.9.3 and v2.0.0 nothing raise
|
1011
|
+
# expect{@cls[0,1,2].shuffle(xawqij: "a")}.to raise_error(ArgumentError)
|
1012
|
+
# expect{@cls[0,1,2].shuffle!(xawqij: "a")}.to raise_error(ArgumentError)
|
1013
|
+
|
1014
|
+
gen = proc do
|
1015
|
+
10000000
|
1016
|
+
end
|
1017
|
+
class << gen
|
1018
|
+
alias rand call
|
1019
|
+
end
|
1020
|
+
expect{@cls[*0..2].shuffle(random: gen)}.to raise_error(RangeError)
|
1021
|
+
|
1022
|
+
# FIXME
|
1023
|
+
# list = @cls[*(0...10000)]
|
1024
|
+
# gen = proc do
|
1025
|
+
# list.replace(@cls[])
|
1026
|
+
# 0.5
|
1027
|
+
# end
|
1028
|
+
# class << gen
|
1029
|
+
# alias rand call
|
1030
|
+
# end
|
1031
|
+
# expect{list.shuffle!(random: gen)}.to raise_error(RuntimeError)
|
1032
|
+
|
1033
|
+
# zero = Object.new
|
1034
|
+
# def zero.to_int
|
1035
|
+
# 0
|
1036
|
+
# end
|
1037
|
+
# gen_to_int = proc do |max|
|
1038
|
+
# zero
|
1039
|
+
# end
|
1040
|
+
# class << gen_to_int
|
1041
|
+
# alias rand call
|
1042
|
+
# end
|
1043
|
+
# list = @cls[*(0...10000)]
|
1044
|
+
# expect(list.shuffle(random: gen_to_int)).to eq(list.rotate)
|
1045
|
+
end
|
1046
|
+
|
1047
|
+
it "sample" do
|
1048
|
+
100.times do
|
1049
|
+
s = [2,1,0].sample
|
1050
|
+
expect(s).to be_a_kind_of(Fixnum)
|
1051
|
+
expect(s).to be >= 0
|
1052
|
+
expect(s).to be <= 2
|
1053
|
+
@cls[2,1,0].sample(2).each {|sample|
|
1054
|
+
expect(sample).to be_a_kind_of(Fixnum)
|
1055
|
+
expect(sample).to be >= 0
|
1056
|
+
expect(sample).to be <= 2
|
1057
|
+
}
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
srand(0)
|
1061
|
+
a = @cls[*1..18]
|
1062
|
+
(0..20).each do |n|
|
1063
|
+
100.times do
|
1064
|
+
b = a.sample(n)
|
1065
|
+
expect(b.size).to eq([n,18].min)
|
1066
|
+
expect((a | b).sort).to eq(a)
|
1067
|
+
expect((a & b).sort).to eq(b.sort)
|
1068
|
+
end
|
1069
|
+
|
1070
|
+
h = Hash.new(0)
|
1071
|
+
1000.times do
|
1072
|
+
a.sample(n).each {|x| h[x] += 1 }
|
1073
|
+
end
|
1074
|
+
expect(h.values.max).to be <= (h.values.min * 2) if n != 0
|
1075
|
+
end
|
1076
|
+
|
1077
|
+
expect{[1,2].sample(-1)}.to raise_error(ArgumentError)
|
1078
|
+
|
1079
|
+
gen = Random.new(0)
|
1080
|
+
srand(0)
|
1081
|
+
a = @cls[*(1..18)]
|
1082
|
+
(0..20).each do |n|
|
1083
|
+
100.times do |i|
|
1084
|
+
expect(a.sample(n, random: gen)).to eq(a.sample(n))
|
1085
|
+
end
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
# FIXME v1.9.3 and v2.0.0 nothing raise
|
1089
|
+
# expect{@cls[0,1,2].sample(xawqij: "a")}.to raise_error(ArgumentError)
|
1090
|
+
end
|
1091
|
+
|
1092
|
+
it "cycle" do
|
1093
|
+
a = @cls[]
|
1094
|
+
@cls[0,1,2].cycle do |i|
|
1095
|
+
a << i
|
1096
|
+
break if a.size == 10
|
1097
|
+
end
|
1098
|
+
expect(a).to eq(@cls[0,1,2,0,1,2,0,1,2,0])
|
1099
|
+
|
1100
|
+
a = @cls[0,1,2]
|
1101
|
+
expect(a.cycle {a.clear}).to eq(nil)
|
1102
|
+
|
1103
|
+
a = @cls[]
|
1104
|
+
@cls[0,1,2].cycle(3) {|i| a << i}
|
1105
|
+
expect(a).to eq(@cls[0,1,2,0,1,2,0,1,2])
|
1106
|
+
|
1107
|
+
expect(@cls[0,1,2].cycle(3).to_a.size).to eq(9)
|
1108
|
+
|
1109
|
+
expect(@cls[0,1,2].cycle(-1){}).to eq(nil)
|
1110
|
+
expect{@cls[0,1,2].cycle("a"){}}.to raise_error(TypeError)
|
1111
|
+
end
|
1112
|
+
|
1113
|
+
it "permutation" do
|
1114
|
+
a = @cls[1,2,3]
|
1115
|
+
expect(a.permutation(0).to_list).to eq(@cls[[]])
|
1116
|
+
expect(a.permutation(1).to_list.sort).to eq(@cls[[1],[2],[3]])
|
1117
|
+
expect(a.permutation(2).to_list.sort).to eq(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]])
|
1118
|
+
end
|
1119
|
+
|
1120
|
+
it "compatible" do
|
1121
|
+
expect(@cls[1,2,3,4].combination(0).to_list).to eq(@cls[[]])
|
1122
|
+
expect(@cls[1,2,3,4].combination(1).to_list).to eq(@cls[[1],[2],[3],[4]])
|
1123
|
+
expect(@cls[1,2,3,4].combination(2).to_list).to eq(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]])
|
1124
|
+
end
|
1125
|
+
|
1126
|
+
it "repeated_combination" do
|
1127
|
+
a = @cls[1,2,3]
|
1128
|
+
expect(a.repeated_combination(0).to_list).to eq(@cls[[]])
|
1129
|
+
expect(a.repeated_combination(1).to_list.sort).to eq(@cls[[1],[2],[3]])
|
1130
|
+
expect(a.repeated_combination(2).to_list.sort).to eq(@cls[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]])
|
1131
|
+
end
|
1132
|
+
|
1133
|
+
it "repeated_permutation" do
|
1134
|
+
a = @cls[1,2]
|
1135
|
+
expect(a.repeated_permutation(0).to_list).to eq(@cls[[]])
|
1136
|
+
expect(a.repeated_permutation(1).to_list.sort).to eq(@cls[[1],[2]])
|
1137
|
+
expect(a.repeated_permutation(2).to_list.sort).to eq(@cls[[1,1],[1,2],[2,1],[2,2]])
|
1138
|
+
end
|
1139
|
+
|
1140
|
+
it "product" do
|
1141
|
+
expect(@cls[1,2,3].product([4,5])).to eq(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]])
|
1142
|
+
expect(@cls[1,2].product([1,2])).to eq(@cls[[1,1],[1,2],[2,1],[2,2]])
|
1143
|
+
expect(@subcls[1,2].product([1,2])).to eq(@subcls[[1,1],[1,2],[2,1],[2,2]])
|
1144
|
+
end
|
1145
|
+
|
1146
|
+
it "take" do
|
1147
|
+
expect(@cls[1,2,3,4,5,0].take(3)).to eq(@cls[1,2,3])
|
1148
|
+
expect(@subcls[1,2,3,4,5,0].take(3)).to eq(@subcls[1,2,3])
|
1149
|
+
expect{@cls[1,2].take(-1)}.to raise_error(ArgumentError)
|
1150
|
+
expect(@cls[1,2].take(1000000000)).to eq(@cls[1,2])
|
1151
|
+
end
|
1152
|
+
|
1153
|
+
it "take_while" do
|
1154
|
+
expect(@cls[1,2,3,4,5,0].take_while {|i| i < 3}).to eq(@cls[1,2])
|
1155
|
+
end
|
1156
|
+
|
1157
|
+
it "drop" do
|
1158
|
+
expect(@cls[1,2,3,4,5,0].drop(3)).to eq(@cls[4,5,0])
|
1159
|
+
expect{@cls[1,2].drop(-1)}.to raise_error(ArgumentError)
|
1160
|
+
expect(@cls[1,2].drop(1000000000)).to eq(@cls[])
|
1161
|
+
end
|
1162
|
+
|
1163
|
+
it "drop_while" do
|
1164
|
+
expect(@cls[1,2,3,4,5,0].drop_while {|i| i < 3 }).to eq(@cls[3,4,5,0])
|
1165
|
+
expect(@subcls[1,2,3,4,5,0].drop_while {|i| i < 3 }).to eq(@subcls[3,4,5,0])
|
1166
|
+
end
|
1167
|
+
|
1168
|
+
it "bsearch" do
|
1169
|
+
expect{@cls[1, 2, 42, 100, 666].bsearch{ "not ok" }}.to raise_error(TypeError)
|
1170
|
+
expect(@cls[1, 2, 42, 100, 666].bsearch{false}).to eq(@cls[1, 2, 42, 100, 666].bsearch{})
|
1171
|
+
enum = @cls[1, 2, 42, 100, 666].bsearch
|
1172
|
+
if enum.respond_to? :size # v1.9.3 not respond to :size
|
1173
|
+
expect(enum.size).to eq nil
|
1174
|
+
end
|
1175
|
+
expect(enum.each{|x| x >= 33}).to eq(42)
|
1176
|
+
|
1177
|
+
a = @cls[0,4,7,10,12]
|
1178
|
+
expect(a.bsearch {|x| 4 - x / 2 }).to eq nil
|
632
1179
|
end
|
633
1180
|
|
634
1181
|
it "ring" do
|
635
1182
|
len = 0
|
636
1183
|
result = []
|
637
|
-
list =
|
1184
|
+
list = @cls[1,2,3]
|
638
1185
|
ring = list.ring
|
639
1186
|
ring.each do |i|
|
640
1187
|
break if len == 1000
|
@@ -649,7 +1196,7 @@ describe List do
|
|
649
1196
|
it "ring!" do
|
650
1197
|
len = 0
|
651
1198
|
result = []
|
652
|
-
list =
|
1199
|
+
list = @cls[1,2,3]
|
653
1200
|
ring = list.ring!
|
654
1201
|
ring.each do |i|
|
655
1202
|
break if len == 1000
|
@@ -662,7 +1209,7 @@ describe List do
|
|
662
1209
|
end
|
663
1210
|
|
664
1211
|
it "ring?" do
|
665
|
-
list =
|
1212
|
+
list = @cls[1,2,3]
|
666
1213
|
expect(list.ring?).to eq(false)
|
667
1214
|
expect(list.ring.ring?).to eq(true)
|
668
1215
|
expect(list.ring!.ring?).to eq(true)
|