diff options
Diffstat (limited to 'spec/ruby/optional/capi/array_spec.rb')
| -rw-r--r-- | spec/ruby/optional/capi/array_spec.rb | 94 |
1 files changed, 82 insertions, 12 deletions
diff --git a/spec/ruby/optional/capi/array_spec.rb b/spec/ruby/optional/capi/array_spec.rb index 56373efe34..7e87859856 100644 --- a/spec/ruby/optional/capi/array_spec.rb +++ b/spec/ruby/optional/capi/array_spec.rb @@ -8,7 +8,7 @@ describe :rb_ary_new2, shared: true do end it "raises an ArgumentError when the given argument is negative" do - lambda { @s.send(@method, -1) }.should raise_error(ArgumentError) + -> { @s.send(@method, -1) }.should raise_error(ArgumentError) end end @@ -83,8 +83,8 @@ describe "C-API Array function" do @s.rb_ary_cat([1, 2], 3, 4).should == [1, 2, 3, 4] end - it "raises a #{frozen_error_class} if the array is frozen" do - lambda { @s.rb_ary_cat([].freeze, 1) }.should raise_error(frozen_error_class) + it "raises a FrozenError if the array is frozen" do + -> { @s.rb_ary_cat([].freeze, 1) }.should raise_error(FrozenError) end end @@ -130,8 +130,8 @@ describe "C-API Array function" do @s.rb_ary_rotate([1, 2, 3, 4], -3).should == [2, 3, 4, 1] end - it "raises a #{frozen_error_class} if the array is frozen" do - lambda { @s.rb_ary_rotate([].freeze, 1) }.should raise_error(frozen_error_class) + it "raises a FrozenError if the array is frozen" do + -> { @s.rb_ary_rotate([].freeze, 1) }.should raise_error(FrozenError) end end @@ -190,6 +190,22 @@ describe "C-API Array function" do end end + describe "rb_ary_sort" do + it "returns a new sorted array" do + a = [2, 1, 3] + @s.rb_ary_sort(a).should == [1, 2, 3] + a.should == [2, 1, 3] + end + end + + describe "rb_ary_sort_bang" do + it "sorts the given array" do + a = [2, 1, 3] + @s.rb_ary_sort_bang(a).should == [1, 2, 3] + a.should == [1, 2, 3] + end + end + describe "rb_ary_store" do it "overwrites the element at the given position" do a = [1, 2, 3] @@ -205,7 +221,7 @@ describe "C-API Array function" do it "raises an IndexError if the negative index is greater than the length" do a = [1, 2, 3] - lambda { @s.rb_ary_store(a, -10, 5) }.should raise_error(IndexError) + -> { @s.rb_ary_store(a, -10, 5) }.should raise_error(IndexError) end it "enlarges the array as needed" do @@ -214,9 +230,9 @@ describe "C-API Array function" do a.should == [nil, nil, 7] end - it "raises a #{frozen_error_class} if the array is frozen" do + it "raises a FrozenError if the array is frozen" do a = [1, 2, 3].freeze - lambda { @s.rb_ary_store(a, 1, 5) }.should raise_error(frozen_error_class) + -> { @s.rb_ary_store(a, 1, 5) }.should raise_error(FrozenError) end end @@ -249,6 +265,14 @@ describe "C-API Array function" do @s.RARRAY_PTR_assign(a, :set) a.should == [:set, :set, :set] end + + it "allows memcpying between arrays" do + a = [1, 2, 3] + b = [0, 0, 0] + @s.RARRAY_PTR_memcpy(a, b) + b.should == [1, 2, 3] + a.should == [1, 2, 3] # check a was not modified + end end describe "RARRAY_LEN" do @@ -264,6 +288,16 @@ describe "C-API Array function" do end end + describe "RARRAY_ASET" do + # This macro does NOT do any bounds checking! + it "writes an element in the array" do + ary = [1, 2, 3] + @s.RARRAY_ASET(ary, 0, 0) + @s.RARRAY_ASET(ary, 2, 42) + ary.should == [0, 2, 42] + end + end + describe "rb_assoc_new" do it "returns an array containing the two elements" do @s.rb_assoc_new(1, 2).should == [1, 2] @@ -309,10 +343,46 @@ describe "C-API Array function" do end end - describe "rb_iterate" do + ruby_version_is ""..."4.0" do + describe "rb_iterate" do + it "calls an callback function as a block passed to an method" do + s = [1,2,3,4] + s2 = @s.rb_iterate(s) + + s2.should == s + + # Make sure they're different objects + s2.equal?(s).should be_false + end + + it "calls a function with the other function available as a block" do + h = {a: 1, b: 2} + + @s.rb_iterate_each_pair(h).sort.should == [1,2] + end + + it "calls a function which can yield into the original block" do + s2 = [] + + o = Object.new + def o.each + yield 1 + yield 2 + yield 3 + yield 4 + end + + @s.rb_iterate_then_yield(o) { |x| s2 << x } + + s2.should == [1,2,3,4] + end + end + end + + describe "rb_block_call" do it "calls an callback function as a block passed to an method" do s = [1,2,3,4] - s2 = @s.rb_iterate(s) + s2 = @s.rb_block_call(s) s2.should == s @@ -323,7 +393,7 @@ describe "C-API Array function" do it "calls a function with the other function available as a block" do h = {a: 1, b: 2} - @s.rb_iterate_each_pair(h).sort.should == [1,2] + @s.rb_block_call_each_pair(h).sort.should == [1,2] end it "calls a function which can yield into the original block" do @@ -337,7 +407,7 @@ describe "C-API Array function" do yield 4 end - @s.rb_iterate_then_yield(o) { |x| s2 << x } + @s.rb_block_call_then_yield(o) { |x| s2 << x } s2.should == [1,2,3,4] end |
