summaryrefslogtreecommitdiff
path: root/spec/ruby/core/array/element_set_spec.rb
blob: 1e192c100ff670bb1c26596fdd194dd7d2ecbd63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#[]=" do
  it "sets the value of the element at index" do
    a = [1, 2, 3, 4]
    a[2] = 5
    a[-1] = 6
    a[5] = 3
    a.should == [1, 2, 5, 6, nil, 3]

    a = []
    a[4] = "e"
    a.should == [nil, nil, nil, nil, "e"]
    a[3] = "d"
    a.should == [nil, nil, nil, "d", "e"]
    a[0] = "a"
    a.should == ["a", nil, nil, "d", "e"]
    a[-3] = "C"
    a.should == ["a", nil, "C", "d", "e"]
    a[-1] = "E"
    a.should == ["a", nil, "C", "d", "E"]
    a[-5] = "A"
    a.should == ["A", nil, "C", "d", "E"]
    a[5] = "f"
    a.should == ["A", nil, "C", "d", "E", "f"]
    a[1] = []
    a.should == ["A", [], "C", "d", "E", "f"]
    a[-1] = nil
    a.should == ["A", [], "C", "d", "E", nil]
  end

  it "sets the section defined by [start,length] to other" do
    a = [1, 2, 3, 4, 5, 6]
    a[0, 1] = 2
    a[3, 2] = ['a', 'b', 'c', 'd']
    a.should == [2, 2, 3, "a", "b", "c", "d", 6]
  end

  it "replaces the section defined by [start,length] with the given values" do
    a = [1, 2, 3, 4, 5, 6]
    a[3, 2] = 'a', 'b', 'c', 'd'
    a.should == [1, 2, 3, "a", "b", "c", "d", 6]
  end

  it "just sets the section defined by [start,length] to other even if other is nil" do
    a = ['a', 'b', 'c', 'd', 'e']
    a[1, 3] = nil
    a.should == ["a", nil, "e"]
  end

  it "returns nil if the rhs is nil" do
    a = [1, 2, 3]
    (a[1, 3] = nil).should == nil
    (a[1..3] = nil).should == nil
  end

  it "sets the section defined by range to other" do
    a = [6, 5, 4, 3, 2, 1]
    a[1...2] = 9
    a[3..6] = [6, 6, 6]
    a.should == [6, 9, 4, 6, 6, 6]
  end

  it "replaces the section defined by range with the given values" do
    a = [6, 5, 4, 3, 2, 1]
    a[3..6] = :a, :b, :c
    a.should == [6, 5, 4, :a, :b, :c]
  end

  it "just sets the section defined by range to other even if other is nil" do
    a = [1, 2, 3, 4, 5]
    a[0..1] = nil
    a.should == [nil, 3, 4, 5]
  end

  it 'expands and nil-pads the array if section assigned by range is outside array boundaries' do
    a = ['a']
    a[3..4] = ['b', 'c']
    a.should == ['a', nil, nil, 'b', 'c']
  end

  it "calls to_int on its start and length arguments" do
    obj = mock('to_int')
    obj.stub!(:to_int).and_return(2)

    a = [1, 2, 3, 4]
    a[obj, 0] = [9]
    a.should == [1, 2, 9, 3, 4]
    a[obj, obj] = []
    a.should == [1, 2, 4]
    a[obj] = -1
    a.should == [1, 2, -1]
  end

  it "checks frozen before attempting to coerce arguments" do
    a = [1,2,3,4].freeze
    -> {a[:foo] = 1}.should raise_error(FrozenError)
    -> {a[:foo, :bar] = 1}.should raise_error(FrozenError)
  end

  it "sets elements in the range arguments when passed ranges" do
    ary = [1, 2, 3]
    rhs = [nil, [], ["x"], ["x", "y"]]
    (0 .. ary.size + 2).each do |a|
      (a .. ary.size + 3).each do |b|
        rhs.each do |c|
          ary1 = ary.dup
          ary1[a .. b] = c
          ary2 = ary.dup
          ary2[a, 1 + b-a] = c
          ary1.should == ary2

          ary1 = ary.dup
          ary1[a ... b] = c
          ary2 = ary.dup
          ary2[a, b-a] = c
          ary1.should == ary2
        end
      end
    end
  end

  it "inserts the given elements with [range] which the range is zero-width" do
    ary = [1, 2, 3]
    ary[1...1] = 0
    ary.should == [1, 0, 2, 3]
    ary[1...1] = [5]
    ary.should == [1, 5, 0, 2, 3]
    ary[1...1] = :a, :b, :c
    ary.should == [1, :a, :b, :c, 5, 0, 2, 3]
  end

  it "inserts the given elements with [start, length] which length is zero" do
    ary = [1, 2, 3]
    ary[1, 0] = 0
    ary.should == [1, 0, 2, 3]
    ary[1, 0] = [5]
    ary.should == [1, 5, 0, 2, 3]
    ary[1, 0] = :a, :b, :c
    ary.should == [1, :a, :b, :c, 5, 0, 2, 3]
  end

  # Now we only have to test cases where the start, length interface would
  # have raise an exception because of negative size
  it "inserts the given elements with [range] which the range has negative width" do
    ary = [1, 2, 3]
    ary[1..0] = 0
    ary.should == [1, 0, 2, 3]
    ary[1..0] = [4, 3]
    ary.should == [1, 4, 3, 0, 2, 3]
    ary[1..0] = :a, :b, :c
    ary.should == [1, :a, :b, :c, 4, 3, 0, 2, 3]
  end

  it "just inserts nil if the section defined by range is zero-width and the rhs is nil" do
    ary = [1, 2, 3]
    ary[1...1] = nil
    ary.should == [1, nil, 2, 3]
  end

  it "just inserts nil if the section defined by range has negative width and the rhs is nil" do
    ary = [1, 2, 3]
    ary[1..0] = nil
    ary.should == [1, nil, 2, 3]
  end

  it "does nothing if the section defined by range is zero-width and the rhs is an empty array" do
    ary = [1, 2, 3]
    ary[1...1] = []
    ary.should == [1, 2, 3]
  end

  it "does nothing if the section defined by range has negative width and the rhs is an empty array" do
    ary = [1, 2, 3, 4, 5]
    ary[1...0] = []
    ary.should == [1, 2, 3, 4, 5]
    ary[-2..2] = []
    ary.should == [1, 2, 3, 4, 5]
  end

  it "tries to convert Range elements to Integers using #to_int with [m..n] and [m...n]" do
    from = mock('from')
    to = mock('to')

    # So we can construct a range out of them...
    def from.<=>(o) 0 end
    def to.<=>(o) 0 end

    def from.to_int() 1 end
    def to.to_int() -2 end

    a = [1, 2, 3, 4]

    a[from .. to] = ["a", "b", "c"]
    a.should == [1, "a", "b", "c", 4]

    a[to .. from] = ["x"]
    a.should == [1, "a", "b", "x", "c", 4]
    -> { a["a" .. "b"] = []  }.should raise_error(TypeError)
    -> { a[from .. "b"] = [] }.should raise_error(TypeError)
  end

  it "raises an IndexError when passed indexes out of bounds" do
    a = [1, 2, 3, 4]
    -> { a[-5] = ""      }.should raise_error(IndexError)
    -> { a[-5, -1] = ""  }.should raise_error(IndexError)
    -> { a[-5, 0] = ""   }.should raise_error(IndexError)
    -> { a[-5, 1] = ""   }.should raise_error(IndexError)
    -> { a[-5, 2] = ""   }.should raise_error(IndexError)
    -> { a[-5, 10] = ""  }.should raise_error(IndexError)

    -> { a[-5..-5] = ""  }.should raise_error(RangeError)
    -> { a[-5...-5] = "" }.should raise_error(RangeError)
    -> { a[-5..-4] = ""  }.should raise_error(RangeError)
    -> { a[-5...-4] = "" }.should raise_error(RangeError)
    -> { a[-5..10] = ""  }.should raise_error(RangeError)
    -> { a[-5...10] = "" }.should raise_error(RangeError)

    # ok
    a[0..-9] = [1]
    a.should == [1, 1, 2, 3, 4]
  end

  it "calls to_ary on its rhs argument for multi-element sets" do
    obj = mock('to_ary')
    def obj.to_ary() [1, 2, 3] end
    ary = [1, 2]
    ary[0, 0] = obj
    ary.should == [1, 2, 3, 1, 2]
    ary[1, 10] = obj
    ary.should == [1, 1, 2, 3]
  end

  it "does not call to_ary on rhs array subclasses for multi-element sets" do
    ary = []
    ary[0, 0] = ArraySpecs::ToAryArray[5, 6, 7]
    ary.should == [5, 6, 7]
  end

  it "raises a FrozenError on a frozen array" do
    -> { ArraySpecs.frozen_array[0, 0] = [] }.should raise_error(FrozenError)
  end
end

describe "Array#[]= with [index]" do
  it "returns value assigned if idx is inside array" do
    a = [1, 2, 3, 4, 5]
    (a[3] = 6).should == 6
  end

  it "returns value assigned if idx is right beyond right array boundary" do
    a = [1, 2, 3, 4, 5]
    (a[5] = 6).should == 6
  end

  it "returns value assigned if idx far beyond right array boundary" do
    a = [1, 2, 3, 4, 5]
    (a[10] = 6).should == 6
  end

  it "sets the value of the element at index" do
    a = [1, 2, 3, 4]
    a[2] = 5
    a[-1] = 6
    a[5] = 3
    a.should == [1, 2, 5, 6, nil, 3]
  end

  it "sets the value of the element if it is right beyond the array boundary" do
    a = [1, 2, 3, 4]
    a[4] = 8
    a.should == [1, 2, 3, 4, 8]
  end

end

describe "Array#[]= with [index, count]" do
  it "returns non-array value if non-array value assigned" do
    a = [1, 2, 3, 4, 5]
    (a[2, 3] = 10).should == 10
  end

  it "returns array if array assigned" do
    a = [1, 2, 3, 4, 5]
    (a[2, 3] = [4, 5]).should == [4, 5]
  end

  it "accepts a frozen String literal as RHS" do
    a = ['a', 'b', 'c']
    a[0, 2] = 'd'.freeze
    a.should == ['d', 'c']
  end

  it "just sets the section defined by [start,length] to nil even if the rhs is nil" do
    a = ['a', 'b', 'c', 'd', 'e']
    a[1, 3] = nil
    a.should == ["a", nil, "e"]
  end

  it "just sets the section defined by [start,length] to nil if negative index within bounds, cnt > 0 and the rhs is nil" do
    a = ['a', 'b', 'c', 'd', 'e']
    a[-3, 2] = nil
    a.should == ["a", "b", nil, "e"]
  end

  it "replaces the section defined by [start,length] to other" do
    a = [1, 2, 3, 4, 5, 6]
    a[0, 1] = 2
    a[3, 2] = ['a', 'b', 'c', 'd']
    a.should == [2, 2, 3, "a", "b", "c", "d", 6]
  end

  it "replaces the section to other if idx < 0 and cnt > 0" do
    a = [1, 2, 3, 4, 5, 6]
    a[-3, 2] = ["x", "y", "z"]
    a.should == [1, 2, 3, "x", "y", "z", 6]
  end

  it "replaces the section to other even if cnt spanning beyond the array boundary" do
    a = [1, 2, 3, 4, 5]
    a[-1, 3] = [7, 8]
    a.should == [1, 2, 3, 4, 7, 8]
  end

  it "pads the Array with nils if the span is past the end" do
    a = [1, 2, 3, 4, 5]
    a[10, 1] = [1]
    a.should == [1, 2, 3, 4, 5, nil, nil, nil, nil, nil, 1]

    b = [1, 2, 3, 4, 5]
    b[10, 0] = [1]
    a.should == [1, 2, 3, 4, 5, nil, nil, nil, nil, nil, 1]

    c = [1, 2, 3, 4, 5]
    c[10, 0] = []
    c.should == [1, 2, 3, 4, 5, nil, nil, nil, nil, nil]
  end

  it "inserts other section in place defined by idx" do
    a = [1, 2, 3, 4, 5]
    a[3, 0] = [7, 8]
    a.should == [1, 2, 3, 7, 8, 4, 5]

    b = [1, 2, 3, 4, 5]
    b[1, 0] = b
    b.should == [1, 1, 2, 3, 4, 5, 2, 3, 4, 5]
  end

  it "raises an IndexError when passed start and negative length" do
    a = [1, 2, 3, 4]
    -> { a[-2, -1] = "" }.should raise_error(IndexError)
    -> { a[0, -1] = ""  }.should raise_error(IndexError)
    -> { a[2, -1] = ""  }.should raise_error(IndexError)
    -> { a[4, -1] = ""  }.should raise_error(IndexError)
    -> { a[10, -1] = "" }.should raise_error(IndexError)
    -> { [1, 2, 3, 4,  5][2, -1] = [7, 8] }.should raise_error(IndexError)
  end
end

describe "Array#[]= with [m..n]" do
  it "returns non-array value if non-array value assigned" do
    a = [1, 2, 3, 4, 5]
    (a[2..4] = 10).should == 10
    (a.[]=(2..4, 10)).should == 10
  end

  it "returns array if array assigned" do
    a = [1, 2, 3, 4, 5]
    (a[2..4] = [7, 8]).should == [7, 8]
    (a.[]=(2..4, [7, 8])).should == [7, 8]
  end

  it "just sets the section defined by range to nil even if the rhs is nil" do
    a = [1, 2, 3, 4, 5]
    a[0..1] = nil
    a.should == [nil, 3, 4, 5]
  end

  it "just sets the section defined by range to nil if m and n < 0 and the rhs is nil" do
    a = [1, 2, 3, 4, 5]
    a[-3..-2] = nil
    a.should == [1, 2, nil, 5]
  end

  it "replaces the section defined by range" do
    a = [6, 5, 4, 3, 2, 1]
    a[1...2] = 9
    a[3..6] = [6, 6, 6]
    a.should == [6, 9, 4, 6, 6, 6]
  end

  it "replaces the section if m and n < 0" do
    a = [1, 2, 3, 4, 5]
    a[-3..-2] = [7, 8, 9]
    a.should == [1, 2, 7, 8, 9, 5]
  end

  it "replaces the section if m < 0 and n > 0" do
    a = [1, 2, 3, 4, 5]
    a[-4..3] = [8]
    a.should == [1, 8, 5]
  end

  it "inserts the other section at m if m > n" do
    a = [1, 2, 3, 4, 5]
    a[3..1] = [8]
    a.should == [1, 2, 3, 8, 4, 5]
  end

  it "inserts at the end if m > the array size" do
    a = [1, 2, 3]
    a[3..3] = [4]
    a.should == [1, 2, 3, 4]
    a[5..7] = [6]
    a.should == [1, 2, 3, 4, nil, 6]
  end

  describe "Range subclasses" do
    before :each do
      @range_incl = ArraySpecs::MyRange.new(1, 2)
      @range_excl = ArraySpecs::MyRange.new(-3, -1, true)
    end

    it "accepts Range subclasses" do
      a = [1, 2, 3, 4]

      a[@range_incl] = ["a", "b"]
      a.should == [1, "a", "b", 4]
      a[@range_excl] = ["A", "B"]
      a.should == [1, "A", "B", 4]
    end

    it "returns non-array value if non-array value assigned" do
      a = [1, 2, 3, 4, 5]
      (a[@range_incl] = 10).should == 10
      (a.[]=(@range_incl, 10)).should == 10
    end

    it "returns array if array assigned" do
      a = [1, 2, 3, 4, 5]
      (a[@range_incl] = [7, 8]).should == [7, 8]
      a.[]=(@range_incl, [7, 8]).should == [7, 8]
    end
  end
end

describe "Array#[]= with [m..]" do
  it "just sets the section defined by range to nil even if the rhs is nil" do
    a = [1, 2, 3, 4, 5]
    a[eval("(2..)")] = nil
    a.should == [1, 2, nil]
  end

  it "just sets the section defined by range to nil if m and n < 0 and the rhs is nil" do
    a = [1, 2, 3, 4, 5]
    a[eval("(-3..)")] = nil
    a.should == [1, 2, nil]
  end

  it "replaces the section defined by range" do
    a = [6, 5, 4, 3, 2, 1]
    a[eval("(3...)")] = 9
    a.should == [6, 5, 4, 9]
    a[eval("(2..)")] = [7, 7, 7]
    a.should == [6, 5, 7, 7, 7]
  end

  it "replaces the section if m and n < 0" do
    a = [1, 2, 3, 4, 5]
    a[eval("(-3..)")] = [7, 8, 9]
    a.should == [1, 2, 7, 8, 9]
  end

  it "inserts at the end if m > the array size" do
    a = [1, 2, 3]
    a[eval("(3..)")] = [4]
    a.should == [1, 2, 3, 4]
    a[eval("(5..)")] = [6]
    a.should == [1, 2, 3, 4, nil, 6]
  end
end

ruby_version_is "2.7" do
  describe "Array#[]= with [..n] and [...n]" do
    it "just sets the section defined by range to nil even if the rhs is nil" do
      a = [1, 2, 3, 4, 5]
      a[eval("(..2)")] = nil
      a.should == [nil, 4, 5]
      a[eval("(...2)")] = nil
      a.should == [nil, 5]
    end

    it "just sets the section defined by range to nil if n < 0 and the rhs is nil" do
      a = [1, 2, 3, 4, 5]
      a[eval("(..-3)")] = nil
      a.should == [nil, 4, 5]
      a[eval("(...-1)")] = [nil, 5]
    end

    it "replaces the section defined by range" do
      a = [6, 5, 4, 3, 2, 1]
      a[eval("(...3)")] = 9
      a.should == [9, 3, 2, 1]
      a[eval("(..2)")] = [7, 7, 7, 7, 7]
      a.should == [7, 7, 7, 7, 7, 1]
    end

    it "replaces the section if n < 0" do
      a = [1, 2, 3, 4, 5]
      a[eval("(..-2)")] = [7, 8, 9]
      a.should == [7, 8, 9, 5]
    end

    it "replaces everything if n > the array size" do
      a = [1, 2, 3]
      a[eval("(...7)")] = [4]
      a.should == [4]
    end

    it "inserts at the beginning if n < negative the array size" do
      a = [1, 2, 3]
      a[eval("(..-7)")] = [4]
      a.should == [4, 1, 2, 3]
      a[eval("(...-10)")] = [6]
      a.should == [6, 4, 1, 2, 3]
    end
  end
end

describe "Array#[] after a shift" do
  it "works for insertion" do
    a = [1,2]
    a.shift
    a.shift
    a[0,0] = [3,4]
    a.should == [3,4]
  end
end