summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commit1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/optional/capi
parent75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff)
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/optional/capi')
-rw-r--r--spec/ruby/optional/capi/README16
-rw-r--r--spec/ruby/optional/capi/array_spec.rb463
-rw-r--r--spec/ruby/optional/capi/bignum_spec.rb214
-rw-r--r--spec/ruby/optional/capi/boolean_spec.rb33
-rw-r--r--spec/ruby/optional/capi/class_spec.rb388
-rw-r--r--spec/ruby/optional/capi/complex_spec.rb45
-rw-r--r--spec/ruby/optional/capi/constants_spec.rb268
-rw-r--r--spec/ruby/optional/capi/data_spec.rb46
-rw-r--r--spec/ruby/optional/capi/encoding_spec.rb485
-rw-r--r--spec/ruby/optional/capi/enumerator_spec.rb39
-rw-r--r--spec/ruby/optional/capi/exception_spec.rb58
-rw-r--r--spec/ruby/optional/capi/ext/.gitignore9
-rw-r--r--spec/ruby/optional/capi/ext/array_spec.c452
-rw-r--r--spec/ruby/optional/capi/ext/bignum_spec.c149
-rw-r--r--spec/ruby/optional/capi/ext/boolean_spec.c34
-rw-r--r--spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c5
-rw-r--r--spec/ruby/optional/capi/ext/class_spec.c261
-rw-r--r--spec/ruby/optional/capi/ext/class_under_autoload_spec.c5
-rw-r--r--spec/ruby/optional/capi/ext/complex_spec.c76
-rw-r--r--spec/ruby/optional/capi/ext/constants_spec.c646
-rw-r--r--spec/ruby/optional/capi/ext/data_spec.c97
-rw-r--r--spec/ruby/optional/capi/ext/encoding_spec.c424
-rw-r--r--spec/ruby/optional/capi/ext/enumerator_spec.c27
-rw-r--r--spec/ruby/optional/capi/ext/exception_spec.c72
-rw-r--r--spec/ruby/optional/capi/ext/file_spec.c44
-rw-r--r--spec/ruby/optional/capi/ext/fixnum_spec.c52
-rw-r--r--spec/ruby/optional/capi/ext/float_spec.c54
-rw-r--r--spec/ruby/optional/capi/ext/gc_spec.c72
-rw-r--r--spec/ruby/optional/capi/ext/globals_spec.c199
-rw-r--r--spec/ruby/optional/capi/ext/hash_spec.c218
-rw-r--r--spec/ruby/optional/capi/ext/integer_spec.c40
-rw-r--r--spec/ruby/optional/capi/ext/io_spec.c303
-rw-r--r--spec/ruby/optional/capi/ext/kernel_spec.c436
-rw-r--r--spec/ruby/optional/capi/ext/marshal_spec.c36
-rw-r--r--spec/ruby/optional/capi/ext/module_spec.c252
-rw-r--r--spec/ruby/optional/capi/ext/module_under_autoload_spec.c7
-rw-r--r--spec/ruby/optional/capi/ext/mutex_spec.c91
-rw-r--r--spec/ruby/optional/capi/ext/numeric_spec.c166
-rw-r--r--spec/ruby/optional/capi/ext/object_spec.c608
-rw-r--r--spec/ruby/optional/capi/ext/proc_spec.c85
-rw-r--r--spec/ruby/optional/capi/ext/range_spec.c66
-rw-r--r--spec/ruby/optional/capi/ext/rational_spec.c95
-rw-r--r--spec/ruby/optional/capi/ext/regexp_spec.c84
-rw-r--r--spec/ruby/optional/capi/ext/rubyspec.h608
-rw-r--r--spec/ruby/optional/capi/ext/st_spec.c93
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c682
-rw-r--r--spec/ruby/optional/capi/ext/struct_spec.c131
-rw-r--r--spec/ruby/optional/capi/ext/symbol_spec.c138
-rw-r--r--spec/ruby/optional/capi/ext/thread_spec.c188
-rw-r--r--spec/ruby/optional/capi/ext/time_spec.c127
-rw-r--r--spec/ruby/optional/capi/ext/typed_data_spec.c177
-rw-r--r--spec/ruby/optional/capi/ext/util_spec.c95
-rw-r--r--spec/ruby/optional/capi/file_spec.rb89
-rw-r--r--spec/ruby/optional/capi/fixnum_spec.rb124
-rw-r--r--spec/ruby/optional/capi/fixtures/class.rb82
-rw-r--r--spec/ruby/optional/capi/fixtures/const_get.rb5
-rw-r--r--spec/ruby/optional/capi/fixtures/const_get_at.rb5
-rw-r--r--spec/ruby/optional/capi/fixtures/const_get_from.rb5
-rw-r--r--spec/ruby/optional/capi/fixtures/const_get_object.rb3
-rw-r--r--spec/ruby/optional/capi/fixtures/encoding.rb3
-rw-r--r--spec/ruby/optional/capi/fixtures/foo.rb1
-rw-r--r--spec/ruby/optional/capi/fixtures/module.rb35
-rw-r--r--spec/ruby/optional/capi/fixtures/module_autoload.rb4
-rw-r--r--spec/ruby/optional/capi/fixtures/path_to_class.rb6
-rw-r--r--spec/ruby/optional/capi/fixtures/proc.rb20
-rw-r--r--spec/ruby/optional/capi/float_spec.rb30
-rw-r--r--spec/ruby/optional/capi/gc_spec.rb54
-rw-r--r--spec/ruby/optional/capi/globals_spec.rb224
-rw-r--r--spec/ruby/optional/capi/hash_spec.rb245
-rw-r--r--spec/ruby/optional/capi/integer_spec.rb275
-rw-r--r--spec/ruby/optional/capi/io_spec.rb344
-rw-r--r--spec/ruby/optional/capi/kernel_spec.rb522
-rw-r--r--spec/ruby/optional/capi/marshal_spec.rb46
-rw-r--r--spec/ruby/optional/capi/module_spec.rb342
-rw-r--r--spec/ruby/optional/capi/mutex_spec.rb88
-rw-r--r--spec/ruby/optional/capi/numeric_spec.rb432
-rw-r--r--spec/ruby/optional/capi/object_spec.rb803
-rw-r--r--spec/ruby/optional/capi/proc_spec.rb112
-rw-r--r--spec/ruby/optional/capi/rake_helper.rb23
-rw-r--r--spec/ruby/optional/capi/range_spec.rb95
-rw-r--r--spec/ruby/optional/capi/rational_spec.rb57
-rw-r--r--spec/ruby/optional/capi/regexp_spec.rb71
-rw-r--r--spec/ruby/optional/capi/spec_helper.rb109
-rw-r--r--spec/ruby/optional/capi/st_spec.rb41
-rw-r--r--spec/ruby/optional/capi/string_spec.rb787
-rw-r--r--spec/ruby/optional/capi/struct_spec.rb209
-rw-r--r--spec/ruby/optional/capi/symbol_spec.rb133
-rw-r--r--spec/ruby/optional/capi/thread_spec.rb124
-rw-r--r--spec/ruby/optional/capi/time_spec.rb302
-rw-r--r--spec/ruby/optional/capi/typed_data_spec.rb56
-rw-r--r--spec/ruby/optional/capi/util_spec.rb191
91 files changed, 15556 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/README b/spec/ruby/optional/capi/README
new file mode 100644
index 0000000000..efbfb09dcb
--- /dev/null
+++ b/spec/ruby/optional/capi/README
@@ -0,0 +1,16 @@
+C-API Specs
+
+These specs test the C-API from Ruby. The following are conventions for the
+specs:
+
+1. Put specs for functions related to a Ruby class in a file named according
+ to the class. For example, for rb_ary_new function, put the specs in
+ optional/capi/array_spec.rb
+2. Put the C file containing the C functions for array_spec.rb in
+ optional/capi/ext/array_spec.c
+3. Add a '#define HAVE_RB_ARY_NEW 1' to rubyspec.h
+4. Name the C extension class 'CApiArraySpecs'.
+5. Name the C functions 'array_spec_rb_ary_new'.
+6. Wrap the code in the optional/capi/ext/array_spec.c in
+ '#ifdef HAVE_RB_ARY_NEW'
+6. Attach the C function to the class using the name 'rb_ary_new'
diff --git a/spec/ruby/optional/capi/array_spec.rb b/spec/ruby/optional/capi/array_spec.rb
new file mode 100644
index 0000000000..2fd898ad94
--- /dev/null
+++ b/spec/ruby/optional/capi/array_spec.rb
@@ -0,0 +1,463 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("array")
+
+describe :rb_ary_new2, shared: true do
+ it "returns an empty array" do
+ @s.send(@method, 5).should == []
+ end
+
+ it "raises an ArgumentError when the given argument is negative" do
+ lambda { @s.send(@method, -1) }.should raise_error(ArgumentError)
+ end
+end
+
+describe "C-API Array function" do
+ before :each do
+ @s = CApiArraySpecs.new
+ end
+
+ describe "rb_Array" do
+ it "returns obj if it is an array" do
+ arr = @s.rb_Array([1,2])
+ arr.should == [1, 2]
+ end
+
+ it "tries to convert obj to an array" do
+ arr = @s.rb_Array({"bar" => "foo"})
+ arr.should == [["bar", "foo"]]
+ end
+
+ it "returns obj wrapped in an array if it cannot be converted to an array" do
+ arr = @s.rb_Array("a")
+ arr.should == ["a"]
+ end
+ end
+
+ describe "rb_ary_new" do
+ it "returns an empty array" do
+ @s.rb_ary_new.should == []
+ end
+ end
+
+ describe "rb_ary_new2" do
+ it_behaves_like :rb_ary_new2, :rb_ary_new2
+ end
+
+ describe "rb_ary_new_capa" do
+ it_behaves_like :rb_ary_new2, :rb_ary_new_capa
+ end
+
+ describe "rb_ary_new3" do
+ it "returns an array with the passed cardinality and varargs" do
+ @s.rb_ary_new3(1,2,3).should == [1,2,3]
+ end
+ end
+
+ describe "rb_ary_new_from_args" do
+ it "returns an array with the passed cardinality and varargs" do
+ @s.rb_ary_new_from_args(1,2,3).should == [1,2,3]
+ end
+ end
+
+ describe "rb_ary_new4" do
+ it "returns an array with the passed values" do
+ @s.rb_ary_new4(1,2,3).should == [1,2,3]
+ end
+ end
+
+ describe "rb_ary_new_from_values" do
+ it "returns an array with the passed values" do
+ @s.rb_ary_new_from_values(1,2,3).should == [1,2,3]
+ end
+ end
+
+ describe "rb_ary_push" do
+ it "adds an element to the array" do
+ @s.rb_ary_push([], 4).should == [4]
+ end
+ end
+
+ describe "rb_ary_cat" do
+ it "pushes the given objects onto the end of the array" do
+ @s.rb_ary_cat([1, 2], 3, 4).should == [1, 2, 3, 4]
+ end
+
+ it "raises a RuntimeError if the array is frozen" do
+ lambda { @s.rb_ary_cat([].freeze, 1) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_ary_pop" do
+ it "removes and returns the last element in the array" do
+ a = [1,2,3]
+ @s.rb_ary_pop(a).should == 3
+ a.should == [1,2]
+ end
+ end
+
+ describe "rb_ary_join" do
+ it "joins elements of an array with a string" do
+ a = [1,2,3]
+ b = ","
+ @s.rb_ary_join(a,b).should == "1,2,3"
+ end
+ end
+
+ describe "rb_ary_to_s" do
+ it "creates an Array literal representation as a String" do
+ @s.rb_ary_to_s([1,2,3]).should == "[1, 2, 3]"
+ @s.rb_ary_to_s([]).should == "[]"
+ end
+ end
+
+ describe "rb_ary_reverse" do
+ it "reverses the order of elements in the array" do
+ a = [1,2,3]
+ @s.rb_ary_reverse(a)
+ a.should == [3,2,1]
+ end
+
+ it "returns the original array" do
+ a = [1,2,3]
+ @s.rb_ary_reverse(a).should equal(a)
+ end
+ end
+
+ describe "rb_ary_rotate" do
+ it "rotates the array so that the element at the specified position comes first" do
+ @s.rb_ary_rotate([1, 2, 3, 4], 2).should == [3, 4, 1, 2]
+ @s.rb_ary_rotate([1, 2, 3, 4], -3).should == [2, 3, 4, 1]
+ end
+
+ it "raises a RuntimeError if the array is frozen" do
+ lambda { @s.rb_ary_rotate([].freeze, 1) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_ary_entry" do
+ it "returns nil when passed an empty array" do
+ @s.rb_ary_entry([], 0).should == nil
+ end
+
+ it "returns elements from the end when passed a negative index" do
+ @s.rb_ary_entry([1, 2, 3], -1).should == 3
+ @s.rb_ary_entry([1, 2, 3], -2).should == 2
+ end
+
+ it "returns nil if the index is out of range" do
+ @s.rb_ary_entry([1, 2, 3], 3).should == nil
+ @s.rb_ary_entry([1, 2, 3], -10).should == nil
+ end
+ end
+
+ describe "rb_ary_clear" do
+ it "removes all elements from the array" do
+ @s.rb_ary_clear([]).should == []
+ @s.rb_ary_clear([1, 2, 3]).should == []
+ end
+ end
+
+ describe "rb_ary_dup" do
+ it "duplicates the array" do
+ @s.rb_ary_dup([]).should == []
+
+ a = [1, 2, 3]
+ b = @s.rb_ary_dup(a)
+
+ b.should == a
+ b.should_not equal(a)
+ end
+ end
+
+ describe "rb_ary_unshift" do
+ it "prepends the element to the array" do
+ a = [1, 2, 3]
+ @s.rb_ary_unshift(a, "a").should == ["a", 1, 2, 3]
+ a.should == ['a', 1, 2, 3]
+ end
+ end
+
+ describe "rb_ary_shift" do
+ it "removes and returns the first element" do
+ a = [5, 1, 1, 5, 4]
+ @s.rb_ary_shift(a).should == 5
+ a.should == [1, 1, 5, 4]
+ end
+
+ it "returns nil when the array is empty" do
+ @s.rb_ary_shift([]).should == nil
+ end
+ end
+
+ describe "rb_ary_store" do
+ it "overwrites the element at the given position" do
+ a = [1, 2, 3]
+ @s.rb_ary_store(a, 1, 5)
+ a.should == [1, 5, 3]
+ end
+
+ it "writes to elements offset from the end if passed a negative index" do
+ a = [1, 2, 3]
+ @s.rb_ary_store(a, -1, 5)
+ a.should == [1, 2, 5]
+ end
+
+ it "raises an IndexError if the negative index is greater than the length" do
+ a = [1, 2, 3]
+ lambda { @s.rb_ary_store(a, -10, 5) }.should raise_error(IndexError)
+ end
+
+ it "enlarges the array as needed" do
+ a = []
+ @s.rb_ary_store(a, 2, 7)
+ a.should == [nil, nil, 7]
+ end
+
+ it "raises a RuntimeError if the array is frozen" do
+ a = [1, 2, 3].freeze
+ lambda { @s.rb_ary_store(a, 1, 5) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_ary_concat" do
+ it "concats two arrays" do
+ a = [5, 1, 1, 5, 4]
+ b = [2, 3]
+ @s.rb_ary_concat(a, b).should == [5, 1, 1, 5, 4, 2, 3]
+ end
+ end
+
+ describe "rb_ary_plus" do
+ it "adds two arrays together" do
+ @s.rb_ary_plus([10], [20]).should == [10, 20]
+ end
+ end
+
+ describe "RARRAY_PTR" do
+ it "returns a pointer to a C array of the array's elements" do
+ a = [1, 2, 3]
+ b = []
+ @s.RARRAY_PTR_iterate(a) do |e|
+ b << e
+ end
+ a.should == b
+ end
+
+ it "allows assigning to the elements of the C array" do
+ a = [1, 2, 3]
+ @s.RARRAY_PTR_assign(a, :set)
+ a.should == [:set, :set, :set]
+ end
+ end
+
+ describe "RARRAY_LEN" do
+ it "returns the size of the array" do
+ @s.RARRAY_LEN([1, 2, 3]).should == 3
+ end
+ end
+
+ describe "RARRAY_AREF" do
+ # This macro does NOT do any bounds checking!
+ it "returns an element from the array" do
+ @s.RARRAY_AREF([1, 2, 3], 1).should == 2
+ end
+ end
+
+ describe "rb_assoc_new" do
+ it "returns an array containing the two elements" do
+ @s.rb_assoc_new(1, 2).should == [1, 2]
+ @s.rb_assoc_new(:h, [:a, :b]).should == [:h, [:a, :b]]
+ end
+ end
+
+ describe "rb_ary_includes" do
+ it "returns true if the array includes the element" do
+ @s.rb_ary_includes([1, 2, 3], 2).should be_true
+ end
+
+ it "returns false if the array does not include the element" do
+ @s.rb_ary_includes([1, 2, 3], 4).should be_false
+ end
+ end
+
+ describe "rb_ary_aref" do
+ it "returns the element at the given index" do
+ @s.rb_ary_aref([:me, :you], 0).should == :me
+ @s.rb_ary_aref([:me, :you], 1).should == :you
+ end
+
+ it "returns nil for an out of range index" do
+ @s.rb_ary_aref([1, 2, 3], 6).should be_nil
+ end
+
+ it "returns a new array where the first argument is the index and the second is the length" do
+ @s.rb_ary_aref([1, 2, 3, 4], 0, 2).should == [1, 2]
+ @s.rb_ary_aref([1, 2, 3, 4], -4, 3).should == [1, 2, 3]
+ end
+
+ it "accepts a range" do
+ @s.rb_ary_aref([1, 2, 3, 4], 0..-1).should == [1, 2, 3, 4]
+ end
+
+ it "returns nil when the start of a range is out of bounds" do
+ @s.rb_ary_aref([1, 2, 3, 4], 6..10).should be_nil
+ end
+
+ it "returns an empty array when the start of a range equals the last element" do
+ @s.rb_ary_aref([1, 2, 3, 4], 4..10).should == []
+ end
+ end
+
+ describe "rb_iterate" do
+ it "calls an callback function as a block passed to an method" do
+ s = [1,2,3,4]
+ s2 = @s.rb_iterate(s)
+
+ s2.should == s
+
+ # Make sure they're different objects
+ s2.equal?(s).should be_false
+ end
+
+ it "calls a function with the other function available as a block" do
+ h = {a: 1, b: 2}
+
+ @s.rb_iterate_each_pair(h).sort.should == [1,2]
+ end
+
+ it "calls a function which can yield into the original block" do
+ s2 = []
+
+ o = Object.new
+ def o.each
+ yield 1
+ yield 2
+ yield 3
+ yield 4
+ end
+
+ @s.rb_iterate_then_yield(o) { |x| s2 << x }
+
+ s2.should == [1,2,3,4]
+ end
+ end
+
+ describe "rb_ary_delete" do
+ it "removes an element from an array and returns it" do
+ ary = [1, 2, 3, 4]
+ @s.rb_ary_delete(ary, 3).should == 3
+ ary.should == [1, 2, 4]
+ end
+
+ it "returns nil if the element is not in the array" do
+ ary = [1, 2, 3, 4]
+ @s.rb_ary_delete(ary, 5).should be_nil
+ ary.should == [1, 2, 3, 4]
+ end
+ end
+
+ describe "rb_mem_clear" do
+ it "sets elements of a C array to nil" do
+ @s.rb_mem_clear(1).should == nil
+ end
+ end
+
+ describe "rb_ary_freeze" do
+ it "freezes the object exactly like Kernel#freeze" do
+ ary = [1,2]
+ @s.rb_ary_freeze(ary)
+ ary.frozen?.should be_true
+ end
+ end
+
+ describe "rb_ary_delete_at" do
+ before :each do
+ @array = [1, 2, 3, 4]
+ end
+
+ it "removes an element from an array at a positive index" do
+ @s.rb_ary_delete_at(@array, 2).should == 3
+ @array.should == [1, 2, 4]
+ end
+
+ it "removes an element from an array at a negative index" do
+ @s.rb_ary_delete_at(@array, -3).should == 2
+ @array.should == [1, 3, 4]
+ end
+
+ it "returns nil if the index is out of bounds" do
+ @s.rb_ary_delete_at(@array, 4).should be_nil
+ @array.should == [1, 2, 3, 4]
+ end
+
+ it "returns nil if the negative index is out of bounds" do
+ @s.rb_ary_delete_at(@array, -5).should be_nil
+ @array.should == [1, 2, 3, 4]
+ end
+ end
+
+ describe "rb_ary_to_ary" do
+
+ describe "with an array" do
+
+ it "returns the given array" do
+ array = [1, 2, 3]
+ @s.rb_ary_to_ary(array).should equal(array)
+ end
+
+ end
+
+ describe "with an object that responds to to_ary" do
+
+ it "calls to_ary on the object" do
+ obj = mock('to_ary')
+ obj.stub!(:to_ary).and_return([1, 2, 3])
+ @s.rb_ary_to_ary(obj).should == [1, 2, 3]
+ end
+
+ end
+
+ describe "with an object that responds to to_a" do
+
+ it "returns the original object in an array" do
+ obj = mock('to_a')
+ obj.stub!(:to_a).and_return([1, 2, 3])
+ @s.rb_ary_to_ary(obj).should == [obj]
+ end
+
+ end
+
+ describe "with an object that doesn't respond to to_ary" do
+
+ it "returns the original object in an array" do
+ obj = mock('no_to_ary')
+ @s.rb_ary_to_ary(obj).should == [obj]
+ end
+
+ end
+
+ end
+
+ describe "rb_ary_subseq" do
+ it "returns a subsequence of the given array" do
+ @s.rb_ary_subseq([1, 2, 3, 4, 5], 1, 3).should == [2, 3, 4]
+ end
+
+ it "returns an empty array for a subsequence of 0 elements" do
+ @s.rb_ary_subseq([1, 2, 3, 4, 5], 1, 0).should == []
+ end
+
+ it "returns nil if the begin index is out of bound" do
+ @s.rb_ary_subseq([1, 2, 3, 4, 5], 6, 3).should be_nil
+ end
+
+ it "returns the existing subsequence of the length is out of bounds" do
+ @s.rb_ary_subseq([1, 2, 3, 4, 5], 4, 3).should == [5]
+ end
+
+ it "returns nil if the size is negative" do
+ @s.rb_ary_subseq([1, 2, 3, 4, 5], 1, -1).should be_nil
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/bignum_spec.rb b/spec/ruby/optional/capi/bignum_spec.rb
new file mode 100644
index 0000000000..a5d5995dc0
--- /dev/null
+++ b/spec/ruby/optional/capi/bignum_spec.rb
@@ -0,0 +1,214 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("bignum")
+
+def ensure_bignum(n)
+ raise "Bignum#coerce returned Fixnum" if fixnum_min <= n && n <= fixnum_max
+ n
+end
+
+full_range_longs = (fixnum_max == 2**(0.size * 8 - 1) - 1)
+
+describe "CApiBignumSpecs" do
+ before :each do
+ @s = CApiBignumSpecs.new
+
+ if full_range_longs
+ @max_long = 2**(0.size * 8 - 1) - 1
+ @min_long = -@max_long - 1
+ @max_ulong = ensure_bignum(2**(0.size * 8) - 1)
+ else
+ @max_long = ensure_bignum(2**(0.size * 8 - 1) - 1)
+ @min_long = ensure_bignum(-@max_long - 1)
+ @max_ulong = ensure_bignum(2**(0.size * 8) - 1)
+ end
+ end
+
+ describe "rb_big2long" do
+ unless full_range_longs
+ it "converts a Bignum" do
+ @s.rb_big2long(@max_long).should == @max_long
+ @s.rb_big2long(@min_long).should == @min_long
+ end
+ end
+
+ it "raises RangeError if passed Bignum overflow long" do
+ lambda { @s.rb_big2long(ensure_bignum(@max_long + 1)) }.should raise_error(RangeError)
+ lambda { @s.rb_big2long(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError)
+ end
+ end
+
+ describe "rb_big2ll" do
+ unless full_range_longs
+ it "converts a Bignum" do
+ @s.rb_big2ll(@max_long).should == @max_long
+ @s.rb_big2ll(@min_long).should == @min_long
+ end
+ end
+
+ it "raises RangeError if passed Bignum overflow long" do
+ lambda { @s.rb_big2ll(ensure_bignum(@max_long << 40)) }.should raise_error(RangeError)
+ lambda { @s.rb_big2ll(ensure_bignum(@min_long << 40)) }.should raise_error(RangeError)
+ end
+ end
+
+ describe "rb_big2ulong" do
+ it "converts a Bignum" do
+ @s.rb_big2ulong(@max_ulong).should == @max_ulong
+ end
+
+ unless full_range_longs
+ it "wraps around if passed a negative bignum" do
+ @s.rb_big2ulong(ensure_bignum(@min_long + 1)).should == -(@min_long - 1)
+ @s.rb_big2ulong(ensure_bignum(@min_long)).should == -(@min_long)
+ end
+ end
+
+ it "raises RangeError if passed Bignum overflow long" do
+ lambda { @s.rb_big2ulong(ensure_bignum(@max_ulong + 1)) }.should raise_error(RangeError)
+ lambda { @s.rb_big2ulong(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError)
+ end
+ end
+
+ describe "rb_big2dbl" do
+ it "converts a Bignum to a double value" do
+ @s.rb_big2dbl(ensure_bignum(Float::MAX.to_i)).eql?(Float::MAX).should == true
+ end
+
+ it "returns Infinity if the number is too big for a double" do
+ huge_bignum = ensure_bignum(Float::MAX.to_i * 2)
+ @s.rb_big2dbl(huge_bignum).should == infinity_value
+ end
+
+ it "returns -Infinity if the number is negative and too big for a double" do
+ huge_bignum = -ensure_bignum(Float::MAX.to_i * 2)
+ @s.rb_big2dbl(huge_bignum).should == -infinity_value
+ end
+ end
+
+ describe "rb_big2str" do
+
+ it "converts a Bignum to a string with base 10" do
+ @s.rb_big2str(ensure_bignum(2**70), 10).eql?("1180591620717411303424").should == true
+ end
+
+ it "converts a Bignum to a string with a different base" do
+ @s.rb_big2str(ensure_bignum(2**70), 16).eql?("400000000000000000").should == true
+ end
+ end
+
+ describe "rb_big_cmp" do
+ it "compares a Bignum with a Bignum" do
+ @s.rb_big_cmp(bignum_value, bignum_value(1)).should == -1
+ end
+
+ it "compares a Bignum with a Fixnum" do
+ @s.rb_big_cmp(bignum_value, 5).should == 1
+ end
+ end
+
+ describe "rb_big_pack" do
+ it "packs a Bignum into an unsigned long" do
+ val = @s.rb_big_pack(@max_ulong)
+ val.should == @max_ulong
+ end
+
+ platform_is wordsize: 64 do
+ it "packs max_ulong into 2 ulongs to allow sign bit" do
+ val = @s.rb_big_pack_length(@max_ulong)
+ val.should == 2
+ val = @s.rb_big_pack_array(@max_ulong, 2)
+ val[0].should == @max_ulong
+ val[1].should == 0
+ end
+
+ it "packs a 72-bit positive Bignum into 2 unsigned longs" do
+ num = 2 ** 71
+ val = @s.rb_big_pack_length(num)
+ val.should == 2
+ end
+
+ it "packs a 72-bit positive Bignum into correct 2 longs" do
+ num = 2 ** 71 + 1
+ val = @s.rb_big_pack_array(num, 2)
+ val[0].should == 1;
+ val[1].should == 0x80;
+ end
+
+ it "packs a 72-bit negative Bignum into correct 2 longs" do
+ num = -(2 ** 71 + 1)
+ val = @s.rb_big_pack_array(num, @s.rb_big_pack_length(num))
+ val[0].should == @max_ulong;
+ val[1].should == @max_ulong - 0x80;
+ end
+
+ it "packs lower order bytes into least significant bytes of longs for positive bignum" do
+ num = 0
+ 32.times { |i| num += i << (i * 8) }
+ val = @s.rb_big_pack_array(num, @s.rb_big_pack_length(num))
+ val.size.should == 4
+ 32.times do |i|
+ a_long = val[i/8]
+ a_byte = (a_long >> ((i % 8) * 8)) & 0xff
+ a_byte.should == i
+ end
+ end
+
+ it "packs lower order bytes into least significant bytes of longs for negative bignum" do
+ num = 0
+ 32.times { |i| num += i << (i * 8) }
+ num = -num
+ val = @s.rb_big_pack_array(num, @s.rb_big_pack_length(num))
+ val.size.should == 4
+ expected_bytes = [0x00, 0xff, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
+ 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
+ 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
+ 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0 ]
+ 32.times do |i|
+ a_long = val[i/8]
+ a_byte = (a_long >> ((i % 8) * 8)) & 0xff
+ a_byte.should == expected_bytes[i]
+ end
+ end
+ end
+ end
+
+ describe "rb_dbl2big" do
+ it "returns a Fixnum for a Fixnum input value" do
+ val = @s.rb_dbl2big(2)
+
+ val.kind_of?(Fixnum).should == true
+ val.should == 2
+ end
+
+ it "returns a Fixnum for a Float input value" do
+ val = @s.rb_dbl2big(2.5)
+
+ val.kind_of?(Fixnum).should == true
+ val.should == 2
+ end
+
+ it "returns a Bignum for a large enough Float input value" do
+ input = 219238102380912830988.5 # chosen by fair dice roll
+ val = @s.rb_dbl2big(input)
+
+ val.kind_of?(Bignum).should == true
+
+ # This value is based on the output of a simple C extension that uses
+ # rb_dbl2big() to convert the above input value to a Bignum.
+ val.should == 219238102380912836608
+ end
+
+ it "raises FloatDomainError for Infinity values" do
+ inf = 1.0 / 0
+
+ lambda { @s.rb_dbl2big(inf) }.should raise_error(FloatDomainError)
+ end
+
+ it "raises FloatDomainError for NaN values" do
+ nan = 0.0 / 0
+
+ lambda { @s.rb_dbl2big(nan) }.should raise_error(FloatDomainError)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/boolean_spec.rb b/spec/ruby/optional/capi/boolean_spec.rb
new file mode 100644
index 0000000000..49303662e7
--- /dev/null
+++ b/spec/ruby/optional/capi/boolean_spec.rb
@@ -0,0 +1,33 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("boolean")
+
+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
+ end
+ end
+
+ describe "a false value from Qfalse" do
+ it "is falsey in C" do
+ @b.is_true(@b.q_false).should == 2
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/class_spec.rb b/spec/ruby/optional/capi/class_spec.rb
new file mode 100644
index 0000000000..d94c1ab902
--- /dev/null
+++ b/spec/ruby/optional/capi/class_spec.rb
@@ -0,0 +1,388 @@
+require File.expand_path('../spec_helper', __FILE__)
+require File.expand_path('../fixtures/class', __FILE__)
+
+load_extension("class")
+
+autoload :ClassUnderAutoload, "#{object_path}/class_under_autoload_spec"
+autoload :ClassIdUnderAutoload, "#{object_path}/class_id_under_autoload_spec"
+
+describe :rb_path_to_class, shared: true do
+ it "returns a class or module from a scoped String" do
+ @s.send(@method, "CApiClassSpecs::A::B").should equal(CApiClassSpecs::A::B)
+ end
+
+ it "resolves autoload constants" do
+ @s.send(@method, "CApiClassSpecs::A::D").name.should == "CApiClassSpecs::A::D"
+ end
+
+ it "raises an ArgumentError if a constant in the path does not exist" do
+ lambda { @s.send(@method, "CApiClassSpecs::NotDefined::B") }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if the final constant does not exist" do
+ lambda { @s.send(@method, "CApiClassSpecs::NotDefined") }.should raise_error(ArgumentError)
+ end
+
+ it "raises a TypeError if the constant is not a class or module" do
+ lambda { @s.send(@method, "CApiClassSpecs::A::C") }.should raise_error(TypeError)
+ end
+
+ it "raises an ArgumentError even if a constant in the path exists on toplevel" do
+ lambda { @s.send(@method, "CApiClassSpecs::Object") }.should raise_error(ArgumentError)
+ end
+end
+
+describe "C-API Class function" do
+ before :each do
+ @s = CApiClassSpecs.new
+ end
+
+ describe "rb_class_new_instance" do
+ it "allocates and initializes a new object" do
+ o = @s.rb_class_new_instance(0, nil, CApiClassSpecs::Alloc)
+ o.class.should == CApiClassSpecs::Alloc
+ o.initialized.should be_true
+ end
+
+ it "passes arguments to the #initialize method" do
+ o = @s.rb_class_new_instance(2, [:one, :two], CApiClassSpecs::Alloc)
+ o.arguments.should == [:one, :two]
+ end
+ end
+
+ describe "rb_include_module" do
+ it "includes a module into a class" do
+ c = Class.new
+ o = c.new
+ lambda { o.included? }.should raise_error(NameError)
+ @s.rb_include_module(c, CApiClassSpecs::M)
+ o.included?.should be_true
+ end
+ end
+
+ describe "rb_define_attr" do
+ before :each do
+ @a = CApiClassSpecs::Attr.new
+ end
+
+ it "defines an attr_reader when passed true, false" do
+ @s.rb_define_attr(CApiClassSpecs::Attr, :foo, true, false)
+ @a.foo.should == 1
+ lambda { @a.foo = 5 }.should raise_error(NameError)
+ end
+
+ it "defines an attr_writer when passed false, true" do
+ @s.rb_define_attr(CApiClassSpecs::Attr, :bar, false, true)
+ lambda { @a.bar }.should raise_error(NameError)
+ @a.bar = 5
+ @a.instance_variable_get(:@bar).should == 5
+ end
+
+ it "defines an attr_accessor when passed true, true" do
+ @s.rb_define_attr(CApiClassSpecs::Attr, :baz, true, true)
+ @a.baz.should == 3
+ @a.baz = 6
+ @a.baz.should == 6
+ end
+ end
+
+ describe "rb_call_super" do
+ it "calls the method in the superclass" do
+ @s.define_call_super_method CApiClassSpecs::Sub, "call_super_method"
+ obj = CApiClassSpecs::Sub.new
+ obj.call_super_method.should == :super_method
+ 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"
+ obj = CApiClassSpecs::SubSub.new
+ obj.call_super_method.should == :super_method
+ end
+ end
+
+ describe "rb_class2name" do
+ it "returns the class name" do
+ @s.rb_class2name(CApiClassSpecs).should == "CApiClassSpecs"
+ end
+
+ it "returns a string for an anonymous class" do
+ @s.rb_class2name(Class.new).should be_kind_of(String)
+ end
+ end
+
+ describe "rb_class_path" do
+ it "returns a String of a class path with no scope modifiers" do
+ @s.rb_class_path(Array).should == "Array"
+ end
+
+ it "returns a String of a class path with scope modifiers" do
+ @s.rb_class_path(File::Stat).should == "File::Stat"
+ end
+ end
+
+ describe "rb_class_name" do
+ it "returns the class name" do
+ @s.rb_class_name(CApiClassSpecs).should == "CApiClassSpecs"
+ end
+
+ it "returns a string for an anonymous class" do
+ @s.rb_class_name(Class.new).should be_kind_of(String)
+ end
+ end
+
+ describe "rb_path2class" do
+ it_behaves_like :rb_path_to_class, :rb_path2class
+ end
+
+ describe "rb_path_to_class" do
+ it_behaves_like :rb_path_to_class, :rb_path_to_class
+ end
+
+ describe "rb_cvar_defined" do
+ it "returns false when the class variable is not defined" do
+ @s.rb_cvar_defined(CApiClassSpecs::CVars, "@@nocvar").should be_false
+ end
+
+ it "returns true when the class variable is defined" do
+ @s.rb_cvar_defined(CApiClassSpecs::CVars, "@@cvar").should be_true
+ end
+
+ it "returns true if the class instance variable is defined" do
+ @s.rb_cvar_defined(CApiClassSpecs::CVars, "@c_ivar").should be_true
+ end
+ end
+
+ describe "rb_cv_set" do
+ it "sets a class variable" do
+ o = CApiClassSpecs::CVars.new
+ o.new_cv.should be_nil
+ @s.rb_cv_set(CApiClassSpecs::CVars, "@@new_cv", 1)
+ o.new_cv.should == 1
+ CApiClassSpecs::CVars.remove_class_variable :@@new_cv
+ end
+ end
+
+ describe "rb_cv_get" do
+ it "returns the value of the class variable" do
+ @s.rb_cvar_get(CApiClassSpecs::CVars, "@@cvar").should == :cvar
+ end
+
+ it "raises a NameError if the class variable is not defined" do
+ lambda {
+ @s.rb_cv_get(CApiClassSpecs::CVars, "@@no_cvar")
+ }.should raise_error(NameError, /class variable @@no_cvar/)
+ end
+ end
+
+ describe "rb_cvar_set" do
+ it "sets a class variable" do
+ o = CApiClassSpecs::CVars.new
+ o.new_cvar.should be_nil
+ @s.rb_cvar_set(CApiClassSpecs::CVars, "@@new_cvar", 1)
+ o.new_cvar.should == 1
+ CApiClassSpecs::CVars.remove_class_variable :@@new_cvar
+ end
+
+ end
+
+ describe "rb_define_class" do
+ before :each do
+ @cls = @s.rb_define_class("ClassSpecDefineClass", CApiClassSpecs::Super)
+ end
+
+ it "creates a subclass of the superclass" do
+ @cls.should be_kind_of(Class)
+ ClassSpecDefineClass.should equal(@cls)
+ @cls.superclass.should == CApiClassSpecs::Super
+ end
+
+ it "sets the class name" do
+ @cls.name.should == "ClassSpecDefineClass"
+ end
+
+ it "calls #inherited on the superclass" do
+ CApiClassSpecs::Super.should_receive(:inherited)
+ @s.rb_define_class("ClassSpecDefineClass2", CApiClassSpecs::Super)
+ Object.send(:remove_const, :ClassSpecDefineClass2)
+ end
+
+ it "raises a TypeError when given a non class object to superclass" do
+ lambda {
+ @s.rb_define_class("ClassSpecDefineClass3", Module.new)
+ }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when given a mismatched class to superclass" do
+ lambda {
+ @s.rb_define_class("ClassSpecDefineClass", Object)
+ }.should raise_error(TypeError)
+ end
+
+ ruby_version_is "2.4" do
+ it "raises a ArgumentError when given NULL as superclass" do
+ lambda {
+ @s.rb_define_class("ClassSpecDefineClass4", nil)
+ }.should raise_error(ArgumentError)
+ end
+ end
+ end
+
+ describe "rb_define_class_under" do
+ it "creates a subclass of the superclass contained in a module" do
+ cls = @s.rb_define_class_under(CApiClassSpecs,
+ "ClassUnder1",
+ CApiClassSpecs::Super)
+ cls.should be_kind_of(Class)
+ CApiClassSpecs::Super.should be_ancestor_of(CApiClassSpecs::ClassUnder1)
+ end
+
+ it "sets the class name" do
+ cls = @s.rb_define_class_under(CApiClassSpecs, "ClassUnder3", Object)
+ cls.name.should == "CApiClassSpecs::ClassUnder3"
+ end
+
+ it "calls #inherited on the superclass" do
+ CApiClassSpecs::Super.should_receive(:inherited)
+ @s.rb_define_class_under(CApiClassSpecs, "ClassUnder4", CApiClassSpecs::Super)
+ CApiClassSpecs.send(:remove_const, :ClassUnder4)
+ end
+
+ it "raises a TypeError when given a non class object to superclass" do
+ lambda { @s.rb_define_class_under(CApiClassSpecs,
+ "ClassUnder5",
+ Module.new)
+ }.should raise_error(TypeError)
+ end
+
+ ruby_version_is "2.3" do
+ it "raises a TypeError when given a mismatched class to superclass" do
+ CApiClassSpecs::ClassUnder6 = Class.new(CApiClassSpecs::Super)
+ lambda { @s.rb_define_class_under(CApiClassSpecs,
+ "ClassUnder6",
+ Class.new)
+ }.should raise_error(TypeError)
+ end
+ end
+
+ ruby_version_is ""..."2.3" do
+ it "raises a NameError when given a mismatched class to superclass" do
+ CApiClassSpecs::ClassUnder6 = Class.new(CApiClassSpecs::Super)
+ lambda { @s.rb_define_class_under(CApiClassSpecs,
+ "ClassUnder6",
+ Class.new)
+ }.should raise_error(NameError)
+ end
+ end
+
+ it "defines a class for an existing Autoload" do
+ compile_extension("class_under_autoload")
+
+ ClassUnderAutoload.name.should == "ClassUnderAutoload"
+ end
+
+ ruby_version_is "2.3" do
+ it "raises a TypeError if class is defined and its superclass mismatches the given one" do
+ lambda { @s.rb_define_class_under(CApiClassSpecs, "Sub", Object) }.should raise_error(TypeError)
+ end
+ end
+ end
+
+ describe "rb_define_class_id_under" do
+ it "creates a subclass of the superclass contained in a module" do
+ cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder1, CApiClassSpecs::Super)
+ cls.should be_kind_of(Class)
+ CApiClassSpecs::Super.should be_ancestor_of(CApiClassSpecs::ClassIdUnder1)
+ end
+
+ it "sets the class name" do
+ cls = @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder3, Object)
+ cls.name.should == "CApiClassSpecs::ClassIdUnder3"
+ end
+
+ it "calls #inherited on the superclass" do
+ CApiClassSpecs::Super.should_receive(:inherited)
+ @s.rb_define_class_id_under(CApiClassSpecs, :ClassIdUnder4, CApiClassSpecs::Super)
+ CApiClassSpecs.send(:remove_const, :ClassIdUnder4)
+ end
+
+ it "defines a class for an existing Autoload" do
+ compile_extension("class_id_under_autoload")
+
+ ClassIdUnderAutoload.name.should == "ClassIdUnderAutoload"
+ end
+
+ ruby_version_is "2.3" do
+ it "raises a TypeError if class is defined and its superclass mismatches the given one" do
+ lambda { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, Object) }.should raise_error(TypeError)
+ end
+ end
+ end
+
+ describe "rb_define_class_variable" do
+ it "sets a class variable" do
+ o = CApiClassSpecs::CVars.new
+ o.rbdcv_cvar.should be_nil
+ @s.rb_define_class_variable(CApiClassSpecs::CVars, "@@rbdcv_cvar", 1)
+ o.rbdcv_cvar.should == 1
+ CApiClassSpecs::CVars.remove_class_variable :@@rbdcv_cvar
+ end
+ end
+
+ describe "rb_cvar_get" do
+ it "returns the value of the class variable" do
+ @s.rb_cvar_get(CApiClassSpecs::CVars, "@@cvar").should == :cvar
+ end
+
+ it "raises a NameError if the class variable is not defined" do
+ lambda {
+ @s.rb_cvar_get(CApiClassSpecs::CVars, "@@no_cvar")
+ }.should raise_error(NameError, /class variable @@no_cvar/)
+ end
+ end
+
+ describe "rb_class_new" do
+ it "returns an new subclass of the superclass" do
+ subclass = @s.rb_class_new(CApiClassSpecs::NewClass)
+ CApiClassSpecs::NewClass.should be_ancestor_of(subclass)
+ end
+
+ it "raises a TypeError if passed Class as the superclass" do
+ lambda { @s.rb_class_new(Class) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if passed a singleton class as the superclass" do
+ metaclass = Object.new.singleton_class
+ lambda { @s.rb_class_new(metaclass) }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_class_superclass" do
+ it "returns the superclass of a class" do
+ cls = @s.rb_class_superclass(CApiClassSpecs::Sub)
+ cls.should == CApiClassSpecs::Super
+ end
+
+ it "returns nil if the class has no superclass" do
+ @s.rb_class_superclass(BasicObject).should be_nil
+ end
+ end
+
+ describe "rb_class_real" do
+ it "returns the class of an object ignoring the singleton class" do
+ obj = CApiClassSpecs::Sub.new
+ def obj.some_method() end
+
+ @s.rb_class_real(obj).should == CApiClassSpecs::Sub
+ end
+
+ it "returns the class of an object ignoring included modules" do
+ obj = CApiClassSpecs::SubM.new
+ @s.rb_class_real(obj).should == CApiClassSpecs::SubM
+ end
+
+ it "returns 0 if passed 0" do
+ @s.rb_class_real(0).should == 0
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/complex_spec.rb b/spec/ruby/optional/capi/complex_spec.rb
new file mode 100644
index 0000000000..ad84e47ced
--- /dev/null
+++ b/spec/ruby/optional/capi/complex_spec.rb
@@ -0,0 +1,45 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("complex")
+
+describe :rb_Complex, shared: true do
+ it "creates a new Complex with numerator and denominator" do
+ @r.send(@method, 1, 2).should == Complex(1, 2)
+ end
+end
+
+describe :rb_complex_new, shared: true do
+ it "creates a normalized Complex" do
+ r = @r.send(@method, 10, 4)
+ r.real.should == 10
+ r.imag.should == 4
+ end
+end
+
+describe "CApiComplexSpecs" do
+ before :each do
+ @r = CApiComplexSpecs.new
+ end
+
+ describe "rb_Complex" do
+ it_behaves_like :rb_Complex, :rb_Complex
+ end
+
+ describe "rb_Complex2" do
+ it_behaves_like :rb_Complex, :rb_Complex2
+ end
+
+ describe "rb_Complex1" do
+ it "creates a new Complex with real and imaginary of 0" do
+ @r.rb_Complex1(5).should == Complex(5, 0)
+ end
+ end
+
+ describe "rb_complex_new" do
+ it_behaves_like :rb_complex_new, :rb_complex_new
+ end
+
+ describe "rb_complex_new2" do
+ it_behaves_like :rb_complex_new, :rb_complex_new2
+ end
+end
diff --git a/spec/ruby/optional/capi/constants_spec.rb b/spec/ruby/optional/capi/constants_spec.rb
new file mode 100644
index 0000000000..d663ecda30
--- /dev/null
+++ b/spec/ruby/optional/capi/constants_spec.rb
@@ -0,0 +1,268 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("constants")
+
+describe "C-API constant" do
+ before :each do
+ @s = CApiConstantsSpecs.new
+ end
+
+ specify "rb_cArray references the Array class" do
+ @s.rb_cArray.should == Array
+ end
+
+ ruby_version_is ""..."2.4" do
+ specify "rb_cBignum references the Bignum class" do
+ @s.rb_cBignum.should == Bignum
+ end
+ end
+
+ specify "rb_cClass references the Class class" do
+ @s.rb_cClass.should == Class
+ end
+
+ specify "rb_mComparable references the Comparable module" do
+ @s.rb_mComparable.should == Comparable
+ end
+
+ specify "rb_cData references the Data class" do
+ @s.rb_cData.should == Data
+ end
+
+ specify "rb_mEnumerable references the Enumerable module" do
+ @s.rb_mEnumerable.should == Enumerable
+ end
+
+ specify "rb_cFalseClass references the FalseClass class" do
+ @s.rb_cFalseClass.should == FalseClass
+ end
+
+ specify "rb_cFile references the File class" do
+ @s.rb_cFile.should == File
+ end
+
+ ruby_version_is ""..."2.4" do
+ specify "rb_cFixnum references the Fixnum class" do
+ @s.rb_cFixnum.should == Fixnum
+ end
+ end
+
+ specify "rb_cFloat references the Float class" do
+ @s.rb_cFloat.should == Float
+ end
+
+ specify "rb_cHash references the Hash class" do
+ @s.rb_cHash.should == Hash
+ end
+
+ specify "rb_cInteger references the Integer class" do
+ @s.rb_cInteger.should == Integer
+ end
+
+ specify "rb_cIO references the IO class" do
+ @s.rb_cIO.should == IO
+ end
+
+ specify "rb_mKernel references the Kernel module" do
+ @s.rb_mKernel.should == Kernel
+ end
+
+ specify "rb_cMatch references the MatchData class" do
+ @s.rb_cMatch.should == MatchData
+ end
+
+ specify "rb_cModule references the Module class" do
+ @s.rb_cModule.should == Module
+ end
+
+ specify "rb_cNilClass references the NilClass class" do
+ @s.rb_cNilClass.should == NilClass
+ end
+
+ specify "rb_cNumeric references the Numeric class" do
+ @s.rb_cNumeric.should == Numeric
+ end
+
+ specify "rb_cObject references the Object class" do
+ @s.rb_cObject.should == Object
+ end
+
+ specify "rb_cRange references the Range class" do
+ @s.rb_cRange.should == Range
+ end
+
+ specify "rb_cRegexp references the Regexp class" do
+ @s.rb_cRegexp.should == Regexp
+ end
+
+ specify "rb_cString references the String class" do
+ @s.rb_cString.should == String
+ end
+
+ specify "rb_cStruct references the Struct class" do
+ @s.rb_cStruct.should == Struct
+ end
+
+ specify "rb_cSymbol references the Symbol class" do
+ @s.rb_cSymbol.should == Symbol
+ end
+
+ specify "rb_cTime references the Time class" do
+ @s.rb_cTime.should == Time
+ end
+
+ specify "rb_cThread references the Thread class" do
+ @s.rb_cThread.should == Thread
+ end
+
+ specify "rb_cTrueClass references the TrueClass class" do
+ @s.rb_cTrueClass.should == TrueClass
+ end
+
+ specify "rb_cProc references the Proc class" do
+ @s.rb_cProc.should == Proc
+ end
+
+ specify "rb_cMethod references the Method class" do
+ @s.rb_cMethod.should == Method
+ end
+
+ specify "rb_cDir references the Dir class" do
+ @s.rb_cDir.should == Dir
+ end
+
+end
+
+describe "C-API exception constant" do
+ before :each do
+ @s = CApiConstantsSpecs.new
+ end
+
+ specify "rb_eArgError references the ArgumentError class" do
+ @s.rb_eArgError.should == ArgumentError
+ end
+
+ specify "rb_eEOFError references the EOFError class" do
+ @s.rb_eEOFError.should == EOFError
+ end
+
+ specify "rb_eErrno references the Errno module" do
+ @s.rb_mErrno.should == Errno
+ end
+
+ specify "rb_eException references the Exception class" do
+ @s.rb_eException.should == Exception
+ end
+
+ specify "rb_eFloatDomainError references the FloatDomainError class" do
+ @s.rb_eFloatDomainError.should == FloatDomainError
+ end
+
+ specify "rb_eIndexError references the IndexError class" do
+ @s.rb_eIndexError.should == IndexError
+ end
+
+ specify "rb_eInterrupt references the Interrupt class" do
+ @s.rb_eInterrupt.should == Interrupt
+ end
+
+ specify "rb_eIOError references the IOError class" do
+ @s.rb_eIOError.should == IOError
+ end
+
+ specify "rb_eLoadError references the LoadError class" do
+ @s.rb_eLoadError.should == LoadError
+ end
+
+ specify "rb_eLocalJumpError references the LocalJumpError class" do
+ @s.rb_eLocalJumpError.should == LocalJumpError
+ end
+
+ specify "rb_eMathDomainError references the Math::DomainError class" do
+ @s.rb_eMathDomainError.should == Math::DomainError
+ end
+
+ specify "rb_eEncCompatError references the Encoding::CompatibilityError" do
+ @s.rb_eEncCompatError.should == Encoding::CompatibilityError
+ end
+
+ specify "rb_eNameError references the NameError class" do
+ @s.rb_eNameError.should == NameError
+ end
+
+ specify "rb_eNoMemError references the NoMemoryError class" do
+ @s.rb_eNoMemError.should == NoMemoryError
+ end
+
+ specify "rb_eNoMethodError references the NoMethodError class" do
+ @s.rb_eNoMethodError.should == NoMethodError
+ end
+
+ specify "rb_eNotImpError references the NotImplementedError class" do
+ @s.rb_eNotImpError.should == NotImplementedError
+ end
+
+ specify "rb_eRangeError references the RangeError class" do
+ @s.rb_eRangeError.should == RangeError
+ end
+
+ specify "rb_eRegexpError references the RegexpError class" do
+ @s.rb_eRegexpError.should == RegexpError
+ end
+
+ specify "rb_eRuntimeError references the RuntimeError class" do
+ @s.rb_eRuntimeError.should == RuntimeError
+ end
+
+ specify "rb_eScriptError references the ScriptError class" do
+ @s.rb_eScriptError.should == ScriptError
+ end
+
+ specify "rb_eSecurityError references the SecurityError class" do
+ @s.rb_eSecurityError.should == SecurityError
+ end
+
+ specify "rb_eSignal references the SignalException class" do
+ @s.rb_eSignal.should == SignalException
+ end
+
+ specify "rb_eStandardError references the StandardError class" do
+ @s.rb_eStandardError.should == StandardError
+ end
+
+ specify "rb_eSyntaxError references the SyntaxError class" do
+ @s.rb_eSyntaxError.should == SyntaxError
+ end
+
+ specify "rb_eSystemCallError references the SystemCallError class" do
+ @s.rb_eSystemCallError.should == SystemCallError
+ end
+
+ specify "rb_eSystemExit references the SystemExit class" do
+ @s.rb_eSystemExit.should == SystemExit
+ end
+
+ specify "rb_eSysStackError references the SystemStackError class" do
+ @s.rb_eSysStackError.should == SystemStackError
+ end
+
+ specify "rb_eTypeError references the TypeError class" do
+ @s.rb_eTypeError.should == TypeError
+ end
+
+ specify "rb_eThreadError references the ThreadError class" do
+ @s.rb_eThreadError.should == ThreadError
+ end
+
+ specify "rb_mWaitReadable references the IO::WaitReadable module" do
+ @s.rb_mWaitReadable.should == IO::WaitReadable
+ end
+
+ specify "rb_mWaitWritable references the IO::WaitWritable module" do
+ @s.rb_mWaitWritable.should == IO::WaitWritable
+ end
+
+ specify "rb_eZeroDivError references the ZeroDivisionError class" do
+ @s.rb_eZeroDivError.should == ZeroDivisionError
+ end
+end
diff --git a/spec/ruby/optional/capi/data_spec.rb b/spec/ruby/optional/capi/data_spec.rb
new file mode 100644
index 0000000000..ae1f57b091
--- /dev/null
+++ b/spec/ruby/optional/capi/data_spec.rb
@@ -0,0 +1,46 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("data")
+
+describe "CApiAllocSpecs (a class with an alloc func defined)" do
+ it "calls the alloc func" do
+ @s = CApiAllocSpecs.new
+ @s.wrapped_data.should == 42 # not defined in initialize
+ end
+end
+
+describe "CApiWrappedStruct" do
+ before :each do
+ @s = CApiWrappedStructSpecs.new
+ end
+
+ it "wraps with Data_Wrap_Struct and Data_Get_Struct returns data" do
+ a = @s.wrap_struct(1024)
+ @s.get_struct(a).should == 1024
+ end
+
+ it "allows for using NULL as the klass for Data_Wrap_Struct" do
+ a = @s.wrap_struct_null(1024)
+ @s.get_struct(a).should == 1024
+ end
+
+ describe "RDATA()" do
+ it "returns the struct data" do
+ a = @s.wrap_struct(1024)
+ @s.get_struct_rdata(a).should == 1024
+ end
+
+ it "allows changing the wrapped struct" do
+ a = @s.wrap_struct(1024)
+ @s.change_struct(a, 100)
+ @s.get_struct(a).should == 100
+ end
+ end
+
+ describe "DATA_PTR" do
+ it "returns the struct data" do
+ a = @s.wrap_struct(1024)
+ @s.get_struct_data_ptr(a).should == 1024
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb
new file mode 100644
index 0000000000..9408c0c6c5
--- /dev/null
+++ b/spec/ruby/optional/capi/encoding_spec.rb
@@ -0,0 +1,485 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../spec_helper', __FILE__)
+require File.expand_path('../fixtures/encoding', __FILE__)
+
+load_extension('encoding')
+
+describe :rb_enc_get_index, shared: true do
+ it "returns the index of the encoding of a String" do
+ @s.send(@method, "string").should >= 0
+ end
+
+ it "returns the index of the encoding of a Regexp" do
+ @s.send(@method, /regexp/).should >= 0
+ end
+
+ it "returns the index of the encoding of an Object" do
+ obj = mock("rb_enc_get_index string")
+ @s.rb_enc_set_index(obj, 1)
+ @s.send(@method, obj).should == 1
+ end
+
+ it "returns the index of the dummy encoding of an Object" do
+ obj = mock("rb_enc_get_index string")
+ index = Encoding.list.index(Encoding::UTF_16)
+ @s.rb_enc_set_index(obj, index)
+ @s.send(@method, obj).should == index
+ end
+
+ it "returns 0 for an object without an encoding" do
+ obj = mock("rb_enc_get_index string")
+ @s.send(@method, obj).should == 0
+ end
+end
+
+describe :rb_enc_set_index, shared: true do
+ it "sets the object's encoding to the Encoding specified by the index" do
+ obj = "abc"
+ result = @s.send(@method, obj, 2)
+
+ # This is used because indexes should be considered implementation
+ # dependent. So a pair is returned:
+ # [rb_enc_find_index()->name, rb_enc_get(obj)->name]
+ result.first.should == result.last
+ end
+
+ it "associates an encoding with a subclass of String" do
+ str = CApiEncodingSpecs::S.new "abc"
+ result = @s.send(@method, str, 1)
+ result.first.should == result.last
+ end
+
+ it "associates an encoding with an object" do
+ obj = mock("rb_enc_set_index string")
+ result = @s.send(@method, obj, 1)
+ result.first.should == result.last
+ end
+end
+
+describe "C-API Encoding function" do
+ before :each do
+ @s = CApiEncodingSpecs.new
+ end
+
+ describe "rb_encdb_alias" do
+ it "creates an alias for an existing Encoding" do
+ @s.rb_encdb_alias("ZOMGWTFBBQ", "UTF-8").should >= 0
+ Encoding.find("ZOMGWTFBBQ").name.should == "UTF-8"
+ end
+ end
+
+ describe "rb_enc_find" do
+ it "returns the encoding of an Encoding" do
+ @s.rb_enc_find("UTF-8").should == "UTF-8"
+ end
+
+ it "returns the encoding of an Encoding specified with lower case" do
+ @s.rb_enc_find("utf-8").should == "UTF-8"
+ end
+ end
+
+ describe "rb_enc_find_index" do
+ it "returns the index of an Encoding" do
+ @s.rb_enc_find_index("UTF-8").should >= 0
+ end
+
+ it "returns the index of an Encoding specified with lower case" do
+ @s.rb_enc_find_index("utf-8").should >= 0
+ end
+
+ it "returns -1 for an non existing encoding" do
+ @s.rb_enc_find_index("non-existant-encoding").should == -1
+ end
+ end
+
+ describe "rb_enc_from_index" do
+ it "returns an Encoding" do
+ @s.rb_enc_from_index(0).should be_an_instance_of(String)
+ end
+ end
+
+ describe "rb_usascii_encoding" do
+ it "returns the encoding for Encoding::US_ASCII" do
+ @s.rb_usascii_encoding.should == "US-ASCII"
+ end
+ end
+
+ describe "rb_ascii8bit_encoding" do
+ it "returns the encoding for Encoding::ASCII_8BIT" do
+ @s.rb_ascii8bit_encoding.should == "ASCII-8BIT"
+ end
+ end
+
+ describe "rb_utf8_encoding" do
+ it "returns the encoding for Encoding::UTF_8" do
+ @s.rb_utf8_encoding.should == "UTF-8"
+ end
+ end
+
+ describe "rb_enc_from_encoding" do
+ it "returns an Encoding instance from an encoding data structure" do
+ @s.rb_enc_from_encoding("UTF-8").should == Encoding::UTF_8
+ end
+ end
+
+ describe "rb_locale_encoding" do
+ it "returns the encoding for the current locale" do
+ @s.rb_locale_encoding.should == Encoding.find('locale').name
+ end
+ end
+
+ describe "rb_filesystem_encoding" do
+ it "returns the encoding for the current filesystem" do
+ @s.rb_filesystem_encoding.should == Encoding.find('filesystem').name
+ end
+ end
+
+ describe "rb_enc_get" do
+ it "returns the encoding ossociated with an object" do
+ str = "abc".encode Encoding::ASCII_8BIT
+ @s.rb_enc_get(str).should == "ASCII-8BIT"
+ end
+ end
+
+ describe "rb_obj_encoding" do
+ it "returns the encoding ossociated with an object" do
+ str = "abc".encode Encoding::ASCII_8BIT
+ @s.rb_obj_encoding(str).should == Encoding::ASCII_8BIT
+ end
+ end
+
+ describe "rb_enc_get_index" do
+ it_behaves_like :rb_enc_get_index, :rb_enc_get_index
+
+ it "returns the index of the encoding of a Symbol" do
+ @s.send(@method, :symbol).should >= 0
+ end
+
+ it "returns -1 as the index of nil" do
+ @s.send(@method, nil).should == -1
+ end
+
+ it "returns -1 as the index for immediates" do
+ @s.send(@method, 1).should == -1
+ end
+ end
+
+ describe "rb_enc_set_index" do
+ it_behaves_like :rb_enc_set_index, :rb_enc_set_index
+ end
+
+ describe "rb_enc_str_new" do
+ it "returns a String in US-ASCII encoding when high bits are set" do
+ xEE = [0xEE].pack('C').force_encoding('utf-8')
+ result = @s.rb_enc_str_new(xEE, 1, Encoding::US_ASCII)
+ result.encoding.should equal(Encoding::US_ASCII)
+ end
+ end
+
+ describe "rb_enc_str_coderange" do
+ describe "when the encoding is ASCII-8BIT" do
+ it "returns ENC_CODERANGE_7BIT if there are no high bits set" do
+ result = @s.rb_enc_str_coderange("abc".force_encoding("ascii-8bit"))
+ result.should == :coderange_7bit
+ end
+
+ it "returns ENC_CODERANGE_VALID if there are high bits set" do
+ xEE = [0xEE].pack('C').force_encoding('utf-8')
+ result = @s.rb_enc_str_coderange(xEE.force_encoding("ascii-8bit"))
+ result.should == :coderange_valid
+ end
+ end
+
+ describe "when the encoding is UTF-8" do
+ it "returns ENC_CODERANGE_7BIT if there are no high bits set" do
+ result = @s.rb_enc_str_coderange("abc".force_encoding("utf-8"))
+ result.should == :coderange_7bit
+ end
+
+ it "returns ENC_CODERANGE_VALID if there are high bits set in a valid string" do
+ result = @s.rb_enc_str_coderange("\xE3\x81\x82".force_encoding("utf-8"))
+ result.should == :coderange_valid
+ end
+
+ it "returns ENC_CODERANGE_BROKEN if there are high bits set in an invalid string" do
+ result = @s.rb_enc_str_coderange([0xEE].pack('C').force_encoding("utf-8"))
+ result.should == :coderange_broken
+ end
+ end
+
+ describe "when the encoding is US-ASCII" do
+ it "returns ENC_CODERANGE_7BIT if there are no high bits set" do
+ result = @s.rb_enc_str_coderange("abc".force_encoding("us-ascii"))
+ result.should == :coderange_7bit
+ end
+
+ it "returns ENC_CODERANGE_BROKEN if there are high bits set" do
+ result = @s.rb_enc_str_coderange([0xEE].pack('C').force_encoding("us-ascii"))
+ result.should == :coderange_broken
+ end
+ end
+ end
+
+ describe "ENCODING_GET" do
+ it_behaves_like :rb_enc_get_index, :ENCODING_GET
+ end
+
+ describe "ENCODING_SET" do
+ it_behaves_like :rb_enc_set_index, :ENCODING_SET
+ end
+
+ describe "ENC_CODERANGE_ASCIIONLY" do
+ it "returns true if the object encoding is only ASCII" do
+ str = "abc".force_encoding("us-ascii")
+ str.valid_encoding? # make sure to set the coderange
+ @s.ENC_CODERANGE_ASCIIONLY(str).should be_true
+ end
+
+ it "returns false if the object encoding is not ASCII only" do
+ str = "ありがとう".force_encoding("utf-8")
+ @s.ENC_CODERANGE_ASCIIONLY(str).should be_false
+ end
+ end
+
+ describe "rb_to_encoding" do
+ it "returns the encoding for the Encoding instance passed" do
+ @s.rb_to_encoding(Encoding::BINARY).should == "ASCII-8BIT"
+ end
+
+ it "returns the correct encoding for a replicated encoding" do
+ @s.rb_to_encoding(Encoding::IBM857).should == "IBM857"
+ end
+
+ it "returns the encoding when passed a String" do
+ @s.rb_to_encoding("ASCII").should == "US-ASCII"
+ end
+
+ it "calls #to_str to convert the argument to a String" do
+ obj = mock("rb_to_encoding Encoding name")
+ obj.should_receive(:to_str).and_return("utf-8")
+
+ @s.rb_to_encoding(obj).should == "UTF-8"
+ end
+ end
+
+ describe "rb_to_encoding_index" do
+ it "returns the index of the encoding for the Encoding instance passed" do
+ @s.rb_to_encoding_index(Encoding::BINARY).should >= 0
+ end
+
+ it "returns the index of the encoding when passed a String" do
+ @s.rb_to_encoding_index("ASCII").should >= 0
+ end
+
+ it "returns the index of the dummy encoding of an Object" do
+ index = Encoding.list.index(Encoding::UTF_16)
+ @s.rb_to_encoding_index(Encoding::UTF_16.name).should == index
+ end
+
+ it "calls #to_str to convert the argument to a String" do
+ obj = mock("rb_to_encoding Encoding name")
+ obj.should_receive(:to_str).and_return("utf-8")
+
+ @s.rb_to_encoding_index(obj).should >= 0
+ end
+ end
+
+ describe "rb_enc_compatible" do
+ it "returns 0 if the encodings of the Strings are not compatible" do
+ a = [0xff].pack('C').force_encoding "ascii-8bit"
+ b = "\u3042".encode("utf-8")
+ @s.rb_enc_compatible(a, b).should == 0
+ end
+
+ # The coverage of this sucks, but there is not a simple way (yet?) to
+ # easily share the specs between rb_enc_compatible and
+ # Encoding.compatible?
+ it "returns the same value as Encoding.compatible? if the Strings have a compatible encoding" do
+ a = "abc".force_encoding("us-ascii")
+ b = "\u3042".encode("utf-8")
+ @s.rb_enc_compatible(a, b).should == Encoding.compatible?(a, b)
+ end
+ end
+
+ describe "rb_enc_copy" do
+ before :each do
+ @obj = "rb_enc_copy".encode(Encoding::US_ASCII)
+ end
+
+ it "sets the encoding of a String to that of the second argument" do
+ @s.rb_enc_copy("string", @obj).encoding.should == Encoding::US_ASCII
+ end
+
+ it "raises a RuntimeError if the second argument is a Symbol" do
+ lambda { @s.rb_enc_copy(:symbol, @obj) }.should raise_error(RuntimeError)
+ end
+
+ it "sets the encoding of a Regexp to that of the second argument" do
+ @s.rb_enc_copy(/regexp/, @obj).encoding.should == Encoding::US_ASCII
+ end
+ end
+
+ describe "rb_default_internal_encoding" do
+ before :each do
+ @default = Encoding.default_internal
+ end
+
+ after :each do
+ Encoding.default_internal = @default
+ end
+
+ it "returns 0 if Encoding.default_internal is nil" do
+ Encoding.default_internal = nil
+ @s.rb_default_internal_encoding.should be_nil
+ end
+
+ it "returns the encoding for Encoding.default_internal" do
+ Encoding.default_internal = "US-ASCII"
+ @s.rb_default_internal_encoding.should == "US-ASCII"
+ Encoding.default_internal = "UTF-8"
+ @s.rb_default_internal_encoding.should == "UTF-8"
+ end
+ end
+
+ describe "rb_default_external_encoding" do
+ before :each do
+ @default = Encoding.default_external
+ end
+
+ after :each do
+ Encoding.default_external = @default
+ end
+
+ it "returns the encoding for Encoding.default_external" do
+ Encoding.default_external = "BINARY"
+ @s.rb_default_external_encoding.should == "ASCII-8BIT"
+ end
+ end
+
+ describe "rb_enc_associate" do
+ it "sets the encoding of a String to the encoding" do
+ @s.rb_enc_associate("string", "ASCII-8BIT").encoding.should == Encoding::ASCII_8BIT
+ end
+
+ it "raises a RuntimeError if the argument is Symbol" do
+ lambda { @s.rb_enc_associate(:symbol, "US-ASCII") }.should raise_error(RuntimeError)
+ end
+
+ it "sets the encoding of a Regexp to the encoding" do
+ @s.rb_enc_associate(/regexp/, "ASCII-8BIT").encoding.should == Encoding::ASCII_8BIT
+ end
+
+ it "sets the encoding of a String to a default when the encoding is NULL" do
+ @s.rb_enc_associate("string", nil).encoding.should == Encoding::ASCII_8BIT
+ end
+ end
+
+ describe "rb_enc_associate_index" do
+ it "sets the encoding of a String to the encoding" do
+ index = @s.rb_enc_find_index("ASCII-8BIT")
+ enc = @s.rb_enc_associate_index("string", index).encoding
+ enc.should == Encoding::ASCII_8BIT
+ end
+
+ it "sets the encoding of a Regexp to the encoding" do
+ index = @s.rb_enc_find_index("UTF-8")
+ enc = @s.rb_enc_associate_index(/regexp/, index).encoding
+ enc.should == Encoding::UTF_8
+ end
+
+ it "sets the encoding of a Symbol to the encoding" do
+ index = @s.rb_enc_find_index("UTF-8")
+ lambda { @s.rb_enc_associate_index(:symbol, index) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_ascii8bit_encindex" do
+ it "returns an index for the ASCII-8BIT encoding" do
+ @s.rb_ascii8bit_encindex().should >= 0
+ end
+ end
+
+ describe "rb_utf8_encindex" do
+ it "returns an index for the UTF-8 encoding" do
+ @s.rb_utf8_encindex().should >= 0
+ end
+ end
+
+ describe "rb_usascii_encindex" do
+ it "returns an index for the US-ASCII encoding" do
+ @s.rb_usascii_encindex().should >= 0
+ end
+ end
+
+ describe "rb_locale_encindex" do
+ it "returns an index for the locale encoding" do
+ @s.rb_locale_encindex().should >= 0
+ end
+ end
+
+ describe "rb_filesystem_encindex" do
+ it "returns an index for the filesystem encoding" do
+ @s.rb_filesystem_encindex().should >= 0
+ end
+ end
+
+ describe "rb_enc_to_index" do
+ it "returns an index for the encoding" do
+ @s.rb_enc_to_index("UTF-8").should >= 0
+ end
+
+ it "returns a non-negative int if the encoding is not defined" do
+ # Encoding indexes are an implementation detail and not guaranteed
+ # across implementations.
+ @s.rb_enc_to_index("FTU-81").should >= 0
+ end
+ end
+
+ describe "rb_enc_nth" do
+ it "returns the byte index of the given character index" do
+ @s.rb_enc_nth("hüllo", 3).should == 4
+ end
+ end
+
+ describe "rb_enc_codepoint_len" do
+ it "raises ArgumentError if an empty string is given" do
+ lambda do
+ @s.rb_enc_codepoint_len("")
+ end.should raise_error(ArgumentError)
+ end
+
+ it "raises ArgumentError if an invalid byte sequence is given" do
+ lambda do
+ @s.rb_enc_codepoint_len([0xa0, 0xa1].pack('CC').force_encoding('utf-8')) # Invalid sequence identifier
+ end.should raise_error(ArgumentError)
+ end
+
+ it "returns codepoint 0x24 and length 1 for character '$'" do
+ codepoint, length = @s.rb_enc_codepoint_len("$")
+
+ codepoint.should == 0x24
+ length.should == 1
+ end
+
+ it "returns codepoint 0xA2 and length 2 for character '¢'" do
+ codepoint, length = @s.rb_enc_codepoint_len("¢")
+
+ codepoint.should == 0xA2
+ length.should == 2
+ end
+
+ it "returns codepoint 0x20AC and length 3 for character '€'" do
+ codepoint, length = @s.rb_enc_codepoint_len("€")
+
+ codepoint.should == 0x20AC
+ length.should == 3
+ end
+
+ it "returns codepoint 0x24B62 and length 4 for character '𤭢'" do
+ codepoint, length = @s.rb_enc_codepoint_len("𤭢")
+
+ codepoint.should == 0x24B62
+ length.should == 4
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/enumerator_spec.rb b/spec/ruby/optional/capi/enumerator_spec.rb
new file mode 100644
index 0000000000..44634f73a1
--- /dev/null
+++ b/spec/ruby/optional/capi/enumerator_spec.rb
@@ -0,0 +1,39 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("enumerator")
+
+describe "C-API Enumerator function" do
+ before :each do
+ @s = CApiEnumeratorSpecs.new
+ end
+
+ describe "rb_enumeratorize" do
+ before do
+ @enumerable = [1, 2, 3]
+ end
+
+ it "constructs a new Enumerator for the given object, method and arguments" do
+ enumerator = @s.rb_enumeratorize(@enumerable, :each, :arg1, :arg2)
+ enumerator.class.should == Enumerator
+ end
+
+ it "enumerates the given object" do
+ enumerator = @s.rb_enumeratorize(@enumerable, :each)
+ enumerated = []
+ enumerator.each { |i| enumerated << i }
+ enumerated.should == @enumerable
+ end
+
+ it "uses the given method for enumeration" do
+ enumerator = @s.rb_enumeratorize(@enumerable, :awesome_each)
+ @enumerable.should_receive(:awesome_each)
+ enumerator.each {}
+ end
+
+ it "passes the given arguments to the enumeration method" do
+ enumerator = @s.rb_enumeratorize(@enumerable, :each, :arg1, :arg2)
+ @enumerable.should_receive(:each).with(:arg1, :arg2)
+ enumerator.each {}
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/exception_spec.rb b/spec/ruby/optional/capi/exception_spec.rb
new file mode 100644
index 0000000000..4800e6ffc0
--- /dev/null
+++ b/spec/ruby/optional/capi/exception_spec.rb
@@ -0,0 +1,58 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("exception")
+
+describe "C-API Exception function" do
+ before :each do
+ @s = CApiExceptionSpecs.new
+ end
+
+ describe "rb_exc_new" do
+ it "creates an exception from a C string and length" do
+ @s.rb_exc_new('foo').to_s.should == 'foo'
+ end
+ end
+
+ describe "rb_exc_new2" do
+ it "creates an exception from a C string" do
+ @s.rb_exc_new2('foo').to_s.should == 'foo'
+ end
+ end
+
+ describe "rb_exc_new3" do
+ it "creates an exception from a Ruby string" do
+ @s.rb_exc_new3('foo').to_s.should == 'foo'
+ end
+ end
+
+ describe "rb_exc_raise" do
+ it "raises passed exception" do
+ runtime_error = RuntimeError.new '42'
+ lambda { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42')
+ end
+
+ it "raises an exception with an empty backtrace" do
+ runtime_error = RuntimeError.new '42'
+ runtime_error.set_backtrace []
+ lambda { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42')
+ end
+ end
+
+ describe "rb_set_errinfo" do
+ after :each do
+ @s.rb_set_errinfo(nil)
+ end
+
+ it "accepts nil" do
+ @s.rb_set_errinfo(nil).should be_nil
+ end
+
+ it "accepts an Exception instance" do
+ @s.rb_set_errinfo(Exception.new).should be_nil
+ end
+
+ it "raises a TypeError if the object is not nil or an Exception instance" do
+ lambda { @s.rb_set_errinfo("error") }.should raise_error(TypeError)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/ext/.gitignore b/spec/ruby/optional/capi/ext/.gitignore
new file mode 100644
index 0000000000..577d117bb1
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/.gitignore
@@ -0,0 +1,9 @@
+# signature of implementation that
+# last compiled an extension
+*.sig
+
+# build artifacts
+*.o
+*.so
+*.bundle
+*.dll
diff --git a/spec/ruby/optional/capi/ext/array_spec.c b/spec/ruby/optional/capi/ext/array_spec.c
new file mode 100644
index 0000000000..8bc144195c
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/array_spec.c
@@ -0,0 +1,452 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_ARRAY
+static VALUE array_spec_rb_Array(VALUE self, VALUE object) {
+ return rb_Array(object);
+}
+#endif
+
+#if defined(HAVE_RARRAY_LEN) && defined(HAVE_RARRAY_PTR)
+static VALUE array_spec_RARRAY_PTR_iterate(VALUE self, VALUE array) {
+ int i;
+ VALUE* ptr;
+
+ ptr = RARRAY_PTR(array);
+ for(i = 0; i < RARRAY_LEN(array); i++) {
+ rb_yield(ptr[i]);
+ }
+ return Qnil;
+}
+
+static VALUE array_spec_RARRAY_PTR_assign(VALUE self, VALUE array, VALUE value) {
+ int i;
+ VALUE* ptr;
+
+ ptr = RARRAY_PTR(array);
+ for(i = 0; i < RARRAY_LEN(array); i++) {
+ ptr[i] = value;
+ }
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RARRAY_LEN
+static VALUE array_spec_RARRAY_LEN(VALUE self, VALUE array) {
+ return INT2FIX(RARRAY_LEN(array));
+}
+#endif
+
+#ifdef HAVE_RARRAY_AREF
+static VALUE array_spec_RARRAY_AREF(VALUE self, VALUE array, VALUE index) {
+ return RARRAY_AREF(array, FIX2INT(index));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_AREF
+static VALUE array_spec_rb_ary_aref(int argc, VALUE *argv, VALUE self) {
+ VALUE ary, args;
+ rb_scan_args(argc, argv, "1*", &ary, &args);
+ return rb_ary_aref((int)RARRAY_LEN(args), RARRAY_PTR(args), ary);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_CLEAR
+static VALUE array_spec_rb_ary_clear(VALUE self, VALUE array) {
+ return rb_ary_clear(array);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_DELETE
+static VALUE array_spec_rb_ary_delete(VALUE self, VALUE array, VALUE item) {
+ return rb_ary_delete(array, item);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_DELETE_AT
+static VALUE array_spec_rb_ary_delete_at(VALUE self, VALUE array, VALUE index) {
+ return rb_ary_delete_at(array, NUM2LONG(index));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_DUP
+static VALUE array_spec_rb_ary_dup(VALUE self, VALUE array) {
+ return rb_ary_dup(array);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_ENTRY
+static VALUE array_spec_rb_ary_entry(VALUE self, VALUE array, VALUE offset) {
+ return rb_ary_entry(array, FIX2INT(offset));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_INCLUDES
+static VALUE array_spec_rb_ary_includes(VALUE self, VALUE ary, VALUE item) {
+ return rb_ary_includes(ary, item);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_JOIN
+static VALUE array_spec_rb_ary_join(VALUE self, VALUE array1, VALUE array2) {
+ return rb_ary_join(array1, array2);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_TO_S
+static VALUE array_spec_rb_ary_to_s(VALUE self, VALUE array) {
+ return rb_ary_to_s(array);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW
+static VALUE array_spec_rb_ary_new(VALUE self) {
+ VALUE ret;
+ ret = rb_ary_new();
+ return ret;
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW2
+static VALUE array_spec_rb_ary_new2(VALUE self, VALUE length) {
+ return rb_ary_new2(NUM2LONG(length));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW_CAPA
+static VALUE array_spec_rb_ary_new_capa(VALUE self, VALUE length) {
+ return rb_ary_new_capa(NUM2LONG(length));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW3
+static VALUE array_spec_rb_ary_new3(VALUE self, VALUE first, VALUE second, VALUE third) {
+ return rb_ary_new3(3, first, second, third);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW_FROM_ARGS
+static VALUE array_spec_rb_ary_new_from_args(VALUE self, VALUE first, VALUE second, VALUE third) {
+ return rb_ary_new_from_args(3, first, second, third);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW4
+static VALUE array_spec_rb_ary_new4(VALUE self, VALUE first, VALUE second, VALUE third) {
+ VALUE values[3];
+ values[0] = first;
+ values[1] = second;
+ values[2] = third;
+ return rb_ary_new4(3, values);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_NEW_FROM_VALUES
+static VALUE array_spec_rb_ary_new_from_values(VALUE self, VALUE first, VALUE second, VALUE third) {
+ VALUE values[3];
+ values[0] = first;
+ values[1] = second;
+ values[2] = third;
+ return rb_ary_new_from_values(3, values);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_POP
+static VALUE array_spec_rb_ary_pop(VALUE self, VALUE array) {
+ return rb_ary_pop(array);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_PUSH
+static VALUE array_spec_rb_ary_push(VALUE self, VALUE array, VALUE item) {
+ rb_ary_push(array, item);
+ return array;
+}
+#endif
+
+#ifdef HAVE_RB_ARY_CAT
+static VALUE array_spec_rb_ary_cat(int argc, VALUE *argv, VALUE self) {
+ VALUE ary, args;
+ rb_scan_args(argc, argv, "1*", &ary, &args);
+ return rb_ary_cat(ary, RARRAY_PTR(args), RARRAY_LEN(args));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_REVERSE
+static VALUE array_spec_rb_ary_reverse(VALUE self, VALUE array) {
+ return rb_ary_reverse(array);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_ROTATE
+static VALUE array_spec_rb_ary_rotate(VALUE self, VALUE array, VALUE count) {
+ return rb_ary_rotate(array, NUM2LONG(count));
+}
+#endif
+
+#ifdef HAVE_RB_ARY_SHIFT
+static VALUE array_spec_rb_ary_shift(VALUE self, VALUE array) {
+ return rb_ary_shift(array);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_STORE
+static VALUE array_spec_rb_ary_store(VALUE self, VALUE array, VALUE offset, VALUE value) {
+ rb_ary_store(array, FIX2INT(offset), value);
+
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_ARY_CONCAT
+static VALUE array_spec_rb_ary_concat(VALUE self, VALUE array1, VALUE array2) {
+ return rb_ary_concat(array1, array2);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_PLUS
+static VALUE array_spec_rb_ary_plus(VALUE self, VALUE array1, VALUE array2) {
+ return rb_ary_plus(array1, array2);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_UNSHIFT
+static VALUE array_spec_rb_ary_unshift(VALUE self, VALUE array, VALUE val) {
+ return rb_ary_unshift(array, val);
+}
+#endif
+
+#ifdef HAVE_RB_ASSOC_NEW
+static VALUE array_spec_rb_assoc_new(VALUE self, VALUE first, VALUE second) {
+ return rb_assoc_new(first, second);
+}
+#endif
+
+#if defined(HAVE_RB_ITERATE) && defined(HAVE_RB_EACH)
+static VALUE copy_ary(VALUE el, VALUE new_ary) {
+ return rb_ary_push(new_ary, el);
+}
+
+static VALUE array_spec_rb_iterate(VALUE self, VALUE ary) {
+ VALUE new_ary = rb_ary_new();
+
+ rb_iterate(rb_each, ary, copy_ary, new_ary);
+
+ return new_ary;
+}
+
+static VALUE sub_pair(VALUE el, VALUE holder) {
+ return rb_ary_push(holder, rb_ary_entry(el, 1));
+}
+
+static VALUE each_pair(VALUE obj) {
+ return rb_funcall(obj, rb_intern("each_pair"), 0);
+}
+
+static VALUE array_spec_rb_iterate_each_pair(VALUE self, VALUE obj) {
+ VALUE new_ary = rb_ary_new();
+
+ rb_iterate(each_pair, obj, sub_pair, new_ary);
+
+ return new_ary;
+}
+
+static VALUE iter_yield(VALUE el, VALUE ary) {
+ rb_yield(el);
+ return Qnil;
+}
+
+static VALUE array_spec_rb_iterate_then_yield(VALUE self, VALUE obj) {
+ rb_iterate(rb_each, obj, iter_yield, obj);
+ return Qnil;
+}
+#endif
+
+#if defined(HAVE_RB_MEM_CLEAR)
+static VALUE array_spec_rb_mem_clear(VALUE self, VALUE obj) {
+ VALUE ary[1];
+ ary[0] = obj;
+ rb_mem_clear(ary, 1);
+ return ary[0];
+}
+#endif
+
+#ifdef HAVE_RB_ARY_FREEZE
+static VALUE array_spec_rb_ary_freeze(VALUE self, VALUE ary) {
+ return rb_ary_freeze(ary);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_TO_ARY
+static VALUE array_spec_rb_ary_to_ary(VALUE self, VALUE ary) {
+ return rb_ary_to_ary(ary);
+}
+#endif
+
+#ifdef HAVE_RB_ARY_SUBSEQ
+static VALUE array_spec_rb_ary_subseq(VALUE self, VALUE ary, VALUE begin, VALUE len) {
+ return rb_ary_subseq(ary, FIX2LONG(begin), FIX2LONG(len));
+}
+#endif
+
+void Init_array_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiArraySpecs", rb_cObject);
+
+#ifdef HAVE_RB_ARRAY
+ rb_define_method(cls, "rb_Array", array_spec_rb_Array, 1);
+#endif
+
+#ifdef HAVE_RARRAY_LEN
+ rb_define_method(cls, "RARRAY_LEN", array_spec_RARRAY_LEN, 1);
+#endif
+
+#if defined(HAVE_RARRAY_LEN) && defined(HAVE_RARRAY_PTR)
+ rb_define_method(cls, "RARRAY_PTR_iterate", array_spec_RARRAY_PTR_iterate, 1);
+ rb_define_method(cls, "RARRAY_PTR_assign", array_spec_RARRAY_PTR_assign, 2);
+#endif
+
+#ifdef HAVE_RARRAY_AREF
+ rb_define_method(cls, "RARRAY_AREF", array_spec_RARRAY_AREF, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_AREF
+ rb_define_method(cls, "rb_ary_aref", array_spec_rb_ary_aref, -1);
+#endif
+
+#ifdef HAVE_RB_ARY_CLEAR
+ rb_define_method(cls, "rb_ary_clear", array_spec_rb_ary_clear, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_DELETE
+ rb_define_method(cls, "rb_ary_delete", array_spec_rb_ary_delete, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_DELETE_AT
+ rb_define_method(cls, "rb_ary_delete_at", array_spec_rb_ary_delete_at, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_DUP
+ rb_define_method(cls, "rb_ary_dup", array_spec_rb_ary_dup, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_ENTRY
+ rb_define_method(cls, "rb_ary_entry", array_spec_rb_ary_entry, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_INCLUDES
+ rb_define_method(cls, "rb_ary_includes", array_spec_rb_ary_includes, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_JOIN
+ rb_define_method(cls, "rb_ary_join", array_spec_rb_ary_join, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_TO_S
+ rb_define_method(cls, "rb_ary_to_s", array_spec_rb_ary_to_s, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW
+ rb_define_method(cls, "rb_ary_new", array_spec_rb_ary_new, 0);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW2
+ rb_define_method(cls, "rb_ary_new2", array_spec_rb_ary_new2, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW_CAPA
+ rb_define_method(cls, "rb_ary_new_capa", array_spec_rb_ary_new_capa, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW3
+ rb_define_method(cls, "rb_ary_new3", array_spec_rb_ary_new3, 3);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW_FROM_ARGS
+ rb_define_method(cls, "rb_ary_new_from_args", array_spec_rb_ary_new_from_args, 3);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW4
+ rb_define_method(cls, "rb_ary_new4", array_spec_rb_ary_new4, 3);
+#endif
+
+#ifdef HAVE_RB_ARY_NEW_FROM_VALUES
+ rb_define_method(cls, "rb_ary_new_from_values", array_spec_rb_ary_new_from_values, 3);
+#endif
+
+#ifdef HAVE_RB_ARY_POP
+ rb_define_method(cls, "rb_ary_pop", array_spec_rb_ary_pop, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_PUSH
+ rb_define_method(cls, "rb_ary_push", array_spec_rb_ary_push, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_CAT
+ rb_define_method(cls, "rb_ary_cat", array_spec_rb_ary_cat, -1);
+#endif
+
+#ifdef HAVE_RB_ARY_REVERSE
+ rb_define_method(cls, "rb_ary_reverse", array_spec_rb_ary_reverse, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_ROTATE
+ rb_define_method(cls, "rb_ary_rotate", array_spec_rb_ary_rotate, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_SHIFT
+ rb_define_method(cls, "rb_ary_shift", array_spec_rb_ary_shift, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_STORE
+ rb_define_method(cls, "rb_ary_store", array_spec_rb_ary_store, 3);
+#endif
+
+#ifdef HAVE_RB_ARY_CONCAT
+ rb_define_method(cls, "rb_ary_concat", array_spec_rb_ary_concat, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_PLUS
+ rb_define_method(cls, "rb_ary_plus", array_spec_rb_ary_plus, 2);
+#endif
+
+#ifdef HAVE_RB_ARY_UNSHIFT
+ rb_define_method(cls, "rb_ary_unshift", array_spec_rb_ary_unshift, 2);
+#endif
+
+#ifdef HAVE_RB_ASSOC_NEW
+ rb_define_method(cls, "rb_assoc_new", array_spec_rb_assoc_new, 2);
+#endif
+
+#if defined(HAVE_RB_ITERATE) && defined(HAVE_RB_EACH)
+ rb_define_method(cls, "rb_iterate", array_spec_rb_iterate, 1);
+ rb_define_method(cls, "rb_iterate_each_pair", array_spec_rb_iterate_each_pair, 1);
+ rb_define_method(cls, "rb_iterate_then_yield", array_spec_rb_iterate_then_yield, 1);
+#endif
+
+#if defined(HAVE_RB_MEM_CLEAR)
+ rb_define_method(cls, "rb_mem_clear", array_spec_rb_mem_clear, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_FREEZE
+ rb_define_method(cls, "rb_ary_freeze", array_spec_rb_ary_freeze, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_TO_ARY
+ rb_define_method(cls, "rb_ary_to_ary", array_spec_rb_ary_to_ary, 1);
+#endif
+
+#ifdef HAVE_RB_ARY_SUBSEQ
+ rb_define_method(cls, "rb_ary_subseq", array_spec_rb_ary_subseq, 3);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/bignum_spec.c b/spec/ruby/optional/capi/ext/bignum_spec.c
new file mode 100644
index 0000000000..ab3b36eadc
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/bignum_spec.c
@@ -0,0 +1,149 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_BIG2DBL
+static VALUE bignum_spec_rb_big2dbl(VALUE self, VALUE num) {
+ return rb_float_new(rb_big2dbl(num));
+}
+#endif
+
+#ifdef HAVE_RB_DBL2BIG
+static VALUE bignum_spec_rb_dbl2big(VALUE self, VALUE num) {
+ double dnum = NUM2DBL(num);
+
+ return rb_dbl2big(dnum);
+}
+#endif
+
+#ifdef HAVE_RB_BIG2LL
+static VALUE bignum_spec_rb_big2ll(VALUE self, VALUE num) {
+ return rb_ll2inum(rb_big2ll(num));
+}
+#endif
+
+#ifdef HAVE_RB_BIG2LONG
+static VALUE bignum_spec_rb_big2long(VALUE self, VALUE num) {
+ return LONG2NUM(rb_big2long(num));
+}
+#endif
+
+#ifdef HAVE_RB_BIG2STR
+static VALUE bignum_spec_rb_big2str(VALUE self, VALUE num, VALUE base) {
+ return rb_big2str(num, FIX2INT(base));
+}
+#endif
+
+#ifdef HAVE_RB_BIG2ULONG
+static VALUE bignum_spec_rb_big2ulong(VALUE self, VALUE num) {
+ return ULONG2NUM(rb_big2ulong(num));
+}
+#endif
+
+#ifdef HAVE_RB_BIG_CMP
+static VALUE bignum_spec_rb_big_cmp(VALUE self, VALUE x, VALUE y) {
+ return rb_big_cmp(x, y);
+}
+#endif
+
+#ifdef HAVE_RB_BIG_PACK
+static VALUE bignum_spec_rb_big_pack(VALUE self, VALUE val) {
+ unsigned long buff;
+
+ rb_big_pack(val, &buff, 1);
+
+ return ULONG2NUM(buff);
+}
+#endif
+
+#if HAVE_ABSINT_SIZE
+static VALUE bignum_spec_rb_big_pack_length(VALUE self, VALUE val) {
+ long long_len;
+ int leading_bits = 0;
+ int divisor = SIZEOF_LONG;
+ size_t len = rb_absint_size(val, &leading_bits);
+ if (leading_bits == 0) {
+ len += 1;
+ }
+
+ long_len = len / divisor + ((len % divisor == 0) ? 0 : 1);
+ return LONG2NUM(long_len);
+}
+#endif
+
+#ifdef HAVE_RB_BIG_PACK
+static VALUE bignum_spec_rb_big_pack_array(VALUE self, VALUE val, VALUE len) {
+ int i;
+ long long_len = NUM2LONG(len);
+
+ VALUE ary = rb_ary_new_capa(long_len);
+ unsigned long *buf = malloc(long_len * SIZEOF_LONG);
+
+ /* The array should be filled with recognisable junk so we can check
+ it is all cleared properly. */
+
+ for (i = 0; i < long_len; i++) {
+#if SIZEOF_LONG == 8
+ buf[i] = 0xfedcba9876543210L;
+#else
+ buf[i] = 0xfedcba98L;
+#endif
+ }
+
+ rb_big_pack(val, buf, long_len);
+ for (i = 0; i < long_len; i++) {
+ rb_ary_store(ary, i, ULONG2NUM(buf[i]));
+ }
+ free(buf);
+ return ary;
+}
+#endif
+
+void Init_bignum_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiBignumSpecs", rb_cObject);
+
+#ifdef HAVE_RB_BIG2DBL
+ rb_define_method(cls, "rb_big2dbl", bignum_spec_rb_big2dbl, 1);
+#endif
+
+#ifdef HAVE_RB_DBL2BIG
+ rb_define_method(cls, "rb_dbl2big", bignum_spec_rb_dbl2big, 1);
+#endif
+
+#ifdef HAVE_RB_BIG2LL
+ rb_define_method(cls, "rb_big2ll", bignum_spec_rb_big2ll, 1);
+#endif
+
+#ifdef HAVE_RB_BIG2LONG
+ rb_define_method(cls, "rb_big2long", bignum_spec_rb_big2long, 1);
+#endif
+
+#ifdef HAVE_RB_BIG2STR
+ rb_define_method(cls, "rb_big2str", bignum_spec_rb_big2str, 2);
+#endif
+
+#ifdef HAVE_RB_BIG2ULONG
+ rb_define_method(cls, "rb_big2ulong", bignum_spec_rb_big2ulong, 1);
+#endif
+
+#ifdef HAVE_RB_BIG_CMP
+ rb_define_method(cls, "rb_big_cmp", bignum_spec_rb_big_cmp, 2);
+#endif
+
+#ifdef HAVE_RB_BIG_PACK
+ rb_define_method(cls, "rb_big_pack", bignum_spec_rb_big_pack, 1);
+ rb_define_method(cls, "rb_big_pack_array", bignum_spec_rb_big_pack_array, 2);
+#endif
+
+#ifdef HAVE_ABSINT_SIZE
+ rb_define_method(cls, "rb_big_pack_length", bignum_spec_rb_big_pack_length, 1);
+#endif
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
diff --git a/spec/ruby/optional/capi/ext/boolean_spec.c b/spec/ruby/optional/capi/ext/boolean_spec.c
new file mode 100644
index 0000000000..94d79c0fc0
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/boolean_spec.c
@@ -0,0 +1,34 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static VALUE boolean_spec_is_true(VALUE self, VALUE boolean) {
+ if (boolean) {
+ return INT2NUM(1);
+ } else {
+ return INT2NUM(2);
+ }
+}
+
+static VALUE boolean_spec_q_true(VALUE self) {
+ return Qtrue;
+}
+
+static VALUE boolean_spec_q_false(VALUE self) {
+ return Qfalse;
+}
+
+void Init_boolean_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiBooleanSpecs", rb_cObject);
+ rb_define_method(cls, "is_true", boolean_spec_is_true, 1);
+ rb_define_method(cls, "q_true", boolean_spec_q_true, 0);
+ rb_define_method(cls, "q_false", boolean_spec_q_false, 0);
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
diff --git a/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c b/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c
new file mode 100644
index 0000000000..64393a9397
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c
@@ -0,0 +1,5 @@
+#include "ruby.h"
+
+void Init_class_id_under_autoload_spec(void) {
+ rb_define_class_id_under(rb_cObject, rb_intern("ClassIdUnderAutoload"), rb_cObject);
+}
diff --git a/spec/ruby/optional/capi/ext/class_spec.c b/spec/ruby/optional/capi/ext/class_spec.c
new file mode 100644
index 0000000000..e3860df1da
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/class_spec.c
@@ -0,0 +1,261 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_CALL_SUPER
+static VALUE class_spec_call_super_method(VALUE self) {
+ return rb_call_super(0, 0);
+}
+
+static VALUE class_spec_define_call_super_method(VALUE self, VALUE obj, VALUE str_name) {
+ rb_define_method(obj, RSTRING_PTR(str_name), class_spec_call_super_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_PATH
+static VALUE class_spec_rb_class_path(VALUE self, VALUE klass) {
+ return rb_class_path(klass);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_NAME
+static VALUE class_spec_rb_class_name(VALUE self, VALUE klass) {
+ return rb_class_name(klass);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS2NAME
+static VALUE class_spec_rb_class2name(VALUE self, VALUE klass) {
+ return rb_str_new2( rb_class2name(klass) );
+}
+#endif
+
+#ifdef HAVE_RB_PATH2CLASS
+static VALUE class_spec_rb_path2class(VALUE self, VALUE path) {
+ return rb_path2class(RSTRING_PTR(path));
+}
+#endif
+
+#ifdef HAVE_RB_PATH_TO_CLASS
+static VALUE class_spec_rb_path_to_class(VALUE self, VALUE path) {
+ return rb_path_to_class(path);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_NEW
+static VALUE class_spec_rb_class_new(VALUE self, VALUE super) {
+ return rb_class_new(super);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_NEW_INSTANCE
+static VALUE class_spec_rb_class_new_instance(VALUE self,
+ VALUE nargs, VALUE args,
+ VALUE klass) {
+ int c_nargs = FIX2INT(nargs);
+ VALUE *c_args = alloca(sizeof(VALUE) * c_nargs);
+ int i;
+
+ for (i = 0; i < c_nargs; i++)
+ c_args[i] = rb_ary_entry(args, i);
+
+ return rb_class_new_instance(c_nargs, c_args, klass);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_REAL
+static VALUE class_spec_rb_class_real(VALUE self, VALUE object) {
+ if(rb_type_p(object, T_FIXNUM)) {
+ return INT2FIX(rb_class_real(FIX2INT(object)));
+ } else {
+ return rb_class_real(CLASS_OF(object));
+ }
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_SUPERCLASS
+static VALUE class_spec_rb_class_superclass(VALUE self, VALUE klass) {
+ return rb_class_superclass(klass);
+}
+#endif
+
+#ifdef HAVE_RB_CVAR_DEFINED
+static VALUE class_spec_cvar_defined(VALUE self, VALUE klass, VALUE id) {
+ ID as_id = rb_intern(StringValuePtr(id));
+ return rb_cvar_defined(klass, as_id);
+}
+#endif
+
+#ifdef HAVE_RB_CVAR_GET
+static VALUE class_spec_cvar_get(VALUE self, VALUE klass, VALUE name) {
+ return rb_cvar_get(klass, rb_intern(StringValuePtr(name)));
+}
+#endif
+
+#ifdef HAVE_RB_CVAR_SET
+static VALUE class_spec_cvar_set(VALUE self, VALUE klass, VALUE name, VALUE val) {
+ rb_cvar_set(klass, rb_intern(StringValuePtr(name)), val);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_CV_GET
+static VALUE class_spec_cv_get(VALUE self, VALUE klass, VALUE name) {
+ return rb_cv_get(klass, StringValuePtr(name));
+}
+#endif
+
+#ifdef HAVE_RB_CV_SET
+static VALUE class_spec_cv_set(VALUE self, VALUE klass, VALUE name, VALUE val) {
+ rb_cv_set(klass, StringValuePtr(name), val);
+
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_ATTR
+VALUE class_spec_define_attr(VALUE self, VALUE klass, VALUE sym, VALUE read, VALUE write) {
+ int int_read, int_write;
+ int_read = read == Qtrue ? 1 : 0;
+ int_write = write == Qtrue ? 1 : 0;
+ rb_define_attr(klass, rb_id2name(SYM2ID(sym)), int_read, int_write);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS
+static VALUE class_spec_rb_define_class(VALUE self, VALUE name, VALUE super) {
+ if(NIL_P(super)) super = 0;
+ return rb_define_class(RSTRING_PTR(name), super);
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS_UNDER
+static VALUE class_spec_rb_define_class_under(VALUE self, VALUE outer,
+ VALUE name, VALUE super) {
+ if(NIL_P(super)) super = 0;
+ return rb_define_class_under(outer, RSTRING_PTR(name), super);
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS_ID_UNDER
+static VALUE class_spec_rb_define_class_id_under(VALUE self, VALUE outer,
+ VALUE name, VALUE super) {
+ if(NIL_P(super)) super = 0;
+ return rb_define_class_id_under(outer, SYM2ID(name), super);
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS_VARIABLE
+static VALUE class_spec_define_class_variable(VALUE self, VALUE klass, VALUE name, VALUE val) {
+ rb_define_class_variable(klass, StringValuePtr(name), val);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_INCLUDE_MODULE
+static VALUE class_spec_include_module(VALUE self, VALUE klass, VALUE module) {
+ rb_include_module(klass, module);
+ return klass;
+}
+#endif
+
+void Init_class_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiClassSpecs", rb_cObject);
+
+#ifdef HAVE_RB_CALL_SUPER
+ rb_define_method(cls, "define_call_super_method", class_spec_define_call_super_method, 2);
+#endif
+
+#ifdef HAVE_RB_CLASS_PATH
+ rb_define_method(cls, "rb_class_path", class_spec_rb_class_path, 1);
+#endif
+
+#ifdef HAVE_RB_CLASS_NAME
+ rb_define_method(cls, "rb_class_name", class_spec_rb_class_name, 1);
+#endif
+
+#ifdef HAVE_RB_CLASS2NAME
+ rb_define_method(cls, "rb_class2name", class_spec_rb_class2name, 1);
+#endif
+
+#ifdef HAVE_RB_PATH2CLASS
+ rb_define_method(cls, "rb_path2class", class_spec_rb_path2class, 1);
+#endif
+
+#ifdef HAVE_RB_PATH_TO_CLASS
+ rb_define_method(cls, "rb_path_to_class", class_spec_rb_path_to_class, 1);
+#endif
+
+#ifdef HAVE_RB_CLASS_NEW
+ rb_define_method(cls, "rb_class_new", class_spec_rb_class_new, 1);
+#endif
+
+#ifdef HAVE_RB_CLASS_NEW_INSTANCE
+ rb_define_method(cls, "rb_class_new_instance", class_spec_rb_class_new_instance, 3);
+#endif
+
+#ifdef HAVE_RB_CLASS_REAL
+ rb_define_method(cls, "rb_class_real", class_spec_rb_class_real, 1);
+#endif
+
+#ifdef HAVE_RB_CLASS_SUPERCLASS
+ rb_define_method(cls, "rb_class_superclass", class_spec_rb_class_superclass, 1);
+#endif
+
+#ifdef HAVE_RB_CVAR_DEFINED
+ rb_define_method(cls, "rb_cvar_defined", class_spec_cvar_defined, 2);
+#endif
+
+#ifdef HAVE_RB_CVAR_GET
+ rb_define_method(cls, "rb_cvar_get", class_spec_cvar_get, 2);
+#endif
+
+#ifdef HAVE_RB_CVAR_SET
+ rb_define_method(cls, "rb_cvar_set", class_spec_cvar_set, 3);
+#endif
+
+#ifdef HAVE_RB_CV_GET
+ rb_define_method(cls, "rb_cv_get", class_spec_cv_get, 2);
+#endif
+
+#ifdef HAVE_RB_CV_SET
+ rb_define_method(cls, "rb_cv_set", class_spec_cv_set, 3);
+#endif
+
+#ifdef HAVE_RB_DEFINE_ATTR
+ rb_define_method(cls, "rb_define_attr", class_spec_define_attr, 4);
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS
+ rb_define_method(cls, "rb_define_class", class_spec_rb_define_class, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS_UNDER
+ rb_define_method(cls, "rb_define_class_under", class_spec_rb_define_class_under, 3);
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS_ID_UNDER
+ rb_define_method(cls, "rb_define_class_id_under", class_spec_rb_define_class_id_under, 3);
+#endif
+
+#ifdef HAVE_RB_DEFINE_CLASS_VARIABLE
+ rb_define_method(cls, "rb_define_class_variable", class_spec_define_class_variable, 3);
+#endif
+
+#ifdef HAVE_RB_INCLUDE_MODULE
+ rb_define_method(cls, "rb_include_module", class_spec_include_module, 2);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/class_under_autoload_spec.c b/spec/ruby/optional/capi/ext/class_under_autoload_spec.c
new file mode 100644
index 0000000000..120dec7327
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/class_under_autoload_spec.c
@@ -0,0 +1,5 @@
+#include "ruby.h"
+
+void Init_class_under_autoload_spec(void) {
+ rb_define_class_under(rb_cObject, "ClassUnderAutoload", rb_cObject);
+}
diff --git a/spec/ruby/optional/capi/ext/complex_spec.c b/spec/ruby/optional/capi/ext/complex_spec.c
new file mode 100644
index 0000000000..476bbce31c
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/complex_spec.c
@@ -0,0 +1,76 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_COMPLEX
+static VALUE complex_spec_rb_Complex(VALUE self, VALUE num, VALUE den) {
+ return rb_Complex(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_COMPLEX1
+static VALUE complex_spec_rb_Complex1(VALUE self, VALUE num) {
+ return rb_Complex1(num);
+}
+#endif
+
+#ifdef HAVE_RB_COMPLEX2
+static VALUE complex_spec_rb_Complex2(VALUE self, VALUE num, VALUE den) {
+ return rb_Complex2(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_COMPLEX_NEW
+static VALUE complex_spec_rb_complex_new(VALUE self, VALUE num, VALUE den) {
+ return rb_complex_new(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_COMPLEX_NEW1
+static VALUE complex_spec_rb_complex_new1(VALUE self, VALUE num) {
+ return rb_complex_new1(num);
+}
+#endif
+
+#ifdef HAVE_RB_COMPLEX_NEW2
+static VALUE complex_spec_rb_complex_new2(VALUE self, VALUE num, VALUE den) {
+ return rb_complex_new2(num, den);
+}
+#endif
+
+void Init_complex_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiComplexSpecs", rb_cObject);
+
+#ifdef HAVE_RB_COMPLEX
+ rb_define_method(cls, "rb_Complex", complex_spec_rb_Complex, 2);
+#endif
+
+#ifdef HAVE_RB_COMPLEX1
+ rb_define_method(cls, "rb_Complex1", complex_spec_rb_Complex1, 1);
+#endif
+
+#ifdef HAVE_RB_COMPLEX2
+ rb_define_method(cls, "rb_Complex2", complex_spec_rb_Complex2, 2);
+#endif
+
+#ifdef HAVE_RB_COMPLEX_NEW
+ rb_define_method(cls, "rb_complex_new", complex_spec_rb_complex_new, 2);
+#endif
+
+#ifdef HAVE_RB_COMPLEX_NEW1
+ rb_define_method(cls, "rb_complex_new1", complex_spec_rb_complex_new1, 1);
+#endif
+
+#ifdef HAVE_RB_COMPLEX_NEW2
+ rb_define_method(cls, "rb_complex_new2", complex_spec_rb_complex_new2, 2);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/spec/ruby/optional/capi/ext/constants_spec.c b/spec/ruby/optional/capi/ext/constants_spec.c
new file mode 100644
index 0000000000..7751b12224
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/constants_spec.c
@@ -0,0 +1,646 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_CARRAY
+static VALUE constants_spec_rb_cArray(VALUE self) {
+ return rb_cArray;
+}
+#endif
+
+#ifdef HAVE_RB_CBIGNUM
+static VALUE constants_spec_rb_cBignum(VALUE self) {
+ return rb_cBignum;
+}
+#endif
+
+#ifdef HAVE_RB_CCLASS
+static VALUE constants_spec_rb_cClass(VALUE self) {
+ return rb_cClass;
+}
+#endif
+
+#ifdef HAVE_RB_CDATA
+static VALUE constants_spec_rb_cData(VALUE self) {
+ return rb_cData;
+}
+#endif
+
+#ifdef HAVE_RB_CFALSECLASS
+static VALUE constants_spec_rb_cFalseClass(VALUE self) {
+ return rb_cFalseClass;
+}
+#endif
+
+#ifdef HAVE_RB_CFILE
+static VALUE constants_spec_rb_cFile(VALUE self) {
+ return rb_cFile;
+}
+#endif
+
+#ifdef HAVE_RB_CFIXNUM
+static VALUE constants_spec_rb_cFixnum(VALUE self) {
+ return rb_cFixnum;
+}
+#endif
+
+#ifdef HAVE_RB_CFLOAT
+static VALUE constants_spec_rb_cFloat(VALUE self) {
+ return rb_cFloat;
+}
+#endif
+
+#ifdef HAVE_RB_CHASH
+static VALUE constants_spec_rb_cHash(VALUE self) {
+ return rb_cHash;
+}
+#endif
+
+#ifdef HAVE_RB_CINTEGER
+static VALUE constants_spec_rb_cInteger(VALUE self) {
+ return rb_cInteger;
+}
+#endif
+
+#ifdef HAVE_RB_CIO
+static VALUE constants_spec_rb_cIO(VALUE self) {
+ return rb_cIO;
+}
+#endif
+
+#ifdef HAVE_RB_CMODULE
+static VALUE constants_spec_rb_cModule(VALUE self) {
+ return rb_cModule;
+}
+#endif
+
+#ifdef HAVE_RB_CMATCH
+static VALUE constants_spec_rb_cMatch(VALUE self) {
+ return rb_cMatch;
+}
+#endif
+
+#ifdef HAVE_RB_CNILCLASS
+static VALUE constants_spec_rb_cNilClass(VALUE self) {
+ return rb_cNilClass;
+}
+#endif
+
+#ifdef HAVE_RB_CNUMERIC
+static VALUE constants_spec_rb_cNumeric(VALUE self) {
+ return rb_cNumeric;
+}
+#endif
+
+#ifdef HAVE_RB_COBJECT
+static VALUE constants_spec_rb_cObject(VALUE self) {
+ return rb_cObject;
+}
+#endif
+
+#ifdef HAVE_RB_CRANGE
+static VALUE constants_spec_rb_cRange(VALUE self) {
+ return rb_cRange;
+}
+#endif
+
+#ifdef HAVE_RB_CREGEXP
+static VALUE constants_spec_rb_cRegexp(VALUE self) {
+ return rb_cRegexp;
+}
+#endif
+
+#ifdef HAVE_RB_CSTRING
+static VALUE constants_spec_rb_cString(VALUE self) {
+ return rb_cString;
+}
+#endif
+
+#ifdef HAVE_RB_CSTRUCT
+static VALUE constants_spec_rb_cStruct(VALUE self) {
+ return rb_cStruct;
+}
+#endif
+
+#ifdef HAVE_RB_CSYMBOL
+static VALUE constants_spec_rb_cSymbol(VALUE self) {
+ return rb_cSymbol;
+}
+#endif
+
+#ifdef HAVE_RB_CTIME
+static VALUE constants_spec_rb_cTime(VALUE self) {
+ return rb_cTime;
+}
+#endif
+
+#ifdef HAVE_RB_CTHREAD
+static VALUE constants_spec_rb_cThread(VALUE self) {
+ return rb_cThread;
+}
+#endif
+
+#ifdef HAVE_RB_CTRUECLASS
+static VALUE constants_spec_rb_cTrueClass(VALUE self) {
+ return rb_cTrueClass;
+}
+#endif
+
+#ifdef HAVE_RB_CPROC
+static VALUE constants_spec_rb_cProc(VALUE self) {
+ return rb_cProc;
+}
+#endif
+
+#ifdef HAVE_RB_CMETHOD
+static VALUE constants_spec_rb_cMethod(VALUE self) {
+ return rb_cMethod;
+}
+#endif
+
+#ifdef HAVE_RB_CENUMERATOR
+static VALUE constants_spec_rb_cEnumerator(VALUE self) {
+ return rb_cEnumerator;
+}
+#endif
+
+#ifdef HAVE_RB_MCOMPARABLE
+static VALUE constants_spec_rb_mComparable(VALUE self) {
+ return rb_mComparable;
+}
+#endif
+
+#ifdef HAVE_RB_MENUMERABLE
+static VALUE constants_spec_rb_mEnumerable(VALUE self) {
+ return rb_mEnumerable;
+}
+#endif
+
+#ifdef HAVE_RB_MKERNEL
+static VALUE constants_spec_rb_mKernel(VALUE self) {
+ return rb_mKernel;
+}
+#endif
+
+#ifdef HAVE_RB_EARGERROR
+static VALUE constants_spec_rb_eArgError(VALUE self) {
+ return rb_eArgError;
+}
+#endif
+
+#ifdef HAVE_RB_EEOFERROR
+static VALUE constants_spec_rb_eEOFError(VALUE self) {
+ return rb_eEOFError;
+}
+#endif
+
+#ifdef HAVE_RB_MERRNO
+static VALUE constants_spec_rb_mErrno(VALUE self) {
+ return rb_mErrno;
+}
+#endif
+
+#ifdef HAVE_RB_EEXCEPTION
+static VALUE constants_spec_rb_eException(VALUE self) {
+ return rb_eException;
+}
+#endif
+
+#ifdef HAVE_RB_EFLOATDOMAINERROR
+static VALUE constants_spec_rb_eFloatDomainError(VALUE self) {
+ return rb_eFloatDomainError;
+}
+#endif
+
+#ifdef HAVE_RB_EINDEXERROR
+static VALUE constants_spec_rb_eIndexError(VALUE self) {
+ return rb_eIndexError;
+}
+#endif
+
+#ifdef HAVE_RB_EINTERRUPT
+static VALUE constants_spec_rb_eInterrupt(VALUE self) {
+ return rb_eInterrupt;
+}
+#endif
+
+#ifdef HAVE_RB_EIOERROR
+static VALUE constants_spec_rb_eIOError(VALUE self) {
+ return rb_eIOError;
+}
+#endif
+
+#ifdef HAVE_RB_ELOADERROR
+static VALUE constants_spec_rb_eLoadError(VALUE self) {
+ return rb_eLoadError;
+}
+#endif
+
+#ifdef HAVE_RB_ELOCALJUMPERROR
+static VALUE constants_spec_rb_eLocalJumpError(VALUE self) {
+ return rb_eLocalJumpError;
+}
+#endif
+
+#ifdef HAVE_RB_ENAMEERROR
+static VALUE constants_spec_rb_eNameError(VALUE self) {
+ return rb_eNameError;
+}
+#endif
+
+#ifdef HAVE_RB_ENOMEMERROR
+static VALUE constants_spec_rb_eNoMemError(VALUE self) {
+ return rb_eNoMemError;
+}
+#endif
+
+#ifdef HAVE_RB_ENOMETHODERROR
+static VALUE constants_spec_rb_eNoMethodError(VALUE self) {
+ return rb_eNoMethodError;
+}
+#endif
+
+#ifdef HAVE_RB_ENOTIMPERROR
+static VALUE constants_spec_rb_eNotImpError(VALUE self) {
+ return rb_eNotImpError;
+}
+#endif
+
+#ifdef HAVE_RB_ERANGEERROR
+static VALUE constants_spec_rb_eRangeError(VALUE self) {
+ return rb_eRangeError;
+}
+#endif
+
+#ifdef HAVE_RB_EREGEXPERROR
+static VALUE constants_spec_rb_eRegexpError(VALUE self) {
+ return rb_eRegexpError;
+}
+#endif
+
+#ifdef HAVE_RB_ERUNTIMEERROR
+static VALUE constants_spec_rb_eRuntimeError(VALUE self) {
+ return rb_eRuntimeError;
+}
+#endif
+
+#ifdef HAVE_RB_ESCRIPTERROR
+static VALUE constants_spec_rb_eScriptError(VALUE self) {
+ return rb_eScriptError;
+}
+#endif
+
+#ifdef HAVE_RB_ESECURITYERROR
+static VALUE constants_spec_rb_eSecurityError(VALUE self) {
+ return rb_eSecurityError;
+}
+#endif
+
+#ifdef HAVE_RB_ESIGNAL
+static VALUE constants_spec_rb_eSignal(VALUE self) {
+ return rb_eSignal;
+}
+#endif
+
+#ifdef HAVE_RB_ESTANDARDERROR
+static VALUE constants_spec_rb_eStandardError(VALUE self) {
+ return rb_eStandardError;
+}
+#endif
+
+#ifdef HAVE_RB_ESYNTAXERROR
+static VALUE constants_spec_rb_eSyntaxError(VALUE self) {
+ return rb_eSyntaxError;
+}
+#endif
+
+#ifdef HAVE_RB_ESYSTEMCALLERROR
+static VALUE constants_spec_rb_eSystemCallError(VALUE self) {
+ return rb_eSystemCallError;
+}
+#endif
+
+#ifdef HAVE_RB_ESYSTEMEXIT
+static VALUE constants_spec_rb_eSystemExit(VALUE self) {
+ return rb_eSystemExit;
+}
+#endif
+
+#ifdef HAVE_RB_ESYSSTACKERROR
+static VALUE constants_spec_rb_eSysStackError(VALUE self) {
+ return rb_eSysStackError;
+}
+#endif
+
+#ifdef HAVE_RB_ETYPEERROR
+static VALUE constants_spec_rb_eTypeError(VALUE self) {
+ return rb_eTypeError;
+}
+#endif
+
+#ifdef HAVE_RB_ETHREADERROR
+static VALUE constants_spec_rb_eThreadError(VALUE self) {
+ return rb_eThreadError;
+}
+#endif
+
+#ifdef HAVE_RB_EZERODIVERROR
+static VALUE constants_spec_rb_eZeroDivError(VALUE self) {
+ return rb_eZeroDivError;
+}
+#endif
+
+#ifdef HAVE_RB_EMATHDOMAINERROR
+static VALUE constants_spec_rb_eMathDomainError(VALUE self) {
+ return rb_eMathDomainError;
+}
+#endif
+
+#ifdef HAVE_RB_EENCCOMPATERROR
+static VALUE constants_spec_rb_eEncCompatError(VALUE self) {
+ return rb_eEncCompatError;
+}
+#endif
+
+#ifdef HAVE_RB_MWAITREADABLE
+static VALUE constants_spec_rb_mWaitReadable(VALUE self) {
+ return rb_mWaitReadable;
+}
+#endif
+
+#ifdef HAVE_RB_MWAITWRITABLE
+static VALUE constants_spec_rb_mWaitWritable(VALUE self) {
+ return rb_mWaitWritable;
+}
+#endif
+
+#ifdef HAVE_RB_CDIR
+static VALUE constants_spec_rb_cDir(VALUE self) {
+ return rb_cDir;
+}
+#endif
+
+void Init_constants_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiConstantsSpecs", rb_cObject);
+
+#ifdef HAVE_RB_CARRAY
+ rb_define_method(cls, "rb_cArray", constants_spec_rb_cArray, 0);
+#endif
+
+#ifdef HAVE_RB_CBIGNUM
+ rb_define_method(cls, "rb_cBignum", constants_spec_rb_cBignum, 0);
+#endif
+
+#ifdef HAVE_RB_CCLASS
+ rb_define_method(cls, "rb_cClass", constants_spec_rb_cClass, 0);
+#endif
+
+#ifdef HAVE_RB_CDATA
+ rb_define_method(cls, "rb_cData", constants_spec_rb_cData, 0);
+#endif
+
+#ifdef HAVE_RB_CFALSECLASS
+ rb_define_method(cls, "rb_cFalseClass", constants_spec_rb_cFalseClass, 0);
+#endif
+
+#ifdef HAVE_RB_CFILE
+ rb_define_method(cls, "rb_cFile", constants_spec_rb_cFile, 0);
+#endif
+
+#ifdef HAVE_RB_CFIXNUM
+ rb_define_method(cls, "rb_cFixnum", constants_spec_rb_cFixnum, 0);
+#endif
+
+#ifdef HAVE_RB_CFLOAT
+ rb_define_method(cls, "rb_cFloat", constants_spec_rb_cFloat, 0);
+#endif
+
+#ifdef HAVE_RB_CHASH
+ rb_define_method(cls, "rb_cHash", constants_spec_rb_cHash, 0);
+#endif
+
+#ifdef HAVE_RB_CINTEGER
+ rb_define_method(cls, "rb_cInteger", constants_spec_rb_cInteger, 0);
+#endif
+
+#ifdef HAVE_RB_CIO
+ rb_define_method(cls, "rb_cIO", constants_spec_rb_cIO, 0);
+#endif
+
+#ifdef HAVE_RB_CMATCH
+ rb_define_method(cls, "rb_cMatch", constants_spec_rb_cMatch, 0);
+#endif
+
+#ifdef HAVE_RB_CMODULE
+ rb_define_method(cls, "rb_cModule", constants_spec_rb_cModule, 0);
+#endif
+
+#ifdef HAVE_RB_CNILCLASS
+ rb_define_method(cls, "rb_cNilClass", constants_spec_rb_cNilClass, 0);
+#endif
+
+#ifdef HAVE_RB_CNUMERIC
+ rb_define_method(cls, "rb_cNumeric", constants_spec_rb_cNumeric, 0);
+#endif
+
+#ifdef HAVE_RB_COBJECT
+ rb_define_method(cls, "rb_cObject", constants_spec_rb_cObject, 0);
+#endif
+
+#ifdef HAVE_RB_CRANGE
+ rb_define_method(cls, "rb_cRange", constants_spec_rb_cRange, 0);
+#endif
+
+#ifdef HAVE_RB_CREGEXP
+ rb_define_method(cls, "rb_cRegexp", constants_spec_rb_cRegexp, 0);
+#endif
+
+#ifdef HAVE_RB_CSTRING
+ rb_define_method(cls, "rb_cString", constants_spec_rb_cString, 0);
+#endif
+
+#ifdef HAVE_RB_CSTRUCT
+ rb_define_method(cls, "rb_cStruct", constants_spec_rb_cStruct, 0);
+#endif
+
+#ifdef HAVE_RB_CSYMBOL
+ rb_define_method(cls, "rb_cSymbol", constants_spec_rb_cSymbol, 0);
+#endif
+
+#ifdef HAVE_RB_CTIME
+ rb_define_method(cls, "rb_cTime", constants_spec_rb_cTime, 0);
+#endif
+
+#ifdef HAVE_RB_CTHREAD
+ rb_define_method(cls, "rb_cThread", constants_spec_rb_cThread, 0);
+#endif
+
+#ifdef HAVE_RB_CTRUECLASS
+ rb_define_method(cls, "rb_cTrueClass", constants_spec_rb_cTrueClass, 0);
+#endif
+
+#ifdef HAVE_RB_CPROC
+ rb_define_method(cls, "rb_cProc", constants_spec_rb_cProc, 0);
+#endif
+
+#ifdef HAVE_RB_CMETHOD
+ rb_define_method(cls, "rb_cMethod", constants_spec_rb_cMethod, 0);
+#endif
+
+#ifdef HAVE_RB_CENUMERATOR
+ rb_define_method(cls, "rb_cEnumerator", constants_spec_rb_cEnumerator, 0);
+#endif
+
+#ifdef HAVE_RB_MCOMPARABLE
+ rb_define_method(cls, "rb_mComparable", constants_spec_rb_mComparable, 0);
+#endif
+
+#ifdef HAVE_RB_MENUMERABLE
+ rb_define_method(cls, "rb_mEnumerable", constants_spec_rb_mEnumerable, 0);
+#endif
+
+#ifdef HAVE_RB_MKERNEL
+ rb_define_method(cls, "rb_mKernel", constants_spec_rb_mKernel, 0);
+#endif
+
+#ifdef HAVE_RB_EARGERROR
+ rb_define_method(cls, "rb_eArgError", constants_spec_rb_eArgError, 0);
+#endif
+
+#ifdef HAVE_RB_EEOFERROR
+ rb_define_method(cls, "rb_eEOFError", constants_spec_rb_eEOFError, 0);
+#endif
+
+#ifdef HAVE_RB_MERRNO
+ rb_define_method(cls, "rb_mErrno", constants_spec_rb_mErrno, 0);
+#endif
+
+#ifdef HAVE_RB_EEXCEPTION
+ rb_define_method(cls, "rb_eException", constants_spec_rb_eException, 0);
+#endif
+
+#ifdef HAVE_RB_EFLOATDOMAINERROR
+ rb_define_method(cls, "rb_eFloatDomainError", constants_spec_rb_eFloatDomainError, 0);
+#endif
+
+#ifdef HAVE_RB_EINDEXERROR
+ rb_define_method(cls, "rb_eIndexError", constants_spec_rb_eIndexError, 0);
+#endif
+
+#ifdef HAVE_RB_EINTERRUPT
+ rb_define_method(cls, "rb_eInterrupt", constants_spec_rb_eInterrupt, 0);
+#endif
+
+#ifdef HAVE_RB_EIOERROR
+ rb_define_method(cls, "rb_eIOError", constants_spec_rb_eIOError, 0);
+#endif
+
+#ifdef HAVE_RB_ELOADERROR
+ rb_define_method(cls, "rb_eLoadError", constants_spec_rb_eLoadError, 0);
+#endif
+
+#ifdef HAVE_RB_ELOCALJUMPERROR
+ rb_define_method(cls, "rb_eLocalJumpError", constants_spec_rb_eLocalJumpError, 0);
+#endif
+
+#ifdef HAVE_RB_ENAMEERROR
+ rb_define_method(cls, "rb_eNameError", constants_spec_rb_eNameError, 0);
+#endif
+
+#ifdef HAVE_RB_ENOMEMERROR
+ rb_define_method(cls, "rb_eNoMemError", constants_spec_rb_eNoMemError, 0);
+#endif
+
+#ifdef HAVE_RB_ENOMETHODERROR
+ rb_define_method(cls, "rb_eNoMethodError", constants_spec_rb_eNoMethodError, 0);
+#endif
+
+#ifdef HAVE_RB_ENOTIMPERROR
+ rb_define_method(cls, "rb_eNotImpError", constants_spec_rb_eNotImpError, 0);
+#endif
+
+#ifdef HAVE_RB_ERANGEERROR
+ rb_define_method(cls, "rb_eRangeError", constants_spec_rb_eRangeError, 0);
+#endif
+
+#ifdef HAVE_RB_EREGEXPERROR
+ rb_define_method(cls, "rb_eRegexpError", constants_spec_rb_eRegexpError, 0);
+#endif
+
+#ifdef HAVE_RB_ERUNTIMEERROR
+ rb_define_method(cls, "rb_eRuntimeError", constants_spec_rb_eRuntimeError, 0);
+#endif
+
+#ifdef HAVE_RB_ESCRIPTERROR
+ rb_define_method(cls, "rb_eScriptError", constants_spec_rb_eScriptError, 0);
+#endif
+
+#ifdef HAVE_RB_ESECURITYERROR
+ rb_define_method(cls, "rb_eSecurityError", constants_spec_rb_eSecurityError, 0);
+#endif
+
+#ifdef HAVE_RB_ESIGNAL
+ rb_define_method(cls, "rb_eSignal", constants_spec_rb_eSignal, 0);
+#endif
+
+#ifdef HAVE_RB_ESTANDARDERROR
+ rb_define_method(cls, "rb_eStandardError", constants_spec_rb_eStandardError, 0);
+#endif
+
+#ifdef HAVE_RB_ESYNTAXERROR
+ rb_define_method(cls, "rb_eSyntaxError", constants_spec_rb_eSyntaxError, 0);
+#endif
+
+#ifdef HAVE_RB_ESYSTEMCALLERROR
+ rb_define_method(cls, "rb_eSystemCallError", constants_spec_rb_eSystemCallError, 0);
+#endif
+
+#ifdef HAVE_RB_ESYSTEMEXIT
+ rb_define_method(cls, "rb_eSystemExit", constants_spec_rb_eSystemExit, 0);
+#endif
+
+#ifdef HAVE_RB_ESYSSTACKERROR
+ rb_define_method(cls, "rb_eSysStackError", constants_spec_rb_eSysStackError, 0);
+#endif
+
+#ifdef HAVE_RB_ETYPEERROR
+ rb_define_method(cls, "rb_eTypeError", constants_spec_rb_eTypeError, 0);
+#endif
+
+#ifdef HAVE_RB_ETHREADERROR
+ rb_define_method(cls, "rb_eThreadError", constants_spec_rb_eThreadError, 0);
+#endif
+
+#ifdef HAVE_RB_EZERODIVERROR
+ rb_define_method(cls, "rb_eZeroDivError", constants_spec_rb_eZeroDivError, 0);
+#endif
+
+#ifdef HAVE_RB_EMATHDOMAINERROR
+ rb_define_method(cls, "rb_eMathDomainError", constants_spec_rb_eMathDomainError, 0);
+#endif
+
+#ifdef HAVE_RB_EENCCOMPATERROR
+ rb_define_method(cls, "rb_eEncCompatError", constants_spec_rb_eEncCompatError, 0);
+#endif
+
+#ifdef HAVE_RB_MWAITREADABLE
+ rb_define_method(cls, "rb_mWaitReadable", constants_spec_rb_mWaitReadable, 0);
+#endif
+
+#ifdef HAVE_RB_MWAITWRITABLE
+ rb_define_method(cls, "rb_mWaitWritable", constants_spec_rb_mWaitWritable, 0);
+#endif
+
+#ifdef HAVE_RB_CDIR
+ rb_define_method(cls, "rb_cDir", constants_spec_rb_cDir, 0);
+#endif
+
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/data_spec.c b/spec/ruby/optional/capi/ext/data_spec.c
new file mode 100644
index 0000000000..ed79497897
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/data_spec.c
@@ -0,0 +1,97 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(HAVE_RDATA) && defined(HAVE_DATA_WRAP_STRUCT)
+struct sample_wrapped_struct {
+ int foo;
+};
+
+void sample_wrapped_struct_free(void* st) {
+ free(st);
+}
+
+void sample_wrapped_struct_mark(void* st) {
+}
+
+VALUE sdaf_alloc_func(VALUE klass) {
+ struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
+ bar->foo = 42;
+ return Data_Wrap_Struct(klass, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar);
+}
+
+VALUE sdaf_get_struct(VALUE self) {
+ struct sample_wrapped_struct* bar;
+ Data_Get_Struct(self, struct sample_wrapped_struct, bar);
+
+ return INT2FIX((*bar).foo);
+}
+
+VALUE sws_wrap_struct(VALUE self, VALUE val) {
+ struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
+ bar->foo = FIX2INT(val);
+ return Data_Wrap_Struct(rb_cObject, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar);
+}
+
+VALUE sws_wrap_struct_null(VALUE self, VALUE val) {
+ struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
+ bar->foo = FIX2INT(val);
+ return Data_Wrap_Struct(0, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar);
+}
+
+VALUE sws_get_struct(VALUE self, VALUE obj) {
+ struct sample_wrapped_struct* bar;
+ Data_Get_Struct(obj, struct sample_wrapped_struct, bar);
+
+ return INT2FIX((*bar).foo);
+}
+
+VALUE sws_get_struct_rdata(VALUE self, VALUE obj) {
+ struct sample_wrapped_struct* bar;
+ bar = (struct sample_wrapped_struct*) RDATA(obj)->data;
+ return INT2FIX(bar->foo);
+}
+
+VALUE sws_get_struct_data_ptr(VALUE self, VALUE obj) {
+ struct sample_wrapped_struct* bar;
+ bar = (struct sample_wrapped_struct*) DATA_PTR(obj);
+ return INT2FIX(bar->foo);
+}
+
+VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) {
+ struct sample_wrapped_struct *old_struct, *new_struct;
+ new_struct = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
+ new_struct->foo = FIX2INT(new_val);
+ old_struct = RDATA(obj)->data;
+ free(old_struct);
+ RDATA(obj)->data = new_struct;
+ return Qnil;
+}
+#endif
+
+void Init_data_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiAllocSpecs", rb_cObject);
+
+#if defined(HAVE_RDATA) && defined(HAVE_DATA_WRAP_STRUCT)
+ rb_define_alloc_func(cls, sdaf_alloc_func);
+ rb_define_method(cls, "wrapped_data", sdaf_get_struct, 0);
+
+ cls = rb_define_class("CApiWrappedStructSpecs", rb_cObject);
+ rb_define_method(cls, "wrap_struct", sws_wrap_struct, 1);
+ rb_define_method(cls, "wrap_struct_null", sws_wrap_struct_null, 1);
+ rb_define_method(cls, "get_struct", sws_get_struct, 1);
+ rb_define_method(cls, "get_struct_rdata", sws_get_struct_rdata, 1);
+ rb_define_method(cls, "get_struct_data_ptr", sws_get_struct_data_ptr, 1);
+ rb_define_method(cls, "change_struct", sws_change_struct, 2);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/encoding_spec.c b/spec/ruby/optional/capi/ext/encoding_spec.c
new file mode 100644
index 0000000000..2e555ebc77
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/encoding_spec.c
@@ -0,0 +1,424 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include "ruby/encoding.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_ENC_CODERANGE_ASCIIONLY
+static VALUE encoding_spec_ENC_CODERANGE_ASCIIONLY(VALUE self, VALUE obj) {
+ if(ENC_CODERANGE_ASCIIONLY(obj)) {
+ return Qtrue;
+ } else {
+ return Qfalse;
+ }
+}
+#endif
+
+#ifdef HAVE_RB_USASCII_ENCODING
+static VALUE encoding_spec_rb_usascii_encoding(VALUE self) {
+ return rb_str_new2(rb_usascii_encoding()->name);
+}
+#endif
+
+#ifdef HAVE_RB_USASCII_ENCINDEX
+static VALUE encoding_spec_rb_usascii_encindex(VALUE self) {
+ return INT2NUM(rb_usascii_encindex());
+}
+#endif
+
+#ifdef HAVE_RB_ASCII8BIT_ENCODING
+static VALUE encoding_spec_rb_ascii8bit_encoding(VALUE self) {
+ return rb_str_new2(rb_ascii8bit_encoding()->name);
+}
+#endif
+
+#ifdef HAVE_RB_ASCII8BIT_ENCINDEX
+static VALUE encoding_spec_rb_ascii8bit_encindex(VALUE self) {
+ return INT2NUM(rb_ascii8bit_encindex());
+}
+#endif
+
+#ifdef HAVE_RB_UTF8_ENCODING
+static VALUE encoding_spec_rb_utf8_encoding(VALUE self) {
+ return rb_str_new2(rb_utf8_encoding()->name);
+}
+#endif
+
+#ifdef HAVE_RB_UTF8_ENCINDEX
+static VALUE encoding_spec_rb_utf8_encindex(VALUE self) {
+ return INT2NUM(rb_utf8_encindex());
+}
+#endif
+
+#ifdef HAVE_RB_LOCALE_ENCODING
+static VALUE encoding_spec_rb_locale_encoding(VALUE self) {
+ return rb_str_new2(rb_locale_encoding()->name);
+}
+#endif
+
+#ifdef HAVE_RB_LOCALE_ENCINDEX
+static VALUE encoding_spec_rb_locale_encindex(VALUE self) {
+ return INT2NUM(rb_locale_encindex());
+}
+#endif
+
+#ifdef HAVE_RB_FILESYSTEM_ENCODING
+static VALUE encoding_spec_rb_filesystem_encoding(VALUE self) {
+ return rb_str_new2(rb_filesystem_encoding()->name);
+}
+#endif
+
+#ifdef HAVE_RB_FILESYSTEM_ENCINDEX
+static VALUE encoding_spec_rb_filesystem_encindex(VALUE self) {
+ return INT2NUM(rb_filesystem_encindex());
+}
+#endif
+
+#ifdef HAVE_RB_DEFAULT_INTERNAL_ENCODING
+static VALUE encoding_spec_rb_default_internal_encoding(VALUE self) {
+ rb_encoding* enc = rb_default_internal_encoding();
+ if(enc == 0) return Qnil;
+ return rb_str_new2(enc->name);
+}
+#endif
+
+#ifdef HAVE_RB_DEFAULT_EXTERNAL_ENCODING
+static VALUE encoding_spec_rb_default_external_encoding(VALUE self) {
+ rb_encoding* enc = rb_default_external_encoding();
+ if(enc == 0) return Qnil;
+ return rb_str_new2(enc->name);
+}
+#endif
+
+#ifdef HAVE_RB_ENCDB_ALIAS
+/* Not exposed by MRI C-API encoding.h but used in the pg gem. */
+extern int rb_encdb_alias(const char* alias, const char* orig);
+
+static VALUE encoding_spec_rb_encdb_alias(VALUE self, VALUE alias, VALUE orig) {
+ return INT2NUM(rb_encdb_alias(RSTRING_PTR(alias), RSTRING_PTR(orig)));
+}
+#endif
+
+#if defined(HAVE_RB_ENC_ASSOCIATE) && defined(HAVE_RB_ENC_FIND)
+static VALUE encoding_spec_rb_enc_associate(VALUE self, VALUE obj, VALUE enc) {
+ return rb_enc_associate(obj, NIL_P(enc) ? NULL : rb_enc_find(RSTRING_PTR(enc)));
+}
+#endif
+
+#if defined(HAVE_RB_ENC_ASSOCIATE_INDEX) && defined(HAVE_RB_ENC_FIND_INDEX)
+static VALUE encoding_spec_rb_enc_associate_index(VALUE self, VALUE obj, VALUE index) {
+ return rb_enc_associate_index(obj, FIX2INT(index));
+}
+#endif
+
+#ifdef HAVE_RB_ENC_COMPATIBLE
+static VALUE encoding_spec_rb_enc_compatible(VALUE self, VALUE a, VALUE b) {
+ rb_encoding* enc = rb_enc_compatible(a, b);
+
+ if(!enc) return INT2FIX(0);
+
+ return rb_enc_from_encoding(enc);
+}
+#endif
+
+#ifdef HAVE_RB_ENC_COPY
+static VALUE encoding_spec_rb_enc_copy(VALUE self, VALUE dest, VALUE src) {
+ rb_enc_copy(dest, src);
+ return dest;
+}
+#endif
+
+#ifdef HAVE_RB_ENC_FIND
+static VALUE encoding_spec_rb_enc_find(VALUE self, VALUE name) {
+ return rb_str_new2(rb_enc_find(RSTRING_PTR(name))->name);
+}
+#endif
+
+#ifdef HAVE_RB_ENC_FIND_INDEX
+static VALUE encoding_spec_rb_enc_find_index(VALUE self, VALUE name) {
+ return INT2NUM(rb_enc_find_index(RSTRING_PTR(name)));
+}
+#endif
+
+#ifdef HAVE_RB_ENC_FROM_INDEX
+static VALUE encoding_spec_rb_enc_from_index(VALUE self, VALUE index) {
+ return rb_str_new2(rb_enc_from_index(NUM2INT(index))->name);
+}
+#endif
+
+#ifdef HAVE_RB_ENC_FROM_ENCODING
+static VALUE encoding_spec_rb_enc_from_encoding(VALUE self, VALUE name) {
+ return rb_enc_from_encoding(rb_enc_find(RSTRING_PTR(name)));
+}
+#endif
+
+#ifdef HAVE_RB_ENC_GET
+static VALUE encoding_spec_rb_enc_get(VALUE self, VALUE obj) {
+ return rb_str_new2(rb_enc_get(obj)->name);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_ENCODING
+static VALUE encoding_spec_rb_obj_encoding(VALUE self, VALUE obj) {
+ return rb_obj_encoding(obj);
+}
+#endif
+
+#ifdef HAVE_RB_ENC_GET_INDEX
+static VALUE encoding_spec_rb_enc_get_index(VALUE self, VALUE obj) {
+ return INT2NUM(rb_enc_get_index(obj));
+}
+#endif
+
+#if defined(HAVE_RB_ENC_SET_INDEX) \
+ && defined(HAVE_RB_ENC_FIND_INDEX) \
+ && defined(HAVE_RB_ENC_FIND_INDEX)
+static VALUE encoding_spec_rb_enc_set_index(VALUE self, VALUE obj, VALUE index) {
+ int i = NUM2INT(index);
+
+ rb_encoding* enc = rb_enc_from_index(i);
+ rb_enc_set_index(obj, i);
+
+ return rb_ary_new3(2, rb_str_new2(rb_enc_name(enc)),
+ rb_str_new2(rb_enc_name(rb_enc_get(obj))));
+}
+#endif
+
+#ifdef HAVE_RB_ENC_STR_CODERANGE
+static VALUE encoding_spec_rb_enc_str_coderange(VALUE self, VALUE str) {
+ int coderange = rb_enc_str_coderange(str);
+
+ switch(coderange) {
+ case ENC_CODERANGE_UNKNOWN:
+ return ID2SYM(rb_intern("coderange_unknown"));
+ case ENC_CODERANGE_7BIT:
+ return ID2SYM(rb_intern("coderange_7bit"));
+ case ENC_CODERANGE_VALID:
+ return ID2SYM(rb_intern("coderange_valid"));
+ case ENC_CODERANGE_BROKEN:
+ return ID2SYM(rb_intern("coderange_broken"));
+ default:
+ return ID2SYM(rb_intern("coderange_unrecognized"));
+ }
+}
+#endif
+
+#ifdef HAVE_RB_ENC_STR_NEW
+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));
+}
+#endif
+
+#ifdef HAVE_ENCODING_GET
+static VALUE encoding_spec_ENCODING_GET(VALUE self, VALUE obj) {
+ return INT2NUM(ENCODING_GET(obj));
+}
+#endif
+
+#ifdef HAVE_ENCODING_SET
+static VALUE encoding_spec_ENCODING_SET(VALUE self, VALUE obj, VALUE index) {
+ int i = NUM2INT(index);
+
+ rb_encoding* enc = rb_enc_from_index(i);
+ ENCODING_SET(obj, i);
+
+ return rb_ary_new3(2, rb_str_new2(rb_enc_name(enc)),
+ rb_str_new2(rb_enc_name(rb_enc_get(obj))));
+}
+#endif
+
+#if defined(HAVE_RB_ENC_TO_INDEX) && defined(HAVE_RB_ENC_FIND)
+static VALUE encoding_spec_rb_enc_to_index(VALUE self, VALUE name) {
+ return INT2NUM(rb_enc_to_index(NIL_P(name) ? NULL : rb_enc_find(RSTRING_PTR(name))));
+}
+#endif
+
+#ifdef HAVE_RB_TO_ENCODING
+static VALUE encoding_spec_rb_to_encoding(VALUE self, VALUE obj) {
+ return rb_str_new2(rb_to_encoding(obj)->name);
+}
+#endif
+
+#ifdef HAVE_RB_TO_ENCODING_INDEX
+static VALUE encoding_spec_rb_to_encoding_index(VALUE self, VALUE obj) {
+ return INT2NUM(rb_to_encoding_index(obj));
+}
+#endif
+
+#ifdef HAVE_RB_ENC_NTH
+static VALUE encoding_spec_rb_enc_nth(VALUE self, VALUE str, VALUE index) {
+ char* start = RSTRING_PTR(str);
+ char* end = start + RSTRING_LEN(str);
+ char* ptr = rb_enc_nth(start, end, FIX2LONG(index), rb_enc_get(str));
+ return LONG2NUM(ptr - start);
+}
+#endif
+
+#ifdef HAVE_RB_ENC_CODEPOINT_LEN
+static VALUE encoding_spec_rb_enc_codepoint_len(VALUE self, VALUE str) {
+ char* start = RSTRING_PTR(str);
+ char* end = start + RSTRING_LEN(str);
+
+ int len;
+ unsigned int codepoint = rb_enc_codepoint_len(start, end, &len, rb_enc_get(str));
+
+ return rb_ary_new3(2, LONG2NUM(codepoint), LONG2NUM(len));
+}
+#endif
+
+void Init_encoding_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiEncodingSpecs", rb_cObject);
+
+#ifdef HAVE_ENC_CODERANGE_ASCIIONLY
+ rb_define_method(cls, "ENC_CODERANGE_ASCIIONLY",
+ encoding_spec_ENC_CODERANGE_ASCIIONLY, 1);
+#endif
+
+#ifdef HAVE_RB_USASCII_ENCODING
+ rb_define_method(cls, "rb_usascii_encoding", encoding_spec_rb_usascii_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_USASCII_ENCINDEX
+ rb_define_method(cls, "rb_usascii_encindex", encoding_spec_rb_usascii_encindex, 0);
+#endif
+
+#ifdef HAVE_RB_ASCII8BIT_ENCODING
+ rb_define_method(cls, "rb_ascii8bit_encoding", encoding_spec_rb_ascii8bit_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_ASCII8BIT_ENCINDEX
+ rb_define_method(cls, "rb_ascii8bit_encindex", encoding_spec_rb_ascii8bit_encindex, 0);
+#endif
+
+#ifdef HAVE_RB_UTF8_ENCODING
+ rb_define_method(cls, "rb_utf8_encoding", encoding_spec_rb_utf8_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_UTF8_ENCINDEX
+ rb_define_method(cls, "rb_utf8_encindex", encoding_spec_rb_utf8_encindex, 0);
+#endif
+
+#ifdef HAVE_RB_LOCALE_ENCODING
+ rb_define_method(cls, "rb_locale_encoding", encoding_spec_rb_locale_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_LOCALE_ENCINDEX
+ rb_define_method(cls, "rb_locale_encindex", encoding_spec_rb_locale_encindex, 0);
+#endif
+
+#ifdef HAVE_RB_FILESYSTEM_ENCODING
+ rb_define_method(cls, "rb_filesystem_encoding", encoding_spec_rb_filesystem_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_FILESYSTEM_ENCINDEX
+ rb_define_method(cls, "rb_filesystem_encindex", encoding_spec_rb_filesystem_encindex, 0);
+#endif
+
+#ifdef HAVE_RB_DEFAULT_INTERNAL_ENCODING
+ rb_define_method(cls, "rb_default_internal_encoding",
+ encoding_spec_rb_default_internal_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_DEFAULT_EXTERNAL_ENCODING
+ rb_define_method(cls, "rb_default_external_encoding",
+ encoding_spec_rb_default_external_encoding, 0);
+#endif
+
+#ifdef HAVE_RB_ENCDB_ALIAS
+ rb_define_method(cls, "rb_encdb_alias", encoding_spec_rb_encdb_alias, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_ASSOCIATE
+ rb_define_method(cls, "rb_enc_associate", encoding_spec_rb_enc_associate, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_ASSOCIATE_INDEX
+ rb_define_method(cls, "rb_enc_associate_index", encoding_spec_rb_enc_associate_index, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_COMPATIBLE
+ rb_define_method(cls, "rb_enc_compatible", encoding_spec_rb_enc_compatible, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_COPY
+ rb_define_method(cls, "rb_enc_copy", encoding_spec_rb_enc_copy, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_FIND
+ rb_define_method(cls, "rb_enc_find", encoding_spec_rb_enc_find, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_FIND_INDEX
+ rb_define_method(cls, "rb_enc_find_index", encoding_spec_rb_enc_find_index, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_FROM_INDEX
+ rb_define_method(cls, "rb_enc_from_index", encoding_spec_rb_enc_from_index, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_FROM_ENCODING
+ rb_define_method(cls, "rb_enc_from_encoding", encoding_spec_rb_enc_from_encoding, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_GET
+ rb_define_method(cls, "rb_enc_get", encoding_spec_rb_enc_get, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_ENCODING
+ rb_define_method(cls, "rb_obj_encoding", encoding_spec_rb_obj_encoding, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_GET_INDEX
+ rb_define_method(cls, "rb_enc_get_index", encoding_spec_rb_enc_get_index, 1);
+#endif
+
+#if defined(HAVE_RB_ENC_SET_INDEX) \
+ && defined(HAVE_RB_ENC_FIND_INDEX) \
+ && defined(HAVE_RB_ENC_FIND_INDEX)
+ rb_define_method(cls, "rb_enc_set_index", encoding_spec_rb_enc_set_index, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_STR_CODERANGE
+ rb_define_method(cls, "rb_enc_str_coderange", encoding_spec_rb_enc_str_coderange, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_STR_NEW
+ rb_define_method(cls, "rb_enc_str_new", encoding_spec_rb_enc_str_new, 3);
+#endif
+
+#ifdef HAVE_ENCODING_GET
+ rb_define_method(cls, "ENCODING_GET", encoding_spec_ENCODING_GET, 1);
+#endif
+
+#ifdef HAVE_ENCODING_SET
+ rb_define_method(cls, "ENCODING_SET", encoding_spec_ENCODING_SET, 2);
+#endif
+
+#if defined(HAVE_RB_ENC_TO_INDEX) && defined(HAVE_RB_ENC_FIND)
+ rb_define_method(cls, "rb_enc_to_index", encoding_spec_rb_enc_to_index, 1);
+#endif
+
+#ifdef HAVE_RB_TO_ENCODING
+ rb_define_method(cls, "rb_to_encoding", encoding_spec_rb_to_encoding, 1);
+#endif
+
+#ifdef HAVE_RB_TO_ENCODING_INDEX
+ rb_define_method(cls, "rb_to_encoding_index", encoding_spec_rb_to_encoding_index, 1);
+#endif
+
+#ifdef HAVE_RB_ENC_NTH
+ rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2);
+#endif
+
+#ifdef HAVE_RB_ENC_CODEPOINT_LEN
+ rb_define_method(cls, "rb_enc_codepoint_len", encoding_spec_rb_enc_codepoint_len, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/enumerator_spec.c b/spec/ruby/optional/capi/ext/enumerator_spec.c
new file mode 100644
index 0000000000..6b08feab52
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/enumerator_spec.c
@@ -0,0 +1,27 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_ENUMERATORIZE
+VALUE enumerator_spec_rb_enumeratorize(int argc, VALUE *argv, VALUE self) {
+ VALUE obj, meth, args;
+ rb_scan_args(argc, argv, "2*", &obj, &meth, &args);
+ return rb_enumeratorize(obj, meth, (int)RARRAY_LEN(args), RARRAY_PTR(args));
+}
+#endif
+
+void Init_enumerator_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiEnumeratorSpecs", rb_cObject);
+
+#ifdef HAVE_RB_ENUMERATORIZE
+ rb_define_method(cls, "rb_enumeratorize", enumerator_spec_rb_enumeratorize, -1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/exception_spec.c b/spec/ruby/optional/capi/ext/exception_spec.c
new file mode 100644
index 0000000000..b37f74f03e
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/exception_spec.c
@@ -0,0 +1,72 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_EXC_NEW
+VALUE exception_spec_rb_exc_new(VALUE self, VALUE str) {
+ char *cstr = StringValuePtr(str);
+ return rb_exc_new(rb_eException, cstr, strlen(cstr));
+}
+#endif
+
+#ifdef HAVE_RB_EXC_NEW2
+VALUE exception_spec_rb_exc_new2(VALUE self, VALUE str) {
+ char *cstr = StringValuePtr(str);
+ return rb_exc_new2(rb_eException, cstr);
+}
+#endif
+
+#ifdef HAVE_RB_EXC_NEW3
+VALUE exception_spec_rb_exc_new3(VALUE self, VALUE str) {
+ return rb_exc_new3(rb_eException, str);
+}
+#endif
+
+#ifdef HAVE_RB_EXC_RAISE
+VALUE exception_spec_rb_exc_raise(VALUE self, VALUE exc) {
+ if (self != Qundef) rb_exc_raise(exc);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_SET_ERRINFO
+VALUE exception_spec_rb_set_errinfo(VALUE self, VALUE exc) {
+ rb_set_errinfo(exc);
+ return Qnil;
+}
+#endif
+
+void Init_exception_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiExceptionSpecs", rb_cObject);
+
+#ifdef HAVE_RB_EXC_NEW
+ rb_define_method(cls, "rb_exc_new", exception_spec_rb_exc_new, 1);
+#endif
+
+#ifdef HAVE_RB_EXC_NEW2
+ rb_define_method(cls, "rb_exc_new2", exception_spec_rb_exc_new2, 1);
+#endif
+
+#ifdef HAVE_RB_EXC_NEW3
+ rb_define_method(cls, "rb_exc_new3", exception_spec_rb_exc_new3, 1);
+#endif
+
+#ifdef HAVE_RB_EXC_RAISE
+ rb_define_method(cls, "rb_exc_raise", exception_spec_rb_exc_raise, 1);
+#endif
+
+#ifdef HAVE_RB_SET_ERRINFO
+ rb_define_method(cls, "rb_set_errinfo", exception_spec_rb_set_errinfo, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/file_spec.c b/spec/ruby/optional/capi/ext/file_spec.c
new file mode 100644
index 0000000000..98f8db1595
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/file_spec.c
@@ -0,0 +1,44 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_FILE_OPEN
+VALUE file_spec_rb_file_open(VALUE self, VALUE name, VALUE mode) {
+ return rb_file_open(RSTRING_PTR(name), RSTRING_PTR(mode));
+}
+#endif
+
+#ifdef HAVE_RB_FILE_OPEN_STR
+VALUE file_spec_rb_file_open_str(VALUE self, VALUE name, VALUE mode) {
+ return rb_file_open_str(name, RSTRING_PTR(mode));
+}
+#endif
+
+#ifdef HAVE_FILEPATHVALUE
+VALUE file_spec_FilePathValue(VALUE self, VALUE obj) {
+ return FilePathValue(obj);
+}
+#endif
+
+void Init_file_spec(void) {
+ VALUE cls = rb_define_class("CApiFileSpecs", rb_cObject);
+
+#ifdef HAVE_RB_FILE_OPEN
+ rb_define_method(cls, "rb_file_open", file_spec_rb_file_open, 2);
+#endif
+
+#ifdef HAVE_RB_FILE_OPEN_STR
+ rb_define_method(cls, "rb_file_open_str", file_spec_rb_file_open_str, 2);
+#endif
+
+#ifdef HAVE_FILEPATHVALUE
+ rb_define_method(cls, "FilePathValue", file_spec_FilePathValue, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/fixnum_spec.c b/spec/ruby/optional/capi/ext/fixnum_spec.c
new file mode 100644
index 0000000000..c3a207b387
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/fixnum_spec.c
@@ -0,0 +1,52 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static VALUE fixnum_spec_FIX2INT(VALUE self, VALUE value) {
+ int i = FIX2INT(value);
+ return INT2NUM(i);
+}
+
+static VALUE fixnum_spec_FIX2UINT(VALUE self, VALUE value) {
+ unsigned int i = FIX2UINT(value);
+ return UINT2NUM(i);
+}
+
+#ifdef HAVE_RB_FIX2UINT
+static VALUE fixnum_spec_rb_fix2uint(VALUE self, VALUE value) {
+ unsigned long i = rb_fix2uint(value);
+ return ULONG2NUM(i);
+}
+#endif
+
+#ifdef HAVE_RB_FIX2INT
+static VALUE fixnum_spec_rb_fix2int(VALUE self, VALUE value) {
+ long i = rb_fix2int(value);
+ return LONG2NUM(i);
+}
+#endif
+
+void Init_fixnum_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiFixnumSpecs", rb_cObject);
+
+ rb_define_method(cls, "FIX2INT", fixnum_spec_FIX2INT, 1);
+ rb_define_method(cls, "FIX2UINT", fixnum_spec_FIX2UINT, 1);
+
+#ifdef HAVE_RB_FIX2UINT
+ rb_define_method(cls, "rb_fix2uint", fixnum_spec_rb_fix2uint, 1);
+#endif
+
+#ifdef HAVE_RB_FIX2INT
+ rb_define_method(cls, "rb_fix2int", fixnum_spec_rb_fix2int, 1);
+#endif
+
+ (void)cls;
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/float_spec.c b/spec/ruby/optional/capi/ext/float_spec.c
new file mode 100644
index 0000000000..15c74e62c9
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/float_spec.c
@@ -0,0 +1,54 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <math.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_FLOAT_NEW
+static VALUE float_spec_new_zero(VALUE self) {
+ double flt = 0;
+ return rb_float_new(flt);
+}
+
+static VALUE float_spec_new_point_five(VALUE self) {
+ double flt = 0.555;
+ return rb_float_new(flt);
+}
+#endif
+
+#ifdef HAVE_RB_RFLOAT
+static VALUE float_spec_rb_Float(VALUE self, VALUE float_str) {
+ return rb_Float(float_str);
+}
+#endif
+
+#ifdef HAVE_RFLOAT_VALUE
+static VALUE float_spec_RFLOAT_VALUE(VALUE self, VALUE float_h) {
+ return rb_float_new(RFLOAT_VALUE(float_h));
+}
+#endif
+
+void Init_float_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiFloatSpecs", rb_cObject);
+
+#ifdef HAVE_RB_FLOAT_NEW
+ rb_define_method(cls, "new_zero", float_spec_new_zero, 0);
+ rb_define_method(cls, "new_point_five", float_spec_new_point_five, 0);
+#endif
+
+#ifdef HAVE_RB_RFLOAT
+ rb_define_method(cls, "rb_Float", float_spec_rb_Float, 1);
+#endif
+
+#ifdef HAVE_RFLOAT_VALUE
+ rb_define_method(cls, "RFLOAT_VALUE", float_spec_RFLOAT_VALUE, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/gc_spec.c b/spec/ruby/optional/capi/ext/gc_spec.c
new file mode 100644
index 0000000000..05341bb01d
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/gc_spec.c
@@ -0,0 +1,72 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_GC_REGISTER_ADDRESS
+VALUE registered_tagged_value;
+VALUE registered_reference_value;
+
+static VALUE registered_tagged_address(VALUE self) {
+ return registered_tagged_value;
+}
+
+static VALUE registered_reference_address(VALUE self) {
+ return registered_reference_value;
+}
+#endif
+
+#ifdef HAVE_RB_GC_ENABLE
+static VALUE gc_spec_rb_gc_enable() {
+ return rb_gc_enable();
+}
+#endif
+
+#ifdef HAVE_RB_GC_DISABLE
+static VALUE gc_spec_rb_gc_disable() {
+ return rb_gc_disable();
+}
+#endif
+
+#ifdef HAVE_RB_GC
+static VALUE gc_spec_rb_gc() {
+ rb_gc();
+ return Qnil;
+}
+#endif
+
+
+void Init_gc_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiGCSpecs", rb_cObject);
+
+#ifdef HAVE_RB_GC_REGISTER_ADDRESS
+ registered_tagged_value = INT2NUM(10);
+ registered_reference_value = rb_str_new2("Globally registered data");
+
+ rb_gc_register_address(&registered_tagged_value);
+ rb_gc_register_address(&registered_reference_value);
+
+ rb_define_method(cls, "registered_tagged_address", registered_tagged_address, 0);
+ rb_define_method(cls, "registered_reference_address", registered_reference_address, 0);
+#endif
+
+#ifdef HAVE_RB_GC_ENABLE
+ rb_define_method(cls, "rb_gc_enable", gc_spec_rb_gc_enable, 0);
+#endif
+
+#ifdef HAVE_RB_GC_DISABLE
+ rb_define_method(cls, "rb_gc_disable", gc_spec_rb_gc_disable, 0);
+#endif
+
+#ifdef HAVE_RB_GC
+ rb_define_method(cls, "rb_gc", gc_spec_rb_gc, 0);
+#endif
+
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/globals_spec.c b/spec/ruby/optional/capi/ext/globals_spec.c
new file mode 100644
index 0000000000..0c28a7f8ab
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/globals_spec.c
@@ -0,0 +1,199 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_DEFINE_HOOKED_VARIABLE
+VALUE g_hooked_var;
+
+void var_2x_setter(VALUE val, ID id, VALUE *var) {
+ *var = INT2NUM(NUM2INT(val) * 2);
+}
+
+static VALUE sb_define_hooked_variable(VALUE self, VALUE var_name) {
+ rb_define_hooked_variable(StringValuePtr(var_name), &g_hooked_var, 0, var_2x_setter);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_READONLY_VARIABLE
+VALUE g_ro_var;
+
+static VALUE sb_define_readonly_variable(VALUE self, VALUE var_name, VALUE val) {
+ g_ro_var = val;
+ rb_define_readonly_variable(StringValuePtr(var_name), &g_ro_var);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_VARIABLE
+VALUE g_var;
+
+static VALUE sb_get_global_value(VALUE self) {
+ return g_var;
+}
+
+static VALUE sb_define_variable(VALUE self, VALUE var_name, VALUE val) {
+ g_var = val;
+ rb_define_variable(StringValuePtr(var_name), &g_var);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_F_GLOBAL_VARIABLES
+static VALUE sb_f_global_variables(VALUE self) {
+ return rb_f_global_variables();
+}
+#endif
+
+#ifdef HAVE_RB_GV_GET
+static VALUE sb_gv_get(VALUE self, VALUE var) {
+ return rb_gv_get(StringValuePtr(var));
+}
+#endif
+
+#ifdef HAVE_RB_GV_SET
+static VALUE sb_gv_set(VALUE self, VALUE var, VALUE val) {
+ return rb_gv_set(StringValuePtr(var), val);
+}
+#endif
+
+#ifdef HAVE_RB_STDIN
+static VALUE global_spec_rb_stdin(VALUE self) {
+ return rb_stdin;
+}
+#endif
+
+#ifdef HAVE_RB_STDOUT
+static VALUE global_spec_rb_stdout(VALUE self) {
+ return rb_stdout;
+}
+#endif
+
+#ifdef HAVE_RB_STDERR
+static VALUE global_spec_rb_stderr(VALUE self) {
+ return rb_stderr;
+}
+#endif
+
+#ifdef HAVE_RB_DEFOUT
+static VALUE global_spec_rb_defout(VALUE self) {
+ return rb_defout;
+}
+#endif
+
+#ifdef HAVE_RB_RS
+static VALUE global_spec_rb_rs(VALUE self) {
+ return rb_rs;
+}
+#endif
+
+#ifdef HAVE_RB_DEFAULT_RS
+static VALUE global_spec_rb_default_rs(VALUE self) {
+ return rb_default_rs;
+}
+#endif
+
+#ifdef HAVE_RB_OUTPUT_RS
+static VALUE global_spec_rb_output_rs(VALUE self) {
+ return rb_output_rs;
+}
+#endif
+
+#ifdef HAVE_RB_OUTPUT_FS
+static VALUE global_spec_rb_output_fs(VALUE self) {
+ return rb_output_fs;
+}
+#endif
+
+#ifdef HAVE_RB_LASTLINE_SET
+static VALUE global_spec_rb_lastline_set(VALUE self, VALUE line) {
+ rb_lastline_set(line);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_LASTLINE_GET
+static VALUE global_spec_rb_lastline_get(VALUE self) {
+ return rb_lastline_get();
+}
+#endif
+
+void Init_globals_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiGlobalSpecs", rb_cObject);
+
+#ifdef HAVE_RB_DEFINE_HOOKED_VARIABLE
+ g_hooked_var = Qnil;
+ rb_define_method(cls, "rb_define_hooked_variable_2x", sb_define_hooked_variable, 1);
+#endif
+
+#ifdef HAVE_RB_DEFINE_READONLY_VARIABLE
+ g_ro_var = Qnil;
+ rb_define_method(cls, "rb_define_readonly_variable", sb_define_readonly_variable, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_VARIABLE
+ g_var = Qnil;
+ rb_define_method(cls, "rb_define_variable", sb_define_variable, 2);
+ rb_define_method(cls, "sb_get_global_value", sb_get_global_value, 0);
+#endif
+
+#ifdef HAVE_RB_F_GLOBAL_VARIABLES
+ rb_define_method(cls, "rb_f_global_variables", sb_f_global_variables, 0);
+#endif
+
+#ifdef HAVE_RB_GV_GET
+ rb_define_method(cls, "sb_gv_get", sb_gv_get, 1);
+#endif
+
+#ifdef HAVE_RB_GV_SET
+ rb_define_method(cls, "sb_gv_set", sb_gv_set, 2);
+#endif
+
+#ifdef HAVE_RB_STDIN
+ rb_define_method(cls, "rb_stdin", global_spec_rb_stdin, 0);
+#endif
+
+#ifdef HAVE_RB_STDOUT
+ rb_define_method(cls, "rb_stdout", global_spec_rb_stdout, 0);
+#endif
+
+#ifdef HAVE_RB_STDERR
+ rb_define_method(cls, "rb_stderr", global_spec_rb_stderr, 0);
+#endif
+
+#ifdef HAVE_RB_DEFOUT
+ rb_define_method(cls, "rb_defout", global_spec_rb_defout, 0);
+#endif
+
+#ifdef HAVE_RB_RS
+ rb_define_method(cls, "rb_rs", global_spec_rb_rs, 0);
+#endif
+
+#ifdef HAVE_RB_DEFAULT_RS
+ rb_define_method(cls, "rb_default_rs", global_spec_rb_default_rs, 0);
+#endif
+
+#ifdef HAVE_RB_OUTPUT_RS
+ rb_define_method(cls, "rb_output_rs", global_spec_rb_output_rs, 0);
+#endif
+
+#ifdef HAVE_RB_OUTPUT_FS
+ rb_define_method(cls, "rb_output_fs", global_spec_rb_output_fs, 0);
+#endif
+
+#ifdef HAVE_RB_LASTLINE_SET
+ rb_define_method(cls, "rb_lastline_set", global_spec_rb_lastline_set, 1);
+#endif
+
+#ifdef HAVE_RB_LASTLINE_GET
+ rb_define_method(cls, "rb_lastline_get", global_spec_rb_lastline_get, 0);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/hash_spec.c b/spec/ruby/optional/capi/ext/hash_spec.c
new file mode 100644
index 0000000000..73e7ef5c13
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/hash_spec.c
@@ -0,0 +1,218 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_HASH
+VALUE hash_spec_rb_hash(VALUE self, VALUE hash) {
+ return rb_hash(hash);
+}
+#endif
+
+#ifdef HAVE_RB_HASH2
+VALUE hash_spec_rb_Hash(VALUE self, VALUE val) {
+ return rb_Hash(val);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_DUP
+VALUE hash_spec_rb_hash_dup(VALUE self, VALUE hash) {
+ return rb_hash_dup(hash);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_FETCH
+VALUE hash_spec_rb_hash_fetch(VALUE self, VALUE hash, VALUE key) {
+ return rb_hash_fetch(hash, key);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_FREEZE
+VALUE hash_spec_rb_hash_freeze(VALUE self, VALUE hash) {
+ return rb_hash_freeze(hash);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_AREF
+VALUE hash_spec_rb_hash_aref(VALUE self, VALUE hash, VALUE key) {
+ return rb_hash_aref(hash, key);
+}
+
+VALUE hash_spec_rb_hash_aref_nil(VALUE self, VALUE hash, VALUE key) {
+ VALUE ret = rb_hash_aref(hash, key);
+ return NIL_P(ret) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_HASH_ASET
+VALUE hash_spec_rb_hash_aset(VALUE self, VALUE hash, VALUE key, VALUE val) {
+ return rb_hash_aset(hash, key, val);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_CLEAR
+VALUE hash_spec_rb_hash_clear(VALUE self, VALUE hash) {
+ return rb_hash_clear(hash);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_DELETE
+VALUE hash_spec_rb_hash_delete(VALUE self, VALUE hash, VALUE key) {
+ return rb_hash_delete(hash, key);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_DELETE_IF
+VALUE hash_spec_rb_hash_delete_if(VALUE self, VALUE hash) {
+ return rb_hash_delete_if(hash);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_FOREACH
+static int foreach_i(VALUE key, VALUE val, VALUE other) {
+ rb_hash_aset(other, key, val);
+ return 0; /* ST_CONTINUE; */
+}
+
+static int foreach_stop_i(VALUE key, VALUE val, VALUE other) {
+ rb_hash_aset(other, key, val);
+ return 1; /* ST_STOP; */
+}
+
+static int foreach_delete_i(VALUE key, VALUE val, VALUE other) {
+ rb_hash_aset(other, key, val);
+ return 2; /* ST_DELETE; */
+}
+
+VALUE hash_spec_rb_hash_foreach(VALUE self, VALUE hsh) {
+ VALUE other = rb_hash_new();
+ rb_hash_foreach(hsh, foreach_i, other);
+ return other;
+}
+
+VALUE hash_spec_rb_hash_foreach_stop(VALUE self, VALUE hsh) {
+ VALUE other = rb_hash_new();
+ rb_hash_foreach(hsh, foreach_stop_i, other);
+ return other;
+}
+
+VALUE hash_spec_rb_hash_foreach_delete(VALUE self, VALUE hsh) {
+ VALUE other = rb_hash_new();
+ rb_hash_foreach(hsh, foreach_delete_i, other);
+ return other;
+}
+#endif
+
+#ifdef HAVE_RB_HASH_LOOKUP
+VALUE hash_spec_rb_hash_lookup(VALUE self, VALUE hash, VALUE key) {
+ return rb_hash_lookup(hash, key);
+}
+
+VALUE hash_spec_rb_hash_lookup_nil(VALUE self, VALUE hash, VALUE key) {
+ VALUE ret = rb_hash_lookup(hash, key);
+ return ret == Qnil ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_HASH_LOOKUP2
+VALUE hash_spec_rb_hash_lookup2(VALUE self, VALUE hash, VALUE key, VALUE def) {
+ return rb_hash_lookup2(hash, key, def);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_NEW
+VALUE hash_spec_rb_hash_new(VALUE self) {
+ return rb_hash_new();
+}
+#endif
+
+#ifdef HAVE_RB_HASH_SIZE
+VALUE hash_spec_rb_hash_size(VALUE self, VALUE hash) {
+ return rb_hash_size(hash);
+}
+#endif
+
+#ifdef HAVE_RB_HASH_SET_IFNONE
+VALUE hash_spec_rb_hash_set_ifnone(VALUE self, VALUE hash, VALUE def) {
+ return rb_hash_set_ifnone(hash, def);
+}
+#endif
+
+void Init_hash_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiHashSpecs", rb_cObject);
+
+#ifdef HAVE_RB_HASH
+ rb_define_method(cls, "rb_hash", hash_spec_rb_hash, 1);
+#endif
+
+#ifdef HAVE_RB_HASH2
+ rb_define_method(cls, "rb_Hash", hash_spec_rb_Hash, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_DUP
+ rb_define_method(cls, "rb_hash_dup", hash_spec_rb_hash_dup, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_FREEZE
+ rb_define_method(cls, "rb_hash_freeze", hash_spec_rb_hash_freeze, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_AREF
+ rb_define_method(cls, "rb_hash_aref", hash_spec_rb_hash_aref, 2);
+ rb_define_method(cls, "rb_hash_aref_nil", hash_spec_rb_hash_aref_nil, 2);
+#endif
+
+#ifdef HAVE_RB_HASH_ASET
+ rb_define_method(cls, "rb_hash_aset", hash_spec_rb_hash_aset, 3);
+#endif
+
+#ifdef HAVE_RB_HASH_CLEAR
+ rb_define_method(cls, "rb_hash_clear", hash_spec_rb_hash_clear, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_DELETE
+ rb_define_method(cls, "rb_hash_delete", hash_spec_rb_hash_delete, 2);
+#endif
+
+#ifdef HAVE_RB_HASH_DELETE_IF
+ rb_define_method(cls, "rb_hash_delete_if", hash_spec_rb_hash_delete_if, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_FETCH
+ rb_define_method(cls, "rb_hash_fetch", hash_spec_rb_hash_fetch, 2);
+#endif
+
+#ifdef HAVE_RB_HASH_FOREACH
+ rb_define_method(cls, "rb_hash_foreach", hash_spec_rb_hash_foreach, 1);
+ rb_define_method(cls, "rb_hash_foreach_stop", hash_spec_rb_hash_foreach_stop, 1);
+ rb_define_method(cls, "rb_hash_foreach_delete", hash_spec_rb_hash_foreach_delete, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_LOOKUP
+ rb_define_method(cls, "rb_hash_lookup_nil", hash_spec_rb_hash_lookup_nil, 2);
+ rb_define_method(cls, "rb_hash_lookup", hash_spec_rb_hash_lookup, 2);
+#endif
+
+#ifdef HAVE_RB_HASH_LOOKUP2
+ rb_define_method(cls, "rb_hash_lookup2", hash_spec_rb_hash_lookup2, 3);
+#endif
+
+#ifdef HAVE_RB_HASH_NEW
+ rb_define_method(cls, "rb_hash_new", hash_spec_rb_hash_new, 0);
+#endif
+
+#ifdef HAVE_RB_HASH_SIZE
+ rb_define_method(cls, "rb_hash_size", hash_spec_rb_hash_size, 1);
+#endif
+
+#ifdef HAVE_RB_HASH_SET_IFNONE
+ rb_define_method(cls, "rb_hash_set_ifnone", hash_spec_rb_hash_set_ifnone, 2);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/integer_spec.c b/spec/ruby/optional/capi/ext/integer_spec.c
new file mode 100644
index 0000000000..821d8373c9
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/integer_spec.c
@@ -0,0 +1,40 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_INTEGER_PACK
+static VALUE integer_spec_rb_integer_pack(VALUE self, VALUE value,
+ VALUE words, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
+{
+ int result = rb_integer_pack(value, (void*)RSTRING_PTR(words), FIX2INT(numwords),
+ FIX2INT(wordsize), FIX2INT(nails), FIX2INT(flags));
+ return INT2FIX(result);
+}
+#endif
+
+void Init_integer_spec(void) {
+#ifdef HAVE_RB_INTEGER_PACK
+ VALUE cls;
+ cls = rb_define_class("CApiIntegerSpecs", rb_cObject);
+
+ rb_define_const(cls, "MSWORD", INT2NUM(INTEGER_PACK_MSWORD_FIRST));
+ rb_define_const(cls, "LSWORD", INT2NUM(INTEGER_PACK_LSWORD_FIRST));
+ rb_define_const(cls, "MSBYTE", INT2NUM(INTEGER_PACK_MSBYTE_FIRST));
+ rb_define_const(cls, "LSBYTE", INT2NUM(INTEGER_PACK_LSBYTE_FIRST));
+ rb_define_const(cls, "NATIVE", INT2NUM(INTEGER_PACK_NATIVE_BYTE_ORDER));
+ rb_define_const(cls, "PACK_2COMP", INT2NUM(INTEGER_PACK_2COMP));
+ rb_define_const(cls, "LITTLE_ENDIAN", INT2NUM(INTEGER_PACK_LITTLE_ENDIAN));
+ rb_define_const(cls, "BIG_ENDIAN", INT2NUM(INTEGER_PACK_BIG_ENDIAN));
+ rb_define_const(cls, "FORCE_BIGNUM", INT2NUM(INTEGER_PACK_FORCE_BIGNUM));
+ rb_define_const(cls, "NEGATIVE", INT2NUM(INTEGER_PACK_NEGATIVE));
+
+ rb_define_method(cls, "rb_integer_pack", integer_spec_rb_integer_pack, 6);
+#endif
+}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
diff --git a/spec/ruby/optional/capi/ext/io_spec.c b/spec/ruby/optional/capi/ext/io_spec.c
new file mode 100644
index 0000000000..40069bd54b
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/io_spec.c
@@ -0,0 +1,303 @@
+#include "ruby.h"
+#include "rubyspec.h"
+#include "ruby/io.h"
+#include <errno.h>
+#include <fcntl.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static int set_non_blocking(int fd) {
+#if defined(O_NONBLOCK) && defined(F_GETFL)
+ int flags = fcntl(fd, F_GETFL, 0);
+ if (flags == -1)
+ flags = 0;
+ return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+#elif defined(FIOBIO)
+ int flags = 1;
+ return ioctl(fd, FIOBIO, &flags);
+#else
+ errno = ENOSYS;
+ return -1;
+#endif
+}
+
+#ifdef HAVE_GET_OPEN_FILE
+static int io_spec_get_fd(VALUE io) {
+ rb_io_t* fp;
+ GetOpenFile(io, fp);
+ return fp->fd;
+}
+
+VALUE io_spec_GetOpenFile_fd(VALUE self, VALUE io) {
+ return INT2NUM(io_spec_get_fd(io));
+}
+#endif
+
+#ifdef HAVE_RB_IO_ADDSTR
+VALUE io_spec_rb_io_addstr(VALUE self, VALUE io, VALUE str) {
+ return rb_io_addstr(io, str);
+}
+#endif
+
+#ifdef HAVE_RB_IO_PRINTF
+VALUE io_spec_rb_io_printf(VALUE self, VALUE io, VALUE ary) {
+ long argc = RARRAY_LEN(ary);
+ VALUE *argv = alloca(sizeof(VALUE) * argc);
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ argv[i] = rb_ary_entry(ary, i);
+ }
+
+ return rb_io_printf((int)argc, argv, io);
+}
+#endif
+
+#ifdef HAVE_RB_IO_PRINT
+VALUE io_spec_rb_io_print(VALUE self, VALUE io, VALUE ary) {
+ long argc = RARRAY_LEN(ary);
+ VALUE *argv = alloca(sizeof(VALUE) * argc);
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ argv[i] = rb_ary_entry(ary, i);
+ }
+
+ return rb_io_print((int)argc, argv, io);
+}
+#endif
+
+#ifdef HAVE_RB_IO_PUTS
+VALUE io_spec_rb_io_puts(VALUE self, VALUE io, VALUE ary) {
+ long argc = RARRAY_LEN(ary);
+ VALUE *argv = alloca(sizeof(VALUE) * argc);
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ argv[i] = rb_ary_entry(ary, i);
+ }
+
+ return rb_io_puts((int)argc, argv, io);
+}
+#endif
+
+#ifdef HAVE_RB_IO_WRITE
+VALUE io_spec_rb_io_write(VALUE self, VALUE io, VALUE str) {
+ return rb_io_write(io, str);
+}
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_IO
+VALUE io_spec_rb_io_check_io(VALUE self, VALUE io) {
+ return rb_io_check_io(io);
+}
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_READABLE
+VALUE io_spec_rb_io_check_readable(VALUE self, VALUE io) {
+ rb_io_t* fp;
+ GetOpenFile(io, fp);
+ rb_io_check_readable(fp);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_WRITABLE
+VALUE io_spec_rb_io_check_writable(VALUE self, VALUE io) {
+ rb_io_t* fp;
+ GetOpenFile(io, fp);
+ rb_io_check_writable(fp);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_CLOSED
+VALUE io_spec_rb_io_check_closed(VALUE self, VALUE io) {
+ rb_io_t* fp;
+ GetOpenFile(io, fp);
+ rb_io_check_closed(fp);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_IO_TAINT_CHECK
+VALUE io_spec_rb_io_taint_check(VALUE self, VALUE io) {
+ /*rb_io_t* fp;
+ GetOpenFile(io, fp);*/
+ rb_io_taint_check(io);
+ return io;
+}
+#endif
+
+#ifdef HAVE_RB_IO_WAIT_READABLE
+#define RB_IO_WAIT_READABLE_BUF 13
+
+VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) {
+ int fd = io_spec_get_fd(io);
+ char buf[RB_IO_WAIT_READABLE_BUF];
+ int ret, saved_errno;
+
+ if (set_non_blocking(fd) == -1)
+ rb_sys_fail("set_non_blocking failed");
+
+ if(RTEST(read_p)) {
+ if (read(fd, buf, RB_IO_WAIT_READABLE_BUF) != -1) {
+ return Qnil;
+ }
+ saved_errno = errno;
+ rb_ivar_set(self, rb_intern("@write_data"), Qtrue);
+ errno = saved_errno;
+ }
+
+ ret = rb_io_wait_readable(fd);
+
+ if(RTEST(read_p)) {
+ ssize_t r = read(fd, buf, RB_IO_WAIT_READABLE_BUF);
+ if (r != RB_IO_WAIT_READABLE_BUF) {
+ perror("read");
+ return SSIZET2NUM(r);
+ }
+ rb_ivar_set(self, rb_intern("@read_data"),
+ rb_str_new(buf, RB_IO_WAIT_READABLE_BUF));
+ }
+
+ return ret ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_IO_WAIT_WRITABLE
+VALUE io_spec_rb_io_wait_writable(VALUE self, VALUE io) {
+ int ret = rb_io_wait_writable(io_spec_get_fd(io));
+ return ret ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_WAIT_FD
+VALUE io_spec_rb_thread_wait_fd(VALUE self, VALUE io) {
+ rb_thread_wait_fd(io_spec_get_fd(io));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_FD_WRITABLE
+VALUE io_spec_rb_thread_fd_writable(VALUE self, VALUE io) {
+ rb_thread_fd_writable(io_spec_get_fd(io));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_IO_BINMODE
+VALUE io_spec_rb_io_binmode(VALUE self, VALUE io) {
+ return rb_io_binmode(io);
+}
+#endif
+
+#ifdef HAVE_RB_FD_FIX_CLOEXEC
+VALUE io_spec_rb_fd_fix_cloexec(VALUE self, VALUE io) {
+ rb_fd_fix_cloexec(io_spec_get_fd(io));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_CLOEXEC_OPEN
+VALUE io_spec_rb_cloexec_open(VALUE self, VALUE path, VALUE flags, VALUE mode) {
+ const char *pathname = StringValuePtr(path);
+ int fd = rb_cloexec_open(pathname, FIX2INT(flags), FIX2INT(mode));
+ return rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2FIX(fd));
+}
+#endif
+
+#ifdef HAVE_RB_IO_CLOSE
+VALUE io_spec_rb_io_close(VALUE self, VALUE io) {
+ return rb_io_close(io);
+}
+#endif
+
+void Init_io_spec(void) {
+ VALUE cls = rb_define_class("CApiIOSpecs", rb_cObject);
+
+#ifdef HAVE_GET_OPEN_FILE
+ rb_define_method(cls, "GetOpenFile_fd", io_spec_GetOpenFile_fd, 1);
+#endif
+
+#ifdef HAVE_RB_IO_ADDSTR
+ rb_define_method(cls, "rb_io_addstr", io_spec_rb_io_addstr, 2);
+#endif
+
+#ifdef HAVE_RB_IO_PRINTF
+ rb_define_method(cls, "rb_io_printf", io_spec_rb_io_printf, 2);
+#endif
+
+#ifdef HAVE_RB_IO_PRINT
+ rb_define_method(cls, "rb_io_print", io_spec_rb_io_print, 2);
+#endif
+
+#ifdef HAVE_RB_IO_PUTS
+ rb_define_method(cls, "rb_io_puts", io_spec_rb_io_puts, 2);
+#endif
+
+#ifdef HAVE_RB_IO_WRITE
+ rb_define_method(cls, "rb_io_write", io_spec_rb_io_write, 2);
+#endif
+
+#ifdef HAVE_RB_IO_CLOSE
+ rb_define_method(cls, "rb_io_close", io_spec_rb_io_close, 1);
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_IO
+ rb_define_method(cls, "rb_io_check_io", io_spec_rb_io_check_io, 1);
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_READABLE
+ rb_define_method(cls, "rb_io_check_readable", io_spec_rb_io_check_readable, 1);
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_WRITABLE
+ rb_define_method(cls, "rb_io_check_writable", io_spec_rb_io_check_writable, 1);
+#endif
+
+#ifdef HAVE_RB_IO_CHECK_CLOSED
+ rb_define_method(cls, "rb_io_check_closed", io_spec_rb_io_check_closed, 1);
+#endif
+
+#ifdef HAVE_RB_IO_TAINT_CHECK
+ rb_define_method(cls, "rb_io_taint_check", io_spec_rb_io_taint_check, 1);
+#endif
+
+#ifdef HAVE_RB_IO_WAIT_READABLE
+ rb_define_method(cls, "rb_io_wait_readable", io_spec_rb_io_wait_readable, 2);
+#endif
+
+#ifdef HAVE_RB_IO_WAIT_WRITABLE
+ rb_define_method(cls, "rb_io_wait_writable", io_spec_rb_io_wait_writable, 1);
+#endif
+
+#ifdef HAVE_RB_THREAD_WAIT_FD
+ rb_define_method(cls, "rb_thread_wait_fd", io_spec_rb_thread_wait_fd, 1);
+#endif
+
+#ifdef HAVE_RB_THREAD_FD_WRITABLE
+ rb_define_method(cls, "rb_thread_fd_writable", io_spec_rb_thread_fd_writable, 1);
+#endif
+
+#ifdef HAVE_RB_IO_BINMODE
+ rb_define_method(cls, "rb_io_binmode", io_spec_rb_io_binmode, 1);
+#endif
+
+#ifdef HAVE_RB_FD_FIX_CLOEXEC
+ rb_define_method(cls, "rb_fd_fix_cloexec", io_spec_rb_fd_fix_cloexec, 1);
+#endif
+
+#ifdef HAVE_RB_CLOEXEC_OPEN
+ rb_define_method(cls, "rb_cloexec_open", io_spec_rb_cloexec_open, 3);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c
new file mode 100644
index 0000000000..f126bd0a4a
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/kernel_spec.c
@@ -0,0 +1,436 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <errno.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+VALUE kernel_spec_call_proc(VALUE arg_array) {
+ VALUE arg = rb_ary_pop(arg_array);
+ VALUE proc = rb_ary_pop(arg_array);
+ return rb_funcall(proc, rb_intern("call"), 1, arg);
+}
+
+#ifdef HAVE_RB_BLOCK_GIVEN_P
+static VALUE kernel_spec_rb_block_given_p(VALUE self) {
+ return rb_block_given_p() ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_NEED_BLOCK
+VALUE kernel_spec_rb_need_block(VALUE self) {
+ rb_need_block();
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_BLOCK_PROC
+VALUE kernel_spec_rb_block_proc(VALUE self) {
+ return rb_block_proc();
+}
+#endif
+
+#ifdef HAVE_RB_BLOCK_CALL
+
+VALUE block_call_inject(VALUE yield_value, VALUE data2) {
+ /* yield_value yields the first block argument */
+ VALUE elem = yield_value;
+ VALUE elem_incr = INT2FIX(FIX2INT(elem) + 1);
+ return elem_incr;
+}
+
+VALUE kernel_spec_rb_block_call(VALUE self, VALUE ary) {
+ return rb_block_call(ary, rb_intern("map"), 0, NULL, block_call_inject, Qnil);
+}
+
+VALUE block_call_inject_multi_arg(VALUE yield_value, VALUE data2, int argc, VALUE argv[]) {
+ /* yield_value yields the first block argument */
+ VALUE sum = yield_value;
+ VALUE elem = argv[1];
+
+ return INT2FIX(FIX2INT(sum) + FIX2INT(elem));
+}
+
+VALUE kernel_spec_rb_block_call_multi_arg(VALUE self, VALUE ary) {
+ VALUE method_args[1];
+ method_args[0] = INT2FIX(0);
+ return rb_block_call(ary, rb_intern("inject"), 1, method_args, block_call_inject_multi_arg, Qnil);
+}
+
+VALUE kernel_spec_rb_block_call_no_func(VALUE self, VALUE ary) {
+ return rb_block_call(ary, rb_intern("map"), 0, NULL, NULL, Qnil);
+}
+
+#endif
+
+#ifdef HAVE_RB_ENSURE
+VALUE kernel_spec_rb_ensure(VALUE self, VALUE main_proc, VALUE arg,
+ VALUE ensure_proc, VALUE arg2) {
+ VALUE main_array, ensure_array;
+
+ main_array = rb_ary_new();
+ rb_ary_push(main_array, main_proc);
+ rb_ary_push(main_array, arg);
+
+ ensure_array = rb_ary_new();
+ rb_ary_push(ensure_array, ensure_proc);
+ rb_ary_push(ensure_array, arg2);
+
+ return rb_ensure(kernel_spec_call_proc, main_array,
+ kernel_spec_call_proc, ensure_array);
+}
+#endif
+
+#ifdef HAVE_RB_CATCH
+VALUE kernel_spec_call_proc_with_catch(VALUE arg, VALUE data) {
+ return rb_funcall(data, rb_intern("call"), 0);
+}
+
+VALUE kernel_spec_rb_catch(VALUE self, VALUE sym, VALUE main_proc) {
+ return rb_catch(StringValuePtr(sym), kernel_spec_call_proc_with_catch, main_proc);
+}
+#endif
+
+#ifdef HAVE_RB_CATCH_OBJ
+VALUE kernel_spec_call_proc_with_catch_obj(VALUE arg, VALUE data) {
+ return rb_funcall(data, rb_intern("call"), 0);
+}
+
+VALUE kernel_spec_rb_catch_obj(VALUE self, VALUE obj, VALUE main_proc) {
+ return rb_catch_obj(obj, kernel_spec_call_proc_with_catch, main_proc);
+}
+#endif
+
+#ifdef HAVE_RB_EVAL_STRING
+VALUE kernel_spec_rb_eval_string(VALUE self, VALUE str) {
+ return rb_eval_string(RSTRING_PTR(str));
+}
+#endif
+
+#ifdef HAVE_RB_RAISE
+VALUE kernel_spec_rb_raise(VALUE self, VALUE hash) {
+ rb_hash_aset(hash, ID2SYM(rb_intern("stage")), ID2SYM(rb_intern("before")));
+ if (self != Qundef)
+ rb_raise(rb_eTypeError, "Wrong argument type %s (expected %s)", "Integer", "String");
+ rb_hash_aset(hash, ID2SYM(rb_intern("stage")), ID2SYM(rb_intern("after")));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_THROW
+VALUE kernel_spec_rb_throw(VALUE self, VALUE result) {
+ if (self != Qundef) rb_throw("foo", result);
+ return ID2SYM(rb_intern("rb_throw_failed"));
+}
+#endif
+
+#ifdef HAVE_RB_THROW_OBJ
+VALUE kernel_spec_rb_throw_obj(VALUE self, VALUE obj, VALUE result) {
+ if (self != Qundef) rb_throw_obj(obj, result);
+ return ID2SYM(rb_intern("rb_throw_failed"));
+}
+#endif
+
+#ifdef HAVE_RB_RESCUE
+VALUE kernel_spec_call_proc_with_raised_exc(VALUE arg_array, VALUE raised_exc) {
+ VALUE argv[2];
+ int argc;
+
+ VALUE arg = rb_ary_pop(arg_array);
+ VALUE proc = rb_ary_pop(arg_array);
+
+ argv[0] = arg;
+ argv[1] = raised_exc;
+
+ argc = 2;
+
+ return rb_funcall2(proc, rb_intern("call"), argc, argv);
+}
+
+VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg,
+ VALUE raise_proc, VALUE arg2) {
+ VALUE main_array, raise_array;
+
+ main_array = rb_ary_new();
+ rb_ary_push(main_array, main_proc);
+ rb_ary_push(main_array, arg);
+
+ raise_array = rb_ary_new();
+ rb_ary_push(raise_array, raise_proc);
+ rb_ary_push(raise_array, arg2);
+
+ return rb_rescue(kernel_spec_call_proc, main_array,
+ kernel_spec_call_proc_with_raised_exc, raise_array);
+}
+#endif
+
+#ifdef HAVE_RB_RESCUE2
+VALUE kernel_spec_rb_rescue2(int argc, VALUE *args, VALUE self) {
+ VALUE main_array, raise_array;
+
+ main_array = rb_ary_new();
+ rb_ary_push(main_array, args[0]);
+ rb_ary_push(main_array, args[1]);
+
+ raise_array = rb_ary_new();
+ rb_ary_push(raise_array, args[2]);
+ rb_ary_push(raise_array, args[3]);
+
+ return rb_rescue2(kernel_spec_call_proc, main_array,
+ kernel_spec_call_proc, raise_array, args[4], args[5], (VALUE)0);
+}
+#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;
+ if(msg == Qnil) {
+ rb_sys_fail(0);
+ } else if (self != Qundef) {
+ rb_sys_fail(StringValuePtr(msg));
+ }
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_SYSERR_FAIL
+VALUE kernel_spec_rb_syserr_fail(VALUE self, VALUE err, VALUE msg) {
+ if(msg == Qnil) {
+ rb_syserr_fail(NUM2INT(err), NULL);
+ } else if (self != Qundef) {
+ rb_syserr_fail(NUM2INT(err), StringValuePtr(msg));
+ }
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_WARN
+VALUE kernel_spec_rb_warn(VALUE self, VALUE msg) {
+ rb_warn("%s", StringValuePtr(msg));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_YIELD
+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
+static VALUE kernel_spec_rb_yield_splat(VALUE self, VALUE ary) {
+ return rb_yield_splat(ary);
+}
+#endif
+
+#ifdef HAVE_RB_YIELD_VALUES
+static VALUE kernel_spec_rb_yield_values(VALUE self, VALUE obj1, VALUE obj2) {
+ return rb_yield_values(2, obj1, obj2);
+}
+#endif
+
+#ifdef HAVE_RB_EXEC_RECURSIVE
+static VALUE do_rec(VALUE obj, VALUE arg, int is_rec) {
+ if(is_rec) {
+ return obj;
+ } else if(arg == Qtrue) {
+ return rb_exec_recursive(do_rec, obj, Qnil);
+ } else {
+ return Qnil;
+ }
+}
+
+static VALUE kernel_spec_rb_exec_recursive(VALUE self, VALUE obj) {
+ return rb_exec_recursive(do_rec, obj, Qtrue);
+}
+#endif
+
+#ifdef HAVE_RB_SET_END_PROC
+static void write_io(VALUE io) {
+ rb_funcall(io, rb_intern("write"), 1, rb_str_new2("e"));
+}
+
+static VALUE kernel_spec_rb_set_end_proc(VALUE self, VALUE io) {
+ rb_set_end_proc(write_io, io);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_F_SPRINTF
+static VALUE kernel_spec_rb_f_sprintf(VALUE self, VALUE ary) {
+ return rb_f_sprintf((int)RARRAY_LEN(ary), RARRAY_PTR(ary));
+}
+#endif
+
+#ifdef HAVE_RB_MAKE_BACKTRACE
+static VALUE kernel_spec_rb_make_backtrace(VALUE self) {
+ return rb_make_backtrace();
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_METHOD
+static VALUE kernel_spec_rb_obj_method(VALUE self, VALUE obj, VALUE method) {
+ return rb_obj_method(obj, method);
+}
+#endif
+
+#ifdef HAVE_RB_FUNCALL3
+static VALUE kernel_spec_rb_funcall3(VALUE self, VALUE obj, VALUE method) {
+ return rb_funcall3(obj, SYM2ID(method), 0, NULL);
+}
+#endif
+
+#ifdef HAVE_RB_FUNCALL_WITH_BLOCK
+static VALUE kernel_spec_rb_funcall_with_block(VALUE self, VALUE obj, VALUE method, VALUE block) {
+ return rb_funcall_with_block(obj, SYM2ID(method), 0, NULL, block);
+}
+#endif
+
+void Init_kernel_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiKernelSpecs", rb_cObject);
+
+#ifdef HAVE_RB_BLOCK_GIVEN_P
+ rb_define_method(cls, "rb_block_given_p", kernel_spec_rb_block_given_p, 0);
+#endif
+
+#ifdef HAVE_RB_NEED_BLOCK
+ rb_define_method(cls, "rb_need_block", kernel_spec_rb_need_block, 0);
+#endif
+
+#ifdef HAVE_RB_BLOCK_CALL
+ rb_define_method(cls, "rb_block_call", kernel_spec_rb_block_call, 1);
+ rb_define_method(cls, "rb_block_call_multi_arg", kernel_spec_rb_block_call_multi_arg, 1);
+ rb_define_method(cls, "rb_block_call_no_func", kernel_spec_rb_block_call_no_func, 1);
+#endif
+
+#ifdef HAVE_RB_BLOCK_PROC
+ rb_define_method(cls, "rb_block_proc", kernel_spec_rb_block_proc, 0);
+#endif
+
+#ifdef HAVE_RB_ENSURE
+ rb_define_method(cls, "rb_ensure", kernel_spec_rb_ensure, 4);
+#endif
+
+#ifdef HAVE_RB_EVAL_STRING
+ rb_define_method(cls, "rb_eval_string", kernel_spec_rb_eval_string, 1);
+#endif
+
+#ifdef HAVE_RB_RAISE
+ rb_define_method(cls, "rb_raise", kernel_spec_rb_raise, 1);
+#endif
+
+#ifdef HAVE_RB_THROW
+ rb_define_method(cls, "rb_throw", kernel_spec_rb_throw, 1);
+#endif
+
+#ifdef HAVE_RB_THROW_OBJ
+ rb_define_method(cls, "rb_throw_obj", kernel_spec_rb_throw_obj, 2);
+#endif
+
+#ifdef HAVE_RB_RESCUE
+ rb_define_method(cls, "rb_rescue", kernel_spec_rb_rescue, 4);
+#endif
+
+#ifdef HAVE_RB_RESCUE2
+ 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
+
+#ifdef HAVE_RB_CATCH_OBJ
+ rb_define_method(cls, "rb_catch_obj", kernel_spec_rb_catch_obj, 2);
+#endif
+
+#ifdef HAVE_RB_SYS_FAIL
+ rb_define_method(cls, "rb_sys_fail", kernel_spec_rb_sys_fail, 1);
+#endif
+
+#ifdef HAVE_RB_SYSERR_FAIL
+ rb_define_method(cls, "rb_syserr_fail", kernel_spec_rb_syserr_fail, 2);
+#endif
+
+#ifdef HAVE_RB_WARN
+ rb_define_method(cls, "rb_warn", kernel_spec_rb_warn, 1);
+#endif
+
+#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
+ rb_define_method(cls, "rb_yield_values", kernel_spec_rb_yield_values, 2);
+#endif
+
+#ifdef HAVE_RB_YIELD_SPLAT
+ rb_define_method(cls, "rb_yield_splat", kernel_spec_rb_yield_splat, 1);
+#endif
+
+#ifdef HAVE_RB_EXEC_RECURSIVE
+ rb_define_method(cls, "rb_exec_recursive", kernel_spec_rb_exec_recursive, 1);
+#endif
+
+#ifdef HAVE_RB_SET_END_PROC
+ rb_define_method(cls, "rb_set_end_proc", kernel_spec_rb_set_end_proc, 1);
+#endif
+
+#ifdef HAVE_RB_F_SPRINTF
+ rb_define_method(cls, "rb_f_sprintf", kernel_spec_rb_f_sprintf, 1);
+#endif
+
+#ifdef HAVE_RB_MAKE_BACKTRACE
+ rb_define_method(cls, "rb_make_backtrace", kernel_spec_rb_make_backtrace, 0);
+#endif
+
+#ifdef HAVE_RB_OBJ_METHOD
+ rb_define_method(cls, "rb_obj_method", kernel_spec_rb_obj_method, 2);
+#endif
+
+#ifdef HAVE_RB_FUNCALL3
+ rb_define_method(cls, "rb_funcall3", kernel_spec_rb_funcall3, 2);
+#endif
+
+#ifdef HAVE_RB_FUNCALL_WITH_BLOCK
+ rb_define_method(cls, "rb_funcall_with_block", kernel_spec_rb_funcall_with_block, 3);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/marshal_spec.c b/spec/ruby/optional/capi/ext/marshal_spec.c
new file mode 100644
index 0000000000..dbb6e71abf
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/marshal_spec.c
@@ -0,0 +1,36 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_MARSHAL_DUMP
+VALUE marshal_spec_rb_marshal_dump(VALUE self, VALUE obj, VALUE port) {
+ return rb_marshal_dump(obj, port);
+}
+#endif
+
+#ifdef HAVE_RB_MARSHAL_LOAD
+VALUE marshal_spec_rb_marshal_load(VALUE self, VALUE data) {
+ return rb_marshal_load(data);
+}
+#endif
+
+void Init_marshal_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiMarshalSpecs", rb_cObject);
+
+#ifdef HAVE_RB_MARSHAL_DUMP
+ rb_define_method(cls, "rb_marshal_dump", marshal_spec_rb_marshal_dump, 2);
+#endif
+
+#ifdef HAVE_RB_MARSHAL_LOAD
+ rb_define_method(cls, "rb_marshal_load", marshal_spec_rb_marshal_load, 1);
+#endif
+
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/module_spec.c b/spec/ruby/optional/capi/ext/module_spec.c
new file mode 100644
index 0000000000..275bc4da32
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/module_spec.c
@@ -0,0 +1,252 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static VALUE module_specs_test_method(VALUE self) {
+ return ID2SYM(rb_intern("test_method"));
+}
+
+#ifdef HAVE_RB_CONST_DEFINED
+static VALUE module_specs_const_defined(VALUE self, VALUE klass, VALUE id) {
+ return rb_const_defined(klass, SYM2ID(id)) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_CONST_DEFINED_AT
+static VALUE module_specs_const_defined_at(VALUE self, VALUE klass, VALUE id) {
+ return rb_const_defined_at(klass, SYM2ID(id)) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_CONST_GET
+static VALUE module_specs_const_get(VALUE self, VALUE klass, VALUE val) {
+ return rb_const_get(klass, SYM2ID(val));
+}
+#endif
+
+#ifdef HAVE_RB_CONST_GET_AT
+static VALUE module_specs_const_get_at(VALUE self, VALUE klass, VALUE val) {
+ return rb_const_get_at(klass, SYM2ID(val));
+}
+#endif
+
+#ifdef HAVE_RB_CONST_GET_FROM
+static VALUE module_specs_const_get_from(VALUE self, VALUE klass, VALUE val) {
+ return rb_const_get_from(klass, SYM2ID(val));
+}
+#endif
+
+#ifdef HAVE_RB_CONST_SET
+static VALUE module_specs_const_set(VALUE self, VALUE klass, VALUE name, VALUE val) {
+ rb_const_set(klass, SYM2ID(name), val);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_ALIAS
+static VALUE module_specs_rb_define_alias(VALUE self, VALUE obj,
+ VALUE new_name, VALUE old_name) {
+
+ rb_define_alias(obj, RSTRING_PTR(new_name), RSTRING_PTR(old_name));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_ALIAS
+static VALUE module_specs_rb_alias(VALUE self, VALUE obj,
+ VALUE new_name, VALUE old_name) {
+
+ rb_alias(obj, SYM2ID(new_name), SYM2ID(old_name));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_MODULE
+static VALUE module_specs_rb_define_module(VALUE self, VALUE name) {
+ return rb_define_module(RSTRING_PTR(name));
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_MODULE_UNDER
+static VALUE module_specs_rb_define_module_under(VALUE self, VALUE outer, VALUE name) {
+ return rb_define_module_under(outer, RSTRING_PTR(name));
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_CONST
+static VALUE module_specs_define_const(VALUE self, VALUE klass, VALUE str_name, VALUE val) {
+ rb_define_const(klass, RSTRING_PTR(str_name), val);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_GLOBAL_CONST
+static VALUE module_specs_define_global_const(VALUE self, VALUE str_name, VALUE obj) {
+ rb_define_global_const(RSTRING_PTR(str_name), obj);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_GLOBAL_FUNCTION
+static VALUE module_specs_rb_define_global_function(VALUE self, VALUE str_name) {
+ rb_define_global_function(RSTRING_PTR(str_name), module_specs_test_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_METHOD
+static VALUE module_specs_rb_define_method(VALUE self, VALUE cls, VALUE str_name) {
+ rb_define_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_MODULE_FUNCTION
+static VALUE module_specs_rb_define_module_function(VALUE self, VALUE cls, VALUE str_name) {
+ rb_define_module_function(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_PRIVATE_METHOD
+static VALUE module_specs_rb_define_private_method(VALUE self, VALUE cls, VALUE str_name) {
+ rb_define_private_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_PROTECTED_METHOD
+static VALUE module_specs_rb_define_protected_method(VALUE self, VALUE cls, VALUE str_name) {
+ rb_define_protected_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_DEFINE_SINGLETON_METHOD
+static VALUE module_specs_rb_define_singleton_method(VALUE self, VALUE cls, VALUE str_name) {
+ rb_define_singleton_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_UNDEF_METHOD
+static VALUE module_specs_rb_undef_method(VALUE self, VALUE cls, VALUE str_name) {
+ rb_undef_method(cls, RSTRING_PTR(str_name));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_UNDEF
+static VALUE module_specs_rb_undef(VALUE self, VALUE cls, VALUE symbol_name) {
+ rb_undef(cls, SYM2ID(symbol_name));
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_CLASS2NAME
+static VALUE module_specs_rbclass2name(VALUE self, VALUE klass) {
+ return rb_str_new2(rb_class2name(klass));
+}
+#endif
+
+void Init_module_spec(void) {
+ VALUE cls;
+
+ cls = rb_define_class("CApiModuleSpecs", rb_cObject);
+
+#ifdef HAVE_RB_CONST_DEFINED
+ rb_define_method(cls, "rb_const_defined", module_specs_const_defined, 2);
+#endif
+
+#ifdef HAVE_RB_CONST_DEFINED_AT
+ rb_define_method(cls, "rb_const_defined_at", module_specs_const_defined_at, 2);
+#endif
+
+#ifdef HAVE_RB_CONST_GET
+ rb_define_method(cls, "rb_const_get", module_specs_const_get, 2);
+#endif
+
+#ifdef HAVE_RB_CONST_GET_AT
+ rb_define_method(cls, "rb_const_get_at", module_specs_const_get_at, 2);
+#endif
+
+#ifdef HAVE_RB_CONST_GET_FROM
+ rb_define_method(cls, "rb_const_get_from", module_specs_const_get_from, 2);
+#endif
+
+#ifdef HAVE_RB_CONST_SET
+ rb_define_method(cls, "rb_const_set", module_specs_const_set, 3);
+#endif
+
+#ifdef HAVE_RB_DEFINE_ALIAS
+ rb_define_method(cls, "rb_define_alias", module_specs_rb_define_alias, 3);
+#endif
+
+#ifdef HAVE_RB_ALIAS
+ rb_define_method(cls, "rb_alias", module_specs_rb_alias, 3);
+#endif
+
+#ifdef HAVE_RB_DEFINE_MODULE
+ rb_define_method(cls, "rb_define_module", module_specs_rb_define_module, 1);
+#endif
+
+#ifdef HAVE_RB_DEFINE_MODULE_UNDER
+ rb_define_method(cls, "rb_define_module_under", module_specs_rb_define_module_under, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_CONST
+ rb_define_method(cls, "rb_define_const", module_specs_define_const, 3);
+#endif
+
+#ifdef HAVE_RB_DEFINE_GLOBAL_CONST
+ rb_define_method(cls, "rb_define_global_const", module_specs_define_global_const, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_GLOBAL_FUNCTION
+ rb_define_method(cls, "rb_define_global_function",
+ module_specs_rb_define_global_function, 1);
+#endif
+
+#ifdef HAVE_RB_DEFINE_METHOD
+ rb_define_method(cls, "rb_define_method", module_specs_rb_define_method, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_MODULE_FUNCTION
+ rb_define_method(cls, "rb_define_module_function",
+ module_specs_rb_define_module_function, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_PRIVATE_METHOD
+ rb_define_method(cls, "rb_define_private_method",
+ module_specs_rb_define_private_method, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_PROTECTED_METHOD
+ rb_define_method(cls, "rb_define_protected_method",
+ module_specs_rb_define_protected_method, 2);
+#endif
+
+#ifdef HAVE_RB_DEFINE_SINGLETON_METHOD
+ rb_define_method(cls, "rb_define_singleton_method",
+ module_specs_rb_define_singleton_method, 2);
+#endif
+
+#ifdef HAVE_RB_UNDEF_METHOD
+ rb_define_method(cls, "rb_undef_method", module_specs_rb_undef_method, 2);
+#endif
+
+#ifdef HAVE_RB_UNDEF
+ rb_define_method(cls, "rb_undef", module_specs_rb_undef, 2);
+#endif
+
+#ifdef HAVE_RB_CLASS2NAME
+ rb_define_method(cls, "rb_class2name", module_specs_rbclass2name, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/module_under_autoload_spec.c b/spec/ruby/optional/capi/ext/module_under_autoload_spec.c
new file mode 100644
index 0000000000..c8f19a287b
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/module_under_autoload_spec.c
@@ -0,0 +1,7 @@
+#include "ruby.h"
+
+void Init_module_under_autoload_spec(void) {
+ VALUE specs = rb_const_get(rb_cObject, rb_intern("CApiModuleSpecs"));
+ rb_define_module_under(specs, "ModuleUnderAutoload");
+ rb_define_module_under(specs, "RubyUnderAutoload");
+}
diff --git a/spec/ruby/optional/capi/ext/mutex_spec.c b/spec/ruby/optional/capi/ext/mutex_spec.c
new file mode 100644
index 0000000000..d5ce06e124
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/mutex_spec.c
@@ -0,0 +1,91 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_MUTEX_NEW
+VALUE mutex_spec_rb_mutex_new(VALUE self) {
+ return rb_mutex_new();
+}
+#endif
+
+#ifdef HAVE_RB_MUTEX_LOCKED_P
+VALUE mutex_spec_rb_mutex_locked_p(VALUE self, VALUE mutex) {
+ return rb_mutex_locked_p(mutex);
+}
+#endif
+
+#ifdef HAVE_RB_MUTEX_TRYLOCK
+VALUE mutex_spec_rb_mutex_trylock(VALUE self, VALUE mutex) {
+ return rb_mutex_trylock(mutex);
+}
+#endif
+
+#ifdef HAVE_RB_MUTEX_LOCK
+VALUE mutex_spec_rb_mutex_lock(VALUE self, VALUE mutex) {
+ return rb_mutex_lock(mutex);
+}
+#endif
+
+#ifdef HAVE_RB_MUTEX_UNLOCK
+VALUE mutex_spec_rb_mutex_unlock(VALUE self, VALUE mutex) {
+ return rb_mutex_unlock(mutex);
+}
+#endif
+
+#ifdef HAVE_RB_MUTEX_SLEEP
+VALUE mutex_spec_rb_mutex_sleep(VALUE self, VALUE mutex, VALUE timeout) {
+ return rb_mutex_sleep(mutex, timeout);
+}
+#endif
+
+#ifdef HAVE_RB_MUTEX_SYNCHRONIZE
+
+VALUE mutex_spec_rb_mutex_callback(VALUE arg) {
+ return rb_funcall(arg, rb_intern("call"), 0);
+}
+
+VALUE mutex_spec_rb_mutex_synchronize(VALUE self, VALUE mutex, VALUE value) {
+ return rb_mutex_synchronize(mutex, mutex_spec_rb_mutex_callback, value);
+}
+#endif
+
+void Init_mutex_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiMutexSpecs", rb_cObject);
+
+#ifdef HAVE_RB_MUTEX_NEW
+ rb_define_method(cls, "rb_mutex_new", mutex_spec_rb_mutex_new, 0);
+#endif
+
+#ifdef HAVE_RB_MUTEX_LOCKED_P
+ rb_define_method(cls, "rb_mutex_locked_p", mutex_spec_rb_mutex_locked_p, 1);
+#endif
+
+#ifdef HAVE_RB_MUTEX_TRYLOCK
+ rb_define_method(cls, "rb_mutex_trylock", mutex_spec_rb_mutex_trylock, 1);
+#endif
+
+#ifdef HAVE_RB_MUTEX_LOCK
+ rb_define_method(cls, "rb_mutex_lock", mutex_spec_rb_mutex_lock, 1);
+#endif
+
+#ifdef HAVE_RB_MUTEX_UNLOCK
+ rb_define_method(cls, "rb_mutex_unlock", mutex_spec_rb_mutex_unlock, 1);
+#endif
+
+#ifdef HAVE_RB_MUTEX_SLEEP
+ rb_define_method(cls, "rb_mutex_sleep", mutex_spec_rb_mutex_sleep, 2);
+#endif
+
+#ifdef HAVE_RB_MUTEX_SYNCHRONIZE
+ rb_define_method(cls, "rb_mutex_synchronize", mutex_spec_rb_mutex_synchronize, 2);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/spec/ruby/optional/capi/ext/numeric_spec.c b/spec/ruby/optional/capi/ext/numeric_spec.c
new file mode 100644
index 0000000000..cd643cc66f
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/numeric_spec.c
@@ -0,0 +1,166 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_NUM2CHR
+static VALUE numeric_spec_NUM2CHR(VALUE self, VALUE value) {
+ return INT2FIX(NUM2CHR(value));
+}
+#endif
+
+#ifdef HAVE_RB_INT2INUM
+static VALUE numeric_spec_rb_int2inum_14(VALUE self) {
+ return rb_int2inum(14);
+}
+#endif
+
+#ifdef HAVE_RB_INTEGER
+static VALUE numeric_spec_rb_Integer(VALUE self, VALUE str) {
+ return rb_Integer(str);
+}
+#endif
+
+#ifdef HAVE_RB_LL2INUM
+static VALUE numeric_spec_rb_ll2inum_14(VALUE self) {
+ return rb_ll2inum(14);
+}
+#endif
+
+#ifdef HAVE_RB_NUM2DBL
+static VALUE numeric_spec_rb_num2dbl(VALUE self, VALUE num) {
+ return rb_float_new(rb_num2dbl(num));
+}
+#endif
+
+#ifdef HAVE_RB_NUM2INT
+static VALUE numeric_spec_rb_num2int(VALUE self, VALUE num) {
+ return LONG2NUM(rb_num2int(num));
+}
+#endif
+
+#ifdef HAVE_RB_INT2NUM
+static VALUE numeric_spec_rb_int2num(VALUE self, VALUE num) {
+ return INT2NUM(rb_num2long(num));
+}
+#endif
+
+#ifdef HAVE_RB_NUM2LONG
+static VALUE numeric_spec_rb_num2long(VALUE self, VALUE num) {
+ return LONG2NUM(rb_num2long(num));
+}
+#endif
+
+#ifdef HAVE_RB_NUM2UINT
+static VALUE numeric_spec_rb_num2uint(VALUE self, VALUE num) {
+ return ULONG2NUM(rb_num2uint(num));
+}
+#endif
+
+#ifdef HAVE_RB_NUM2ULONG
+static VALUE numeric_spec_rb_num2ulong(VALUE self, VALUE num) {
+ return ULONG2NUM(rb_num2ulong(num));
+}
+#endif
+
+#ifdef HAVE_RB_NUM_ZERODIV
+static VALUE numeric_spec_rb_num_zerodiv(VALUE self) {
+ rb_num_zerodiv();
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_CMPINT
+static VALUE numeric_spec_rb_cmpint(VALUE self, VALUE val, VALUE b) {
+ return INT2FIX(rb_cmpint(val, val, b));
+}
+#endif
+
+#ifdef HAVE_RB_NUM_COERCE_BIN
+static VALUE numeric_spec_rb_num_coerce_bin(VALUE self, VALUE x, VALUE y, VALUE op) {
+ return rb_num_coerce_bin(x, y, SYM2ID(op));
+}
+#endif
+
+#ifdef HAVE_RB_NUM_COERCE_CMP
+static VALUE numeric_spec_rb_num_coerce_cmp(VALUE self, VALUE x, VALUE y, VALUE op) {
+ return rb_num_coerce_cmp(x, y, SYM2ID(op));
+}
+#endif
+
+#ifdef HAVE_RB_NUM_COERCE_RELOP
+static VALUE numeric_spec_rb_num_coerce_relop(VALUE self, VALUE x, VALUE y, VALUE op) {
+ return rb_num_coerce_relop(x, y, SYM2ID(op));
+}
+#endif
+
+void Init_numeric_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiNumericSpecs", rb_cObject);
+
+#ifdef HAVE_NUM2CHR
+ rb_define_method(cls, "NUM2CHR", numeric_spec_NUM2CHR, 1);
+#endif
+
+#ifdef HAVE_RB_INT2INUM
+ rb_define_method(cls, "rb_int2inum_14", numeric_spec_rb_int2inum_14, 0);
+#endif
+
+#ifdef HAVE_RB_INTEGER
+ rb_define_method(cls, "rb_Integer", numeric_spec_rb_Integer, 1);
+#endif
+
+#ifdef HAVE_RB_LL2INUM
+ rb_define_method(cls, "rb_ll2inum_14", numeric_spec_rb_ll2inum_14, 0);
+#endif
+
+#ifdef HAVE_RB_NUM2DBL
+ rb_define_method(cls, "rb_num2dbl", numeric_spec_rb_num2dbl, 1);
+#endif
+
+#ifdef HAVE_RB_NUM2INT
+ rb_define_method(cls, "rb_num2int", numeric_spec_rb_num2int, 1);
+#endif
+
+#ifdef HAVE_RB_NUM2LONG
+ rb_define_method(cls, "rb_num2long", numeric_spec_rb_num2long, 1);
+#endif
+
+#ifdef HAVE_RB_INT2NUM
+ rb_define_method(cls, "rb_int2num", numeric_spec_rb_int2num, 1);
+#endif
+
+#ifdef HAVE_RB_NUM2UINT
+ rb_define_method(cls, "rb_num2uint", numeric_spec_rb_num2uint, 1);
+#endif
+
+#ifdef HAVE_RB_NUM2ULONG
+ rb_define_method(cls, "rb_num2ulong", numeric_spec_rb_num2ulong, 1);
+#endif
+
+#ifdef HAVE_RB_NUM_ZERODIV
+ rb_define_method(cls, "rb_num_zerodiv", numeric_spec_rb_num_zerodiv, 0);
+#endif
+
+#ifdef HAVE_RB_CMPINT
+ rb_define_method(cls, "rb_cmpint", numeric_spec_rb_cmpint, 2);
+#endif
+
+#ifdef HAVE_RB_NUM_COERCE_BIN
+ rb_define_method(cls, "rb_num_coerce_bin", numeric_spec_rb_num_coerce_bin, 3);
+#endif
+
+#ifdef HAVE_RB_NUM_COERCE_CMP
+ rb_define_method(cls, "rb_num_coerce_cmp", numeric_spec_rb_num_coerce_cmp, 3);
+#endif
+
+#ifdef HAVE_RB_NUM_COERCE_RELOP
+ rb_define_method(cls, "rb_num_coerce_relop", numeric_spec_rb_num_coerce_relop, 3);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c
new file mode 100644
index 0000000000..ad1ebecc78
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/object_spec.c
@@ -0,0 +1,608 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_OBJ_TAINT
+static VALUE object_spec_OBJ_TAINT(VALUE self, VALUE obj) {
+ OBJ_TAINT(obj);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_OBJ_TAINTED
+static VALUE object_spec_OBJ_TAINTED(VALUE self, VALUE obj) {
+ return OBJ_TAINTED(obj) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_OBJ_INFECT
+static VALUE object_spec_OBJ_INFECT(VALUE self, VALUE host, VALUE source) {
+ OBJ_INFECT(host, source);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_ANY_TO_S
+static VALUE object_spec_rb_any_to_s(VALUE self, VALUE obj) {
+ return rb_any_to_s(obj);
+}
+#endif
+
+#ifdef HAVE_RB_ATTR_GET
+static VALUE so_attr_get(VALUE self, VALUE obj, VALUE attr) {
+ return rb_attr_get(obj, SYM2ID(attr));
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_INSTANCE_VARIABLES
+static VALUE object_spec_rb_obj_instance_variables(VALUE self, VALUE obj) {
+ return rb_obj_instance_variables(obj);
+}
+#endif
+
+#ifdef HAVE_RB_CHECK_ARRAY_TYPE
+static VALUE so_check_array_type(VALUE self, VALUE ary) {
+ return rb_check_array_type(ary);
+}
+#endif
+
+#ifdef HAVE_RB_CHECK_CONVERT_TYPE
+static VALUE so_check_convert_type(VALUE self, VALUE obj, VALUE klass, VALUE method) {
+ return rb_check_convert_type(obj, T_ARRAY, RSTRING_PTR(klass), RSTRING_PTR(method));
+}
+#endif
+
+#ifdef HAVE_RB_CHECK_TO_INTEGER
+static VALUE so_check_to_integer(VALUE self, VALUE obj, VALUE method) {
+ return rb_check_to_integer(obj, RSTRING_PTR(method));
+}
+#endif
+
+#ifdef HAVE_RB_CHECK_FROZEN
+static VALUE object_spec_rb_check_frozen(VALUE self, VALUE obj) {
+ rb_check_frozen(obj);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_CHECK_STRING_TYPE
+static VALUE so_check_string_type(VALUE self, VALUE str) {
+ return rb_check_string_type(str);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_OF
+static VALUE so_rbclassof(VALUE self, VALUE obj) {
+ return rb_class_of(obj);
+}
+#endif
+
+#ifdef HAVE_RB_CONVERT_TYPE
+static VALUE so_convert_type(VALUE self, VALUE obj, VALUE klass, VALUE method) {
+ return rb_convert_type(obj, T_ARRAY, RSTRING_PTR(klass), RSTRING_PTR(method));
+}
+#endif
+
+#ifdef HAVE_RB_EXTEND_OBJECT
+static VALUE object_spec_rb_extend_object(VALUE self, VALUE obj, VALUE mod) {
+ rb_extend_object(obj, mod);
+ return obj;
+}
+#endif
+
+#ifdef HAVE_RB_INSPECT
+static VALUE so_inspect(VALUE self, VALUE obj) {
+ return rb_inspect(obj);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_ALLOC
+static VALUE so_rb_obj_alloc(VALUE self, VALUE klass) {
+ return rb_obj_alloc(klass);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_DUP
+static VALUE so_rb_obj_dup(VALUE self, VALUE klass) {
+ return rb_obj_dup(klass);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_CALL_INIT
+static VALUE so_rb_obj_call_init(VALUE self, VALUE object,
+ VALUE nargs, VALUE args) {
+ int c_nargs = FIX2INT(nargs);
+ VALUE *c_args = alloca(sizeof(VALUE) * c_nargs);
+ int i;
+
+ for (i = 0; i < c_nargs; i++)
+ c_args[i] = rb_ary_entry(args, i);
+
+ rb_obj_call_init(object, c_nargs, c_args);
+
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_CLASSNAME
+static VALUE so_rbobjclassname(VALUE self, VALUE obj) {
+ return rb_str_new2(rb_obj_classname(obj));
+}
+
+#endif
+
+#ifdef HAVE_RB_OBJ_FREEZE
+static VALUE object_spec_rb_obj_freeze(VALUE self, VALUE obj) {
+ return rb_obj_freeze(obj);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_FROZEN_P
+static VALUE object_spec_rb_obj_frozen_p(VALUE self, VALUE obj) {
+ return rb_obj_frozen_p(obj);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_ID
+static VALUE object_spec_rb_obj_id(VALUE self, VALUE obj) {
+ return rb_obj_id(obj);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_IS_INSTANCE_OF
+static VALUE so_instance_of(VALUE self, VALUE obj, VALUE klass) {
+ return rb_obj_is_instance_of(obj, klass);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_IS_KIND_OF
+static VALUE so_kind_of(VALUE self, VALUE obj, VALUE klass) {
+ return rb_obj_is_kind_of(obj, klass);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_METHOD_ARITY
+static VALUE object_specs_rb_obj_method_arity(VALUE self, VALUE obj, VALUE mid) {
+ return INT2FIX(rb_obj_method_arity(obj, SYM2ID(mid)));
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_TAINT
+static VALUE object_spec_rb_obj_taint(VALUE self, VALUE obj) {
+ return rb_obj_taint(obj);
+}
+#endif
+
+#ifdef HAVE_RB_REQUIRE
+static VALUE so_require(VALUE self) {
+ rb_require("fixtures/foo");
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_RESPOND_TO
+static VALUE so_respond_to(VALUE self, VALUE obj, VALUE sym) {
+ return rb_respond_to(obj, SYM2ID(sym)) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_RESPOND_TO
+static VALUE so_obj_respond_to(VALUE self, VALUE obj, VALUE sym, VALUE priv) {
+ return rb_obj_respond_to(obj, SYM2ID(sym), priv == Qtrue ? 1 : 0) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_METHOD_BOUNDP
+static VALUE object_spec_rb_method_boundp(VALUE self, VALUE obj, VALUE method, VALUE exclude_private) {
+ ID id = SYM2ID(method);
+ return rb_method_boundp(obj, id, exclude_private == Qtrue ? 1 : 0) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_SPECIAL_CONST_P
+static VALUE object_spec_rb_special_const_p(VALUE self, VALUE value) {
+ return rb_special_const_p(value);
+}
+#endif
+
+#ifdef HAVE_RB_TO_ID
+static VALUE so_to_id(VALUE self, VALUE obj) {
+ return ID2SYM(rb_to_id(obj));
+}
+#endif
+
+#ifdef HAVE_RTEST
+static VALUE object_spec_RTEST(VALUE self, VALUE value) {
+ return RTEST(value) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_TYPE
+static VALUE so_is_type_nil(VALUE self, VALUE obj) {
+ if(TYPE(obj) == T_NIL) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_type_object(VALUE self, VALUE obj) {
+ if(TYPE(obj) == T_OBJECT) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_type_array(VALUE self, VALUE obj) {
+ if(TYPE(obj) == T_ARRAY) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_type_module(VALUE self, VALUE obj) {
+ if(TYPE(obj) == T_MODULE) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_type_class(VALUE self, VALUE obj) {
+ if(TYPE(obj) == T_CLASS) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_type_data(VALUE self, VALUE obj) {
+ if(TYPE(obj) == T_DATA) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_TYPE_P
+static VALUE so_is_rb_type_p_nil(VALUE self, VALUE obj) {
+ if(rb_type_p(obj, T_NIL)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_rb_type_p_object(VALUE self, VALUE obj) {
+ if(rb_type_p(obj, T_OBJECT)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_rb_type_p_array(VALUE self, VALUE obj) {
+ if(rb_type_p(obj, T_ARRAY)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_rb_type_p_module(VALUE self, VALUE obj) {
+ if(rb_type_p(obj, T_MODULE)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_rb_type_p_class(VALUE self, VALUE obj) {
+ if(rb_type_p(obj, T_CLASS)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_rb_type_p_data(VALUE self, VALUE obj) {
+ if(rb_type_p(obj, T_DATA)) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+#endif
+
+#ifdef HAVE_BUILTIN_TYPE
+static VALUE so_is_builtin_type_object(VALUE self, VALUE obj) {
+ if(BUILTIN_TYPE(obj) == T_OBJECT) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_builtin_type_array(VALUE self, VALUE obj) {
+ if(BUILTIN_TYPE(obj) == T_ARRAY) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_builtin_type_module(VALUE self, VALUE obj) {
+ if(BUILTIN_TYPE(obj) == T_MODULE) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_builtin_type_class(VALUE self, VALUE obj) {
+ if(BUILTIN_TYPE(obj) == T_CLASS) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
+static VALUE so_is_builtin_type_data(VALUE self, VALUE obj) {
+ if(BUILTIN_TYPE(obj) == T_DATA) {
+ return Qtrue;
+ }
+ return Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_TO_INT
+static VALUE object_spec_rb_to_int(VALUE self, VALUE obj) {
+ return rb_to_int(obj);
+}
+#endif
+
+#ifdef HAVE_RB_OBJ_INSTANCE_EVAL
+static VALUE object_spec_rb_obj_instance_eval(VALUE self, VALUE obj) {
+ return rb_obj_instance_eval(0, NULL, obj);
+}
+#endif
+
+#ifdef HAVE_RB_IV_GET
+static VALUE object_spec_rb_iv_get(VALUE self, VALUE obj, VALUE name) {
+ return rb_iv_get(obj, RSTRING_PTR(name));
+}
+#endif
+
+#ifdef HAVE_RB_IV_SET
+static VALUE object_spec_rb_iv_set(VALUE self, VALUE obj, VALUE name, VALUE value) {
+ return rb_iv_set(obj, RSTRING_PTR(name), value);
+}
+#endif
+
+#ifdef HAVE_RB_IVAR_GET
+static VALUE object_spec_rb_ivar_get(VALUE self, VALUE obj, VALUE sym_name) {
+ return rb_ivar_get(obj, SYM2ID(sym_name));
+}
+#endif
+
+#ifdef HAVE_RB_IVAR_SET
+static VALUE object_spec_rb_ivar_set(VALUE self, VALUE obj, VALUE sym_name, VALUE value) {
+ return rb_ivar_set(obj, SYM2ID(sym_name), value);
+}
+#endif
+
+#ifdef HAVE_RB_IVAR_DEFINED
+static VALUE object_spec_rb_ivar_defined(VALUE self, VALUE obj, VALUE sym_name) {
+ return rb_ivar_defined(obj, SYM2ID(sym_name));
+}
+#endif
+
+#ifdef HAVE_RB_EQUAL
+static VALUE object_spec_rb_equal(VALUE self, VALUE a, VALUE b) {
+ return rb_equal(a, b);
+}
+#endif
+
+#ifdef HAVE_RB_CLASS_INHERITED_P
+static VALUE object_spec_rb_class_inherited_p(VALUE self, VALUE mod, VALUE arg) {
+ return rb_class_inherited_p(mod, arg);
+}
+#endif
+
+
+void Init_object_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiObjectSpecs", rb_cObject);
+
+#ifdef HAVE_OBJ_TAINT
+ rb_define_method(cls, "OBJ_TAINT", object_spec_OBJ_TAINT, 1);
+#endif
+
+#ifdef HAVE_OBJ_TAINTED
+ rb_define_method(cls, "OBJ_TAINTED", object_spec_OBJ_TAINTED, 1);
+#endif
+
+#ifdef HAVE_OBJ_INFECT
+ rb_define_method(cls, "OBJ_INFECT", object_spec_OBJ_INFECT, 2);
+#endif
+
+#ifdef HAVE_RB_ANY_TO_S
+ rb_define_method(cls, "rb_any_to_s", object_spec_rb_any_to_s, 1);
+#endif
+
+#ifdef HAVE_RB_ATTR_GET
+ rb_define_method(cls, "rb_attr_get", so_attr_get, 2);
+#endif
+
+#ifdef HAVE_RB_OBJ_INSTANCE_VARIABLES
+ rb_define_method(cls, "rb_obj_instance_variables", object_spec_rb_obj_instance_variables, 1);
+#endif
+
+#ifdef HAVE_RB_CHECK_ARRAY_TYPE
+ rb_define_method(cls, "rb_check_array_type", so_check_array_type, 1);
+#endif
+
+#ifdef HAVE_RB_CHECK_CONVERT_TYPE
+ rb_define_method(cls, "rb_check_convert_type", so_check_convert_type, 3);
+#endif
+
+#ifdef HAVE_RB_CHECK_TO_INTEGER
+ rb_define_method(cls, "rb_check_to_integer", so_check_to_integer, 2);
+#endif
+
+#ifdef HAVE_RB_CHECK_FROZEN
+ rb_define_method(cls, "rb_check_frozen", object_spec_rb_check_frozen, 1);
+#endif
+
+#ifdef HAVE_RB_CHECK_STRING_TYPE
+ rb_define_method(cls, "rb_check_string_type", so_check_string_type, 1);
+#endif
+
+#ifdef HAVE_RB_CLASS_OF
+ rb_define_method(cls, "rb_class_of", so_rbclassof, 1);
+#endif
+
+#ifdef HAVE_RB_CONVERT_TYPE
+ rb_define_method(cls, "rb_convert_type", so_convert_type, 3);
+#endif
+
+#ifdef HAVE_RB_EXTEND_OBJECT
+ rb_define_method(cls, "rb_extend_object", object_spec_rb_extend_object, 2);
+#endif
+
+#ifdef HAVE_RB_INSPECT
+ rb_define_method(cls, "rb_inspect", so_inspect, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_ALLOC
+ rb_define_method(cls, "rb_obj_alloc", so_rb_obj_alloc, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_ALLOC
+ rb_define_method(cls, "rb_obj_dup", so_rb_obj_dup, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_CALL_INIT
+ rb_define_method(cls, "rb_obj_call_init", so_rb_obj_call_init, 3);
+#endif
+
+#ifdef HAVE_RB_OBJ_CLASSNAME
+ rb_define_method(cls, "rb_obj_classname", so_rbobjclassname, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_FREEZE
+ rb_define_method(cls, "rb_obj_freeze", object_spec_rb_obj_freeze, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_FROZEN_P
+ rb_define_method(cls, "rb_obj_frozen_p", object_spec_rb_obj_frozen_p, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_ID
+ rb_define_method(cls, "rb_obj_id", object_spec_rb_obj_id, 1);
+#endif
+
+#ifdef HAVE_RB_OBJ_IS_INSTANCE_OF
+ rb_define_method(cls, "rb_obj_is_instance_of", so_instance_of, 2);
+#endif
+
+#ifdef HAVE_RB_OBJ_IS_KIND_OF
+ rb_define_method(cls, "rb_obj_is_kind_of", so_kind_of, 2);
+#endif
+
+#ifdef HAVE_RB_OBJ_METHOD_ARITY
+ rb_define_method(cls, "rb_obj_method_arity", object_specs_rb_obj_method_arity, 2);
+#endif
+
+#ifdef HAVE_RB_OBJ_TAINT
+ rb_define_method(cls, "rb_obj_taint", object_spec_rb_obj_taint, 1);
+#endif
+
+#ifdef HAVE_RB_REQUIRE
+ rb_define_method(cls, "rb_require", so_require, 0);
+#endif
+
+#ifdef HAVE_RB_RESPOND_TO
+ rb_define_method(cls, "rb_respond_to", so_respond_to, 2);
+#endif
+
+#ifdef HAVE_RB_METHOD_BOUNDP
+ rb_define_method(cls, "rb_method_boundp", object_spec_rb_method_boundp, 3);
+#endif
+
+#ifdef HAVE_RB_OBJ_RESPOND_TO
+ rb_define_method(cls, "rb_obj_respond_to", so_obj_respond_to, 3);
+#endif
+
+#ifdef HAVE_RB_SPECIAL_CONST_P
+ rb_define_method(cls, "rb_special_const_p", object_spec_rb_special_const_p, 1);
+#endif
+
+#ifdef HAVE_RB_STR_NEW2
+#endif
+
+#ifdef HAVE_RB_TO_ID
+ rb_define_method(cls, "rb_to_id", so_to_id, 1);
+#endif
+
+#ifdef HAVE_RTEST
+ rb_define_method(cls, "RTEST", object_spec_RTEST, 1);
+#endif
+
+#ifdef HAVE_TYPE
+ rb_define_method(cls, "rb_is_type_nil", so_is_type_nil, 1);
+ rb_define_method(cls, "rb_is_type_object", so_is_type_object, 1);
+ rb_define_method(cls, "rb_is_type_array", so_is_type_array, 1);
+ rb_define_method(cls, "rb_is_type_module", so_is_type_module, 1);
+ rb_define_method(cls, "rb_is_type_class", so_is_type_class, 1);
+ rb_define_method(cls, "rb_is_type_data", so_is_type_data, 1);
+#endif
+
+#ifdef HAVE_RB_TYPE_P
+ rb_define_method(cls, "rb_is_rb_type_p_nil", so_is_rb_type_p_nil, 1);
+ rb_define_method(cls, "rb_is_rb_type_p_object", so_is_rb_type_p_object, 1);
+ rb_define_method(cls, "rb_is_rb_type_p_array", so_is_rb_type_p_array, 1);
+ rb_define_method(cls, "rb_is_rb_type_p_module", so_is_rb_type_p_module, 1);
+ rb_define_method(cls, "rb_is_rb_type_p_class", so_is_rb_type_p_class, 1);
+ rb_define_method(cls, "rb_is_rb_type_p_data", so_is_rb_type_p_data, 1);
+#endif
+
+#ifdef HAVE_BUILTIN_TYPE
+ rb_define_method(cls, "rb_is_builtin_type_object", so_is_builtin_type_object, 1);
+ rb_define_method(cls, "rb_is_builtin_type_array", so_is_builtin_type_array, 1);
+ rb_define_method(cls, "rb_is_builtin_type_module", so_is_builtin_type_module, 1);
+ rb_define_method(cls, "rb_is_builtin_type_class", so_is_builtin_type_class, 1);
+ rb_define_method(cls, "rb_is_builtin_type_data", so_is_builtin_type_data, 1);
+#endif
+
+#ifdef HAVE_RB_TO_INT
+ rb_define_method(cls, "rb_to_int", object_spec_rb_to_int, 1);
+#endif
+
+#ifdef HAVE_RB_EQUAL
+ rb_define_method(cls, "rb_equal", object_spec_rb_equal, 2);
+#endif
+
+#ifdef HAVE_RB_CLASS_INHERITED_P
+ rb_define_method(cls, "rb_class_inherited_p", object_spec_rb_class_inherited_p, 2);
+#endif
+
+#ifdef HAVE_RB_OBJ_INSTANCE_EVAL
+ rb_define_method(cls, "rb_obj_instance_eval", object_spec_rb_obj_instance_eval, 1);
+#endif
+
+#ifdef HAVE_RB_IV_GET
+ rb_define_method(cls, "rb_iv_get", object_spec_rb_iv_get, 2);
+#endif
+
+#ifdef HAVE_RB_IV_SET
+ rb_define_method(cls, "rb_iv_set", object_spec_rb_iv_set, 3);
+#endif
+
+#ifdef HAVE_RB_IVAR_GET
+ rb_define_method(cls, "rb_ivar_get", object_spec_rb_ivar_get, 2);
+#endif
+
+#ifdef HAVE_RB_IVAR_SET
+ rb_define_method(cls, "rb_ivar_set", object_spec_rb_ivar_set, 3);
+#endif
+
+#ifdef HAVE_RB_IVAR_DEFINED
+ rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2);
+#endif
+
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/proc_spec.c b/spec/ruby/optional/capi/ext/proc_spec.c
new file mode 100644
index 0000000000..f9c0f6b1b9
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/proc_spec.c
@@ -0,0 +1,85 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_PROC_NEW
+VALUE proc_spec_rb_proc_new_function(VALUE args) {
+ return rb_funcall(args, rb_intern("inspect"), 0);
+}
+
+VALUE proc_spec_rb_proc_new(VALUE self) {
+ return rb_proc_new(proc_spec_rb_proc_new_function, Qnil);
+}
+#endif
+
+#ifdef HAVE_RB_PROC_ARITY
+VALUE proc_spec_rb_proc_arity(VALUE self, VALUE prc) {
+ return INT2FIX(rb_proc_arity(prc));
+}
+#endif
+
+#ifdef HAVE_RB_PROC_CALL
+VALUE proc_spec_rb_proc_call(VALUE self, VALUE prc, VALUE args) {
+ return rb_proc_call(prc, args);
+}
+#endif
+
+/* This helper is not strictly necessary but reflects the code in wxRuby that
+ * originally exposed issues with this Proc.new behavior.
+ */
+VALUE proc_spec_rb_Proc_new_helper(void) {
+ return rb_funcall(rb_cProc, rb_intern("new"), 0);
+}
+
+VALUE proc_spec_rb_Proc_new(VALUE self, VALUE scenario) {
+ switch(FIX2INT(scenario)) {
+ case 0:
+ return proc_spec_rb_Proc_new_helper();
+ case 1:
+ rb_funcall(self, rb_intern("call_nothing"), 0);
+ return proc_spec_rb_Proc_new_helper();
+ case 2:
+ return rb_funcall(self, rb_intern("call_Proc_new"), 0);
+ case 3:
+ return rb_funcall(self, rb_intern("call_rb_Proc_new"), 0);
+ case 4:
+ return rb_funcall(self, rb_intern("call_rb_Proc_new_with_block"), 0);
+ case 5:
+ rb_funcall(self, rb_intern("call_rb_Proc_new_with_block"), 0);
+ return proc_spec_rb_Proc_new_helper();
+ case 6:
+ return rb_funcall(self, rb_intern("call_block_given?"), 0);
+ default:
+ rb_raise(rb_eException, "invalid scenario");
+ }
+
+ return Qnil;
+}
+
+void Init_proc_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiProcSpecs", rb_cObject);
+
+#ifdef HAVE_RB_PROC_NEW
+ rb_define_method(cls, "rb_proc_new", proc_spec_rb_proc_new, 0);
+#endif
+
+#ifdef HAVE_RB_PROC_ARITY
+ rb_define_method(cls, "rb_proc_arity", proc_spec_rb_proc_arity, 1);
+#endif
+
+#ifdef HAVE_RB_PROC_CALL
+ rb_define_method(cls, "rb_proc_call", proc_spec_rb_proc_call, 2);
+#endif
+
+ rb_define_method(cls, "rb_Proc_new", proc_spec_rb_Proc_new, 1);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/range_spec.c b/spec/ruby/optional/capi/ext/range_spec.c
new file mode 100644
index 0000000000..6dc2d579fd
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/range_spec.c
@@ -0,0 +1,66 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_RANGE_NEW
+VALUE range_spec_rb_range_new(int argc, VALUE* argv, VALUE self) {
+ int exclude_end = 0;
+ if(argc == 3) {
+ exclude_end = RTEST(argv[2]);
+ }
+ return rb_range_new(argv[0], argv[1], exclude_end);
+}
+#endif
+
+#ifdef HAVE_RB_RANGE_VALUES
+VALUE range_spec_rb_range_values(VALUE self, VALUE range) {
+ VALUE beg;
+ VALUE end;
+ int excl;
+ VALUE ary = rb_ary_new();
+ rb_range_values(range, &beg, &end, &excl);
+ rb_ary_store(ary, 0, beg);
+ rb_ary_store(ary, 1, end);
+ rb_ary_store(ary, 2, excl ? Qtrue : Qfalse);
+ return ary;
+}
+#endif
+
+#ifdef HAVE_RB_RANGE_BEG_LEN
+VALUE range_spec_rb_range_beg_len(VALUE self, VALUE range, VALUE begpv, VALUE lenpv, VALUE lenv, VALUE errv) {
+ long begp = FIX2LONG(begpv);
+ long lenp = FIX2LONG(lenpv);
+ long len = FIX2LONG(lenv);
+ int err = FIX2INT(errv);
+ VALUE ary = rb_ary_new();
+ VALUE res = rb_range_beg_len(range, &begp, &lenp, len, err);
+ rb_ary_store(ary, 0, LONG2FIX(begp));
+ rb_ary_store(ary, 1, LONG2FIX(lenp));
+ rb_ary_store(ary, 2, res);
+ return ary;
+}
+#endif
+
+void Init_range_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiRangeSpecs", rb_cObject);
+
+#ifdef HAVE_RB_RANGE_NEW
+ rb_define_method(cls, "rb_range_new", range_spec_rb_range_new, -1);
+#endif
+
+#ifdef HAVE_RB_RANGE_VALUES
+ rb_define_method(cls, "rb_range_values", range_spec_rb_range_values, 1);
+#endif
+
+#ifdef HAVE_RB_RANGE_BEG_LEN
+ rb_define_method(cls, "rb_range_beg_len", range_spec_rb_range_beg_len, 5);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/rational_spec.c b/spec/ruby/optional/capi/ext/rational_spec.c
new file mode 100644
index 0000000000..9f349261a0
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/rational_spec.c
@@ -0,0 +1,95 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_RATIONAL
+static VALUE rational_spec_rb_Rational(VALUE self, VALUE num, VALUE den) {
+ return rb_Rational(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL1
+static VALUE rational_spec_rb_Rational1(VALUE self, VALUE num) {
+ return rb_Rational1(num);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL2
+static VALUE rational_spec_rb_Rational2(VALUE self, VALUE num, VALUE den) {
+ return rb_Rational2(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NEW
+static VALUE rational_spec_rb_rational_new(VALUE self, VALUE num, VALUE den) {
+ return rb_rational_new(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NEW1
+static VALUE rational_spec_rb_rational_new1(VALUE self, VALUE num) {
+ return rb_rational_new1(num);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NEW2
+static VALUE rational_spec_rb_rational_new2(VALUE self, VALUE num, VALUE den) {
+ return rb_rational_new2(num, den);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NUM
+static VALUE rational_spec_rb_rational_num(VALUE self, VALUE rational) {
+ return rb_rational_num(rational);
+}
+#endif
+
+#ifdef HAVE_RB_RATIONAL_DEN
+static VALUE rational_spec_rb_rational_den(VALUE self, VALUE rational) {
+ return rb_rational_den(rational);
+}
+#endif
+
+void Init_rational_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiRationalSpecs", rb_cObject);
+
+#ifdef HAVE_RB_RATIONAL
+ rb_define_method(cls, "rb_Rational", rational_spec_rb_Rational, 2);
+#endif
+
+#ifdef HAVE_RB_RATIONAL1
+ rb_define_method(cls, "rb_Rational1", rational_spec_rb_Rational1, 1);
+#endif
+
+#ifdef HAVE_RB_RATIONAL2
+ rb_define_method(cls, "rb_Rational2", rational_spec_rb_Rational2, 2);
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NEW
+ rb_define_method(cls, "rb_rational_new", rational_spec_rb_rational_new, 2);
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NEW1
+ rb_define_method(cls, "rb_rational_new1", rational_spec_rb_rational_new1, 1);
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NEW2
+ rb_define_method(cls, "rb_rational_new2", rational_spec_rb_rational_new2, 2);
+#endif
+
+#ifdef HAVE_RB_RATIONAL_NUM
+ rb_define_method(cls, "rb_rational_num", rational_spec_rb_rational_num, 1);
+#endif
+
+#ifdef HAVE_RB_RATIONAL_DEN
+ rb_define_method(cls, "rb_rational_den", rational_spec_rb_rational_den, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/regexp_spec.c b/spec/ruby/optional/capi/ext/regexp_spec.c
new file mode 100644
index 0000000000..1058293444
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/regexp_spec.c
@@ -0,0 +1,84 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include "ruby/re.h"
+
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_REG_NEW
+VALUE regexp_spec_re(VALUE self) {
+ return rb_reg_new("a", 1, 0);
+}
+#endif
+
+#ifdef HAVE_RB_REG_NTH_MATCH
+VALUE regexp_spec_reg_1st_match(VALUE self, VALUE md) {
+ return rb_reg_nth_match(1, md);
+}
+#endif
+
+#ifdef HAVE_RB_REG_OPTIONS
+VALUE regexp_spec_rb_reg_options(VALUE self, VALUE regexp) {
+ return INT2FIX(rb_reg_options(regexp));
+}
+#endif
+
+#ifdef HAVE_RB_REG_REGCOMP
+VALUE regexp_spec_rb_reg_regcomp(VALUE self, VALUE str) {
+ return rb_reg_regcomp(str);
+}
+#endif
+
+#ifdef HAVE_RB_REG_MATCH
+VALUE regexp_spec_reg_match(VALUE self, VALUE re, VALUE str) {
+ return rb_reg_match(re, str);
+}
+#endif
+
+#ifdef HAVE_RB_BACKREF_GET
+VALUE regexp_spec_backref_get(VALUE self) {
+ return rb_backref_get();
+}
+#endif
+
+VALUE regexp_spec_match(VALUE self, VALUE regexp, VALUE str) {
+ return rb_funcall(regexp, rb_intern("match"), 1, str);
+}
+
+void Init_regexp_spec(void) {
+ VALUE cls = rb_define_class("CApiRegexpSpecs", rb_cObject);
+
+ rb_define_method(cls, "match", regexp_spec_match, 2);
+
+#ifdef HAVE_RB_REG_NEW
+ rb_define_method(cls, "a_re", regexp_spec_re, 0);
+#endif
+
+#ifdef HAVE_RB_REG_NTH_MATCH
+ rb_define_method(cls, "a_re_1st_match", regexp_spec_reg_1st_match, 1);
+#endif
+
+#ifdef HAVE_RB_REG_MATCH
+ rb_define_method(cls, "rb_reg_match", regexp_spec_reg_match, 2);
+#endif
+
+#ifdef HAVE_RB_BACKREF_GET
+ rb_define_method(cls, "rb_backref_get", regexp_spec_backref_get, 0);
+#endif
+
+#ifdef HAVE_RB_REG_OPTIONS
+ rb_define_method(cls, "rb_reg_options", regexp_spec_rb_reg_options, 1);
+#endif
+
+#ifdef HAVE_RB_REG_REGCOMP
+ rb_define_method(cls, "rb_reg_regcomp", regexp_spec_rb_reg_regcomp, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h
new file mode 100644
index 0000000000..8866e39af4
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/rubyspec.h
@@ -0,0 +1,608 @@
+#ifndef RUBYSPEC_H
+#define RUBYSPEC_H
+
+/* Define convenience macros similar to the mspec guards to assist
+ * with version incompatibilities.
+ */
+
+#include <ruby.h>
+#ifdef HAVE_RUBY_VERSION_H
+# include <ruby/version.h>
+#else
+# include <version.h>
+#endif
+
+#ifndef RUBY_VERSION_MAJOR
+#define RUBY_VERSION_MAJOR RUBY_API_VERSION_MAJOR
+#define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
+#define RUBY_VERSION_TEENY RUBY_API_VERSION_TEENY
+#endif
+
+#define RUBY_VERSION_BEFORE(major,minor,teeny) \
+ ((RUBY_VERSION_MAJOR < (major)) || \
+ (RUBY_VERSION_MAJOR == (major) && RUBY_VERSION_MINOR < (minor)) || \
+ (RUBY_VERSION_MAJOR == (major) && RUBY_VERSION_MINOR == (minor) && RUBY_VERSION_TEENY < (teeny)))
+
+#if RUBY_VERSION_MAJOR > 2 || (RUBY_VERSION_MAJOR == 2 && RUBY_VERSION_MINOR >= 4)
+#define RUBY_VERSION_IS_2_4
+#endif
+
+#if RUBY_VERSION_MAJOR > 2 || (RUBY_VERSION_MAJOR == 2 && RUBY_VERSION_MINOR >= 3)
+#define RUBY_VERSION_IS_2_3
+#endif
+
+/* Define all function flags */
+
+/* Array */
+#define HAVE_RB_ARRAY 1
+#define HAVE_RARRAY_AREF 1
+#define HAVE_RARRAY_LEN 1
+#define HAVE_RARRAY_PTR 1
+#define HAVE_RB_ARY_AREF 1
+#define HAVE_RB_ARY_CLEAR 1
+#define HAVE_RB_ARY_DELETE 1
+#define HAVE_RB_ARY_DELETE_AT 1
+#define HAVE_RB_ARY_DUP 1
+#define HAVE_RB_ARY_ENTRY 1
+#define HAVE_RB_ARY_FREEZE 1
+#define HAVE_RB_ARY_INCLUDES 1
+#define HAVE_RB_ARY_JOIN 1
+#define HAVE_RB_ARY_NEW 1
+#define HAVE_RB_ARY_NEW2 1
+#define HAVE_RB_ARY_NEW_CAPA 1
+#define HAVE_RB_ARY_NEW3 1
+#define HAVE_RB_ARY_NEW_FROM_ARGS 1
+#define HAVE_RB_ARY_NEW4 1
+#define HAVE_RB_ARY_NEW_FROM_VALUES 1
+#define HAVE_RB_ARY_POP 1
+#define HAVE_RB_ARY_PUSH 1
+#define HAVE_RB_ARY_CAT 1
+#define HAVE_RB_ARY_REVERSE 1
+#define HAVE_RB_ARY_ROTATE 1
+#define HAVE_RB_ARY_SHIFT 1
+#define HAVE_RB_ARY_STORE 1
+#define HAVE_RB_ARY_CONCAT 1
+#define HAVE_RB_ARY_PLUS 1
+#define HAVE_RB_ARY_TO_ARY 1
+#define HAVE_RB_ARY_SUBSEQ 1
+#define HAVE_RB_ARY_TO_S 1
+#define HAVE_RB_ARY_UNSHIFT 1
+#define HAVE_RB_ASSOC_NEW 1
+
+#define HAVE_RB_EACH 1
+#define HAVE_RB_ITERATE 1
+#define HAVE_RB_MEM_CLEAR 1
+
+/* Bignum */
+#define HAVE_ABSINT_SIZE 1
+#define HAVE_RB_BIG2DBL 1
+#define HAVE_RB_DBL2BIG 1
+#define HAVE_RB_BIG2LL 1
+#define HAVE_RB_BIG2LONG 1
+#define HAVE_RB_BIG2STR 1
+#define HAVE_RB_BIG2ULONG 1
+#define HAVE_RB_BIG_CMP 1
+#define HAVE_RB_BIG_PACK 1
+
+/* Class */
+#define HAVE_RB_CALL_SUPER 1
+#define HAVE_RB_CLASS2NAME 1
+#define HAVE_RB_CLASS_NAME 1
+#define HAVE_RB_CLASS_NEW 1
+#define HAVE_RB_CLASS_NEW_INSTANCE 1
+#define HAVE_RB_CLASS_PATH 1
+#define HAVE_RB_CLASS_REAL 1
+#define HAVE_RB_CVAR_DEFINED 1
+#define HAVE_RB_CVAR_GET 1
+#define HAVE_RB_CVAR_SET 1
+#define HAVE_RB_CV_GET 1
+#define HAVE_RB_CV_SET 1
+#define HAVE_RB_DEFINE_ATTR 1
+#define HAVE_RB_DEFINE_CLASS_VARIABLE 1
+#define HAVE_RB_INCLUDE_MODULE 1
+#define HAVE_RB_PATH2CLASS 1
+#define HAVE_RB_PATH_TO_CLASS 1
+#define HAVE_RB_CLASS_SUPERCLASS 1
+
+/* Complex */
+#define HAVE_RB_COMPLEX 1
+#define HAVE_RB_COMPLEX1 1
+#define HAVE_RB_COMPLEX2 1
+#define HAVE_RB_COMPLEX_NEW 1
+#define HAVE_RB_COMPLEX_NEW1 1
+#define HAVE_RB_COMPLEX_NEW2 1
+
+/* Constants */
+#define HAVE_RB_CARRAY 1
+#ifndef RUBY_INTEGER_UNIFICATION
+#define HAVE_RB_CBIGNUM 1
+#endif
+#define HAVE_RB_CCLASS 1
+#define HAVE_RB_CDATA 1
+#define HAVE_RB_CFALSECLASS 1
+#define HAVE_RB_CFILE 1
+#ifndef RUBY_INTEGER_UNIFICATION
+#define HAVE_RB_CFIXNUM 1
+#endif
+#define HAVE_RB_CFLOAT 1
+#define HAVE_RB_CHASH 1
+#define HAVE_RB_CINTEGER 1
+#define HAVE_RB_CIO 1
+#define HAVE_RB_CMATCH 1
+#define HAVE_RB_CMODULE 1
+#define HAVE_RB_CNILCLASS 1
+#define HAVE_RB_CNUMERIC 1
+#define HAVE_RB_COBJECT 1
+#define HAVE_RB_CPROC 1
+#define HAVE_RB_CMETHOD 1
+#define HAVE_RB_CRANGE 1
+#define HAVE_RB_CREGEXP 1
+#define HAVE_RB_CSTRING 1
+#define HAVE_RB_CSTRUCT 1
+#define HAVE_RB_CSYMBOL 1
+#define HAVE_RB_CTIME 1
+#define HAVE_RB_CTHREAD 1
+#define HAVE_RB_CTRUECLASS 1
+#define HAVE_RB_CNUMERATOR 1
+#define HAVE_RB_EARGERROR 1
+#define HAVE_RB_EEOFERROR 1
+#define HAVE_RB_EEXCEPTION 1
+#define HAVE_RB_EFLOATDOMAINERROR 1
+#define HAVE_RB_EINDEXERROR 1
+#define HAVE_RB_EINTERRUPT 1
+#define HAVE_RB_EIOERROR 1
+#define HAVE_RB_ELOADERROR 1
+#define HAVE_RB_ELOCALJUMPERROR 1
+#define HAVE_RB_EMATHDOMAINERROR 1
+#define HAVE_RB_ENAMEERROR 1
+#define HAVE_RB_ENOMEMERROR 1
+#define HAVE_RB_ENOMETHODERROR 1
+#define HAVE_RB_ENOTIMPERROR 1
+#define HAVE_RB_ERANGEERROR 1
+#define HAVE_RB_EREGEXPERROR 1
+#define HAVE_RB_ERUNTIMEERROR 1
+#define HAVE_RB_ESCRIPTERROR 1
+#define HAVE_RB_ESECURITYERROR 1
+#define HAVE_RB_ESIGNAL 1
+#define HAVE_RB_ESTANDARDERROR 1
+#define HAVE_RB_ESYNTAXERROR 1
+#define HAVE_RB_ESYSSTACKERROR 1
+#define HAVE_RB_ESYSTEMCALLERROR 1
+#define HAVE_RB_ESYSTEMEXIT 1
+#define HAVE_RB_ETHREADERROR 1
+#define HAVE_RB_ETYPEERROR 1
+#define HAVE_RB_EZERODIVERROR 1
+#define HAVE_RB_MCOMPARABLE 1
+#define HAVE_RB_MENUMERABLE 1
+#define HAVE_RB_MERRNO 1
+#define HAVE_RB_MKERNEL 1
+#define HAVE_RB_CDIR 1
+
+/* Data */
+#define HAVE_DATA_WRAP_STRUCT 1
+#define HAVE_RDATA 1
+
+#define HAVE_TYPEDDATA_WRAP_STRUCT 1
+#define HAVE_RTYPEDDATA
+
+/* Encoding */
+#define HAVE_ENCODING_GET 1
+#define HAVE_ENCODING_SET 1
+#define HAVE_ENC_CODERANGE_ASCIIONLY 1
+
+#define HAVE_RB_ASCII8BIT_ENCODING 1
+#define HAVE_RB_ASCII8BIT_ENCINDEX 1
+#define HAVE_RB_USASCII_ENCODING 1
+#define HAVE_RB_USASCII_ENCINDEX 1
+#define HAVE_RB_UTF8_ENCODING 1
+#define HAVE_RB_UTF8_ENCINDEX 1
+#define HAVE_RB_LOCALE_ENCODING 1
+#define HAVE_RB_LOCALE_ENCINDEX 1
+#define HAVE_RB_FILESYSTEM_ENCODING 1
+#define HAVE_RB_FILESYSTEM_ENCINDEX 1
+
+#define HAVE_RB_DEFAULT_INTERNAL_ENCODING 1
+#define HAVE_RB_DEFAULT_EXTERNAL_ENCODING 1
+
+#define HAVE_RB_ENCDB_ALIAS 1
+#define HAVE_RB_ENC_ASSOCIATE 1
+#define HAVE_RB_ENC_ASSOCIATE_INDEX 1
+#define HAVE_RB_ENC_CODEPOINT_LEN 1
+#define HAVE_RB_ENC_COMPATIBLE 1
+#define HAVE_RB_ENC_COPY 1
+#define HAVE_RB_ENC_FIND 1
+#define HAVE_RB_ENC_FIND_INDEX 1
+#define HAVE_RB_ENC_FROM_ENCODING 1
+#define HAVE_RB_ENC_FROM_INDEX 1
+#define HAVE_RB_ENC_GET 1
+#define HAVE_RB_ENC_GET_INDEX 1
+#define HAVE_RB_ENC_SET_INDEX 1
+#define HAVE_RB_ENC_STR_CODERANGE 1
+#define HAVE_RB_ENC_STR_NEW 1
+#define HAVE_RB_ENC_TO_INDEX 1
+#define HAVE_RB_OBJ_ENCODING 1
+
+#define HAVE_RB_STR_ENCODE 1
+#define HAVE_RB_STR_NEW_CSTR 1
+#define HAVE_RB_USASCII_STR_NEW 1
+#define HAVE_RB_USASCII_STR_NEW_CSTR 1
+#define HAVE_RB_EXTERNAL_STR_NEW 1
+#define HAVE_RB_EXTERNAL_STR_NEW_CSTR 1
+#define HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC 1
+
+#define HAVE_RB_TO_ENCODING 1
+#define HAVE_RB_TO_ENCODING_INDEX 1
+#define HAVE_RB_ENC_NTH 1
+
+#define HAVE_RB_EENCCOMPATERROR 1
+
+#define HAVE_RB_MWAITREADABLE 1
+#define HAVE_RB_MWAITWRITABLE 1
+
+#define HAVE_RSTRING_LENINT 1
+#define HAVE_TIMET2NUM 1
+
+#define HAVE_RB_LONG2INT 1
+#define HAVE_RB_INTERN3 1
+
+#define HAVE_RB_ITER_BREAK 1
+#define HAVE_RB_SOURCEFILE 1
+#define HAVE_RB_SOURCELINE 1
+#define HAVE_RB_METHOD_BOUNDP 1
+
+/* Enumerable */
+#define HAVE_RB_ENUMERATORIZE 1
+
+/* Exception */
+#define HAVE_RB_EXC_NEW 1
+#define HAVE_RB_EXC_NEW2 1
+#define HAVE_RB_EXC_NEW3 1
+#define HAVE_RB_EXC_RAISE 1
+#define HAVE_RB_SET_ERRINFO 1
+
+/* File */
+#define HAVE_RB_FILE_OPEN 1
+#define HAVE_RB_FILE_OPEN_STR 1
+#define HAVE_FILEPATHVALUE 1
+
+/* Float */
+#define HAVE_RB_FLOAT_NEW 1
+#define HAVE_RB_RFLOAT 1
+#define HAVE_RFLOAT_VALUE 1
+
+/* Globals */
+#define HAVE_RB_DEFAULT_RS 1
+#define HAVE_RB_DEFINE_HOOKED_VARIABLE 1
+#define HAVE_RB_DEFINE_READONLY_VARIABLE 1
+#define HAVE_RB_DEFINE_VARIABLE 1
+#define HAVE_RB_F_GLOBAL_VARIABLES 1
+#define HAVE_RB_GV_GET 1
+#define HAVE_RB_GV_SET 1
+#define HAVE_RB_RS 1
+#define HAVE_RB_OUTPUT_RS 1
+#define HAVE_RB_OUTPUT_FS 1
+#define HAVE_RB_STDERR 1
+#define HAVE_RB_STDIN 1
+#define HAVE_RB_STDOUT 1
+#define HAVE_RB_DEFOUT 1
+
+#define HAVE_RB_LASTLINE_SET 1
+#define HAVE_RB_LASTLINE_GET 1
+
+/* Hash */
+#define HAVE_RB_HASH 1
+#define HAVE_RB_HASH2 1
+#define HAVE_RB_HASH_DUP 1
+#define HAVE_RB_HASH_FREEZE 1
+#define HAVE_RB_HASH_AREF 1
+#define HAVE_RB_HASH_ASET 1
+#define HAVE_RB_HASH_CLEAR 1
+#define HAVE_RB_HASH_DELETE 1
+#define HAVE_RB_HASH_DELETE_IF 1
+#define HAVE_RB_HASH_FETCH 1
+#define HAVE_RB_HASH_FOREACH 1
+#define HAVE_RB_HASH_LOOKUP 1
+#define HAVE_RB_HASH_LOOKUP2 1
+#define HAVE_RB_HASH_NEW 1
+#define HAVE_RB_HASH_SET_IFNONE 1
+#define HAVE_RB_HASH_SIZE 1
+
+/* Integer */
+#define HAVE_RB_INTEGER_PACK 1
+
+/* IO */
+#define HAVE_GET_OPEN_FILE 1
+#define HAVE_RB_IO_ADDSTR 1
+#define HAVE_RB_IO_CHECK_IO 1
+#define HAVE_RB_IO_CHECK_CLOSED 1
+#define HAVE_RB_IO_TAINT_CHECK 1
+#define HAVE_RB_IO_CHECK_READABLE 1
+#define HAVE_RB_IO_CHECK_WRITABLE 1
+#define HAVE_RB_IO_CLOSE 1
+#define HAVE_RB_IO_PRINT 1
+#define HAVE_RB_IO_PRINTF 1
+#define HAVE_RB_IO_PUTS 1
+#define HAVE_RB_IO_WAIT_READABLE 1
+#define HAVE_RB_IO_WAIT_WRITABLE 1
+#define HAVE_RB_IO_WRITE 1
+#define HAVE_RB_IO_BINMODE 1
+
+#define HAVE_RB_THREAD_FD_WRITABLE 1
+#define HAVE_RB_THREAD_WAIT_FD 1
+
+#define HAVE_RB_MUTEX_NEW 1
+#define HAVE_RB_MUTEX_LOCKED_P 1
+#define HAVE_RB_MUTEX_TRYLOCK 1
+#define HAVE_RB_MUTEX_LOCK 1
+#define HAVE_RB_MUTEX_UNLOCK 1
+#define HAVE_RB_MUTEX_SLEEP 1
+#define HAVE_RB_MUTEX_SYNCHRONIZE 1
+
+#define HAVE_RB_FD_FIX_CLOEXEC 1
+#define HAVE_RB_CLOEXEC_OPEN 1
+
+/* Kernel */
+#define HAVE_RB_BLOCK_GIVEN_P 1
+#define HAVE_RB_BLOCK_PROC 1
+#define HAVE_RB_BLOCK_CALL 1
+#define HAVE_RB_ENSURE 1
+#define HAVE_RB_EVAL_STRING 1
+#define HAVE_RB_EXEC_RECURSIVE 1
+#define HAVE_RB_F_SPRINTF 1
+#define HAVE_RB_NEED_BLOCK 1
+#define HAVE_RB_RAISE 1
+#define HAVE_RB_RESCUE 1
+#define HAVE_RB_RESCUE2 1
+#define HAVE_RB_SET_END_PROC 1
+#define HAVE_RB_SYS_FAIL 1
+#define HAVE_RB_SYSERR_FAIL 1
+#define HAVE_RB_MAKE_BACKTRACE 1
+#define HAVE_RB_THROW 1
+#define HAVE_RB_CATCH 1
+#define HAVE_RB_THROW_OBJ 1
+#define HAVE_RB_CATCH_OBJ 1
+#define HAVE_RB_WARN 1
+#define HAVE_RB_YIELD 1
+#define HAVE_RB_YIELD_SPLAT 1
+#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
+#define HAVE_RB_GC_ENABLE 1
+#define HAVE_RB_GC_DISABLE 1
+#define HAVE_RB_GC 1
+
+/* Marshal */
+#define HAVE_RB_MARSHAL_DUMP 1
+#define HAVE_RB_MARSHAL_LOAD 1
+
+/* Module */
+#define HAVE_RB_ALIAS 1
+#define HAVE_RB_CONST_DEFINED 1
+#define HAVE_RB_CONST_DEFINED_AT 1
+#define HAVE_RB_CONST_GET 1
+#define HAVE_RB_CONST_GET_AT 1
+#define HAVE_RB_CONST_GET_FROM 1
+#define HAVE_RB_CONST_SET 1
+#define HAVE_RB_DEFINE_ALIAS 1
+#define HAVE_RB_DEFINE_CLASS 1
+#define HAVE_RB_DEFINE_CLASS_UNDER 1
+#define HAVE_RB_DEFINE_CLASS_ID_UNDER 1
+#define HAVE_RB_DEFINE_CONST 1
+#define HAVE_RB_DEFINE_GLOBAL_CONST 1
+#define HAVE_RB_DEFINE_GLOBAL_FUNCTION 1
+#define HAVE_RB_DEFINE_METHOD 1
+#define HAVE_RB_DEFINE_MODULE_FUNCTION 1
+#define HAVE_RB_DEFINE_MODULE 1
+#define HAVE_RB_DEFINE_MODULE_UNDER 1
+#define HAVE_RB_DEFINE_PRIVATE_METHOD 1
+#define HAVE_RB_DEFINE_PROTECTED_METHOD 1
+#define HAVE_RB_DEFINE_SINGLETON_METHOD 1
+#define HAVE_RB_UNDEF 1
+#define HAVE_RB_UNDEF_METHOD 1
+
+/* Numeric */
+#define HAVE_NUM2CHR 1
+#define HAVE_RB_CMPINT 1
+#define HAVE_RB_INT2INUM 1
+#define HAVE_RB_INTEGER 1
+#define HAVE_RB_LL2INUM 1
+#define HAVE_RB_NUM2DBL 1
+#if SIZEOF_INT < SIZEOF_LONG
+#define HAVE_RB_NUM2INT 1
+#define HAVE_RB_NUM2UINT 1
+#endif
+#define HAVE_RB_NUM2LONG 1
+#define HAVE_RB_INT2NUM 1
+#define HAVE_RB_NUM2ULONG 1
+#define HAVE_RB_NUM_COERCE_BIN 1
+#define HAVE_RB_NUM_COERCE_CMP 1
+#define HAVE_RB_NUM_COERCE_RELOP 1
+#define HAVE_RB_NUM_ZERODIV 1
+
+/* Fixnum */
+#if SIZEOF_INT < SIZEOF_LONG
+#define HAVE_RB_FIX2UINT 1
+#define HAVE_RB_FIX2INT 1
+#endif
+
+/* Object */
+#define HAVE_OBJ_TAINT 1
+#define HAVE_OBJ_TAINTED 1
+#define HAVE_OBJ_INFECT 1
+#define HAVE_RB_ANY_TO_S 1
+#define HAVE_RB_ATTR_GET 1
+#define HAVE_RB_OBJ_INSTANCE_VARIABLES 1
+#define HAVE_RB_CHECK_ARRAY_TYPE 1
+#define HAVE_RB_CHECK_CONVERT_TYPE 1
+#define HAVE_RB_CHECK_TO_INTEGER 1
+#define HAVE_RB_CHECK_FROZEN 1
+#define HAVE_RB_CHECK_STRING_TYPE 1
+#define HAVE_RB_CLASS_OF 1
+#define HAVE_RB_CONVERT_TYPE 1
+#define HAVE_RB_EQUAL 1
+#define HAVE_RB_CLASS_INHERITED_P 1
+#define HAVE_RB_EXTEND_OBJECT 1
+#define HAVE_RB_INSPECT 1
+#define HAVE_RB_IVAR_DEFINED 1
+#define HAVE_RB_IVAR_GET 1
+#define HAVE_RB_IVAR_SET 1
+#define HAVE_RB_IV_GET 1
+#define HAVE_RB_IV_SET 1
+#define HAVE_RB_OBJ_ALLOC 1
+#define HAVE_RB_OBJ_CALL_INIT 1
+#define HAVE_RB_OBJ_CLASSNAME 1
+#define HAVE_RB_OBJ_DUP 1
+#define HAVE_RB_OBJ_FREEZE 1
+#define HAVE_RB_OBJ_FROZEN_P 1
+#define HAVE_RB_OBJ_ID 1
+#define HAVE_RB_OBJ_INSTANCE_EVAL 1
+#define HAVE_RB_OBJ_IS_INSTANCE_OF 1
+#define HAVE_RB_OBJ_IS_KIND_OF 1
+#define HAVE_RB_OBJ_TAINT 1
+#define HAVE_RB_OBJ_METHOD 1
+#define HAVE_RB_OBJ_METHOD_ARITY 1
+#define HAVE_RB_REQUIRE 1
+#define HAVE_RB_RESPOND_TO 1
+#define HAVE_RB_OBJ_RESPOND_TO 1
+#define HAVE_RB_SPECIAL_CONST_P 1
+#define HAVE_RB_TO_ID 1
+#define HAVE_RB_TO_INT 1
+#define HAVE_RTEST 1
+#define HAVE_TYPE 1
+#define HAVE_RB_TYPE_P 1
+#define HAVE_BUILTIN_TYPE 1
+
+/* Proc */
+#define HAVE_RB_PROC_NEW 1
+#define HAVE_RB_PROC_ARITY 1
+#define HAVE_RB_PROC_CALL 1
+
+/* Range */
+#define HAVE_RB_RANGE_NEW 1
+#define HAVE_RB_RANGE_VALUES 1
+#define HAVE_RB_RANGE_BEG_LEN 1
+
+/* Rational */
+#define HAVE_RB_RATIONAL 1
+#define HAVE_RB_RATIONAL1 1
+#define HAVE_RB_RATIONAL2 1
+#define HAVE_RB_RATIONAL_NEW 1
+#define HAVE_RB_RATIONAL_NEW1 1
+#define HAVE_RB_RATIONAL_NEW2 1
+#define HAVE_RB_RATIONAL_NUM 1
+#define HAVE_RB_RATIONAL_DEN 1
+
+/* Regexp */
+#define HAVE_RB_BACKREF_GET 1
+#define HAVE_RB_REG_MATCH 1
+#define HAVE_RB_REG_NEW 1
+#define HAVE_RB_REG_NTH_MATCH 1
+#define HAVE_RB_REG_OPTIONS 1
+#define HAVE_RB_REG_REGCOMP 1
+
+/* st */
+#define HAVE_RB_ST 1
+
+/* String */
+#define HAVE_RB_CSTR2INUM 1
+#define HAVE_RB_CSTR_TO_INUM 1
+#define HAVE_RB_STR2INUM 1
+#define HAVE_RB_STR_APPEND 1
+#define HAVE_RB_STR_BUF_CAT 1
+#define HAVE_RB_STR_BUF_NEW 1
+#define HAVE_RB_STR_BUF_NEW2 1
+#define HAVE_RB_STR_CAT 1
+#define HAVE_RB_STR_CAT2 1
+#define HAVE_RB_STR_CMP 1
+#define HAVE_RB_STR_DUP 1
+#define HAVE_RB_STR_FLUSH 1
+#define HAVE_RB_STR_FREEZE 1
+#define HAVE_RB_STR_HASH 1
+#define HAVE_RB_STR_UPDATE 1
+#define HAVE_RB_STR_INSPECT 1
+#define HAVE_RB_STR_INTERN 1
+#define HAVE_RB_STR_NEW 1
+#define HAVE_RB_STR_NEW2 1
+#define HAVE_RB_STR_NEW3 1
+#define HAVE_RB_STR_NEW4 1
+#define HAVE_RB_STR_NEW5 1
+#define HAVE_RB_TAINTED_STR_NEW 1
+#define HAVE_RB_TAINTED_STR_NEW2 1
+#define HAVE_RB_STR_PLUS 1
+#define HAVE_RB_STR_TIMES 1
+#define HAVE_RB_STR_RESIZE 1
+#define HAVE_RB_STR_SET_LEN 1
+#define HAVE_RB_STR_SPLIT 1
+#define HAVE_RB_STR_SUBSTR 1
+#define HAVE_RB_STR_TO_STR 1
+#define HAVE_RSTRING_LEN 1
+#define HAVE_RSTRING_PTR 1
+#define HAVE_STRINGVALUE 1
+
+#define HAVE_RB_STR_FREE 1
+#define HAVE_RB_SPRINTF 1
+#define HAVE_RB_LOCALE_STR_NEW 1
+#define HAVE_RB_LOCALE_STR_NEW_CSTR 1
+#define HAVE_RB_STR_CONV_ENC 1
+#define HAVE_RB_STR_CONV_ENC_OPTS 1
+#define HAVE_RB_STR_EXPORT 1
+#define HAVE_RB_STR_EXPORT_LOCALE 1
+#define HAVE_RB_STR_LENGTH 1
+#define HAVE_RB_STR_EQUAL 1
+#define HAVE_RB_STR_SUBSEQ 1
+#define HAVE_RB_VSPRINTF 1
+#define HAVE_RB_STRING 1
+
+/* Struct */
+#define HAVE_RB_STRUCT_AREF 1
+#define HAVE_RB_STRUCT_ASET 1
+#define HAVE_RB_STRUCT_DEFINE 1
+#define HAVE_RB_STRUCT_DEFINE_UNDER 1
+#define HAVE_RB_STRUCT_NEW 1
+#define HAVE_RB_STRUCT_GETMEMBER 1
+#define HAVE_RB_STRUCT_S_MEMBERS 1
+#define HAVE_RB_STRUCT_MEMBERS 1
+#ifdef RUBY_VERSION_IS_2_4
+#define HAVE_RB_STRUCT_SIZE 1
+#endif
+
+/* Symbol */
+#define HAVE_RB_ID2NAME 1
+#define HAVE_RB_ID2STR 1
+#define HAVE_RB_INTERN_STR 1
+#define HAVE_RB_INTERN 1
+#define HAVE_RB_IS_CLASS_ID 1
+#define HAVE_RB_IS_CONST_ID 1
+#define HAVE_RB_IS_INSTANCE_ID 1
+#define HAVE_RB_SYM2STR 1
+
+/* Thread */
+#define HAVE_RB_THREAD_ALONE 1
+#define HAVE_RB_THREAD_CALL_WITHOUT_GVL 1
+#define HAVE_RB_THREAD_CURRENT 1
+#define HAVE_RB_THREAD_LOCAL_AREF 1
+#define HAVE_RB_THREAD_LOCAL_ASET 1
+#define HAVE_RB_THREAD_WAIT_FOR 1
+#define HAVE_RB_THREAD_WAKEUP 1
+#define HAVE_RB_THREAD_CREATE 1
+
+/* Time */
+#define HAVE_RB_TIME_NEW 1
+#define HAVE_RB_TIME_NANO_NEW 1
+#define HAVE_RB_TIME_NUM_NEW 1
+#define HAVE_RB_TIME_INTERVAL 1
+#define HAVE_RB_TIME_TIMEVAL 1
+#define HAVE_RB_TIME_TIMESPEC 1
+#ifdef RUBY_VERSION_IS_2_3
+#define HAVE_RB_TIMESPEC_NOW 1
+#define HAVE_RB_TIME_TIMESPEC_NEW 1
+#endif
+
+/* Util */
+#define HAVE_RB_SCAN_ARGS 1
+
+#endif
diff --git a/spec/ruby/optional/capi/ext/st_spec.c b/spec/ruby/optional/capi/ext/st_spec.c
new file mode 100644
index 0000000000..4e59698d77
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/st_spec.c
@@ -0,0 +1,93 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <string.h>
+#include <stdarg.h>
+
+#ifdef HAVE_RB_ST
+#include <ruby/st.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_ST
+
+#if SIZEOF_LONG == SIZEOF_VOIDP
+# define ST2NUM(x) ULONG2NUM(x)
+#else
+# define ST2NUM(x) ULL2NUM(x)
+#endif
+
+VALUE st_spec_st_init_numtable(VALUE self) {
+ st_table *tbl = st_init_numtable();
+ st_index_t entries = tbl->num_entries;
+ st_free_table(tbl);
+ return ST2NUM(entries);
+}
+
+VALUE st_spec_st_init_numtable_with_size(VALUE self) {
+ st_table *tbl = st_init_numtable_with_size(128);
+ st_index_t entries = tbl->num_entries;
+ st_free_table(tbl);
+ return ST2NUM(entries);
+}
+
+VALUE st_spec_st_insert(VALUE self) {
+ st_index_t entries;
+ st_table *tbl = st_init_numtable_with_size(128);
+ st_insert(tbl, 1, 1);
+ entries = tbl->num_entries;
+ st_free_table(tbl);
+ return ST2NUM(entries);
+}
+
+static int sum(st_data_t key, st_data_t value, st_data_t arg) {
+ *(int*)arg += (int)value;
+ return ST_CONTINUE;
+}
+
+VALUE st_spec_st_foreach(VALUE self) {
+ int total = 0;
+ st_table *tbl = st_init_numtable_with_size(128);
+ st_insert(tbl, 1, 3);
+ st_insert(tbl, 2, 4);
+ st_foreach(tbl, sum, (st_data_t)&total);
+ st_free_table(tbl);
+ return INT2FIX(total);
+}
+
+VALUE st_spec_st_lookup(VALUE self) {
+ st_data_t result = (st_data_t)0;
+ st_table *tbl = st_init_numtable_with_size(128);
+ st_insert(tbl, 7, 42);
+ st_insert(tbl, 2, 4);
+ st_lookup(tbl, (st_data_t)7, &result);
+ st_free_table(tbl);
+#if SIZEOF_LONG == SIZEOF_VOIDP
+ return ULONG2NUM(result);
+#else
+ return ULL2NUM(result);
+#endif
+}
+
+#endif
+
+void Init_st_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiStSpecs", rb_cObject);
+
+#ifdef HAVE_RB_ST
+ rb_define_method(cls, "st_init_numtable", st_spec_st_init_numtable, 0);
+ rb_define_method(cls, "st_init_numtable_with_size", st_spec_st_init_numtable_with_size, 0);
+ rb_define_method(cls, "st_insert", st_spec_st_insert, 0);
+ rb_define_method(cls, "st_foreach", st_spec_st_foreach, 0);
+ rb_define_method(cls, "st_lookup", st_spec_st_lookup, 0);
+#endif
+
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
new file mode 100644
index 0000000000..0568b42212
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -0,0 +1,682 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <string.h>
+#include <stdarg.h>
+
+#ifdef HAVE_RUBY_ENCODING_H
+#include "ruby/encoding.h"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_CSTR2INUM
+VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) {
+ int num = FIX2INT(inum);
+ return rb_cstr2inum(RSTRING_PTR(str), num);
+}
+#endif
+
+#ifdef HAVE_RB_CSTR_TO_INUM
+static VALUE string_spec_rb_cstr_to_inum(VALUE self, VALUE str, VALUE inum, VALUE badcheck) {
+ int num = FIX2INT(inum);
+ return rb_cstr_to_inum(RSTRING_PTR(str), num, RTEST(badcheck));
+}
+#endif
+
+#ifdef HAVE_RB_STR2INUM
+VALUE string_spec_rb_str2inum(VALUE self, VALUE str, VALUE inum) {
+ int num = FIX2INT(inum);
+ return rb_str2inum(str, num);
+}
+#endif
+
+#ifdef HAVE_RB_STR_APPEND
+VALUE string_spec_rb_str_append(VALUE self, VALUE str, VALUE str2) {
+ return rb_str_append(str, str2);
+}
+#endif
+
+#ifdef HAVE_RB_STR_SET_LEN
+VALUE string_spec_rb_str_set_len(VALUE self, VALUE str, VALUE len) {
+ rb_str_set_len(str, NUM2LONG(len));
+
+ return str;
+}
+
+VALUE string_spec_rb_str_set_len_RSTRING_LEN(VALUE self, VALUE str, VALUE len) {
+ rb_str_set_len(str, NUM2LONG(len));
+
+ return INT2FIX(RSTRING_LEN(str));
+}
+#endif
+
+#ifdef HAVE_RB_STR_BUF_NEW
+VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) {
+ VALUE buf;
+
+ buf = rb_str_buf_new(NUM2LONG(len));
+
+ if(RTEST(str)) {
+ snprintf(RSTRING_PTR(buf), NUM2LONG(len), "%s", RSTRING_PTR(str));
+ }
+
+ return buf;
+}
+#endif
+
+#ifdef HAVE_RB_STR_BUF_NEW2
+VALUE string_spec_rb_str_buf_new2(VALUE self) {
+ return rb_str_buf_new2("hello\0invisible");
+}
+#endif
+
+#ifdef HAVE_RB_STR_BUF_CAT
+VALUE string_spec_rb_str_buf_cat(VALUE self, VALUE str) {
+ const char *question_mark = "?";
+ rb_str_buf_cat(str, question_mark, strlen(question_mark));
+ return str;
+}
+#endif
+
+#ifdef HAVE_RB_STR_CAT
+VALUE string_spec_rb_str_cat(VALUE self, VALUE str) {
+ return rb_str_cat(str, "?", 1);
+}
+#endif
+
+#ifdef HAVE_RB_STR_CAT2
+VALUE string_spec_rb_str_cat2(VALUE self, VALUE str) {
+ return rb_str_cat2(str, "?");
+}
+#endif
+
+#ifdef HAVE_RB_STR_CMP
+VALUE string_spec_rb_str_cmp(VALUE self, VALUE str1, VALUE str2) {
+ return INT2NUM(rb_str_cmp(str1, str2));
+}
+#endif
+
+#ifdef HAVE_RB_STR_CONV_ENC
+VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) {
+ rb_encoding* from_enc;
+ rb_encoding* to_enc;
+
+ from_enc = rb_to_encoding(from);
+
+ if(NIL_P(to)) {
+ to_enc = 0;
+ } else {
+ to_enc = rb_to_encoding(to);
+ }
+
+ return rb_str_conv_enc(str, from_enc, to_enc);
+}
+#endif
+
+#ifdef HAVE_RB_STR_CONV_ENC_OPTS
+VALUE string_spec_rb_str_conv_enc_opts(VALUE self, VALUE str, VALUE from, VALUE to,
+ VALUE ecflags, VALUE ecopts)
+{
+ rb_encoding* from_enc;
+ rb_encoding* to_enc;
+
+ from_enc = rb_to_encoding(from);
+
+ if(NIL_P(to)) {
+ to_enc = 0;
+ } else {
+ to_enc = rb_to_encoding(to);
+ }
+
+ return rb_str_conv_enc_opts(str, from_enc, to_enc, FIX2INT(ecflags), ecopts);
+}
+#endif
+
+#ifdef HAVE_RB_STR_EXPORT
+VALUE string_spec_rb_str_export(VALUE self, VALUE str) {
+ return rb_str_export(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_EXPORT_LOCALE
+VALUE string_spec_rb_str_export_locale(VALUE self, VALUE str) {
+ return rb_str_export_locale(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_DUP
+VALUE string_spec_rb_str_dup(VALUE self, VALUE str) {
+ return rb_str_dup(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_FREEZE
+VALUE string_spec_rb_str_freeze(VALUE self, VALUE str) {
+ return rb_str_freeze(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_INSPECT
+VALUE string_spec_rb_str_inspect(VALUE self, VALUE str) {
+ return rb_str_inspect(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_INTERN
+VALUE string_spec_rb_str_intern(VALUE self, VALUE str) {
+ return rb_str_intern(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_LENGTH
+VALUE string_spec_rb_str_length(VALUE self, VALUE str) {
+ return rb_str_length(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_NEW
+VALUE string_spec_rb_str_new(VALUE self, VALUE str, VALUE len) {
+ return rb_str_new(RSTRING_PTR(str), FIX2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_STR_NEW2
+VALUE string_spec_rb_str_new2(VALUE self, VALUE str) {
+ if(NIL_P(str)) {
+ return rb_str_new2("");
+ } else {
+ return rb_str_new2(RSTRING_PTR(str));
+ }
+}
+#endif
+
+#ifdef HAVE_RB_STR_ENCODE
+VALUE string_spec_rb_str_encode(VALUE self, VALUE str, VALUE enc, VALUE flags, VALUE opts) {
+ return rb_str_encode(str, enc, FIX2INT(flags), opts);
+}
+#endif
+
+#ifdef HAVE_RB_STR_NEW_CSTR
+VALUE string_spec_rb_str_new_cstr(VALUE self, VALUE str) {
+ if(NIL_P(str)) {
+ return rb_str_new_cstr("");
+ } else {
+ return rb_str_new_cstr(RSTRING_PTR(str));
+ }
+}
+#endif
+
+#ifdef HAVE_RB_EXTERNAL_STR_NEW
+VALUE string_spec_rb_external_str_new(VALUE self, VALUE str) {
+ return rb_external_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
+}
+#endif
+
+#ifdef HAVE_RB_EXTERNAL_STR_NEW_CSTR
+VALUE string_spec_rb_external_str_new_cstr(VALUE self, VALUE str) {
+ return rb_external_str_new_cstr(RSTRING_PTR(str));
+}
+#endif
+
+#ifdef HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC
+VALUE string_spec_rb_external_str_new_with_enc(VALUE self, VALUE str, VALUE len, VALUE encoding) {
+ return rb_external_str_new_with_enc(RSTRING_PTR(str), FIX2LONG(len), rb_to_encoding(encoding));
+}
+#endif
+
+#ifdef HAVE_RB_LOCALE_STR_NEW
+VALUE string_spec_rb_locale_str_new(VALUE self, VALUE str, VALUE len) {
+ return rb_locale_str_new(RSTRING_PTR(str), FIX2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_LOCALE_STR_NEW_CSTR
+VALUE string_spec_rb_locale_str_new_cstr(VALUE self, VALUE str) {
+ return rb_locale_str_new_cstr(RSTRING_PTR(str));
+}
+#endif
+
+#ifdef HAVE_RB_STR_NEW3
+VALUE string_spec_rb_str_new3(VALUE self, VALUE str) {
+ return rb_str_new3(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_NEW4
+VALUE string_spec_rb_str_new4(VALUE self, VALUE str) {
+ return rb_str_new4(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_NEW5
+VALUE string_spec_rb_str_new5(VALUE self, VALUE str, VALUE ptr, VALUE len) {
+ return rb_str_new5(str, RSTRING_PTR(ptr), FIX2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_TAINTED_STR_NEW
+VALUE string_spec_rb_tainted_str_new(VALUE self, VALUE str, VALUE len) {
+ return rb_tainted_str_new(RSTRING_PTR(str), FIX2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_TAINTED_STR_NEW2
+VALUE string_spec_rb_tainted_str_new2(VALUE self, VALUE str) {
+ return rb_tainted_str_new2(RSTRING_PTR(str));
+}
+#endif
+
+#ifdef HAVE_RB_STR_PLUS
+VALUE string_spec_rb_str_plus(VALUE self, VALUE str1, VALUE str2) {
+ return rb_str_plus(str1, str2);
+}
+#endif
+
+#ifdef HAVE_RB_STR_TIMES
+VALUE string_spec_rb_str_times(VALUE self, VALUE str, VALUE times) {
+ return rb_str_times(str, times);
+}
+#endif
+
+#ifdef HAVE_RB_STR_RESIZE
+VALUE string_spec_rb_str_resize(VALUE self, VALUE str, VALUE size) {
+ return rb_str_resize(str, FIX2INT(size));
+}
+
+VALUE string_spec_rb_str_resize_RSTRING_LEN(VALUE self, VALUE str, VALUE size) {
+ VALUE modified = rb_str_resize(str, FIX2INT(size));
+ return INT2FIX(RSTRING_LEN(modified));
+}
+#endif
+
+#ifdef HAVE_RB_STR_SPLIT
+VALUE string_spec_rb_str_split(VALUE self, VALUE str) {
+ return rb_str_split(str, ",");
+}
+#endif
+
+#ifdef HAVE_RB_STR_SUBSEQ
+VALUE string_spec_rb_str_subseq(VALUE self, VALUE str, VALUE beg, VALUE len) {
+ return rb_str_subseq(str, FIX2INT(beg), FIX2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_STR_SUBSTR
+VALUE string_spec_rb_str_substr(VALUE self, VALUE str, VALUE beg, VALUE len) {
+ return rb_str_substr(str, FIX2INT(beg), FIX2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_STR_TO_STR
+VALUE string_spec_rb_str_to_str(VALUE self, VALUE arg) {
+ return rb_str_to_str(arg);
+}
+#endif
+
+#ifdef HAVE_RSTRING_LEN
+VALUE string_spec_RSTRING_LEN(VALUE self, VALUE str) {
+ return INT2FIX(RSTRING_LEN(str));
+}
+#endif
+
+#ifdef HAVE_RSTRING_LENINT
+VALUE string_spec_RSTRING_LENINT(VALUE self, VALUE str) {
+ return INT2FIX(RSTRING_LENINT(str));
+}
+#endif
+
+#ifdef HAVE_RSTRING_PTR
+VALUE string_spec_RSTRING_PTR_iterate(VALUE self, VALUE str) {
+ int i;
+ char* ptr;
+
+ ptr = RSTRING_PTR(str);
+ for(i = 0; i < RSTRING_LEN(str); i++) {
+ rb_yield(CHR2FIX(ptr[i]));
+ }
+ return Qnil;
+}
+
+VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) {
+ int i;
+ char c;
+ char* ptr;
+
+ ptr = RSTRING_PTR(str);
+ c = FIX2INT(chr);
+
+ for(i = 0; i < RSTRING_LEN(str); i++) {
+ ptr[i] = c;
+ }
+ return Qnil;
+}
+
+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 */
+ rb_funcall(cb, rb_intern("call"), 1, str);
+ }
+
+ return rb_str_new2(RSTRING_PTR(str));
+}
+#endif
+
+#ifdef HAVE_STRINGVALUE
+VALUE string_spec_StringValue(VALUE self, VALUE str) {
+ return StringValue(str);
+}
+#endif
+
+#ifdef HAVE_RB_STR_HASH
+static VALUE string_spec_rb_str_hash(VALUE self, VALUE str) {
+ st_index_t val = rb_str_hash(str);
+
+#if SIZEOF_LONG == SIZEOF_VOIDP || SIZEOF_LONG_LONG == SIZEOF_VOIDP
+ return LONG2FIX((long)val);
+#else
+# error unsupported platform
+#endif
+}
+#endif
+
+#ifdef HAVE_RB_STR_UPDATE
+static VALUE string_spec_rb_str_update(VALUE self, VALUE str, VALUE beg, VALUE end, VALUE replacement) {
+ rb_str_update(str, FIX2LONG(beg), FIX2LONG(end), replacement);
+ return str;
+}
+#endif
+
+#ifdef HAVE_RB_STR_FREE
+static VALUE string_spec_rb_str_free(VALUE self, VALUE str) {
+ rb_str_free(str);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_SPRINTF
+static VALUE string_spec_rb_sprintf1(VALUE self, VALUE str, VALUE repl) {
+ return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl));
+}
+static VALUE string_spec_rb_sprintf2(VALUE self, VALUE str, VALUE repl1, VALUE repl2) {
+ return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl1), RSTRING_PTR(repl2));
+}
+#endif
+
+#ifdef HAVE_RB_VSPRINTF
+static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...) {
+ va_list varargs;
+ VALUE str;
+
+ va_start(varargs, fmt);
+ str = rb_vsprintf(fmt, varargs);
+ va_end(varargs);
+
+ return str;
+}
+
+static VALUE string_spec_rb_vsprintf(VALUE self, VALUE fmt, VALUE str, VALUE i, VALUE f) {
+ return string_spec_rb_vsprintf_worker(RSTRING_PTR(fmt), RSTRING_PTR(str),
+ FIX2INT(i), RFLOAT_VALUE(f));
+}
+#endif
+
+#ifdef HAVE_RB_STR_EQUAL
+VALUE string_spec_rb_str_equal(VALUE self, VALUE str1, VALUE str2) {
+ return rb_str_equal(str1, str2);
+}
+#endif
+
+#ifdef HAVE_RB_USASCII_STR_NEW
+static VALUE string_spec_rb_usascii_str_new(VALUE self, VALUE str, VALUE len) {
+ return rb_usascii_str_new(RSTRING_PTR(str), NUM2INT(len));
+}
+#endif
+
+#ifdef HAVE_RB_USASCII_STR_NEW_CSTR
+static VALUE string_spec_rb_usascii_str_new_cstr(VALUE self, VALUE str) {
+ return rb_usascii_str_new_cstr(RSTRING_PTR(str));
+}
+#endif
+
+#ifdef HAVE_RB_STRING
+static VALUE string_spec_rb_String(VALUE self, VALUE val) {
+ return rb_String(val);
+}
+#endif
+
+void Init_string_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiStringSpecs", rb_cObject);
+
+#ifdef HAVE_RB_CSTR2INUM
+ rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
+#endif
+
+#ifdef HAVE_RB_CSTR_TO_INUM
+ rb_define_method(cls, "rb_cstr_to_inum", string_spec_rb_cstr_to_inum, 3);
+#endif
+
+#ifdef HAVE_RB_STR2INUM
+ rb_define_method(cls, "rb_str2inum", string_spec_rb_str2inum, 2);
+#endif
+
+#ifdef HAVE_RB_STR_APPEND
+ rb_define_method(cls, "rb_str_append", string_spec_rb_str_append, 2);
+#endif
+
+#ifdef HAVE_RB_STR_BUF_NEW
+ rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2);
+#endif
+
+#ifdef HAVE_RB_STR_BUF_NEW2
+ rb_define_method(cls, "rb_str_buf_new2", string_spec_rb_str_buf_new2, 0);
+#endif
+
+#ifdef HAVE_RB_STR_BUF_CAT
+ rb_define_method(cls, "rb_str_buf_cat", string_spec_rb_str_buf_cat, 1);
+#endif
+
+#ifdef HAVE_RB_STR_CAT
+ rb_define_method(cls, "rb_str_cat", string_spec_rb_str_cat, 1);
+#endif
+
+#ifdef HAVE_RB_STR_CAT2
+ rb_define_method(cls, "rb_str_cat2", string_spec_rb_str_cat2, 1);
+#endif
+
+#ifdef HAVE_RB_STR_CMP
+ rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2);
+#endif
+
+#ifdef HAVE_RB_STR_CONV_ENC
+ rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3);
+#endif
+
+#ifdef HAVE_RB_STR_CONV_ENC_OPTS
+ rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5);
+#endif
+
+#ifdef HAVE_RB_STR_EXPORT
+ rb_define_method(cls, "rb_str_export", string_spec_rb_str_export, 1);
+#endif
+
+#ifdef HAVE_RB_STR_EXPORT_LOCALE
+ rb_define_method(cls, "rb_str_export_locale", string_spec_rb_str_export_locale, 1);
+#endif
+
+#ifdef HAVE_RB_STR_DUP
+ rb_define_method(cls, "rb_str_dup", string_spec_rb_str_dup, 1);
+#endif
+
+#ifdef HAVE_RB_STR_FREEZE
+ rb_define_method(cls, "rb_str_freeze", string_spec_rb_str_freeze, 1);
+#endif
+
+#ifdef HAVE_RB_STR_INSPECT
+ rb_define_method(cls, "rb_str_inspect", string_spec_rb_str_inspect, 1);
+#endif
+
+#ifdef HAVE_RB_STR_INTERN
+ rb_define_method(cls, "rb_str_intern", string_spec_rb_str_intern, 1);
+#endif
+
+#ifdef HAVE_RB_STR_LENGTH
+ rb_define_method(cls, "rb_str_length", string_spec_rb_str_length, 1);
+#endif
+
+#ifdef HAVE_RB_STR_NEW
+ rb_define_method(cls, "rb_str_new", string_spec_rb_str_new, 2);
+#endif
+
+#ifdef HAVE_RB_STR_NEW2
+ rb_define_method(cls, "rb_str_new2", string_spec_rb_str_new2, 1);
+#endif
+
+#ifdef HAVE_RB_STR_ENCODE
+ rb_define_method(cls, "rb_str_encode", string_spec_rb_str_encode, 4);
+#endif
+
+#ifdef HAVE_RB_STR_NEW_CSTR
+ rb_define_method(cls, "rb_str_new_cstr", string_spec_rb_str_new_cstr, 1);
+#endif
+
+#ifdef HAVE_RB_EXTERNAL_STR_NEW
+ rb_define_method(cls, "rb_external_str_new", string_spec_rb_external_str_new, 1);
+#endif
+
+#ifdef HAVE_RB_EXTERNAL_STR_NEW_CSTR
+ rb_define_method(cls, "rb_external_str_new_cstr",
+ string_spec_rb_external_str_new_cstr, 1);
+#endif
+
+#ifdef HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC
+ rb_define_method(cls, "rb_external_str_new_with_enc", string_spec_rb_external_str_new_with_enc, 3);
+#endif
+
+#ifdef HAVE_RB_LOCALE_STR_NEW
+ rb_define_method(cls, "rb_locale_str_new", string_spec_rb_locale_str_new, 2);
+#endif
+
+#ifdef HAVE_RB_LOCALE_STR_NEW_CSTR
+ rb_define_method(cls, "rb_locale_str_new_cstr", string_spec_rb_locale_str_new_cstr, 1);
+#endif
+
+#ifdef HAVE_RB_STR_NEW3
+ rb_define_method(cls, "rb_str_new3", string_spec_rb_str_new3, 1);
+#endif
+
+#ifdef HAVE_RB_STR_NEW4
+ rb_define_method(cls, "rb_str_new4", string_spec_rb_str_new4, 1);
+#endif
+
+#ifdef HAVE_RB_STR_NEW5
+ rb_define_method(cls, "rb_str_new5", string_spec_rb_str_new5, 3);
+#endif
+
+#ifdef HAVE_RB_TAINTED_STR_NEW
+ rb_define_method(cls, "rb_tainted_str_new", string_spec_rb_tainted_str_new, 2);
+#endif
+
+#ifdef HAVE_RB_TAINTED_STR_NEW2
+ rb_define_method(cls, "rb_tainted_str_new2", string_spec_rb_tainted_str_new2, 1);
+#endif
+
+#ifdef HAVE_RB_STR_PLUS
+ rb_define_method(cls, "rb_str_plus", string_spec_rb_str_plus, 2);
+#endif
+
+#ifdef HAVE_RB_STR_TIMES
+ rb_define_method(cls, "rb_str_times", string_spec_rb_str_times, 2);
+#endif
+
+#ifdef HAVE_RB_STR_RESIZE
+ rb_define_method(cls, "rb_str_resize", string_spec_rb_str_resize, 2);
+ rb_define_method(cls, "rb_str_resize_RSTRING_LEN",
+ string_spec_rb_str_resize_RSTRING_LEN, 2);
+#endif
+
+#ifdef HAVE_RB_STR_SET_LEN
+ rb_define_method(cls, "rb_str_set_len", string_spec_rb_str_set_len, 2);
+ rb_define_method(cls, "rb_str_set_len_RSTRING_LEN",
+ string_spec_rb_str_set_len_RSTRING_LEN, 2);
+#endif
+
+#ifdef HAVE_RB_STR_SPLIT
+ rb_define_method(cls, "rb_str_split", string_spec_rb_str_split, 1);
+#endif
+
+#ifdef HAVE_RB_STR_SUBSEQ
+ rb_define_method(cls, "rb_str_subseq", string_spec_rb_str_subseq, 3);
+#endif
+
+#ifdef HAVE_RB_STR_SUBSTR
+ rb_define_method(cls, "rb_str_substr", string_spec_rb_str_substr, 3);
+#endif
+
+#ifdef HAVE_RB_STR_TO_STR
+ rb_define_method(cls, "rb_str_to_str", string_spec_rb_str_to_str, 1);
+#endif
+
+#ifdef HAVE_RSTRING_LEN
+ rb_define_method(cls, "RSTRING_LEN", string_spec_RSTRING_LEN, 1);
+#endif
+
+#ifdef HAVE_RSTRING_LENINT
+ rb_define_method(cls, "RSTRING_LENINT", string_spec_RSTRING_LENINT, 1);
+#endif
+
+#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);
+#endif
+
+#ifdef HAVE_STRINGVALUE
+ rb_define_method(cls, "StringValue", string_spec_StringValue, 1);
+#endif
+
+#ifdef HAVE_RB_STR_HASH
+ rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1);
+#endif
+
+#ifdef HAVE_RB_STR_UPDATE
+ rb_define_method(cls, "rb_str_update", string_spec_rb_str_update, 4);
+#endif
+
+#ifdef HAVE_RB_STR_FREE
+ rb_define_method(cls, "rb_str_free", string_spec_rb_str_free, 1);
+#endif
+
+#ifdef HAVE_RB_SPRINTF
+ rb_define_method(cls, "rb_sprintf1", string_spec_rb_sprintf1, 2);
+ rb_define_method(cls, "rb_sprintf2", string_spec_rb_sprintf2, 3);
+#endif
+
+#ifdef HAVE_RB_VSPRINTF
+ rb_define_method(cls, "rb_vsprintf", string_spec_rb_vsprintf, 4);
+#endif
+
+#ifdef HAVE_RB_STR_EQUAL
+ rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2);
+#endif
+
+#ifdef HAVE_RB_USASCII_STR_NEW
+ rb_define_method(cls, "rb_usascii_str_new", string_spec_rb_usascii_str_new, 2);
+#endif
+
+#ifdef HAVE_RB_USASCII_STR_NEW_CSTR
+ rb_define_method(cls, "rb_usascii_str_new_cstr", string_spec_rb_usascii_str_new_cstr, 1);
+#endif
+
+#ifdef HAVE_RB_STRING
+ rb_define_method(cls, "rb_String", string_spec_rb_String, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/struct_spec.c b/spec/ruby/optional/capi/ext/struct_spec.c
new file mode 100644
index 0000000000..8f373d9f48
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/struct_spec.c
@@ -0,0 +1,131 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include "ruby/intern.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_STRUCT_AREF
+static VALUE struct_spec_rb_struct_aref(VALUE self, VALUE st, VALUE key) {
+ return rb_struct_aref(st, key);
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_GETMEMBER
+static VALUE struct_spec_rb_struct_getmember(VALUE self, VALUE st, VALUE key) {
+ return rb_struct_getmember(st, SYM2ID(key));
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_S_MEMBERS
+static VALUE struct_spec_rb_struct_s_members(VALUE self, VALUE klass)
+{
+ return rb_ary_dup(rb_struct_s_members(klass));
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_MEMBERS
+static VALUE struct_spec_rb_struct_members(VALUE self, VALUE st)
+{
+ return rb_ary_dup(rb_struct_members(st));
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_ASET
+static VALUE struct_spec_rb_struct_aset(VALUE self, VALUE st, VALUE key, VALUE value) {
+ return rb_struct_aset(st, key, value);
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_DEFINE
+/* Only allow setting three attributes, should be sufficient for testing. */
+static VALUE struct_spec_struct_define(VALUE self, VALUE name,
+ VALUE attr1, VALUE attr2, VALUE attr3) {
+
+ const char *a1 = StringValuePtr(attr1);
+ const char *a2 = StringValuePtr(attr2);
+ const char *a3 = StringValuePtr(attr3);
+ char *nm = NULL;
+
+ if (name != Qnil) nm = StringValuePtr(name);
+
+ return rb_struct_define(nm, a1, a2, a3, NULL);
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_DEFINE_UNDER
+/* Only allow setting three attributes, should be sufficient for testing. */
+static VALUE struct_spec_struct_define_under(VALUE self, VALUE outer,
+ VALUE name, VALUE attr1, VALUE attr2, VALUE attr3) {
+
+ const char *nm = StringValuePtr(name);
+ const char *a1 = StringValuePtr(attr1);
+ const char *a2 = StringValuePtr(attr2);
+ const char *a3 = StringValuePtr(attr3);
+
+ return rb_struct_define_under(outer, nm, a1, a2, a3, NULL);
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_NEW
+static VALUE struct_spec_rb_struct_new(VALUE self, VALUE klass,
+ VALUE a, VALUE b, VALUE c)
+{
+
+ return rb_struct_new(klass, a, b, c);
+}
+#endif
+
+#ifdef HAVE_RB_STRUCT_SIZE
+static VALUE struct_spec_rb_struct_size(VALUE self, VALUE st)
+{
+ return rb_struct_size(st);
+}
+#endif
+
+void Init_struct_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiStructSpecs", rb_cObject);
+
+#ifdef HAVE_RB_STRUCT_AREF
+ rb_define_method(cls, "rb_struct_aref", struct_spec_rb_struct_aref, 2);
+#endif
+
+#ifdef HAVE_RB_STRUCT_GETMEMBER
+ rb_define_method(cls, "rb_struct_getmember", struct_spec_rb_struct_getmember, 2);
+#endif
+
+#ifdef HAVE_RB_STRUCT_S_MEMBERS
+ rb_define_method(cls, "rb_struct_s_members", struct_spec_rb_struct_s_members, 1);
+#endif
+
+#ifdef HAVE_RB_STRUCT_MEMBERS
+ rb_define_method(cls, "rb_struct_members", struct_spec_rb_struct_members, 1);
+#endif
+
+#ifdef HAVE_RB_STRUCT_ASET
+ rb_define_method(cls, "rb_struct_aset", struct_spec_rb_struct_aset, 3);
+#endif
+
+#ifdef HAVE_RB_STRUCT_DEFINE
+ rb_define_method(cls, "rb_struct_define", struct_spec_struct_define, 4);
+#endif
+
+#ifdef HAVE_RB_STRUCT_DEFINE_UNDER
+ rb_define_method(cls, "rb_struct_define_under", struct_spec_struct_define_under, 5);
+#endif
+
+#ifdef HAVE_RB_STRUCT_NEW
+ rb_define_method(cls, "rb_struct_new", struct_spec_rb_struct_new, 4);
+#endif
+
+#ifdef HAVE_RB_STRUCT_SIZE
+ rb_define_method(cls, "rb_struct_size", struct_spec_rb_struct_size, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/symbol_spec.c b/spec/ruby/optional/capi/ext/symbol_spec.c
new file mode 100644
index 0000000000..7ffa7cf9b1
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/symbol_spec.c
@@ -0,0 +1,138 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef HAVE_RUBY_ENCODING_H
+#include "ruby/encoding.h"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_INTERN
+VALUE symbol_spec_rb_intern(VALUE self, VALUE string) {
+ return ID2SYM(rb_intern(RSTRING_PTR(string)));
+}
+
+VALUE symbol_spec_rb_intern2(VALUE self, VALUE string, VALUE len) {
+ return ID2SYM(rb_intern2(RSTRING_PTR(string), FIX2LONG(len)));
+}
+
+VALUE symbol_spec_rb_intern_const(VALUE self, VALUE string) {
+ return ID2SYM(rb_intern_const(RSTRING_PTR(string)));
+}
+
+VALUE symbol_spec_rb_intern_c_compare(VALUE self, VALUE string, VALUE sym) {
+ ID symbol = rb_intern(RSTRING_PTR(string));
+ return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
+}
+
+VALUE symbol_spec_rb_intern2_c_compare(VALUE self, VALUE string, VALUE len, VALUE sym) {
+ ID symbol = rb_intern2(RSTRING_PTR(string), FIX2LONG(len));
+ return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_INTERN3
+VALUE symbol_spec_rb_intern3(VALUE self, VALUE string, VALUE len, VALUE enc) {
+ return ID2SYM(rb_intern3(RSTRING_PTR(string), FIX2LONG(len), rb_enc_get(enc)));
+}
+
+VALUE symbol_spec_rb_intern3_c_compare(VALUE self, VALUE string, VALUE len, VALUE enc, VALUE sym) {
+ ID symbol = rb_intern3(RSTRING_PTR(string), FIX2LONG(len), rb_enc_get(enc));
+ return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_ID2NAME
+VALUE symbol_spec_rb_id2name(VALUE self, VALUE symbol) {
+ const char* c_str = rb_id2name(SYM2ID(symbol));
+ return rb_str_new(c_str, strlen(c_str));
+}
+#endif
+
+#ifdef HAVE_RB_ID2STR
+VALUE symbol_spec_rb_id2str(VALUE self, VALUE symbol) {
+ return rb_id2str(SYM2ID(symbol));
+}
+#endif
+
+#ifdef HAVE_RB_INTERN_STR
+VALUE symbol_spec_rb_intern_str(VALUE self, VALUE str) {
+ return ID2SYM(rb_intern_str(str));
+}
+#endif
+
+#ifdef HAVE_RB_IS_CLASS_ID
+VALUE symbol_spec_rb_is_class_id(VALUE self, VALUE sym) {
+ return rb_is_class_id(SYM2ID(sym)) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_IS_CONST_ID
+VALUE symbol_spec_rb_is_const_id(VALUE self, VALUE sym) {
+ return rb_is_const_id(SYM2ID(sym)) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_IS_INSTANCE_ID
+VALUE symbol_spec_rb_is_instance_id(VALUE self, VALUE sym) {
+ return rb_is_instance_id(SYM2ID(sym)) ? Qtrue : Qfalse;
+}
+#endif
+
+#ifdef HAVE_RB_SYM2STR
+VALUE symbol_spec_rb_sym2str(VALUE self, VALUE sym) {
+ return rb_sym2str(sym);
+}
+#endif
+
+void Init_symbol_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiSymbolSpecs", rb_cObject);
+
+#ifdef HAVE_RB_INTERN
+ rb_define_method(cls, "rb_intern", symbol_spec_rb_intern, 1);
+ rb_define_method(cls, "rb_intern2", symbol_spec_rb_intern2, 2);
+ rb_define_method(cls, "rb_intern_const", symbol_spec_rb_intern_const, 1);
+ rb_define_method(cls, "rb_intern_c_compare", symbol_spec_rb_intern_c_compare, 2);
+ rb_define_method(cls, "rb_intern2_c_compare", symbol_spec_rb_intern2_c_compare, 3);
+#endif
+
+#ifdef HAVE_RB_INTERN3
+ rb_define_method(cls, "rb_intern3", symbol_spec_rb_intern3, 3);
+ rb_define_method(cls, "rb_intern3_c_compare", symbol_spec_rb_intern3_c_compare, 4);
+#endif
+
+#ifdef HAVE_RB_ID2NAME
+ rb_define_method(cls, "rb_id2name", symbol_spec_rb_id2name, 1);
+#endif
+
+#ifdef HAVE_RB_ID2STR
+ rb_define_method(cls, "rb_id2str", symbol_spec_rb_id2str, 1);
+#endif
+
+#ifdef HAVE_RB_INTERN_STR
+ rb_define_method(cls, "rb_intern_str", symbol_spec_rb_intern_str, 1);
+#endif
+
+#ifdef HAVE_RB_IS_CLASS_ID
+ rb_define_method(cls, "rb_is_class_id", symbol_spec_rb_is_class_id, 1);
+#endif
+
+#ifdef HAVE_RB_IS_CONST_ID
+ rb_define_method(cls, "rb_is_const_id", symbol_spec_rb_is_const_id, 1);
+#endif
+
+#ifdef HAVE_RB_IS_INSTANCE_ID
+ rb_define_method(cls, "rb_is_instance_id", symbol_spec_rb_is_instance_id, 1);
+#endif
+
+#ifdef HAVE_RB_SYM2STR
+ rb_define_method(cls, "rb_sym2str", symbol_spec_rb_sym2str, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/thread_spec.c b/spec/ruby/optional/capi/ext/thread_spec.c
new file mode 100644
index 0000000000..3bfe76b925
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/thread_spec.c
@@ -0,0 +1,188 @@
+#include "ruby.h"
+#include "ruby/thread.h"
+#include "rubyspec.h"
+
+#include <math.h>
+#include <errno.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if defined(_WIN32)
+#define pipe(p) rb_w32_pipe(p)
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_THREAD_ALONE
+static VALUE thread_spec_rb_thread_alone() {
+ return rb_thread_alone() ? Qtrue : Qfalse;
+}
+#endif
+
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+
+#ifdef HAVE_RB_THREAD_CALL_WITHOUT_GVL
+/* This is unblocked by unblock_func(). */
+static void* blocking_gvl_func(void* data) {
+ int rfd = *(int *)data;
+ char dummy;
+ ssize_t r;
+
+ do {
+ r = read(rfd, &dummy, 1);
+ } while (r == -1 && errno == EINTR);
+
+ close(rfd);
+
+ return (void*)((r == 1) ? Qtrue : Qfalse);
+}
+
+static void unblock_gvl_func(void *data) {
+ int wfd = *(int *)data;
+ char dummy = 'A';
+ ssize_t r;
+
+ do {
+ r = write(wfd, &dummy, 1);
+ } while (r == -1 && errno == EINTR);
+
+ close(wfd);
+}
+
+/* Returns true if the thread is interrupted. */
+static VALUE thread_spec_rb_thread_call_without_gvl(VALUE self) {
+ int fds[2];
+ void* ret;
+
+ if (pipe(fds) == -1) {
+ return Qfalse;
+ }
+ ret = rb_thread_call_without_gvl(blocking_gvl_func, &fds[0],
+ unblock_gvl_func, &fds[1]);
+ return (VALUE)ret;
+}
+
+/* This is unblocked by a signal. */
+static void* blocking_gvl_func_for_udf_io(void *data) {
+ int rfd = (int)(size_t)data;
+ char dummy;
+
+ if (read(rfd, &dummy, 1) == -1 && errno == EINTR) {
+ return (void*)Qtrue;
+ } else {
+ return (void*)Qfalse;
+ }
+}
+
+/* Returns true if the thread is interrupted. */
+static VALUE thread_spec_rb_thread_call_without_gvl_with_ubf_io(VALUE self) {
+ int fds[2];
+ void* ret;
+
+ if (pipe(fds) == -1) {
+ return Qfalse;
+ }
+
+ ret = rb_thread_call_without_gvl(blocking_gvl_func_for_udf_io,
+ (void*)(size_t)fds[0], RUBY_UBF_IO, 0);
+ close(fds[0]);
+ close(fds[1]);
+ return (VALUE)ret;
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_CURRENT
+static VALUE thread_spec_rb_thread_current() {
+ return rb_thread_current();
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_LOCAL_AREF
+static VALUE thread_spec_rb_thread_local_aref(VALUE self, VALUE thr, VALUE sym) {
+ return rb_thread_local_aref(thr, SYM2ID(sym));
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_LOCAL_ASET
+static VALUE thread_spec_rb_thread_local_aset(VALUE self, VALUE thr, VALUE sym, VALUE value) {
+ return rb_thread_local_aset(thr, SYM2ID(sym), value);
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_WAKEUP
+static VALUE thread_spec_rb_thread_wakeup(VALUE self, VALUE thr) {
+ return rb_thread_wakeup(thr);
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_WAIT_FOR
+static VALUE thread_spec_rb_thread_wait_for(VALUE self, VALUE s, VALUE ms) {
+ struct timeval tv;
+ tv.tv_sec = NUM2INT(s);
+ tv.tv_usec = NUM2INT(ms);
+ rb_thread_wait_for(tv);
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_THREAD_CREATE
+
+VALUE thread_spec_call_proc(VALUE arg_array) {
+ VALUE arg = rb_ary_pop(arg_array);
+ VALUE proc = rb_ary_pop(arg_array);
+ return rb_funcall(proc, rb_intern("call"), 1, arg);
+}
+
+static VALUE thread_spec_rb_thread_create(VALUE self, VALUE proc, VALUE arg) {
+ VALUE args = rb_ary_new();
+ rb_ary_push(args, proc);
+ rb_ary_push(args, arg);
+
+ return rb_thread_create(thread_spec_call_proc, (void*)args);
+}
+#endif
+
+
+void Init_thread_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiThreadSpecs", rb_cObject);
+
+#ifdef HAVE_RB_THREAD_ALONE
+ rb_define_method(cls, "rb_thread_alone", thread_spec_rb_thread_alone, 0);
+#endif
+
+#ifdef HAVE_RB_THREAD_CALL_WITHOUT_GVL
+ rb_define_method(cls, "rb_thread_call_without_gvl", thread_spec_rb_thread_call_without_gvl, 0);
+ rb_define_method(cls, "rb_thread_call_without_gvl_with_ubf_io", thread_spec_rb_thread_call_without_gvl_with_ubf_io, 0);
+#endif
+
+#ifdef HAVE_RB_THREAD_CURRENT
+ rb_define_method(cls, "rb_thread_current", thread_spec_rb_thread_current, 0);
+#endif
+
+#ifdef HAVE_RB_THREAD_LOCAL_AREF
+ rb_define_method(cls, "rb_thread_local_aref", thread_spec_rb_thread_local_aref, 2);
+#endif
+
+#ifdef HAVE_RB_THREAD_LOCAL_ASET
+ rb_define_method(cls, "rb_thread_local_aset", thread_spec_rb_thread_local_aset, 3);
+#endif
+
+#ifdef HAVE_RB_THREAD_WAKEUP
+ rb_define_method(cls, "rb_thread_wakeup", thread_spec_rb_thread_wakeup, 1);
+#endif
+
+#ifdef HAVE_RB_THREAD_WAIT_FOR
+ rb_define_method(cls, "rb_thread_wait_for", thread_spec_rb_thread_wait_for, 2);
+#endif
+
+#ifdef HAVE_RB_THREAD_CREATE
+ rb_define_method(cls, "rb_thread_create", thread_spec_rb_thread_create, 2);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/time_spec.c b/spec/ruby/optional/capi/ext/time_spec.c
new file mode 100644
index 0000000000..6b51120f1b
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/time_spec.c
@@ -0,0 +1,127 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_TIME_NEW
+static VALUE time_spec_rb_time_new(VALUE self, VALUE sec, VALUE usec) {
+ return rb_time_new(NUM2TIMET(sec), NUM2LONG(usec));
+}
+#endif
+
+#ifdef HAVE_RB_TIME_NANO_NEW
+static VALUE time_spec_rb_time_nano_new(VALUE self, VALUE sec, VALUE nsec) {
+ return rb_time_nano_new(NUM2TIMET(sec), NUM2LONG(nsec));
+}
+#endif
+
+#ifdef HAVE_RB_TIME_NUM_NEW
+static VALUE time_spec_rb_time_num_new(VALUE self, VALUE ts, VALUE offset) {
+ return rb_time_num_new(ts, offset);
+}
+#endif
+
+#ifdef HAVE_RB_TIME_INTERVAL
+static VALUE time_spec_rb_time_interval(VALUE self, VALUE ts) {
+ struct timeval interval = rb_time_interval(ts);
+ VALUE ary = rb_ary_new();
+ rb_ary_push(ary, TIMET2NUM(interval.tv_sec));
+ rb_ary_push(ary, TIMET2NUM(interval.tv_usec));
+ return ary;
+}
+#endif
+
+#ifdef HAVE_RB_TIME_TIMEVAL
+static VALUE time_spec_rb_time_timeval(VALUE self, VALUE ts) {
+ struct timeval tv = rb_time_timeval(ts);
+ VALUE ary = rb_ary_new();
+ rb_ary_push(ary, TIMET2NUM(tv.tv_sec));
+ rb_ary_push(ary, TIMET2NUM(tv.tv_usec));
+ return ary;
+}
+#endif
+
+#ifdef HAVE_RB_TIME_TIMESPEC
+static VALUE time_spec_rb_time_timespec(VALUE self, VALUE time) {
+ struct timespec ts = rb_time_timespec(time);
+ VALUE ary = rb_ary_new();
+ rb_ary_push(ary, TIMET2NUM(ts.tv_sec));
+ rb_ary_push(ary, TIMET2NUM(ts.tv_nsec));
+ return ary;
+}
+#endif
+
+#ifdef HAVE_RB_TIME_TIMESPEC_NEW
+static VALUE time_spec_rb_time_timespec_new(VALUE self, VALUE sec, VALUE nsec, VALUE offset) {
+ struct timespec ts;
+ ts.tv_sec = NUM2TIMET(sec);
+ ts.tv_nsec = NUM2LONG(nsec);
+
+ return rb_time_timespec_new(&ts, NUM2INT(offset));
+}
+#endif
+
+#ifdef HAVE_RB_TIMESPEC_NOW
+static VALUE time_spec_rb_time_from_timspec_now(VALUE self, VALUE offset) {
+ struct timespec ts;
+ rb_timespec_now(&ts);
+
+ return rb_time_timespec_new(&ts, NUM2INT(offset));
+}
+#endif
+
+#ifdef HAVE_TIMET2NUM
+static VALUE time_spec_TIMET2NUM(VALUE self) {
+ time_t t = 10;
+ return TIMET2NUM(t);
+}
+#endif
+
+void Init_time_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiTimeSpecs", rb_cObject);
+
+#ifdef HAVE_RB_TIME_NEW
+ rb_define_method(cls, "rb_time_new", time_spec_rb_time_new, 2);
+#endif
+
+#ifdef HAVE_TIMET2NUM
+ rb_define_method(cls, "TIMET2NUM", time_spec_TIMET2NUM, 0);
+#endif
+
+#ifdef HAVE_RB_TIME_NANO_NEW
+ rb_define_method(cls, "rb_time_nano_new", time_spec_rb_time_nano_new, 2);
+#endif
+
+#ifdef HAVE_RB_TIME_NUM_NEW
+ rb_define_method(cls, "rb_time_num_new", time_spec_rb_time_num_new, 2);
+#endif
+
+#ifdef HAVE_RB_TIME_INTERVAL
+ rb_define_method(cls, "rb_time_interval", time_spec_rb_time_interval, 1);
+#endif
+
+#ifdef HAVE_RB_TIME_TIMEVAL
+ rb_define_method(cls, "rb_time_timeval", time_spec_rb_time_timeval, 1);
+#endif
+
+#ifdef HAVE_RB_TIME_TIMESPEC
+ rb_define_method(cls, "rb_time_timespec", time_spec_rb_time_timespec, 1);
+#endif
+
+#ifdef HAVE_RB_TIME_TIMESPEC_NEW
+ rb_define_method(cls, "rb_time_timespec_new", time_spec_rb_time_timespec_new, 3);
+#endif
+
+#ifdef HAVE_RB_TIMESPEC_NOW
+ rb_define_method(cls, "rb_time_from_timespec", time_spec_rb_time_from_timspec_now, 1);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/typed_data_spec.c b/spec/ruby/optional/capi/ext/typed_data_spec.c
new file mode 100644
index 0000000000..8c9a0708b4
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/typed_data_spec.c
@@ -0,0 +1,177 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(HAVE_RTYPEDDATA) && defined(HAVE_TYPEDDATA_WRAP_STRUCT)
+struct sample_typed_wrapped_struct_parent {
+ int foo;
+};
+
+void sample_typed_wrapped_struct_parent_free(void* st) {
+ free(st);
+}
+
+void sample_typed_wrapped_struct_parent_mark(void* st) {
+}
+
+size_t sample_typed_wrapped_struct_parent_memsize(const void* st) {
+ return sizeof(struct sample_typed_wrapped_struct_parent);
+}
+
+static const rb_data_type_t sample_typed_wrapped_struct_parent_data_type = {
+ "sample_typed_wrapped_struct_parent",
+ {
+ sample_typed_wrapped_struct_parent_mark,
+ sample_typed_wrapped_struct_parent_free,
+ sample_typed_wrapped_struct_parent_memsize,
+ },
+};
+
+struct sample_typed_wrapped_struct {
+ int foo;
+};
+
+void sample_typed_wrapped_struct_free(void* st) {
+ free(st);
+}
+
+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);
+}
+
+static const rb_data_type_t sample_typed_wrapped_struct_data_type = {
+ "sample_typed_wrapped_struct",
+ {
+ sample_typed_wrapped_struct_mark,
+ sample_typed_wrapped_struct_free,
+ sample_typed_wrapped_struct_memsize,
+ },
+ &sample_typed_wrapped_struct_parent_data_type,
+};
+
+struct sample_typed_wrapped_struct_other {
+ int foo;
+};
+
+void sample_typed_wrapped_struct_other_free(void* st) {
+ free(st);
+}
+
+void sample_typed_wrapped_struct_other_mark(void* st) {
+}
+
+size_t sample_typed_wrapped_struct_other_memsize(const void* st) {
+ return sizeof(struct sample_typed_wrapped_struct_other);
+}
+
+static const rb_data_type_t sample_typed_wrapped_struct_other_data_type = {
+ "sample_typed_wrapped_struct_other",
+ {
+ sample_typed_wrapped_struct_other_mark,
+ sample_typed_wrapped_struct_other_free,
+ sample_typed_wrapped_struct_other_memsize,
+ },
+};
+
+
+VALUE sdaf_alloc_typed_func(VALUE klass) {
+ struct sample_typed_wrapped_struct* bar = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct));
+ bar->foo = 42;
+ return TypedData_Wrap_Struct(klass, &sample_typed_wrapped_struct_data_type, bar);
+}
+
+VALUE sdaf_typed_get_struct(VALUE self) {
+ struct sample_typed_wrapped_struct* bar;
+ TypedData_Get_Struct(self, struct sample_typed_wrapped_struct, &sample_typed_wrapped_struct_data_type, bar);
+
+ return INT2FIX((*bar).foo);
+}
+
+VALUE sws_typed_wrap_struct(VALUE self, VALUE val) {
+ struct sample_typed_wrapped_struct* bar = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct));
+ bar->foo = FIX2INT(val);
+ return TypedData_Wrap_Struct(rb_cObject, &sample_typed_wrapped_struct_data_type, bar);
+}
+
+VALUE sws_typed_wrap_struct_null(VALUE self, VALUE val) {
+ struct sample_typed_wrapped_struct* bar = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct));
+ bar->foo = FIX2INT(val);
+ return TypedData_Wrap_Struct(0, &sample_typed_wrapped_struct_data_type, bar);
+}
+
+VALUE sws_typed_get_struct(VALUE self, VALUE obj) {
+ struct sample_typed_wrapped_struct* bar;
+ TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct, &sample_typed_wrapped_struct_data_type, bar);
+
+ return INT2FIX((*bar).foo);
+}
+
+VALUE sws_typed_get_struct_different_type(VALUE self, VALUE obj) {
+ struct sample_typed_wrapped_struct_other* bar;
+ TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct_other, &sample_typed_wrapped_struct_other_data_type, bar);
+
+ return INT2FIX((*bar).foo);
+}
+
+VALUE sws_typed_get_struct_parent_type(VALUE self, VALUE obj) {
+ struct sample_typed_wrapped_struct_parent* bar;
+ TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct_parent, &sample_typed_wrapped_struct_parent_data_type, bar);
+
+ return INT2FIX((*bar).foo);
+}
+
+VALUE sws_typed_get_struct_rdata(VALUE self, VALUE obj) {
+ struct sample_typed_wrapped_struct* bar;
+ bar = (struct sample_typed_wrapped_struct*) RTYPEDDATA(obj)->data;
+ return INT2FIX(bar->foo);
+}
+
+VALUE sws_typed_get_struct_data_ptr(VALUE self, VALUE obj) {
+ struct sample_typed_wrapped_struct* bar;
+ bar = (struct sample_typed_wrapped_struct*) DATA_PTR(obj);
+ return INT2FIX(bar->foo);
+}
+
+VALUE sws_typed_change_struct(VALUE self, VALUE obj, VALUE new_val) {
+ struct sample_typed_wrapped_struct *old_struct, *new_struct;
+ new_struct = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct));
+ new_struct->foo = FIX2INT(new_val);
+ old_struct = RTYPEDDATA(obj)->data;
+ free(old_struct);
+ RTYPEDDATA(obj)->data = new_struct;
+ return Qnil;
+}
+#endif
+
+void Init_typed_data_spec(void) {
+ VALUE cls;
+ cls = rb_define_class("CApiAllocTypedSpecs", rb_cObject);
+
+#if defined(HAVE_RTYPEDDATA) && defined(HAVE_TYPEDDATA_WRAP_STRUCT)
+ rb_define_alloc_func(cls, sdaf_alloc_typed_func);
+ rb_define_method(cls, "typed_wrapped_data", sdaf_typed_get_struct, 0);
+
+ cls = rb_define_class("CApiWrappedTypedStructSpecs", rb_cObject);
+ rb_define_method(cls, "typed_wrap_struct", sws_typed_wrap_struct, 1);
+ rb_define_method(cls, "typed_wrap_struct_null", sws_typed_wrap_struct_null, 1);
+ rb_define_method(cls, "typed_get_struct", sws_typed_get_struct, 1);
+ rb_define_method(cls, "typed_get_struct_other", sws_typed_get_struct_different_type, 1);
+ rb_define_method(cls, "typed_get_struct_parent", sws_typed_get_struct_parent_type, 1);
+ 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);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/spec/ruby/optional/capi/ext/util_spec.c b/spec/ruby/optional/capi/ext/util_spec.c
new file mode 100644
index 0000000000..50795b51af
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/util_spec.c
@@ -0,0 +1,95 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_RB_SCAN_ARGS
+VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, VALUE acc) {
+ int i, result, argc = (int)RARRAY_LEN(argv);
+ VALUE args[6], failed, a1, a2, a3, a4, a5, a6;
+
+ failed = rb_intern("failed");
+ a1 = a2 = a3 = a4 = a5 = a6 = failed;
+
+ for(i = 0; i < argc; i++) {
+ args[i] = rb_ary_entry(argv, i);
+ }
+
+ result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6);
+
+ switch(NUM2INT(expected)) {
+ case 6:
+ rb_ary_unshift(acc, a6);
+ case 5:
+ rb_ary_unshift(acc, a5);
+ case 4:
+ rb_ary_unshift(acc, a4);
+ case 3:
+ rb_ary_unshift(acc, a3);
+ case 2:
+ rb_ary_unshift(acc, a2);
+ case 1:
+ rb_ary_unshift(acc, a1);
+ break;
+ default:
+ rb_raise(rb_eException, "unexpected number of arguments returned by rb_scan_args");
+ }
+
+ return INT2NUM(result);
+}
+#endif
+
+#ifdef HAVE_RB_LONG2INT
+static VALUE util_spec_rb_long2int(VALUE self, VALUE n) {
+ return INT2NUM(rb_long2int(NUM2LONG(n)));
+}
+#endif
+
+#ifdef HAVE_RB_ITER_BREAK
+static VALUE util_spec_rb_iter_break(VALUE self) {
+ rb_iter_break();
+ return Qnil;
+}
+#endif
+
+#ifdef HAVE_RB_SOURCEFILE
+static VALUE util_spec_rb_sourcefile(VALUE self) {
+ return rb_str_new2(rb_sourcefile());
+}
+#endif
+
+#ifdef HAVE_RB_SOURCELINE
+static VALUE util_spec_rb_sourceline(VALUE self) {
+ return INT2NUM(rb_sourceline());
+}
+#endif
+
+void Init_util_spec(void) {
+ VALUE cls = rb_define_class("CApiUtilSpecs", rb_cObject);
+
+#ifdef HAVE_RB_SCAN_ARGS
+ rb_define_method(cls, "rb_scan_args", util_spec_rb_scan_args, 4);
+#endif
+
+#ifdef HAVE_RB_LONG2INT
+ rb_define_method(cls, "rb_long2int", util_spec_rb_long2int, 1);
+#endif
+
+#ifdef HAVE_RB_ITER_BREAK
+ rb_define_method(cls, "rb_iter_break", util_spec_rb_iter_break, 0);
+#endif
+
+#ifdef HAVE_RB_SOURCEFILE
+ rb_define_method(cls, "rb_sourcefile", util_spec_rb_sourcefile, 0);
+#endif
+
+#ifdef HAVE_RB_SOURCELINE
+ rb_define_method(cls, "rb_sourceline", util_spec_rb_sourceline, 0);
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/file_spec.rb b/spec/ruby/optional/capi/file_spec.rb
new file mode 100644
index 0000000000..2459dba979
--- /dev/null
+++ b/spec/ruby/optional/capi/file_spec.rb
@@ -0,0 +1,89 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension('file')
+
+describe :rb_file_open, shared: true do
+ it "raises an ArgumentError if passed an empty mode string" do
+ touch @name
+ lambda { @s.rb_file_open(@name, "") }.should raise_error(ArgumentError)
+ end
+
+ it "opens a file in read-only mode with 'r'" do
+ touch(@name) { |f| f.puts "readable" }
+ @file = @s.send(@method, @name, "r")
+ @file.should be_an_instance_of(File)
+ @file.read.chomp.should == "readable"
+ end
+
+ it "creates and opens a non-existent file with 'w'" do
+ @file = @s.send(@method, @name, "w")
+ @file.write "writable"
+ @file.flush
+ File.read(@name).should == "writable"
+ end
+
+ it "truncates an existing file with 'w'" do
+ touch(@name) { |f| f.puts "existing" }
+ @file = @s.send(@method, @name, "w")
+ File.read(@name).should == ""
+ end
+end
+
+describe "C-API File function" do
+ before :each do
+ @s = CApiFileSpecs.new
+ @name = tmp("rb_file_open")
+ end
+
+ after :each do
+ @file.close if @file and !@file.closed?
+ rm_r @name
+ end
+
+ describe "rb_file_open" do
+ it_behaves_like :rb_file_open, :rb_file_open
+ end
+
+ describe "rb_file_open_str" do
+ it_behaves_like :rb_file_open, :rb_file_open_str
+ end
+
+ describe "rb_file_open_str" do
+ it "calls #to_path to convert on object to a path" do
+ path = mock("rb_file_open_str to_path")
+ path.should_receive(:to_path).and_return(@name)
+ @file = @s.rb_file_open_str(path, "w")
+ end
+
+ it "calls #to_str to convert an object to a path if #to_path isn't defined" do
+ path = mock("rb_file_open_str to_str")
+ path.should_receive(:to_str).and_return(@name)
+ @file = @s.rb_file_open_str(path, "w")
+ end
+ end
+
+ describe "FilePathValue" do
+ it "returns a String argument unchanged" do
+ obj = "path"
+ @s.FilePathValue(obj).should eql(obj)
+ end
+
+ it "does not call #to_str on a String" do
+ obj = "path"
+ obj.should_not_receive(:to_str)
+ @s.FilePathValue(obj).should eql(obj)
+ end
+
+ it "calls #to_path to convert an object to a String" do
+ obj = mock("FilePathValue to_path")
+ obj.should_receive(:to_path).and_return("path")
+ @s.FilePathValue(obj).should == "path"
+ end
+
+ it "calls #to_str to convert an object to a String if #to_path isn't defined" do
+ obj = mock("FilePathValue to_str")
+ obj.should_receive(:to_str).and_return("path")
+ @s.FilePathValue(obj).should == "path"
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/fixnum_spec.rb b/spec/ruby/optional/capi/fixnum_spec.rb
new file mode 100644
index 0000000000..d9e9d1946d
--- /dev/null
+++ b/spec/ruby/optional/capi/fixnum_spec.rb
@@ -0,0 +1,124 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("fixnum")
+
+describe "CApiFixnumSpecs" do
+ before :each do
+ @s = CApiFixnumSpecs.new
+ end
+
+ describe "FIX2INT" do
+ it "converts a Fixnum to a native int" do
+ @s.FIX2INT(42).should == 42
+ @s.FIX2INT(-14).should == -14
+ end
+
+ max_int = (1 << 31) - 1
+ min_int = -(1 << 31)
+
+ guard -> { fixnum_min <= min_int and max_int <= fixnum_max } do
+ it "converts a Fixnum representing the minimum and maximum native int" do
+ @s.FIX2INT(max_int).should == max_int
+ @s.FIX2INT(min_int).should == min_int
+ end
+ end
+
+ end
+
+ describe "FIX2UINT" do
+ it "converts a Fixnum to a native int" do
+ @s.FIX2UINT(42).should == 42
+ @s.FIX2UINT(0).should == 0
+ end
+
+ max_uint = (1 << 32) - 1
+
+ guard -> { max_uint <= fixnum_max } do
+ it "converts a Fixnum representing the maximum native uint" do
+ @s.FIX2UINT(max_uint).should == max_uint
+ end
+ end
+
+ end
+
+ platform_is wordsize: 64 do
+ describe "rb_fix2uint" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_fix2uint(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_fix2uint(0).should == 0
+ @s.rb_fix2uint(1).should == 1
+ end
+
+ it "converts the maximum uint value" do
+ @s.rb_fix2uint(0xffff_ffff).should == 0xffff_ffff
+ end
+
+ it "converts a Float" do
+ @s.rb_fix2uint(25.4567).should == 25
+ end
+
+ it "raises a RangeError if the value does not fit a native uint" do
+ # Interestingly, on MRI rb_fix2uint(-1) is allowed
+ lambda { @s.rb_fix2uint(0xffff_ffff+1) }.should raise_error(RangeError)
+ lambda { @s.rb_fix2uint(-(1 << 31) - 1) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError if the value is more than 32bits" do
+ lambda { @s.rb_fix2uint(0xffff_ffff+1) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError if the value is more than 64bits" do
+ lambda { @s.rb_fix2uint(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError)
+ end
+ end
+
+ describe "rb_fix2int" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_fix2int(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_fix2int(-1).should == -1
+ @s.rb_fix2int(1).should == 1
+ end
+
+ it "converts the minimum int value" do
+ @s.rb_fix2int(-(1 << 31)).should == -(1 << 31)
+ end
+
+ it "converts the maximum int value" do
+ @s.rb_fix2int(0x7fff_ffff).should == 0x7fff_ffff
+ end
+
+ it "converts a Float" do
+ @s.rb_fix2int(25.4567).should == 25
+ end
+
+ it "converts a negative Bignum into an signed number" do
+ @s.rb_fix2int(-2147442171).should == -2147442171
+ end
+
+ it "raises a RangeError if the value does not fit a native int" do
+ lambda { @s.rb_fix2int(0x7fff_ffff+1) }.should raise_error(RangeError)
+ lambda { @s.rb_fix2int(-(1 << 31) - 1) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError if the value is more than 32bits" do
+ lambda { @s.rb_fix2int(0xffff_ffff+1) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError if the value is more than 64bits" do
+ lambda { @s.rb_fix2int(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError)
+ end
+
+ it "calls #to_int to coerce the value" do
+ obj = mock("number")
+ obj.should_receive(:to_int).and_return(2)
+ @s.rb_fix2int(obj).should == 2
+ end
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/class.rb b/spec/ruby/optional/capi/fixtures/class.rb
new file mode 100644
index 0000000000..de824b3ab0
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/class.rb
@@ -0,0 +1,82 @@
+class CApiClassSpecs
+ module M
+ def included?
+ true
+ end
+ end
+
+ class Alloc
+ attr_reader :initialized
+ attr_reader :arguments
+
+ def initialize(*args)
+ @initialized = true
+ @arguments = args
+ end
+ end
+
+ class Attr
+ def initialize
+ @foo, @bar, @baz = 1, 2, 3
+ end
+ end
+
+ class CVars
+ @@cvar = :cvar
+ @c_ivar = :c_ivar
+
+ def new_cv
+ @@new_cv if defined? @@new_cv
+ end
+
+ def new_cvar
+ @@new_cvar if defined? @@new_cvar
+ end
+
+ def rbdcv_cvar
+ @@rbdcv_cvar if defined? @@rbdcv_cvar
+ end
+ end
+
+ class Inherited
+ def self.inherited(klass)
+ klass
+ end
+ end
+
+ class NewClass
+ def self.inherited(klass)
+ raise "#{name}.inherited called"
+ end
+ end
+
+ class Super
+ def call_super_method
+ :super_method
+ end
+ end
+
+ class Sub < Super
+ def call_super_method
+ :subclass_method
+ end
+ end
+
+ class SubM < Super
+ include M
+ end
+
+ class SubSub < Sub
+ def call_super_method
+ :subclass_method
+ end
+ end
+
+ class A
+ C = 1
+ autoload :D, File.expand_path('../path_to_class.rb', __FILE__)
+
+ class B
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/const_get.rb b/spec/ruby/optional/capi/fixtures/const_get.rb
new file mode 100644
index 0000000000..b261a07f7e
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/const_get.rb
@@ -0,0 +1,5 @@
+class CApiModuleSpecs
+ class A
+ D = 123
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/const_get_at.rb b/spec/ruby/optional/capi/fixtures/const_get_at.rb
new file mode 100644
index 0000000000..700b570607
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/const_get_at.rb
@@ -0,0 +1,5 @@
+class CApiModuleSpecs
+ class A
+ B = 123
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/const_get_from.rb b/spec/ruby/optional/capi/fixtures/const_get_from.rb
new file mode 100644
index 0000000000..9e297daba7
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/const_get_from.rb
@@ -0,0 +1,5 @@
+class CApiModuleSpecs
+ class A
+ C = 123
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/const_get_object.rb b/spec/ruby/optional/capi/fixtures/const_get_object.rb
new file mode 100644
index 0000000000..03d9365a3e
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/const_get_object.rb
@@ -0,0 +1,3 @@
+class Object
+ CApiModuleSpecsAutoload = 123
+end
diff --git a/spec/ruby/optional/capi/fixtures/encoding.rb b/spec/ruby/optional/capi/fixtures/encoding.rb
new file mode 100644
index 0000000000..0858807aa0
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/encoding.rb
@@ -0,0 +1,3 @@
+class CApiEncodingSpecs
+ class S < String; end
+end
diff --git a/spec/ruby/optional/capi/fixtures/foo.rb b/spec/ruby/optional/capi/fixtures/foo.rb
new file mode 100644
index 0000000000..bc4e8f3f7d
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/foo.rb
@@ -0,0 +1 @@
+$foo = 7
diff --git a/spec/ruby/optional/capi/fixtures/module.rb b/spec/ruby/optional/capi/fixtures/module.rb
new file mode 100644
index 0000000000..ba90eb7181
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/module.rb
@@ -0,0 +1,35 @@
+class Object
+ autoload :CApiModuleSpecsAutoload, File.expand_path('../const_get_object.rb', __FILE__)
+
+ module CApiModuleSpecsModuleA
+ X = 1
+ end
+end
+
+class CApiModuleSpecs
+ class A
+ autoload :B, File.expand_path('../const_get_at.rb', __FILE__)
+ autoload :C, File.expand_path('../const_get_from.rb', __FILE__)
+ autoload :D, File.expand_path('../const_get.rb', __FILE__)
+
+ X = 1
+ end
+
+ class B < A
+ Y = 2
+ end
+
+ class C
+ Z = 3
+ end
+
+ module M
+ end
+
+ class Super
+ end
+
+ autoload :ModuleUnderAutoload, "#{object_path}/module_under_autoload_spec"
+ autoload :RubyUnderAutoload, File.expand_path('../module_autoload', __FILE__)
+
+end
diff --git a/spec/ruby/optional/capi/fixtures/module_autoload.rb b/spec/ruby/optional/capi/fixtures/module_autoload.rb
new file mode 100644
index 0000000000..8130a24421
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/module_autoload.rb
@@ -0,0 +1,4 @@
+class CApiModuleSpecs
+ module RubyUnderAutoload
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/path_to_class.rb b/spec/ruby/optional/capi/fixtures/path_to_class.rb
new file mode 100644
index 0000000000..acd577ff24
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/path_to_class.rb
@@ -0,0 +1,6 @@
+class CApiClassSpecs
+ class A
+ module D
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/fixtures/proc.rb b/spec/ruby/optional/capi/fixtures/proc.rb
new file mode 100644
index 0000000000..fbe37312da
--- /dev/null
+++ b/spec/ruby/optional/capi/fixtures/proc.rb
@@ -0,0 +1,20 @@
+class CApiProcSpecs
+ def call_nothing
+ end
+
+ def call_Proc_new
+ Proc.new
+ end
+
+ def call_block_given?
+ block_given?
+ end
+
+ def call_rb_Proc_new
+ rb_Proc_new(0)
+ end
+
+ def call_rb_Proc_new_with_block
+ rb_Proc_new(0) { :calling_with_block }
+ end
+end
diff --git a/spec/ruby/optional/capi/float_spec.rb b/spec/ruby/optional/capi/float_spec.rb
new file mode 100644
index 0000000000..8d709b2b82
--- /dev/null
+++ b/spec/ruby/optional/capi/float_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("float")
+
+describe "CApiFloatSpecs" do
+ before :each do
+ @f = CApiFloatSpecs.new
+ end
+
+ describe "rb_float_new" do
+ it "creates a new float" do
+ ((@f.new_zero - 0).abs < 0.000001).should == true
+ ((@f.new_point_five - 0.555).abs < 0.000001).should == true
+ end
+ end
+
+ describe "RFLOAT_VALUE" do
+ it "returns the C double value of the Float" do
+ @f.RFLOAT_VALUE(2.3).should == 2.3
+ end
+ end
+
+ describe "rb_Float" do
+ it "creates a new Float from a String" do
+ f = @f.rb_Float("101.99")
+ f.should be_kind_of(Float)
+ f.should eql(101.99)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/gc_spec.rb b/spec/ruby/optional/capi/gc_spec.rb
new file mode 100644
index 0000000000..ee9e11d11c
--- /dev/null
+++ b/spec/ruby/optional/capi/gc_spec.rb
@@ -0,0 +1,54 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("gc")
+
+describe "CApiGCSpecs" do
+ before :each do
+ @f = CApiGCSpecs.new
+ end
+
+ it "correctly gets the value from a registered address" do
+ @f.registered_tagged_address.should == 10
+ @f.registered_tagged_address.object_id.should == @f.registered_tagged_address.object_id
+ @f.registered_reference_address.should == "Globally registered data"
+ @f.registered_reference_address.object_id.should == @f.registered_reference_address.object_id
+ end
+
+ describe "rb_gc_enable" do
+
+ after do
+ GC.enable
+ end
+
+ it "enables GC when disabled" do
+ GC.disable
+ @f.rb_gc_enable.should be_true
+ end
+
+ it "GC stays enabled when enabled" do
+ GC.enable
+ @f.rb_gc_enable.should be_false
+ end
+
+ it "disables GC when enabled" do
+ GC.enable
+ @f.rb_gc_disable.should be_false
+ end
+
+ it "GC stays disabled when disabled" do
+ GC.disable
+ @f.rb_gc_disable.should be_true
+ end
+ end
+
+ describe "rb_gc" do
+
+ it "increases gc count" do
+ gc_count = GC.count
+ @f.rb_gc
+ GC.count.should > gc_count
+ end
+
+ end
+
+end
diff --git a/spec/ruby/optional/capi/globals_spec.rb b/spec/ruby/optional/capi/globals_spec.rb
new file mode 100644
index 0000000000..c6e2ed912b
--- /dev/null
+++ b/spec/ruby/optional/capi/globals_spec.rb
@@ -0,0 +1,224 @@
+require File.expand_path('../spec_helper', __FILE__)
+require "stringio"
+
+load_extension("globals")
+
+describe "CApiGlobalSpecs" do
+ before :each do
+ @f = CApiGlobalSpecs.new
+ end
+
+ it "correctly gets global values" do
+ @f.sb_gv_get("$BLAH").should == nil
+ @f.sb_gv_get("$\\").should == nil
+ @f.sb_gv_get("\\").should == nil # rb_gv_get should change \ to $\
+ end
+
+ it "returns $~" do
+ 'a' =~ /a/
+ @f.sb_gv_get("$~").to_a.should == ['a']
+ @f.sb_gv_get("~").to_a.should == ['a']
+ end
+
+ it "correctly sets global values" do
+ @f.sb_gv_get("$BLAH").should == nil
+ @f.sb_gv_set("$BLAH", 10)
+ begin
+ @f.sb_gv_get("$BLAH").should == 10
+ ensure
+ @f.sb_gv_set("$BLAH", nil)
+ end
+ end
+
+ it "lists all global variables" do
+ @f.rb_f_global_variables.should == Kernel.global_variables
+ end
+
+ it "rb_define_variable should define a new global variable" do
+ @f.rb_define_variable("my_gvar", "ABC")
+ $my_gvar.should == "ABC"
+ $my_gvar = "XYZ"
+ @f.sb_get_global_value.should == "XYZ"
+ end
+
+ it "rb_define_readonly_variable should define a new readonly global variable" do
+ @f.rb_define_readonly_variable("ro_gvar", 15)
+ $ro_gvar.should == 15
+ lambda { $ro_gvar = 10 }.should raise_error(NameError)
+ end
+
+ it "rb_define_hooked_variable should define a C hooked global variable" do
+ @f.rb_define_hooked_variable_2x("$hooked_gvar")
+ $hooked_gvar = 2
+ $hooked_gvar.should == 4
+ end
+
+ describe "rb_rs" do
+ before :each do
+ @dollar_slash = $/
+ end
+
+ after :each do
+ $/ = @dollar_slash
+ end
+
+ it "returns \\n by default" do
+ @f.rb_rs.should == "\n"
+ end
+
+ it "returns the value of $/" do
+ $/ = "foo"
+ @f.rb_rs.should == "foo"
+ end
+ end
+
+ context "rb_std streams" do
+ before :each do
+ @name = tmp("rb_std_streams")
+ @stream = new_io @name
+ end
+
+ after :each do
+ @stream.close
+ rm_r @name
+ end
+
+ describe "rb_stdin" do
+ after :each do
+ $stdin = STDIN
+ end
+
+ it "returns $stdin" do
+ $stdin = @stream
+ @f.rb_stdin.should equal($stdin)
+ end
+ end
+
+ describe "rb_stdout" do
+ after :each do
+ $stdout = STDOUT
+ end
+
+ it "returns $stdout" do
+ $stdout = @stream
+ @f.rb_stdout.should equal($stdout)
+ end
+ end
+
+ describe "rb_stderr" do
+ after :each do
+ $stderr = STDERR
+ end
+
+ it "returns $stderr" do
+ $stderr = @stream
+ @f.rb_stderr.should equal($stderr)
+ end
+ end
+
+ describe "rb_defout" do
+ after :each do
+ $stdout = STDOUT
+ end
+
+ it "returns $stdout" do
+ $stdout = @stream
+ @f.rb_defout.should equal($stdout)
+ end
+ end
+ end
+
+ describe "rb_default_rs" do
+ it "returns \\n" do
+ @f.rb_default_rs.should == "\n"
+ end
+ end
+
+ describe "rb_output_rs" do
+ before :each do
+ @dollar_backslash = $\
+ end
+
+ after :each do
+ $\ = @dollar_backslash
+ end
+
+ it "returns nil by default" do
+ @f.rb_output_rs.should be_nil
+ end
+
+ it "returns the value of $\\" do
+ $\ = "foo"
+ @f.rb_output_rs.should == "foo"
+ end
+ end
+
+ describe "rb_output_fs" do
+ before :each do
+ @dollar_comma = $,
+ end
+
+ after :each do
+ $, = @dollar_comma
+ end
+
+ it "returns nil by default" do
+ @f.rb_output_fs.should be_nil
+ end
+
+ it "returns the value of $\\" do
+ $, = "foo"
+ @f.rb_output_fs.should == "foo"
+ end
+ end
+
+ describe "rb_lastline_set" do
+ it "sets the value of $_" do
+ @f.rb_lastline_set("last line")
+ $_.should == "last line"
+ end
+
+ it "sets a Thread-local value" do
+ $_ = nil
+ running = false
+
+ thr = Thread.new do
+ @f.rb_lastline_set("last line")
+ $_.should == "last line"
+ running = true
+ end
+
+ Thread.pass while thr.status and !running
+ $_.should be_nil
+
+ thr.join
+ end
+ end
+
+ describe "rb_lastline_get" do
+ before do
+ @io = StringIO.new("last line")
+ end
+
+ it "gets the value of $_" do
+ @io.gets
+ @f.rb_lastline_get.should == "last line"
+ end
+
+ it "gets a Thread-local value" do
+ $_ = nil
+ running = false
+
+ thr = Thread.new do
+ @io.gets
+ @f.rb_lastline_get.should == "last line"
+ running = true
+ end
+
+ Thread.pass while thr.status and !running
+ $_.should be_nil
+
+ thr.join
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/hash_spec.rb b/spec/ruby/optional/capi/hash_spec.rb
new file mode 100644
index 0000000000..1c406f0394
--- /dev/null
+++ b/spec/ruby/optional/capi/hash_spec.rb
@@ -0,0 +1,245 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("hash")
+
+describe "C-API Hash function" do
+ before :each do
+ @s = CApiHashSpecs.new
+ end
+
+ describe "rb_hash" do
+ it "calls #hash on the object" do
+ obj = mock("rb_hash")
+ obj.should_receive(:hash).and_return(5)
+ @s.rb_hash(obj).should == 5
+ end
+
+ it "converts a Bignum returned by #hash to a Fixnum" do
+ obj = mock("rb_hash bignum")
+ obj.should_receive(:hash).and_return(bignum_value)
+
+ # The actual conversion is an implementation detail.
+ # We only care that ultimately we get a Fixnum instance.
+ @s.rb_hash(obj).should be_an_instance_of(Fixnum)
+ end
+
+ it "calls #to_int to converts a value returned by #hash to a Fixnum" do
+ obj = mock("rb_hash to_int")
+ obj.should_receive(:hash).and_return(obj)
+ obj.should_receive(:to_int).and_return(12)
+
+ @s.rb_hash(obj).should == 12
+ end
+
+ it "raises a TypeError if the object does not implement #to_int" do
+ obj = mock("rb_hash no to_int")
+ obj.should_receive(:hash).and_return(nil)
+
+ lambda { @s.rb_hash(obj) }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_hash_new" do
+ it "returns a new hash" do
+ @s.rb_hash_new.should == {}
+ end
+
+ it "creates a hash with no default proc" do
+ @s.rb_hash_new {}.default_proc.should be_nil
+ end
+ end
+
+ describe "rb_hash_dup" do
+ it "returns a copy of the hash" do
+ hsh = {}
+ dup = @s.rb_hash_dup(hsh)
+ dup.should == hsh
+ dup.should_not equal(hsh)
+ end
+ end
+
+ describe "rb_hash_freeze" do
+ it "freezes the hash" do
+ @s.rb_hash_freeze({}).frozen?.should be_true
+ end
+ end
+
+ describe "rb_hash_aref" do
+ it "returns the value associated with the key" do
+ hsh = {chunky: 'bacon'}
+ @s.rb_hash_aref(hsh, :chunky).should == 'bacon'
+ end
+
+ it "returns the default value if it exists" do
+ hsh = Hash.new(0)
+ @s.rb_hash_aref(hsh, :chunky).should == 0
+ @s.rb_hash_aref_nil(hsh, :chunky).should be_false
+ end
+
+ it "returns nil if the key does not exist" do
+ hsh = { }
+ @s.rb_hash_aref(hsh, :chunky).should be_nil
+ @s.rb_hash_aref_nil(hsh, :chunky).should be_true
+ end
+ end
+
+ describe "rb_hash_aset" do
+ it "adds the key/value pair and returns the value" do
+ hsh = {}
+ @s.rb_hash_aset(hsh, :chunky, 'bacon').should == 'bacon'
+ hsh.should == {chunky: 'bacon'}
+ end
+ end
+
+ describe "rb_hash_clear" do
+ it "returns self that cleared keys and values" do
+ hsh = { :key => 'value' }
+ @s.rb_hash_clear(hsh).should equal(hsh)
+ hsh.should == {}
+ end
+ end
+
+ describe "rb_hash_delete" do
+ it "removes the key and returns the value" do
+ hsh = {chunky: 'bacon'}
+ @s.rb_hash_delete(hsh, :chunky).should == 'bacon'
+ hsh.should == {}
+ end
+ end
+
+ describe "rb_hash_delete_if" do
+ it "removes an entry if the block returns true" do
+ h = { a: 1, b: 2, c: 3 }
+ @s.rb_hash_delete_if(h) { |k, v| v == 2 }
+ h.should == { a: 1, c: 3 }
+ end
+
+ it "returns an Enumerator when no block is passed" do
+ @s.rb_hash_delete_if({a: 1}).should be_an_instance_of(Enumerator)
+ end
+ end
+
+ describe "rb_hash_fetch" do
+ before :each do
+ @hsh = {:a => 1, :b => 2}
+ end
+
+ it "returns the value associated with the key" do
+ @s.rb_hash_fetch(@hsh, :b).should == 2
+ end
+
+ it "raises a KeyError if the key is not found and default is set" do
+ @hsh.default = :d
+ lambda { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError)
+ end
+
+ it "raises a KeyError if the key is not found and no default is set" do
+ lambda { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError)
+ end
+ end
+
+ describe "rb_hash_foreach" do
+ it "iterates over the hash" do
+ hsh = {name: "Evan", sign: :libra}
+
+ out = @s.rb_hash_foreach(hsh)
+ out.equal?(hsh).should == false
+ out.should == hsh
+ end
+
+ it "stops via the callback" do
+ hsh = {name: "Evan", sign: :libra}
+
+ out = @s.rb_hash_foreach_stop(hsh)
+ out.size.should == 1
+ end
+
+ it "deletes via the callback" do
+ hsh = {name: "Evan", sign: :libra}
+
+ out = @s.rb_hash_foreach_delete(hsh)
+ out.should == {name: "Evan", sign: :libra}
+ hsh.should == {}
+ end
+ end
+
+ describe "rb_hash_size" do
+ it "returns the size of the hash" do
+ hsh = {fast: 'car', good: 'music'}
+ @s.rb_hash_size(hsh).should == 2
+ end
+
+ it "returns zero for an empty hash" do
+ @s.rb_hash_size({}).should == 0
+ end
+ end
+
+ describe "rb_hash_lookup" do
+ it "returns the value associated with the key" do
+ hsh = {chunky: 'bacon'}
+ @s.rb_hash_lookup(hsh, :chunky).should == 'bacon'
+ end
+
+ it "does not return the default value if it exists" do
+ hsh = Hash.new(0)
+ @s.rb_hash_lookup(hsh, :chunky).should be_nil
+ @s.rb_hash_lookup_nil(hsh, :chunky).should be_true
+ end
+
+ it "returns nil if the key does not exist" do
+ hsh = { }
+ @s.rb_hash_lookup(hsh, :chunky).should be_nil
+ @s.rb_hash_lookup_nil(hsh, :chunky).should be_true
+ end
+
+ describe "rb_hash_lookup2" do
+ it "returns the value associated with the key" do
+ hash = {chunky: 'bacon'}
+
+ @s.rb_hash_lookup2(hash, :chunky, nil).should == 'bacon'
+ end
+
+ it "returns the default value if the key does not exist" do
+ hash = {}
+
+ @s.rb_hash_lookup2(hash, :chunky, 10).should == 10
+ end
+ end
+ end
+
+ describe "rb_hash_set_ifnone" do
+ it "sets the default value of non existing keys" do
+ hash = {}
+
+ @s.rb_hash_set_ifnone(hash, 10)
+
+ hash[:chunky].should == 10
+ end
+ end
+
+ describe "rb_Hash" do
+ it "returns an empty hash when the argument is nil" do
+ @s.rb_Hash(nil).should == {}
+ end
+
+ it "returns an empty hash when the argument is []" do
+ @s.rb_Hash([]).should == {}
+ end
+
+ it "tries to convert the passed argument to a hash by calling #to_hash" do
+ h = BasicObject.new
+ def h.to_hash; {"bar" => "foo"}; end
+ @s.rb_Hash(h).should == {"bar" => "foo"}
+ end
+
+ it "raises a TypeError if the argument does not respond to #to_hash" do
+ lambda { @s.rb_Hash(42) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if #to_hash does not return a hash" do
+ h = BasicObject.new
+ def h.to_hash; 42; end
+ lambda { @s.rb_Hash(h) }.should raise_error(TypeError)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/integer_spec.rb b/spec/ruby/optional/capi/integer_spec.rb
new file mode 100644
index 0000000000..9a660cdb5c
--- /dev/null
+++ b/spec/ruby/optional/capi/integer_spec.rb
@@ -0,0 +1,275 @@
+# -*- encoding: binary -*-
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("integer")
+
+describe "CApiIntegerSpecs" do
+ before :each do
+ @s = CApiIntegerSpecs.new
+ end
+
+ describe "rb_integer_pack" do
+ it "converts zero" do
+ words = "\000" * 9
+ result = @s.rb_integer_pack(0, words, 1, 9, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 0
+ words.should == "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ end
+
+ describe "without two's complement flag" do
+ before :each do
+ @value = 0x9876_abcd_4532_ef01_0123_4567_89ab_cdef
+ @words = "\000" * 16
+ end
+
+ describe "with big endian output" do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN)
+ result.should == 1
+ @words.should == "\x98\x76\xAB\xCD\x45\x32\xEF\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN)
+ result.should == -1
+ @words.should == "\x98\x76\xAB\xCD\x45\x32\xEF\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number exactly -2**(numwords*wordsize*8)" do
+ result = @s.rb_integer_pack(-2**(2*8*8), @words, 2, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN)
+ result.should == -2
+ @words.should == "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ end
+ end
+
+ describe "with little endian output" do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN)
+ result.should == 1
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01\x01\xEF\x32\x45\xCD\xAB\x76\x98"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN)
+ result.should == -1
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01\x01\xEF\x32\x45\xCD\xAB\x76\x98"
+ end
+
+ it "converts a negative number exactly -2**(numwords*wordsize*8)" do
+ result = @s.rb_integer_pack(-2**(2*8*8), @words, 2, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN)
+ result.should == -2
+ @words.should == "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ end
+ end
+ end
+
+ describe "with two's complement flag" do
+ describe "with input less than 64 bits" do
+ before :each do
+ @value = 0x0123_4567_89ab_cdef
+ @words = "\000" * 8
+ end
+
+ describe "with big endian output" do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\xFE\xDC\xBA\x98\x76\x54\x32\x11"
+ end
+ end
+
+ describe "with little endian output" do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x11\x32\x54\x76\x98\xBA\xDC\xFE"
+ end
+ end
+
+ describe "with native endian output" do
+ big_endian do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\xFE\xDC\xBA\x98\x76\x54\x32\x11"
+ end
+ end
+
+ little_endian do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 8, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x11\x32\x54\x76\x98\xBA\xDC\xFE"
+ end
+ end
+ end
+ end
+
+ describe "with input greater than 64 bits" do
+ before :each do
+ @value = 0x9876_abcd_4532_ef01_0123_4567_89ab_cdef
+ @words = "\000" * 16
+ end
+
+ describe "with big endian output" do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\x98\x76\xAB\xCD\x45\x32\xEF\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x67\x89\x54\x32\xBA\xCD\x10\xFE\xFE\xDC\xBA\x98\x76\x54\x32\x11"
+ end
+
+ describe "with overflow" do
+ before :each do
+ @words = "\000" * 9
+ end
+
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 9, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 2
+ @words.should == "\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 9, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -2
+ @words.should == "\xFE\xFE\xDC\xBA\x98\x76\x54\x32\x11"
+ end
+
+ it "converts a negative number exactly -2**(numwords*wordsize*8)" do
+ result = @s.rb_integer_pack(-2**(9*8), @words, 1, 9, 0,
+ CApiIntegerSpecs::BIG_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ end
+ end
+ end
+
+ describe "with little endian output" do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01\x01\xEF\x32\x45\xCD\xAB\x76\x98"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 2, 8, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x11\x32\x54\x76\x98\xBA\xDC\xFE\xFE\x10\xCD\xBA\x32\x54\x89\x67"
+ end
+
+ describe "with overflow" do
+ before :each do
+ @words = "\000" * 9
+ end
+
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 9, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 2
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01\x01"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 9, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -2
+ @words.should == "\x11\x32\x54\x76\x98\xBA\xDC\xFE\xFE"
+ end
+
+ it "converts a negative number exactly -2**(numwords*wordsize*8)" do
+ result = @s.rb_integer_pack(-2**(9*8), @words, 1, 9, 0,
+ CApiIntegerSpecs::LITTLE_ENDIAN|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ end
+ end
+ end
+
+ describe "with native endian output" do
+ big_endian do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 16, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\x98\x76\xAB\xCD\x45\x32\xEF\x01\x01\x23\x45\x67\x89\xAB\xCD\xEF"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 16, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x67\x89\x54\x32\xBA\xCD\x10\xFE\xFE\xDC\xBA\x98\x76\x54\x32\x11"
+ end
+ end
+
+ little_endian do
+ it "converts a positive number" do
+ result = @s.rb_integer_pack(@value, @words, 1, 16, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == 1
+ @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x01\x01\xEF\x32\x45\xCD\xAB\x76\x98"
+ end
+
+ it "converts a negative number" do
+ result = @s.rb_integer_pack(-@value, @words, 1, 16, 0,
+ CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP)
+ result.should == -1
+ @words.should == "\x11\x32\x54\x76\x98\xBA\xDC\xFE\xFE\x10\xCD\xBA\x32\x54\x89\x67"
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/io_spec.rb b/spec/ruby/optional/capi/io_spec.rb
new file mode 100644
index 0000000000..bc75ad4307
--- /dev/null
+++ b/spec/ruby/optional/capi/io_spec.rb
@@ -0,0 +1,344 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension('io')
+
+describe "C-API IO function" do
+ before :each do
+ @o = CApiIOSpecs.new
+
+ @name = tmp("c_api_rb_io_specs")
+ touch @name
+
+ @io = new_io @name, fmode("w:utf-8")
+ @io.sync = true
+ end
+
+ after :each do
+ @io.close unless @io.closed?
+ rm_r @name
+ end
+
+ describe "rb_io_addstr" do
+ it "calls #to_s to convert the object to a String" do
+ obj = mock("rb_io_addstr string")
+ obj.should_receive(:to_s).and_return("rb_io_addstr data")
+
+ @o.rb_io_addstr(@io, obj)
+ File.read(@name).should == "rb_io_addstr data"
+ end
+
+ it "writes the String to the IO" do
+ @o.rb_io_addstr(@io, "rb_io_addstr data")
+ File.read(@name).should == "rb_io_addstr data"
+ end
+
+ it "returns the io" do
+ @o.rb_io_addstr(@io, "rb_io_addstr data").should eql(@io)
+ end
+ end
+
+ describe "rb_io_printf" do
+ it "calls #to_str to convert the format object to a String" do
+ obj = mock("rb_io_printf format")
+ obj.should_receive(:to_str).and_return("%s")
+
+ @o.rb_io_printf(@io, [obj, "rb_io_printf"])
+ File.read(@name).should == "rb_io_printf"
+ end
+
+ it "calls #to_s to convert the object to a String" do
+ obj = mock("rb_io_printf string")
+ obj.should_receive(:to_s).and_return("rb_io_printf")
+
+ @o.rb_io_printf(@io, ["%s", obj])
+ File.read(@name).should == "rb_io_printf"
+ end
+
+ it "writes the Strings to the IO" do
+ @o.rb_io_printf(@io, ["%s_%s_%s", "rb", "io", "printf"])
+ File.read(@name).should == "rb_io_printf"
+ end
+ end
+
+ describe "rb_io_print" do
+ it "calls #to_s to convert the object to a String" do
+ obj = mock("rb_io_print string")
+ obj.should_receive(:to_s).and_return("rb_io_print")
+
+ @o.rb_io_print(@io, [obj])
+ File.read(@name).should == "rb_io_print"
+ end
+
+ it "writes the Strings to the IO with no separator" do
+ @o.rb_io_print(@io, ["rb_", "io_", "print"])
+ File.read(@name).should == "rb_io_print"
+ end
+ end
+
+ describe "rb_io_puts" do
+ it "calls #to_s to convert the object to a String" do
+ obj = mock("rb_io_puts string")
+ obj.should_receive(:to_s).and_return("rb_io_puts")
+
+ @o.rb_io_puts(@io, [obj])
+ File.read(@name).should == "rb_io_puts\n"
+ end
+
+ it "writes the Strings to the IO separated by newlines" do
+ @o.rb_io_puts(@io, ["rb", "io", "write"])
+ File.read(@name).should == "rb\nio\nwrite\n"
+ end
+ end
+
+ describe "rb_io_write" do
+ it "calls #to_s to convert the object to a String" do
+ obj = mock("rb_io_write string")
+ obj.should_receive(:to_s).and_return("rb_io_write")
+
+ @o.rb_io_write(@io, obj)
+ File.read(@name).should == "rb_io_write"
+ end
+
+ it "writes the String to the IO" do
+ @o.rb_io_write(@io, "rb_io_write")
+ File.read(@name).should == "rb_io_write"
+ end
+ end
+end
+
+describe "C-API IO function" do
+ before :each do
+ @o = CApiIOSpecs.new
+
+ @name = tmp("c_api_io_specs")
+ touch @name
+
+ @io = new_io @name, fmode("r:utf-8")
+ end
+
+ after :each do
+ @io.close unless @io.closed?
+ rm_r @name
+ end
+
+ describe "rb_io_close" do
+ it "closes an IO object" do
+ @io.closed?.should be_false
+ @o.rb_io_close(@io)
+ @io.closed?.should be_true
+ end
+ end
+
+ describe "rb_io_check_io" do
+ it "returns the IO object if it is valid" do
+ @o.rb_io_check_io(@io).should == @io
+ end
+
+ it "returns nil for non IO objects" do
+ @o.rb_io_check_io({}).should be_nil
+ end
+ end
+
+ describe "rb_io_check_closed" do
+ it "does not raise an exception if the IO is not closed" do
+ # The MRI function is void, so we use should_not raise_error
+ lambda { @o.rb_io_check_closed(@io) }.should_not raise_error
+ end
+
+ it "raises an error if the IO is closed" do
+ @io.close
+ lambda { @o.rb_io_check_closed(@io) }.should raise_error(IOError)
+ end
+ end
+
+ # NOTE: unlike the name might suggest in MRI this function checks if an
+ # object is frozen, *not* if it's tainted.
+ describe "rb_io_taint_check" do
+ it "does not raise an exception if the IO is not frozen" do
+ lambda { @o.rb_io_taint_check(@io) }.should_not raise_error
+ end
+
+ it "raises an exception if the IO is frozen" do
+ @io.freeze
+
+ lambda { @o.rb_io_taint_check(@io) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "GetOpenFile" do
+ it "allows access to the system fileno" do
+ @o.GetOpenFile_fd($stdin).should == 0
+ @o.GetOpenFile_fd($stdout).should == 1
+ @o.GetOpenFile_fd($stderr).should == 2
+ @o.GetOpenFile_fd(@io).should == @io.fileno
+ end
+ end
+
+ describe "rb_io_binmode" do
+ it "returns self" do
+ @o.rb_io_binmode(@io).should == @io
+ end
+
+ it "sets binmode" do
+ @o.rb_io_binmode(@io)
+ @io.binmode?.should be_true
+ end
+ end
+end
+
+describe "C-API IO function" do
+ before :each do
+ @o = CApiIOSpecs.new
+ @r_io, @w_io = IO.pipe
+
+ @name = tmp("c_api_io_specs")
+ touch @name
+ @rw_io = new_io @name, fmode("w+")
+ end
+
+ after :each do
+ @r_io.close unless @r_io.closed?
+ @w_io.close unless @w_io.closed?
+ @rw_io.close unless @rw_io.closed?
+ rm_r @name
+ end
+
+ describe "rb_io_check_readable" do
+ it "does not raise an exception if the IO is opened for reading" do
+ # The MRI function is void, so we use should_not raise_error
+ lambda { @o.rb_io_check_readable(@r_io) }.should_not raise_error
+ end
+
+ it "does not raise an exception if the IO is opened for read and write" do
+ lambda { @o.rb_io_check_readable(@rw_io) }.should_not raise_error
+ end
+
+ it "raises an IOError if the IO is not opened for reading" do
+ lambda { @o.rb_io_check_readable(@w_io) }.should raise_error(IOError)
+ end
+
+ end
+
+ describe "rb_io_check_writable" do
+ it "does not raise an exeption if the IO is opened for writing" do
+ # The MRI function is void, so we use should_not raise_error
+ lambda { @o.rb_io_check_writable(@w_io) }.should_not raise_error
+ end
+
+ it "does not raise an exception if the IO is opened for read and write" do
+ lambda { @o.rb_io_check_writable(@rw_io) }.should_not raise_error
+ end
+
+ it "raises an IOError if the IO is not opened for reading" do
+ lambda { @o.rb_io_check_writable(@r_io) }.should raise_error(IOError)
+ end
+ end
+
+ describe "rb_io_wait_writeable" do
+ it "returns false if there is no error condition" do
+ @o.rb_io_wait_writable(@w_io).should be_false
+ end
+
+ it "raises an IOError if the IO is closed" do
+ @w_io.close
+ lambda { @o.rb_io_wait_writable(@w_io) }.should raise_error(IOError)
+ end
+ end
+
+ describe "rb_thread_fd_writable" do
+ it "waits til an fd is ready for writing" do
+ @o.rb_thread_fd_writable(@w_io).should be_nil
+ end
+ end
+
+ platform_is_not :windows do
+ describe "rb_io_wait_readable" do
+ it "returns false if there is no error condition" do
+ @o.rb_io_wait_readable(@r_io, false).should be_false
+ end
+
+ 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)
+ end
+
+ it "blocks until the io is readable and returns true" do
+ @o.instance_variable_set :@write_data, false
+ thr = Thread.new do
+ Thread.pass until @o.instance_variable_get(:@write_data)
+ @w_io.write "rb_io_wait_readable"
+ end
+
+ @o.rb_io_wait_readable(@r_io, true).should be_true
+ @o.instance_variable_get(:@read_data).should == "rb_io_wait_re"
+
+ thr.join
+ end
+ end
+ end
+
+ describe "rb_thread_wait_fd" do
+ it "waits til an fd is ready for reading" do
+ start = false
+ thr = Thread.new do
+ start = true
+ sleep 0.05
+ @w_io.write "rb_io_wait_readable"
+ end
+
+ Thread.pass until start
+
+ @o.rb_thread_wait_fd(@r_io).should be_nil
+
+ thr.join
+ end
+ end
+
+end
+
+describe "rb_fd_fix_cloexec" do
+
+ before :each do
+ @o = CApiIOSpecs.new
+
+ @name = tmp("c_api_rb_io_specs")
+ touch @name
+
+ @io = new_io @name, fmode("w:utf-8")
+ @io.close_on_exec = false
+ @io.sync = true
+ end
+
+ after :each do
+ @io.close unless @io.closed?
+ rm_r @name
+ end
+
+ it "sets close_on_exec on the IO" do
+ @o.rb_fd_fix_cloexec(@io)
+ @io.close_on_exec?.should be_true
+ end
+
+end
+
+describe "rb_cloexec_open" do
+ before :each do
+ @o = CApiIOSpecs.new
+ @name = tmp("c_api_rb_io_specs")
+ touch @name
+
+ @io = nil
+ end
+
+ after :each do
+ @io.close unless @io.nil? || @io.closed?
+ rm_r @name
+ end
+
+ it "sets close_on_exec on the newly-opened IO" do
+ @io = @o.rb_cloexec_open(@name, 0, 0)
+ @io.close_on_exec?.should be_true
+ end
+end
diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb
new file mode 100644
index 0000000000..47f368b517
--- /dev/null
+++ b/spec/ruby/optional/capi/kernel_spec.rb
@@ -0,0 +1,522 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("kernel")
+
+describe "C-API Kernel function" do
+ before :each do
+ @s = CApiKernelSpecs.new
+ end
+
+ describe "rb_block_given_p" do
+ it "returns false if no block is passed" do
+ @s.rb_block_given_p.should == false
+ end
+
+ it "returns true if a block is passed" do
+ (@s.rb_block_given_p { puts "FOO" } ).should == true
+ end
+ end
+
+ describe "rb_need_block" do
+ it "raises a LocalJumpError if no block is given" do
+ lambda { @s.rb_need_block }.should raise_error(LocalJumpError)
+ end
+
+ it "does not raise a LocalJumpError if a block is given" do
+ @s.rb_need_block { }.should == nil
+ end
+ end
+
+ describe "rb_block_call" do
+ before :each do
+ ScratchPad.record []
+ end
+
+ it "calls the block with a single argument" do
+ ary = [1, 3, 5]
+ @s.rb_block_call(ary).should == [2, 4, 6]
+ end
+
+ it "calls the block with multiple arguments in argc / argv" do
+ ary = [1, 3, 5]
+ @s.rb_block_call_multi_arg(ary).should == 9
+ end
+
+ it "calls the method with no function callback and no block" do
+ ary = [1, 3, 5]
+ @s.rb_block_call_no_func(ary).should be_kind_of(Enumerator)
+ end
+
+ it "calls the method with no function callback and a block" do
+ ary = [1, 3, 5]
+ @s.rb_block_call_no_func(ary) do |i|
+ i + 1
+ end.should == [2, 4, 6]
+ end
+ end
+
+ describe "rb_raise" do
+ it "raises an exception" do
+ lambda { @s.rb_raise({}) }.should raise_error(TypeError)
+ end
+
+ it "terminates the function at the point it was called" do
+ h = {}
+ lambda { @s.rb_raise(h) }.should raise_error(TypeError)
+ h[:stage].should == :before
+ end
+ end
+
+ describe "rb_throw" do
+ before :each do
+ ScratchPad.record []
+ end
+
+ it "sets the return value of the catch block to the specified value" do
+ catch(:foo) do
+ @s.rb_throw(:return_value)
+ end.should == :return_value
+ end
+
+ it "terminates the function at the point it was called" do
+ catch(:foo) do
+ ScratchPad << :before_throw
+ @s.rb_throw(:thrown_value)
+ ScratchPad << :after_throw
+ end.should == :thrown_value
+ ScratchPad.recorded.should == [:before_throw]
+ end
+
+ it "raises an ArgumentError if there is no catch block for the symbol" do
+ lambda { @s.rb_throw(nil) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_throw_obj" do
+ before :each do
+ ScratchPad.record []
+ @tag = Object.new
+ end
+
+ it "sets the return value of the catch block to the specified value" do
+ catch(@tag) do
+ @s.rb_throw_obj(@tag, :thrown_value)
+ end.should == :thrown_value
+ end
+
+ it "terminates the function at the point it was called" do
+ catch(@tag) do
+ ScratchPad << :before_throw
+ @s.rb_throw_obj(@tag, :thrown_value)
+ ScratchPad << :after_throw
+ end.should == :thrown_value
+ ScratchPad.recorded.should == [:before_throw]
+ end
+
+ it "raises an ArgumentError if there is no catch block for the symbol" do
+ lambda { @s.rb_throw(nil) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_warn" do
+ before :each do
+ @stderr, $stderr = $stderr, IOStub.new
+ @verbose = $VERBOSE
+ end
+
+ after :each do
+ $stderr = @stderr
+ $VERBOSE = @verbose
+ end
+
+ it "prints a message to $stderr if $VERBOSE evaluates to true" do
+ $VERBOSE = true
+ @s.rb_warn("This is a warning")
+ $stderr.should =~ /This is a warning/
+ end
+
+ it "prints a message to $stderr if $VERBOSE evaluates to false" do
+ $VERBOSE = false
+ @s.rb_warn("This is a warning")
+ $stderr.should =~ /This is a warning/
+ end
+ end
+
+ describe "rb_sys_fail" do
+ it "raises an exception from the value of errno" do
+ lambda do
+ @s.rb_sys_fail("additional info")
+ end.should raise_error(SystemCallError, /additional info/)
+ end
+
+ it "can take a NULL message" do
+ lambda do
+ @s.rb_sys_fail(nil)
+ end.should raise_error(Errno::EPERM)
+ end
+ end
+
+ describe "rb_syserr_fail" do
+ it "raises an exception from the given error" do
+ lambda do
+ @s.rb_syserr_fail(Errno::EINVAL::Errno, "additional info")
+ end.should raise_error(Errno::EINVAL, /additional info/)
+ end
+
+ it "can take a NULL message" do
+ lambda do
+ @s.rb_syserr_fail(Errno::EINVAL::Errno, nil)
+ end.should raise_error(Errno::EINVAL)
+ end
+ end
+
+ describe "rb_yield" do
+ it "yields passed argument" do
+ ret = nil
+ @s.rb_yield(1) { |z| ret = z }
+ ret.should == 1
+ end
+
+ it "returns the result from block evaluation" do
+ @s.rb_yield(1) { |z| z * 1000 }.should == 1000
+ end
+
+ 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
+
+ platform_is_not :"solaris2.10" do # NOTE: i386-pc-solaris2.10
+ 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
+ end
+
+ describe "rb_yield_values" do
+ it "yields passed arguments" do
+ ret = nil
+ @s.rb_yield_values(1, 2) { |x, y| ret = x + y }
+ ret.should == 3
+ end
+
+ it "returns the result from block evaluation" do
+ @s.rb_yield_values(1, 2) { |x, y| x + y }.should == 3
+ end
+
+ it "raises LocalJumpError when no block is given" do
+ lambda { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError)
+ end
+ end
+
+ describe "rb_yield_splat" do
+ it "yields with passed array's contents" do
+ ret = nil
+ @s.rb_yield_splat([1, 2]) { |x, y| ret = x + y }
+ ret.should == 3
+ end
+
+ it "returns the result from block evaluation" do
+ @s.rb_yield_splat([1, 2]) { |x, y| x + y }.should == 3
+ end
+
+ it "raises LocalJumpError when no block is given" do
+ lambda { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError)
+ 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 }
+ @raise_proc_returns_sentinel = lambda {|*_| :raise_proc_executed }
+ @raise_proc_returns_arg = lambda {|*a| a }
+ @arg_error_proc = lambda { |*_| raise ArgumentError, '' }
+ @std_error_proc = lambda { |*_| raise StandardError, '' }
+ @exc_error_proc = lambda { |*_| raise Exception, '' }
+ end
+
+ it "executes passed function" do
+ @s.rb_rescue(@proc, :no_exc, @raise_proc_returns_arg, :exc).should == :no_exc
+ end
+
+ it "executes passed 'raise function' if a StandardError exception is raised" do
+ @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_sentinel, :exc).should == :raise_proc_executed
+ @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_sentinel, :exc).should == :raise_proc_executed
+ end
+
+ it "passes the user supplied argument to the 'raise function' if a StandardError exception is raised" do
+ arg1, _ = @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_arg, :exc1)
+ arg1.should == :exc1
+
+ arg2, _ = @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_arg, :exc2)
+ arg2.should == :exc2
+ end
+
+ it "passes the raised exception to the 'raise function' if a StandardError exception is raised" do
+ _, exc1 = @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_arg, :exc)
+ exc1.class.should == ArgumentError
+
+ _, exc2 = @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_arg, :exc)
+ exc2.class.should == StandardError
+ end
+
+ it "raises an exception if passed function raises an exception other than StandardError" do
+ lambda { @s.rb_rescue(@exc_error_proc, nil, @raise_proc_returns_arg, nil) }.should raise_error(Exception)
+ end
+
+ it "raises an exception if any exception is raised inside 'raise function'" do
+ lambda { @s.rb_rescue(@std_error_proc, nil, @std_error_proc, nil) }.should raise_error(StandardError)
+ end
+
+ it "makes $! available only during 'raise function' execution" do
+ @s.rb_rescue(@std_error_proc, nil, lambda { |*_| $! }, nil).class.should == StandardError
+ $!.should == nil
+ end
+
+ it "returns the break value if the passed function yields to a block with a break" do
+ def proc_caller
+ @s.rb_rescue(lambda { |*_| yield }, nil, @proc, nil)
+ end
+
+ proc_caller { break :value }.should == :value
+ end
+ end
+
+ describe "rb_rescue2" do
+ it "only rescues if one of the passed exceptions is raised" do
+ proc = lambda { |x| x }
+ arg_error_proc = lambda { |*_| raise ArgumentError, '' }
+ run_error_proc = lambda { |*_| raise RuntimeError, '' }
+ type_error_proc = lambda { |*_| raise TypeError, '' }
+ @s.rb_rescue2(arg_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError).should == :exc
+ @s.rb_rescue2(run_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError).should == :exc
+ lambda {
+ @s.rb_rescue2(type_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError)
+ }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_catch" do
+ before :each do
+ ScratchPad.record []
+ end
+
+ it "executes passed function" do
+ @s.rb_catch("foo", lambda { 1 }).should == 1
+ end
+
+ it "terminates the function at the point it was called" do
+ proc = lambda do
+ ScratchPad << :before_throw
+ throw :thrown_value
+ ScratchPad << :after_throw
+ end
+ @s.rb_catch("thrown_value", proc).should be_nil
+ ScratchPad.recorded.should == [:before_throw]
+ end
+
+ it "raises an ArgumentError if the throw symbol isn't caught" do
+ lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_catch_obj" do
+
+ before :each do
+ ScratchPad.record []
+ @tag = Object.new
+ end
+
+ it "executes passed function" do
+ @s.rb_catch_obj(@tag, lambda { 1 }).should == 1
+ end
+
+ it "terminates the function at the point it was called" do
+ proc = lambda do
+ ScratchPad << :before_throw
+ throw @tag
+ ScratchPad << :after_throw
+ end
+ @s.rb_catch_obj(@tag, proc).should be_nil
+ ScratchPad.recorded.should == [:before_throw]
+ end
+
+ it "raises an ArgumentError if the throw symbol isn't caught" do
+ lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_ensure" do
+ it "executes passed function and returns its value" do
+ proc = lambda { |x| x }
+ @s.rb_ensure(proc, :proc, proc, :ensure_proc).should == :proc
+ end
+
+ it "executes passed 'ensure function' when no exception is raised" do
+ foo = nil
+ proc = lambda { |*_| }
+ ensure_proc = lambda { |x| foo = x }
+ @s.rb_ensure(proc, nil, ensure_proc, :foo)
+ foo.should == :foo
+ end
+
+ it "executes passed 'ensure function' when an exception is raised" do
+ foo = nil
+ raise_proc = lambda { raise '' }
+ ensure_proc = lambda { |x| foo = x }
+ @s.rb_ensure(raise_proc, nil, ensure_proc, :foo) rescue nil
+ foo.should == :foo
+ end
+
+ it "raises the same exception raised inside passed function" do
+ raise_proc = lambda { |*_| raise RuntimeError, 'foo' }
+ proc = lambda { |*_| }
+ lambda { @s.rb_ensure(raise_proc, nil, proc, nil) }.should raise_error(RuntimeError, 'foo')
+ end
+ end
+
+ describe "rb_eval_string" do
+ it "evaluates a string of ruby code" do
+ @s.rb_eval_string("1+1").should == 2
+ end
+ end
+
+ describe "rb_block_proc" do
+ it "converts the implicit block into a proc" do
+ proc = @s.rb_block_proc() { 1+1 }
+ proc.should be_kind_of(Proc)
+ proc.call.should == 2
+ end
+ end
+
+ describe "rb_exec_recursive" do
+ it "detects recursive invocations of a method and indicates as such" do
+ s = "hello"
+ @s.rb_exec_recursive(s).should == s
+ end
+ end
+
+ platform_is_not :windows do
+ describe "rb_set_end_proc" do
+ before :each do
+ @r, @w = IO.pipe
+ end
+
+ after :each do
+ @r.close
+ @w.close
+ Process.wait @pid
+ end
+
+ it "runs a C function on shutdown" do
+ @pid = fork {
+ @s.rb_set_end_proc(@w)
+ }
+
+ @r.read(1).should == "e"
+ end
+ end
+ end
+
+ describe "rb_f_sprintf" do
+ it "returns a string according to format and arguments" do
+ @s.rb_f_sprintf(["%d %f %s", 10, 2.5, "test"]).should == "10 2.500000 test"
+ end
+ end
+
+ describe "rb_make_backtrace" do
+ it "returns a caller backtrace" do
+ backtrace = @s.rb_make_backtrace
+ lines = backtrace.select {|l| l =~ /#{__FILE__}/ }
+ lines.should_not be_empty
+ end
+ end
+
+ describe "rb_obj_method" do
+ it "returns the method object for a symbol" do
+ method = @s.rb_obj_method("test", :size)
+ method.owner.should == String
+ method.name.to_sym.should == :size
+ end
+
+ it "returns the method object for a string" do
+ method = @s.rb_obj_method("test", "size")
+ method.owner.should == String
+ method.name.to_sym.should == :size
+ end
+ end
+
+ describe "rb_funcall3" do
+ before :each do
+ @obj = Object.new
+ class << @obj
+ def method_public; :method_public end
+ def method_private; :method_private end
+ private :method_private
+ end
+ end
+
+ it "calls a public method" do
+ @s.rb_funcall3(@obj, :method_public).should == :method_public
+ end
+ it "does not call a private method" do
+ lambda { @s.rb_funcall3(@obj, :method_private) }.should raise_error(NoMethodError, /private/)
+ end
+ end
+
+ describe 'rb_funcall_with_block' do
+ before :each do
+ @obj = Object.new
+ class << @obj
+ def method_public; yield end
+ def method_private; yield end
+ private :method_private
+ end
+ end
+
+ it "calls a method with block" do
+ @s.rb_funcall_with_block(@obj, :method_public, proc { :result }).should == :result
+ end
+
+ it "does not call a private method" do
+ lambda { @s.rb_funcall_with_block(@obj, :method_private, proc { :result }) }.should raise_error(NoMethodError, /private/)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/marshal_spec.rb b/spec/ruby/optional/capi/marshal_spec.rb
new file mode 100644
index 0000000000..ee06f96b72
--- /dev/null
+++ b/spec/ruby/optional/capi/marshal_spec.rb
@@ -0,0 +1,46 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("marshal")
+
+describe "CApiMarshalSpecs" do
+ before :each do
+ @s = CApiMarshalSpecs.new
+ end
+
+ describe "rb_marshal_dump" do
+ before :each do
+ @obj = "foo"
+ end
+
+ it "marshals an object" do
+ expected = Marshal.dump(@obj)
+
+ @s.rb_marshal_dump(@obj, nil).should == expected
+ end
+
+ it "marshals an object and write to an IO when passed" do
+ expected_io = IOStub.new
+ test_io = IOStub.new
+
+ Marshal.dump(@obj, expected_io)
+
+ @s.rb_marshal_dump(@obj, test_io)
+
+ test_io.should == expected_io
+ end
+
+ end
+
+ describe "rb_marshal_load" do
+ before :each do
+ @obj = "foo"
+ @data = Marshal.dump(@obj)
+ end
+
+ it "unmarshals an object" do
+ @s.rb_marshal_load(@data).should == @obj
+ end
+
+ end
+
+end
diff --git a/spec/ruby/optional/capi/module_spec.rb b/spec/ruby/optional/capi/module_spec.rb
new file mode 100644
index 0000000000..23fb674f0e
--- /dev/null
+++ b/spec/ruby/optional/capi/module_spec.rb
@@ -0,0 +1,342 @@
+require File.expand_path('../spec_helper', __FILE__)
+require File.expand_path('../fixtures/module', __FILE__)
+
+load_extension('module')
+
+describe "CApiModule" do
+
+ before :each do
+ @m = CApiModuleSpecs.new
+ end
+
+ describe "rb_define_global_const" do
+ it "defines a constant on Object" do
+ @m.rb_define_global_const("CApiModuleSpecsGlobalConst", 7)
+ ::CApiModuleSpecsGlobalConst.should == 7
+ Object.send :remove_const, :CApiModuleSpecsGlobalConst
+ end
+ end
+
+ describe "rb_const_set given a symbol name and a value" do
+ it "sets a new constant on a module" do
+ @m.rb_const_set(CApiModuleSpecs::C, :W, 7)
+ CApiModuleSpecs::C::W.should == 7
+ end
+
+ it "sets an existing constant's value" do
+ -> {
+ @m.rb_const_set(CApiModuleSpecs::C, :Z, 8)
+ }.should complain(/already initialized constant/)
+ CApiModuleSpecs::C::Z.should == 8
+ end
+ end
+
+ describe "rb_define_module" do
+ it "returns the module if it is already defined" do
+ mod = @m.rb_define_module("CApiModuleSpecsModuleA")
+ mod.const_get(:X).should == 1
+ end
+
+ it "raises a TypeError if the constant is not a module" do
+ ::CApiModuleSpecsGlobalConst = 7
+ lambda { @m.rb_define_module("CApiModuleSpecsGlobalConst") }.should raise_error(TypeError)
+ Object.send :remove_const, :CApiModuleSpecsGlobalConst
+ end
+
+ it "defines a new module at toplevel" do
+ mod = @m.rb_define_module("CApiModuleSpecsModuleB")
+ mod.should be_kind_of(Module)
+ mod.name.should == "CApiModuleSpecsModuleB"
+ ::CApiModuleSpecsModuleB.should be_kind_of(Module)
+ Object.send :remove_const, :CApiModuleSpecsModuleB
+ end
+ end
+
+ describe "rb_define_module_under" do
+ it "creates a new module inside the inner class" do
+ mod = @m.rb_define_module_under(CApiModuleSpecs, "ModuleSpecsModuleUnder1")
+ mod.should be_kind_of(Module)
+ end
+
+ it "sets the module name" do
+ mod = @m.rb_define_module_under(CApiModuleSpecs, "ModuleSpecsModuleUnder2")
+ mod.name.should == "CApiModuleSpecs::ModuleSpecsModuleUnder2"
+ end
+ end
+
+ describe "rb_define_module_under" do
+ before :each do
+ compile_extension("module_under_autoload")
+ end
+
+ it "defines a module for an existing Autoload with an extension" do
+ CApiModuleSpecs::ModuleUnderAutoload.name.should == "CApiModuleSpecs::ModuleUnderAutoload"
+ end
+
+ it "defines a module for an existing Autoload with a ruby object" do
+ CApiModuleSpecs::RubyUnderAutoload.name.should == "CApiModuleSpecs::RubyUnderAutoload"
+ end
+ end
+
+ describe "rb_define_const given a String name and a value" do
+ it "defines a new constant on a module" do
+ @m.rb_define_const(CApiModuleSpecs::C, "V", 7)
+ CApiModuleSpecs::C::V.should == 7
+ end
+
+ it "sets an existing constant's value" do
+ -> {
+ @m.rb_define_const(CApiModuleSpecs::C, "Z", 9)
+ }.should complain(/already initialized constant/)
+ CApiModuleSpecs::C::Z.should == 9
+ end
+ end
+
+ describe "rb_const_defined" do
+ # The fixture converts C boolean test to Ruby 'true' / 'false'
+ it "returns C non-zero if a constant is defined" do
+ @m.rb_const_defined(CApiModuleSpecs::A, :X).should be_true
+ end
+
+ it "returns C non-zero if a constant is defined in Object" do
+ @m.rb_const_defined(CApiModuleSpecs::A, :Module).should be_true
+ end
+ end
+
+ describe "rb_const_defined_at" do
+ # The fixture converts C boolean test to Ruby 'true' / 'false'
+ it "returns C non-zero if a constant is defined" do
+ @m.rb_const_defined_at(CApiModuleSpecs::A, :X).should be_true
+ end
+
+ it "does not search in ancestors for the constant" do
+ @m.rb_const_defined_at(CApiModuleSpecs::B, :X).should be_false
+ end
+
+ it "does not search in Object" do
+ @m.rb_const_defined_at(CApiModuleSpecs::A, :Module).should be_false
+ end
+ end
+
+ describe "rb_const_get" do
+ it "returns a constant defined in the module" do
+ @m.rb_const_get(CApiModuleSpecs::A, :X).should == 1
+ end
+
+ it "returns a constant defined at toplevel" do
+ @m.rb_const_get(CApiModuleSpecs::A, :Fixnum).should == Fixnum
+ end
+
+ it "returns a constant defined in a superclass" do
+ @m.rb_const_get(CApiModuleSpecs::B, :X).should == 1
+ end
+
+ it "calls #const_missing if the constant is not defined in the class or ancestors" do
+ CApiModuleSpecs::A.should_receive(:const_missing).with(:CApiModuleSpecsUndefined)
+ @m.rb_const_get(CApiModuleSpecs::A, :CApiModuleSpecsUndefined)
+ end
+
+ it "resolves autoload constants in classes" do
+ @m.rb_const_get(CApiModuleSpecs::A, :D).should == 123
+ end
+
+ it "resolves autoload constants in Object" do
+ @m.rb_const_get(Object, :CApiModuleSpecsAutoload).should == 123
+ end
+ end
+
+ describe "rb_const_get_from" do
+ it "returns a constant defined in the module" do
+ @m.rb_const_get_from(CApiModuleSpecs::B, :Y).should == 2
+ end
+
+ it "returns a constant defined in a superclass" do
+ @m.rb_const_get_from(CApiModuleSpecs::B, :X).should == 1
+ end
+
+ it "calls #const_missing if the constant is not defined in the class or ancestors" do
+ CApiModuleSpecs::M.should_receive(:const_missing).with(:Fixnum)
+ @m.rb_const_get_from(CApiModuleSpecs::M, :Fixnum)
+ end
+
+ it "resolves autoload constants" do
+ @m.rb_const_get_from(CApiModuleSpecs::A, :C).should == 123
+ end
+ end
+
+ describe "rb_const_get_at" do
+ it "returns a constant defined in the module" do
+ @m.rb_const_get_at(CApiModuleSpecs::B, :Y).should == 2
+ end
+
+ it "resolves autoload constants" do
+ @m.rb_const_get_at(CApiModuleSpecs::A, :B).should == 123
+ end
+
+ it "calls #const_missing if the constant is not defined in the module" do
+ CApiModuleSpecs::B.should_receive(:const_missing).with(:X)
+ @m.rb_const_get_at(CApiModuleSpecs::B, :X)
+ end
+ end
+
+ describe "rb_define_alias" do
+ it "defines an alias for an existing method" do
+ cls = Class.new do
+ def method_to_be_aliased
+ :method_to_be_aliased
+ end
+ end
+
+ @m.rb_define_alias cls, "method_alias", "method_to_be_aliased"
+ cls.new.method_alias.should == :method_to_be_aliased
+ end
+ end
+
+ describe "rb_alias" do
+ it "defines an alias for an existing method" do
+ cls = Class.new do
+ def method_to_be_aliased
+ :method_to_be_aliased
+ end
+ end
+
+ @m.rb_alias cls, :method_alias, :method_to_be_aliased
+ cls.new.method_alias.should == :method_to_be_aliased
+ end
+ end
+
+ describe "rb_define_global_function" do
+ it "defines a method on Kernel" do
+ @m.rb_define_global_function("module_specs_global_function")
+ Kernel.should have_method(:module_specs_global_function)
+ module_specs_global_function.should == :test_method
+ end
+ end
+
+ describe "rb_define_method" do
+ it "defines a method on a class" do
+ cls = Class.new
+ @m.rb_define_method(cls, "test_method")
+ cls.should have_instance_method(:test_method)
+ cls.new.test_method.should == :test_method
+ end
+
+ it "defines a method on a module" do
+ mod = Module.new
+ @m.rb_define_method(mod, "test_method")
+ mod.should have_instance_method(:test_method)
+ end
+ end
+
+ describe "rb_define_module_function" do
+ before :each do
+ @mod = Module.new
+ @m.rb_define_module_function @mod, "test_module_function"
+ end
+
+ it "defines a module function" do
+ @mod.test_module_function.should == :test_method
+ end
+
+ it "defines a private instance method" do
+ cls = Class.new
+ cls.include(@mod)
+
+ cls.should have_private_instance_method(:test_module_function)
+ end
+ end
+
+ describe "rb_define_private_method" do
+ it "defines a private method on a class" do
+ cls = Class.new
+ @m.rb_define_private_method(cls, "test_method")
+ cls.should have_private_instance_method(:test_method)
+ cls.new.send(:test_method).should == :test_method
+ end
+
+ it "defines a private method on a module" do
+ mod = Module.new
+ @m.rb_define_private_method(mod, "test_method")
+ mod.should have_private_instance_method(:test_method)
+ end
+ end
+
+ describe "rb_define_protected_method" do
+ it "defines a protected method on a class" do
+ cls = Class.new
+ @m.rb_define_protected_method(cls, "test_method")
+ cls.should have_protected_instance_method(:test_method)
+ cls.new.send(:test_method).should == :test_method
+ end
+
+ it "defines a protected method on a module" do
+ mod = Module.new
+ @m.rb_define_protected_method(mod, "test_method")
+ mod.should have_protected_instance_method(:test_method)
+ end
+ end
+
+ describe "rb_define_singleton_method" do
+ it "defines a method on the singleton class" do
+ cls = Class.new
+ a = cls.new
+ @m.rb_define_singleton_method a, "module_specs_singleton_method"
+ a.module_specs_singleton_method.should == :test_method
+ lambda { cls.new.module_specs_singleton_method }.should raise_error(NoMethodError)
+ end
+ end
+
+ describe "rb_undef_method" do
+ before :each do
+ @class = Class.new do
+ def ruby_test_method
+ :ruby_test_method
+ end
+ end
+ end
+
+ it "undef'ines a method on a class" do
+ @class.new.ruby_test_method.should == :ruby_test_method
+ @m.rb_undef_method @class, "ruby_test_method"
+ @class.should_not have_instance_method(:ruby_test_method)
+ end
+
+ it "does not raise exceptions when passed a missing name" do
+ lambda { @m.rb_undef_method @class, "not_exist" }.should_not raise_error
+ end
+
+ describe "when given a frozen Class" do
+ before :each do
+ @frozen = @class.dup.freeze
+ end
+
+ it "raises a RuntimeError when passed a name" do
+ lambda { @m.rb_undef_method @frozen, "ruby_test_method" }.should raise_error(RuntimeError)
+ end
+
+ it "raises a RuntimeError when passed a missing name" do
+ lambda { @m.rb_undef_method @frozen, "not_exist" }.should raise_error(RuntimeError)
+ end
+ end
+ end
+
+ describe "rb_undef" do
+ it "undef'ines a method on a class" do
+ cls = Class.new do
+ def ruby_test_method
+ :ruby_test_method
+ end
+ end
+
+ cls.new.ruby_test_method.should == :ruby_test_method
+ @m.rb_undef cls, :ruby_test_method
+ cls.should_not have_instance_method(:ruby_test_method)
+ end
+ end
+
+ describe "rb_class2name" do
+ it "returns the module name" do
+ @m.rb_class2name(CApiModuleSpecs::M).should == "CApiModuleSpecs::M"
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/mutex_spec.rb b/spec/ruby/optional/capi/mutex_spec.rb
new file mode 100644
index 0000000000..c435d09eff
--- /dev/null
+++ b/spec/ruby/optional/capi/mutex_spec.rb
@@ -0,0 +1,88 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("mutex")
+
+describe "C-API Mutex functions" do
+ before :each do
+ @s = CApiMutexSpecs.new
+ @m = Mutex.new
+ end
+
+ describe "rb_mutex_new" do
+ it "creates a new mutex" do
+ @s.rb_mutex_new.should be_an_instance_of(Mutex)
+ end
+ end
+
+ describe "rb_mutex_locked_p" do
+ it "returns false if the mutex is not locked" do
+ @s.rb_mutex_locked_p(@m).should be_false
+ end
+
+ it "returns true if the mutex is locked" do
+ @m.lock
+ @s.rb_mutex_locked_p(@m).should be_true
+ end
+ end
+
+ describe "rb_mutex_trylock" do
+ it "locks the mutex if not locked" do
+ @s.rb_mutex_trylock(@m).should be_true
+ @m.locked?.should be_true
+ end
+
+ it "returns false if the mutex is already locked" do
+ @m.lock
+ @s.rb_mutex_trylock(@m).should be_false
+ @m.locked?.should be_true
+ end
+ end
+
+ describe "rb_mutex_lock" do
+ it "returns when the mutex isn't locked" do
+ @s.rb_mutex_lock(@m).should == @m
+ @m.locked?.should be_true
+ end
+
+ it "throws an exception when already locked in the same thread" do
+ @m.lock
+ lambda { @s.rb_mutex_lock(@m) }.should raise_error(ThreadError)
+ @m.locked?.should be_true
+ end
+ end
+
+ describe "rb_mutex_unlock" do
+ it "raises an exception when not locked" do
+ lambda { @s.rb_mutex_unlock(@m) }.should raise_error(ThreadError)
+ @m.locked?.should be_false
+ end
+
+ it "unlocks the mutex when locked" do
+ @m.lock
+ @s.rb_mutex_unlock(@m).should == @m
+ @m.locked?.should be_false
+ end
+ end
+
+ describe "rb_mutex_sleep" do
+ it "throws an exception when the mutex is not locked" do
+ lambda { @s.rb_mutex_sleep(@m, 0.1) }.should raise_error(ThreadError)
+ @m.locked?.should be_false
+ end
+
+ 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)
+ @m.locked?.should be_true
+ end
+ end
+
+ describe "rb_mutex_synchronize" do
+ it "calls the function while the mutex is locked" do
+ callback = lambda { @m.locked?.should be_true }
+ @s.rb_mutex_synchronize(@m, callback)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/numeric_spec.rb b/spec/ruby/optional/capi/numeric_spec.rb
new file mode 100644
index 0000000000..fdf794247d
--- /dev/null
+++ b/spec/ruby/optional/capi/numeric_spec.rb
@@ -0,0 +1,432 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("numeric")
+
+describe "CApiNumericSpecs" do
+ before :each do
+ @s = CApiNumericSpecs.new
+ end
+
+ platform_is wordsize: 64 do
+ describe "rb_num2int" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_num2int(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Float" do
+ @s.rb_num2int(4.2).should == 4
+ end
+
+ it "converts a Bignum" do
+ @s.rb_num2int(0x7fff_ffff).should == 0x7fff_ffff
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_num2int(5).should == 5
+ end
+
+ it "converts -1 to an signed number" do
+ @s.rb_num2int(-1).should == -1
+ end
+
+ it "converts a negative Bignum into an signed number" do
+ @s.rb_num2int(-2147442171).should == -2147442171
+ end
+
+ it "raises a RangeError if the value is more than 32bits" do
+ lambda { @s.rb_num2int(0xffff_ffff+1) }.should raise_error(RangeError)
+ end
+
+ it "calls #to_int to coerce the value" do
+ obj = mock("number")
+ obj.should_receive(:to_int).and_return(2)
+ @s.rb_num2long(obj).should == 2
+ end
+ end
+ end
+
+ platform_is wordsize: 64 do
+ describe "rb_num2uint" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_num2uint(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Float" do
+ @s.rb_num2uint(4.2).should == 4
+ end
+
+ it "converts a Bignum" do
+ @s.rb_num2uint(0xffff_ffff).should == 0xffff_ffff
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_num2uint(5).should == 5
+ end
+
+ it "converts a negative number to the complement" do
+ @s.rb_num2uint(-1).should == 18446744073709551615
+ end
+
+ it "converts a signed int value to the complement" do
+ @s.rb_num2uint(-0x8000_0000).should == 18446744071562067968
+ end
+
+ it "raises a RangeError if the value is more than 32bits" do
+ lambda { @s.rb_num2uint(0xffff_ffff+1) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError if the value is less than 32bits negative" do
+ lambda { @s.rb_num2uint(-0x8000_0000-1) }.should raise_error(RangeError)
+ end
+
+ it "raises a RangeError if the value is more than 64bits" do
+ lambda do
+ @s.rb_num2uint(0xffff_ffff_ffff_ffff+1)
+ end.should raise_error(RangeError)
+ end
+
+ it "calls #to_int to coerce the value" do
+ obj = mock("number")
+ obj.should_receive(:to_int).and_return(2)
+ @s.rb_num2uint(obj).should == 2
+ end
+ end
+ end
+
+ describe "rb_num2long" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_num2long(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Float" do
+ @s.rb_num2long(4.2).should == 4
+ end
+
+ it "converts a Bignum" do
+ @s.rb_num2long(0x7fff_ffff).should == 0x7fff_ffff
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_num2long(5).should == 5
+ end
+
+ platform_is wordsize: 32 do
+ it "converts -1 to an signed number" do
+ @s.rb_num2long(-1).should == -1
+ end
+
+ it "converts a negative Bignum into an signed number" do
+ @s.rb_num2long(-2147442171).should == -2147442171
+ end
+
+ it "raises a RangeError if the value is more than 32bits" do
+ lambda { @s.rb_num2long(0xffff_ffff+1) }.should raise_error(RangeError)
+ end
+ end
+
+ platform_is wordsize: 64 do
+ it "converts -1 to an signed number" do
+ @s.rb_num2long(-1).should == -1
+ end
+
+ it "converts a negative Bignum into an signed number" do
+ @s.rb_num2long(-9223372036854734331).should == -9223372036854734331
+ end
+
+ it "raises a RangeError if the value is more than 64bits" do
+ lambda do
+ @s.rb_num2long(0xffff_ffff_ffff_ffff+1)
+ end.should raise_error(RangeError)
+ end
+ end
+
+ it "calls #to_int to coerce the value" do
+ obj = mock("number")
+ obj.should_receive(:to_int).and_return(2)
+ @s.rb_num2long(obj).should == 2
+ end
+ end
+
+ describe "rb_int2num" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_int2num(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Float" do
+ @s.rb_int2num(4.2).should == 4
+ end
+
+ it "raises a RangeError when passed a Bignum" do
+ lambda { @s.rb_int2num(bignum_value) }.should raise_error(RangeError)
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_int2num(5).should == 5
+ end
+
+ it "converts a negative Fixnum" do
+ @s.rb_int2num(-11).should == -11
+ end
+ end
+
+ describe "rb_num2ulong" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_num2ulong(nil) }.should raise_error(TypeError)
+ end
+
+ it "converts a Float" do
+ @s.rb_num2ulong(4.2).should == 4
+ end
+
+ it "converts a Bignum" do
+ @s.rb_num2ulong(0xffff_ffff).should == 0xffff_ffff
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_num2ulong(5).should == 5
+ end
+
+ platform_is wordsize: 32 do
+ it "converts -1 to an unsigned number" do
+ @s.rb_num2ulong(-1).should == 4294967295
+ end
+
+ it "converts a negative Bignum into an unsigned number" do
+ @s.rb_num2ulong(-2147442171).should == 2147525125
+ end
+
+ it "raises a RangeError if the value is more than 32bits" do
+ lambda { @s.rb_num2ulong(0xffff_ffff+1) }.should raise_error(RangeError)
+ end
+ end
+
+ platform_is wordsize: 64 do
+ it "converts -1 to an unsigned number" do
+ @s.rb_num2ulong(-1).should == 18446744073709551615
+ end
+
+ it "converts a negative Bignum into an unsigned number" do
+ @s.rb_num2ulong(-9223372036854734331).should == 9223372036854817285
+ end
+
+ it "raises a RangeError if the value is more than 64bits" do
+ lambda do
+ @s.rb_num2ulong(0xffff_ffff_ffff_ffff+1)
+ end.should raise_error(RangeError)
+ end
+ end
+
+ it "calls #to_int to coerce the value" do
+ obj = mock("number")
+ obj.should_receive(:to_int).and_return(2)
+ @s.rb_num2ulong(obj).should == 2
+ end
+ end
+
+ describe "rb_Integer" do
+ it "creates a new Integer from a String" do
+ i = @s.rb_Integer("8675309")
+ i.should be_kind_of(Integer)
+ i.should eql(8675309)
+ end
+ end
+
+ describe "rb_ll2inum" do
+ it "creates a new Fixnum from a small signed long long" do
+ i = @s.rb_ll2inum_14()
+ i.should be_kind_of(Fixnum)
+ i.should eql(14)
+ end
+ end
+
+ describe "rb_int2inum" do
+ it "creates a new Fixnum from a long" do
+ i = @s.rb_int2inum_14()
+ i.should be_kind_of(Fixnum)
+ i.should eql(14)
+ end
+ end
+
+ describe "rb_num2dbl" do
+ it "raises a TypeError if passed nil" do
+ lambda { @s.rb_num2dbl(nil) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if passed a String" do
+ lambda { @s.rb_num2dbl("1.2") }.should raise_error(TypeError)
+ end
+
+ it "converts a Float" do
+ @s.rb_num2dbl(4.2).should == 4.2
+ end
+
+ it "converts a Bignum" do
+ @s.rb_num2dbl(2**70).should == (2**70).to_f
+ end
+
+ it "converts a Fixnum" do
+ @s.rb_num2dbl(5).should == 5.0
+ end
+
+ it "calls #to_f to coerce the value" do
+ obj = mock("number")
+ obj.should_receive(:to_f).and_return(2.0)
+ @s.rb_num2dbl(obj).should == 2.0
+ end
+ end
+
+ describe "NUM2CHR" do
+ it "returns the first character of a String" do
+ @s.NUM2CHR("Abc").should == 65
+ end
+
+ it "returns the least significant byte of an Integer" do
+ @s.NUM2CHR(0xa7c).should == 0x07c
+ end
+
+ it "returns the least significant byte of a Float converted to an Integer" do
+ @s.NUM2CHR(0xa7c.to_f).should == 0x07c
+ end
+
+ it "raises a TypeError when passed an empty String" do
+ lambda { @s.NUM2CHR("") }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_num_zerodiv" do
+ it "raises a RuntimeError" do
+ lambda { @s.rb_num_zerodiv() }.should raise_error(ZeroDivisionError, 'divided by 0')
+ end
+ end
+
+ describe "rb_cmpint" do
+ it "returns a Fixnum if passed one" do
+ @s.rb_cmpint(1, 2).should == 1
+ end
+
+ it "uses > to check if the value is greater than 1" do
+ m = mock("number")
+ m.should_receive(:>).and_return(true)
+ @s.rb_cmpint(m, 4).should == 1
+ end
+
+ it "uses < to check if the value is less than 1" do
+ m = mock("number")
+ m.should_receive(:>).and_return(false)
+ m.should_receive(:<).and_return(true)
+ @s.rb_cmpint(m, 4).should == -1
+ end
+
+ it "returns 0 if < and > are false" do
+ m = mock("number")
+ m.should_receive(:>).and_return(false)
+ m.should_receive(:<).and_return(false)
+ @s.rb_cmpint(m, 4).should == 0
+ end
+
+ it "raises an ArgumentError when passed nil" do
+ lambda {
+ @s.rb_cmpint(nil, 4)
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_num_coerce_bin" do
+ it "calls #coerce on the first argument" do
+ obj = mock("rb_num_coerce_bin")
+ obj.should_receive(:coerce).with(2).and_return([1, 2])
+
+ @s.rb_num_coerce_bin(2, obj, :+).should == 3
+ end
+
+ it "calls the specified method on the first argument returned by #coerce" do
+ obj = mock("rb_num_coerce_bin")
+ obj.should_receive(:coerce).with(2).and_return([obj, 2])
+ obj.should_receive(:+).with(2).and_return(3)
+
+ @s.rb_num_coerce_bin(2, obj, :+).should == 3
+ end
+
+ it "raises a TypeError if #coerce does not return an Array" do
+ obj = mock("rb_num_coerce_bin")
+ obj.should_receive(:coerce).with(2).and_return(nil)
+
+ lambda { @s.rb_num_coerce_bin(2, obj, :+) }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_num_coerce_cmp" do
+ it "calls #coerce on the first argument" do
+ obj = mock("rb_num_coerce_cmp")
+ obj.should_receive(:coerce).with(2).and_return([1, 2])
+
+ @s.rb_num_coerce_cmp(2, obj, :<=>).should == -1
+ end
+
+ it "calls the specified method on the first argument returned by #coerce" do
+ obj = mock("rb_num_coerce_cmp")
+ obj.should_receive(:coerce).with(2).and_return([obj, 2])
+ obj.should_receive(:<=>).with(2).and_return(-1)
+
+ @s.rb_num_coerce_cmp(2, obj, :<=>).should == -1
+ end
+
+ ruby_version_is ""..."2.5" do
+ it "returns nil if passed nil" do
+ -> {
+ @result = @s.rb_num_coerce_cmp(nil, 2, :<=>)
+ }.should complain(/comparison operators will no more rescue exceptions/)
+ @result.should be_nil
+ end
+ end
+
+ ruby_version_is "2.5" do
+ it "lets the exception go through if #coerce raises an exception" do
+ obj = mock("rb_num_coerce_cmp")
+ obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error"))
+ -> {
+ @s.rb_num_coerce_cmp(2, obj, :<=>)
+ }.should raise_error(RuntimeError, "my error")
+ end
+ end
+
+ it "returns nil if #coerce does not return an Array" do
+ obj = mock("rb_num_coerce_cmp")
+ obj.should_receive(:coerce).with(2).and_return(nil)
+
+ @s.rb_num_coerce_cmp(2, obj, :<=>).should be_nil
+ end
+ end
+
+ describe "rb_num_coerce_relop" do
+ it "calls #coerce on the first argument" do
+ obj = mock("rb_num_coerce_relop")
+ obj.should_receive(:coerce).with(2).and_return([1, 2])
+
+ @s.rb_num_coerce_relop(2, obj, :<).should be_true
+ end
+
+ it "calls the specified method on the first argument returned by #coerce" do
+ obj = mock("rb_num_coerce_relop")
+ obj.should_receive(:coerce).with(2).and_return([obj, 2])
+ obj.should_receive(:<).with(2).and_return(false)
+
+ @s.rb_num_coerce_relop(2, obj, :<).should be_false
+ end
+
+ it "raises an ArgumentError if #<op> returns nil" do
+ obj = mock("rb_num_coerce_relop")
+ obj.should_receive(:coerce).with(2).and_return([obj, 2])
+ obj.should_receive(:<).with(2).and_return(nil)
+
+ lambda { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError if #coerce does not return an Array" do
+ obj = mock("rb_num_coerce_relop")
+ obj.should_receive(:coerce).with(2).and_return(nil)
+
+ lambda { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError)
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb
new file mode 100644
index 0000000000..b16d26ce47
--- /dev/null
+++ b/spec/ruby/optional/capi/object_spec.rb
@@ -0,0 +1,803 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("object")
+
+# TODO: fix all these specs
+
+class CApiObjectSpecs
+ class Alloc
+ attr_reader :initialized, :arguments
+
+ def initialize(*args)
+ @initialized = true
+ @arguments = args
+ end
+ end
+
+ class SubArray < ::Array
+ def to_array
+ self
+ end
+ end
+end
+
+describe "CApiObject" do
+
+ before do
+ @o = CApiObjectSpecs.new
+ end
+
+ class ObjectTest
+ def initialize
+ @foo = 7
+ end
+
+ def foo
+ end
+
+ def private_foo
+ end
+ private :private_foo
+ end
+
+ class AryChild < Array
+ end
+
+ class StrChild < String
+ end
+
+ class DescObjectTest < ObjectTest
+ end
+
+ class MethodArity
+ def one; end
+ def two(a); end
+ def three(*a); end
+ def four(a, b); end
+ def five(a, b, *c); end
+ def six(a, b, *c, &d); end
+ end
+
+ describe "rb_obj_alloc" do
+ it "allocates a new uninitialized object" do
+ o = @o.rb_obj_alloc(CApiObjectSpecs::Alloc)
+ o.class.should == CApiObjectSpecs::Alloc
+ o.initialized.should be_nil
+ end
+ end
+
+ describe "rb_obj_dup" do
+ it "duplicates an object" do
+ obj1 = ObjectTest.new
+ obj2 = @o.rb_obj_dup(obj1)
+
+ obj2.class.should == obj1.class
+
+ obj2.foo.should == obj1.foo
+
+ obj2.should_not equal(obj1)
+ end
+ end
+
+ describe "rb_obj_call_init" do
+ it "sends #initialize" do
+ o = @o.rb_obj_alloc(CApiObjectSpecs::Alloc)
+ o.initialized.should be_nil
+
+ @o.rb_obj_call_init(o, 2, [:one, :two])
+ o.initialized.should be_true
+ o.arguments.should == [:one, :two]
+ end
+ end
+
+ describe "rb_is_instance_of" do
+ it "returns true if an object is an instance" do
+ @o.rb_obj_is_instance_of(ObjectTest.new, ObjectTest).should == true
+ @o.rb_obj_is_instance_of(DescObjectTest.new, ObjectTest).should == false
+ end
+ end
+
+ describe "rb_is_kind_of" do
+ it "returns true if an object is an instance or descendent" do
+ @o.rb_obj_is_kind_of(ObjectTest.new, ObjectTest).should == true
+ @o.rb_obj_is_kind_of(DescObjectTest.new, ObjectTest).should == true
+ @o.rb_obj_is_kind_of(Object.new, ObjectTest).should == false
+ end
+ end
+
+ describe "rb_respond_to" do
+ it "returns 1 if respond_to? is true and 0 if respond_to? is false" do
+ @o.rb_respond_to(ObjectTest.new, :foo).should == true
+ @o.rb_respond_to(ObjectTest.new, :bar).should == false
+ end
+ end
+
+ describe "rb_obj_respond_to" do
+ it "returns true if respond_to? is true and false if respond_to? is false" do
+ @o.rb_obj_respond_to(ObjectTest.new, :foo, true).should == true
+ @o.rb_obj_respond_to(ObjectTest.new, :bar, true).should == false
+ @o.rb_obj_respond_to(ObjectTest.new, :private_foo, false).should == false
+ @o.rb_obj_respond_to(ObjectTest.new, :private_foo, true).should == true
+ end
+ end
+
+ describe "rb_obj_method_arity" do
+ before :each do
+ @obj = MethodArity.new
+ end
+
+ it "returns 0 when the method takes no arguments" do
+ @o.rb_obj_method_arity(@obj, :one).should == 0
+ end
+
+ it "returns 1 when the method takes a single, required argument" do
+ @o.rb_obj_method_arity(@obj, :two).should == 1
+ end
+
+ it "returns -1 when the method takes a variable number of arguments" do
+ @o.rb_obj_method_arity(@obj, :three).should == -1
+ end
+
+ it "returns 2 when the method takes two required arguments" do
+ @o.rb_obj_method_arity(@obj, :four).should == 2
+ end
+
+ it "returns -N-1 when the method takes N required and variable additional arguments" do
+ @o.rb_obj_method_arity(@obj, :five).should == -3
+ end
+
+ it "returns -N-1 when the method takes N required, variable additional, and a block argument" do
+ @o.rb_obj_method_arity(@obj, :six).should == -3
+ end
+ end
+
+ describe "rb_method_boundp" do
+ it "returns true when the given method is bound" do
+ @o.rb_method_boundp(Object, :class, true).should == true
+ @o.rb_method_boundp(Object, :class, false).should == true
+ @o.rb_method_boundp(Object, :initialize, true).should == false
+ @o.rb_method_boundp(Object, :initialize, false).should == true
+ end
+
+ it "returns false when the given method is not bound" do
+ @o.rb_method_boundp(Object, :foo, true).should == false
+ @o.rb_method_boundp(Object, :foo, false).should == false
+ end
+ end
+
+ describe "rb_to_id" do
+ it "returns a symbol representation of the object" do
+ @o.rb_to_id("foo").should == :foo
+ @o.rb_to_id(:foo).should == :foo
+ end
+ end
+
+ describe "rb_require" do
+ before :each do
+ @saved_loaded_features = $LOADED_FEATURES.dup
+ $foo = nil
+ end
+
+ after :each do
+ $foo = nil
+ $LOADED_FEATURES.replace @saved_loaded_features
+ end
+
+ it "requires a ruby file" do
+ $:.unshift File.dirname(__FILE__)
+ @o.rb_require()
+ $foo.should == 7
+ end
+ end
+
+ describe "rb_attr_get" do
+ it "gets an instance variable" do
+ o = ObjectTest.new
+ @o.rb_attr_get(o, :@foo).should == 7
+ end
+ end
+
+ describe "rb_obj_instance_variables" do
+ it "returns an array with instance variable names as symbols" do
+ o = ObjectTest.new
+ @o.rb_obj_instance_variables(o).should include(:@foo)
+ end
+ end
+
+ describe "rb_check_convert_type" do
+ it "returns the passed object and does not call the converting method if the object is the specified type" do
+ ary = [1, 2]
+ ary.should_not_receive(:to_ary)
+
+ @o.rb_check_convert_type(ary, "Array", "to_ary").should equal(ary)
+ end
+
+ it "returns the passed object and does not call the converting method if the object is a subclass of the specified type" do
+ obj = CApiObjectSpecs::SubArray.new
+ obj.should_not_receive(:to_array)
+
+ @o.rb_check_convert_type(obj, "Array", "to_array").should equal(obj)
+ end
+
+ it "returns nil if the converting method returns nil" do
+ obj = mock("rb_check_convert_type")
+ obj.should_receive(:to_array).and_return(nil)
+
+ @o.rb_check_convert_type(obj, "Array", "to_array").should be_nil
+ end
+
+ it "raises a TypeError if the converting method returns an object that is not the specified type" do
+ obj = mock("rb_check_convert_type")
+ obj.should_receive(:to_array).and_return("string")
+
+ lambda do
+ @o.rb_check_convert_type(obj, "Array", "to_array")
+ end.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_convert_type" do
+ it "returns the passed object and does not call the converting method if the object is the specified type" do
+ ary = [1, 2]
+ ary.should_not_receive(:to_ary)
+
+ @o.rb_convert_type(ary, "Array", "to_ary").should equal(ary)
+ end
+
+ it "returns the passed object and does not call the converting method if the object is a subclass of the specified type" do
+ obj = CApiObjectSpecs::SubArray.new
+ obj.should_not_receive(:to_array)
+
+ @o.rb_convert_type(obj, "Array", "to_array").should equal(obj)
+ end
+
+ it "raises a TypeError if the converting method returns nil" do
+ obj = mock("rb_convert_type")
+ obj.should_receive(:to_array).and_return(nil)
+
+ lambda do
+ @o.rb_convert_type(obj, "Array", "to_array")
+ end.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if the converting method returns an object that is not the specified type" do
+ obj = mock("rb_convert_type")
+ obj.should_receive(:to_array).and_return("string")
+
+ lambda do
+ @o.rb_convert_type(obj, "Array", "to_array")
+ end.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_check_array_type" do
+ it "returns the argument if it's an Array" do
+ x = Array.new
+ @o.rb_check_array_type(x).should equal(x)
+ end
+
+ it "returns the argument if it's a kind of Array" do
+ x = AryChild.new
+ @o.rb_check_array_type(x).should equal(x)
+ end
+
+ it "returns nil when the argument does not respond to #to_ary" do
+ @o.rb_check_array_type(Object.new).should be_nil
+ end
+
+ it "sends #to_ary to the argument and returns the result if it's nil" do
+ obj = mock("to_ary")
+ obj.should_receive(:to_ary).and_return(nil)
+ @o.rb_check_array_type(obj).should be_nil
+ end
+
+ it "sends #to_ary to the argument and returns the result if it's an Array" do
+ x = Array.new
+ obj = mock("to_ary")
+ obj.should_receive(:to_ary).and_return(x)
+ @o.rb_check_array_type(obj).should equal(x)
+ end
+
+ it "sends #to_ary to the argument and returns the result if it's a kind of Array" do
+ x = AryChild.new
+ obj = mock("to_ary")
+ obj.should_receive(:to_ary).and_return(x)
+ @o.rb_check_array_type(obj).should equal(x)
+ end
+
+ it "sends #to_ary to the argument and raises TypeError if it's not a kind of Array" do
+ obj = mock("to_ary")
+ obj.should_receive(:to_ary).and_return(Object.new)
+ lambda { @o.rb_check_array_type obj }.should raise_error(TypeError)
+ end
+
+ it "does not rescue exceptions raised by #to_ary" do
+ obj = mock("to_ary")
+ obj.should_receive(:to_ary).and_raise(RuntimeError)
+ lambda { @o.rb_check_array_type obj }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_check_string_type" do
+ it "returns the argument if it's a String" do
+ x = String.new
+ @o.rb_check_string_type(x).should equal(x)
+ end
+
+ it "returns the argument if it's a kind of String" do
+ x = StrChild.new
+ @o.rb_check_string_type(x).should equal(x)
+ end
+
+ it "returns nil when the argument does not respond to #to_str" do
+ @o.rb_check_string_type(Object.new).should be_nil
+ end
+
+ it "sends #to_str to the argument and returns the result if it's nil" do
+ obj = mock("to_str")
+ obj.should_receive(:to_str).and_return(nil)
+ @o.rb_check_string_type(obj).should be_nil
+ end
+
+ it "sends #to_str to the argument and returns the result if it's a String" do
+ x = String.new
+ obj = mock("to_str")
+ obj.should_receive(:to_str).and_return(x)
+ @o.rb_check_string_type(obj).should equal(x)
+ end
+
+ it "sends #to_str to the argument and returns the result if it's a kind of String" do
+ x = StrChild.new
+ obj = mock("to_str")
+ obj.should_receive(:to_str).and_return(x)
+ @o.rb_check_string_type(obj).should equal(x)
+ end
+
+ it "sends #to_str to the argument and raises TypeError if it's not a kind of String" do
+ obj = mock("to_str")
+ obj.should_receive(:to_str).and_return(Object.new)
+ lambda { @o.rb_check_string_type obj }.should raise_error(TypeError)
+ end
+
+ it "does not rescue exceptions raised by #to_str" do
+ obj = mock("to_str")
+ obj.should_receive(:to_str).and_raise(RuntimeError)
+ lambda { @o.rb_check_string_type obj }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_check_to_integer" do
+ it "returns the object when passed a Fixnum" do
+ @o.rb_check_to_integer(5, "to_int").should equal(5)
+ end
+
+ it "returns the object when passed a Bignum" do
+ @o.rb_check_to_integer(bignum_value, "to_int").should == bignum_value
+ end
+
+ it "calls the converting method and returns a Fixnum value" do
+ obj = mock("rb_check_to_integer")
+ obj.should_receive(:to_integer).and_return(10)
+
+ @o.rb_check_to_integer(obj, "to_integer").should equal(10)
+ end
+
+ it "calls the converting method and returns a Bignum value" do
+ obj = mock("rb_check_to_integer")
+ obj.should_receive(:to_integer).and_return(bignum_value)
+
+ @o.rb_check_to_integer(obj, "to_integer").should == bignum_value
+ end
+
+ it "returns nil when the converting method returns nil" do
+ obj = mock("rb_check_to_integer")
+ obj.should_receive(:to_integer).and_return(nil)
+
+ @o.rb_check_to_integer(obj, "to_integer").should be_nil
+ end
+
+ it "returns nil when the converting method does not return an Integer" do
+ obj = mock("rb_check_to_integer")
+ obj.should_receive(:to_integer).and_return("string")
+
+ @o.rb_check_to_integer(obj, "to_integer").should be_nil
+ end
+ end
+
+ describe "rb_inspect" do
+ it "returns a string with the inspect representation" do
+ @o.rb_inspect(nil).should == "nil"
+ @o.rb_inspect(0).should == '0'
+ @o.rb_inspect([1,2,3]).should == '[1, 2, 3]'
+ @o.rb_inspect("0").should == '"0"'
+ end
+ end
+
+ describe "rb_class_of" do
+ it "returns the class of an object" do
+ @o.rb_class_of(nil).should == NilClass
+ @o.rb_class_of(0).should == Fixnum
+ @o.rb_class_of(0.1).should == Float
+ @o.rb_class_of(ObjectTest.new).should == ObjectTest
+ end
+ end
+
+ describe "rb_obj_classname" do
+ it "returns the class name of an object" do
+ @o.rb_obj_classname(nil).should == 'NilClass'
+ @o.rb_obj_classname(0).should == Fixnum.to_s
+ @o.rb_obj_classname(0.1).should == 'Float'
+ @o.rb_obj_classname(ObjectTest.new).should == 'ObjectTest'
+ end
+ end
+
+ describe "rb_type" do
+ it "returns the type constant for the object" do
+ class DescArray < Array
+ end
+ @o.rb_is_type_nil(nil).should == true
+ @o.rb_is_type_object([]).should == false
+ @o.rb_is_type_object(ObjectTest.new).should == true
+ @o.rb_is_type_array([]).should == true
+ @o.rb_is_type_array(DescArray.new).should == true
+ @o.rb_is_type_module(ObjectTest).should == false
+ @o.rb_is_type_class(ObjectTest).should == true
+ @o.rb_is_type_data(Time.now).should == true
+ end
+ end
+
+ describe "rb_type_p" do
+ it "returns whether object is of the given type" do
+ class DescArray < Array
+ end
+ @o.rb_is_rb_type_p_nil(nil).should == true
+ @o.rb_is_rb_type_p_object([]).should == false
+ @o.rb_is_rb_type_p_object(ObjectTest.new).should == true
+ @o.rb_is_rb_type_p_array([]).should == true
+ @o.rb_is_rb_type_p_array(DescArray.new).should == true
+ @o.rb_is_rb_type_p_module(ObjectTest).should == false
+ @o.rb_is_rb_type_p_class(ObjectTest).should == true
+ @o.rb_is_rb_type_p_data(Time.now).should == true
+ end
+ end
+
+ describe "BUILTIN_TYPE" do
+ it "returns the type constant for the object" do
+ class DescArray < Array
+ end
+ @o.rb_is_builtin_type_object([]).should == false
+ @o.rb_is_builtin_type_object(ObjectTest.new).should == true
+ @o.rb_is_builtin_type_array([]).should == true
+ @o.rb_is_builtin_type_array(DescArray.new).should == true
+ @o.rb_is_builtin_type_module(ObjectTest).should == false
+ @o.rb_is_builtin_type_class(ObjectTest).should == true
+ @o.rb_is_builtin_type_data(Time.now).should == true
+ end
+ end
+
+ describe "RTEST" do
+ it "returns C false if passed Qfalse" do
+ @o.RTEST(false).should be_false
+ end
+
+ it "returns C false if passed Qnil" do
+ @o.RTEST(nil).should be_false
+ end
+
+ it "returns C true if passed Qtrue" do
+ @o.RTEST(true).should be_true
+ end
+
+ it "returns C true if passed a Symbol" do
+ @o.RTEST(:test).should be_true
+ end
+
+ it "returns C true if passed an Object" do
+ @o.RTEST(Object.new).should be_true
+ end
+ end
+
+ describe "rb_special_const_p" do
+ it "returns true if passed Qfalse" do
+ @o.rb_special_const_p(false).should be_true
+ end
+
+ it "returns true if passed Qtrue" do
+ @o.rb_special_const_p(true).should be_true
+ end
+
+ it "returns true if passed Qnil" do
+ @o.rb_special_const_p(nil).should be_true
+ end
+
+ it "returns true if passed a Symbol" do
+ @o.rb_special_const_p(:test).should be_true
+ end
+
+ it "returns true if passed a Fixnum" do
+ @o.rb_special_const_p(10).should be_true
+ end
+
+ it "returns false if passed an Object" do
+ @o.rb_special_const_p(Object.new).should be_false
+ end
+ end
+
+ describe "rb_extend_object" do
+ it "adds the module's instance methods to the object" do
+ module CApiObjectSpecs::Extend
+ def reach
+ :extended
+ end
+ end
+
+ obj = mock("extended object")
+ @o.rb_extend_object(obj, CApiObjectSpecs::Extend)
+ obj.reach.should == :extended
+ end
+ end
+
+ describe "OBJ_TAINT" do
+ it "taints the object" do
+ obj = mock("tainted")
+ @o.OBJ_TAINT(obj)
+ obj.tainted?.should be_true
+ end
+ end
+
+ describe "OBJ_TAINTED" do
+ it "returns C true if the object is tainted" do
+ obj = mock("tainted")
+ obj.taint
+ @o.OBJ_TAINTED(obj).should be_true
+ end
+
+ it "returns C false if the object is not tainted" do
+ obj = mock("untainted")
+ @o.OBJ_TAINTED(obj).should be_false
+ end
+ end
+
+ describe "OBJ_INFECT" do
+ it "does not taint the first argument if the second argument is not tainted" do
+ host = mock("host")
+ source = mock("source")
+ @o.OBJ_INFECT(host, source)
+ host.tainted?.should be_false
+ end
+
+ it "taints the first argument if the second argument is tainted" do
+ host = mock("host")
+ source = mock("source").taint
+ @o.OBJ_INFECT(host, source)
+ host.tainted?.should be_true
+ end
+
+ it "does not untrust the first argument if the second argument is trusted" do
+ host = mock("host")
+ source = mock("source")
+ @o.OBJ_INFECT(host, source)
+ host.untrusted?.should be_false
+ end
+
+ it "untrusts the first argument if the second argument is untrusted" do
+ host = mock("host")
+ source = mock("source").untrust
+ @o.OBJ_INFECT(host, source)
+ host.untrusted?.should be_true
+ end
+
+ it "propagates both taint and distrust" do
+ host = mock("host")
+ source = mock("source").taint.untrust
+ @o.OBJ_INFECT(host, source)
+ host.tainted?.should be_true
+ host.untrusted?.should be_true
+ end
+ end
+
+ describe "rb_obj_freeze" do
+ it "freezes the object passed to it" do
+ obj = ""
+ @o.rb_obj_freeze(obj).should == obj
+ obj.frozen?.should be_true
+ end
+ end
+
+ describe "rb_obj_instance_eval" do
+ it "evaluates the block in the object context, that includes private methods" do
+ obj = ObjectTest
+ lambda do
+ @o.rb_obj_instance_eval(obj) { include Kernel }
+ end.should_not raise_error(NoMethodError)
+ end
+ end
+
+ describe "rb_obj_frozen_p" do
+ it "returns true if object passed to it is frozen" do
+ obj = ""
+ obj.freeze
+ @o.rb_obj_frozen_p(obj).should == true
+ end
+
+ it "returns false if object passed to it is not frozen" do
+ obj = ""
+ @o.rb_obj_frozen_p(obj).should == false
+ end
+ end
+
+ describe "rb_obj_taint" do
+ it "marks the object passed as tainted" do
+ obj = ""
+ obj.tainted?.should == false
+ @o.rb_obj_taint(obj)
+ obj.tainted?.should == true
+ end
+
+ it "raises a RuntimeError if the object passed is frozen" do
+ lambda { @o.rb_obj_taint("".freeze) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_check_frozen" do
+ it "raises a RuntimeError if the obj is frozen" do
+ lambda { @o.rb_check_frozen("".freeze) }.should raise_error(RuntimeError)
+ end
+
+ it "does nothing when object isn't frozen" do
+ obj = ""
+ lambda { @o.rb_check_frozen(obj) }.should_not raise_error(TypeError)
+ end
+ end
+
+ describe "rb_any_to_s" do
+ it "converts an Integer to string" do
+ obj = 1
+ i = @o.rb_any_to_s(obj)
+ i.should be_kind_of(String)
+ end
+
+ it "converts an Object to string" do
+ obj = Object.new
+ i = @o.rb_any_to_s(obj)
+ i.should be_kind_of(String)
+ end
+ end
+
+ describe "rb_to_int" do
+ it "returns self when called on an Integer" do
+ @o.rb_to_int(5).should == 5
+ end
+
+ it "returns self when called on a Bignum" do
+ @o.rb_to_int(bignum_value).should == bignum_value
+ end
+
+ it "calls #to_int to convert and object to an integer" do
+ x = mock("to_int")
+ x.should_receive(:to_int).and_return(5)
+ @o.rb_to_int(x).should == 5
+ end
+
+ it "converts a Float to an Integer by truncation" do
+ @o.rb_to_int(1.35).should == 1
+ end
+
+ it "raises a TypeError if #to_int does not return an Integer" do
+ x = mock("to_int")
+ x.should_receive(:to_int).and_return("5")
+ lambda { @o.rb_to_int(x) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if called with nil" do
+ lambda { @o.rb_to_int(nil) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if called with true" do
+ lambda { @o.rb_to_int(true) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if called with false" do
+ lambda { @o.rb_to_int(false) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError if called with a String" do
+ lambda { @o.rb_to_int("1") }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_equal" do
+ it "returns true if the arguments are the same exact object" do
+ s = "hello"
+ @o.rb_equal(s, s).should be_true
+ end
+
+ it "calls == to check equality and coerces to true/false" do
+ m = mock("string")
+ m.should_receive(:==).and_return(8)
+ @o.rb_equal(m, "hello").should be_true
+
+ m2 = mock("string")
+ m2.should_receive(:==).and_return(nil)
+ @o.rb_equal(m2, "hello").should be_false
+ end
+ end
+
+ describe "rb_class_inherited_p" do
+
+ it "returns true if mod equals arg" do
+ @o.rb_class_inherited_p(Array, Array).should be_true
+ end
+
+ it "returns true if mod is a subclass of arg" do
+ @o.rb_class_inherited_p(Array, Object).should be_true
+ end
+
+ it "returns nil if mod is not a subclass of arg" do
+ @o.rb_class_inherited_p(Array, Hash).should be_nil
+ end
+
+ it "raises a TypeError if arg is no class or module" do
+ lambda{
+ @o.rb_class_inherited_p(1, 2)
+ }.should raise_error(TypeError)
+ end
+
+ end
+
+ describe "instance variable access" do
+ before do
+ @test = ObjectTest.new
+ end
+
+ describe "rb_iv_get" do
+ it "returns the instance variable on an object" do
+ @o.rb_iv_get(@test, "@foo").should == @test.instance_eval { @foo }
+ end
+
+ it "returns nil if the instance variable has not been initialized" do
+ @o.rb_iv_get(@test, "@bar").should == nil
+ end
+ end
+
+ describe "rb_iv_set" do
+ it "sets and returns the instance variable on an object" do
+ @o.rb_iv_set(@test, "@foo", 42).should == 42
+ @test.instance_eval { @foo }.should == 42
+ end
+
+ it "sets and returns the instance variable with a bare name" do
+ @o.rb_iv_set(@test, "foo", 42).should == 42
+ @o.rb_iv_get(@test, "foo").should == 42
+ @test.instance_eval { @foo }.should == 7
+ end
+ end
+
+ describe "rb_ivar_get" do
+ it "returns the instance variable on an object" do
+ @o.rb_ivar_get(@test, :@foo).should == @test.instance_eval { @foo }
+ end
+
+ it "returns nil if the instance variable has not been initialized" do
+ @o.rb_ivar_get(@test, :@bar).should == nil
+ end
+ end
+
+ describe "rb_ivar_set" do
+ it "sets and returns the instance variable on an object" do
+ @o.rb_ivar_set(@test, :@foo, 42).should == 42
+ @test.instance_eval { @foo }.should == 42
+ end
+ end
+
+ describe "rb_ivar_defined" do
+ it "returns true if the instance variable is defined" do
+ @o.rb_ivar_defined(@test, :@foo).should == true
+ end
+
+ it "returns false if the instance variable is not defined" do
+ @o.rb_ivar_defined(@test, :@bar).should == false
+ end
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/proc_spec.rb b/spec/ruby/optional/capi/proc_spec.rb
new file mode 100644
index 0000000000..1cd8de9a86
--- /dev/null
+++ b/spec/ruby/optional/capi/proc_spec.rb
@@ -0,0 +1,112 @@
+require File.expand_path('../spec_helper', __FILE__)
+require File.expand_path('../fixtures/proc', __FILE__)
+
+load_extension("proc")
+
+describe "C-API Proc function" do
+ before :each do
+ @p = CApiProcSpecs.new
+ @prc = @p.rb_proc_new
+ end
+
+ describe "rb_proc_new" do
+ it "returns a new valid Proc" do
+ @prc.kind_of?(Proc).should == true
+ end
+
+ it "calls the C function wrapped by the Proc instance when sent #call" do
+ @prc.call(:foo_bar).should == ":foo_bar"
+ @prc.call([:foo, :bar]).should == "[:foo, :bar]"
+ end
+
+ it "calls the C function wrapped by the Proc instance when sent #[]" do
+ @prc[:foo_bar].should == ":foo_bar"
+ @prc[[:foo, :bar]].should == "[:foo, :bar]"
+ end
+
+ it "returns a Proc instance correctly described in #inspect without source location" do
+ @prc.inspect.should =~ /^#<Proc:([^ :@]*?)>$/
+ end
+
+ it "returns a Proc instance with #arity == -1" do
+ @prc.arity.should == -1
+ end
+
+ it "shouldn't be equal to another one" do
+ @prc.should_not == @p.rb_proc_new
+ end
+
+ it "returns a Proc instance with #source_location == nil" do
+ @prc.source_location.should == nil
+ end
+ end
+
+ describe "rb_proc_arity" do
+ it "returns the correct arity" do
+ prc = Proc.new {|a,b,c|}
+ @p.rb_proc_arity(prc).should == 3
+ end
+ end
+
+ describe "rb_proc_call" do
+ it "calls the Proc" do
+ prc = Proc.new {|a,b| a * b }
+ @p.rb_proc_call(prc, [6, 7]).should == 42
+ end
+ end
+end
+
+describe "C-API when calling Proc.new from a C function" do
+ before :each do
+ @p = CApiProcSpecs.new
+ end
+
+ # In the scenarios below: X -> Y means execution context X called to Y.
+ # For example: Ruby -> C means a Ruby code called a C function.
+ #
+ # X -> Y <- X -> Z means exection context X called Y which returned to X,
+ # then X called Z.
+ # For example: C -> Ruby <- C -> Ruby means a C function called into Ruby
+ # code which returned to C, then C called into Ruby code again.
+
+ # Ruby -> C -> rb_funcall(Proc.new)
+ it "returns the Proc passed by the Ruby code calling the C function" do
+ prc = @p.rb_Proc_new(0) { :called }
+ prc.call.should == :called
+ end
+
+ # Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new)
+ it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do
+ prc = @p.rb_Proc_new(1) { :called }
+ prc.call.should == :called
+ end
+
+ # Ruby -> C -> Ruby -> Proc.new
+ it "raises an ArgumentError when the C function calls a Ruby method that calls Proc.new" do
+ def @p.Proc_new() Proc.new end
+ lambda { @p.rb_Proc_new(2) { :called } }.should raise_error(ArgumentError)
+ end
+
+ # Ruby -> C -> Ruby -> C -> rb_funcall(Proc.new)
+ it "raises an ArgumentError when the C function calls a Ruby method and that method calls a C function that calls Proc.new" do
+ def @p.redispatch() rb_Proc_new(0) end
+ lambda { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError)
+ end
+
+ # Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new)
+ it "returns the most recent Proc passed when the Ruby method called the C function" do
+ prc = @p.rb_Proc_new(4) { :called }
+ prc.call.should == :calling_with_block
+ end
+
+ # Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new)
+ it "returns the Proc passed from the original Ruby call to the C function" do
+ prc = @p.rb_Proc_new(5) { :called }
+ prc.call.should == :called
+ end
+
+ # Ruby -> C -> Ruby -> block_given?
+ it "returns false from block_given? in a Ruby method called by the C function" do
+ @p.rb_Proc_new(6).should be_false
+ end
+end
diff --git a/spec/ruby/optional/capi/rake_helper.rb b/spec/ruby/optional/capi/rake_helper.rb
new file mode 100644
index 0000000000..2b7a6ccbe3
--- /dev/null
+++ b/spec/ruby/optional/capi/rake_helper.rb
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/clean'
+
+input = "#{$cwd}/#{$ext_name}.c"
+common = "-I shotgun/lib/subtend -g #{input}"
+
+case PLATFORM
+when /darwin/
+ output = "#{$cwd}/#{$ext_name}.bundle"
+ build_cmd = "cc -bundle -undefined suppress -flat_namespace #{common} -o #{output}"
+else
+ output = "#{$cwd}/#{$ext_name}.so"
+ build_cmd = "cc -shared #{common} -o #{output}"
+end
+
+CLOBBER.include(output)
+
+task default: [output]
+
+file output => [input] do
+ sh build_cmd
+end
+
diff --git a/spec/ruby/optional/capi/range_spec.rb b/spec/ruby/optional/capi/range_spec.rb
new file mode 100644
index 0000000000..aa16f9773e
--- /dev/null
+++ b/spec/ruby/optional/capi/range_spec.rb
@@ -0,0 +1,95 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("range")
+
+describe "C-API Range function" do
+ before :each do
+ @s = CApiRangeSpecs.new
+ end
+
+ describe "rb_range_new" do
+ it "constructs a range using the given start and end" do
+ range = @s.rb_range_new('a', 'c')
+ range.should == ('a'..'c')
+
+ range.first.should == 'a'
+ range.last.should == 'c'
+ end
+
+ it "includes the end object when the third parameter is omitted or false" do
+ @s.rb_range_new('a', 'c').to_a.should == ['a', 'b', 'c']
+ @s.rb_range_new(1, 3).to_a.should == [1, 2, 3]
+
+ @s.rb_range_new('a', 'c', false).to_a.should == ['a', 'b', 'c']
+ @s.rb_range_new(1, 3, false).to_a.should == [1, 2, 3]
+
+ @s.rb_range_new('a', 'c', true).to_a.should == ['a', 'b']
+ @s.rb_range_new(1, 3, 1).to_a.should == [1, 2]
+
+ @s.rb_range_new(1, 3, mock('[1,2]')).to_a.should == [1, 2]
+ @s.rb_range_new(1, 3, :test).to_a.should == [1, 2]
+ end
+
+ it "raises an ArgumentError when the given start and end can't be compared by using #<=>" do
+ lambda { @s.rb_range_new(1, mock('x')) }.should raise_error(ArgumentError)
+ lambda { @s.rb_range_new(mock('x'), mock('y')) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_range_values" do
+ it "stores the range properties" do
+ beg, fin, excl = @s.rb_range_values(10..20)
+ beg.should == 10
+ fin.should == 20
+ excl.should be_false
+ end
+
+ it "stores the range properties of non-Range object" do
+ range_like = mock('range')
+
+ def range_like.begin
+ 10
+ end
+
+ def range_like.end
+ 20
+ end
+
+ def range_like.exclude_end?
+ false
+ end
+
+ beg, fin, excl = @s.rb_range_values(range_like)
+ beg.should == 10
+ fin.should == 20
+ excl.should be_false
+ end
+ end
+
+ describe "rb_range_beg_len" do
+ it "returns correct begin, length and result" do
+ r = 2..5
+ begp, lenp, result = @s.rb_range_beg_len(r, 0, 0, 10, 0)
+ result.should be_true
+ begp.should == 2
+ lenp.should == 4
+ end
+
+ it "returns nil when not in range" do
+ r = 2..5
+ begp, lenp, result = @s.rb_range_beg_len(r, 0, 0, 1, 0)
+ result.should be_nil
+ end
+
+ it "raises a RangeError when not in range and err is 1" do
+ r = -5..-1
+ lambda { @s.rb_range_beg_len(r, 0, 0, 1, 1) }.should raise_error(RangeError)
+ end
+
+ it "returns nil when not in range and err is 0" do
+ r = -5..-1
+ begp, lenp, result = @s.rb_range_beg_len(r, 0, 0, 1, 0)
+ result.should be_nil
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/rational_spec.rb b/spec/ruby/optional/capi/rational_spec.rb
new file mode 100644
index 0000000000..e3144666d3
--- /dev/null
+++ b/spec/ruby/optional/capi/rational_spec.rb
@@ -0,0 +1,57 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("rational")
+
+describe :rb_Rational, shared: true do
+ it "creates a new Rational with numerator and denominator" do
+ @r.send(@method, 1, 2).should == Rational(1, 2)
+ end
+end
+
+describe :rb_rational_new, shared: true do
+ it "creates a normalized Rational" do
+ r = @r.send(@method, 10, 4)
+ r.numerator.should == 5
+ r.denominator.should == 2
+ end
+end
+
+describe "CApiRationalSpecs" do
+ before :each do
+ @r = CApiRationalSpecs.new
+ end
+
+ describe "rb_Rational" do
+ it_behaves_like :rb_Rational, :rb_Rational
+ end
+
+ describe "rb_Rational2" do
+ it_behaves_like :rb_Rational, :rb_Rational2
+ end
+
+ describe "rb_Rational1" do
+ it "creates a new Rational with numerator and denominator of 1" do
+ @r.rb_Rational1(5).should == Rational(5, 1)
+ end
+ end
+
+ describe "rb_rational_new" do
+ it_behaves_like :rb_rational_new, :rb_rational_new
+ end
+
+ describe "rb_rational_new2" do
+ it_behaves_like :rb_rational_new, :rb_rational_new2
+ end
+
+ describe "rb_rational_num" do
+ it "returns the numerator of a Rational" do
+ @r.rb_rational_num(Rational(7, 2)).should == 7
+ end
+ end
+
+ describe "rb_rational_den" do
+ it "returns the denominator of a Rational" do
+ @r.rb_rational_den(Rational(7, 2)).should == 2
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/regexp_spec.rb b/spec/ruby/optional/capi/regexp_spec.rb
new file mode 100644
index 0000000000..27b1627c50
--- /dev/null
+++ b/spec/ruby/optional/capi/regexp_spec.rb
@@ -0,0 +1,71 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("regexp")
+
+describe "C-API Regexp function" do
+ before :each do
+ @p = CApiRegexpSpecs.new
+ end
+
+ describe "rb_reg_new" do
+ it "returns a new valid Regexp" do
+ my_re = @p.a_re
+ my_re.kind_of?(Regexp).should == true
+ ('1a' =~ my_re).should == 1
+ ('1b' =~ my_re).should == nil
+ my_re.source.should == 'a'
+ end
+ end
+
+ describe "rb_reg_nth_match" do
+ it "returns a the appropriate match data entry" do
+ @p.a_re_1st_match(/([ab])/.match("a")).should == 'a'
+ @p.a_re_1st_match(/([ab])/.match("b")).should == 'b'
+ @p.a_re_1st_match(/[ab]/.match("a")).should == nil
+ @p.a_re_1st_match(/[ab]/.match("c")).should == nil
+ end
+ end
+
+ describe "rb_reg_options" do
+ it "returns the options used to create the regexp" do
+ @p.rb_reg_options(/42/im).should == //im.options
+ @p.rb_reg_options(/42/i).should == //i.options
+ @p.rb_reg_options(/42/m).should == //m.options
+ end
+ end
+
+ describe "rb_reg_regcomp" do
+ it "creates a valid regexp from a string" do
+ regexp = /\b([A-Z0-9._%+-]+)\.{2,4}/
+ @p.rb_reg_regcomp(regexp.source).should == regexp
+ end
+ end
+
+ it "allows matching in C, properly setting the back references" do
+ mail_regexp = /\b([A-Z0-9._%+-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})\b/i
+ name = "john.doe"
+ domain = "example.com"
+ @p.match(mail_regexp, "#{name}@#{domain}")
+ $1.should == name
+ $2.should == domain
+ end
+
+ describe "rb_reg_match" do
+ it "returns the matched position or nil" do
+ @p.rb_reg_match(/a/, 'ab').should == 0
+ @p.rb_reg_match(/b/, 'ab').should == 1
+ @p.rb_reg_match(/c/, 'ab').should == nil
+ end
+ end
+
+ describe "rb_backref_get" do
+ it "returns the last MatchData" do
+ md = /a/.match('ab')
+ @p.rb_backref_get.should == md
+ md = /b/.match('ab')
+ @p.rb_backref_get.should == md
+ md = /c/.match('ab')
+ @p.rb_backref_get.should == md
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb
new file mode 100644
index 0000000000..21d2cd04c5
--- /dev/null
+++ b/spec/ruby/optional/capi/spec_helper.rb
@@ -0,0 +1,109 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+# MRI magic to use built but not installed ruby
+$extmk = false
+
+require 'rbconfig'
+
+OBJDIR ||= File.expand_path("../../../ext/#{RUBY_ENGINE}/#{RUBY_VERSION}", __FILE__)
+mkdir_p(OBJDIR)
+
+def extension_path
+ File.expand_path("../ext", __FILE__)
+end
+
+def object_path
+ OBJDIR
+end
+
+def compile_extension(name)
+ debug = false
+ run_mkmf_in_process = RUBY_ENGINE == 'truffleruby'
+
+ ext = "#{name}_spec"
+ lib = "#{object_path}/#{ext}.#{RbConfig::CONFIG['DLEXT']}"
+ ruby_header = "#{RbConfig::CONFIG['rubyhdrdir']}/ruby.h"
+
+ return lib if File.exist?(lib) and
+ File.mtime(lib) > File.mtime("#{extension_path}/rubyspec.h") and
+ File.mtime(lib) > File.mtime("#{extension_path}/#{ext}.c") and
+ File.mtime(lib) > File.mtime(ruby_header) and
+ true # sentinel
+
+ # Copy needed source files to tmpdir
+ tmpdir = tmp("cext_#{name}")
+ Dir.mkdir(tmpdir)
+ begin
+ ["rubyspec.h", "#{ext}.c"].each do |file|
+ cp "#{extension_path}/#{file}", "#{tmpdir}/#{file}"
+ end
+
+ Dir.chdir(tmpdir) do
+ if run_mkmf_in_process
+ required = require 'mkmf'
+ # Reinitialize mkmf if already required
+ init_mkmf unless required
+ create_makefile(ext, tmpdir)
+ else
+ File.write("extconf.rb", "require 'mkmf'\n" +
+ "$ruby = ENV.values_at('RUBY_EXE', 'RUBY_FLAGS').join(' ')\n" +
+ # MRI magic to consider building non-bundled extensions
+ "$extout = nil\n" +
+ "create_makefile(#{ext.inspect})\n")
+ output = ruby_exe("extconf.rb")
+ raise "extconf failed:\n#{output}" unless $?.success?
+ $stderr.puts output if debug
+ end
+
+ # Do not capture stderr as we want to show compiler warnings
+ make, opts = setup_make
+ output = IO.popen([make, "V=1", "DESTDIR=", opts], &:read)
+ raise "#{make} failed:\n#{output}" unless $?.success?
+ $stderr.puts output if debug
+
+ cp File.basename(lib), lib
+ end
+ ensure
+ rm_r tmpdir
+ end
+
+ File.chmod(0755, lib)
+ lib
+end
+
+def setup_make
+ make = ENV['MAKE']
+ make ||= (RbConfig::CONFIG['host_os'].include?("mswin") ? "nmake" : "make")
+ make_flags = ENV["MAKEFLAGS"] || ''
+
+ # suppress logo of nmake.exe to stderr
+ if File.basename(make, ".*").downcase == "nmake" and !make_flags.include?("l")
+ ENV["MAKEFLAGS"] = "l#{make_flags}"
+ end
+
+ opts = {}
+ if /(?:\A|\s)--jobserver-(?:auth|fds)=(\d+),(\d+)/ =~ make_flags
+ begin
+ r = IO.for_fd($1.to_i(10), "rb", autoclose: false)
+ w = IO.for_fd($2.to_i(10), "wb", autoclose: false)
+ rescue Errno::EBADF
+ else
+ opts[r] = r
+ opts[w] = w
+ end
+ end
+
+ [make, opts]
+end
+
+def load_extension(name)
+ require compile_extension(name)
+rescue LoadError
+ if %r{/usr/sbin/execerror ruby "\(ld 3 1 main ([/a-zA-Z0-9_\-.]+_spec\.so)"} =~ $!.message
+ system('/usr/sbin/execerror', "#{RbConfig::CONFIG["bindir"]}/ruby", "(ld 3 1 main #{$1}")
+ end
+ raise
+end
+
+# Constants
+CAPI_SIZEOF_LONG = [0].pack('l!').size
diff --git a/spec/ruby/optional/capi/st_spec.rb b/spec/ruby/optional/capi/st_spec.rb
new file mode 100644
index 0000000000..7f2bc08c62
--- /dev/null
+++ b/spec/ruby/optional/capi/st_spec.rb
@@ -0,0 +1,41 @@
+# encoding: utf-8
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension('st')
+
+describe "st hash table function" do
+ before :each do
+ @s = CApiStSpecs.new
+ end
+
+ describe "st_init_numtable" do
+ it "initializes without error" do
+ @s.st_init_numtable.should == 0
+ end
+ end
+
+ describe "st_init_numtable_with_size" do
+ it "initializes without error" do
+ @s.st_init_numtable_with_size.should == 0
+ end
+ end
+
+ describe "st_insert" do
+ it "returns size 1 after insert" do
+ @s.st_insert.should == 1
+ end
+ end
+
+ describe "st_foreach" do
+ it "iterates over each pair of key and value" do
+ @s.st_foreach.should == 7
+ end
+ end
+
+ describe "st_lookup" do
+ it "returns the expected value" do
+ @s.st_lookup.should == 42
+ end
+ end
+
+end
diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb
new file mode 100644
index 0000000000..25ef10b6f6
--- /dev/null
+++ b/spec/ruby/optional/capi/string_spec.rb
@@ -0,0 +1,787 @@
+# encoding: utf-8
+require File.expand_path('../spec_helper', __FILE__)
+require File.expand_path('../../../shared/string/times', __FILE__)
+
+load_extension('string')
+
+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"
+ @s.send(@method, "hello\0not used").should == "hello"
+ end
+
+ it "encodes the string with ASCII_8BIT" do
+ @s.send(@method, "hello").encoding.should == Encoding::ASCII_8BIT
+ end
+end
+
+describe "C-API String function" do
+ before :each do
+ @s = CApiStringSpecs.new
+ end
+
+ class ValidTostrTest
+ def to_str
+ "ruby"
+ end
+ end
+
+ class InvalidTostrTest
+ def to_str
+ []
+ end
+ end
+
+ describe "rb_str_set_len" do
+ before :each do
+ # Make a completely new copy of the string
+ # for every example (#dup doesn't cut it).
+ @str = "abcdefghij"[0..-1]
+ end
+
+ it "reduces the size of the string" do
+ @s.rb_str_set_len(@str, 5).should == "abcde"
+ end
+
+ it "inserts a NULL byte at the length" do
+ @s.rb_str_set_len(@str, 5).should == "abcde"
+ @s.rb_str_set_len(@str, 8).should == "abcde\x00gh"
+ end
+
+ it "updates the string's attributes visible in C code" do
+ @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4
+ 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 == ""
+ end
+
+ it "returns a string that can be appended to" do
+ str = @s.rb_str_buf_new(10, "defg")
+ str << "abcde"
+ str.should == "abcde"
+ end
+
+ it "returns a string that can be concatenated to another string" do
+ str = @s.rb_str_buf_new(10, "defg")
+ ("abcde" + str).should == "abcde"
+ end
+
+ it "returns a string whose bytes can be accessed by RSTRING_PTR" do
+ str = @s.rb_str_buf_new(10, "abcdefghi")
+ @s.rb_str_new(str, 10).should == "abcdefghi\x00"
+ end
+
+ it "returns a string that can be modified by rb_str_set_len" do
+ str = @s.rb_str_buf_new(10, "abcdef")
+ @s.rb_str_set_len(str, 4)
+ str.should == "abcd"
+
+ @s.rb_str_set_len(str, 8)
+ str[0, 6].should == "abcd\x00f"
+ @s.RSTRING_LEN(str).should == 8
+ end
+ end
+
+ describe "rb_str_buf_new2" do
+ it "returns a new string object calling strlen on the passed C string" do
+ # Hardcoded to pass const char * = "hello\0invisible"
+ @s.rb_str_buf_new2.should == "hello"
+ end
+ end
+
+ describe "rb_str_new" do
+ it "returns a new string object from a char buffer of len characters" do
+ @s.rb_str_new("hello", 3).should == "hel"
+ end
+
+ it "returns an empty string if len is 0" do
+ @s.rb_str_new("hello", 0).should == ""
+ end
+ end
+
+ describe "rb_str_new2" 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
+
+ describe "rb_usascii_str_new" do
+ it "creates a new String with US-ASCII Encoding from a char buffer of len characters" do
+ str = "abc".force_encoding("us-ascii")
+ result = @s.rb_usascii_str_new("abcdef", 3)
+ result.should == str
+ result.encoding.should == Encoding::US_ASCII
+ end
+ end
+
+ describe "rb_usascii_str_new_cstr" do
+ it "creates a new String with US-ASCII Encoding" do
+ str = "abc".force_encoding("us-ascii")
+ result = @s.rb_usascii_str_new_cstr("abc")
+ result.should == str
+ result.encoding.should == Encoding::US_ASCII
+ end
+ end
+
+ describe "rb_str_encode" do
+ it "returns a String in the destination encoding" do
+ result = @s.rb_str_encode("abc", Encoding::ISO_8859_1, 0, nil)
+ result.encoding.should == Encoding::ISO_8859_1
+ end
+
+ it "transcodes the String" do
+ result = @s.rb_str_encode("ありがとう", "euc-jp", 0, nil)
+ euc_jp = [0xa4, 0xa2, 0xa4, 0xea, 0xa4, 0xac, 0xa4, 0xc8, 0xa4, 0xa6].pack('C*').force_encoding("euc-jp")
+ result.should == euc_jp
+ result.encoding.should == Encoding::EUC_JP
+ end
+
+ it "returns a dup of the original String" do
+ a = "abc"
+ b = @s.rb_str_encode("abc", "us-ascii", 0, nil)
+ a.should_not equal(b)
+ end
+
+ it "returns a duplicate of the original when the encoding doesn't change" do
+ a = "abc"
+ b = @s.rb_str_encode("abc", Encoding::UTF_8, 0, nil)
+ a.should_not equal(b)
+ end
+
+ it "accepts encoding flags" do
+ xFF = [0xFF].pack('C').force_encoding('utf-8')
+ result = @s.rb_str_encode("a#{xFF}c", "us-ascii",
+ Encoding::Converter::INVALID_REPLACE, nil)
+ result.should == "a?c"
+ result.encoding.should == Encoding::US_ASCII
+ end
+
+ it "accepts an encoding options Hash specifying replacement String" do
+ # Yeah, MRI aborts with rb_bug() if the options Hash is not frozen
+ options = { replace: "b" }.freeze
+ xFF = [0xFF].pack('C').force_encoding('utf-8')
+ result = @s.rb_str_encode("a#{xFF}c", "us-ascii",
+ Encoding::Converter::INVALID_REPLACE,
+ options)
+ result.should == "abc"
+ result.encoding.should == Encoding::US_ASCII
+ end
+ end
+
+ describe "rb_str_new3" do
+ it "returns a copy of the string" do
+ str1 = "hi"
+ str2 = @s.rb_str_new3 str1
+ str1.should == str2
+ str1.object_id.should_not == str2.object_id
+ end
+ end
+
+ describe "rb_str_new4" do
+ it "returns the original string if it is already frozen" do
+ str1 = "hi"
+ str1.freeze
+ str2 = @s.rb_str_new4 str1
+ str1.should == str2
+ str1.should equal(str2)
+ str1.frozen?.should == true
+ str2.frozen?.should == true
+ end
+
+ it "returns a frozen copy of the string" do
+ str1 = "hi"
+ str2 = @s.rb_str_new4 str1
+ str1.should == str2
+ str1.should_not equal(str2)
+ str2.frozen?.should == true
+ end
+ end
+
+ describe "rb_str_dup" do
+ it "returns a copy of the string" do
+ str1 = "hi"
+ str2 = @s.rb_str_dup str1
+ str1.should == str2
+ str1.object_id.should_not == str2.object_id
+ end
+ end
+
+ describe "rb_str_new5" do
+ it "returns a new string with the same class as the passed string" do
+ string_class = Class.new(String)
+ template_string = string_class.new("hello world")
+ new_string = @s.rb_str_new5(template_string, "hello world", 11)
+
+ new_string.should == "hello world"
+ new_string.class.should == string_class
+ end
+ end
+
+ describe "rb_tainted_str_new" do
+ it "creates a new tainted String" do
+ newstring = @s.rb_tainted_str_new("test", 4)
+ newstring.should == "test"
+ newstring.tainted?.should be_true
+ end
+ end
+
+ describe "rb_tainted_str_new2" do
+ it "creates a new tainted String" do
+ newstring = @s.rb_tainted_str_new2("test")
+ newstring.should == "test"
+ newstring.tainted?.should be_true
+ end
+ end
+
+ describe "rb_str_append" do
+ it "appends a string to another string" do
+ @s.rb_str_append("Hello", " Goodbye").should == "Hello Goodbye"
+ end
+
+ it "raises a TypeError trying to append non-String-like object" do
+ lambda { @s.rb_str_append("Hello", 32323)}.should raise_error(TypeError)
+ end
+
+ it "changes Encoding if a string is appended to an empty string" do
+ string = "パスタ".encode(Encoding::ISO_2022_JP)
+ @s.rb_str_append("", string).encoding.should == Encoding::ISO_2022_JP
+ end
+ end
+
+ describe "rb_str_plus" do
+ it "returns a new string from concatenating two other strings" do
+ @s.rb_str_plus("Hello", " Goodbye").should == "Hello Goodbye"
+ end
+ end
+
+ describe "rb_str_times" do
+ it_behaves_like :string_times, :rb_str_times, ->(str, times) { @s.rb_str_times(str, times) }
+ end
+
+ describe "rb_str_buf_cat" do
+ it "concatenates a C string to a ruby string" do
+ @s.rb_str_buf_cat("Your house is on fire").should == "Your house is on fire?"
+ end
+ end
+
+ describe "rb_str_cat" do
+ it "concatenates a C string to ruby string" do
+ @s.rb_str_cat("Your house is on fire").should == "Your house is on fire?"
+ end
+ end
+
+ describe "rb_str_cat2" do
+ it "concatenates a C string to a ruby string" do
+ @s.rb_str_cat2("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
+ end
+
+ it "returns -1 if the first string is shorter than the second" do
+ @s.rb_str_cmp("xxx", "xxxx").should == -1
+ end
+
+ it "returns -1 if the first string is lexically less than the second" do
+ @s.rb_str_cmp("xxx", "yyy").should == -1
+ end
+
+ it "returns 1 if the first string is longer than the second" do
+ @s.rb_str_cmp("xxxx", "xxx").should == 1
+ end
+
+ it "returns 1 if the first string is lexically greater than the second" do
+ @s.rb_str_cmp("yyy", "xxx").should == 1
+ end
+ end
+
+ describe "rb_str_split" do
+ it "splits strings over a splitter" do
+ @s.rb_str_split("Hello,Goodbye").should == ["Hello", "Goodbye"]
+ end
+ end
+
+ describe "rb_str2inum" do
+ it "converts a string to a number given a base" do
+ @s.rb_str2inum("10", 10).should == 10
+ @s.rb_str2inum("A", 16).should == 10
+ end
+ end
+
+ describe "rb_cstr2inum" do
+ it "converts a C string to a Fixnum given a base" do
+ @s.rb_cstr2inum("10", 10).should == 10
+ @s.rb_cstr2inum("10", 16).should == 16
+ end
+
+ it "converts a C string to a Bignum given a base" do
+ @s.rb_cstr2inum(bignum_value.to_s, 10).should == bignum_value
+ end
+
+ it "converts a C string to a Fixnum non-strictly if base is not 0" do
+ @s.rb_cstr2inum("1234a", 10).should == 1234
+ end
+
+ it "converts a C string to a Fixnum strictly if base is 0" do
+ lambda { @s.rb_cstr2inum("1234a", 0) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_cstr_to_inum" do
+ it "converts a C string to a Fixnum given a base" do
+ @s.rb_cstr_to_inum("1234", 10, true).should == 1234
+ end
+
+ it "converts a C string to a Bignum given a base" do
+ @s.rb_cstr_to_inum(bignum_value.to_s, 10, true).should == bignum_value
+ end
+
+ it "converts a C string to a Fixnum non-strictly" do
+ @s.rb_cstr_to_inum("1234a", 10, false).should == 1234
+ end
+
+ it "converts a C string to a Fixnum strictly" do
+ lambda { @s.rb_cstr_to_inum("1234a", 10, true) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_str_subseq" do
+ it "returns a byte-indexed substring" do
+ str = "\x00\x01\x02\x03\x04".force_encoding("binary")
+ @s.rb_str_subseq(str, 1, 2).should == "\x01\x02".force_encoding("binary")
+ end
+ end
+
+ describe "rb_str_substr" do
+ it "returns a substring" do
+ "hello".length.times do |time|
+ @s.rb_str_substr("hello", 0, time + 1).should == "hello"[0..time]
+ end
+ end
+ end
+
+ 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"
+ 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)
+ end
+ end
+
+ describe "RSTRING_PTR" do
+ it "returns a pointer to the string's contents" do
+ str = "abc"
+ chars = []
+ @s.RSTRING_PTR_iterate(str) do |c|
+ chars << c
+ end
+ chars.should == [97, 98, 99]
+ end
+
+ it "allows changing the characters in the string" do
+ str = "abc"
+ @s.RSTRING_PTR_assign(str, 65)
+ str.should == "AAA"
+ end
+
+ it "reflects changes after a rb_funcall" do
+ lamb = proc { |s| s.replace "NEW CONTENT" }
+
+ str = "beforebefore"
+
+ ret = @s.RSTRING_PTR_after_funcall(str, lamb)
+
+ str.should == "NEW CONTENT"
+ ret.should == str
+ end
+
+ it "returns a pointer to the contents of encoded pointer-sized string" do
+ s = "70パク".
+ encode(Encoding::UTF_16LE).
+ force_encoding(Encoding::UTF_16LE).
+ encode(Encoding::UTF_8)
+
+ chars = []
+ @s.RSTRING_PTR_iterate(s) do |c|
+ chars << c
+ end
+ chars.should == [55, 48, 227, 131, 145, 227, 130, 175]
+ end
+ end
+
+ describe "RSTRING_LEN" do
+ it "returns the size of the string" do
+ @s.RSTRING_LEN("gumdrops").should == 8
+ end
+ end
+
+ describe "RSTRING_LENINT" do
+ it "returns the size of a string" do
+ @s.RSTRING_LENINT("silly").should == 5
+ end
+ end
+
+ describe "StringValue" do
+ it "does not call #to_str on a String" do
+ str = "genuine"
+ str.should_not_receive(:to_str)
+ @s.StringValue(str)
+ end
+
+ it "does not call #to_s on a String" do
+ str = "genuine"
+ str.should_not_receive(:to_str)
+ @s.StringValue(str)
+ end
+
+ it "calls #to_str on non-String objects" do
+ str = mock("fake")
+ str.should_receive(:to_str).and_return("wannabe")
+ @s.StringValue(str)
+ end
+
+ it "does not call #to_s on non-String objects" do
+ str = mock("fake")
+ str.should_not_receive(:to_s)
+ lambda { @s.StringValue(str) }.should raise_error(TypeError)
+ end
+ end
+
+ describe "rb_str_resize" do
+ it "reduces the size of the string" do
+ str = @s.rb_str_resize("test", 2)
+ str.size.should == 2
+ @s.RSTRING_LEN(str).should == 2
+ str.should == "te"
+ end
+
+ it "updates the string's attributes visible in C code" do
+ @s.rb_str_resize_RSTRING_LEN("test", 2).should == 2
+ end
+
+ it "increases the size of the string" do
+ expected = "test".force_encoding("US-ASCII")
+ str = @s.rb_str_resize(expected.dup, 12)
+ str.size.should == 12
+ @s.RSTRING_LEN(str).should == 12
+ str[0, 4].should == expected
+ end
+ end
+
+ describe "rb_str_inspect" do
+ it "returns the equivalent of calling #inspect on the String" do
+ @s.rb_str_inspect("value").should == %["value"]
+ end
+ end
+
+ describe "rb_str_intern" do
+ it "returns a symbol created from the string" do
+ @s.rb_str_intern("symbol").should == :symbol
+ end
+
+ it "returns a symbol even if passed an empty string" do
+ @s.rb_str_intern("").should == "".to_sym
+ end
+
+ it "returns a symbol even if the passed string contains NULL characters" do
+ @s.rb_str_intern("no\0no").should == "no\0no".to_sym
+ end
+ end
+
+ describe "rb_str_freeze" do
+ it "freezes the string" do
+ s = ""
+ @s.rb_str_freeze(s).should == s
+ s.frozen?.should be_true
+ end
+ end
+
+ describe "rb_str_hash" do
+ it "hashes the string into a number" do
+ s = "hello"
+ @s.rb_str_hash(s).should be_kind_of(Integer)
+ end
+ end
+
+ describe "rb_str_update" do
+ it "splices the replacement string into the original at the given location" do
+ @s.rb_str_update("hello", 2, 3, "wuh").should == "hewuh"
+ end
+ end
+end
+
+describe "rb_str_free" do
+ # This spec only really exists to make sure the symbol
+ # is available. There is no guarantee this even does
+ # anything at all
+ it "indicates data for a string might be freed" do
+ @s.rb_str_free("xyz").should be_nil
+ end
+end
+
+describe :rb_external_str_new, shared: true do
+ it "returns a String in the default external encoding" do
+ Encoding.default_external = "UTF-8"
+ @s.send(@method, "abc").encoding.should == Encoding::UTF_8
+ end
+
+ it "returns an ASCII-8BIT encoded string if any non-ascii bytes are present and default external is US-ASCII" do
+ Encoding.default_external = "US-ASCII"
+ x80 = [0x80].pack('C')
+ @s.send(@method, "#{x80}abc").encoding.should == Encoding::ASCII_8BIT
+ end
+
+ it "returns a tainted String" do
+ @s.send(@method, "abc").tainted?.should be_true
+ end
+end
+
+describe "C-API String function" do
+ before :each do
+ @s = CApiStringSpecs.new
+ @external = Encoding.default_external
+ @internal = Encoding.default_internal
+ end
+
+ after :each do
+ Encoding.default_external = @external
+ Encoding.default_internal = @internal
+ end
+
+ describe "rb_str_length" do
+ it "returns the string's length" do
+ @s.rb_str_length("dewdrops").should == 8
+ end
+
+ it "counts characters in multi byte encodings" do
+ @s.rb_str_length("düwdrops").should == 8
+ end
+ end
+
+ describe "rb_str_equal" do
+ it "compares two same strings" do
+ s = "hello"
+ @s.rb_str_equal(s, "hello").should be_true
+ end
+
+ it "compares two different strings" do
+ s = "hello"
+ @s.rb_str_equal(s, "hella").should be_false
+ end
+ end
+
+ describe "rb_external_str_new" do
+ it_behaves_like :rb_external_str_new, :rb_external_str_new
+ end
+
+ describe "rb_external_str_new_cstr" do
+ it_behaves_like :rb_external_str_new, :rb_external_str_new_cstr
+ end
+
+ describe "rb_external_str_new_with_enc" do
+ it "returns a String in the specified encoding" do
+ s = @s.rb_external_str_new_with_enc("abc", 3, Encoding::UTF_8)
+ s.encoding.should == Encoding::UTF_8
+ end
+
+ it "returns an ASCII-8BIT encoded String if any non-ascii bytes are present and the specified encoding is US-ASCII" do
+ x80 = [0x80].pack('C')
+ s = @s.rb_external_str_new_with_enc("#{x80}abc", 4, Encoding::US_ASCII)
+ s.encoding.should == Encoding::ASCII_8BIT
+ end
+
+
+# it "transcodes a String to Encoding.default_internal if it is set" do
+# Encoding.default_internal = Encoding::EUC_JP
+#
+# - a = "\xE3\x81\x82\xe3\x82\x8c".force_encoding("utf-8")
+# + a = [0xE3, 0x81, 0x82, 0xe3, 0x82, 0x8c].pack('C6').force_encoding("utf-8")
+# s = @s.rb_external_str_new_with_enc(a, a.bytesize, Encoding::UTF_8)
+# -
+# - s.should == "\xA4\xA2\xA4\xEC".force_encoding("euc-jp")
+# + x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4')#.force_encoding('ascii-8bit')
+# + s.should == x
+# s.encoding.should equal(Encoding::EUC_JP)
+# end
+
+ it "transcodes a String to Encoding.default_internal if it is set" do
+ Encoding.default_internal = Encoding::EUC_JP
+
+ a = [0xE3, 0x81, 0x82, 0xe3, 0x82, 0x8c].pack('C6').force_encoding("utf-8")
+ s = @s.rb_external_str_new_with_enc(a, a.bytesize, Encoding::UTF_8)
+ x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('euc-jp')
+ s.should == x
+ s.encoding.should equal(Encoding::EUC_JP)
+ end
+
+ it "returns a tainted String" do
+ s = @s.rb_external_str_new_with_enc("abc", 3, Encoding::US_ASCII)
+ s.tainted?.should be_true
+ end
+ end
+
+ describe "rb_locale_str_new" do
+ it "returns a String with 'locale' encoding" do
+ s = @s.rb_locale_str_new("abc", 3)
+ s.should == "abc".force_encoding(Encoding.find("locale"))
+ s.encoding.should equal(Encoding.find("locale"))
+ end
+ end
+
+ describe "rb_locale_str_new_cstr" do
+ it "returns a String with 'locale' encoding" do
+ s = @s.rb_locale_str_new_cstr("abc")
+ s.should == "abc".force_encoding(Encoding.find("locale"))
+ s.encoding.should equal(Encoding.find("locale"))
+ end
+ end
+
+ describe "rb_str_conv_enc" do
+ it "returns the original String when to encoding is not specified" do
+ a = "abc".force_encoding("us-ascii")
+ @s.rb_str_conv_enc(a, Encoding::US_ASCII, nil).should equal(a)
+ end
+
+ it "returns the original String if a transcoding error occurs" do
+ a = [0xEE].pack('C').force_encoding("utf-8")
+ @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::EUC_JP).should equal(a)
+ end
+
+ it "returns a transcoded String" do
+ a = "\xE3\x81\x82\xE3\x82\x8C".force_encoding("utf-8")
+ result = @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::EUC_JP)
+ x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('utf-8')
+ result.should == x.force_encoding("euc-jp")
+ result.encoding.should equal(Encoding::EUC_JP)
+ end
+
+ describe "when the String encoding is equal to the destination encoding" do
+ it "returns the original String" do
+ a = "abc".force_encoding("us-ascii")
+ @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::US_ASCII).should equal(a)
+ end
+
+ it "returns the original String if the destination encoding is ASCII compatible and the String has no high bits set" do
+ a = "abc".encode("us-ascii")
+ @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::US_ASCII).should equal(a)
+ end
+
+ it "returns the origin String if the destination encoding is ASCII-8BIT" do
+ a = "abc".force_encoding("ascii-8bit")
+ @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::ASCII_8BIT).should equal(a)
+ end
+ end
+ end
+
+ describe "rb_str_conv_enc_opts" do
+ it "returns the original String when to encoding is not specified" do
+ a = "abc".force_encoding("us-ascii")
+ @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, nil, 0, nil).should equal(a)
+ end
+
+ it "returns the original String if a transcoding error occurs" do
+ a = [0xEE].pack('C').force_encoding("utf-8")
+ @s.rb_str_conv_enc_opts(a, Encoding::UTF_8,
+ Encoding::EUC_JP, 0, nil).should equal(a)
+ end
+
+ it "returns a transcoded String" do
+ a = "\xE3\x81\x82\xE3\x82\x8C".force_encoding("utf-8")
+ result = @s.rb_str_conv_enc_opts(a, Encoding::UTF_8, Encoding::EUC_JP, 0, nil)
+ x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('utf-8')
+ result.should == x.force_encoding("euc-jp")
+ result.encoding.should equal(Encoding::EUC_JP)
+ end
+
+ describe "when the String encoding is equal to the destination encoding" do
+ it "returns the original String" do
+ a = "abc".force_encoding("us-ascii")
+ @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII,
+ Encoding::US_ASCII, 0, nil).should equal(a)
+ end
+
+ it "returns the original String if the destination encoding is ASCII compatible and the String has no high bits set" do
+ a = "abc".encode("us-ascii")
+ @s.rb_str_conv_enc_opts(a, Encoding::UTF_8,
+ Encoding::US_ASCII, 0, nil).should equal(a)
+ end
+
+ it "returns the origin String if the destination encoding is ASCII-8BIT" do
+ a = "abc".force_encoding("ascii-8bit")
+ @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII,
+ Encoding::ASCII_8BIT, 0, nil).should equal(a)
+ end
+ end
+ end
+
+ describe "rb_str_export" do
+ it "returns the original String with the external encoding" do
+ Encoding.default_external = Encoding::ISO_8859_1
+ s = @s.rb_str_export("Hëllo")
+ s.encoding.should equal(Encoding::ISO_8859_1)
+ end
+ end
+
+ describe "rb_str_export_locale" do
+ it "returns the original String with the locale encoding" do
+ s = @s.rb_str_export_locale("abc")
+ s.should == "abc".force_encoding(Encoding.find("locale"))
+ s.encoding.should equal(Encoding.find("locale"))
+ end
+ end
+
+ describe "rb_sprintf" do
+ it "replaces the parts like sprintf" do
+ @s.rb_sprintf1("Awesome %s is replaced", "string").should == "Awesome string is replaced"
+ @s.rb_sprintf1("%s", "TestFoobarTest").should == "TestFoobarTest"
+ end
+
+ it "accepts multiple arguments" do
+ s = "Awesome %s is here with %s"
+ @s.rb_sprintf2(s, "string", "content").should == "Awesome string is here with content"
+ end
+ end
+
+ describe "rb_vsprintf" do
+ it "returns a formatted String from a variable number of arguments" do
+ s = @s.rb_vsprintf("%s, %d, %.2f", "abc", 42, 2.7);
+ s.should == "abc, 42, 2.70"
+ end
+ end
+
+ describe "rb_String" do
+ it "returns the passed argument if it is a string" do
+ @s.rb_String("a").should == "a"
+ end
+
+ it "tries to convert the passed argument to a string by calling #to_str first" do
+ @s.rb_String(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)
+ end
+
+ it "tries to convert the passed argument to a string by calling #to_s" do
+ @s.rb_String({"bar" => "foo"}).should == '{"bar"=>"foo"}'
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/struct_spec.rb b/spec/ruby/optional/capi/struct_spec.rb
new file mode 100644
index 0000000000..9a0eafeb7b
--- /dev/null
+++ b/spec/ruby/optional/capi/struct_spec.rb
@@ -0,0 +1,209 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("struct")
+
+describe "C-API Struct function" do
+ before :each do
+ @s = CApiStructSpecs.new
+ @struct = @s.rb_struct_define("CAPIStruct", "a", "b", "c")
+ end
+
+ after :each do
+ Struct.send(:remove_const, :CAPIStruct)
+ end
+
+ describe "rb_struct_define" do
+ it "creates accessors for the struct members" do
+ instance = @struct.new
+ instance.a = 1
+ instance.b = 2
+ instance.c = 3
+ instance.a.should == 1
+ instance.b.should == 2
+ instance.c.should == 3
+ end
+
+ it "has a value of nil for the member of a newly created instance" do
+ # Verify that attributes are on an instance basis
+ Struct::CAPIStruct.new.b.should be_nil
+ end
+
+ it "creates a constant scoped under Struct for the named Struct" do
+ Struct.should have_constant(:CAPIStruct)
+ end
+
+ it "returns the member names as Symbols" do
+ @struct.members.should == [:a, :b, :c]
+ end
+ end
+end
+
+describe "C-API Struct function" do
+ before :each do
+ @s = CApiStructSpecs.new
+ @struct = @s.rb_struct_define(nil, "a", "b", "c")
+ end
+
+ describe "rb_struct_define for an anonymous struct" do
+ it "creates accessors for the struct members" do
+ instance = @struct.new
+ instance.a = 1
+ instance.b = 2
+ instance.c = 3
+ instance.a.should == 1
+ instance.b.should == 2
+ instance.c.should == 3
+ end
+
+ it "returns the member names as Symbols" do
+ @struct.members.should == [:a, :b, :c]
+ end
+ end
+end
+
+describe "C-API Struct function" do
+ before :each do
+ @s = CApiStructSpecs.new
+ @struct = @s.rb_struct_define_under(CApiStructSpecs, "CAPIStruct", "a", "b", "c")
+ end
+
+ describe "rb_struct_define_under" do
+ it "creates accessors for the struct members" do
+ instance = @struct.new
+ instance.a = 1
+ instance.b = 2
+ instance.c = 3
+ instance.a.should == 1
+ instance.b.should == 2
+ instance.c.should == 3
+ end
+
+ it "has a value of nil for the member of a newly created instance" do
+ # Verify that attributes are on an instance basis
+ CApiStructSpecs::CAPIStruct.new.b.should be_nil
+ end
+
+ it "creates a constant scoped under the namespace of the given class" do
+ CApiStructSpecs.should have_constant(:CAPIStruct)
+ end
+
+ it "returns the member names as Symbols" do
+ @struct.members.should == [:a, :b, :c]
+ end
+ end
+end
+
+describe "C-API Struct function" do
+ before :each do
+ @s = CApiStructSpecs.new
+ @klass = Struct.new(:a, :b, :c)
+ @struct = @klass.new
+ end
+
+ describe "rb_struct_define" do
+ it "raises an ArgumentError if arguments contain duplicate member name" do
+ lambda { @s.rb_struct_define(nil, "a", "b", "a") }.should raise_error(ArgumentError)
+ end
+
+ it "raises a NameError if an invalid constant name is given" do
+ lambda { @s.rb_struct_define("foo", "a", "b", "c") }.should raise_error(NameError)
+ end
+ end
+
+ describe "rb_struct_aref" do
+ it "returns the value of a struct member with a symbol key" do
+ @struct[:a] = 2
+ @s.rb_struct_aref(@struct, :a).should == 2
+ end
+
+ it "returns the value of a struct member with a string key" do
+ @struct[:b] = 2
+ @s.rb_struct_aref(@struct, "b").should == 2
+ end
+
+ it "returns the value of a struct member by index" do
+ @struct[:c] = 3
+ @s.rb_struct_aref(@struct, 2).should == 3
+ end
+
+ it "raises a NameError if the struct member does not exist" do
+ lambda { @s.rb_struct_aref(@struct, :d) }.should raise_error(NameError)
+ end
+
+ it "raises an IndexError if the given index is out of range" do
+ lambda { @s.rb_struct_aref(@struct, -4) }.should raise_error(IndexError)
+ lambda { @s.rb_struct_aref(@struct, 3) }.should raise_error(IndexError)
+ end
+ end
+
+ describe "rb_struct_getmember" do
+ it "returns the value of a struct member" do
+ @struct[:a] = 2
+ @s.rb_struct_getmember(@struct, :a).should == 2
+ end
+
+ it "raises a NameError if the struct member does not exist" do
+ lambda { @s.rb_struct_getmember(@struct, :d) }.should raise_error(NameError)
+ end
+ end
+
+ describe "rb_struct_s_members" do
+ it "returns the struct members as an array of symbols" do
+ @s.rb_struct_s_members(@klass).should == [:a, :b, :c]
+ end
+ end
+
+ describe "rb_struct_members" do
+ it "returns the struct members as an array of symbols" do
+ @s.rb_struct_members(@struct).should == [:a, :b, :c]
+ end
+ end
+
+ describe "rb_struct_aset" do
+ it "sets the value of a struct member with a symbol key" do
+ @s.rb_struct_aset(@struct, :a, 1)
+ @struct[:a].should == 1
+ end
+
+ it "sets the value of a struct member with a string key" do
+ @s.rb_struct_aset(@struct, "b", 1)
+ @struct[:b].should == 1
+ end
+
+ it "sets the value of a struct member by index" do
+ @s.rb_struct_aset(@struct, 2, 1)
+ @struct[:c].should == 1
+ end
+
+ it "raises a NameError if the struct member does not exist" do
+ lambda { @s.rb_struct_aset(@struct, :d, 1) }.should raise_error(NameError)
+ end
+
+ it "raises an IndexError if the given index is out of range" do
+ lambda { @s.rb_struct_aset(@struct, -4, 1) }.should raise_error(IndexError)
+ lambda { @s.rb_struct_aset(@struct, 3, 1) }.should raise_error(IndexError)
+ end
+
+ it "raises a RuntimeError if the struct is frozen" do
+ @struct.freeze
+ lambda { @s.rb_struct_aset(@struct, :a, 1) }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "rb_struct_new" do
+ it "creates a new instance of a struct" do
+ i = @s.rb_struct_new(@klass, 1, 2, 3)
+ i.a.should == 1
+ i.b.should == 2
+ i.c.should == 3
+ end
+ end
+
+ ruby_version_is "2.4" do
+ describe "rb_struct_size" do
+ it "returns the number of struct members" do
+ @s.rb_struct_size(@struct).should == 3
+ end
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/symbol_spec.rb b/spec/ruby/optional/capi/symbol_spec.rb
new file mode 100644
index 0000000000..b6532f4a4e
--- /dev/null
+++ b/spec/ruby/optional/capi/symbol_spec.rb
@@ -0,0 +1,133 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension('symbol')
+
+describe "C-API Symbol function" do
+ before :each do
+ @s = CApiSymbolSpecs.new
+ end
+
+ describe "rb_intern" do
+ it "converts a string to a symbol, uniquely" do
+ @s.rb_intern("test_symbol").should == :test_symbol
+ @s.rb_intern_c_compare("test_symbol", :test_symbol).should == true
+ end
+ end
+
+ describe "rb_intern2" do
+ it "converts a string to a symbol, uniquely, for a string of given length" do
+ @s.rb_intern2("test_symbol", 4).should == :test
+ @s.rb_intern2_c_compare("test_symbol", 4, :test).should == true
+ end
+ end
+
+ describe "rb_intern3" do
+ it "converts a multibyte symbol with the encoding" do
+ sym = @s.rb_intern3("Ω", 2, Encoding::UTF_8)
+ sym.encoding.should == Encoding::UTF_8
+ sym.should == :Ω
+ @s.rb_intern3_c_compare("Ω", 2, Encoding::UTF_8, :Ω).should == true
+ end
+
+ it "converts an ascii compatible symbol with the ascii encoding" do
+ sym = @s.rb_intern3("foo", 3, Encoding::UTF_8)
+ sym.encoding.should == Encoding::US_ASCII
+ sym.should == :foo
+ end
+
+ it "should respect the symbol encoding via rb_intern3" do
+ :Ω.to_s.encoding.should == Encoding::UTF_8
+ end
+ end
+
+ describe "rb_intern_const" do
+ it "converts a string to a Symbol" do
+ @s.rb_intern_const("test").should == :test
+ end
+ end
+
+ describe "rb_id2name" do
+ it "converts a symbol to a C char array" do
+ @s.rb_id2name(:test_symbol).should == "test_symbol"
+ end
+ end
+
+ describe "rb_id2str" do
+ it "converts a symbol to a Ruby string" do
+ @s.rb_id2str(:test_symbol).should == "test_symbol"
+ end
+
+ it "creates a string with the same encoding as the symbol" do
+ str = "test_symbol".encode(Encoding::UTF_16LE)
+ @s.rb_id2str(str.to_sym).encoding.should == Encoding::UTF_16LE
+ end
+ end
+
+ describe "rb_intern_str" do
+ it "converts a Ruby String to a Symbol" do
+ str = "test_symbol"
+ @s.rb_intern_str(str).should == :test_symbol
+ end
+ end
+
+ describe "rb_is_const_id" do
+ it "returns true given a const-like symbol" do
+ @s.rb_is_const_id(:Foo).should == true
+ end
+
+ it "returns false given an ivar-like symbol" do
+ @s.rb_is_const_id(:@foo).should == false
+ end
+
+ it "returns false given a cvar-like symbol" do
+ @s.rb_is_const_id(:@@foo).should == false
+ end
+
+ it "returns false given an undecorated symbol" do
+ @s.rb_is_const_id(:foo).should == false
+ end
+ end
+
+ describe "rb_is_instance_id" do
+ it "returns false given a const-like symbol" do
+ @s.rb_is_instance_id(:Foo).should == false
+ end
+
+ it "returns true given an ivar-like symbol" do
+ @s.rb_is_instance_id(:@foo).should == true
+ end
+
+ it "returns false given a cvar-like symbol" do
+ @s.rb_is_instance_id(:@@foo).should == false
+ end
+
+ it "returns false given an undecorated symbol" do
+ @s.rb_is_instance_id(:foo).should == false
+ end
+ end
+
+ describe "rb_is_class_id" do
+ it "returns false given a const-like symbol" do
+ @s.rb_is_class_id(:Foo).should == false
+ end
+
+ it "returns false given an ivar-like symbol" do
+ @s.rb_is_class_id(:@foo).should == false
+ end
+
+ it "returns true given a cvar-like symbol" do
+ @s.rb_is_class_id(:@@foo).should == true
+ end
+
+ it "returns false given an undecorated symbol" do
+ @s.rb_is_class_id(:foo).should == false
+ end
+ end
+
+ describe "rb_sym2str" do
+ it "converts a Symbol to a String" do
+ @s.rb_sym2str(:bacon).should == "bacon"
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/thread_spec.rb b/spec/ruby/optional/capi/thread_spec.rb
new file mode 100644
index 0000000000..0ca701e35d
--- /dev/null
+++ b/spec/ruby/optional/capi/thread_spec.rb
@@ -0,0 +1,124 @@
+require File.expand_path('../spec_helper', __FILE__)
+require File.expand_path('../../../core/thread/shared/wakeup', __FILE__)
+
+load_extension("thread")
+
+class Thread
+ def self.capi_thread_specs=(t)
+ @@capi_thread_specs = t
+ end
+
+ def call_capi_rb_thread_wakeup
+ @@capi_thread_specs.rb_thread_wakeup(self)
+ end
+end
+
+describe "C-API Thread function" do
+ before :each do
+ @t = CApiThreadSpecs.new
+ ScratchPad.clear
+ Thread.capi_thread_specs = @t
+ end
+
+ describe "rb_thread_wait_for" do
+ it "sleeps the current thread for the give ammount of time" do
+ start = Time.now
+ @t.rb_thread_wait_for(0, 100_000)
+ (Time.now - start).should be_close(0.1, 0.2)
+ end
+ end
+
+ describe "rb_thread_alone" do
+ it "returns true if there is only one thread" do
+ pred = Thread.list.size == 1
+ @t.rb_thread_alone.should == pred
+ end
+ end
+
+ describe "rb_thread_current" do
+ it "equals Thread.current" do
+ @t.rb_thread_current.should == Thread.current
+ end
+ end
+
+ describe "rb_thread_local_aref" do
+ it "returns the value of a thread-local variable" do
+ thr = Thread.current
+ sym = :thread_capi_specs_aref
+ thr[sym] = 1
+ @t.rb_thread_local_aref(thr, sym).should == 1
+ end
+
+ it "returns nil if the value has not been set" do
+ @t.rb_thread_local_aref(Thread.current, :thread_capi_specs_undefined).should be_nil
+ end
+ end
+
+ describe "rb_thread_local_aset" do
+ it "sets the value of a thread-local variable" do
+ thr = Thread.current
+ sym = :thread_capi_specs_aset
+ @t.rb_thread_local_aset(thr, sym, 2).should == 2
+ thr[sym].should == 2
+ end
+ end
+
+ describe "rb_thread_wakeup" do
+ it_behaves_like :thread_wakeup, :call_capi_rb_thread_wakeup
+ end
+
+ describe "rb_thread_create" do
+ it "creates a new thread" do
+ obj = Object.new
+ proc = lambda { |x| ScratchPad.record x }
+ thr = @t.rb_thread_create(proc, obj)
+ thr.should be_kind_of(Thread)
+ thr.join
+ ScratchPad.recorded.should == obj
+ end
+
+ it "handles throwing an exception in the thread" do
+ proc = lambda { |x| raise "my error" }
+ thr = @t.rb_thread_create(proc, nil)
+ thr.should be_kind_of(Thread)
+
+ lambda {
+ thr.join
+ }.should raise_error(RuntimeError, "my error")
+ end
+
+ it "sets the thread's group" do
+ thr = @t.rb_thread_create(lambda { |x| }, nil)
+ begin
+ thread_group = thr.group
+ thread_group.should be_an_instance_of(ThreadGroup)
+ ensure
+ thr.join
+ end
+ end
+ end
+
+ describe "rb_thread_call_without_gvl" do
+ it "runs a C function with the global lock unlocked" do
+ thr = Thread.new do
+ @t.rb_thread_call_without_gvl
+ end
+
+ # Wait until it's blocking...
+ Thread.pass while thr.status and thr.status != "sleep"
+
+ # The thread status is set to sleep by rb_thread_call_without_gvl(),
+ # but the thread might not be in the blocking read(2) yet, so wait a bit.
+ sleep 0.1
+
+ # Wake it up, causing the unblock function to be run.
+ thr.wakeup
+
+ # Make sure it stopped
+ thr.join(1).should_not be_nil
+
+ # And we got a proper value
+ thr.value.should be_true
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/time_spec.rb b/spec/ruby/optional/capi/time_spec.rb
new file mode 100644
index 0000000000..c8f0d9d4e0
--- /dev/null
+++ b/spec/ruby/optional/capi/time_spec.rb
@@ -0,0 +1,302 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("time")
+
+describe "CApiTimeSpecs" do
+ before :each do
+ @s = CApiTimeSpecs.new
+ end
+
+ describe "rb_time_new" do
+ it "creates a Time from the sec and usec" do
+ usec = CAPI_SIZEOF_LONG == 8 ? 4611686018427387903 : 1413123123
+ @s.rb_time_new(1232141421, usec).should == Time.at(1232141421, usec)
+ end
+ end
+
+ describe "TIMET2NUM" do
+ it "returns an Integer" do
+ @s.TIMET2NUM.should be_kind_of(Integer)
+ end
+ end
+
+ describe "rb_time_nano_new" do
+ it "creates a Time from the sec and usec" do
+ time = @s.rb_time_nano_new(1232141421, 1413123123)
+ time.to_i.should == 1232141422
+ time.nsec.should == 413123123
+ end
+ end
+
+ describe "rb_time_num_new" do
+ it "creates a Time in the local zone with only a timestamp" do
+ with_timezone("Europe/Amsterdam") do
+ time = @s.rb_time_num_new(1232141421, nil)
+ time.should be_an_instance_of(Time)
+ time.to_i.should == 1232141421
+ platform_is_not :windows do
+ time.gmt_offset.should == 3600
+ end
+ end
+ end
+
+ it "creates a Time with the given offset" do
+ with_timezone("Europe/Amsterdam") do
+ time = @s.rb_time_num_new(1232141421, 7200)
+ time.should be_an_instance_of(Time)
+ time.to_i.should == 1232141421
+ time.gmt_offset.should == 7200
+ end
+ end
+
+ it "creates a Time with a Float timestamp" do
+ with_timezone("Europe/Amsterdam") do
+ time = @s.rb_time_num_new(1.5, 7200)
+ time.should be_an_instance_of(Time)
+ time.to_i.should == 1
+ time.nsec.should == 500000000
+ time.gmt_offset.should == 7200
+ end
+ end
+
+ it "creates a Time with a Rational timestamp" do
+ with_timezone("Europe/Amsterdam") do
+ time = @s.rb_time_num_new(Rational(3, 2), 7200)
+ time.should be_an_instance_of(Time)
+ time.to_i.should == 1
+ time.nsec.should == 500000000
+ time.gmt_offset.should == 7200
+ end
+ end
+ end
+
+ describe "rb_time_interval" do
+ it "creates a timeval interval for a Fixnum" do
+ sec, usec = @s.rb_time_interval(1232141421)
+ sec.should be_kind_of(Integer)
+ sec.should == 1232141421
+ usec.should be_kind_of(Integer)
+ usec.should == 0
+ end
+
+ it "creates a timeval interval for a Float" do
+ sec, usec = @s.rb_time_interval(1.5)
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ it "creates a timeval interval for a Rational" do
+ sec, usec = @s.rb_time_interval(Rational(3, 2))
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ it "throws an argument error for a negative value" do
+ lambda { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError)
+ lambda { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError)
+ lambda { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError)
+ end
+
+ end
+
+ describe "rb_time_interval" do
+ it "creates a timeval interval for a Fixnum" do
+ sec, usec = @s.rb_time_interval(1232141421)
+ sec.should be_kind_of(Integer)
+ sec.should == 1232141421
+ usec.should be_kind_of(Integer)
+ usec.should == 0
+ end
+
+ it "creates a timeval interval for a Float" do
+ sec, usec = @s.rb_time_interval(1.5)
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ it "creates a timeval interval for a Rational" do
+ sec, usec = @s.rb_time_interval(Rational(3, 2))
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ it "throws an argument error for a negative value" do
+ lambda { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError)
+ lambda { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError)
+ lambda { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError)
+ end
+
+ it "throws an argument error when given a Time instance" do
+ lambda { @s.rb_time_interval(Time.now) }.should raise_error(TypeError)
+ end
+
+ end
+
+ describe "rb_time_timeval" do
+ it "creates a timeval for a Fixnum" do
+ sec, usec = @s.rb_time_timeval(1232141421)
+ sec.should be_kind_of(Integer)
+ sec.should == 1232141421
+ usec.should be_kind_of(Integer)
+ usec.should == 0
+ end
+
+ it "creates a timeval for a Float" do
+ sec, usec = @s.rb_time_timeval(1.5)
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ it "creates a timeval for a Rational" do
+ sec, usec = @s.rb_time_timeval(Rational(3, 2))
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ platform_is_not :mingw32 do
+ it "creates a timeval for a negative Fixnum" do
+ sec, usec = @s.rb_time_timeval(-1232141421)
+ sec.should be_kind_of(Integer)
+ sec.should == -1232141421
+ usec.should be_kind_of(Integer)
+ usec.should == 0
+ end
+
+ it "creates a timeval for a negative Float" do
+ sec, usec = @s.rb_time_timeval(-1.5)
+ sec.should be_kind_of(Integer)
+ sec.should == -2
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+
+ it "creates a timeval for a negative Rational" do
+ sec, usec = @s.rb_time_timeval(Rational(-3, 2))
+ sec.should be_kind_of(Integer)
+ sec.should == -2
+ usec.should be_kind_of(Integer)
+ usec.should == 500000
+ end
+ end
+
+ it "creates a timeval from a Time object" do
+ t = Time.now
+ sec, usec = @s.rb_time_timeval(t)
+ sec.should == t.to_i
+ usec.should == t.nsec.div(1000)
+ end
+ end
+
+ describe "rb_time_timespec" do
+ it "creates a timespec for a Fixnum" do
+ sec, nsec = @s.rb_time_timespec(1232141421)
+ sec.should be_kind_of(Integer)
+ sec.should == 1232141421
+ nsec.should be_kind_of(Integer)
+ nsec.should == 0
+ end
+
+ it "creates a timespec for a Float" do
+ sec, nsec = @s.rb_time_timespec(1.5)
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ nsec.should be_kind_of(Integer)
+ nsec.should == 500000000
+ end
+
+ it "creates a timespec for a Rational" do
+ sec, nsec = @s.rb_time_timespec(Rational(3, 2))
+ sec.should be_kind_of(Integer)
+ sec.should == 1
+ nsec.should be_kind_of(Integer)
+ nsec.should == 500000000
+ end
+
+ platform_is_not :mingw32 do
+ it "creates a timespec for a negative Fixnum" do
+ sec, nsec = @s.rb_time_timespec(-1232141421)
+ sec.should be_kind_of(Integer)
+ sec.should == -1232141421
+ nsec.should be_kind_of(Integer)
+ nsec.should == 0
+ end
+
+ it "creates a timespec for a negative Float" do
+ sec, nsec = @s.rb_time_timespec(-1.5)
+ sec.should be_kind_of(Integer)
+ sec.should == -2
+ nsec.should be_kind_of(Integer)
+ nsec.should == 500000000
+ end
+
+ it "creates a timespec for a negative Rational" do
+ sec, nsec = @s.rb_time_timespec(Rational(-3, 2))
+ sec.should be_kind_of(Integer)
+ sec.should == -2
+ nsec.should be_kind_of(Integer)
+ nsec.should == 500000000
+ end
+ end
+
+ it "creates a timespec from a Time object" do
+ t = Time.now
+ sec, nsec = @s.rb_time_timespec(t)
+ sec.should == t.to_i
+ nsec.should == t.nsec
+ end
+ end
+
+ ruby_version_is "2.3" do
+ describe "rb_time_timespec_new" do
+ it "returns a time object with the given timespec and UTC offset" do
+ @s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400)
+ end
+
+ describe "when offset given is within range of -86400 and 86400 (exclusive)" do
+ it "sets time's is_gmt to false" do
+ @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false
+ end
+
+ it "sets time's offset to the offset given" do
+ @s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399
+ end
+ end
+
+ it "returns time object in UTC if offset given equals INT_MAX - 1" do
+ @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true
+ end
+
+ it "returns time object in localtime if offset given equals INT_MAX" do
+ @s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime
+ t = Time.now
+ @s.rb_time_timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff.should == t.gmtoff
+ end
+
+ it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do
+ lambda { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError)
+ lambda { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "rb_timespec_now" do
+ it "fills a struct timespec with the current time" do
+ now = Time.now
+ time = @s.rb_time_from_timespec(now.utc_offset)
+ time.should be_an_instance_of(Time)
+ (time - now).should be_close(0, 10)
+ end
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/typed_data_spec.rb b/spec/ruby/optional/capi/typed_data_spec.rb
new file mode 100644
index 0000000000..1b2852d349
--- /dev/null
+++ b/spec/ruby/optional/capi/typed_data_spec.rb
@@ -0,0 +1,56 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension("typed_data")
+
+describe "CApiAllocTypedSpecs (a class with an alloc func defined)" do
+ it "calls the alloc func" do
+ @s = CApiAllocTypedSpecs.new
+ @s.typed_wrapped_data.should == 42 # not defined in initialize
+ end
+end
+
+describe "CApiWrappedTypedStruct" do
+ before :each do
+ @s = CApiWrappedTypedStructSpecs.new
+ end
+
+ it "wraps and unwraps data" do
+ a = @s.typed_wrap_struct(1024)
+ @s.typed_get_struct(a).should == 1024
+ end
+
+ it "throws an exception for a wrong type" do
+ a = @s.typed_wrap_struct(1024)
+ lambda { @s.typed_get_struct_other(a) }.should raise_error(TypeError)
+ end
+
+ it "unwraps data for a parent type" do
+ a = @s.typed_wrap_struct(1024)
+ @s.typed_get_struct_parent(a).should == 1024
+ end
+
+ it "allows for using NULL as the klass for Data_Wrap_Struct" do
+ a = @s.typed_wrap_struct_null(1024)
+ @s.typed_get_struct(a).should == 1024
+ end
+
+ describe "RTYPEDATA" do
+ it "returns the struct data" do
+ a = @s.typed_wrap_struct(1024)
+ @s.typed_get_struct_rdata(a).should == 1024
+ end
+
+ it "can be used to change the wrapped struct" do
+ a = @s.typed_wrap_struct(1024)
+ @s.typed_change_struct(a, 100)
+ @s.typed_get_struct(a).should == 100
+ end
+ end
+
+ describe "DATA_PTR" do
+ it "returns the struct data" do
+ a = @s.typed_wrap_struct(1024)
+ @s.typed_get_struct_data_ptr(a).should == 1024
+ end
+ end
+end
diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb
new file mode 100644
index 0000000000..c8d0401cbc
--- /dev/null
+++ b/spec/ruby/optional/capi/util_spec.rb
@@ -0,0 +1,191 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+load_extension('util')
+
+describe "C-API Util function" do
+ before :each do
+ @o = CApiUtilSpecs.new
+ end
+
+ describe "rb_scan_args" do
+ before :each do
+ @prc = lambda { 1 }
+ @acc = []
+ ScratchPad.record @acc
+ end
+
+ it "assigns the required arguments scanned" do
+ @o.rb_scan_args([1, 2], "2", 2, @acc).should == 2
+ ScratchPad.recorded.should == [1, 2]
+ end
+
+ it "raises an ArgumentError if there are insufficient arguments" do
+ lambda { @o.rb_scan_args([1, 2], "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]
+ end
+
+ it "assigns the optional arguments scanned" do
+ @o.rb_scan_args([1, 2], "02", 2, @acc).should == 2
+ ScratchPad.recorded.should == [1, 2]
+ end
+
+ it "assigns nil for optional arguments that are not present" do
+ @o.rb_scan_args([1], "03", 3, @acc).should == 1
+ ScratchPad.recorded.should == [1, nil, nil]
+ end
+
+ it "assigns the required and optional arguments and splats the rest" do
+ @o.rb_scan_args([1, 2, 3, 4], "11*", 3, @acc).should == 4
+ ScratchPad.recorded.should == [1, 2, [3, 4]]
+ end
+
+ it "assigns the required and optional arguments and and empty Array when there are no arguments to splat" do
+ @o.rb_scan_args([1, 2], "11*", 3, @acc).should == 2
+ ScratchPad.recorded.should == [1, 2, []]
+ end
+
+ it "assigns required, optional arguments scanned and the passed block" do
+ @o.rb_scan_args([1, 2], "11&", 3, @acc, &@prc).should == 2
+ ScratchPad.recorded.should == [1, 2, @prc]
+ end
+
+ it "assigns required, optional, splatted arguments scanned and the passed block" do
+ @o.rb_scan_args([1, 2, 3, 4], "11*&", 4, @acc, &@prc).should == 4
+ ScratchPad.recorded.should == [1, 2, [3, 4], @prc]
+ end
+
+ it "assigns required arguments, nil for missing optional arguments and the passed block" do
+ @o.rb_scan_args([1], "12&", 4, @acc, &@prc).should == 1
+ ScratchPad.recorded.should == [1, nil, nil, @prc]
+ end
+
+ it "assigns required, splatted arguments and the passed block" do
+ @o.rb_scan_args([1, 2, 3], "1*&", 3, @acc, &@prc).should == 3
+ ScratchPad.recorded.should == [1, [2, 3], @prc]
+ end
+
+ it "assigns post-splat arguments" do
+ @o.rb_scan_args([1, 2, 3], "00*1", 2, @acc).should == 3
+ ScratchPad.recorded.should == [[1, 2], 3]
+ end
+
+ it "assigns required, optional, splat and post-splat arguments" do
+ @o.rb_scan_args([1, 2, 3, 4, 5], "11*1", 4, @acc).should == 5
+ ScratchPad.recorded.should == [1, 2, [3, 4], 5]
+ end
+
+ it "assigns required, splat, post-splat arguments" do
+ @o.rb_scan_args([1, 2, 3, 4], "10*1", 3, @acc).should == 4
+ ScratchPad.recorded.should == [1, [2, 3], 4]
+ end
+
+ it "assigns optional, splat, post-splat arguments" do
+ @o.rb_scan_args([1, 2, 3, 4], "01*1", 3, @acc).should == 4
+ ScratchPad.recorded.should == [1, [2, 3], 4]
+ end
+
+ it "assigns required, optional, splat, post-splat and block arguments" do
+ @o.rb_scan_args([1, 2, 3, 4, 5], "11*1&", 5, @acc, &@prc).should == 5
+ ScratchPad.recorded.should == [1, 2, [3, 4], 5, @prc]
+ end
+
+ it "assigns Hash arguments" do
+ h = {a: 1, b: 2}
+ @o.rb_scan_args([h], "0:", 1, @acc).should == 0
+ ScratchPad.recorded.should == [h]
+ end
+
+ it "assigns required and Hash arguments" do
+ h = {a: 1, b: 2}
+ @o.rb_scan_args([1, h], "1:", 2, @acc).should == 1
+ ScratchPad.recorded.should == [1, h]
+ end
+
+ it "assigns required, optional, splat, post-splat, Hash and block arguments" do
+ h = {a: 1, b: 2}
+ @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 5
+ ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc]
+ end
+
+ # r43934
+ it "rejects non-keyword arguments" do
+ h = {1 => 2, 3 => 4}
+ lambda {
+ @o.rb_scan_args([h], "0:", 1, @acc)
+ }.should raise_error(ArgumentError)
+ ScratchPad.recorded.should == []
+ end
+
+ it "rejects required and non-keyword arguments" do
+ h = {1 => 2, 3 => 4}
+ lambda {
+ @o.rb_scan_args([1, h], "1:", 2, @acc)
+ }.should raise_error(ArgumentError)
+ ScratchPad.recorded.should == []
+ end
+
+ it "considers the hash as a post argument when there is a splat" do
+ h = {1 => 2, 3 => 4}
+ @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 6
+ ScratchPad.recorded.should == [1, 2, [3, 4, 5], h, nil, @prc]
+ end
+ end
+
+ platform_is wordsize: 64 do
+ describe "rb_long2int" do
+ it "raises a RangeError if the value is outside the range of a C int" do
+ lambda { @o.rb_long2int(0xffff_ffff_ffff) }.should raise_error(RangeError)
+ end
+ end
+
+ it "returns the C int value" do
+ @o.rb_long2int(1234).should == 1234
+ end
+ end
+
+ # #7896
+ describe "rb_iter_break" do
+ before :each do
+ ScratchPad.record []
+ end
+
+ it "breaks a loop" do
+ 3.times do |i|
+ if i == 2
+ @o.rb_iter_break
+ end
+ ScratchPad << i
+ end
+ ScratchPad.recorded.should == [0, 1]
+ end
+
+ it "breaks the inner loop" do
+ 3.times do |i|
+ 3.times do |j|
+ if i == 1
+ @o.rb_iter_break
+ end
+ ScratchPad << [i, j]
+ end
+ end
+ ScratchPad.recorded.should == [[0, 0], [0, 1], [0, 2], [2, 0], [2, 1], [2, 2]]
+ end
+ end
+
+ describe "rb_sourcefile" do
+ it "returns the current ruby file" do
+ @o.rb_sourcefile.should == __FILE__
+ end
+ end
+
+ describe "rb_sourceline" do
+ it "returns the current ruby file" do
+ @o.rb_sourceline.should be_kind_of(Fixnum)
+ end
+ end
+
+end