From b46da8d84efeb97cb52ba0560d5b149ccda0d561 Mon Sep 17 00:00:00 2001 From: eregon Date: Wed, 13 Jun 2018 21:58:54 +0000 Subject: Update to ruby/spec@4bb0f25 * Specs added by TruffleRuby. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63654 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/optional/capi/class_spec.rb | 6 +++ spec/ruby/optional/capi/ext/string_spec.c | 46 ++++++++++++++++++++- spec/ruby/optional/capi/fixtures/class.rb | 11 ++++- spec/ruby/optional/capi/string_spec.rb | 68 +++++++++++++++++++++++++++---- 4 files changed, 120 insertions(+), 11 deletions(-) (limited to 'spec/ruby/optional/capi') diff --git a/spec/ruby/optional/capi/class_spec.rb b/spec/ruby/optional/capi/class_spec.rb index acfb8e3728..bab4f52831 100644 --- a/spec/ruby/optional/capi/class_spec.rb +++ b/spec/ruby/optional/capi/class_spec.rb @@ -95,6 +95,12 @@ describe "C-API Class function" do obj.call_super_method.should == :super_method end + it "calls the method in the superclass with the correct self" do + @s.define_call_super_method CApiClassSpecs::SubSelf, "call_super_method" + obj = CApiClassSpecs::SubSelf.new + obj.call_super_method.should equal obj + end + it "calls the method in the superclass through two native levels" do @s.define_call_super_method CApiClassSpecs::Sub, "call_super_method" @s.define_call_super_method CApiClassSpecs::SubSub, "call_super_method" diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index 929d69f9e5..1b9c11a149 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -12,6 +12,19 @@ extern "C" { #endif +/* Make sure the RSTRING_PTR and the bytes are in native memory. + * On TruffleRuby RSTRING_PTR and the bytes remain in managed memory + * until they must be written to native memory. + * In some specs we want to test using the native memory. */ +char* NATIVE_RSTRING_PTR(VALUE str) { + char* ptr = RSTRING_PTR(str); + char** native = malloc(sizeof(char*)); + *native = ptr; + ptr = *native; + free(native); + return ptr; +} + #ifdef HAVE_RB_CSTR2INUM VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) { int num = FIX2INT(inum); @@ -65,6 +78,10 @@ VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) { return buf; } + +VALUE string_spec_rb_str_capacity(VALUE self, VALUE str) { + return SIZET2NUM(rb_str_capacity(str)); +} #endif #ifdef HAVE_RB_STR_BUF_NEW2 @@ -182,6 +199,10 @@ VALUE string_spec_rb_str_new(VALUE self, VALUE str, VALUE len) { return rb_str_new(RSTRING_PTR(str), FIX2INT(len)); } +VALUE string_spec_rb_str_new_native(VALUE self, VALUE str, VALUE len) { + return rb_str_new(NATIVE_RSTRING_PTR(str), FIX2INT(len)); +} + VALUE string_spec_rb_str_new_offset(VALUE self, VALUE str, VALUE offset, VALUE len) { return rb_str_new(RSTRING_PTR(str) + FIX2INT(offset), FIX2INT(len)); } @@ -358,6 +379,11 @@ VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) { return Qnil; } +VALUE string_spec_RSTRING_PTR_set(VALUE self, VALUE str, VALUE i, VALUE chr) { + RSTRING_PTR(str)[FIX2INT(i)] = (char) FIX2INT(chr); + return str; +} + VALUE string_spec_RSTRING_PTR_after_funcall(VALUE self, VALUE str, VALUE cb) { /* Silence gcc 4.3.2 warning about computed value not used */ if(RSTRING_PTR(str)) { /* force it out */ @@ -366,6 +392,19 @@ VALUE string_spec_RSTRING_PTR_after_funcall(VALUE self, VALUE str, VALUE cb) { return rb_str_new2(RSTRING_PTR(str)); } + +VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) { + char* ptr = NATIVE_RSTRING_PTR(str); + long len = RSTRING_LEN(str); + VALUE from_rstring_ptr; + + ptr[0] = '1'; + rb_yield(str); + ptr[2] = '2'; + + from_rstring_ptr = rb_str_new(ptr, len); + return from_rstring_ptr; +} #endif #ifdef HAVE_STRINGVALUE @@ -480,6 +519,7 @@ void Init_string_spec(void) { #ifdef HAVE_RB_STR_BUF_NEW rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2); + rb_define_method(cls, "rb_str_capacity", string_spec_rb_str_capacity, 1); #endif #ifdef HAVE_RB_STR_BUF_NEW2 @@ -540,6 +580,7 @@ void Init_string_spec(void) { #ifdef HAVE_RB_STR_NEW rb_define_method(cls, "rb_str_new", string_spec_rb_str_new, 2); + rb_define_method(cls, "rb_str_new_native", string_spec_rb_str_new_native, 2); rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3); #endif @@ -643,8 +684,9 @@ void Init_string_spec(void) { #ifdef HAVE_RSTRING_PTR rb_define_method(cls, "RSTRING_PTR_iterate", string_spec_RSTRING_PTR_iterate, 1); rb_define_method(cls, "RSTRING_PTR_assign", string_spec_RSTRING_PTR_assign, 2); - rb_define_method(cls, "RSTRING_PTR_after_funcall", - string_spec_RSTRING_PTR_after_funcall, 2); + rb_define_method(cls, "RSTRING_PTR_set", string_spec_RSTRING_PTR_set, 3); + rb_define_method(cls, "RSTRING_PTR_after_funcall", string_spec_RSTRING_PTR_after_funcall, 2); + rb_define_method(cls, "RSTRING_PTR_after_yield", string_spec_RSTRING_PTR_after_yield, 1); #endif #ifdef HAVE_STRINGVALUE diff --git a/spec/ruby/optional/capi/fixtures/class.rb b/spec/ruby/optional/capi/fixtures/class.rb index de824b3ab0..dbb0b69967 100644 --- a/spec/ruby/optional/capi/fixtures/class.rb +++ b/spec/ruby/optional/capi/fixtures/class.rb @@ -68,10 +68,19 @@ class CApiClassSpecs class SubSub < Sub def call_super_method - :subclass_method + :subsubclass_method end end + class SuperSelf + def call_super_method + self + end + end + + class SubSelf < SuperSelf + end + class A C = 1 autoload :D, File.expand_path('../path_to_class.rb', __FILE__) diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index c042d5edad..cc6ac2e0cb 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -48,14 +48,43 @@ describe "C-API String function" do @s.rb_str_set_len(@str, 8).should == "abcde\x00gh" end + it "updates the byte size and character size" do + @s.rb_str_set_len(@str, 4) + @str.bytesize.should == 4 + @str.size.should == 4 + @str.should == "abcd" + end + it "updates the string's attributes visible in C code" do @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4 end + + it "can reveal characters written from C with RSTRING_PTR" do + @s.rb_str_set_len(@str, 1) + @str.should == "a" + + @str.force_encoding(Encoding::UTF_8) + @s.RSTRING_PTR_set(@str, 1, 'B'.ord) + @s.RSTRING_PTR_set(@str, 2, 'C'.ord) + @s.rb_str_set_len(@str, 3) + + @str.bytesize.should == 3 + @str.should == "aBC" + end end describe "rb_str_buf_new" do it "returns the equivalent of an empty string" do - @s.rb_str_buf_new(10, nil).should == "" + buf = @s.rb_str_buf_new(10, nil) + buf.should == "" + buf.bytesize.should == 0 + buf.size.should == 0 + @s.RSTRING_LEN(buf).should == 0 + end + + it "returns a string with the given capacity" do + buf = @s.rb_str_buf_new(256, nil) + @s.rb_str_capacity(buf).should == 256 end it "returns a string that can be appended to" do @@ -83,6 +112,19 @@ describe "C-API String function" do str[0, 6].should == "abcd\x00f" @s.RSTRING_LEN(str).should == 8 end + + it "can be used as a general buffer and reveal characters with rb_str_set_len" do + str = @s.rb_str_buf_new(10, "abcdef") + + @s.RSTRING_PTR_set(str, 0, 195) + @s.RSTRING_PTR_set(str, 1, 169) + @s.rb_str_set_len(str, 2) + + str.force_encoding(Encoding::UTF_8) + str.bytesize.should == 2 + str.size.should == 1 + str.should == "é" + end end describe "rb_str_buf_new2" do @@ -93,6 +135,10 @@ describe "C-API String function" do end describe "rb_str_new" do + it "creates a new String with ASCII-8BIT Encoding" do + @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT + end + it "returns a new string object from a char buffer of len characters" do @s.rb_str_new("hello", 3).should == "hel" end @@ -101,6 +147,11 @@ describe "C-API String function" do @s.rb_str_new("hello", 0).should == "" end + it "copy length bytes and does not stop at the first \\0 byte" do + @s.rb_str_new("he\x00llo", 6).should == "he\x00llo" + @s.rb_str_new_native("he\x00llo", 6).should == "he\x00llo" + end + it "returns a string from an offset char buffer" do @s.rb_str_new_offset("hello", 1, 3).should == "ell" end @@ -110,12 +161,6 @@ describe "C-API String function" do it_behaves_like :rb_str_new2, :rb_str_new2 end - describe "rb_str_new" do - it "creates a new String with ASCII-8BIT Encoding" do - @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT - end - end - describe "rb_str_new_cstr" do it_behaves_like :rb_str_new2, :rb_str_new_cstr end @@ -402,7 +447,7 @@ describe "C-API String function" do it "allows changing the characters in the string" do str = "abc" - @s.RSTRING_PTR_assign(str, 65) + @s.RSTRING_PTR_assign(str, 'A'.ord) str.should == "AAA" end @@ -417,6 +462,13 @@ describe "C-API String function" do ret.should == str end + it "reflects changes from native memory and from String#setbyte in bounds" do + str = "abc" + from_rstring_ptr = @s.RSTRING_PTR_after_yield(str) { str.setbyte(1, 'B'.ord) } + from_rstring_ptr.should == "1B2" + str.should == "1B2" + end + it "returns a pointer to the contents of encoded pointer-sized string" do s = "70パク". encode(Encoding::UTF_16LE). -- cgit v1.2.3