list 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +30 -0
- data/ext/list/extconf.rb +5 -0
- data/ext/list/list.c +2084 -0
- data/full_spec.sh +16 -0
- data/list.gemspec +25 -0
- data/spec/bench.rb +40 -0
- data/spec/list_spec.rb +670 -0
- data/spec/mem_spec.rb +11 -0
- data/spec/spec_helper.rb +5 -0
- metadata +118 -0
data/full_spec.sh
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# this script need installed *rbenv* and installed all ruby version
|
4
|
+
|
5
|
+
VERSIONS=(1.9.3-p484 2.0.0-p247 2.1.0)
|
6
|
+
ORIG=`rbenv version-name`
|
7
|
+
|
8
|
+
for version in ${VERSIONS[@]}
|
9
|
+
do
|
10
|
+
rbenv global ${version}
|
11
|
+
rbenv rehash
|
12
|
+
rake clean
|
13
|
+
bundle exec rake
|
14
|
+
done
|
15
|
+
rbenv global ${ORIG} && rbenv rehash
|
16
|
+
|
data/list.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "list"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.author = "ksss"
|
9
|
+
spec.email = "co000ri@gmail.com"
|
10
|
+
spec.summary = %q{List in Ruby}
|
11
|
+
spec.description = %q{List in Ruby}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
spec.extensions = ["ext/list/extconf.rb"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", ['~> 2.11']
|
24
|
+
spec.add_development_dependency "rake-compiler", ["~> 0.8.3"]
|
25
|
+
end
|
data/spec/bench.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'list'
|
7
|
+
require 'benchmark'
|
8
|
+
|
9
|
+
Benchmark.bm(32) do |x|
|
10
|
+
[1000,10000,100000].each do |n|
|
11
|
+
x.report("#{n}times") do
|
12
|
+
n.times do
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
[Array, List].each do |obj|
|
18
|
+
[1000,10000,100000].each do |n|
|
19
|
+
x.report("#{obj}#new #{n}times") do
|
20
|
+
n.times do
|
21
|
+
obj.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
[[:push, 1], [:unshift, 1], [:pop], [:shift], [:insert, 0, 1], [:delete_at, 0]].each do |args|
|
28
|
+
m = args.shift
|
29
|
+
[(0..100000).to_a, (0..100000).to_list].each do |obj|
|
30
|
+
[1000,10000,100000].each do |n|
|
31
|
+
o = obj.dup
|
32
|
+
x.report("#{o.class}##{m} #{n}times") do
|
33
|
+
n.times do
|
34
|
+
o.send(m, *args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/list_spec.rb
ADDED
@@ -0,0 +1,670 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe List do
|
4
|
+
before :each do
|
5
|
+
GC.start
|
6
|
+
end
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
GC.start
|
10
|
+
end
|
11
|
+
|
12
|
+
it "class" do
|
13
|
+
expect(List).to be_include(Enumerable)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "create" do
|
17
|
+
expect(List[]).to eq(List.new)
|
18
|
+
expect(List[1,2,3]).to eq(List.new [1,2,3])
|
19
|
+
expect(L[1,2,3]).to eq(L.new [1,2,3])
|
20
|
+
end
|
21
|
+
|
22
|
+
it "try_convert" do
|
23
|
+
expect(List.try_convert List[1,2,3]).to eq(List[1,2,3])
|
24
|
+
expect(List.try_convert [1,2,3]).to eq(List[1,2,3])
|
25
|
+
expect(List.try_convert "[1,2,3]").to eq(nil)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "to_list" do
|
29
|
+
expect([].to_list).to eq(List.new)
|
30
|
+
expect([1,[2],3].to_list).to eq(List[1,[2],3])
|
31
|
+
expect((0..5).to_list).to eq(List[0,1,2,3,4,5])
|
32
|
+
expect(List[1,2,3].to_list).to eq(List[1,2,3])
|
33
|
+
expect(L[1,2,3].to_list).to eq(L[1,2,3])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "initialize" do
|
37
|
+
expect(List.new).to be_a_kind_of(List)
|
38
|
+
expect(List.new([])).to be_a_kind_of(List)
|
39
|
+
expect(List.new([1,2,3])).to be_a_kind_of(List)
|
40
|
+
expect(List.new([1,2,3])).to eq([1,2,3].to_list)
|
41
|
+
expect(List.new(3)).to eq([nil,nil,nil].to_list)
|
42
|
+
expect(List.new(3, 0)).to eq([0,0,0].to_list)
|
43
|
+
expect(List.new(3){|i| "foo"}).to eq(["foo", "foo", "foo"].to_list)
|
44
|
+
expect{List.new(3,0,0)}.to raise_error(ArgumentError)
|
45
|
+
expect{List.new("a")}.to raise_error(TypeError)
|
46
|
+
expect{List.new("a",0)}.to raise_error(TypeError)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "dup and replace" do
|
50
|
+
list = List.new
|
51
|
+
expect(list.dup).to eq(list)
|
52
|
+
list = List[1,2,3]
|
53
|
+
expect(list.dup).to eq(list)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "inspect" do
|
57
|
+
list = List.new
|
58
|
+
expect(list.inspect).to eq("#<List: []>")
|
59
|
+
list.push 1,[2],{:a=>3}
|
60
|
+
expect(list.inspect).to eq("#<List: [1, [2], {:a=>3}]>")
|
61
|
+
expect(L[1,2,3].inspect).to eq("#<L: [1, 2, 3]>")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "to_a" do
|
65
|
+
list = List.new
|
66
|
+
expect(list.to_a).to eq([])
|
67
|
+
a = (0..10).to_a
|
68
|
+
list = List.new(a)
|
69
|
+
expect(list.to_a).to eq(a)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "freeze and frozen?" do
|
73
|
+
list = List.new
|
74
|
+
list.freeze
|
75
|
+
expect(list).to eq(List[])
|
76
|
+
expect(list.frozen?).to eq(true)
|
77
|
+
expect{ list.push 1 }.to raise_error
|
78
|
+
end
|
79
|
+
|
80
|
+
it "==" do
|
81
|
+
list = List.new
|
82
|
+
expect(list).to eq(List.new)
|
83
|
+
expect(list).to eq(list)
|
84
|
+
list.push(1)
|
85
|
+
expect(list).to_not eq(List.new)
|
86
|
+
expect(list).to eq(List[1])
|
87
|
+
expect(list).to_not eq([1])
|
88
|
+
expect(L[1]).to eq(L[1])
|
89
|
+
expect(L[1]).to eq(List[1])
|
90
|
+
end
|
91
|
+
|
92
|
+
it "eql?" do
|
93
|
+
list = List.new
|
94
|
+
expect(list.eql?(list)).to be true
|
95
|
+
expect(list.eql?(List.new)).to be false
|
96
|
+
expect(list.eql?(L.new)).to be false
|
97
|
+
end
|
98
|
+
|
99
|
+
it "hash" do
|
100
|
+
expect(List.new.hash).to eq(List.new.hash)
|
101
|
+
list1 = List[1,2,3]
|
102
|
+
list2 = List[1,2,3]
|
103
|
+
expect(list1.hash).to eq(list2.hash)
|
104
|
+
hash = {}
|
105
|
+
hash[list1] = 1
|
106
|
+
expect(hash[list1]).to eq(1)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "[]" do
|
110
|
+
list = List.new
|
111
|
+
expect(list[0]).to eq(nil)
|
112
|
+
list.push 1,2,3
|
113
|
+
expect(list[2]).to eq(3)
|
114
|
+
expect(list[-1]).to eq(3)
|
115
|
+
expect(list[10]).to eq(nil)
|
116
|
+
expect(list[1,1]).to eq(List[2])
|
117
|
+
expect(list[1,2]).to eq(List[2,3])
|
118
|
+
expect(list[1,10]).to eq(List[2,3])
|
119
|
+
|
120
|
+
# special cases
|
121
|
+
expect(list[3]).to eq(nil)
|
122
|
+
expect(list[10,0]).to eq(nil)
|
123
|
+
expect(list[3,0]).to eq(List[])
|
124
|
+
expect(list[3..10]).to eq(List[])
|
125
|
+
end
|
126
|
+
|
127
|
+
it "[]=" do
|
128
|
+
list = List.new
|
129
|
+
list[0] = 1
|
130
|
+
expect(list).to eq(List[1])
|
131
|
+
list[1] = 2
|
132
|
+
expect(list).to eq(List[1,2])
|
133
|
+
list[4] = 4
|
134
|
+
expect(list).to eq(List[1,2,nil,nil,4])
|
135
|
+
list[0,3] = List['a','b','c']
|
136
|
+
expect(list).to eq(List['a','b','c',nil,4])
|
137
|
+
list[0,3] = List['a','b','c']
|
138
|
+
expect(list).to eq(List['a','b','c',nil,4])
|
139
|
+
list[1..2] = List[1,2]
|
140
|
+
expect(list).to eq(List['a',1,2,nil,4])
|
141
|
+
list[0,2] = "?"
|
142
|
+
expect(list).to eq(List["?",2,nil,4])
|
143
|
+
list[0..2] = "A"
|
144
|
+
expect(list).to eq(List["A",4])
|
145
|
+
list[-1] = "Z"
|
146
|
+
expect(list).to eq(List["A","Z"])
|
147
|
+
list[1..-1] = nil
|
148
|
+
expect(list).to eq(List["A",nil])
|
149
|
+
list[1..-1] = List[]
|
150
|
+
expect(list).to eq(List["A"])
|
151
|
+
list[0,0] = List[1,2]
|
152
|
+
expect(list).to eq(List[1,2,"A"])
|
153
|
+
list[3,0] = "B"
|
154
|
+
expect(list).to eq(List[1,2,"A","B"])
|
155
|
+
list[6,0] = "C"
|
156
|
+
expect(list).to eq(List[1,2,"A","B",nil,nil,"C"])
|
157
|
+
expect{list["10"]}.to raise_error(TypeError)
|
158
|
+
|
159
|
+
a = List[1,2,3]
|
160
|
+
a[1,0] = a
|
161
|
+
expect(a).to eq(List[1,1,2,3,2,3]);
|
162
|
+
|
163
|
+
a = List[1,2,3]
|
164
|
+
a[-1,0] = a
|
165
|
+
expect(a).to eq(List[1,2,1,2,3,3]);
|
166
|
+
end
|
167
|
+
|
168
|
+
it "at" do
|
169
|
+
list = List.new
|
170
|
+
expect(list.at(0)).to eq(nil)
|
171
|
+
list.push 1,2,3
|
172
|
+
expect(list.at(0)).to eq(1)
|
173
|
+
expect(list.at(2)).to eq(3)
|
174
|
+
expect(list.at(3)).to eq(nil)
|
175
|
+
expect{list.at("10")}.to raise_error(TypeError)
|
176
|
+
end
|
177
|
+
|
178
|
+
it "fetch" do
|
179
|
+
list = List[11,22,33,44]
|
180
|
+
expect(list.fetch(1)).to eq(22)
|
181
|
+
expect(list.fetch(-1)).to eq(44)
|
182
|
+
expect(list.fetch(4, 'cat')).to eq('cat')
|
183
|
+
expect(list.fetch(100){|i| "#{i} is out of bounds" }).to eq('100 is out of bounds')
|
184
|
+
expect{list.fetch(100)}.to raise_error(IndexError)
|
185
|
+
expect{list.fetch("100")}.to raise_error(TypeError)
|
186
|
+
end
|
187
|
+
|
188
|
+
it "first" do
|
189
|
+
list = List.new
|
190
|
+
expect(list.first).to eq(nil)
|
191
|
+
list.push 1,2,3
|
192
|
+
expect(list.first).to eq(1)
|
193
|
+
expect(list.first).to eq(1)
|
194
|
+
expect(list.first(2)).to eq(List[1,2])
|
195
|
+
expect(list.first(10)).to eq(List[1,2,3])
|
196
|
+
expect{list.first("1")}.to raise_error(TypeError)
|
197
|
+
expect{list.first(-1)}.to raise_error(ArgumentError)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "last" do
|
201
|
+
list = List.new
|
202
|
+
expect(list.last).to eq(nil)
|
203
|
+
list.push 1,2,3
|
204
|
+
expect(list.last).to eq(3)
|
205
|
+
expect(list.last).to eq(3)
|
206
|
+
expect(list.last(2)).to eq(List[2,3])
|
207
|
+
expect(list.last(10)).to eq(List[1,2,3])
|
208
|
+
expect{list.last("1")}.to raise_error(TypeError)
|
209
|
+
expect{list.last(-1)}.to raise_error(ArgumentError)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "concat" do
|
213
|
+
list = List.new
|
214
|
+
list2 = List[1,2,3]
|
215
|
+
expect(list.concat(list2)).to eq(List[1,2,3])
|
216
|
+
expect(list.concat([4,5,6])).to eq(List[1,2,3,4,5,6])
|
217
|
+
expect(list.concat(L[7,8,9])).to eq(List[1,2,3,4,5,6,7,8,9])
|
218
|
+
end
|
219
|
+
|
220
|
+
it "push" do
|
221
|
+
list = List.new
|
222
|
+
expect(list.push).to eq(list)
|
223
|
+
10.times { |i|
|
224
|
+
list << i
|
225
|
+
}
|
226
|
+
expect(list).to eq((0...10).to_list)
|
227
|
+
list.push(*(10...20).to_a)
|
228
|
+
expect(list).to eq((0...20).to_list)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "pop" do
|
232
|
+
list = List.new
|
233
|
+
expect(list.pop).to eq(nil)
|
234
|
+
list.push 1,2,3
|
235
|
+
expect(list.pop(2).to_a).to eq([2,3])
|
236
|
+
expect(list.pop).to eq(1)
|
237
|
+
expect{list.pop("1")}.to raise_error(TypeError)
|
238
|
+
end
|
239
|
+
|
240
|
+
it "shift" do
|
241
|
+
list = List.new
|
242
|
+
expect(list.shift).to eq(nil)
|
243
|
+
list.push 1,2,3
|
244
|
+
expect(list.shift(2).to_a).to eq([1,2])
|
245
|
+
expect(list.shift).to eq(3)
|
246
|
+
expect{list.shift("1")}.to raise_error(TypeError)
|
247
|
+
end
|
248
|
+
|
249
|
+
it "unshift" do
|
250
|
+
list = List.new
|
251
|
+
expect(list.unshift).to eq(list)
|
252
|
+
expect(list.unshift(3).to_a).to eq([3])
|
253
|
+
expect(list.unshift(2,1).to_a).to eq([1,2,3])
|
254
|
+
end
|
255
|
+
|
256
|
+
it "insert" do
|
257
|
+
list = List['a','b','c','d']
|
258
|
+
expect(list.insert(2, 99)).to eq(List["a","b",99,"c","d"]);
|
259
|
+
expect{list.insert("a", 1)}.to raise_error(TypeError)
|
260
|
+
end
|
261
|
+
|
262
|
+
it "each" do
|
263
|
+
list = List.new
|
264
|
+
expect(list.each).to be_a_kind_of(Enumerator)
|
265
|
+
expect(list.each{}).to eq(list)
|
266
|
+
|
267
|
+
result = []
|
268
|
+
list.each do |i|
|
269
|
+
result << i
|
270
|
+
end
|
271
|
+
expect(result).to eq([])
|
272
|
+
|
273
|
+
list.push 1,2,3
|
274
|
+
list.each do |i|
|
275
|
+
result << i
|
276
|
+
end
|
277
|
+
expect(result).to eq([1,2,3])
|
278
|
+
end
|
279
|
+
|
280
|
+
it "each_index" do
|
281
|
+
list = List.new
|
282
|
+
expect(list.each_index).to be_a_kind_of(Enumerator)
|
283
|
+
expect(list.each_index{}).to eq(list)
|
284
|
+
|
285
|
+
result = []
|
286
|
+
list.each_index do |i|
|
287
|
+
result << i
|
288
|
+
end
|
289
|
+
expect(result).to eq([])
|
290
|
+
|
291
|
+
list.push 1,2,3
|
292
|
+
list.each_index do |i|
|
293
|
+
result << i
|
294
|
+
end
|
295
|
+
expect(result).to eq([0,1,2])
|
296
|
+
end
|
297
|
+
|
298
|
+
it "reverse_each" do
|
299
|
+
list = List[1,2,3]
|
300
|
+
result = []
|
301
|
+
list.reverse_each do |i|
|
302
|
+
result << i
|
303
|
+
end
|
304
|
+
expect(result).to eq([3,2,1])
|
305
|
+
end
|
306
|
+
|
307
|
+
it "length" do
|
308
|
+
list = List.new
|
309
|
+
expect(list.length).to eq(0)
|
310
|
+
list.push 1,2,3
|
311
|
+
expect(list.length).to eq(3)
|
312
|
+
expect(list.size).to eq(3)
|
313
|
+
end
|
314
|
+
|
315
|
+
it "empty?" do
|
316
|
+
list = List.new
|
317
|
+
expect(list.empty?).to eq(true)
|
318
|
+
list.push 1,2,3
|
319
|
+
expect(list.empty?).to eq(false)
|
320
|
+
end
|
321
|
+
|
322
|
+
it "find_index" do
|
323
|
+
list = List.new
|
324
|
+
expect(list.find_index(1)).to eq(nil)
|
325
|
+
list.push 1,2,3
|
326
|
+
expect(list.find_index(1)).to eq(0)
|
327
|
+
expect(list.find_index(2)).to eq(1)
|
328
|
+
expect(list.index(3)).to eq(2)
|
329
|
+
end
|
330
|
+
|
331
|
+
it "replace" do
|
332
|
+
list = List[1,2,3,4,5]
|
333
|
+
expect(list.replace(List[4,5,6])).to eq(List[4,5,6])
|
334
|
+
expect(list.replace([7,8,9])).to eq(List[7,8,9])
|
335
|
+
expect{list.replace "1"}.to raise_error(TypeError)
|
336
|
+
end
|
337
|
+
|
338
|
+
it "clear" do
|
339
|
+
list = List[1,2,3]
|
340
|
+
expect(list.clear).to eq(List[])
|
341
|
+
expect(list.clear).to eq(List[])
|
342
|
+
end
|
343
|
+
|
344
|
+
it "rindex" do
|
345
|
+
list = List[1,2,2,2,3]
|
346
|
+
expect(list.rindex(2)).to eq(3)
|
347
|
+
expect(list.rindex(-10)).to eq(nil)
|
348
|
+
expect(list.rindex { |x| x == 2 }).to eq(3)
|
349
|
+
end
|
350
|
+
|
351
|
+
it "join" do
|
352
|
+
expect(List.new.join).to eq("")
|
353
|
+
expect(List[1,2,3].join).to eq("123")
|
354
|
+
expect(List["a","b","c"].join).to eq("abc")
|
355
|
+
expect(List["a","b","c"].join("-")).to eq("a-b-c")
|
356
|
+
expect(List["a",List["b",List["c"]]].join("-")).to eq("a-b-c")
|
357
|
+
expect{List["a","b","c"].join(1)}.to raise_error(TypeError)
|
358
|
+
orig_sep = $,
|
359
|
+
$, = "*"
|
360
|
+
expect(List["a","b","c"].join).to eq("a*b*c")
|
361
|
+
$, = orig_sep
|
362
|
+
end
|
363
|
+
|
364
|
+
it "reverse" do
|
365
|
+
list = List.new
|
366
|
+
expect(list.reverse).to eq(List.new)
|
367
|
+
list.push 1,2,3
|
368
|
+
expect(list.reverse).to eq(List[3,2,1])
|
369
|
+
expect(list).to eq(List[1,2,3])
|
370
|
+
end
|
371
|
+
|
372
|
+
it "reverse!" do
|
373
|
+
list = List.new
|
374
|
+
expect(list.reverse!).to eq(List.new)
|
375
|
+
list.push 1,2,3
|
376
|
+
expect(list.reverse!).to eq(List[3,2,1])
|
377
|
+
expect(list).to eq(List[3,2,1])
|
378
|
+
end
|
379
|
+
|
380
|
+
it "rotate" do
|
381
|
+
list = List.new
|
382
|
+
expect(list.rotate).to eq(List.new)
|
383
|
+
list.push 1,2,3
|
384
|
+
expect(list.rotate).to eq(List[2,3,1])
|
385
|
+
expect(list.rotate(2)).to eq(List[3,1,2])
|
386
|
+
expect(list.rotate(-2)).to eq(List[2,3,1])
|
387
|
+
expect(list).to eq(List[1,2,3])
|
388
|
+
expect{list.rotate("a")}.to raise_error(TypeError)
|
389
|
+
expect{list.rotate(1,2)}.to raise_error(ArgumentError)
|
390
|
+
end
|
391
|
+
|
392
|
+
it "rotate!" do
|
393
|
+
list = List.new
|
394
|
+
expect(list.rotate!).to eq(List.new)
|
395
|
+
list.push 1,2,3
|
396
|
+
expect(list.rotate!).to eq(List[2,3,1])
|
397
|
+
expect(list.rotate!(2)).to eq(List[1,2,3])
|
398
|
+
expect(list.rotate!(-2)).to eq(List[2,3,1])
|
399
|
+
expect(list).to eq(List[2,3,1])
|
400
|
+
expect{list.rotate!("a")}.to raise_error(TypeError)
|
401
|
+
expect{list.rotate!(1,2)}.to raise_error(ArgumentError)
|
402
|
+
end
|
403
|
+
|
404
|
+
it "sort" do
|
405
|
+
list = List.new
|
406
|
+
expect(list.sort).to eq(List.new)
|
407
|
+
list.push *[4,1,3,5,2]
|
408
|
+
expect(list.sort).to eq(List[1,2,3,4,5])
|
409
|
+
expect(list.sort{|a,b| b - a}).to eq(List[5,4,3,2,1])
|
410
|
+
expect(list).to eq(List[4,1,3,5,2])
|
411
|
+
end
|
412
|
+
|
413
|
+
it "sort!" do
|
414
|
+
list = List.new
|
415
|
+
expect(list.sort!).to eq(List.new)
|
416
|
+
list.push *[4,1,3,5,2]
|
417
|
+
expect(list.sort!).to eq(List[1,2,3,4,5])
|
418
|
+
expect(list).to eq(List[1,2,3,4,5])
|
419
|
+
expect(list.sort!{|a,b| b - a}).to eq(List[5,4,3,2,1])
|
420
|
+
expect(list).to eq(List[5,4,3,2,1])
|
421
|
+
end
|
422
|
+
|
423
|
+
it "sort_by" do
|
424
|
+
list = List.new
|
425
|
+
expect(list.sort_by{|a| a}).to eq(List.new)
|
426
|
+
list.push *[4,1,3,5,2]
|
427
|
+
expect(list.sort_by{|a| a}).to eq(List[1,2,3,4,5])
|
428
|
+
expect(list.sort_by{|a| -a}).to eq(List[5,4,3,2,1])
|
429
|
+
expect(list).to eq(List[4,1,3,5,2])
|
430
|
+
end
|
431
|
+
|
432
|
+
it "sort_by!" do
|
433
|
+
list = List.new
|
434
|
+
expect(list.sort_by!{|a| a}).to eq(List.new)
|
435
|
+
list.push *[4,1,3,5,2]
|
436
|
+
expect(list.sort_by!{|a| a}).to eq(List[1,2,3,4,5])
|
437
|
+
expect(list.sort_by!{|a| -a}).to eq(List[5,4,3,2,1])
|
438
|
+
expect(list).to eq(List[5,4,3,2,1])
|
439
|
+
end
|
440
|
+
|
441
|
+
it "collect" do
|
442
|
+
list = List.new
|
443
|
+
expect(list.collect{|a| a}).to eq(List.new)
|
444
|
+
list.push *[4,1,3,5,2]
|
445
|
+
expect(list.collect{|a| a * a}).to eq(List[16,1,9,25,4])
|
446
|
+
expect(list.map.each{|a| -a}).to eq(List[-4,-1,-3,-5,-2])
|
447
|
+
expect(list).to eq(List[4,1,3,5,2])
|
448
|
+
end
|
449
|
+
|
450
|
+
it "collect!" do
|
451
|
+
list = List.new
|
452
|
+
expect(list.collect!{|a| a}).to eq(List.new)
|
453
|
+
list.push *[4,1,3,5,2]
|
454
|
+
expect(list.collect!{|a| a * a}).to eq(List[16,1,9,25,4])
|
455
|
+
expect(list.map!.each{|a| -a}).to eq(List[-16,-1,-9,-25,-4])
|
456
|
+
end
|
457
|
+
|
458
|
+
it "select" do
|
459
|
+
list = List.new
|
460
|
+
expect(list.select{|i| i}).to eq(List.new)
|
461
|
+
list.push *[4,1,3,5,2]
|
462
|
+
expect(list.select{|i| i.even?}).to eq(List[4,2])
|
463
|
+
expect(list.select.each{|i| i.odd?}).to eq(List[1,3,5])
|
464
|
+
expect(list).to eq(List[4,1,3,5,2])
|
465
|
+
end
|
466
|
+
|
467
|
+
it "select!" do
|
468
|
+
list = List.new
|
469
|
+
expect(list.select!{|i| i}).to eq(nil)
|
470
|
+
list.push *[4,1,3,5,2]
|
471
|
+
expect(list.select!{|i| i.even?}).to eq(List[4,2])
|
472
|
+
expect(list.select!.each{|i| i % 4 == 0}).to eq(List[4])
|
473
|
+
expect(list.select!.each{|i| i % 4 == 0}).to eq(nil)
|
474
|
+
expect(list).to eq(List[4])
|
475
|
+
end
|
476
|
+
|
477
|
+
it "keep_if" do
|
478
|
+
list = List.new
|
479
|
+
expect(list.keep_if{|i| i}).to eq(list)
|
480
|
+
list.push *[4,1,3,5,2]
|
481
|
+
expect(list.keep_if{|i| i.even?}).to eq(List[4,2])
|
482
|
+
expect(list.keep_if.each{|i| i % 4 == 0}).to eq(List[4])
|
483
|
+
expect(list.keep_if.each{|i| i % 4 == 0}).to eq(list)
|
484
|
+
expect(list).to eq(List[4])
|
485
|
+
end
|
486
|
+
|
487
|
+
it "values_at" do
|
488
|
+
expect(List.new.values_at(1)).to eq(List[nil])
|
489
|
+
list = List[4,1,3,5,2]
|
490
|
+
expect(list.values_at).to eq(List.new)
|
491
|
+
expect(list.values_at 1).to eq(List[1])
|
492
|
+
expect(list.values_at 1,3,10).to eq(List[1,5,nil])
|
493
|
+
expect(list.values_at -1,-2).to eq(List[2,5])
|
494
|
+
expect(list.values_at 1..5).to eq(List[1,3,5,2,nil])
|
495
|
+
expect(list.values_at -2..-1).to eq(List[5,2])
|
496
|
+
expect(list.values_at -1..-2).to eq(List.new)
|
497
|
+
expect(list.values_at 0,1..5).to eq(List[4,1,3,5,2,nil])
|
498
|
+
expect(list.values_at 10..12).to eq(List[nil,nil,nil])
|
499
|
+
expect{list.values_at "a"}.to raise_error(TypeError)
|
500
|
+
end
|
501
|
+
|
502
|
+
it "delete" do
|
503
|
+
expect(List.new.delete(nil)).to eq(nil)
|
504
|
+
list = List[4,1,3,2,5,5]
|
505
|
+
expect(list.delete(1)).to eq(1)
|
506
|
+
expect(list).to eq(List[4,3,2,5,5])
|
507
|
+
expect(list.delete(5){"not found"}).to eq(5)
|
508
|
+
expect(list).to eq(List[4,3,2])
|
509
|
+
expect(list.delete(4)).to eq(4)
|
510
|
+
expect(list.delete(1){"not found"}).to eq("not found")
|
511
|
+
|
512
|
+
list = Array.new(1024,0).to_list
|
513
|
+
expect(list.delete(0)).to eq(0)
|
514
|
+
expect(list).to eq(List.new)
|
515
|
+
end
|
516
|
+
|
517
|
+
it "delete_at" do
|
518
|
+
list = List[1,2,3]
|
519
|
+
expect(list.delete_at(1)).to eq(2)
|
520
|
+
expect(list).to eq(List[1,3])
|
521
|
+
expect(list.delete_at(-1)).to eq(3)
|
522
|
+
expect(list).to eq(List[1])
|
523
|
+
expect(list.delete_at(0)).to eq(1)
|
524
|
+
expect(list).to eq(List[])
|
525
|
+
expect(list.delete_at(0)).to eq(nil)
|
526
|
+
expect{list.delete_at("a")}.to raise_error(TypeError)
|
527
|
+
end
|
528
|
+
|
529
|
+
it "delete_if" do
|
530
|
+
list = List[1,2,3,4,5]
|
531
|
+
expect(list.delete_if{|i| 3 < i}).to eq(List[1,2,3])
|
532
|
+
expect(list.delete_if.each{|i| i.even?}).to eq(List[1,3])
|
533
|
+
expect(list.delete_if{|i| 0 < i}).to eq(List[])
|
534
|
+
expect(list.delete_if{|i| 0 < i}).to eq(List[])
|
535
|
+
expect(list).to eq(List[])
|
536
|
+
end
|
537
|
+
|
538
|
+
it "reject" do
|
539
|
+
list = List[1,2,3,4,5]
|
540
|
+
expect(list.reject{|i| 3 < i}).to eq(List[1,2,3])
|
541
|
+
expect(list.reject.each{|i| i.even?}).to eq(List[1,3,5])
|
542
|
+
expect(list.reject{|i| 0 < i}).to eq(List[])
|
543
|
+
expect(list).to eq(List[1,2,3,4,5])
|
544
|
+
end
|
545
|
+
|
546
|
+
it "reject!" do
|
547
|
+
list = List[1,2,3,4,5]
|
548
|
+
expect(list.reject!{|i| i % 10 == 0}).to eq(nil)
|
549
|
+
expect(list.reject!{|i| 3 < i}).to eq(List[1,2,3])
|
550
|
+
expect(list.reject!.each{|i| i.even?}).to eq(List[1,3])
|
551
|
+
expect(list.reject!{|i| 0 < i}).to eq(List[])
|
552
|
+
expect(list.reject!{|i| 0 < i}).to eq(nil)
|
553
|
+
expect(list).to eq(List[])
|
554
|
+
end
|
555
|
+
|
556
|
+
it "zip" do
|
557
|
+
a = List[4,5,6]
|
558
|
+
b = List[7,8,9]
|
559
|
+
ary = []
|
560
|
+
expect(a.zip(b)).to eq(List[List[4,7],List[5,8],List[6,9]])
|
561
|
+
expect(List[1,2,3].zip(a,b)).to eq(List[List[1,4,7],List[2,5,8],List[3,6,9]])
|
562
|
+
expect(List[1,2].zip(a,b)).to eq(List[List[1,4,7],List[2,5,8]])
|
563
|
+
expect(a.zip(List[1,2],List[8])).to eq(List[List[4,1,8],List[5,2,nil],List[6,nil,nil]])
|
564
|
+
expect(a.zip([1,2],[8])).to eq(List[List[4,1,8],List[5,2,nil],List[6,nil,nil]])
|
565
|
+
expect(a.zip([1,2],[8]){|i| ary << i}).to eq(nil)
|
566
|
+
expect(ary).to eq([List[4,1,8],List[5,2,nil],List[6,nil,nil]])
|
567
|
+
expect{a.zip("a")}.to raise_error # <= 1.9.3 NoMethodError, >= 2.0.0 TypeError
|
568
|
+
end
|
569
|
+
|
570
|
+
it "transpose" do
|
571
|
+
expect(List[].transpose).to eq(List[])
|
572
|
+
expect(List[List[1,2],List[3,4],List[5,6]].transpose).to eq(List[List[1,3,5],List[2,4,6]])
|
573
|
+
expect(List[[1,2],[3,4],[5,6]].transpose).to eq(List[List[1,3,5],List[2,4,6]])
|
574
|
+
expect{List[[1],[2,3]].transpose}.to raise_error(IndexError)
|
575
|
+
expect{List[1].transpose}.to raise_error(TypeError)
|
576
|
+
end
|
577
|
+
|
578
|
+
it "fill" do
|
579
|
+
list = List["a","b","c","d"]
|
580
|
+
expect(list.fill("x")).to eq(List["x","x","x","x"])
|
581
|
+
expect(list.fill("z",2,2)).to eq(List["x","x","z","z"])
|
582
|
+
expect(list.fill("y",0..1)).to eq(List["y","y","z","z"])
|
583
|
+
expect(list.fill{"foo"}).to eq(List["foo","foo","foo","foo"])
|
584
|
+
expect(list[0].object_id).to_not eq(list[1].object_id)
|
585
|
+
expect(list.fill{|i| i*i}).to eq(List[0,1,4,9])
|
586
|
+
expect(list.fill(-2){|i| i*i*i}).to eq(List[0,1,8,27])
|
587
|
+
expect(list.fill("z",2)).to eq(List[0,1,"z","z"])
|
588
|
+
expect{list.fill("z","a")}.to raise_error(TypeError)
|
589
|
+
end
|
590
|
+
|
591
|
+
it "include?" do
|
592
|
+
list = List.new
|
593
|
+
expect(list.include?(1)).to eq(false)
|
594
|
+
list.push 1,2,3
|
595
|
+
expect(list.include?(1)).to eq(true)
|
596
|
+
expect(list.include?(10)).to eq(false)
|
597
|
+
end
|
598
|
+
|
599
|
+
it "<=>" do
|
600
|
+
expect(List["a","a","c"] <=> List["a","b","c"]).to eq(-1)
|
601
|
+
expect(List[1,2,3,4,5,6] <=> List[1,2]).to eq(+1)
|
602
|
+
expect(List[1,2] <=> List[1,2]).to eq(0)
|
603
|
+
expect(List[1,2] <=> List[1,:two]).to eq(nil)
|
604
|
+
end
|
605
|
+
|
606
|
+
it "slice" do
|
607
|
+
list = List.new
|
608
|
+
expect(list.slice(0)).to eq(nil)
|
609
|
+
list.push 1,2,3,4,5
|
610
|
+
expect(list.slice(0)).to eq(1)
|
611
|
+
expect(list.slice(1,3)).to eq(List[2,3,4])
|
612
|
+
expect(list.slice(1..3)).to eq(List[2,3,4])
|
613
|
+
end
|
614
|
+
|
615
|
+
it "slice!" do
|
616
|
+
list = List.new
|
617
|
+
expect(list.slice!(0)).to eq(nil)
|
618
|
+
list.push 1,2,3,4,5
|
619
|
+
expect(list.slice!(2)).to eq(3)
|
620
|
+
expect(list.slice!(2,3)).to eq(List[4,5])
|
621
|
+
expect(list.slice!(0..1)).to eq(List[1,2])
|
622
|
+
list.push 1,2,3,4,5
|
623
|
+
expect(list.slice!(-1)).to eq(5)
|
624
|
+
expect(list.slice!(100..110)).to eq(nil)
|
625
|
+
expect(list.slice!(100)).to eq(nil)
|
626
|
+
expect(list.slice!(100,1)).to eq(nil)
|
627
|
+
expect{list.slice!(0xffffffffffffffff)}.to raise_error(RangeError)
|
628
|
+
expect{list.slice!("a")}.to raise_error(TypeError)
|
629
|
+
expect{list.slice!(1,"a")}.to raise_error(TypeError)
|
630
|
+
expect{list.slice!(1,2,3)}.to raise_error(ArgumentError)
|
631
|
+
expect(list).to eq(List[1,2,3,4])
|
632
|
+
end
|
633
|
+
|
634
|
+
it "ring" do
|
635
|
+
len = 0
|
636
|
+
result = []
|
637
|
+
list = List[1,2,3]
|
638
|
+
ring = list.ring
|
639
|
+
ring.each do |i|
|
640
|
+
break if len == 1000
|
641
|
+
result << i
|
642
|
+
len += 1
|
643
|
+
end
|
644
|
+
expect(result).to eq([1,2,3] * 333 + [1])
|
645
|
+
expect(ring.object_id).to_not eq(list.object_id)
|
646
|
+
expect{ring.push 1}.to raise_error(RuntimeError)
|
647
|
+
end
|
648
|
+
|
649
|
+
it "ring!" do
|
650
|
+
len = 0
|
651
|
+
result = []
|
652
|
+
list = List[1,2,3]
|
653
|
+
ring = list.ring!
|
654
|
+
ring.each do |i|
|
655
|
+
break if len == 1000
|
656
|
+
result << i
|
657
|
+
len += 1
|
658
|
+
end
|
659
|
+
expect(result).to eq([1,2,3] * 333 + [1])
|
660
|
+
expect(ring.object_id).to eq(list.object_id)
|
661
|
+
expect{ring.push 1}.to raise_error(RuntimeError)
|
662
|
+
end
|
663
|
+
|
664
|
+
it "ring?" do
|
665
|
+
list = List[1,2,3]
|
666
|
+
expect(list.ring?).to eq(false)
|
667
|
+
expect(list.ring.ring?).to eq(true)
|
668
|
+
expect(list.ring!.ring?).to eq(true)
|
669
|
+
end
|
670
|
+
end
|