diff options
author | Benoit Daloze <eregontp@gmail.com> | 2020-07-27 21:41:08 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2020-07-27 21:41:08 +0200 |
commit | 126fd5f15cff0d3bf314d90d8c21a3ae25ae8e68 (patch) | |
tree | 33350f7170436c32ed4c8e79f0be2c334c7bc8a9 /spec/ruby/optional | |
parent | 7429841ab6494b849106e6d3b119f147adfee3b7 (diff) | |
download | ruby-126fd5f15cff0d3bf314d90d8c21a3ae25ae8e68.tar.gz |
Update to ruby/spec@07164da
Diffstat (limited to 'spec/ruby/optional')
-rw-r--r-- | spec/ruby/optional/capi/encoding_spec.rb | 38 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/encoding_spec.c | 37 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/typed_data_spec.c | 15 | ||||
-rw-r--r-- | spec/ruby/optional/capi/object_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/optional/capi/spec_helper.rb | 1 | ||||
-rw-r--r-- | spec/ruby/optional/capi/typed_data_spec.rb | 17 |
6 files changed, 110 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb index 8e59ee8b1b..93bde54069 100644 --- a/spec/ruby/optional/capi/encoding_spec.rb +++ b/spec/ruby/optional/capi/encoding_spec.rb @@ -58,6 +58,15 @@ describe "C-API Encoding function" do end end + describe "rb_enc_codelen" do + it "returns the correct length for the given codepoint" do + @s.rb_enc_codelen(0x24, Encoding::UTF_8).should == 1 + @s.rb_enc_codelen(0xA2, Encoding::UTF_8).should == 2 + @s.rb_enc_codelen(0x20AC, Encoding::UTF_8).should == 3 + @s.rb_enc_codelen(0x24B62, Encoding::UTF_8).should == 4 + end + end + describe "rb_enc_find" do it "returns the encoding of an Encoding" do @s.rb_enc_find("UTF-8").should == "UTF-8" @@ -130,6 +139,18 @@ describe "C-API Encoding function" do end end + describe "rb_enc_mbcput" do + it "writes the correct bytes to the buffer" do + @s.rb_enc_mbcput(0x24, Encoding::UTF_8).should == "$" + @s.rb_enc_mbcput(0xA2, Encoding::UTF_8).should == "¢" + @s.rb_enc_mbcput(0x20AC, Encoding::UTF_8).should == "€" + @s.rb_enc_mbcput(0x24B62, Encoding::UTF_8).should == "𤭢" + + @s.rb_enc_mbcput(0x24, Encoding::UTF_16BE).bytes.should == [0, 0x24] + @s.rb_enc_mbcput(0x24B62, Encoding::UTF_16LE).bytes.should == [82, 216, 98, 223] + end + end + describe "rb_usascii_encoding" do it "returns the encoding for Encoding::US_ASCII" do @s.rb_usascii_encoding.should == "US-ASCII" @@ -610,4 +631,21 @@ describe "C-API Encoding function" do end end end + + describe "ONIGENC_MBC_CASE_FOLD" do + it "returns the correct case fold for the given string" do + @s.ONIGENC_MBC_CASE_FOLD("lower").should == ["l", 1] + @s.ONIGENC_MBC_CASE_FOLD("Upper").should == ["u", 1] + end + + it "works with other encodings" do + @s.ONIGENC_MBC_CASE_FOLD("lower".force_encoding("binary")).should == ["l", 1] + @s.ONIGENC_MBC_CASE_FOLD("Upper".force_encoding("binary")).should == ["u", 1] + @s.ONIGENC_MBC_CASE_FOLD("É").should == ["é", 2] + + str, length = @s.ONIGENC_MBC_CASE_FOLD('$'.encode(Encoding::UTF_16BE)) + length.should == 2 + str.bytes.should == [0, 0x24] + end + end end diff --git a/spec/ruby/optional/capi/ext/encoding_spec.c b/spec/ruby/optional/capi/ext/encoding_spec.c index 8935db966e..cde4d0c351 100644 --- a/spec/ruby/optional/capi/ext/encoding_spec.c +++ b/spec/ruby/optional/capi/ext/encoding_spec.c @@ -127,6 +127,18 @@ static VALUE encoding_spec_rb_enc_mbc_to_codepoint(VALUE self, VALUE str, VALUE return INT2FIX(rb_enc_mbc_to_codepoint(p, e, rb_enc_get(str))); } +static VALUE encoding_spec_rb_enc_mbcput(VALUE self, VALUE code, VALUE encoding) { + unsigned int c = FIX2UINT(code); + rb_encoding *enc = rb_to_encoding(encoding); + char buf[ONIGENC_CODE_TO_MBC_MAXLEN]; + memset(buf, '\1', sizeof(buf)); + int len = rb_enc_mbcput(c, buf, enc); + if (buf[len] != '\1') { + rb_raise(rb_eRuntimeError, "should not change bytes after len"); + } + return rb_enc_str_new(buf, len, enc); +} + static VALUE encoding_spec_rb_enc_from_encoding(VALUE self, VALUE name) { return rb_enc_from_encoding(rb_enc_find(RSTRING_PTR(name))); } @@ -266,6 +278,28 @@ static VALUE encoding_spec_rb_uv_to_utf8(VALUE self, VALUE buf, VALUE num) { return INT2NUM(rb_uv_to_utf8(RSTRING_PTR(buf), NUM2INT(num))); } +static VALUE encoding_spec_ONIGENC_MBC_CASE_FOLD(VALUE self, VALUE str) { + char *beg = RSTRING_PTR(str); + char *beg_initial = beg; + char *end = beg + 2; + OnigUChar fold[ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM]; + memset(fold, '\1', sizeof(fold)); + rb_encoding *enc = rb_enc_get(str); + int r = ONIGENC_MBC_CASE_FOLD(enc, ONIGENC_CASE_FOLD, &beg, (const OnigUChar *)end, fold); + if (r > 0 && fold[r] != '\1') { + rb_raise(rb_eRuntimeError, "should not change bytes after len"); + } + VALUE str_result = r <= 0 ? Qnil : rb_enc_str_new((char *)fold, r, enc); + long bytes_used = beg - beg_initial; + return rb_ary_new3(2, str_result, INT2FIX(bytes_used)); +} + +static VALUE encoding_spec_rb_enc_codelen(VALUE self, VALUE code, VALUE encoding) { + unsigned int c = FIX2UINT(code); + rb_encoding *enc = rb_to_encoding(encoding); + return INT2FIX(rb_enc_codelen(c, enc)); +} + void Init_encoding_spec(void) { VALUE cls; native_rb_encoding_pointer = (rb_encoding**) malloc(sizeof(rb_encoding*)); @@ -299,12 +333,14 @@ void Init_encoding_spec(void) { rb_define_method(cls, "rb_enc_associate_index", encoding_spec_rb_enc_associate_index, 2); rb_define_method(cls, "rb_enc_compatible", encoding_spec_rb_enc_compatible, 2); rb_define_method(cls, "rb_enc_copy", encoding_spec_rb_enc_copy, 2); + rb_define_method(cls, "rb_enc_codelen", encoding_spec_rb_enc_codelen, 2); rb_define_method(cls, "rb_enc_find", encoding_spec_rb_enc_find, 1); rb_define_method(cls, "rb_enc_find_index", encoding_spec_rb_enc_find_index, 1); rb_define_method(cls, "rb_enc_isalnum", encoding_spec_rb_enc_isalnum, 2); rb_define_method(cls, "rb_enc_isspace", encoding_spec_rb_enc_isspace, 2); rb_define_method(cls, "rb_enc_from_index", encoding_spec_rb_enc_from_index, 1); rb_define_method(cls, "rb_enc_mbc_to_codepoint", encoding_spec_rb_enc_mbc_to_codepoint, 2); + rb_define_method(cls, "rb_enc_mbcput", encoding_spec_rb_enc_mbcput, 2); rb_define_method(cls, "rb_enc_from_encoding", encoding_spec_rb_enc_from_encoding, 1); rb_define_method(cls, "rb_enc_get", encoding_spec_rb_enc_get, 1); rb_define_method(cls, "rb_enc_precise_mbclen", encoding_spec_rb_enc_precise_mbclen, 2); @@ -326,6 +362,7 @@ void Init_encoding_spec(void) { rb_define_method(cls, "rb_enc_codepoint_len", encoding_spec_rb_enc_codepoint_len, 1); rb_define_method(cls, "rb_enc_str_asciionly_p", encoding_spec_rb_enc_str_asciionly_p, 1); rb_define_method(cls, "rb_uv_to_utf8", encoding_spec_rb_uv_to_utf8, 2); + rb_define_method(cls, "ONIGENC_MBC_CASE_FOLD", encoding_spec_ONIGENC_MBC_CASE_FOLD, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/typed_data_spec.c b/spec/ruby/optional/capi/ext/typed_data_spec.c index 6eb91b76bf..70f21ce36f 100644 --- a/spec/ruby/optional/capi/ext/typed_data_spec.c +++ b/spec/ruby/optional/capi/ext/typed_data_spec.c @@ -148,6 +148,18 @@ VALUE sws_typed_change_struct(VALUE self, VALUE obj, VALUE new_val) { return Qnil; } +VALUE sws_typed_rb_check_typeddata_same_type(VALUE self, VALUE obj) { + return rb_check_typeddata(obj, &sample_typed_wrapped_struct_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; +} + +VALUE sws_typed_rb_check_typeddata_same_type_parent(VALUE self, VALUE obj) { + return rb_check_typeddata(obj, &sample_typed_wrapped_struct_parent_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; +} + +VALUE sws_typed_rb_check_typeddata_different_type(VALUE self, VALUE obj) { + return rb_check_typeddata(obj, &sample_typed_wrapped_struct_other_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; +} + void Init_typed_data_spec(void) { VALUE cls = rb_define_class("CApiAllocTypedSpecs", rb_cObject); rb_define_alloc_func(cls, sdaf_alloc_typed_func); @@ -160,6 +172,9 @@ void Init_typed_data_spec(void) { rb_define_method(cls, "typed_get_struct_rdata", sws_typed_get_struct_rdata, 1); rb_define_method(cls, "typed_get_struct_data_ptr", sws_typed_get_struct_data_ptr, 1); rb_define_method(cls, "typed_change_struct", sws_typed_change_struct, 2); + rb_define_method(cls, "rb_check_typeddata_same_type", sws_typed_rb_check_typeddata_same_type, 1); + rb_define_method(cls, "rb_check_typeddata_same_type_parent", sws_typed_rb_check_typeddata_same_type_parent, 1); + rb_define_method(cls, "rb_check_typeddata_different_type", sws_typed_rb_check_typeddata_different_type, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb index bbcaec2ba8..484dfb851c 100644 --- a/spec/ruby/optional/capi/object_spec.rb +++ b/spec/ruby/optional/capi/object_spec.rb @@ -831,6 +831,7 @@ describe "CApiObject" do it "returns nil if the instance variable has not been initialized and is not a valid Ruby name" do @o.rb_ivar_get(@test, :bar).should == nil + @o.rb_ivar_get(@test, :mesg).should == nil end it 'returns the instance variable when it is not a valid Ruby name' do @@ -866,6 +867,7 @@ describe "CApiObject" do it "does not throw an error if the instance variable is not a valid Ruby name" do @o.rb_ivar_defined(@test, :bar).should == false + @o.rb_ivar_defined(@test, :mesg).should == false end end diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb index a7029a74ed..3b13e0854d 100644 --- a/spec/ruby/optional/capi/spec_helper.rb +++ b/spec/ruby/optional/capi/spec_helper.rb @@ -76,6 +76,7 @@ def compile_extension(name) $ruby = ENV.values_at('RUBY_EXE', 'RUBY_FLAGS').join(' ') # MRI magic to consider building non-bundled extensions $extout = nil + $warnflags << ' -Wno-declaration-after-statement' create_makefile(#{ext.inspect}) RUBY output = ruby_exe("extconf.rb") diff --git a/spec/ruby/optional/capi/typed_data_spec.rb b/spec/ruby/optional/capi/typed_data_spec.rb index da56a050fd..9ccfa562d1 100644 --- a/spec/ruby/optional/capi/typed_data_spec.rb +++ b/spec/ruby/optional/capi/typed_data_spec.rb @@ -57,4 +57,21 @@ describe "CApiWrappedTypedStruct" do @s.typed_get_struct_data_ptr(a).should == 1024 end end + + describe "rb_check_typeddata" do + it "returns data pointer when the struct has the given type" do + a = @s.typed_wrap_struct(1024) + @s.rb_check_typeddata_same_type(a).should == true + end + + it "returns data pointer when the parent struct has the given type" do + a = @s.typed_wrap_struct(1024) + @s.rb_check_typeddata_same_type_parent(a).should == true + end + + it "raises an error for different types" do + a = @s.typed_wrap_struct(1024) + -> { @s.rb_check_typeddata_different_type(a) }.should raise_error(TypeError) + end + end end |