Skip to content

Commit e11a36d

Browse files
committed
[GR-18163] Fix Array#reject
PullRequest: truffleruby/3612
2 parents 255007f + 0c505f5 commit e11a36d

29 files changed

+366
-183
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Compatibility:
6868
* Fix `File.open` and support `flags` option (#2820, @andrykonchin).
6969
* Support writing to `RData.dfree` for native extensions (#2830, #2732, #2165, @eregon).
7070
* Fix `IO#write` and support multiple arguments with different encodings (#2829, @andrykonchin).
71+
* Fix `Array` methods `reject`, `reject!`, `inject`, `map`, `select`, `each_index` and handle a case when array is modified by a passed block like CRuby does (#2822, andrykonchin, @eregon).
7172

7273
Performance:
7374

spec/ruby/core/array/all_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'shared/iterable_and_tolerating_size_increasing'
3+
4+
describe "Array#all?" do
5+
@value_to_return = -> (_) { true }
6+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :all?
7+
end

spec/ruby/core/array/any_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'shared/iterable_and_tolerating_size_increasing'
23

34
describe "Array#any?" do
45
describe 'with no block given (a default block of { |x| x } is implicit)' do
@@ -19,6 +20,9 @@
1920
end
2021

2122
describe 'with a block given' do
23+
@value_to_return = -> (_) { false }
24+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :any?
25+
2226
it 'is false if the array is empty' do
2327
empty_array = []
2428
empty_array.any? {|v| 1 == 1 }.should == false

spec/ruby/core/array/count_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'shared/iterable_and_tolerating_size_increasing'
23

34
describe "Array#count" do
45
it "returns the number of elements" do
@@ -12,4 +13,8 @@
1213
it "returns the number of element for which the block evaluates to true" do
1314
[:a, :b, :c].count { |s| s != :b }.should == 2
1415
end
16+
17+
context "when a block argument given" do
18+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :count
19+
end
1520
end

spec/ruby/core/array/delete_if_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_relative 'fixtures/classes'
33
require_relative 'shared/enumeratorize'
44
require_relative 'shared/delete_if'
5+
require_relative 'shared/iterable_and_tolerating_size_increasing'
56
require_relative '../enumerable/shared/enumeratorized'
67

78
describe "Array#delete_if" do
@@ -49,4 +50,7 @@
4950

5051
it_behaves_like :enumeratorized_with_origin_size, :delete_if, [1,2,3]
5152
it_behaves_like :delete_if, :delete_if
53+
54+
@value_to_return = -> (_) { false }
55+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :delete_if
5256
end

spec/ruby/core/array/drop_while_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/classes'
3+
require_relative 'shared/iterable_and_tolerating_size_increasing'
34

45
describe "Array#drop_while" do
6+
@value_to_return = -> (_) { true }
7+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :drop_while
8+
59
it "removes elements from the start of the array while the block evaluates to true" do
610
[1, 2, 3, 4].drop_while { |n| n < 4 }.should == [4]
711
end

spec/ruby/core/array/each_index_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@
4040
it_behaves_like :enumeratorize, :each_index
4141
it_behaves_like :enumeratorized_with_origin_size, :each_index, [1,2,3]
4242
end
43+
44+
describe "Array#each_index" do
45+
it "tolerates increasing an array size during iteration" do
46+
array = [:a, :b, :c]
47+
ScratchPad.record []
48+
i = 0
49+
50+
array.each_index do |index|
51+
ScratchPad << index
52+
array << i if i < 100
53+
i += 1
54+
end
55+
56+
ScratchPad.recorded.should == (0..102).to_a # element indices
57+
end
58+
end

spec/ruby/core/array/each_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/classes'
33
require_relative 'shared/enumeratorize'
4+
require_relative 'shared/iterable_and_tolerating_size_increasing'
45
require_relative '../enumerable/shared/enumeratorized'
56

67
# Mutating the array while it is being iterated is discouraged as it can result in confusing behavior.
@@ -75,3 +76,7 @@
7576
it_behaves_like :enumeratorize, :each
7677
it_behaves_like :enumeratorized_with_origin_size, :each, [1,2,3]
7778
end
79+
80+
describe "Array#each" do
81+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :each
82+
end

spec/ruby/core/array/fill_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@
7272
-> { [].fill(1, 2) {|i|} }.should_not raise_error(ArgumentError)
7373
-> { [].fill(1, 2, true) {|i|} }.should raise_error(ArgumentError)
7474
end
75+
76+
it "tolerates increasing an array size during iteration" do
77+
array = [:a, :b, :c]
78+
ScratchPad.record []
79+
i = 0
80+
81+
array.fill do |index|
82+
ScratchPad << index
83+
array << i if i < 100
84+
i++
85+
index
86+
end
87+
88+
ScratchPad.recorded.should == [0, 1, 2]
89+
end
7590
end
7691

7792
describe "Array#fill with (filler, index, length)" do

spec/ruby/core/array/none_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'shared/iterable_and_tolerating_size_increasing'
3+
4+
describe "Array#none?" do
5+
@value_to_return = -> (_) { false }
6+
it_behaves_like :array_iterable_and_tolerating_size_increasing, :none?
7+
end

0 commit comments

Comments
 (0)