From 34776105c8a6739ca3aad1de4a2c942f4a8f2f29 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 31 May 2020 18:22:49 +0200 Subject: Update to ruby/spec@4e486fa --- spec/ruby/optional/capi/encoding_spec.rb | 71 +++++++++++++++++++++++++++++ spec/ruby/optional/capi/ext/encoding_spec.c | 30 ++++++++++++ spec/ruby/optional/capi/ext/string_spec.c | 17 +++++++ spec/ruby/optional/capi/ext/util_spec.c | 15 ++++++ spec/ruby/optional/capi/string_spec.rb | 29 ++++++++++++ spec/ruby/optional/capi/util_spec.rb | 30 ++++++++++++ 6 files changed, 192 insertions(+) (limited to 'spec/ruby/optional') diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb index 430c2dfcc6..f550390c22 100644 --- a/spec/ruby/optional/capi/encoding_spec.rb +++ b/spec/ruby/optional/capi/encoding_spec.rb @@ -131,6 +131,29 @@ describe "C-API Encoding function" do end end + describe "rb_enc_precise_mbclen" do + it "returns the correct length for single byte characters" do + @s.rb_enc_precise_mbclen("hello", 7).should == 1 + @s.rb_enc_precise_mbclen("hello", 5).should == 1 + @s.rb_enc_precise_mbclen("hello", 1).should == 1 + @s.rb_enc_precise_mbclen("hello", 0).should == -2 + @s.rb_enc_precise_mbclen("hello", -1).should == -2 + @s.rb_enc_precise_mbclen("hello", -5).should == -2 + end + + it "returns the correct length for multi-byte characters" do + @s.rb_enc_precise_mbclen("ésumé", 2).should == 2 + @s.rb_enc_precise_mbclen("ésumé", 3).should == 2 + @s.rb_enc_precise_mbclen("ésumé", 0).should == -2 + @s.rb_enc_precise_mbclen("ésumé", 1).should == -2 + @s.rb_enc_precise_mbclen("あ", 20).should == 3 + @s.rb_enc_precise_mbclen("あ", 3).should == 3 + @s.rb_enc_precise_mbclen("あ", 2).should == -2 + @s.rb_enc_precise_mbclen("あ", 0).should == -2 + @s.rb_enc_precise_mbclen("あ", -2).should == -2 + end + end + describe "rb_obj_encoding" do it "returns the encoding associated with an object" do str = "abc".encode Encoding::BINARY @@ -173,6 +196,26 @@ describe "C-API Encoding function" do end end + describe "rb_enc_str_new_cstr" do + it "creates a new ruby string from a c string literal" do + result = @s.rb_enc_str_new_cstr_constant(Encoding::US_ASCII) + result.should == "test string literal" + result.encoding.should == Encoding::US_ASCII + end + + it "creates a new ruby string from a c string variable" do + result = @s.rb_enc_str_new_cstr("test string", Encoding::US_ASCII) + result.should == "test string" + result.encoding.should == Encoding::US_ASCII + end + + it "when null encoding is given with a c string literal, it creates a new ruby string with ASCII_8BIT encoding" do + result = @s.rb_enc_str_new_cstr_constant(nil) + result.should == "test string literal" + result.encoding.should == Encoding::ASCII_8BIT + end + end + describe "rb_enc_str_coderange" do describe "when the encoding is BINARY" do it "returns ENC_CODERANGE_7BIT if there are no high bits set" do @@ -217,6 +260,17 @@ describe "C-API Encoding function" do end end + describe "MBCLEN_CHARFOUND_P" do + it "returns non-zero for valid character" do + @s.MBCLEN_CHARFOUND_P("a".ord).should == 1 + end + + it "returns zero for invalid characters" do + @s.MBCLEN_CHARFOUND_P(0).should == 0 + @s.MBCLEN_CHARFOUND_P(-1).should == 0 + end + end + describe "ENCODING_GET" do it_behaves_like :rb_enc_get_index, :ENCODING_GET end @@ -497,4 +551,21 @@ describe "C-API Encoding function" do @s.rb_enc_str_asciionly_p("hüllo").should be_false end end + + describe "rb_uv_to_utf8" do + it 'converts a Unicode codepoint to a UTF-8 C string' do + str = ' ' * 6 + { + 0 => "\x01", + 0x7f => "\xC2\x80", + 0x7ff => "\xE0\xA0\x80", + 0xffff => "\xF0\x90\x80\x80", + 0x1fffff => "\xF8\x88\x80\x80\x80", + 0x3ffffff => "\xFC\x84\x80\x80\x80\x80", + }.each do |num, result| + len = @s.rb_uv_to_utf8(str, num + 1) + str[0..len-1].should == result + end + end + end end diff --git a/spec/ruby/optional/capi/ext/encoding_spec.c b/spec/ruby/optional/capi/ext/encoding_spec.c index 6b3a8be01a..6e2e7cece1 100644 --- a/spec/ruby/optional/capi/ext/encoding_spec.c +++ b/spec/ruby/optional/capi/ext/encoding_spec.c @@ -7,6 +7,10 @@ extern "C" { #endif +static VALUE encoding_spec_MBCLEN_CHARFOUND_P(VALUE self, VALUE obj) { + return INT2FIX(MBCLEN_CHARFOUND_P(FIX2INT(obj))); +} + static VALUE encoding_spec_ENC_CODERANGE_ASCIIONLY(VALUE self, VALUE obj) { if(ENC_CODERANGE_ASCIIONLY(obj)) { return Qtrue; @@ -114,6 +118,13 @@ static VALUE encoding_spec_rb_enc_get(VALUE self, VALUE obj) { return rb_str_new2(rb_enc_get(obj)->name); } +static VALUE encoding_spec_rb_enc_precise_mbclen(VALUE self, VALUE str, VALUE offset) { + int o = FIX2INT(offset); + char *p = RSTRING_PTR(str); + char *e = p + o; + return INT2FIX(rb_enc_precise_mbclen(p, e, rb_enc_get(str))); +} + static VALUE encoding_spec_rb_obj_encoding(VALUE self, VALUE obj) { return rb_obj_encoding(obj); } @@ -149,6 +160,16 @@ static VALUE encoding_spec_rb_enc_str_coderange(VALUE self, VALUE str) { } } +static VALUE encoding_spec_rb_enc_str_new_cstr(VALUE self, VALUE str, VALUE enc) { + rb_encoding *e = rb_to_encoding(enc); + return rb_enc_str_new_cstr(StringValueCStr(str), e); +} + +static VALUE encoding_spec_rb_enc_str_new_cstr_constant(VALUE self, VALUE enc) { + rb_encoding *e = NIL_P(enc) ? NULL : rb_to_encoding(enc); + return rb_enc_str_new_cstr("test string literal", e); +} + static VALUE encoding_spec_rb_enc_str_new(VALUE self, VALUE str, VALUE len, VALUE enc) { return rb_enc_str_new(RSTRING_PTR(str), FIX2INT(len), rb_to_encoding(enc)); } @@ -219,6 +240,10 @@ static VALUE encoding_spec_rb_enc_str_asciionly_p(VALUE self, VALUE str) { } } +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))); +} + void Init_encoding_spec(void) { VALUE cls; native_rb_encoding_pointer = (rb_encoding**) malloc(sizeof(rb_encoding*)); @@ -247,6 +272,7 @@ void Init_encoding_spec(void) { rb_define_method(cls, "rb_enc_alias", encoding_spec_rb_enc_alias, 2); #endif + rb_define_method(cls, "MBCLEN_CHARFOUND_P", encoding_spec_MBCLEN_CHARFOUND_P, 1); rb_define_method(cls, "rb_enc_associate", encoding_spec_rb_enc_associate, 2); 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); @@ -256,10 +282,13 @@ void Init_encoding_spec(void) { rb_define_method(cls, "rb_enc_from_index", encoding_spec_rb_enc_from_index, 1); 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); rb_define_method(cls, "rb_obj_encoding", encoding_spec_rb_obj_encoding, 1); rb_define_method(cls, "rb_enc_get_index", encoding_spec_rb_enc_get_index, 1); rb_define_method(cls, "rb_enc_set_index", encoding_spec_rb_enc_set_index, 2); rb_define_method(cls, "rb_enc_str_coderange", encoding_spec_rb_enc_str_coderange, 1); + rb_define_method(cls, "rb_enc_str_new_cstr", encoding_spec_rb_enc_str_new_cstr, 2); + rb_define_method(cls, "rb_enc_str_new_cstr_constant", encoding_spec_rb_enc_str_new_cstr_constant, 1); rb_define_method(cls, "rb_enc_str_new", encoding_spec_rb_enc_str_new, 3); rb_define_method(cls, "ENCODING_GET", encoding_spec_ENCODING_GET, 1); rb_define_method(cls, "ENCODING_SET", encoding_spec_ENCODING_SET, 2); @@ -271,6 +300,7 @@ void Init_encoding_spec(void) { rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2); 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); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index 270412673e..b6e03f29a5 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -49,6 +49,12 @@ VALUE string_spec_rb_str_set_len_RSTRING_LEN(VALUE self, VALUE str, VALUE len) { return INT2FIX(RSTRING_LEN(str)); } +VALUE rb_fstring(VALUE str); /* internal.h, used in ripper */ + +VALUE string_spec_rb_str_fstring(VALUE self, VALUE str) { + return rb_fstring(str); +} + VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) { VALUE buf; @@ -93,6 +99,14 @@ VALUE string_spec_rb_str_cat2(VALUE self, VALUE str) { return rb_str_cat2(str, "?"); } +VALUE string_spec_rb_str_cat_cstr(VALUE self, VALUE str, VALUE other) { + return rb_str_cat_cstr(str, StringValueCStr(other)); +} + +VALUE string_spec_rb_str_cat_cstr_constant(VALUE self, VALUE str) { + return rb_str_cat_cstr(str, "?"); +} + VALUE string_spec_rb_str_cmp(VALUE self, VALUE str1, VALUE str2) { return INT2NUM(rb_str_cmp(str1, str2)); } @@ -452,6 +466,7 @@ void Init_string_spec(void) { VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject); rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2); rb_define_method(cls, "rb_cstr_to_inum", string_spec_rb_cstr_to_inum, 3); + rb_define_method(cls, "rb_fstring", string_spec_rb_str_fstring, 1); rb_define_method(cls, "rb_str2inum", string_spec_rb_str2inum, 2); rb_define_method(cls, "rb_str_append", string_spec_rb_str_append, 2); rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2); @@ -462,6 +477,8 @@ void Init_string_spec(void) { rb_define_method(cls, "rb_str_buf_cat", string_spec_rb_str_buf_cat, 1); rb_define_method(cls, "rb_str_cat", string_spec_rb_str_cat, 1); rb_define_method(cls, "rb_str_cat2", string_spec_rb_str_cat2, 1); + rb_define_method(cls, "rb_str_cat_cstr", string_spec_rb_str_cat_cstr, 2); + rb_define_method(cls, "rb_str_cat_cstr_constant", string_spec_rb_str_cat_cstr_constant, 1); rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2); rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3); rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5); diff --git a/spec/ruby/optional/capi/ext/util_spec.c b/spec/ruby/optional/capi/ext/util_spec.c index f7b45de6b6..a7269353c2 100644 --- a/spec/ruby/optional/capi/ext/util_spec.c +++ b/spec/ruby/optional/capi/ext/util_spec.c @@ -1,4 +1,5 @@ #include "ruby.h" +#include "ruby/util.h" #include "rubyspec.h" #ifdef __cplusplus @@ -93,6 +94,18 @@ static VALUE util_spec_rb_sourceline(VALUE self) { return INT2NUM(rb_sourceline()); } +static VALUE util_spec_strtod(VALUE self, VALUE string) { + char *endptr = NULL; + double value = strtod(RSTRING_PTR(string), &endptr); + return rb_ary_new_from_args(2, rb_float_new(value), endptr ? rb_str_new2(endptr) : Qnil); +} + +static VALUE util_spec_ruby_strtod(VALUE self, VALUE string) { + char *endptr = NULL; + double value = ruby_strtod(RSTRING_PTR(string), &endptr); + return rb_ary_new_from_args(2, rb_float_new(value), endptr ? rb_str_new2(endptr) : Qnil); +} + void Init_util_spec(void) { VALUE cls = rb_define_class("CApiUtilSpecs", rb_cObject); rb_define_method(cls, "rb_scan_args", util_spec_rb_scan_args, 4); @@ -101,6 +114,8 @@ void Init_util_spec(void) { rb_define_method(cls, "rb_iter_break", util_spec_rb_iter_break, 0); rb_define_method(cls, "rb_sourcefile", util_spec_rb_sourcefile, 0); rb_define_method(cls, "rb_sourceline", util_spec_rb_sourceline, 0); + rb_define_method(cls, "strtod", util_spec_strtod, 1); + rb_define_method(cls, "ruby_strtod", util_spec_ruby_strtod, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index 57ac9545f4..35a6fd1c92 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -382,6 +382,16 @@ describe "C-API String function" do end end + describe "rb_str_cat_cstr" do + it "concatenates a C string literal to a ruby string" do + @s.rb_str_cat_cstr_constant("Your house is on fire").should == "Your house is on fire?" + end + + it "concatenates a variable C string to a ruby string" do + @s.rb_str_cat_cstr("Your house is on fire", "?").should == "Your house is on fire?" + end + end + describe "rb_str_cmp" do it "returns 0 if two strings are identical" do @s.rb_str_cmp("ppp", "ppp").should == 0 @@ -454,6 +464,25 @@ describe "C-API String function" do end end + describe "rb_fstring" do + it 'returns self if the String is frozen' do + input = 'foo'.freeze + output = @s.rb_fstring(input) + + output.should equal(input) + output.should.frozen? + end + + it 'returns a frozen copy if the String is not frozen' do + input = 'foo' + output = @s.rb_fstring(input) + + output.should.frozen? + output.should_not equal(input) + output.should == 'foo' + end + end + describe "rb_str_subseq" do it "returns a byte-indexed substring" do str = "\x00\x01\x02\x03\x04".force_encoding("binary") diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb index cf19c55b57..099222b2d0 100644 --- a/spec/ruby/optional/capi/util_spec.rb +++ b/spec/ruby/optional/capi/util_spec.rb @@ -293,4 +293,34 @@ describe "C-API Util function" do end end + # ruby/util.h redefines strtod as a macro calling ruby_strtod + + describe "strtod" do + it "converts a string to a double and returns the remaining string" do + d, s = @o.strtod("14.25test") + d.should == 14.25 + s.should == "test" + end + + it "returns 0 and the full string if there's no numerical value" do + d, s = @o.strtod("test") + d.should == 0 + s.should == "test" + end + end + + describe "ruby_strtod" do + it "converts a string to a double and returns the remaining string" do + d, s = @o.ruby_strtod("14.25test") + d.should == 14.25 + s.should == "test" + end + + it "returns 0 and the full string if there's no numerical value" do + d, s = @o.ruby_strtod("test") + d.should == 0 + s.should == "test" + end + end + end -- cgit v1.2.3