aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/optional/capi
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-03-28 14:22:29 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-03-28 14:22:29 +0000
commita28aa80c739a1d169649a4da833ef48cfb3465b3 (patch)
treec2f6bb79c268bd60116b54319ea96f01bb7dda79 /spec/ruby/optional/capi
parent0f64776745ef31e626dec0d42b7fb2a5988397ec (diff)
downloadruby-a28aa80c739a1d169649a4da833ef48cfb3465b3.tar.gz
Update to ruby/spec@e81b3cd
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67361 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/optional/capi')
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c10
-rw-r--r--spec/ruby/optional/capi/ext/typed_data_spec.c6
-rw-r--r--spec/ruby/optional/capi/kernel_spec.rb4
-rw-r--r--spec/ruby/optional/capi/mutex_spec.rb7
-rw-r--r--spec/ruby/optional/capi/string_spec.rb64
-rw-r--r--spec/ruby/optional/capi/typed_data_spec.rb9
-rw-r--r--spec/ruby/optional/capi/util_spec.rb4
7 files changed, 84 insertions, 20 deletions
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
index 3eeceae743..3255c4450d 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -358,6 +358,14 @@ static VALUE string_spec_rb_sprintf2(VALUE self, VALUE str, VALUE repl1, VALUE r
return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl1), RSTRING_PTR(repl2));
}
+static VALUE string_spec_rb_sprintf3(VALUE self, VALUE str) {
+ return rb_sprintf("Result: %"PRIsVALUE".", str);
+}
+
+static VALUE string_spec_rb_sprintf4(VALUE self, VALUE str) {
+ return rb_sprintf("Result: %+"PRIsVALUE".", str);
+}
+
static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...) {
va_list varargs;
VALUE str;
@@ -463,6 +471,8 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_free", string_spec_rb_str_free, 1);
rb_define_method(cls, "rb_sprintf1", string_spec_rb_sprintf1, 2);
rb_define_method(cls, "rb_sprintf2", string_spec_rb_sprintf2, 3);
+ rb_define_method(cls, "rb_sprintf3", string_spec_rb_sprintf3, 1);
+ rb_define_method(cls, "rb_sprintf4", string_spec_rb_sprintf4, 1);
rb_define_method(cls, "rb_vsprintf", string_spec_rb_vsprintf, 4);
rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2);
rb_define_method(cls, "rb_usascii_str_new", string_spec_rb_usascii_str_new, 2);
diff --git a/spec/ruby/optional/capi/ext/typed_data_spec.c b/spec/ruby/optional/capi/ext/typed_data_spec.c
index 60408a5750..a2cc53f54d 100644
--- a/spec/ruby/optional/capi/ext/typed_data_spec.c
+++ b/spec/ruby/optional/capi/ext/typed_data_spec.c
@@ -43,7 +43,11 @@ void sample_typed_wrapped_struct_mark(void* st) {
}
size_t sample_typed_wrapped_struct_memsize(const void* st) {
- return sizeof(struct sample_typed_wrapped_struct);
+ if (st == NULL) {
+ return 0;
+ } else {
+ return ((struct sample_typed_wrapped_struct *)st)->foo;
+ }
}
static const rb_data_type_t sample_typed_wrapped_struct_data_type = {
diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb
index 627442104f..f68a46ed50 100644
--- a/spec/ruby/optional/capi/kernel_spec.rb
+++ b/spec/ruby/optional/capi/kernel_spec.rb
@@ -244,6 +244,10 @@ describe "C-API Kernel function" do
@s.rb_yield_splat([1, 2]) { |x, y| x + y }.should == 3
end
+ it "passes arguments to a block accepting splatted args" do
+ @s.rb_yield_splat([1, 2]) { |*v| v }.should == [1, 2]
+ end
+
it "raises LocalJumpError when no block is given" do
lambda { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError)
end
diff --git a/spec/ruby/optional/capi/mutex_spec.rb b/spec/ruby/optional/capi/mutex_spec.rb
index 8844d311f2..1b76c5c250 100644
--- a/spec/ruby/optional/capi/mutex_spec.rb
+++ b/spec/ruby/optional/capi/mutex_spec.rb
@@ -72,9 +72,10 @@ describe "C-API Mutex functions" do
it "sleeps when the mutex is locked" do
@m.lock
- start = Time.now
- @s.rb_mutex_sleep(@m, 0.1)
- (Time.now - start).should be_close(0.1, 0.2)
+ t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ @s.rb_mutex_sleep(@m, 0.001)
+ t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ (t2 - t1).should >= 0
@m.locked?.should be_true
end
end
diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb
index 5ad8169e8e..ac23198662 100644
--- a/spec/ruby/optional/capi/string_spec.rb
+++ b/spec/ruby/optional/capi/string_spec.rb
@@ -4,6 +4,30 @@ require_relative '../../shared/string/times'
load_extension('string')
+class CApiStringSpecs
+ class ValidTostrTest
+ def to_str
+ "ruby"
+ end
+ end
+
+ class InvalidTostrTest
+ def to_str
+ []
+ end
+ end
+
+ class ToSOrInspect
+ def to_s
+ 'A string'
+ end
+
+ def inspect
+ 'A different string'
+ end
+ end
+end
+
describe :rb_str_new2, shared: true do
it "returns a new string object calling strlen on the passed C string" do
# Hardcoded to pass const char * = "hello\0invisible"
@@ -20,18 +44,6 @@ describe "C-API String function" do
@s = CApiStringSpecs.new
end
- class ValidTostrTest
- def to_str
- "ruby"
- end
- end
-
- class InvalidTostrTest
- def to_str
- []
- end
- end
-
[Encoding::BINARY, Encoding::UTF_8].each do |enc|
describe "rb_str_set_len on a #{enc.name} String" do
before :each do
@@ -438,12 +450,12 @@ describe "C-API String function" do
describe "rb_str_to_str" do
it "calls #to_str to coerce the value to a String" do
@s.rb_str_to_str("foo").should == "foo"
- @s.rb_str_to_str(ValidTostrTest.new).should == "ruby"
+ @s.rb_str_to_str(CApiStringSpecs::ValidTostrTest.new).should == "ruby"
end
it "raises a TypeError if coercion fails" do
lambda { @s.rb_str_to_str(0) }.should raise_error(TypeError)
- lambda { @s.rb_str_to_str(InvalidTostrTest.new) }.should raise_error(TypeError)
+ lambda { @s.rb_str_to_str(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError)
end
end
@@ -875,6 +887,26 @@ describe "C-API String function" do
s = "Awesome %s is here with %s"
@s.rb_sprintf2(s, "string", "content").should == "Awesome string is here with content"
end
+
+ it "formats a string VALUE using to_s if sign not specified in format" do
+ s = 'Result: A string.'
+ @s.rb_sprintf3(CApiStringSpecs::ToSOrInspect.new).should == s
+ end
+
+ it "formats a string VALUE using inspect if sign specified in format" do
+ s = 'Result: A different string.'
+ @s.rb_sprintf4(CApiStringSpecs::ToSOrInspect.new).should == s
+ end
+
+ it "formats a TrueClass VALUE as `TrueClass` if sign not specified in format" do
+ s = 'Result: TrueClass.'
+ @s.rb_sprintf3(true.class).should == s
+ end
+
+ it "formats a TrueClass VALUE as 'true' if sign specified in format" do
+ s = 'Result: true.'
+ @s.rb_sprintf4(true.class).should == s
+ end
end
describe "rb_vsprintf" do
@@ -890,11 +922,11 @@ describe "C-API String function" do
end
it "tries to convert the passed argument to a string by calling #to_str first" do
- @s.rb_String(ValidTostrTest.new).should == "ruby"
+ @s.rb_String(CApiStringSpecs::ValidTostrTest.new).should == "ruby"
end
it "raises a TypeError if #to_str does not return a string" do
- lambda { @s.rb_String(InvalidTostrTest.new) }.should raise_error(TypeError)
+ lambda { @s.rb_String(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError)
end
it "tries to convert the passed argument to a string by calling #to_s" do
diff --git a/spec/ruby/optional/capi/typed_data_spec.rb b/spec/ruby/optional/capi/typed_data_spec.rb
index b0c0cd48ed..8d2910457c 100644
--- a/spec/ruby/optional/capi/typed_data_spec.rb
+++ b/spec/ruby/optional/capi/typed_data_spec.rb
@@ -1,4 +1,5 @@
require_relative 'spec_helper'
+require 'objspace'
load_extension("typed_data")
@@ -7,6 +8,14 @@ describe "CApiAllocTypedSpecs (a class with an alloc func defined)" do
@s = CApiAllocTypedSpecs.new
@s.typed_wrapped_data.should == 42 # not defined in initialize
end
+
+ it "uses the specified memsize function for ObjectSpace.memsize" do
+ @s = CApiAllocTypedSpecs.new
+ # The defined memsize function for the type should return 42 as
+ # the size, and this should be added to the size of the object as
+ # known by Ruby.
+ ObjectSpace.memsize_of(@s).should > 42
+ end
end
describe "CApiWrappedTypedStruct" do
diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb
index da2f758bec..b3e43d29ce 100644
--- a/spec/ruby/optional/capi/util_spec.rb
+++ b/spec/ruby/optional/capi/util_spec.rb
@@ -23,6 +23,10 @@ describe "C-API Util function" do
lambda { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should raise_error(ArgumentError)
end
+ it "raises an ArgumentError if there are too many arguments" do
+ lambda { @o.rb_scan_args([1, 2, 3, 4], "3", 0, @acc) }.should raise_error(ArgumentError)
+ end
+
it "assigns the required and optional arguments scanned" do
@o.rb_scan_args([1, 2], "11", 2, @acc).should == 2
ScratchPad.recorded.should == [1, 2]