Skip to content

Commit

Permalink
[GR-20329] Implement rb_array_sort and rb_array_sort_bang.
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/1692
  • Loading branch information
bjfish committed Jun 12, 2020
2 parents d19dfeb + 2b935b2 commit 17701a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Compatibility:
* Implemented `rb_uv_to_utf8` (#1998).
* Single character IDs now behave more like those in MRI to improve C extension compatibility, so `rb_funcall(a, '+', b)` will now do the same thing as in MRI.
* Removed extra public methods on `String`.
* Implemented `rb_array_sort` and `rb_array_sort_bang`.

Performance:

Expand Down
16 changes: 16 additions & 0 deletions spec/ruby/optional/capi/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@
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]
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/ext/array_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ static VALUE array_spec_rb_ary_shift(VALUE self, VALUE array) {
return rb_ary_shift(array);
}

static VALUE array_spec_rb_ary_sort(VALUE self, VALUE array) {
return rb_ary_sort(array);
}

static VALUE array_spec_rb_ary_sort_bang(VALUE self, VALUE array) {
return rb_ary_sort_bang(array);
}

static VALUE array_spec_rb_ary_store(VALUE self, VALUE array, VALUE offset, VALUE value) {
rb_ary_store(array, FIX2INT(offset), value);

Expand Down Expand Up @@ -272,6 +280,8 @@ void Init_array_spec(void) {
rb_define_method(cls, "rb_ary_reverse", array_spec_rb_ary_reverse, 1);
rb_define_method(cls, "rb_ary_rotate", array_spec_rb_ary_rotate, 2);
rb_define_method(cls, "rb_ary_shift", array_spec_rb_ary_shift, 1);
rb_define_method(cls, "rb_ary_sort", array_spec_rb_ary_sort, 1);
rb_define_method(cls, "rb_ary_sort_bang", array_spec_rb_ary_sort_bang, 1);
rb_define_method(cls, "rb_ary_store", array_spec_rb_ary_store, 3);
rb_define_method(cls, "rb_ary_concat", array_spec_rb_ary_concat, 2);
rb_define_method(cls, "rb_ary_plus", array_spec_rb_ary_plus, 2);
Expand Down
8 changes: 8 additions & 0 deletions src/main/c/cext/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ VALUE rb_ary_pop(VALUE array) {
return RUBY_INVOKE(array, "pop");
}

VALUE rb_ary_sort(VALUE array) {
return RUBY_INVOKE(array, "sort");
}

VALUE rb_ary_sort_bang(VALUE array) {
return RUBY_INVOKE(array, "sort!");
}

void rb_ary_store(VALUE array, long index, VALUE value) {
RUBY_INVOKE_NO_WRAP(array, "[]=", LONG2FIX(index), value);
}
Expand Down

0 comments on commit 17701a0

Please sign in to comment.