Project

General

Profile

Feature #6515 ยป test_array.rb

lellisga (Li Ellis Galardo), 05/30/2012 12:14 PM

 
1
require 'test/unit'
2
require_relative 'envutil'
3

    
4
class TestArray < Test::Unit::TestCase
5
  def setup
6
    @verbose = $VERBOSE
7
    $VERBOSE = nil
8
    @cls = Array
9
  end
10

    
11
  def teardown
12
    $VERBOSE = @verbose
13
  end
14

    
15
  def test_0_literal
16
    assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
17
    assert_equal([1, 2, 1, 2], [1, 2] * 2)
18
    assert_equal("1:2", [1, 2] * ":")
19

    
20
    assert_equal([1, 2].hash, [1, 2].hash)
21

    
22
    assert_equal([2,3], [1,2,3] & [2,3,4])
23
    assert_equal([1,2,3,4], [1,2,3] | [2,3,4])
24
    assert_equal([1,2,3] - [2,3], [1])
25

    
26
    x = [0, 1, 2, 3, 4, 5]
27
    assert_equal(2, x[2])
28
    assert_equal([1, 2, 3], x[1..3])
29
    assert_equal([1, 2, 3], x[1,3])
30

    
31
    x[0, 2] = 10
32
    assert(x[0] == 10 && x[1] == 2)
33

    
34
    x[0, 0] = -1
35
    assert(x[0] == -1 && x[1] == 10)
36

    
37
    x[-1, 1] = 20
38
    assert(x[-1] == 20 && x.pop == 20)
39
  end
40

    
41
  def test_array_andor_0
42
    assert_equal([2], ([1,2,3]&[2,4,6]))
43
    assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6]))
44
  end
45

    
46
  def test_compact_0
47
    a = [nil, 1, nil, nil, 5, nil, nil]
48
    assert_equal [1, 5], a.compact
49
    assert_equal [nil, 1, nil, nil, 5, nil, nil], a
50
    a.compact!
51
    assert_equal [1, 5], a
52
  end
53

    
54
  def test_uniq_0
55
    x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
56
    x.uniq!
57
    assert_equal([1, 4, 2, 5], x)
58
  end
59

    
60
  def test_empty_0
61
    assert_equal true, [].empty?
62
    assert_equal false, [1].empty?
63
    assert_equal false, [1, 1, 4, 2, 5, 4, 5, 1, 2].empty?
64
  end
65

    
66
  def test_sort_0
67
    x = ["it", "came", "to", "pass", "that", "..."]
68
    x = x.sort.join(" ")
69
    assert_equal("... came it pass that to", x)
70
    x = [2,5,3,1,7]
71
    x.sort!{|a,b| a<=>b}		# sort with condition
72
    assert_equal([1,2,3,5,7], x)
73
    x.sort!{|a,b| b-a}		# reverse sort
74
    assert_equal([7,5,3,2,1], x)
75
  end
76

    
77
  def test_split_0
78
    x = "The Book of Mormon"
79
    assert_equal(x.reverse, x.split(//).reverse!.join)
80
    assert_equal(x.reverse, x.reverse!)
81
    assert_equal("g:n:i:r:t:s: :e:t:y:b: :1", "1 byte string".split(//).reverse.join(":"))
82
    x = "a b c  d"
83
    assert_equal(['a', 'b', 'c', 'd'], x.split)
84
    assert_equal(['a', 'b', 'c', 'd'], x.split(' '))
85
  end
86

    
87
  def test_misc_0
88
    assert(defined? "a".chomp)
89
    assert_equal(["a", "b", "c"], "abc".scan(/./))
90
    assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/))
91
    # non-greedy match
92
    assert_equal([["a", "12"], ["b", "22"]], "a=12;b=22".scan(/(.*?)=(\d*);?/))
93

    
94
    x = [1]
95
    assert_equal('1:1:1:1:1', (x * 5).join(":"))
96
    assert_equal('1', (x * 1).join(":"))
97
    assert_equal('', (x * 0).join(":"))
98

    
99
    *x = *(1..7).to_a
100
    assert_equal(7, x.size)
101
    assert_equal([1, 2, 3, 4, 5, 6, 7], x)
102

    
103
    x = [1,2,3]
104
    x[1,0] = x
105
    assert_equal([1,1,2,3,2,3], x)
106

    
107
    x = [1,2,3]
108
    x[-1,0] = x
109
    assert_equal([1,2,1,2,3,3], x)
110

    
111
    x = [1,2,3]
112
    x.concat(x)
113
    assert_equal([1,2,3,1,2,3], x)
114

    
115
    x = [1,2,3]
116
    x.clear
117
    assert_equal([], x)
118

    
119
    x = [1,2,3]
120
    y = x.dup
121
    x << 4
122
    y << 5
123
    assert_equal([1,2,3,4], x)
124
    assert_equal([1,2,3,5], y)
125
  end
126

    
127
  def test_beg_end_0
128
    x = [1, 2, 3, 4, 5]
129

    
130
    assert_equal(1, x.first)
131
    assert_equal([1], x.first(1))
132
    assert_equal([1, 2, 3], x.first(3))
133

    
134
    assert_equal(5, x.last)
135
    assert_equal([5], x.last(1))
136
    assert_equal([3, 4, 5], x.last(3))
137

    
138
    assert_equal(1, x.shift)
139
    assert_equal([2, 3, 4], x.shift(3))
140
    assert_equal([5], x)
141

    
142
    assert_equal([2, 3, 4, 5], x.unshift(2, 3, 4))
143
    assert_equal([1, 2, 3, 4, 5], x.unshift(1))
144
    assert_equal([1, 2, 3, 4, 5], x)
145

    
146
    assert_equal(5, x.pop)
147
    assert_equal([3, 4], x.pop(2))
148
    assert_equal([1, 2], x)
149

    
150
    assert_equal([1, 2, 3, 4], x.push(3, 4))
151
    assert_equal([1, 2, 3, 4, 5], x.push(5))
152
    assert_equal([1, 2, 3, 4, 5], x)
153
  end
154

    
155
  def test_find_all_0
156
    assert_respond_to([], :find_all)
157
    assert_respond_to([], :select)       # Alias
158
    assert_equal([], [].find_all{ |obj| obj == "foo"})
159

    
160
    x = ["foo", "bar", "baz", "baz", 1, 2, 3, 3, 4]
161
    assert_equal(["baz","baz"], x.find_all{ |obj| obj == "baz" })
162
    assert_equal([3,3], x.find_all{ |obj| obj == 3 })
163
  end
164

    
165
  def test_fill_0
166
    assert_equal([-1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1))
167
    assert_equal([0, 1, 2, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3))
168
    assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3, 2))
169
    assert_equal([0, 1, 2, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3, 5))
170
    assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2, 2))
171
    assert_equal([0, 1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 2, 5))
172
    assert_equal([0, 1, 2, 3, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, -2, 1))
173
    assert_equal([0, 1, 2, 3, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, -2, 3))
174
    assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3..4))
175
    assert_equal([0, 1, 2, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3...4))
176
    assert_equal([0, 1, -1, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2..-2))
177
    assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2...-2))
178
    assert_equal([10, 11, 12, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill{|i| i+10})
179
    assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3){|i| i+10})
180
    assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3, 2){|i| i+10})
181
    assert_equal([0, 1, 2, 13, 14, 15, 16, 17], [0, 1, 2, 3, 4, 5].fill(3, 5){|i| i+10})
182
    assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3..4){|i| i+10})
183
    assert_equal([0, 1, 2, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(3...4){|i| i+10})
184
    assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10})
185
    assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10})
186
  end
187

    
188
  # From rubicon
189

    
190
  def test_00_new
191
    a = @cls.new()
192
    assert_instance_of(@cls, a)
193
    assert_equal(0, a.length)
194
    assert_nil(a[0])
195
  end
196

    
197
  def test_01_square_brackets
198
    a = @cls[ 5, 4, 3, 2, 1 ]
199
    assert_instance_of(@cls, a)
200
    assert_equal(5, a.length)
201
    5.times { |i| assert_equal(5-i, a[i]) }
202
    assert_nil(a[6])
203
  end
204

    
205
  def test_AND # '&'
206
    assert_equal(@cls[1, 3], @cls[ 1, 1, 3, 5 ] & @cls[ 1, 2, 3 ])
207
    assert_equal(@cls[],     @cls[ 1, 1, 3, 5 ] & @cls[ ])
208
    assert_equal(@cls[],     @cls[  ]           & @cls[ 1, 2, 3 ])
209
    assert_equal(@cls[],     @cls[ 1, 2, 3 ]    & @cls[ 4, 5, 6 ])
210
  end
211

    
212
  def test_MUL # '*'
213
    assert_equal(@cls[], @cls[]*3)
214
    assert_equal(@cls[1, 1, 1], @cls[1]*3)
215
    assert_equal(@cls[1, 2, 1, 2, 1, 2], @cls[1, 2]*3)
216
    assert_equal(@cls[], @cls[1, 2, 3] * 0)
217
    assert_raise(ArgumentError) { @cls[1, 2]*(-3) }
218

    
219
    assert_equal('1-2-3-4-5', @cls[1, 2, 3, 4, 5] * '-')
220
    assert_equal('12345',     @cls[1, 2, 3, 4, 5] * '')
221

    
222
  end
223

    
224
  def test_PLUS # '+'
225
    assert_equal(@cls[],     @cls[]  + @cls[])
226
    assert_equal(@cls[1],    @cls[1] + @cls[])
227
    assert_equal(@cls[1],    @cls[]  + @cls[1])
228
    assert_equal(@cls[1, 1], @cls[1] + @cls[1])
229
    assert_equal(@cls['cat', 'dog', 1, 2, 3], %w(cat dog) + (1..3).to_a)
230
  end
231

    
232
  def test_MINUS # '-'
233
    assert_equal(@cls[],  @cls[1] - @cls[1])
234
    assert_equal(@cls[1], @cls[1, 2, 3, 4, 5] - @cls[2, 3, 4, 5])
235
    # Ruby 1.8 feature change
236
    #assert_equal(@cls[1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5])
237
    assert_equal(@cls[1, 1, 1, 1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5])
238
    a = @cls[]
239
    1000.times { a << 1 }
240
    assert_equal(1000, a.length)
241
    #assert_equal(@cls[1], a - @cls[2])
242
    assert_equal(@cls[1] * 1000, a - @cls[2])
243
    #assert_equal(@cls[1],  @cls[1, 2, 1] - @cls[2])
244
    assert_equal(@cls[1, 1],  @cls[1, 2, 1] - @cls[2])
245
    assert_equal(@cls[1, 2, 3], @cls[1, 2, 3] - @cls[4, 5, 6])
246
  end
247

    
248
  def test_LSHIFT # '<<'
249
    a = @cls[]
250
    a << 1
251
    assert_equal(@cls[1], a)
252
    a << 2 << 3
253
    assert_equal(@cls[1, 2, 3], a)
254
    a << nil << 'cat'
255
    assert_equal(@cls[1, 2, 3, nil, 'cat'], a)
256
    a << a
257
    assert_equal(@cls[1, 2, 3, nil, 'cat', a], a)
258
  end
259

    
260
  def test_CMP # '<=>'
261
    assert_equal(0,  @cls[] <=> @cls[])
262
    assert_equal(0,  @cls[1] <=> @cls[1])
263
    assert_equal(0,  @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'cat'])
264
    assert_equal(-1, @cls[] <=> @cls[1])
265
    assert_equal(1,  @cls[1] <=> @cls[])
266
    assert_equal(-1, @cls[1, 2, 3] <=> @cls[1, 2, 3, 'cat'])
267
    assert_equal(1,  @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3])
268
    assert_equal(-1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'dog'])
269
    assert_equal(1,  @cls[1, 2, 3, 'dog'] <=> @cls[1, 2, 3, 'cat'])
270
  end
271

    
272
  def test_EQUAL # '=='
273
    assert(@cls[] == @cls[])
274
    assert(@cls[1] == @cls[1])
275
    assert(@cls[1, 1, 2, 2] == @cls[1, 1, 2, 2])
276
    assert(@cls[1.0, 1.0, 2.0, 2.0] == @cls[1, 1, 2, 2])
277
  end
278

    
279
  def test_VERY_EQUAL # '==='
280
    assert(@cls[] === @cls[])
281
    assert(@cls[1] === @cls[1])
282
    assert(@cls[1, 1, 2, 2] === @cls[1, 1, 2, 2])
283
    assert(@cls[1.0, 1.0, 2.0, 2.0] === @cls[1, 1, 2, 2])
284
  end
285

    
286
  def test_AREF # '[]'
287
    a = @cls[*(1..100).to_a]
288

    
289
    assert_equal(1, a[0])
290
    assert_equal(100, a[99])
291
    assert_nil(a[100])
292
    assert_equal(100, a[-1])
293
    assert_equal(99,  a[-2])
294
    assert_equal(1,   a[-100])
295
    assert_nil(a[-101])
296
    assert_nil(a[-101,0])
297
    assert_nil(a[-101,1])
298
    assert_nil(a[-101,-1])
299
    assert_nil(a[10,-1])
300

    
301
    assert_equal(@cls[1],   a[0,1])
302
    assert_equal(@cls[100], a[99,1])
303
    assert_equal(@cls[],    a[100,1])
304
    assert_equal(@cls[100], a[99,100])
305
    assert_equal(@cls[100], a[-1,1])
306
    assert_equal(@cls[99],  a[-2,1])
307
    assert_equal(@cls[],    a[-100,0])
308
    assert_equal(@cls[1],   a[-100,1])
309

    
310
    assert_equal(@cls[10, 11, 12], a[9, 3])
311
    assert_equal(@cls[10, 11, 12], a[-91, 3])
312

    
313
    assert_equal(@cls[1],   a[0..0])
314
    assert_equal(@cls[100], a[99..99])
315
    assert_equal(@cls[],    a[100..100])
316
    assert_equal(@cls[100], a[99..200])
317
    assert_equal(@cls[100], a[-1..-1])
318
    assert_equal(@cls[99],  a[-2..-2])
319

    
320
    assert_equal(@cls[10, 11, 12], a[9..11])
321
    assert_equal(@cls[10, 11, 12], a[-91..-89])
322

    
323
    assert_nil(a[10, -3])
324
    # Ruby 1.8 feature change:
325
    # Array#[size..x] returns [] instead of nil.
326
    #assert_nil(a[10..7])
327
    assert_equal [], a[10..7]
328

    
329
    assert_raise(TypeError) {a['cat']}
330
  end
331

    
332
  def test_ASET # '[]='
333
    a = @cls[*(0..99).to_a]
334
    assert_equal(0, a[0] = 0)
335
    assert_equal(@cls[0] + @cls[*(1..99).to_a], a)
336

    
337
    a = @cls[*(0..99).to_a]
338
    assert_equal(0, a[10,10] = 0)
339
    assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
340

    
341
    a = @cls[*(0..99).to_a]
342
    assert_equal(0, a[-1] = 0)
343
    assert_equal(@cls[*(0..98).to_a] + @cls[0], a)
344

    
345
    a = @cls[*(0..99).to_a]
346
    assert_equal(0, a[-10, 10] = 0)
347
    assert_equal(@cls[*(0..89).to_a] + @cls[0], a)
348

    
349
    a = @cls[*(0..99).to_a]
350
    assert_equal(0, a[0,1000] = 0)
351
    assert_equal(@cls[0] , a)
352

    
353
    a = @cls[*(0..99).to_a]
354
    assert_equal(0, a[10..19] = 0)
355
    assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
356

    
357
    b = @cls[*%w( a b c )]
358
    a = @cls[*(0..99).to_a]
359
    assert_equal(b, a[0,1] = b)
360
    assert_equal(b + @cls[*(1..99).to_a], a)
361

    
362
    a = @cls[*(0..99).to_a]
363
    assert_equal(b, a[10,10] = b)
364
    assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
365

    
366
    a = @cls[*(0..99).to_a]
367
    assert_equal(b, a[-1, 1] = b)
368
    assert_equal(@cls[*(0..98).to_a] + b, a)
369

    
370
    a = @cls[*(0..99).to_a]
371
    assert_equal(b, a[-10, 10] = b)
372
    assert_equal(@cls[*(0..89).to_a] + b, a)
373

    
374
    a = @cls[*(0..99).to_a]
375
    assert_equal(b, a[0,1000] = b)
376
    assert_equal(b , a)
377

    
378
    a = @cls[*(0..99).to_a]
379
    assert_equal(b, a[10..19] = b)
380
    assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
381

    
382
    # Ruby 1.8 feature change:
383
    # assigning nil does not remove elements.
384
=begin
385
    a = @cls[*(0..99).to_a]
386
    assert_equal(nil, a[0,1] = nil)
387
    assert_equal(@cls[*(1..99).to_a], a)
388

    
389
    a = @cls[*(0..99).to_a]
390
    assert_equal(nil, a[10,10] = nil)
391
    assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
392

    
393
    a = @cls[*(0..99).to_a]
394
    assert_equal(nil, a[-1, 1] = nil)
395
    assert_equal(@cls[*(0..98).to_a], a)
396

    
397
    a = @cls[*(0..99).to_a]
398
    assert_equal(nil, a[-10, 10] = nil)
399
    assert_equal(@cls[*(0..89).to_a], a)
400

    
401
    a = @cls[*(0..99).to_a]
402
    assert_equal(nil, a[0,1000] = nil)
403
    assert_equal(@cls[] , a)
404

    
405
    a = @cls[*(0..99).to_a]
406
    assert_equal(nil, a[10..19] = nil)
407
    assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
408
=end
409

    
410
    a = @cls[1, 2, 3]
411
    a[1, 0] = a
412
    assert_equal([1, 1, 2, 3, 2, 3], a)
413

    
414
    a = @cls[1, 2, 3]
415
    a[-1, 0] = a
416
    assert_equal([1, 2, 1, 2, 3, 3], a)
417
  end
418

    
419
  def test_assoc
420
    a1 = @cls[*%w( cat feline )]
421
    a2 = @cls[*%w( dog canine )]
422
    a3 = @cls[*%w( mule asinine )]
423

    
424
    a = @cls[ a1, a2, a3 ]
425

    
426
    assert_equal(a1, a.assoc('cat'))
427
    assert_equal(a3, a.assoc('mule'))
428
    assert_equal(nil, a.assoc('asinine'))
429
    assert_equal(nil, a.assoc('wombat'))
430
    assert_equal(nil, a.assoc(1..2))
431
  end
432

    
433
  def test_at
434
    a = @cls[*(0..99).to_a]
435
    assert_equal(0,   a.at(0))
436
    assert_equal(10,  a.at(10))
437
    assert_equal(99,  a.at(99))
438
    assert_equal(nil, a.at(100))
439
    assert_equal(99,  a.at(-1))
440
    assert_equal(0,  a.at(-100))
441
    assert_equal(nil, a.at(-101))
442
    assert_raise(TypeError) { a.at('cat') }
443
  end
444

    
445
  def test_clear
446
    a = @cls[1, 2, 3]
447
    b = a.clear
448
    assert_equal(@cls[], a)
449
    assert_equal(@cls[], b)
450
    assert_equal(a.__id__, b.__id__)
451
  end
452

    
453
  def test_clone
454
    for taint in [ false, true ]
455
      for untrust in [ false, true ]
456
        for frozen in [ false, true ]
457
          a = @cls[*(0..99).to_a]
458
          a.taint  if taint
459
          a.untrust  if untrust
460
          a.freeze if frozen
461
          b = a.clone
462

    
463
          assert_equal(a, b)
464
          assert(a.__id__ != b.__id__)
465
          assert_equal(a.frozen?, b.frozen?)
466
          assert_equal(a.untrusted?, b.untrusted?)
467
          assert_equal(a.tainted?, b.tainted?)
468
        end
469
      end
470
    end
471
  end
472

    
473
  def test_collect
474
    a = @cls[ 1, 'cat', 1..1 ]
475
    assert_equal([ Fixnum, String, Range], a.collect {|e| e.class} )
476
    assert_equal([ 99, 99, 99], a.collect { 99 } )
477

    
478
    assert_equal([], @cls[].collect { 99 })
479

    
480
    # Ruby 1.9 feature change:
481
    # Enumerable#collect without block returns an Enumerator.
482
    #assert_equal([1, 2, 3], @cls[1, 2, 3].collect)
483
    assert_kind_of Enumerator, @cls[1, 2, 3].collect
484
  end
485

    
486
  # also update map!
487
  def test_collect!
488
    a = @cls[ 1, 'cat', 1..1 ]
489
    assert_equal([ Fixnum, String, Range], a.collect! {|e| e.class} )
490
    assert_equal([ Fixnum, String, Range], a)
491

    
492
    a = @cls[ 1, 'cat', 1..1 ]
493
    assert_equal([ 99, 99, 99], a.collect! { 99 } )
494
    assert_equal([ 99, 99, 99], a)
495

    
496
    a = @cls[ ]
497
    assert_equal([], a.collect! { 99 })
498
    assert_equal([], a)
499
  end
500

    
501
  def test_compact
502
    a = @cls[ 1, nil, nil, 2, 3, nil, 4 ]
503
    assert_equal(@cls[1, 2, 3, 4], a.compact)
504

    
505
    a = @cls[ nil, 1, nil, 2, 3, nil, 4 ]
506
    assert_equal(@cls[1, 2, 3, 4], a.compact)
507

    
508
    a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ]
509
    assert_equal(@cls[1, 2, 3, 4], a.compact)
510

    
511
    a = @cls[ 1, 2, 3, 4 ]
512
    assert_equal(@cls[1, 2, 3, 4], a.compact)
513
  end
514

    
515
  def test_compact!
516
    a = @cls[ 1, nil, nil, 2, 3, nil, 4 ]
517
    assert_equal(@cls[1, 2, 3, 4], a.compact!)
518
    assert_equal(@cls[1, 2, 3, 4], a)
519

    
520
    a = @cls[ nil, 1, nil, 2, 3, nil, 4 ]
521
    assert_equal(@cls[1, 2, 3, 4], a.compact!)
522
    assert_equal(@cls[1, 2, 3, 4], a)
523

    
524
    a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ]
525
    assert_equal(@cls[1, 2, 3, 4], a.compact!)
526
    assert_equal(@cls[1, 2, 3, 4], a)
527

    
528
    a = @cls[ 1, 2, 3, 4 ]
529
    assert_equal(nil, a.compact!)
530
    assert_equal(@cls[1, 2, 3, 4], a)
531
  end
532

    
533
  def test_concat
534
    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2].concat(@cls[3, 4]))
535
    assert_equal(@cls[1, 2, 3, 4],     @cls[].concat(@cls[1, 2, 3, 4]))
536
    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2, 3, 4].concat(@cls[]))
537
    assert_equal(@cls[],               @cls[].concat(@cls[]))
538
    assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]]))
539

    
540
    a = @cls[1, 2, 3]
541
    a.concat(a)
542
    assert_equal([1, 2, 3, 1, 2, 3], a)
543

    
544
    assert_raise(TypeError) { [0].concat(:foo) }
545
    assert_raise(RuntimeError) { [0].freeze.concat(:foo) }
546
  end
547

    
548
  def test_count
549
    a = @cls[1, 2, 3, 1, 2]
550
    assert_equal(5, a.count)
551
    assert_equal(2, a.count(1))
552
    assert_equal(3, a.count {|x| x % 2 == 1 })
553
    assert_equal(2, a.count(1) {|x| x % 2 == 1 })
554
    assert_raise(ArgumentError) { a.count(0, 1) }
555
  end
556

    
557
  def test_delete
558
    a = @cls[*('cab'..'cat').to_a]
559
    assert_equal('cap', a.delete('cap'))
560
    assert_equal(@cls[*('cab'..'cao').to_a] + @cls[*('caq'..'cat').to_a], a)
561

    
562
    a = @cls[*('cab'..'cat').to_a]
563
    assert_equal('cab', a.delete('cab'))
564
    assert_equal(@cls[*('cac'..'cat').to_a], a)
565

    
566
    a = @cls[*('cab'..'cat').to_a]
567
    assert_equal('cat', a.delete('cat'))
568
    assert_equal(@cls[*('cab'..'cas').to_a], a)
569

    
570
    a = @cls[*('cab'..'cat').to_a]
571
    assert_equal(nil, a.delete('cup'))
572
    assert_equal(@cls[*('cab'..'cat').to_a], a)
573

    
574
    a = @cls[*('cab'..'cat').to_a]
575
    assert_equal(99, a.delete('cup') { 99 } )
576
    assert_equal(@cls[*('cab'..'cat').to_a], a)
577
  end
578

    
579
  def test_delete_at
580
    a = @cls[*(1..5).to_a]
581
    assert_equal(3, a.delete_at(2))
582
    assert_equal(@cls[1, 2, 4, 5], a)
583

    
584
    a = @cls[*(1..5).to_a]
585
    assert_equal(4, a.delete_at(-2))
586
    assert_equal(@cls[1, 2, 3, 5], a)
587

    
588
    a = @cls[*(1..5).to_a]
589
    assert_equal(nil, a.delete_at(5))
590
    assert_equal(@cls[1, 2, 3, 4, 5], a)
591

    
592
    a = @cls[*(1..5).to_a]
593
    assert_equal(nil, a.delete_at(-6))
594
    assert_equal(@cls[1, 2, 3, 4, 5], a)
595
  end
596

    
597
  # also reject!
598
  def test_delete_if
599
    a = @cls[ 1, 2, 3, 4, 5 ]
600
    assert_equal(a, a.delete_if { false })
601
    assert_equal(@cls[1, 2, 3, 4, 5], a)
602

    
603
    a = @cls[ 1, 2, 3, 4, 5 ]
604
    assert_equal(a, a.delete_if { true })
605
    assert_equal(@cls[], a)
606

    
607
    a = @cls[ 1, 2, 3, 4, 5 ]
608
    assert_equal(a, a.delete_if { |i| i > 3 })
609
    assert_equal(@cls[1, 2, 3], a)
610

    
611
    bug2545 = '[ruby-core:27366]'
612
    a = @cls[ 5, 6, 7, 8, 9, 10 ]
613
    assert_equal(9, a.delete_if {|i| break i if i > 8; assert_equal(a[0], i) || true if i < 7})
614
    assert_equal(@cls[7, 8, 9, 10], a, bug2545)
615
  end
616

    
617
  def test_dup
618
    for taint in [ false, true ]
619
      for frozen in [ false, true ]
620
        a = @cls[*(0..99).to_a]
621
        a.taint  if taint
622
        a.freeze if frozen
623
        b = a.dup
624

    
625
        assert_equal(a, b)
626
        assert(a.__id__ != b.__id__)
627
        assert_equal(false, b.frozen?)
628
        assert_equal(a.tainted?, b.tainted?)
629
      end
630
    end
631
  end
632

    
633
  def test_each
634
    a = @cls[*%w( ant bat cat dog )]
635
    i = 0
636
    a.each { |e|
637
      assert_equal(a[i], e)
638
      i += 1
639
    }
640
    assert_equal(4, i)
641

    
642
    a = @cls[]
643
    i = 0
644
    a.each { |e|
645
      assert_equal(a[i], e)
646
      i += 1
647
    }
648
    assert_equal(0, i)
649

    
650
    assert_equal(a, a.each {})
651
  end
652

    
653
  def test_each_index
654
    a = @cls[*%w( ant bat cat dog )]
655
    i = 0
656
    a.each_index { |ind|
657
      assert_equal(i, ind)
658
      i += 1
659
    }
660
    assert_equal(4, i)
661

    
662
    a = @cls[]
663
    i = 0
664
    a.each_index { |ind|
665
      assert_equal(i, ind)
666
      i += 1
667
    }
668
    assert_equal(0, i)
669

    
670
    assert_equal(a, a.each_index {})
671
  end
672

    
673
  def test_empty?
674
    assert(@cls[].empty?)
675
    assert(!@cls[1].empty?)
676
  end
677

    
678
  def test_eql?
679
    assert(@cls[].eql?(@cls[]))
680
    assert(@cls[1].eql?(@cls[1]))
681
    assert(@cls[1, 1, 2, 2].eql?(@cls[1, 1, 2, 2]))
682
    assert(!@cls[1.0, 1.0, 2.0, 2.0].eql?(@cls[1, 1, 2, 2]))
683
  end
684

    
685
  def test_fill
686
    assert_equal(@cls[],   @cls[].fill(99))
687
    assert_equal(@cls[],   @cls[].fill(99, 0))
688
    assert_equal(@cls[99], @cls[].fill(99, 0, 1))
689
    assert_equal(@cls[99], @cls[].fill(99, 0..0))
690

    
691
    assert_equal(@cls[99],   @cls[1].fill(99))
692
    assert_equal(@cls[99],   @cls[1].fill(99, 0))
693
    assert_equal(@cls[99],   @cls[1].fill(99, 0, 1))
694
    assert_equal(@cls[99],   @cls[1].fill(99, 0..0))
695

    
696
    assert_equal(@cls[99, 99], @cls[1, 2].fill(99))
697
    assert_equal(@cls[99, 99], @cls[1, 2].fill(99, 0))
698
    assert_equal(@cls[99, 99], @cls[1, 2].fill(99, nil))
699
    assert_equal(@cls[1,  99], @cls[1, 2].fill(99, 1, nil))
700
    assert_equal(@cls[99,  2], @cls[1, 2].fill(99, 0, 1))
701
    assert_equal(@cls[99,  2], @cls[1, 2].fill(99, 0..0))
702
  end
703

    
704
  def test_first
705
    assert_equal(3,   @cls[3, 4, 5].first)
706
    assert_equal(nil, @cls[].first)
707
  end
708

    
709
  def test_flatten
710
    a1 = @cls[ 1, 2, 3]
711
    a2 = @cls[ 5, 6 ]
712
    a3 = @cls[ 4, a2 ]
713
    a4 = @cls[ a1, a3 ]
714
    assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten)
715
    assert_equal(@cls[ a1, a3], a4)
716

    
717
    a5 = @cls[ a1, @cls[], a3 ]
718
    assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten)
719
    assert_equal(@cls[], @cls[].flatten)
720
    assert_equal(@cls[],
721
                 @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
722

    
723
    assert_raise(TypeError, "[ruby-dev:31197]") { [[]].flatten("") }
724

    
725
    a6 = @cls[[1, 2], 3]
726
    a6.taint
727
    a6.untrust
728
    a7 = a6.flatten
729
    assert_equal(true, a7.tainted?)
730
    assert_equal(true, a7.untrusted?)
731

    
732
    a8 = @cls[[1, 2], 3]
733
    a9 = a8.flatten(0)
734
    assert_equal(a8, a9)
735
    assert_not_same(a8, a9)
736
  end
737

    
738
  def test_flatten!
739
    a1 = @cls[ 1, 2, 3]
740
    a2 = @cls[ 5, 6 ]
741
    a3 = @cls[ 4, a2 ]
742
    a4 = @cls[ a1, a3 ]
743
    assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten!)
744
    assert_equal(@cls[1, 2, 3, 4, 5, 6], a4)
745

    
746
    a5 = @cls[ a1, @cls[], a3 ]
747
    assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten!)
748
    assert_nil(a5.flatten!(0), '[ruby-core:23382]')
749
    assert_equal(@cls[1, 2, 3, 4, 5, 6], a5)
750

    
751
    assert_equal(@cls[], @cls[].flatten)
752
    assert_equal(@cls[],
753
                 @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
754

    
755
    assert_nil(@cls[].flatten!(0), '[ruby-core:23382]')
756
  end
757

    
758
  def test_flatten_with_callcc
759
    respond_to?(:callcc, true) or require 'continuation'
760
    o = Object.new
761
    def o.to_ary() callcc {|k| @cont = k; [1,2,3]} end
762
    begin
763
      assert_equal([10, 20, 1, 2, 3, 30, 1, 2, 3, 40], [10, 20, o, 30, o, 40].flatten)
764
    rescue => e
765
    else
766
      o.instance_eval {@cont}.call
767
    end
768
    assert_instance_of(RuntimeError, e, '[ruby-dev:34798]')
769
    assert_match(/reentered/, e.message, '[ruby-dev:34798]')
770
  end
771

    
772
  def test_permutation_with_callcc
773
    respond_to?(:callcc, true) or require 'continuation'
774
    n = 1000
775
    cont = nil
776
    ary = [1,2,3]
777
    begin
778
      ary.permutation {
779
        callcc {|k| cont = k} unless cont
780
      }
781
    rescue => e
782
    end
783
    n -= 1
784
    cont.call if 0 < n
785
    assert_instance_of(RuntimeError, e)
786
    assert_match(/reentered/, e.message)
787
  end
788

    
789
  def test_product_with_callcc
790
    respond_to?(:callcc, true) or require 'continuation'
791
    n = 1000
792
    cont = nil
793
    ary = [1,2,3]
794
    begin
795
      ary.product {
796
        callcc {|k| cont = k} unless cont
797
      }
798
    rescue => e
799
    end
800
    n -= 1
801
    cont.call if 0 < n
802
    assert_instance_of(RuntimeError, e)
803
    assert_match(/reentered/, e.message)
804
  end
805

    
806
  def test_combination_with_callcc
807
    respond_to?(:callcc, true) or require 'continuation'
808
    n = 1000
809
    cont = nil
810
    ary = [1,2,3]
811
    begin
812
      ary.combination(2) {
813
        callcc {|k| cont = k} unless cont
814
      }
815
    rescue => e
816
    end
817
    n -= 1
818
    cont.call if 0 < n
819
    assert_instance_of(RuntimeError, e)
820
    assert_match(/reentered/, e.message)
821
  end
822

    
823
  def test_repeated_permutation_with_callcc
824
    respond_to?(:callcc, true) or require 'continuation'
825
    n = 1000
826
    cont = nil
827
    ary = [1,2,3]
828
    begin
829
      ary.repeated_permutation(2) {
830
        callcc {|k| cont = k} unless cont
831
      }
832
    rescue => e
833
    end
834
    n -= 1
835
    cont.call if 0 < n
836
    assert_instance_of(RuntimeError, e)
837
    assert_match(/reentered/, e.message)
838
  end
839

    
840
  def test_repeated_combination_with_callcc
841
    respond_to?(:callcc, true) or require 'continuation'
842
    n = 1000
843
    cont = nil
844
    ary = [1,2,3]
845
    begin
846
      ary.repeated_combination(2) {
847
        callcc {|k| cont = k} unless cont
848
      }
849
    rescue => e
850
    end
851
    n -= 1
852
    cont.call if 0 < n
853
    assert_instance_of(RuntimeError, e)
854
    assert_match(/reentered/, e.message)
855
  end
856

    
857
  def test_hash
858
    a1 = @cls[ 'cat', 'dog' ]
859
    a2 = @cls[ 'cat', 'dog' ]
860
    a3 = @cls[ 'dog', 'cat' ]
861
    assert(a1.hash == a2.hash)
862
    assert(a1.hash != a3.hash)
863
  end
864

    
865
  def test_include?
866
    a = @cls[ 'cat', 99, /a/, @cls[ 1, 2, 3] ]
867
    assert(a.include?('cat'))
868
    assert(a.include?(99))
869
    assert(a.include?(/a/))
870
    assert(a.include?([1,2,3]))
871
    assert(!a.include?('ca'))
872
    assert(!a.include?([1,2]))
873
  end
874

    
875
  def test_index
876
    a = @cls[ 'cat', 99, /a/, 99, @cls[ 1, 2, 3] ]
877
    assert_equal(0, a.index('cat'))
878
    assert_equal(1, a.index(99))
879
    assert_equal(4, a.index([1,2,3]))
880
    assert_nil(a.index('ca'))
881
    assert_nil(a.index([1,2]))
882

    
883
    assert_equal(1, a.index(99) {|x| x == 'cat' })
884
  end
885

    
886
  def test_values_at
887
    a = @cls[*('a'..'j').to_a]
888
    assert_equal(@cls['a', 'c', 'e'], a.values_at(0, 2, 4))
889
    assert_equal(@cls['j', 'h', 'f'], a.values_at(-1, -3, -5))
890
    assert_equal(@cls['h', nil, 'a'], a.values_at(-3, 99, 0))
891
  end
892

    
893
  def test_join
894
    $, = ""
895
    a = @cls[]
896
    assert_equal("", a.join)
897
    assert_equal("", a.join(','))
898
    assert_equal(Encoding::US_ASCII, a.join.encoding)
899

    
900
    $, = ""
901
    a = @cls[1, 2]
902
    assert_equal("12", a.join)
903
    assert_equal("1,2", a.join(','))
904

    
905
    $, = ""
906
    a = @cls[1, 2, 3]
907
    assert_equal("123", a.join)
908
    assert_equal("1,2,3", a.join(','))
909

    
910
    $, = ":"
911
    a = @cls[1, 2, 3]
912
    assert_equal("1:2:3", a.join)
913
    assert_equal("1,2,3", a.join(','))
914

    
915
    $, = ""
916
    a = @cls[1, 2, 3]
917
    a.taint
918
    a.untrust
919
    s = a.join
920
    assert_equal(true, s.tainted?)
921
    assert_equal(true, s.untrusted?)
922

    
923
    e = ''.force_encoding('EUC-JP')
924
    u = ''.force_encoding('UTF-8')
925
    assert_equal(Encoding::US_ASCII, [[]].join.encoding)
926
    assert_equal(Encoding::US_ASCII, [1, [u]].join.encoding)
927
    assert_equal(Encoding::UTF_8, [u, [e]].join.encoding)
928
    assert_equal(Encoding::UTF_8, [u, [1]].join.encoding)
929
    bug5379 = '[ruby-core:39776]'
930
    assert_equal(Encoding::US_ASCII, [[], u, nil].join.encoding, bug5379)
931
    assert_equal(Encoding::UTF_8, [[], "\u3042", nil].join.encoding, bug5379)
932
  ensure
933
    $, = nil
934
  end
935

    
936
  def test_last
937
    assert_equal(nil, @cls[].last)
938
    assert_equal(1, @cls[1].last)
939
    assert_equal(99, @cls[*(3..99).to_a].last)
940
  end
941

    
942
  def test_length
943
    assert_equal(0, @cls[].length)
944
    assert_equal(1, @cls[1].length)
945
    assert_equal(2, @cls[1, nil].length)
946
    assert_equal(2, @cls[nil, 1].length)
947
    assert_equal(234, @cls[*(0..233).to_a].length)
948
  end
949

    
950
  # also update collect!
951
  def test_map!
952
    a = @cls[ 1, 'cat', 1..1 ]
953
    assert_equal(@cls[ Fixnum, String, Range], a.map! {|e| e.class} )
954
    assert_equal(@cls[ Fixnum, String, Range], a)
955

    
956
    a = @cls[ 1, 'cat', 1..1 ]
957
    assert_equal(@cls[ 99, 99, 99], a.map! { 99 } )
958
    assert_equal(@cls[ 99, 99, 99], a)
959

    
960
    a = @cls[ ]
961
    assert_equal(@cls[], a.map! { 99 })
962
    assert_equal(@cls[], a)
963
  end
964

    
965
  def test_pack
966
    a = @cls[*%w( cat wombat x yy)]
967
    assert_equal("catwomx  yy ", a.pack("A3A3A3A3"))
968
    assert_equal("cat", a.pack("A*"))
969
    assert_equal("cwx  yy ", a.pack("A3@1A3@2A3A3"))
970
    assert_equal("catwomx\000\000yy\000", a.pack("a3a3a3a3"))
971
    assert_equal("cat", a.pack("a*"))
972
    assert_equal("ca", a.pack("a2"))
973
    assert_equal("cat\000\000", a.pack("a5"))
974

    
975
    assert_equal("\x61",     @cls["01100001"].pack("B8"))
976
    assert_equal("\x61",     @cls["01100001"].pack("B*"))
977
    assert_equal("\x61",     @cls["0110000100110111"].pack("B8"))
978
    assert_equal("\x61\x37", @cls["0110000100110111"].pack("B16"))
979
    assert_equal("\x61\x37", @cls["01100001", "00110111"].pack("B8B8"))
980
    assert_equal("\x60",     @cls["01100001"].pack("B4"))
981
    assert_equal("\x40",     @cls["01100001"].pack("B2"))
982

    
983
    assert_equal("\x86",     @cls["01100001"].pack("b8"))
984
    assert_equal("\x86",     @cls["01100001"].pack("b*"))
985
    assert_equal("\x86",     @cls["0110000100110111"].pack("b8"))
986
    assert_equal("\x86\xec", @cls["0110000100110111"].pack("b16"))
987
    assert_equal("\x86\xec", @cls["01100001", "00110111"].pack("b8b8"))
988
    assert_equal("\x06",     @cls["01100001"].pack("b4"))
989
    assert_equal("\x02",     @cls["01100001"].pack("b2"))
990

    
991
    assert_equal("ABC",      @cls[ 65, 66, 67 ].pack("C3"))
992
    assert_equal("\377BC",   @cls[ -1, 66, 67 ].pack("C*"))
993
    assert_equal("ABC",      @cls[ 65, 66, 67 ].pack("c3"))
994
    assert_equal("\377BC",   @cls[ -1, 66, 67 ].pack("c*"))
995

    
996

    
997
    assert_equal("AB\n\x10",  @cls["4142", "0a", "12"].pack("H4H2H1"))
998
    assert_equal("AB\n\x02",  @cls["1424", "a0", "21"].pack("h4h2h1"))
999

    
1000
    assert_equal("abc=02def=\ncat=\n=01=\n",
1001
                 @cls["abc\002def", "cat", "\001"].pack("M9M3M4"))
1002

    
1003
    assert_equal("aGVsbG8K\n",  @cls["hello\n"].pack("m"))
1004
    assert_equal(",:&5L;&\\*:&5L;&\\*\n",  @cls["hello\nhello\n"].pack("u"))
1005

    
1006
    assert_equal("\u{a9 42 2260}", @cls[0xa9, 0x42, 0x2260].pack("U*"))
1007

    
1008

    
1009
    format = "c2x5CCxsdils_l_a6";
1010
    # Need the expression in here to force ary[5] to be numeric.  This avoids
1011
    # test2 failing because ary2 goes str->numeric->str and ary does not.
1012
    ary = [1, -100, 127, 128, 32767, 987.654321098/100.0,
1013
      12345, 123456, -32767, -123456, "abcdef"]
1014
    x    = ary.pack(format)
1015
    ary2 = x.unpack(format)
1016

    
1017
    assert_equal(ary.length, ary2.length)
1018
    assert_equal(ary.join(':'), ary2.join(':'))
1019
    assert_not_nil(x =~ /def/)
1020

    
1021
=begin
1022
    skipping "Not tested:
1023
        D,d & double-precision float, native format\\
1024
        E & double-precision float, little-endian byte order\\
1025
        e & single-precision float, little-endian byte order\\
1026
        F,f & single-precision float, native format\\
1027
        G & double-precision float, network (big-endian) byte order\\
1028
        g & single-precision float, network (big-endian) byte order\\
1029
        I & unsigned integer\\
1030
        i & integer\\
1031
        L & unsigned long\\
1032
        l & long\\
1033

    
1034
        N & long, network (big-endian) byte order\\
1035
        n & short, network (big-endian) byte-order\\
1036
        P & pointer to a structure (fixed-length string)\\
1037
        p & pointer to a null-terminated string\\
1038
        S & unsigned short\\
1039
        s & short\\
1040
        V & long, little-endian byte order\\
1041
        v & short, little-endian byte order\\
1042
        X & back up a byte\\
1043
        x & null byte\\
1044
        Z & ASCII string (null padded, count is width)\\
1045
"
1046
=end
1047
  end
1048

    
1049
  def test_pop
1050
    a = @cls[ 'cat', 'dog' ]
1051
    assert_equal('dog', a.pop)
1052
    assert_equal(@cls['cat'], a)
1053
    assert_equal('cat', a.pop)
1054
    assert_equal(@cls[], a)
1055
    assert_nil(a.pop)
1056
    assert_equal(@cls[], a)
1057
  end
1058

    
1059
  def test_push
1060
    a = @cls[1, 2, 3]
1061
    assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5))
1062
    assert_equal(@cls[1, 2, 3, 4, 5, nil], a.push(nil))
1063
    # Ruby 1.8 feature:
1064
    # Array#push accepts any number of arguments.
1065
    #assert_raise(ArgumentError, "a.push()") { a.push() }
1066
    a.push
1067
    assert_equal @cls[1, 2, 3, 4, 5, nil], a
1068
    a.push 6, 7
1069
    assert_equal @cls[1, 2, 3, 4, 5, nil, 6, 7], a
1070
  end
1071

    
1072
  def test_rassoc
1073
    a1 = @cls[*%w( cat  feline )]
1074
    a2 = @cls[*%w( dog  canine )]
1075
    a3 = @cls[*%w( mule asinine )]
1076
    a  = @cls[ a1, a2, a3 ]
1077

    
1078
    assert_equal(a1,  a.rassoc('feline'))
1079
    assert_equal(a3,  a.rassoc('asinine'))
1080
    assert_equal(nil, a.rassoc('dog'))
1081
    assert_equal(nil, a.rassoc('mule'))
1082
    assert_equal(nil, a.rassoc(1..2))
1083
  end
1084

    
1085
  # also delete_if
1086
  def test_reject!
1087
    a = @cls[ 1, 2, 3, 4, 5 ]
1088
    assert_equal(nil, a.reject! { false })
1089
    assert_equal(@cls[1, 2, 3, 4, 5], a)
1090

    
1091
    a = @cls[ 1, 2, 3, 4, 5 ]
1092
    assert_equal(a, a.reject! { true })
1093
    assert_equal(@cls[], a)
1094

    
1095
    a = @cls[ 1, 2, 3, 4, 5 ]
1096
    assert_equal(a, a.reject! { |i| i > 3 })
1097
    assert_equal(@cls[1, 2, 3], a)
1098

    
1099
    bug2545 = '[ruby-core:27366]'
1100
    a = @cls[ 5, 6, 7, 8, 9, 10 ]
1101
    assert_equal(9, a.reject! {|i| break i if i > 8; assert_equal(a[0], i) || true if i < 7})
1102
    assert_equal(@cls[7, 8, 9, 10], a, bug2545)
1103
  end
1104

    
1105
  def test_replace
1106
    a = @cls[ 1, 2, 3]
1107
    a_id = a.__id__
1108
    assert_equal(@cls[4, 5, 6], a.replace(@cls[4, 5, 6]))
1109
    assert_equal(@cls[4, 5, 6], a)
1110
    assert_equal(a_id, a.__id__)
1111
    assert_equal(@cls[], a.replace(@cls[]))
1112

    
1113
    fa = a.dup.freeze
1114
    assert_nothing_raised(RuntimeError) { a.replace(a) }
1115
    assert_raise(RuntimeError) { fa.replace(fa) }
1116
    assert_raise(ArgumentError) { fa.replace() }
1117
    assert_raise(TypeError) { a.replace(42) }
1118
    assert_raise(RuntimeError) { fa.replace(42) }
1119
  end
1120

    
1121
  def test_reverse
1122
    a = @cls[*%w( dog cat bee ant )]
1123
    assert_equal(@cls[*%w(ant bee cat dog)], a.reverse)
1124
    assert_equal(@cls[*%w(dog cat bee ant)], a)
1125
    assert_equal(@cls[], @cls[].reverse)
1126
  end
1127

    
1128
  def test_reverse!
1129
    a = @cls[*%w( dog cat bee ant )]
1130
    assert_equal(@cls[*%w(ant bee cat dog)], a.reverse!)
1131
    assert_equal(@cls[*%w(ant bee cat dog)], a)
1132
    # Ruby 1.8 feature change:
1133
    # Array#reverse always returns self.
1134
    #assert_nil(@cls[].reverse!)
1135
    assert_equal @cls[], @cls[].reverse!
1136
  end
1137

    
1138
  def test_reverse_each
1139
    a = @cls[*%w( dog cat bee ant )]
1140
    i = a.length
1141
    a.reverse_each { |e|
1142
      i -= 1
1143
      assert_equal(a[i], e)
1144
    }
1145
    assert_equal(0, i)
1146

    
1147
    a = @cls[]
1148
    i = 0
1149
    a.reverse_each { |e|
1150
      assert(false, "Never get here")
1151
    }
1152
    assert_equal(0, i)
1153
  end
1154

    
1155
  def test_rindex
1156
    a = @cls[ 'cat', 99, /a/, 99, [ 1, 2, 3] ]
1157
    assert_equal(0, a.rindex('cat'))
1158
    assert_equal(3, a.rindex(99))
1159
    assert_equal(4, a.rindex([1,2,3]))
1160
    assert_nil(a.rindex('ca'))
1161
    assert_nil(a.rindex([1,2]))
1162

    
1163
    assert_equal(3, a.rindex(99) {|x| x == [1,2,3] })
1164
  end
1165

    
1166
  def test_shift
1167
    a = @cls[ 'cat', 'dog' ]
1168
    assert_equal('cat', a.shift)
1169
    assert_equal(@cls['dog'], a)
1170
    assert_equal('dog', a.shift)
1171
    assert_equal(@cls[], a)
1172
    assert_nil(a.shift)
1173
    assert_equal(@cls[], a)
1174
  end
1175

    
1176
  def test_size
1177
    assert_equal(0,   @cls[].size)
1178
    assert_equal(1,   @cls[1].size)
1179
    assert_equal(100, @cls[*(0..99).to_a].size)
1180
  end
1181

    
1182
  def test_slice
1183
    a = @cls[*(1..100).to_a]
1184

    
1185
    assert_equal(1, a.slice(0))
1186
    assert_equal(100, a.slice(99))
1187
    assert_nil(a.slice(100))
1188
    assert_equal(100, a.slice(-1))
1189
    assert_equal(99,  a.slice(-2))
1190
    assert_equal(1,   a.slice(-100))
1191
    assert_nil(a.slice(-101))
1192

    
1193
    assert_equal(@cls[1],   a.slice(0,1))
1194
    assert_equal(@cls[100], a.slice(99,1))
1195
    assert_equal(@cls[],    a.slice(100,1))
1196
    assert_equal(@cls[100], a.slice(99,100))
1197
    assert_equal(@cls[100], a.slice(-1,1))
1198
    assert_equal(@cls[99],  a.slice(-2,1))
1199

    
1200
    assert_equal(@cls[10, 11, 12], a.slice(9, 3))
1201
    assert_equal(@cls[10, 11, 12], a.slice(-91, 3))
1202

    
1203
    assert_nil(a.slice(-101, 2))
1204

    
1205
    assert_equal(@cls[1],   a.slice(0..0))
1206
    assert_equal(@cls[100], a.slice(99..99))
1207
    assert_equal(@cls[],    a.slice(100..100))
1208
    assert_equal(@cls[100], a.slice(99..200))
1209
    assert_equal(@cls[100], a.slice(-1..-1))
1210
    assert_equal(@cls[99],  a.slice(-2..-2))
1211

    
1212
    assert_equal(@cls[10, 11, 12], a.slice(9..11))
1213
    assert_equal(@cls[10, 11, 12], a.slice(-91..-89))
1214

    
1215
    assert_nil(a.slice(-101..-1))
1216

    
1217
    assert_nil(a.slice(10, -3))
1218
    # Ruby 1.8 feature change:
1219
    # Array#slice[size..x] always returns [].
1220
    #assert_nil(a.slice(10..7))
1221
    assert_equal @cls[], a.slice(10..7)
1222
  end
1223

    
1224
  def test_slice!
1225
    a = @cls[1, 2, 3, 4, 5]
1226
    assert_equal(3, a.slice!(2))
1227
    assert_equal(@cls[1, 2, 4, 5], a)
1228

    
1229
    a = @cls[1, 2, 3, 4, 5]
1230
    assert_equal(4, a.slice!(-2))
1231
    assert_equal(@cls[1, 2, 3, 5], a)
1232

    
1233
    a = @cls[1, 2, 3, 4, 5]
1234
    assert_equal(@cls[3,4], a.slice!(2,2))
1235
    assert_equal(@cls[1, 2, 5], a)
1236

    
1237
    a = @cls[1, 2, 3, 4, 5]
1238
    assert_equal(@cls[4,5], a.slice!(-2,2))
1239
    assert_equal(@cls[1, 2, 3], a)
1240

    
1241
    a = @cls[1, 2, 3, 4, 5]
1242
    assert_equal(@cls[3,4], a.slice!(2..3))
1243
    assert_equal(@cls[1, 2, 5], a)
1244

    
1245
    a = @cls[1, 2, 3, 4, 5]
1246
    assert_equal(nil, a.slice!(20))
1247
    assert_equal(@cls[1, 2, 3, 4, 5], a)
1248

    
1249
    a = @cls[1, 2, 3, 4, 5]
1250
    assert_equal(nil, a.slice!(-6))
1251
    assert_equal(@cls[1, 2, 3, 4, 5], a)
1252

    
1253
    a = @cls[1, 2, 3, 4, 5]
1254
    assert_equal(nil, a.slice!(-6..4))
1255
    assert_equal(@cls[1, 2, 3, 4, 5], a)
1256

    
1257
    a = @cls[1, 2, 3, 4, 5]
1258
    assert_equal(nil, a.slice!(-6,2))
1259
    assert_equal(@cls[1, 2, 3, 4, 5], a)
1260

    
1261
    assert_raise(ArgumentError) { @cls[1].slice! }
1262
    assert_raise(ArgumentError) { @cls[1].slice!(0, 0, 0) }
1263
  end
1264

    
1265
  def test_sort
1266
    a = @cls[ 4, 1, 2, 3 ]
1267
    assert_equal(@cls[1, 2, 3, 4], a.sort)
1268
    assert_equal(@cls[4, 1, 2, 3], a)
1269

    
1270
    assert_equal(@cls[4, 3, 2, 1], a.sort { |x, y| y <=> x} )
1271
    assert_equal(@cls[4, 1, 2, 3], a)
1272

    
1273
    assert_equal(@cls[1, 2, 3, 4], a.sort { |x, y| (x - y) * (2**100) })
1274

    
1275
    a.fill(1)
1276
    assert_equal(@cls[1, 1, 1, 1], a.sort)
1277

    
1278
    assert_equal(@cls[], @cls[].sort)
1279
  end
1280

    
1281
  def test_sort!
1282
    a = @cls[ 4, 1, 2, 3 ]
1283
    assert_equal(@cls[1, 2, 3, 4], a.sort!)
1284
    assert_equal(@cls[1, 2, 3, 4], a)
1285

    
1286
    assert_equal(@cls[4, 3, 2, 1], a.sort! { |x, y| y <=> x} )
1287
    assert_equal(@cls[4, 3, 2, 1], a)
1288

    
1289
    a.fill(1)
1290
    assert_equal(@cls[1, 1, 1, 1], a.sort!)
1291

    
1292
    assert_equal(@cls[1], @cls[1].sort!)
1293
    assert_equal(@cls[], @cls[].sort!)
1294

    
1295
    a = @cls[4, 3, 2, 1]
1296
    a.sort! {|m, n| a.replace([9, 8, 7, 6]); m <=> n }
1297
    assert_equal([1, 2, 3, 4], a)
1298

    
1299
    a = @cls[4, 3, 2, 1]
1300
    a.sort! {|m, n| a.replace([9, 8, 7]); m <=> n }
1301
    assert_equal([1, 2, 3, 4], a)
1302
  end
1303

    
1304
  def test_sort_with_callcc
1305
    respond_to?(:callcc, true) or require 'continuation'
1306
    n = 1000
1307
    cont = nil
1308
    ary = (1..100).to_a
1309
    begin
1310
      ary.sort! {|a,b|
1311
        callcc {|k| cont = k} unless cont
1312
        assert_equal(100, ary.size, '[ruby-core:16679]')
1313
        a <=> b
1314
      }
1315
    rescue => e
1316
    end
1317
    n -= 1
1318
    cont.call if 0 < n
1319
    assert_instance_of(RuntimeError, e, '[ruby-core:16679]')
1320
    assert_match(/reentered/, e.message, '[ruby-core:16679]')
1321
  end
1322

    
1323
  def test_sort_with_replace
1324
    xary = (1..100).to_a
1325
    100.times do
1326
      ary = (1..100).to_a
1327
      ary.sort! {|a,b| ary.replace(xary); a <=> b}
1328
      GC.start
1329
      assert_equal(xary, ary, '[ruby-dev:34732]')
1330
    end
1331
  end
1332

    
1333
  def test_to_a
1334
    a = @cls[ 1, 2, 3 ]
1335
    a_id = a.__id__
1336
    assert_equal(a, a.to_a)
1337
    assert_equal(a_id, a.to_a.__id__)
1338
  end
1339

    
1340
  def test_to_ary
1341
    a = [ 1, 2, 3 ]
1342
    b = @cls[*a]
1343

    
1344
    a_id = a.__id__
1345
    assert_equal(a, b.to_ary)
1346
    if (@cls == Array)
1347
      assert_equal(a_id, a.to_ary.__id__)
1348
    end
1349

    
1350
    o = Object.new
1351
    def o.to_ary
1352
      [4, 5]
1353
    end
1354
    assert_equal([1, 2, 3, 4, 5], a.concat(o))
1355

    
1356
    o = Object.new
1357
    def o.to_ary
1358
      foo_bar()
1359
    end
1360
    assert_match(/foo_bar/, assert_raise(NoMethodError) {a.concat(o)}.message)
1361
  end
1362

    
1363
  def test_to_s
1364
    $, = ""
1365
    a = @cls[]
1366
    assert_equal("[]", a.to_s)
1367

    
1368
    $, = ""
1369
    a = @cls[1, 2]
1370
    assert_equal("[1, 2]", a.to_s)
1371

    
1372
    $, = ""
1373
    a = @cls[1, 2, 3]
1374
    assert_equal("[1, 2, 3]", a.to_s)
1375

    
1376
    $, = ":"
1377
    a = @cls[1, 2, 3]
1378
    assert_equal("[1, 2, 3]", a.to_s)
1379
  ensure
1380
    $, = nil
1381
  end
1382

    
1383
  def test_uniq
1384
    a = []
1385
    b = a.uniq
1386
    assert_equal([], a)
1387
    assert_equal([], b)
1388
    assert_not_same(a, b)
1389

    
1390
    a = [1]
1391
    b = a.uniq
1392
    assert_equal([1], a)
1393
    assert_equal([1], b)
1394
    assert_not_same(a, b)
1395

    
1396
    a = [1,1]
1397
    b = a.uniq
1398
    assert_equal([1,1], a)
1399
    assert_equal([1], b)
1400
    assert_not_same(a, b)
1401

    
1402
    a = [1,2]
1403
    b = a.uniq
1404
    assert_equal([1,2], a)
1405
    assert_equal([1,2], b)
1406
    assert_not_same(a, b)
1407

    
1408
    a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
1409
    b = a.dup
1410
    assert_equal(@cls[1, 2, 3, 4, nil], a.uniq)
1411
    assert_equal(b, a)
1412

    
1413
    c = @cls["a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl"]
1414
    d = c.dup
1415
    assert_equal(@cls[ "a:def", "b:abc", "c:jkl" ], c.uniq {|s| s[/^\w+/]})
1416
    assert_equal(d, c)
1417

    
1418
    assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq)
1419
  end
1420

    
1421
  def test_uniq_with_block
1422
    a = []
1423
    b = a.uniq {|v| v.even? }
1424
    assert_equal([], a)
1425
    assert_equal([], b)
1426
    assert_not_same(a, b)
1427

    
1428
    a = [1]
1429
    b = a.uniq {|v| v.even? }
1430
    assert_equal([1], a)
1431
    assert_equal([1], b)
1432
    assert_not_same(a, b)
1433

    
1434
    a = [1,3]
1435
    b = a.uniq {|v| v.even? }
1436
    assert_equal([1,3], a)
1437
    assert_equal([1], b)
1438
    assert_not_same(a, b)
1439
  end
1440

    
1441
  def test_uniq!
1442
    a = []
1443
    b = a.uniq!
1444
    assert_equal(nil, b)
1445

    
1446
    a = [1]
1447
    b = a.uniq!
1448
    assert_equal(nil, b)
1449

    
1450
    a = [1,1]
1451
    b = a.uniq!
1452
    assert_equal([1], a)
1453
    assert_equal([1], b)
1454
    assert_same(a, b)
1455

    
1456
    a = [1,2]
1457
    b = a.uniq!
1458
    assert_equal([1,2], a)
1459
    assert_equal(nil, b)
1460

    
1461
    a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
1462
    assert_equal(@cls[1, 2, 3, 4, nil], a.uniq!)
1463
    assert_equal(@cls[1, 2, 3, 4, nil], a)
1464

    
1465
    c = @cls["a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl"]
1466
    assert_equal(@cls[ "a:def", "b:abc", "c:jkl" ], c.uniq! {|s| s[/^\w+/]})
1467
    assert_equal(@cls[ "a:def", "b:abc", "c:jkl" ], c)
1468

    
1469
    c = @cls["a:def", "b:abc", "c:jkl"]
1470
    assert_equal(nil, c.uniq! {|s| s[/^\w+/]})
1471
    assert_equal(@cls[ "a:def", "b:abc", "c:jkl" ], c)
1472

    
1473
    assert_nil(@cls[1, 2, 3].uniq!)
1474

    
1475
    f = a.dup.freeze
1476
    assert_raise(ArgumentError) { a.uniq!(1) }
1477
    assert_raise(ArgumentError) { f.uniq!(1) }
1478
    assert_raise(RuntimeError) { f.uniq! }
1479

    
1480
    assert_nothing_raised do
1481
      a = [ {c: "b"}, {c: "r"}, {c: "w"}, {c: "g"}, {c: "g"} ]
1482
      a.sort_by!{|e| e[:c]}
1483
      a.uniq!   {|e| e[:c]}
1484
    end
1485
  end
1486

    
1487
  def test_uniq_bang_with_block
1488
    a = []
1489
    b = a.uniq! {|v| v.even? }
1490
    assert_equal(nil, b)
1491

    
1492
    a = [1]
1493
    b = a.uniq! {|v| v.even? }
1494
    assert_equal(nil, b)
1495

    
1496
    a = [1,3]
1497
    b = a.uniq! {|v| v.even? }
1498
    assert_equal([1], a)
1499
    assert_equal([1], b)
1500
    assert_same(a, b)
1501

    
1502
    a = [1,2]
1503
    b = a.uniq! {|v| v.even? }
1504
    assert_equal([1,2], a)
1505
    assert_equal(nil, b)
1506
  end
1507

    
1508
  def test_unshift
1509
    a = @cls[]
1510
    assert_equal(@cls['cat'], a.unshift('cat'))
1511
    assert_equal(@cls['dog', 'cat'], a.unshift('dog'))
1512
    assert_equal(@cls[nil, 'dog', 'cat'], a.unshift(nil))
1513
    assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2]))
1514
  end
1515

    
1516
  def test_OR # '|'
1517
    assert_equal(@cls[],  @cls[]  | @cls[])
1518
    assert_equal(@cls[1], @cls[1] | @cls[])
1519
    assert_equal(@cls[1], @cls[]  | @cls[1])
1520
    assert_equal(@cls[1], @cls[1] | @cls[1])
1521

    
1522
    assert_equal(@cls[1,2], @cls[1] | @cls[2])
1523
    assert_equal(@cls[1,2], @cls[1, 1] | @cls[2, 2])
1524
    assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
1525
  end
1526

    
1527
  def test_combination
1528
    assert_equal(@cls[[]], @cls[1,2,3,4].combination(0).to_a)
1529
    assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a)
1530
    assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], @cls[1,2,3,4].combination(2).to_a)
1531
    assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], @cls[1,2,3,4].combination(3).to_a)
1532
    assert_equal(@cls[[1,2,3,4]], @cls[1,2,3,4].combination(4).to_a)
1533
    assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a)
1534
  end
1535

    
1536
  def test_product
1537
    assert_equal(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]],
1538
                 @cls[1,2,3].product([4,5]))
1539
    assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @cls[1,2].product([1,2]))
1540

    
1541
    assert_equal(@cls[[1,3,5],[1,3,6],[1,4,5],[1,4,6],
1542
                   [2,3,5],[2,3,6],[2,4,5],[2,4,6]],
1543
                 @cls[1,2].product([3,4],[5,6]))
1544
    assert_equal(@cls[[1],[2]], @cls[1,2].product)
1545
    assert_equal(@cls[], @cls[1,2].product([]))
1546

    
1547
    bug3394 = '[ruby-dev:41540]'
1548
    acc = []
1549
    EnvUtil.under_gc_stress {[1,2].product([3,4,5],[6,8]){|array| acc << array}}
1550
    assert_equal([[1, 3, 6], [1, 3, 8], [1, 4, 6], [1, 4, 8], [1, 5, 6], [1, 5, 8],
1551
                  [2, 3, 6], [2, 3, 8], [2, 4, 6], [2, 4, 8], [2, 5, 6], [2, 5, 8]],
1552
                 acc, bug3394)
1553

    
1554
    def (o = Object.new).to_ary; GC.start; [3,4] end
1555
    acc = [1,2].product(*[o]*10)
1556
    assert_equal([1,2].product([3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4]),
1557
                 acc)
1558

    
1559
    a = []
1560
    [1, 2].product([0, 1, 2, 3, 4][1, 4]) {|x| a << x }
1561
    assert(a.all?{|x| !x.include?(0) })
1562
  end
1563

    
1564
  def test_permutation
1565
    a = @cls[1,2,3]
1566
    assert_equal(@cls[[]], a.permutation(0).to_a)
1567
    assert_equal(@cls[[1],[2],[3]], a.permutation(1).to_a.sort)
1568
    assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
1569
                 a.permutation(2).to_a.sort)
1570
    assert_equal(@cls[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
1571
                 a.permutation(3).sort.to_a)
1572
    assert_equal(@cls[], a.permutation(4).to_a)
1573
    assert_equal(@cls[], a.permutation(-1).to_a)
1574
    assert_equal("abcde".each_char.to_a.permutation(5).sort,
1575
                 "edcba".each_char.to_a.permutation(5).sort)
1576
    assert_equal(@cls[].permutation(0).to_a, @cls[[]])
1577

    
1578
    a = @cls[1, 2, 3, 4]
1579
    b = @cls[]
1580
    a.permutation {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
1581
    assert_equal(@cls[9, 8, 7, 6], a)
1582
    assert_equal(@cls[1, 2, 3, 4].permutation.to_a, b)
1583

    
1584
    bug3708 = '[ruby-dev:42067]'
1585
    assert_equal(b, @cls[0, 1, 2, 3, 4][1, 4].permutation.to_a, bug3708)
1586
  end
1587

    
1588
  def test_repeated_permutation
1589
    a = @cls[1,2]
1590
    assert_equal(@cls[[]], a.repeated_permutation(0).to_a)
1591
    assert_equal(@cls[[1],[2]], a.repeated_permutation(1).to_a.sort)
1592
    assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]],
1593
                 a.repeated_permutation(2).to_a.sort)
1594
    assert_equal(@cls[[1,1,1],[1,1,2],[1,2,1],[1,2,2],
1595
                      [2,1,1],[2,1,2],[2,2,1],[2,2,2]],
1596
                 a.repeated_permutation(3).to_a.sort)
1597
    assert_equal(@cls[], a.repeated_permutation(-1).to_a)
1598
    assert_equal("abcde".each_char.to_a.repeated_permutation(5).sort,
1599
                 "edcba".each_char.to_a.repeated_permutation(5).sort)
1600
    assert_equal(@cls[].repeated_permutation(0).to_a, @cls[[]])
1601
    assert_equal(@cls[].repeated_permutation(1).to_a, @cls[])
1602

    
1603
    a = @cls[1, 2, 3, 4]
1604
    b = @cls[]
1605
    a.repeated_permutation(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
1606
    assert_equal(@cls[9, 8, 7, 6], a)
1607
    assert_equal(@cls[1, 2, 3, 4].repeated_permutation(4).to_a, b)
1608

    
1609
    a = @cls[0, 1, 2, 3, 4][1, 4].repeated_permutation(2)
1610
    assert(a.all?{|x| !x.include?(0) })
1611
  end
1612

    
1613
  def test_repeated_combination
1614
    a = @cls[1,2,3]
1615
    assert_equal(@cls[[]], a.repeated_combination(0).to_a)
1616
    assert_equal(@cls[[1],[2],[3]], a.repeated_combination(1).to_a.sort)
1617
    assert_equal(@cls[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]],
1618
                 a.repeated_combination(2).to_a.sort)
1619
    assert_equal(@cls[[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
1620
                      [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]],
1621
                 a.repeated_combination(3).to_a.sort)
1622
    assert_equal(@cls[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],
1623
                      [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
1624
                      [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]],
1625
                 a.repeated_combination(4).to_a.sort)
1626
    assert_equal(@cls[], a.repeated_combination(-1).to_a)
1627
    assert_equal("abcde".each_char.to_a.repeated_combination(5).map{|a|a.sort}.sort,
1628
                 "edcba".each_char.to_a.repeated_combination(5).map{|a|a.sort}.sort)
1629
    assert_equal(@cls[].repeated_combination(0).to_a, @cls[[]])
1630
    assert_equal(@cls[].repeated_combination(1).to_a, @cls[])
1631

    
1632
    a = @cls[1, 2, 3, 4]
1633
    b = @cls[]
1634
    a.repeated_combination(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
1635
    assert_equal(@cls[9, 8, 7, 6], a)
1636
    assert_equal(@cls[1, 2, 3, 4].repeated_combination(4).to_a, b)
1637

    
1638
    a = @cls[0, 1, 2, 3, 4][1, 4].repeated_combination(2)
1639
    assert(a.all?{|x| !x.include?(0) })
1640
  end
1641

    
1642
  def test_take
1643
    assert_equal([1,2,3], [1,2,3,4,5,0].take(3))
1644
    assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].take(-1) }
1645
    assert_equal([1,2], [1,2].take(1000000000), '[ruby-dev:34123]')
1646
  end
1647

    
1648
  def test_take_while
1649
    assert_equal([1,2], [1,2,3,4,5,0].take_while {|i| i < 3 })
1650
  end
1651

    
1652
  def test_drop
1653
    assert_equal([4,5,0], [1,2,3,4,5,0].drop(3))
1654
    assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].drop(-1) }
1655
    assert_equal([], [1,2].drop(1000000000), '[ruby-dev:34123]')
1656
  end
1657

    
1658
  def test_drop_while
1659
    assert_equal([3,4,5,0], [1,2,3,4,5,0].drop_while {|i| i < 3 })
1660
  end
1661

    
1662
  def test_modify_check
1663
    a = []
1664
    a.freeze
1665
    assert_raise(RuntimeError) { a.shift }
1666
    a = [1, 2]
1667
    assert_raise(SecurityError) do
1668
      Thread.new do
1669
        $SAFE = 4
1670
       a.shift
1671
      end.value
1672
    end
1673
  end
1674

    
1675
  LONGP = [127, 63, 31, 15, 7].map {|x| 2**x-1 }.find do |x|
1676
    begin
1677
      [].first(x)
1678
    rescue ArgumentError
1679
      true
1680
    rescue RangeError
1681
      false
1682
    end
1683
  end
1684

    
1685
  def test_ary_new
1686
    assert_raise(ArgumentError) { [].to_enum.first(-1) }
1687
    assert_raise(ArgumentError) { [].to_enum.first(LONGP) }
1688
  end
1689

    
1690
  def test_try_convert
1691
    assert_equal([1], Array.try_convert([1]))
1692
    assert_equal(nil, Array.try_convert("1"))
1693
  end
1694

    
1695
  def test_initialize
1696
    assert_nothing_raised { [].instance_eval { initialize } }
1697
    assert_nothing_raised { Array.new { } }
1698
    assert_equal([1, 2, 3], Array.new([1, 2, 3]))
1699
    assert_raise(ArgumentError) { Array.new(-1, 1) }
1700
    assert_raise(ArgumentError) { Array.new(LONGP, 1) }
1701
    assert_equal([1, 1, 1], Array.new(3, 1))
1702
    assert_equal([1, 1, 1], Array.new(3) { 1 })
1703
    assert_equal([1, 1, 1], Array.new(3, 1) { 1 })
1704
  end
1705

    
1706
  def test_aset_error
1707
    assert_raise(IndexError) { [0][-2] = 1 }
1708
    assert_raise(IndexError) { [0][LONGP] = 2 }
1709
    assert_raise(IndexError) { [0][(LONGP + 1) / 2 - 1] = 2 }
1710
    assert_raise(IndexError) { [0][LONGP..-1] = 2 }
1711
    a = [0]
1712
    a[2] = 4
1713
    assert_equal([0, nil, 4], a)
1714
    assert_raise(ArgumentError) { [0][0, 0, 0] = 0 }
1715
    assert_raise(ArgumentError) { [0].freeze[0, 0, 0] = 0 }
1716
    assert_raise(TypeError) { [0][:foo] = 0 }
1717
    assert_raise(RuntimeError) { [0].freeze[:foo] = 0 }
1718
  end
1719

    
1720
  def test_first2
1721
    assert_equal([0], [0].first(2))
1722
    assert_raise(ArgumentError) { [0].first(-1) }
1723
  end
1724

    
1725
  def test_shift2
1726
    assert_equal(0, ([0] * 16).shift)
1727
    # check
1728
    a = [0, 1, 2]
1729
    a[3] = 3
1730
    a.shift(2)
1731
    assert_equal([2, 3], a)
1732
  end
1733

    
1734
  def test_unshift_error
1735
    assert_raise(RuntimeError) { [].freeze.unshift('cat') }
1736
    assert_raise(RuntimeError) { [].freeze.unshift() }
1737
  end
1738

    
1739
  def test_aref
1740
    assert_raise(ArgumentError) { [][0, 0, 0] }
1741
  end
1742

    
1743
  def test_fetch
1744
    assert_equal(1, [].fetch(0, 0) { 1 })
1745
    assert_equal(1, [0, 1].fetch(-1))
1746
    assert_raise(IndexError) { [0, 1].fetch(2) }
1747
    assert_raise(IndexError) { [0, 1].fetch(-3) }
1748
    assert_equal(2, [0, 1].fetch(2, 2))
1749
  end
1750

    
1751
  def test_index2
1752
    a = [0, 1, 2]
1753
    assert_equal(a, a.index.to_a)
1754
    assert_equal(1, a.index {|x| x == 1 })
1755
  end
1756

    
1757
  def test_rindex2
1758
    a = [0, 1, 2]
1759
    assert_equal([2, 1, 0], a.rindex.to_a)
1760
    assert_equal(1, a.rindex {|x| x == 1 })
1761

    
1762
    a = [0, 1]
1763
    e = a.rindex
1764
    assert_equal(1, e.next)
1765
    a.clear
1766
    assert_raise(StopIteration) { e.next }
1767

    
1768
    o = Object.new
1769
    class << o; self; end.class_eval do
1770
      define_method(:==) {|x| a.clear; false }
1771
    end
1772
    a = [nil, o]
1773
    assert_equal(nil, a.rindex(0))
1774
  end
1775

    
1776
  def test_ary_to_ary
1777
    o = Object.new
1778
    def o.to_ary; [1, 2, 3]; end
1779
    a, b, c = o
1780
    assert_equal([1, 2, 3], [a, b, c])
1781
  end
1782

    
1783
  def test_splice
1784
    a = [0]
1785
    assert_raise(IndexError) { a[-2, 0] = nil }
1786
  end
1787

    
1788
  def test_insert
1789
    a = [0]
1790
    assert_equal([0], a.insert(1))
1791
    assert_equal([0, 1], a.insert(1, 1))
1792
    assert_raise(ArgumentError) { a.insert }
1793
    assert_equal([0, 1, 2], a.insert(-1, 2))
1794
    assert_equal([0, 1, 3, 2], a.insert(-2, 3))
1795
    assert_raise(RuntimeError) { [0].freeze.insert(0)}
1796
    assert_raise(ArgumentError) { [0].freeze.insert }
1797
  end
1798

    
1799
  def test_join2
1800
    a = []
1801
    a << a
1802
    assert_raise(ArgumentError){a.join}
1803

    
1804
    def (a = Object.new).to_ary
1805
      [self]
1806
    end
1807
    assert_raise(ArgumentError, '[ruby-core:24150]'){[a].join}
1808
    assert_equal("12345", [1,[2,[3,4],5]].join)
1809
  end
1810

    
1811
  def test_to_a2
1812
    klass = Class.new(Array)
1813
    a = klass.new.to_a
1814
    assert_equal([], a)
1815
    assert_equal(Array, a.class)
1816
  end
1817

    
1818
  def test_values_at2
1819
    a = [0, 1, 2, 3, 4, 5]
1820
    assert_equal([1, 2, 3], a.values_at(1..3))
1821
    assert_equal([], a.values_at(7..8))
1822
    assert_equal([nil], a.values_at(2**31-1))
1823
  end
1824

    
1825
  def test_select
1826
    assert_equal([0, 2], [0, 1, 2, 3].select {|x| x % 2 == 0 })
1827
  end
1828

    
1829
  # also keep_if
1830
  def test_select!
1831
    a = @cls[ 1, 2, 3, 4, 5 ]
1832
    assert_equal(nil, a.select! { true })
1833
    assert_equal(a, a.keep_if { true })
1834
    assert_equal(@cls[1, 2, 3, 4, 5], a)
1835

    
1836
    a = @cls[ 1, 2, 3, 4, 5 ]
1837
    assert_equal(a, a.select! { false })
1838
    assert_equal(@cls[], a)
1839

    
1840
    a = @cls[ 1, 2, 3, 4, 5 ]
1841
    assert_equal(a, a.select! { |i| i > 3 })
1842
    assert_equal(@cls[4, 5], a)
1843
  end
1844

    
1845
  def test_delete2
1846
    a = [0] * 1024 + [1] + [0] * 1024
1847
    a.delete(0)
1848
    assert_equal([1], a)
1849
  end
1850

    
1851
  def test_reject
1852
    assert_equal([1, 3], [0, 1, 2, 3].reject {|x| x % 2 == 0 })
1853
  end
1854

    
1855
  def test_zip
1856
    assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]],
1857
      [1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]))
1858
    a = []
1859
    [1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]) {|x| a << x }
1860
    assert_equal([[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]], a)
1861

    
1862
    ary = Object.new
1863
    def ary.to_a;   [1, 2]; end
1864
    assert_raise(NoMethodError){ %w(a b).zip(ary) }
1865
    def ary.each; [3, 4].each{|e|yield e}; end
1866
    assert_equal([['a', 3], ['b', 4]], %w(a b).zip(ary))
1867
    def ary.to_ary; [5, 6]; end
1868
    assert_equal([['a', 5], ['b', 6]], %w(a b).zip(ary))
1869
  end
1870

    
1871
  def test_transpose
1872
    assert_equal([[1, :a], [2, :b], [3, :c]],
1873
      [[1, 2, 3], [:a, :b, :c]].transpose)
1874
    assert_raise(IndexError) { [[1, 2, 3], [:a, :b]].transpose }
1875
  end
1876

    
1877
  def test_clear2
1878
    assert_equal([], ([0] * 1024).clear)
1879
  end
1880

    
1881
  def test_fill2
1882
    assert_raise(ArgumentError) { [].fill(0, 1, LONGP) }
1883
  end
1884

    
1885
  def test_times
1886
    assert_raise(ArgumentError) { [0, 0, 0, 0] * ((LONGP + 1) / 4) }
1887
  end
1888

    
1889
  def test_equal
1890
    o = Object.new
1891
    def o.to_ary; end
1892
    def o.==(x); :foo; end
1893
    assert_equal([0, 1, 2], o)
1894
    assert_not_equal([0, 1, 2], [0, 1, 3])
1895
  end
1896

    
1897
  def test_hash2
1898
    a = []
1899
    a << a
1900
    assert_equal([[a]].hash, a.hash)
1901
    assert_not_equal([a, a].hash, a.hash) # Implementation dependent
1902
  end
1903

    
1904
  def test_flatten_error
1905
    a = []
1906
    a << a
1907
    assert_raise(ArgumentError) { a.flatten }
1908

    
1909
    f = [].freeze
1910
    assert_raise(ArgumentError) { a.flatten!(1, 2) }
1911
    assert_raise(TypeError) { a.flatten!(:foo) }
1912
    assert_raise(ArgumentError) { f.flatten!(1, 2) }
1913
    assert_raise(RuntimeError) { f.flatten! }
1914
    assert_raise(RuntimeError) { f.flatten!(:foo) }
1915
  end
1916

    
1917
  def test_shuffle
1918
    100.times do
1919
      assert_equal([0, 1, 2], [2, 1, 0].shuffle.sort)
1920
    end
1921

    
1922
    gen = Random.new(0)
1923
    assert_raise(ArgumentError) {[1, 2, 3].shuffle(1, random: gen)}
1924
    srand(0)
1925
    100.times do
1926
      assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen))
1927
    end
1928
  end
1929

    
1930
  def test_shuffle_random
1931
    gen = proc do
1932
      10000000
1933
    end
1934
    class << gen
1935
      alias rand call
1936
    end
1937
    assert_raise(RangeError) {
1938
      [*0..2].shuffle(random: gen)
1939
    }
1940

    
1941
    ary = (0...10000).to_a
1942
    gen = proc do
1943
      ary.replace([])
1944
      0.5
1945
    end
1946
    class << gen
1947
      alias rand call
1948
    end
1949
    assert_raise(RuntimeError) {ary.shuffle!(random: gen)}
1950
  end
1951

    
1952
  def test_sample
1953
    100.times do
1954
      assert([0, 1, 2].include?([2, 1, 0].sample))
1955
      samples = [2, 1, 0].sample(2)
1956
      samples.each{|sample|
1957
        assert([0, 1, 2].include?(sample))
1958
      }
1959
    end
1960

    
1961
    srand(0)
1962
    a = (1..18).to_a
1963
    (0..20).each do |n|
1964
      100.times do
1965
        b = a.sample(n)
1966
        assert_equal([n, 18].min, b.size)
1967
        assert_equal(a, (a | b).sort)
1968
        assert_equal(b.sort, (a & b).sort)
1969
      end
1970

    
1971
      h = Hash.new(0)
1972
      1000.times do
1973
        a.sample(n).each {|x| h[x] += 1 }
1974
      end
1975
      assert_operator(h.values.min * 2, :>=, h.values.max) if n != 0
1976
    end
1977

    
1978
    assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)}
1979

    
1980
    gen = Random.new(0)
1981
    srand(0)
1982
    a = (1..18).to_a
1983
    (0..20).each do |n|
1984
      100.times do |i|
1985
        assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}")
1986
      end
1987
    end
1988
  end
1989

    
1990
  def test_sample_random
1991
    ary = (0...10000).to_a
1992
    assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)}
1993
    gen0 = proc do
1994
      0.5
1995
    end
1996
    class << gen0
1997
      alias rand call
1998
    end
1999
    gen1 = proc do
2000
      ary.replace([])
2001
      0.5
2002
    end
2003
    class << gen1
2004
      alias rand call
2005
    end
2006
    assert_equal(5000, ary.sample(random: gen0))
2007
    assert_nil(ary.sample(random: gen1))
2008
    assert_equal([], ary)
2009
    ary = (0...10000).to_a
2010
    assert_equal([5000], ary.sample(1, random: gen0))
2011
    assert_equal([], ary.sample(1, random: gen1))
2012
    assert_equal([], ary)
2013
    ary = (0...10000).to_a
2014
    assert_equal([5000, 4999], ary.sample(2, random: gen0))
2015
    assert_equal([], ary.sample(2, random: gen1))
2016
    assert_equal([], ary)
2017
    ary = (0...10000).to_a
2018
    assert_equal([5000, 4999, 5001], ary.sample(3, random: gen0))
2019
    assert_equal([], ary.sample(3, random: gen1))
2020
    assert_equal([], ary)
2021
    ary = (0...10000).to_a
2022
    assert_equal([5000, 4999, 5001, 4998], ary.sample(4, random: gen0))
2023
    assert_equal([], ary.sample(4, random: gen1))
2024
    assert_equal([], ary)
2025
    ary = (0...10000).to_a
2026
    assert_equal([5000, 4999, 5001, 4998, 5002, 4997, 5003, 4996, 5004, 4995], ary.sample(10, random: gen0))
2027
    assert_equal([], ary.sample(10, random: gen1))
2028
    assert_equal([], ary)
2029
    ary = (0...10000).to_a
2030
    assert_equal([5000, 0, 5001, 2, 5002, 4, 5003, 6, 5004, 8, 5005], ary.sample(11, random: gen0))
2031
    ary.sample(11, random: gen1) # implementation detail, may change in the future
2032
    assert_equal([], ary)
2033
  end
2034

    
2035
  def test_cycle
2036
    a = []
2037
    [0, 1, 2].cycle do |i|
2038
      a << i
2039
      break if a.size == 10
2040
    end
2041
    assert_equal([0, 1, 2, 0, 1, 2, 0, 1, 2, 0], a)
2042

    
2043
    a = [0, 1, 2]
2044
    assert_nil(a.cycle { a.clear })
2045

    
2046
    a = []
2047
    [0, 1, 2].cycle(3) {|i| a << i }
2048
    assert_equal([0, 1, 2, 0, 1, 2, 0, 1, 2], a)
2049
  end
2050

    
2051
  def test_reverse_each2
2052
    a = [0, 1, 2, 3, 4, 5]
2053
    r = []
2054
    a.reverse_each do |x|
2055
      r << x
2056
      a.pop
2057
      a.pop
2058
    end
2059
    assert_equal([5, 3, 1], r)
2060
  end
2061

    
2062
  def test_combination2
2063
    assert_equal(:called, (0..100).to_a.combination(50) { break :called }, "[ruby-core:29240] ... must be yielded even if 100C50 > signed integer")
2064
  end
2065

    
2066
  def test_product2
2067
    a = (0..100).to_a
2068
    assert_raise(RangeError) do
2069
      a.product(a, a, a, a, a, a, a, a, a, a)
2070
    end
2071
    assert_nothing_raised(RangeError) do
2072
      a.product(a, a, a, a, a, a, a, a, a, a) { break }
2073
    end
2074
  end
2075

    
2076
  class Array2 < Array
2077
  end
2078

    
2079
  def test_array_subclass
2080
    assert_equal(Array2, Array2[1,2,3].uniq.class, "[ruby-dev:34581]")
2081
    assert_equal(Array2, Array2[1,2][0,1].class) # embeded
2082
    assert_equal(Array2, Array2[*(1..100)][1..99].class) #not embeded
2083
  end
2084

    
2085
  def test_inspect
2086
    a = @cls[1, 2, 3]
2087
    a.taint
2088
    a.untrust
2089
    s = a.inspect
2090
    assert_equal(true, s.tainted?)
2091
    assert_equal(true, s.untrusted?)
2092
  end
2093

    
2094
  def test_initialize2
2095
    a = [1] * 1000
2096
    a.instance_eval { initialize }
2097
    assert_equal([], a)
2098
  end
2099

    
2100
  def test_shift_shared_ary
2101
    a = (1..100).to_a
2102
    b = []
2103
    b.replace(a)
2104
    assert_equal((1..10).to_a, a.shift(10))
2105
    assert_equal((11..100).to_a, a)
2106
  end
2107

    
2108
  def test_replace_shared_ary
2109
    a = [1] * 100
2110
    b = []
2111
    b.replace(a)
2112
    a.replace([1, 2, 3])
2113
    assert_equal([1, 2, 3], a)
2114
    assert_equal([1] * 100, b)
2115
  end
2116

    
2117
  def test_fill_negative_length
2118
    a = (1..10).to_a
2119
    a.fill(:foo, 5, -3)
2120
    assert_equal((1..10).to_a, a)
2121
  end
2122

    
2123
  def test_slice_frozen_array
2124
    a = [1,2,3,4,5].freeze
2125
    assert_equal([1,2,3,4], a[0,4])
2126
    assert_equal([2,3,4,5], a[1,4])
2127
  end
2128

    
2129
  def test_sort_by!
2130
    a = [1,3,5,2,4]
2131
    a.sort_by! {|x| -x }
2132
    assert_equal([5,4,3,2,1], a)
2133
  end
2134

    
2135
  def test_rotate
2136
    a = [1,2,3,4,5].freeze
2137
    assert_equal([2,3,4,5,1], a.rotate)
2138
    assert_equal([5,1,2,3,4], a.rotate(-1))
2139
    assert_equal([3,4,5,1,2], a.rotate(2))
2140
    assert_equal([4,5,1,2,3], a.rotate(-2))
2141
    assert_equal([4,5,1,2,3], a.rotate(13))
2142
    assert_equal([3,4,5,1,2], a.rotate(-13))
2143
    a = [1].freeze
2144
    assert_equal([1], a.rotate)
2145
    assert_equal([1], a.rotate(2))
2146
    assert_equal([1], a.rotate(-4))
2147
    assert_equal([1], a.rotate(13))
2148
    assert_equal([1], a.rotate(-13))
2149
    a = [].freeze
2150
    assert_equal([], a.rotate)
2151
    assert_equal([], a.rotate(2))
2152
    assert_equal([], a.rotate(-4))
2153
    assert_equal([], a.rotate(13))
2154
    assert_equal([], a.rotate(-13))
2155
    a = [1,2,3]
2156
    assert_raise(ArgumentError) { a.rotate(1, 1) }
2157
    assert_equal([1,2,3,4,5].rotate(2**31-1), [1,2,3,4,5].rotate(2**31-0.1))
2158
    assert_equal([1,2,3,4,5].rotate(-2**31), [1,2,3,4,5].rotate(-2**31-0.9))
2159
  end
2160

    
2161
  def test_rotate!
2162
    a = [1,2,3,4,5]
2163
    assert_equal([2,3,4,5,1], a.rotate!)
2164
    assert_equal([2,3,4,5,1], a)
2165
    assert_equal([4,5,1,2,3], a.rotate!(2))
2166
    assert_equal([5,1,2,3,4], a.rotate!(-4))
2167
    assert_equal([3,4,5,1,2], a.rotate!(13))
2168
    assert_equal([5,1,2,3,4], a.rotate!(-13))
2169
    a = [1]
2170
    assert_equal([1], a.rotate!)
2171
    assert_equal([1], a.rotate!(2))
2172
    assert_equal([1], a.rotate!(-4))
2173
    assert_equal([1], a.rotate!(13))
2174
    assert_equal([1], a.rotate!(-13))
2175
    a = []
2176
    assert_equal([], a.rotate!)
2177
    assert_equal([], a.rotate!(2))
2178
    assert_equal([], a.rotate!(-4))
2179
    assert_equal([], a.rotate!(13))
2180
    assert_equal([], a.rotate!(-13))
2181
    a = [].freeze
2182
    e = assert_raise(RuntimeError) {a.rotate!}
2183
    assert_match(/can't modify frozen/, e.message)
2184
    a = [1,2,3]
2185
    assert_raise(ArgumentError) { a.rotate!(1, 1) }
2186
  end
2187

    
2188
  def test_part_of?
2189
    assert_equal(true, [].part_of?([]))
2190
    a = [1, 3, 4]
2191
    b = [1, 2, 3, 4, 5]
2192
    assert_equal(true, a.part_of?(b))
2193
    assert_equal(false, b.part_of?(a))
2194
    a = %w( ant bat cat dog )
2195
    b = %w( dog cat bat ant )
2196
    assert_equal(true, a.part_of?(b))
2197
    assert_equal(true, b.part_of?(a))
2198
    assert_raise(TypeError) { a.part_of? 1 }
2199
    assert_raise(ArgumentError) { a.part_of?() }
2200
  end
2201
end