diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-27 21:55:02 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-27 21:55:02 +0000 |
commit | ead40959357817c0937553a45208ef748975e340 (patch) | |
tree | 307b315a455953e97bd86cf2bfdc5d53a04cf581 /spec/rubyspec/optional | |
parent | 24db428785c938d401c9e582b5ea2622540222ad (diff) |
Update to ruby/spec@2795010
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58931 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/optional')
-rw-r--r-- | spec/rubyspec/optional/capi/boolean_spec.rb (renamed from spec/rubyspec/optional/capi/false_spec.rb) | 14 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/ext/io_spec.c | 6 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/ext/kernel_spec.c | 32 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/ext/rubyspec.h | 1 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/io_spec.rb | 10 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/kernel_spec.rb | 42 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/spec_helper.rb | 53 | ||||
-rw-r--r-- | spec/rubyspec/optional/capi/true_spec.rb | 21 |
8 files changed, 124 insertions, 55 deletions
diff --git a/spec/rubyspec/optional/capi/false_spec.rb b/spec/rubyspec/optional/capi/boolean_spec.rb index 8c0d94cf70..49303662e7 100644 --- a/spec/rubyspec/optional/capi/false_spec.rb +++ b/spec/rubyspec/optional/capi/boolean_spec.rb @@ -2,11 +2,23 @@ require File.expand_path('../spec_helper', __FILE__) load_extension("boolean") -describe "CApiFalseSpecs" do +describe "CApiBooleanSpecs" do before :each do @b = CApiBooleanSpecs.new end + describe "a true value from Ruby" do + it "is truthy in C" do + @b.is_true(true).should == 1 + end + end + + describe "a true value from Qtrue" do + it "is truthy in C" do + @b.is_true(@b.q_true).should == 1 + end + end + describe "a false value from Ruby" do it "is falsey in C" do @b.is_true(false).should == 2 diff --git a/spec/rubyspec/optional/capi/ext/io_spec.c b/spec/rubyspec/optional/capi/ext/io_spec.c index 491ceed9cb..751cb33fd8 100644 --- a/spec/rubyspec/optional/capi/ext/io_spec.c +++ b/spec/rubyspec/optional/capi/ext/io_spec.c @@ -2,7 +2,7 @@ #include "rubyspec.h" #include "ruby/io.h" #include <fcntl.h> -#if HAVE_UNISTD_H +#ifdef HAVE_UNISTD_H #include <unistd.h> #endif @@ -145,13 +145,13 @@ VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) { wait_bool ret; if (set_non_blocking(fd) == -1) - rb_sys_fail(0); + rb_sys_fail("set_non_blocking failed"); if(RTEST(read_p)) { - rb_ivar_set(self, rb_intern("@write_data"), Qtrue); if(read(fd, buf, RB_IO_WAIT_READABLE_BUF) != -1) { return Qnil; } + rb_ivar_set(self, rb_intern("@write_data"), Qtrue); } ret = rb_io_wait_readable(fd); diff --git a/spec/rubyspec/optional/capi/ext/kernel_spec.c b/spec/rubyspec/optional/capi/ext/kernel_spec.c index d855b5689f..f126bd0a4a 100644 --- a/spec/rubyspec/optional/capi/ext/kernel_spec.c +++ b/spec/rubyspec/optional/capi/ext/kernel_spec.c @@ -183,6 +183,18 @@ VALUE kernel_spec_rb_rescue2(int argc, VALUE *args, VALUE self) { } #endif +#ifdef HAVE_RB_PROTECT +static VALUE kernel_spec_rb_protect_yield(VALUE self, VALUE obj, VALUE ary) { + int status = 0; + VALUE res = rb_protect(rb_yield, obj, &status); + rb_ary_store(ary, 0, INT2NUM(23)); + if (status) { + rb_jump_tag(status); + } + return res; +} +#endif + #ifdef HAVE_RB_SYS_FAIL VALUE kernel_spec_rb_sys_fail(VALUE self, VALUE msg) { errno = 1; @@ -217,6 +229,21 @@ VALUE kernel_spec_rb_warn(VALUE self, VALUE msg) { static VALUE kernel_spec_rb_yield(VALUE self, VALUE obj) { return rb_yield(obj); } + +static int kernel_cb(const void *a, const void *b) { + rb_yield(Qtrue); + return 0; +} + +static VALUE kernel_indirected(int (*compar)(const void *, const void *)) { + int bob[] = { 1, 1, 2, 3, 5, 8, 13 }; + qsort(bob, 7, sizeof(int), compar); + return Qfalse; +} + +static VALUE kernel_spec_rb_yield_indirected(VALUE self, VALUE obj) { + return kernel_indirected(kernel_cb); +} #endif #ifdef HAVE_RB_YIELD_SPLAT @@ -338,6 +365,10 @@ void Init_kernel_spec(void) { rb_define_method(cls, "rb_rescue2", kernel_spec_rb_rescue2, -1); #endif +#ifdef HAVE_RB_PROTECT + rb_define_method(cls, "rb_protect_yield", kernel_spec_rb_protect_yield, 2); +#endif + #ifdef HAVE_RB_CATCH rb_define_method(cls, "rb_catch", kernel_spec_rb_catch, 2); #endif @@ -360,6 +391,7 @@ void Init_kernel_spec(void) { #ifdef HAVE_RB_YIELD rb_define_method(cls, "rb_yield", kernel_spec_rb_yield, 1); + rb_define_method(cls, "rb_yield_indirected", kernel_spec_rb_yield_indirected, 1); #endif #ifdef HAVE_RB_YIELD_VALUES diff --git a/spec/rubyspec/optional/capi/ext/rubyspec.h b/spec/rubyspec/optional/capi/ext/rubyspec.h index f20bdde37c..aa0f4def19 100644 --- a/spec/rubyspec/optional/capi/ext/rubyspec.h +++ b/spec/rubyspec/optional/capi/ext/rubyspec.h @@ -366,6 +366,7 @@ #define HAVE_RB_YIELD_VALUES 1 #define HAVE_RB_FUNCALL3 1 #define HAVE_RB_FUNCALL_WITH_BLOCK 1 +#define HAVE_RB_PROTECT 1 /* GC */ #define HAVE_RB_GC_REGISTER_ADDRESS 1 diff --git a/spec/rubyspec/optional/capi/io_spec.rb b/spec/rubyspec/optional/capi/io_spec.rb index a0bcc9ef15..bc75ad4307 100644 --- a/spec/rubyspec/optional/capi/io_spec.rb +++ b/spec/rubyspec/optional/capi/io_spec.rb @@ -259,18 +259,18 @@ describe "C-API IO function" do it "raises and IOError if passed a closed stream" do @r_io.close - lambda { @o.rb_io_wait_readable(@r_io, false) }.should raise_error(IOError) + lambda { + @o.rb_io_wait_readable(@r_io, false) + }.should raise_error(IOError) end it "blocks until the io is readable and returns true" do @o.instance_variable_set :@write_data, false thr = Thread.new do - sleep 0.1 until @o.instance_variable_get(:@write_data) + Thread.pass until @o.instance_variable_get(:@write_data) @w_io.write "rb_io_wait_readable" end - Thread.pass until thr.alive? - @o.rb_io_wait_readable(@r_io, true).should be_true @o.instance_variable_get(:@read_data).should == "rb_io_wait_re" @@ -284,7 +284,7 @@ describe "C-API IO function" do start = false thr = Thread.new do start = true - sleep 0.5 + sleep 0.05 @w_io.write "rb_io_wait_readable" end diff --git a/spec/rubyspec/optional/capi/kernel_spec.rb b/spec/rubyspec/optional/capi/kernel_spec.rb index 4d721b4b56..5747fe169f 100644 --- a/spec/rubyspec/optional/capi/kernel_spec.rb +++ b/spec/rubyspec/optional/capi/kernel_spec.rb @@ -184,6 +184,18 @@ describe "C-API Kernel function" do it "raises LocalJumpError when no block is given" do lambda { @s.rb_yield(1) }.should raise_error(LocalJumpError) end + + it "rb_yield to a block that breaks does not raise an error" do + @s.rb_yield(1) { break }.should == nil + end + + it "rb_yield to a block that breaks with a value returns the value" do + @s.rb_yield(1) { break 73 }.should == 73 + end + + it "rb_yield through a callback to a block that breaks with a value returns the value" do + @s.rb_yield_indirected(1) { break 73 }.should == 73 + end end describe "rb_yield_values" do @@ -218,6 +230,36 @@ describe "C-API Kernel function" do end end + describe "rb_protect" do + it "will run a function with an argument" do + proof = [] # Hold proof of work performed after the yield. + res = @s.rb_protect_yield(7, proof) { |x| x + 1 } + res.should == 8 + proof[0].should == 23 + end + + it "will allow cleanup code to run after break" do + proof = [] # Hold proof of work performed after the yield. + @s.rb_protect_yield(7, proof) { |x| break } + proof[0].should == 23 + end + + it "will allow cleanup code to run after break with value" do + proof = [] # Hold proof of work performed after the yield. + res = @s.rb_protect_yield(7, proof) { |x| break x + 1 } + res.should == 8 + proof[0].should == 23 + end + + it "will allow cleanup code to run after a raise" do + proof = [] # Hold proof of work performed after the yield. + lambda do + @s.rb_protect_yield(7, proof) { |x| raise NameError} + end.should raise_error(NameError) + proof[0].should == 23 + end +end + describe "rb_rescue" do before :each do @proc = lambda { |x| x } diff --git a/spec/rubyspec/optional/capi/spec_helper.rb b/spec/rubyspec/optional/capi/spec_helper.rb index f8314029c9..2a0c515306 100644 --- a/spec/rubyspec/optional/capi/spec_helper.rb +++ b/spec/rubyspec/optional/capi/spec_helper.rb @@ -2,8 +2,6 @@ require File.expand_path('../../../spec_helper', __FILE__) $extmk = false require 'rbconfig' -require 'fileutils' -require 'tmpdir' OBJDIR ||= File.expand_path("../../../ext/#{RUBY_NAME}/#{RUBY_VERSION}", __FILE__) mkdir_p(OBJDIR) @@ -83,15 +81,13 @@ def compile_extension(name) ldshared = RbConfig::CONFIG["LDSHARED"] ldshared += " #{RbConfig::CONFIG["ARCH_FLAG"]}" if RbConfig::CONFIG["ARCH_FLAG"] - libs = RbConfig::CONFIG["LIBRUBYARG_SHARED"] - libs += " " if libs - libs += RbConfig::CONFIG["LIBS"] + libs = RbConfig::CONFIG["LIBS"] dldflags = "#{RbConfig::CONFIG["LDFLAGS"]} #{RbConfig::CONFIG["DLDFLAGS"]} #{RbConfig::CONFIG["EXTDLDFLAGS"]}" dldflags.sub!(/-Wl,-soname,\S+/, '') - dldflags.sub!(/\$\(DEFFILE\)/, '') if /mswin/ =~ RUBY_PLATFORM dldflags.sub!("$(LIBPATH)", RbConfig::CONFIG["LIBPATHFLAG"] % path) + libs += RbConfig::CONFIG["LIBRUBY"] outflag = RbConfig::CONFIG["OUTFLAG"] link_cmd = "#{ldshared} #{outflag}#{lib} #{obj} #{libs} -link #{dldflags} /export:Init_#{ext}" @@ -114,29 +110,36 @@ ensure end def compile_truffleruby_extconf_make(name, path, objdir) - ext = "#{name}_spec" - file = "#{ext}.c" - source = "#{path}/#{file}" - lib_target = "#{objdir}/#{ext}.#{RbConfig::CONFIG['DLEXT']}" - temp_dir = Dir.mktmpdir + ext = "#{name}_spec" + file = "#{ext}.c" + source = "#{path}/#{ext}.c" + lib = "#{objdir}/#{ext}.#{RbConfig::CONFIG['DLEXT']}" + + # Copy needed source files to tmpdir + tmpdir = tmp("cext_#{name}") + Dir.mkdir tmpdir begin - copy = "#{temp_dir}/#{file}" - FileUtils.cp "#{path}/rubyspec.h", temp_dir - FileUtils.cp "#{path}/truffleruby.h", temp_dir - FileUtils.cp source, copy - extconf_src = "require 'mkmf'\n" + - "create_makefile('#{ext}', '#{temp_dir}')" - File.write("#{temp_dir}/extconf.rb", extconf_src) - Dir.chdir(temp_dir) do - system "#{RbConfig.ruby} extconf.rb" - system "make" # run make in temp dir - FileUtils.cp "#{ext}.su", lib_target # copy to .su file to library dir - FileUtils.cp "#{ext}.bc", objdir # copy to .bc file to library dir + ["rubyspec.h", "truffleruby.h", "#{ext}.c"].each do |file| + cp "#{path}/#{file}", "#{tmpdir}/#{file}" + end + + Dir.chdir(tmpdir) do + required = require 'mkmf' + # Reinitialize mkmf if already required + init_mkmf unless required + create_makefile(ext, tmpdir) + system "make" + + copy_exts = RbConfig::CONFIG.values_at('OBJEXT', 'DLEXT') + Dir.glob("*.{#{copy_exts.join(',')}}") do |file| + cp file, "#{objdir}/#{file}" + end end ensure - FileUtils.remove_entry temp_dir + rm_r tmpdir end - lib_target + + lib end def load_extension(name) diff --git a/spec/rubyspec/optional/capi/true_spec.rb b/spec/rubyspec/optional/capi/true_spec.rb deleted file mode 100644 index fcb57567ec..0000000000 --- a/spec/rubyspec/optional/capi/true_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require File.expand_path('../spec_helper', __FILE__) - -load_extension("boolean") - -describe "CApiTrueSpecs" do - before :each do - @b = CApiBooleanSpecs.new - end - - describe "a true value from Ruby" do - it "is truthy in C" do - @b.is_true(true).should == 1 - end - end - - describe "a true value from Qtrue" do - it "is truthy in C" do - @b.is_true(@b.q_true).should == 1 - end - end -end |