3 class TestArray < Test::Unit::TestCase
15 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
16 assert_equal([1, 2, 1, 2], [1, 2] * 2)
17 assert_equal("1:2", [1, 2] * ":")
19 assert_equal([1, 2].hash, [1, 2].hash)
21 assert_equal([2,3], [1,2,3] & [2,3,4])
22 assert_equal([1,2,3,4], [1,2,3] | [2,3,4])
23 assert_equal([1,2,3] - [2,3], [1])
25 x = [0, 1, 2, 3, 4, 5]
27 assert_equal([1, 2, 3], x[1..3])
28 assert_equal([1, 2, 3], x[1,3])
31 assert(x[0] == 10 && x[1] == 2)
34 assert(x[0] == -1 && x[1] == 10)
37 assert(x[-1] == 20 && x.pop == 20)
40 def test_array_andor_0
41 assert_equal([2], ([1,2,3]&[2,4,6]))
42 assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6]))
46 a = [nil, 1, nil, nil, 5, nil, nil]
47 assert_equal [1, 5], a.compact
48 assert_equal [nil, 1, nil, nil, 5, nil, nil], a
50 assert_equal [1, 5], a
54 x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
56 assert_equal([1, 4, 2, 5], x)
60 assert_equal true, [].empty?
61 assert_equal false, [1].empty?
62 assert_equal false, [1, 1, 4, 2, 5, 4, 5, 1, 2].empty?
66 x = ["it", "came", "to", "pass", "that", "..."]
68 assert_equal("... came it pass that to", x)
70 x.sort!{|a,b| a<=>b} # sort with condition
71 assert_equal([1,2,3,5,7], x)
72 x.sort!{|a,b| b-a} # reverse sort
73 assert_equal([7,5,3,2,1], x)
77 x = "The Book of Mormon"
78 assert_equal(x.reverse, x.split(//).reverse!.join)
79 assert_equal(x.reverse, x.reverse!)
80 assert_equal("g:n:i:r:t:s: :e:t:y:b: :1", "1 byte string".split(//).reverse.join(":"))
82 assert_equal(['a', 'b', 'c', 'd'], x.split)
83 assert_equal(['a', 'b', 'c', 'd'], x.split(' '))
87 assert(defined? "a".chomp)
88 assert_equal(["a", "b", "c"], "abc".scan(/./))
89 assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/))
91 assert_equal([["a", "12"], ["b", "22"]], "a=12;b=22".scan(/(.*?)=(\d*);?/))
94 assert_equal('1:1:1:1:1', (x * 5).join(":"))
95 assert_equal('1', (x * 1).join(":"))
96 assert_equal('', (x * 0).join(":"))
99 assert_equal(7, x.size)
100 assert_equal([1, 2, 3, 4, 5, 6, 7], x)
104 assert_equal([1,1,2,3,2,3], x)
108 assert_equal([1,2,1,2,3,3], x)
112 assert_equal([1,2,3,1,2,3], x)
122 assert_equal([1,2,3,4], x)
123 assert_equal([1,2,3,5], y)
129 assert_equal(1, x.first)
130 assert_equal([1], x.first(1))
131 assert_equal([1, 2, 3], x.first(3))
133 assert_equal(5, x.last)
134 assert_equal([5], x.last(1))
135 assert_equal([3, 4, 5], x.last(3))
137 assert_equal(1, x.shift)
138 assert_equal([2, 3, 4], x.shift(3))
141 assert_equal([2, 3, 4, 5], x.unshift(2, 3, 4))
142 assert_equal([1, 2, 3, 4, 5], x.unshift(1))
143 assert_equal([1, 2, 3, 4, 5], x)
145 assert_equal(5, x.pop)
146 assert_equal([3, 4], x.pop(2))
147 assert_equal([1, 2], x)
149 assert_equal([1, 2, 3, 4], x.push(3, 4))
150 assert_equal([1, 2, 3, 4, 5], x.push(5))
151 assert_equal([1, 2, 3, 4, 5], x)
155 assert_respond_to([], :find_all)
156 assert_respond_to([], :select) # Alias
157 assert_equal([], [].find_all{ |obj| obj == "foo"})
159 x = ["foo", "bar", "baz", "baz", 1, 2, 3, 3, 4]
160 assert_equal(["baz","baz"], x.find_all{ |obj| obj == "baz" })
161 assert_equal([3,3], x.find_all{ |obj| obj == 3 })
165 assert_equal([-1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1))
166 assert_equal([0, 1, 2, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3))
167 assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3, 2))
168 assert_equal([0, 1, 2, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3, 5))
169 assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2, 2))
170 assert_equal([0, 1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 2, 5))
171 assert_equal([0, 1, 2, 3, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, -2, 1))
172 assert_equal([0, 1, 2, 3, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, -2, 3))
173 assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3..4))
174 assert_equal([0, 1, 2, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3...4))
175 assert_equal([0, 1, -1, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2..-2))
176 assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2...-2))
177 assert_equal([10, 11, 12, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill{|i| i+10})
178 assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3){|i| i+10})
179 assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3, 2){|i| i+10})
180 assert_equal([0, 1, 2, 13, 14, 15, 16, 17], [0, 1, 2, 3, 4, 5].fill(3, 5){|i| i+10})
181 assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3..4){|i| i+10})
182 assert_equal([0, 1, 2, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(3...4){|i| i+10})
183 assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10})
184 assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10})
191 assert_instance_of(@cls, a)
192 assert_equal(0, a.length)
196 def test_01_square_brackets
197 a = @cls[ 5, 4, 3, 2, 1 ]
198 assert_instance_of(@cls, a)
199 assert_equal(5, a.length)
200 5.times { |i| assert_equal(5-i, a[i]) }
205 assert_equal(@cls[1, 3], @cls[ 1, 1, 3, 5 ] & @cls[ 1, 2, 3 ])
206 assert_equal(@cls[], @cls[ 1, 1, 3, 5 ] & @cls[ ])
207 assert_equal(@cls[], @cls[ ] & @cls[ 1, 2, 3 ])
208 assert_equal(@cls[], @cls[ 1, 2, 3 ] & @cls[ 4, 5, 6 ])
212 assert_equal(@cls[], @cls[]*3)
213 assert_equal(@cls[1, 1, 1], @cls[1]*3)
214 assert_equal(@cls[1, 2, 1, 2, 1, 2], @cls[1, 2]*3)
215 assert_equal(@cls[], @cls[1, 2, 3] * 0)
216 assert_raise(ArgumentError) { @cls[1, 2]*(-3) }
218 assert_equal('1-2-3-4-5', @cls[1, 2, 3, 4, 5] * '-')
219 assert_equal('12345', @cls[1, 2, 3, 4, 5] * '')
224 assert_equal(@cls[], @cls[] + @cls[])
225 assert_equal(@cls[1], @cls[1] + @cls[])
226 assert_equal(@cls[1], @cls[] + @cls[1])
227 assert_equal(@cls[1, 1], @cls[1] + @cls[1])
228 assert_equal(@cls['cat', 'dog', 1, 2, 3], %w(cat dog) + (1..3).to_a)
232 assert_equal(@cls[], @cls[1] - @cls[1])
233 assert_equal(@cls[1], @cls[1, 2, 3, 4, 5] - @cls[2, 3, 4, 5])
234 # Ruby 1.8 feature change
235 #assert_equal(@cls[1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5])
236 assert_equal(@cls[1, 1, 1, 1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5])
238 1000.times { a << 1 }
239 assert_equal(1000, a.length)
240 #assert_equal(@cls[1], a - @cls[2])
241 assert_equal(@cls[1] * 1000, a - @cls[2])
242 #assert_equal(@cls[1], @cls[1, 2, 1] - @cls[2])
243 assert_equal(@cls[1, 1], @cls[1, 2, 1] - @cls[2])
244 assert_equal(@cls[1, 2, 3], @cls[1, 2, 3] - @cls[4, 5, 6])
247 def test_LSHIFT # '<<'
250 assert_equal(@cls[1], a)
252 assert_equal(@cls[1, 2, 3], a)
254 assert_equal(@cls[1, 2, 3, nil, 'cat'], a)
256 assert_equal(@cls[1, 2, 3, nil, 'cat', a], a)
260 assert_equal(0, @cls[] <=> @cls[])
261 assert_equal(0, @cls[1] <=> @cls[1])
262 assert_equal(0, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'cat'])
263 assert_equal(-1, @cls[] <=> @cls[1])
264 assert_equal(1, @cls[1] <=> @cls[])
265 assert_equal(-1, @cls[1, 2, 3] <=> @cls[1, 2, 3, 'cat'])
266 assert_equal(1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3])
267 assert_equal(-1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'dog'])
268 assert_equal(1, @cls[1, 2, 3, 'dog'] <=> @cls[1, 2, 3, 'cat'])
271 def test_EQUAL # '=='
272 assert(@cls[] == @cls[])
273 assert(@cls[1] == @cls[1])
274 assert(@cls[1, 1, 2, 2] == @cls[1, 1, 2, 2])
275 assert(@cls[1.0, 1.0, 2.0, 2.0] == @cls[1, 1, 2, 2])
278 def test_VERY_EQUAL # '==='
279 assert(@cls[] === @cls[])
280 assert(@cls[1] === @cls[1])
281 assert(@cls[1, 1, 2, 2] === @cls[1, 1, 2, 2])
282 assert(@cls[1.0, 1.0, 2.0, 2.0] === @cls[1, 1, 2, 2])
286 a = @cls[*(1..100).to_a]
288 assert_equal(1, a[0])
289 assert_equal(100, a[99])
291 assert_equal(100, a[-1])
292 assert_equal(99, a[-2])
293 assert_equal(1, a[-100])
295 assert_nil(a[-101,0])
296 assert_nil(a[-101,1])
297 assert_nil(a[-101,-1])
300 assert_equal(@cls[1], a[0,1])
301 assert_equal(@cls[100], a[99,1])
302 assert_equal(@cls[], a[100,1])
303 assert_equal(@cls[100], a[99,100])
304 assert_equal(@cls[100], a[-1,1])
305 assert_equal(@cls[99], a[-2,1])
306 assert_equal(@cls[], a[-100,0])
307 assert_equal(@cls[1], a[-100,1])
309 assert_equal(@cls[10, 11, 12], a[9, 3])
310 assert_equal(@cls[10, 11, 12], a[-91, 3])
312 assert_equal(@cls[1], a[0..0])
313 assert_equal(@cls[100], a[99..99])
314 assert_equal(@cls[], a[100..100])
315 assert_equal(@cls[100], a[99..200])
316 assert_equal(@cls[100], a[-1..-1])
317 assert_equal(@cls[99], a[-2..-2])
319 assert_equal(@cls[10, 11, 12], a[9..11])
320 assert_equal(@cls[10, 11, 12], a[-91..-89])
322 assert_nil(a[10, -3])
323 # Ruby 1.8 feature change:
324 # Array#[size..x] returns [] instead of nil.
325 #assert_nil(a[10..7])
326 assert_equal [], a[10..7]
328 assert_raise(TypeError) {a['cat']}
331 def test_ASET # '[]='
332 a = @cls[*(0..99).to_a]
333 assert_equal(0, a[0] = 0)
334 assert_equal(@cls[0] + @cls[*(1..99).to_a], a)
336 a = @cls[*(0..99).to_a]
337 assert_equal(0, a[10,10] = 0)
338 assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
340 a = @cls[*(0..99).to_a]
341 assert_equal(0, a[-1] = 0)
342 assert_equal(@cls[*(0..98).to_a] + @cls[0], a)
344 a = @cls[*(0..99).to_a]
345 assert_equal(0, a[-10, 10] = 0)
346 assert_equal(@cls[*(0..89).to_a] + @cls[0], a)
348 a = @cls[*(0..99).to_a]
349 assert_equal(0, a[0,1000] = 0)
350 assert_equal(@cls[0] , a)
352 a = @cls[*(0..99).to_a]
353 assert_equal(0, a[10..19] = 0)
354 assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
356 b = @cls[*%w( a b c )]
357 a = @cls[*(0..99).to_a]
358 assert_equal(b, a[0,1] = b)
359 assert_equal(b + @cls[*(1..99).to_a], a)
361 a = @cls[*(0..99).to_a]
362 assert_equal(b, a[10,10] = b)
363 assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
365 a = @cls[*(0..99).to_a]
366 assert_equal(b, a[-1, 1] = b)
367 assert_equal(@cls[*(0..98).to_a] + b, a)
369 a = @cls[*(0..99).to_a]
370 assert_equal(b, a[-10, 10] = b)
371 assert_equal(@cls[*(0..89).to_a] + b, a)
373 a = @cls[*(0..99).to_a]
374 assert_equal(b, a[0,1000] = b)
377 a = @cls[*(0..99).to_a]
378 assert_equal(b, a[10..19] = b)
379 assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
381 # Ruby 1.8 feature change:
382 # assigning nil does not remove elements.
384 a = @cls[*(0..99).to_a]
385 assert_equal(nil, a[0,1] = nil)
386 assert_equal(@cls[*(1..99).to_a], a)
388 a = @cls[*(0..99).to_a]
389 assert_equal(nil, a[10,10] = nil)
390 assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
392 a = @cls[*(0..99).to_a]
393 assert_equal(nil, a[-1, 1] = nil)
394 assert_equal(@cls[*(0..98).to_a], a)
396 a = @cls[*(0..99).to_a]
397 assert_equal(nil, a[-10, 10] = nil)
398 assert_equal(@cls[*(0..89).to_a], a)
400 a = @cls[*(0..99).to_a]
401 assert_equal(nil, a[0,1000] = nil)
402 assert_equal(@cls[] , a)
404 a = @cls[*(0..99).to_a]
405 assert_equal(nil, a[10..19] = nil)
406 assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
411 assert_equal([1, 1, 2, 3, 2, 3], a)
415 assert_equal([1, 2, 1, 2, 3, 3], a)
419 a1 = @cls[*%w( cat feline )]
420 a2 = @cls[*%w( dog canine )]
421 a3 = @cls[*%w( mule asinine )]
423 a = @cls[ a1, a2, a3 ]
425 assert_equal(a1, a.assoc('cat'))
426 assert_equal(a3, a.assoc('mule'))
427 assert_equal(nil, a.assoc('asinine'))
428 assert_equal(nil, a.assoc('wombat'))
429 assert_equal(nil, a.assoc(1..2))
433 a = @cls[*(0..99).to_a]
434 assert_equal(0, a.at(0))
435 assert_equal(10, a.at(10))
436 assert_equal(99, a.at(99))
437 assert_equal(nil, a.at(100))
438 assert_equal(99, a.at(-1))
439 assert_equal(0, a.at(-100))
440 assert_equal(nil, a.at(-101))
441 assert_raise(TypeError) { a.at('cat') }
447 assert_equal(@cls[], a)
448 assert_equal(@cls[], b)
449 assert_equal(a.__id__, b.__id__)
453 for taint in [ false, true ]
454 for frozen in [ false, true ]
455 a = @cls[*(0..99).to_a]
461 assert(a.__id__ != b.__id__)
462 assert_equal(a.frozen?, b.frozen?)
463 assert_equal(a.tainted?, b.tainted?)
469 a = @cls[ 1, 'cat', 1..1 ]
470 assert_equal([ Fixnum, String, Range], a.collect {|e| e.class} )
471 assert_equal([ 99, 99, 99], a.collect { 99 } )
473 assert_equal([], @cls[].collect { 99 })
475 # Ruby 1.9 feature change:
476 # Enumerable#collect without block returns an Enumerator.
477 #assert_equal([1, 2, 3], @cls[1, 2, 3].collect)
478 assert_kind_of Enumerable::Enumerator, @cls[1, 2, 3].collect
483 a = @cls[ 1, 'cat', 1..1 ]
484 assert_equal([ Fixnum, String, Range], a.collect! {|e| e.class} )
485 assert_equal([ Fixnum, String, Range], a)
487 a = @cls[ 1, 'cat', 1..1 ]
488 assert_equal([ 99, 99, 99], a.collect! { 99 } )
489 assert_equal([ 99, 99, 99], a)
492 assert_equal([], a.collect! { 99 })
497 a = @cls[ 1, nil, nil, 2, 3, nil, 4 ]
498 assert_equal(@cls[1, 2, 3, 4], a.compact)
500 a = @cls[ nil, 1, nil, 2, 3, nil, 4 ]
501 assert_equal(@cls[1, 2, 3, 4], a.compact)
503 a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ]
504 assert_equal(@cls[1, 2, 3, 4], a.compact)
506 a = @cls[ 1, 2, 3, 4 ]
507 assert_equal(@cls[1, 2, 3, 4], a.compact)
511 a = @cls[ 1, nil, nil, 2, 3, nil, 4 ]
512 assert_equal(@cls[1, 2, 3, 4], a.compact!)
513 assert_equal(@cls[1, 2, 3, 4], a)
515 a = @cls[ nil, 1, nil, 2, 3, nil, 4 ]
516 assert_equal(@cls[1, 2, 3, 4], a.compact!)
517 assert_equal(@cls[1, 2, 3, 4], a)
519 a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ]
520 assert_equal(@cls[1, 2, 3, 4], a.compact!)
521 assert_equal(@cls[1, 2, 3, 4], a)
523 a = @cls[ 1, 2, 3, 4 ]
524 assert_equal(nil, a.compact!)
525 assert_equal(@cls[1, 2, 3, 4], a)
529 assert_equal(@cls[1, 2, 3, 4], @cls[1, 2].concat(@cls[3, 4]))
530 assert_equal(@cls[1, 2, 3, 4], @cls[].concat(@cls[1, 2, 3, 4]))
531 assert_equal(@cls[1, 2, 3, 4], @cls[1, 2, 3, 4].concat(@cls[]))
532 assert_equal(@cls[], @cls[].concat(@cls[]))
533 assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]]))
537 assert_equal([1, 2, 3, 1, 2, 3], a)
541 a = @cls[1, 2, 3, 1, 2]
542 assert_equal(5, a.count)
543 assert_equal(2, a.count(1))
544 assert_equal(3, a.count {|x| x % 2 == 1 })
545 assert_equal(2, a.count(1) {|x| x % 2 == 1 })
546 assert_raise(ArgumentError) { a.count(0, 1) }
550 a = @cls[*('cab'..'cat').to_a]
551 assert_equal('cap', a.delete('cap'))
552 assert_equal(@cls[*('cab'..'cao').to_a] + @cls[*('caq'..'cat').to_a], a)
554 a = @cls[*('cab'..'cat').to_a]
555 assert_equal('cab', a.delete('cab'))
556 assert_equal(@cls[*('cac'..'cat').to_a], a)
558 a = @cls[*('cab'..'cat').to_a]
559 assert_equal('cat', a.delete('cat'))
560 assert_equal(@cls[*('cab'..'cas').to_a], a)
562 a = @cls[*('cab'..'cat').to_a]
563 assert_equal(nil, a.delete('cup'))
564 assert_equal(@cls[*('cab'..'cat').to_a], a)
566 a = @cls[*('cab'..'cat').to_a]
567 assert_equal(99, a.delete('cup') { 99 } )
568 assert_equal(@cls[*('cab'..'cat').to_a], a)
572 a = @cls[*(1..5).to_a]
573 assert_equal(3, a.delete_at(2))
574 assert_equal(@cls[1, 2, 4, 5], a)
576 a = @cls[*(1..5).to_a]
577 assert_equal(4, a.delete_at(-2))
578 assert_equal(@cls[1, 2, 3, 5], a)
580 a = @cls[*(1..5).to_a]
581 assert_equal(nil, a.delete_at(5))
582 assert_equal(@cls[1, 2, 3, 4, 5], a)
584 a = @cls[*(1..5).to_a]
585 assert_equal(nil, a.delete_at(-6))
586 assert_equal(@cls[1, 2, 3, 4, 5], a)
591 a = @cls[ 1, 2, 3, 4, 5 ]
592 assert_equal(a, a.delete_if { false })
593 assert_equal(@cls[1, 2, 3, 4, 5], a)
595 a = @cls[ 1, 2, 3, 4, 5 ]
596 assert_equal(a, a.delete_if { true })
597 assert_equal(@cls[], a)
599 a = @cls[ 1, 2, 3, 4, 5 ]
600 assert_equal(a, a.delete_if { |i| i > 3 })
601 assert_equal(@cls[1, 2, 3], a)
605 for taint in [ false, true ]
606 for frozen in [ false, true ]
607 a = @cls[*(0..99).to_a]
613 assert(a.__id__ != b.__id__)
614 assert_equal(false, b.frozen?)
615 assert_equal(a.tainted?, b.tainted?)
621 a = @cls[*%w( ant bat cat dog )]
624 assert_equal(a[i], e)
632 assert_equal(a[i], e)
637 assert_equal(a, a.each {})
641 a = @cls[*%w( ant bat cat dog )]
657 assert_equal(a, a.each_index {})
661 assert(@cls[].empty?)
662 assert(!@cls[1].empty?)
666 assert(@cls[].eql?(@cls[]))
667 assert(@cls[1].eql?(@cls[1]))
668 assert(@cls[1, 1, 2, 2].eql?(@cls[1, 1, 2, 2]))
669 assert(!@cls[1.0, 1.0, 2.0, 2.0].eql?(@cls[1, 1, 2, 2]))
673 assert_equal(@cls[], @cls[].fill(99))
674 assert_equal(@cls[], @cls[].fill(99, 0))
675 assert_equal(@cls[99], @cls[].fill(99, 0, 1))
676 assert_equal(@cls[99], @cls[].fill(99, 0..0))
678 assert_equal(@cls[99], @cls[1].fill(99))
679 assert_equal(@cls[99], @cls[1].fill(99, 0))
680 assert_equal(@cls[99], @cls[1].fill(99, 0, 1))
681 assert_equal(@cls[99], @cls[1].fill(99, 0..0))
683 assert_equal(@cls[99, 99], @cls[1, 2].fill(99))
684 assert_equal(@cls[99, 99], @cls[1, 2].fill(99, 0))
685 assert_equal(@cls[99, 99], @cls[1, 2].fill(99, nil))
686 assert_equal(@cls[1, 99], @cls[1, 2].fill(99, 1, nil))
687 assert_equal(@cls[99, 2], @cls[1, 2].fill(99, 0, 1))
688 assert_equal(@cls[99, 2], @cls[1, 2].fill(99, 0..0))
692 assert_equal(3, @cls[3, 4, 5].first)
693 assert_equal(nil, @cls[].first)
701 assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten)
702 assert_equal(@cls[ a1, a3], a4)
704 a5 = @cls[ a1, @cls[], a3 ]
705 assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten)
706 assert_equal(@cls[], @cls[].flatten)
708 @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
710 assert_raise(TypeError, "[ruby-dev:31197]") { [[]].flatten("") }
718 assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten!)
719 assert_equal(@cls[1, 2, 3, 4, 5, 6], a4)
721 a5 = @cls[ a1, @cls[], a3 ]
722 assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten!)
723 assert_equal(@cls[1, 2, 3, 4, 5, 6], a5)
725 assert_equal(@cls[], @cls[].flatten)
727 @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
730 def test_flatten_with_callcc
731 respond_to?(:callcc, true) or require 'continuation'
733 def o.to_ary() callcc {|k| @cont = k; [1,2,3]} end
735 assert_equal([10, 20, 1, 2, 3, 30, 1, 2, 3, 40], [10, 20, o, 30, o, 40].flatten)
738 o.instance_eval {@cont}.call
740 assert_instance_of(RuntimeError, e, '[ruby-dev:34798]')
741 assert_match(/reentered/, e.message, '[ruby-dev:34798]')
745 a1 = @cls[ 'cat', 'dog' ]
746 a2 = @cls[ 'cat', 'dog' ]
747 a3 = @cls[ 'dog', 'cat' ]
748 assert(a1.hash == a2.hash)
749 assert(a1.hash != a3.hash)
753 a = @cls[ 'cat', 99, /a/, @cls[ 1, 2, 3] ]
754 assert(a.include?('cat'))
755 assert(a.include?(99))
756 assert(a.include?(/a/))
757 assert(a.include?([1,2,3]))
758 assert(!a.include?('ca'))
759 assert(!a.include?([1,2]))
763 a = @cls[ 'cat', 99, /a/, 99, @cls[ 1, 2, 3] ]
764 assert_equal(0, a.index('cat'))
765 assert_equal(1, a.index(99))
766 assert_equal(4, a.index([1,2,3]))
767 assert_nil(a.index('ca'))
768 assert_nil(a.index([1,2]))
772 a = @cls[*('a'..'j').to_a]
773 assert_equal(@cls['a', 'c', 'e'], a.values_at(0, 2, 4))
774 assert_equal(@cls['j', 'h', 'f'], a.values_at(-1, -3, -5))
775 assert_equal(@cls['h', nil, 'a'], a.values_at(-3, 99, 0))
781 assert_equal("", a.join)
782 assert_equal("", a.join(','))
786 assert_equal("12", a.join)
787 assert_equal("1,2", a.join(','))
791 assert_equal("123", a.join)
792 assert_equal("1,2,3", a.join(','))
796 assert_equal("1:2:3", a.join)
797 assert_equal("1,2,3", a.join(','))
803 assert_equal(nil, @cls[].last)
804 assert_equal(1, @cls[1].last)
805 assert_equal(99, @cls[*(3..99).to_a].last)
809 assert_equal(0, @cls[].length)
810 assert_equal(1, @cls[1].length)
811 assert_equal(2, @cls[1, nil].length)
812 assert_equal(2, @cls[nil, 1].length)
813 assert_equal(234, @cls[*(0..233).to_a].length)
816 # also update collect!
818 a = @cls[ 1, 'cat', 1..1 ]
819 assert_equal(@cls[ Fixnum, String, Range], a.map! {|e| e.class} )
820 assert_equal(@cls[ Fixnum, String, Range], a)
822 a = @cls[ 1, 'cat', 1..1 ]
823 assert_equal(@cls[ 99, 99, 99], a.map! { 99 } )
824 assert_equal(@cls[ 99, 99, 99], a)
827 assert_equal(@cls[], a.map! { 99 })
828 assert_equal(@cls[], a)
832 a = @cls[*%w( cat wombat x yy)]
833 assert_equal("catwomx yy ", a.pack("A3A3A3A3"))
834 assert_equal("cat", a.pack("A*"))
835 assert_equal("cwx yy ", a.pack("A3@1A3@2A3A3"))
836 assert_equal("catwomx\000\000yy\000", a.pack("a3a3a3a3"))
837 assert_equal("cat", a.pack("a*"))
838 assert_equal("ca", a.pack("a2"))
839 assert_equal("cat\000\000", a.pack("a5"))
841 assert_equal("\x61", @cls["01100001"].pack("B8"))
842 assert_equal("\x61", @cls["01100001"].pack("B*"))
843 assert_equal("\x61", @cls["0110000100110111"].pack("B8"))
844 assert_equal("\x61\x37", @cls["0110000100110111"].pack("B16"))
845 assert_equal("\x61\x37", @cls["01100001", "00110111"].pack("B8B8"))
846 assert_equal("\x60", @cls["01100001"].pack("B4"))
847 assert_equal("\x40", @cls["01100001"].pack("B2"))
849 assert_equal("\x86", @cls["01100001"].pack("b8"))
850 assert_equal("\x86", @cls["01100001"].pack("b*"))
851 assert_equal("\x86", @cls["0110000100110111"].pack("b8"))
852 assert_equal("\x86\xec", @cls["0110000100110111"].pack("b16"))
853 assert_equal("\x86\xec", @cls["01100001", "00110111"].pack("b8b8"))
854 assert_equal("\x06", @cls["01100001"].pack("b4"))
855 assert_equal("\x02", @cls["01100001"].pack("b2"))
857 assert_equal("ABC", @cls[ 65, 66, 67 ].pack("C3"))
858 assert_equal("\377BC", @cls[ -1, 66, 67 ].pack("C*"))
859 assert_equal("ABC", @cls[ 65, 66, 67 ].pack("c3"))
860 assert_equal("\377BC", @cls[ -1, 66, 67 ].pack("c*"))
863 assert_equal("AB\n\x10", @cls["4142", "0a", "12"].pack("H4H2H1"))
864 assert_equal("AB\n\x02", @cls["1424", "a0", "21"].pack("h4h2h1"))
866 assert_equal("abc=02def=\ncat=\n=01=\n",
867 @cls["abc\002def", "cat", "\001"].pack("M9M3M4"))
869 assert_equal("aGVsbG8K\n", @cls["hello\n"].pack("m"))
870 assert_equal(",:&5L;&\\*:&5L;&\\*\n", @cls["hello\nhello\n"].pack("u"))
872 assert_equal("\xc2\xa9B\xe2\x89\xa0", @cls[0xa9, 0x42, 0x2260].pack("U*"))
875 format = "c2x5CCxsdils_l_a6";
876 # Need the expression in here to force ary[5] to be numeric. This avoids
877 # test2 failing because ary2 goes str->numeric->str and ary does not.
878 ary = [1, -100, 127, 128, 32767, 987.654321098/100.0,
879 12345, 123456, -32767, -123456, "abcdef"]
881 ary2 = x.unpack(format)
883 assert_equal(ary.length, ary2.length)
884 assert_equal(ary.join(':'), ary2.join(':'))
885 assert_not_nil(x =~ /def/)
888 skipping "Not tested:
889 D,d & double-precision float, native format\\
890 E & double-precision float, little-endian byte order\\
891 e & single-precision float, little-endian byte order\\
892 F,f & single-precision float, native format\\
893 G & double-precision float, network (big-endian) byte order\\
894 g & single-precision float, network (big-endian) byte order\\
895 I & unsigned integer\\
900 N & long, network (big-endian) byte order\\
901 n & short, network (big-endian) byte-order\\
902 P & pointer to a structure (fixed-length string)\\
903 p & pointer to a null-terminated string\\
906 V & long, little-endian byte order\\
907 v & short, little-endian byte order\\
910 Z & ASCII string (null padded, count is width)\\
916 a = @cls[ 'cat', 'dog' ]
917 assert_equal('dog', a.pop)
918 assert_equal(@cls['cat'], a)
919 assert_equal('cat', a.pop)
920 assert_equal(@cls[], a)
922 assert_equal(@cls[], a)
927 assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5))
928 assert_equal(@cls[1, 2, 3, 4, 5, nil], a.push(nil))
930 # Array#push accepts any number of arguments.
931 #assert_raise(ArgumentError, "a.push()") { a.push() }
933 assert_equal @cls[1, 2, 3, 4, 5, nil], a
935 assert_equal @cls[1, 2, 3, 4, 5, nil, 6, 7], a
939 a1 = @cls[*%w( cat feline )]
940 a2 = @cls[*%w( dog canine )]
941 a3 = @cls[*%w( mule asinine )]
942 a = @cls[ a1, a2, a3 ]
944 assert_equal(a1, a.rassoc('feline'))
945 assert_equal(a3, a.rassoc('asinine'))
946 assert_equal(nil, a.rassoc('dog'))
947 assert_equal(nil, a.rassoc('mule'))
948 assert_equal(nil, a.rassoc(1..2))
953 a = @cls[ 1, 2, 3, 4, 5 ]
954 assert_equal(nil, a.reject! { false })
955 assert_equal(@cls[1, 2, 3, 4, 5], a)
957 a = @cls[ 1, 2, 3, 4, 5 ]
958 assert_equal(a, a.reject! { true })
959 assert_equal(@cls[], a)
961 a = @cls[ 1, 2, 3, 4, 5 ]
962 assert_equal(a, a.reject! { |i| i > 3 })
963 assert_equal(@cls[1, 2, 3], a)
969 assert_equal(@cls[4, 5, 6], a.replace(@cls[4, 5, 6]))
970 assert_equal(@cls[4, 5, 6], a)
971 assert_equal(a_id, a.__id__)
972 assert_equal(@cls[], a.replace(@cls[]))
976 a = @cls[*%w( dog cat bee ant )]
977 assert_equal(@cls[*%w(ant bee cat dog)], a.reverse)
978 assert_equal(@cls[*%w(dog cat bee ant)], a)
979 assert_equal(@cls[], @cls[].reverse)
983 a = @cls[*%w( dog cat bee ant )]
984 assert_equal(@cls[*%w(ant bee cat dog)], a.reverse!)
985 assert_equal(@cls[*%w(ant bee cat dog)], a)
986 # Ruby 1.8 feature change:
987 # Array#reverse always returns self.
988 #assert_nil(@cls[].reverse!)
989 assert_equal @cls[], @cls[].reverse!
992 def test_reverse_each
993 a = @cls[*%w( dog cat bee ant )]
997 assert_equal(a[i], e)
1003 a.reverse_each { |e|
1004 assert(false, "Never get here")
1010 a = @cls[ 'cat', 99, /a/, 99, [ 1, 2, 3] ]
1011 assert_equal(0, a.rindex('cat'))
1012 assert_equal(3, a.rindex(99))
1013 assert_equal(4, a.rindex([1,2,3]))
1014 assert_nil(a.rindex('ca'))
1015 assert_nil(a.rindex([1,2]))
1019 a = @cls[ 'cat', 'dog' ]
1020 assert_equal('cat', a.shift)
1021 assert_equal(@cls['dog'], a)
1022 assert_equal('dog', a.shift)
1023 assert_equal(@cls[], a)
1025 assert_equal(@cls[], a)
1029 assert_equal(0, @cls[].size)
1030 assert_equal(1, @cls[1].size)
1031 assert_equal(100, @cls[*(0..99).to_a].size)
1035 a = @cls[*(1..100).to_a]
1037 assert_equal(1, a.slice(0))
1038 assert_equal(100, a.slice(99))
1039 assert_nil(a.slice(100))
1040 assert_equal(100, a.slice(-1))
1041 assert_equal(99, a.slice(-2))
1042 assert_equal(1, a.slice(-100))
1043 assert_nil(a.slice(-101))
1045 assert_equal(@cls[1], a.slice(0,1))
1046 assert_equal(@cls[100], a.slice(99,1))
1047 assert_equal(@cls[], a.slice(100,1))
1048 assert_equal(@cls[100], a.slice(99,100))
1049 assert_equal(@cls[100], a.slice(-1,1))
1050 assert_equal(@cls[99], a.slice(-2,1))
1052 assert_equal(@cls[10, 11, 12], a.slice(9, 3))
1053 assert_equal(@cls[10, 11, 12], a.slice(-91, 3))
1055 assert_nil(a.slice(-101, 2))
1057 assert_equal(@cls[1], a.slice(0..0))
1058 assert_equal(@cls[100], a.slice(99..99))
1059 assert_equal(@cls[], a.slice(100..100))
1060 assert_equal(@cls[100], a.slice(99..200))
1061 assert_equal(@cls[100], a.slice(-1..-1))
1062 assert_equal(@cls[99], a.slice(-2..-2))
1064 assert_equal(@cls[10, 11, 12], a.slice(9..11))
1065 assert_equal(@cls[10, 11, 12], a.slice(-91..-89))
1067 assert_nil(a.slice(-101..-1))
1069 assert_nil(a.slice(10, -3))
1070 # Ruby 1.8 feature change:
1071 # Array#slice[size..x] always returns [].
1072 #assert_nil(a.slice(10..7))
1073 assert_equal @cls[], a.slice(10..7)
1077 a = @cls[1, 2, 3, 4, 5]
1078 assert_equal(3, a.slice!(2))
1079 assert_equal(@cls[1, 2, 4, 5], a)
1081 a = @cls[1, 2, 3, 4, 5]
1082 assert_equal(4, a.slice!(-2))
1083 assert_equal(@cls[1, 2, 3, 5], a)
1085 a = @cls[1, 2, 3, 4, 5]
1086 assert_equal(@cls[3,4], a.slice!(2,2))
1087 assert_equal(@cls[1, 2, 5], a)
1089 a = @cls[1, 2, 3, 4, 5]
1090 assert_equal(@cls[4,5], a.slice!(-2,2))
1091 assert_equal(@cls[1, 2, 3], a)
1093 a = @cls[1, 2, 3, 4, 5]
1094 assert_equal(@cls[3,4], a.slice!(2..3))
1095 assert_equal(@cls[1, 2, 5], a)
1097 a = @cls[1, 2, 3, 4, 5]
1098 assert_equal(nil, a.slice!(20))
1099 assert_equal(@cls[1, 2, 3, 4, 5], a)
1101 a = @cls[1, 2, 3, 4, 5]
1102 assert_equal(nil, a.slice!(-6))
1103 assert_equal(@cls[1, 2, 3, 4, 5], a)
1105 a = @cls[1, 2, 3, 4, 5]
1106 assert_equal(nil, a.slice!(-6..4))
1107 assert_equal(@cls[1, 2, 3, 4, 5], a)
1109 a = @cls[1, 2, 3, 4, 5]
1110 assert_equal(nil, a.slice!(-6,2))
1111 assert_equal(@cls[1, 2, 3, 4, 5], a)
1115 a = @cls[ 4, 1, 2, 3 ]
1116 assert_equal(@cls[1, 2, 3, 4], a.sort)
1117 assert_equal(@cls[4, 1, 2, 3], a)
1119 assert_equal(@cls[4, 3, 2, 1], a.sort { |x, y| y <=> x} )
1120 assert_equal(@cls[4, 1, 2, 3], a)
1123 assert_equal(@cls[1, 1, 1, 1], a.sort)
1125 assert_equal(@cls[], @cls[].sort)
1129 a = @cls[ 4, 1, 2, 3 ]
1130 assert_equal(@cls[1, 2, 3, 4], a.sort!)
1131 assert_equal(@cls[1, 2, 3, 4], a)
1133 assert_equal(@cls[4, 3, 2, 1], a.sort! { |x, y| y <=> x} )
1134 assert_equal(@cls[4, 3, 2, 1], a)
1137 assert_equal(@cls[1, 1, 1, 1], a.sort!)
1139 assert_equal(@cls[1], @cls[1].sort!)
1140 assert_equal(@cls[], @cls[].sort!)
1143 def test_sort_with_callcc
1144 respond_to?(:callcc, true) or require 'continuation'
1150 callcc {|k| cont = k} unless cont
1151 assert_equal(100, ary.size, '[ruby-core:16679]')
1158 assert_instance_of(RuntimeError, e, '[ruby-core:16679]')
1159 assert_match(/reentered/, e.message, '[ruby-core:16679]')
1162 def test_sort_with_replace
1163 xary = (1..100).to_a
1166 ary.sort! {|a,b| ary.replace(xary); a <=> b}
1168 assert_equal(xary, ary, '[ruby-dev:34732]')
1175 assert_equal(a, a.to_a)
1176 assert_equal(a_id, a.to_a.__id__)
1184 assert_equal(a, b.to_ary)
1186 assert_equal(a_id, a.to_ary.__id__)
1193 assert_equal("[]", a.to_s)
1197 assert_equal("[1, 2]", a.to_s)
1201 assert_equal("[1, 2, 3]", a.to_s)
1205 assert_equal("[1, 2, 3]", a.to_s)
1211 a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
1213 assert_equal(@cls[1, 2, 3, 4, nil], a.uniq)
1216 assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq)
1220 a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
1221 assert_equal(@cls[1, 2, 3, 4, nil], a.uniq!)
1222 assert_equal(@cls[1, 2, 3, 4, nil], a)
1224 assert_nil(@cls[1, 2, 3].uniq!)
1229 assert_equal(@cls['cat'], a.unshift('cat'))
1230 assert_equal(@cls['dog', 'cat'], a.unshift('dog'))
1231 assert_equal(@cls[nil, 'dog', 'cat'], a.unshift(nil))
1232 assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2]))
1236 assert_equal(@cls[], @cls[] | @cls[])
1237 assert_equal(@cls[1], @cls[1] | @cls[])
1238 assert_equal(@cls[1], @cls[] | @cls[1])
1239 assert_equal(@cls[1], @cls[1] | @cls[1])
1241 assert_equal(@cls[1,2], @cls[1] | @cls[2])
1242 assert_equal(@cls[1,2], @cls[1, 1] | @cls[2, 2])
1243 assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
1246 def test_combination
1247 assert_equal(@cls[[]], @cls[1,2,3,4].combination(0).to_a)
1248 assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a)
1249 assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], @cls[1,2,3,4].combination(2).to_a)
1250 assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], @cls[1,2,3,4].combination(3).to_a)
1251 assert_equal(@cls[[1,2,3,4]], @cls[1,2,3,4].combination(4).to_a)
1252 assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a)
1256 assert_equal(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]],
1257 @cls[1,2,3].product([4,5]))
1258 assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @cls[1,2].product([1,2]))
1260 assert_equal(@cls[[1,3,5],[1,3,6],[1,4,5],[1,4,6],
1261 [2,3,5],[2,3,6],[2,4,5],[2,4,6]],
1262 @cls[1,2].product([3,4],[5,6]))
1263 assert_equal(@cls[[1],[2]], @cls[1,2].product)
1264 assert_equal(@cls[], @cls[1,2].product([]))
1267 def test_permutation
1269 assert_equal(@cls[[]], a.permutation(0).to_a)
1270 assert_equal(@cls[[1],[2],[3]], a.permutation(1).to_a.sort)
1271 assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
1272 a.permutation(2).to_a.sort)
1273 assert_equal(@cls[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
1274 a.permutation(3).sort.to_a)
1275 assert_equal(@cls[], a.permutation(4).to_a)
1276 assert_equal(@cls[], a.permutation(-1).to_a)
1277 assert_equal("abcde".each_char.to_a.permutation(5).sort,
1278 "edcba".each_char.to_a.permutation(5).sort)
1279 assert_equal(@cls[].permutation(0).to_a, @cls[[]])
1284 assert_equal([1,2,3], [1,2,3,4,5,0].take(3))
1285 assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].take(-1) }
1286 assert_equal([1,2], [1,2].take(1000000000), '[ruby-dev:34123]')
1290 assert_equal([1,2], [1,2,3,4,5,0].take_while {|i| i < 3 })
1294 assert_equal([4,5,0], [1,2,3,4,5,0].drop(3))
1295 assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].drop(-1) }
1296 assert_equal([], [1,2].drop(1000000000), '[ruby-dev:34123]')
1300 assert_equal([3,4,5,0], [1,2,3,4,5,0].drop_while {|i| i < 3 })
1303 def test_modify_check
1306 assert_raise(RuntimeError) { a.shift }
1308 assert_raise(SecurityError) do
1316 LONGP = [127, 63, 31, 15, 7].map {|x| 2**x-1 }.find do |x|
1319 rescue ArgumentError
1327 assert_raise(ArgumentError) { [].to_enum.first(-1) }
1328 assert_raise(ArgumentError) { [].to_enum.first(LONGP) }
1331 def test_try_convert
1332 assert_equal([1], Array.try_convert([1]))
1333 assert_equal(nil, Array.try_convert("1"))
1337 assert_nothing_raised { [].instance_eval { initialize } }
1338 assert_nothing_raised { Array.new { } }
1339 assert_equal([1, 2, 3], Array.new([1, 2, 3]))
1340 assert_raise(ArgumentError) { Array.new(-1, 1) }
1341 assert_raise(ArgumentError) { Array.new(LONGP, 1) }
1342 assert_equal([1, 1, 1], Array.new(3, 1))
1343 assert_equal([1, 1, 1], Array.new(3) { 1 })
1344 assert_equal([1, 1, 1], Array.new(3, 1) { 1 })
1348 assert_raise(IndexError) { [0][-2] = 1 }
1349 assert_raise(IndexError) { [0][LONGP] = 2 }
1350 assert_raise(IndexError) { [0][(LONGP + 1) / 2 - 1] = 2 }
1353 assert_equal([0, nil, 4], a)
1354 assert_raise(ArgumentError) { [0][0, 0, 0] = 0 }
1358 assert_equal([0], [0].first(2))
1359 assert_raise(ArgumentError) { [0].first(-1) }
1363 assert_equal(0, ([0] * 16).shift)
1368 assert_equal([2, 3], a)
1372 Struct.new(:a, :b, :c)
1376 assert_raise(ArgumentError) { [][0, 0, 0] }
1380 assert_equal(1, [].fetch(0, 0) { 1 })
1381 assert_equal(1, [0, 1].fetch(-1))
1382 assert_raise(IndexError) { [0, 1].fetch(2) }
1383 assert_raise(IndexError) { [0, 1].fetch(-3) }
1384 assert_equal(2, [0, 1].fetch(2, 2))
1389 assert_equal(a, a.index.to_a)
1390 assert_equal(1, a.index {|x| x == 1 })
1395 assert_equal([2, 1, 0], a.rindex.to_a)
1396 assert_equal(1, a.rindex {|x| x == 1 })
1400 assert_equal(1, e.next)
1402 assert_raise(StopIteration) { e.next }
1405 class << o; self; end.class_eval do
1406 define_method(:==) {|x| a.clear; false }
1409 assert_equal(nil, a.rindex(0))
1414 def o.to_ary; [1, 2, 3]; end
1416 assert_equal([1, 2, 3], [a, b, c])
1421 assert_raise(IndexError) { a[-2, 0] = nil }
1426 assert_equal([0], a.insert(1))
1427 assert_equal([0, 1], a.insert(1, 1))
1428 assert_raise(ArgumentError) { a.insert }
1429 assert_equal([0, 1, 2], a.insert(-1, 2))
1430 assert_equal([0, 1, 3, 2], a.insert(-2, 3))
1436 assert_equal("[...]", a.join)
1440 klass = Class.new(Array)
1443 assert_equal(Array, a.class)
1447 a = [0, 1, 2, 3, 4, 5]
1448 assert_equal([1, 2, 3], a.values_at(1..3))
1449 assert_equal([], a.values_at(7..8))
1450 assert_equal([nil], a.values_at(2**31-1))
1454 assert_equal([0, 2], [0, 1, 2, 3].select {|x| x % 2 == 0 })
1458 a = [0] * 1024 + [1] + [0] * 1024
1460 assert_equal([1], a)
1464 assert_equal([1, 3], [0, 1, 2, 3].reject {|x| x % 2 == 0 })
1468 assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]],
1469 [1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]))
1471 [1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]) {|x| a << x }
1472 assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]], a)
1476 assert_equal([[1, :a], [2, :b], [3, :c]],
1477 [[1, 2, 3], [:a, :b, :c]].transpose)
1478 assert_raise(IndexError) { [[1, 2, 3], [:a, :b]].transpose }
1482 assert_equal([], ([0] * 1024).clear)
1486 assert_raise(ArgumentError) { [].fill(0, 1, LONGP) }
1490 assert_raise(ArgumentError) { [0, 0, 0, 0] * ((LONGP + 1) / 4) }
1496 def o.==(x); :foo; end
1497 assert(:foo, [0, 1, 2] == o)
1498 assert([0, 1, 2] != [0, 1, 3])
1506 assert_equal(a.hash, b.hash)
1512 assert_raise(ArgumentError) { a.flatten }
1517 assert_equal([0, 1, 2], [2, 1, 0].shuffle.sort)
1523 assert([0, 1, 2].include?([2, 1, 0].sample))
1524 samples = [2, 1, 0].sample(2)
1525 samples.each{|sample|
1526 assert([0, 1, 2].include?(sample))
1533 [0, 1, 2].cycle do |i|
1535 break if a.size == 10
1537 assert_equal([0, 1, 2, 0, 1, 2, 0, 1, 2, 0], a)
1540 assert_nil(a.cycle { a.clear })
1543 [0, 1, 2].cycle(3) {|i| a << i }
1544 assert_equal([0, 1, 2, 0, 1, 2, 0, 1, 2], a)
1547 def test_reverse_each2
1548 a = [0, 1, 2, 3, 4, 5]
1550 a.reverse_each do |x|
1555 assert_equal([5, 3, 1], r)
1558 def test_combination2
1559 assert_raise(RangeError) do
1560 (0..100).to_a.combination(50) {}
1566 assert_raise(RangeError) do
1567 a.product(a, a, a, a, a, a, a, a, a, a) {}
1571 class Array2 < Array
1574 def test_array_subclass
1575 assert_equal(Array2, Array2[1,2,3].uniq.class, "[ruby-dev:34581]")