Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix ffi buffer #1412

Closed
wants to merge 3 commits into from

3 participants

@tduehr

add buffer_spec for FFI and fix FFI::Buffer so the tests pass.

tduehr added some commits
@tduehr tduehr add MRI ffi specs
last failing currently
63c67d3
@tduehr tduehr this *should* work
but... doesn't
4e97891
@headius

After uncommenting these lines, I was able to run buffer_spec.rb to completion.

maybe I was somehow picking up my jruby-1.7.10 install then.

@headius
Owner

Merged by hand to jruby-1_7 and I'll merge to master now.

@headius headius closed this
@enebo enebo added this to the JRuby 1.7.11 milestone
@tduehr tduehr deleted the tduehr:fix_ffi_buffer branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 17, 2014
  1. @tduehr

    add MRI ffi specs

    tduehr authored
    last failing currently
  2. @tduehr

    this *should* work

    tduehr authored
    but... doesn't
  3. @tduehr

    fixup! this *should* work

    tduehr authored
This page is out of date. Refresh to see the latest.
View
20 core/src/main/java/org/jruby/ext/ffi/Buffer.java
@@ -76,32 +76,36 @@ private static Buffer allocate(ThreadContext context, IRubyObject recv,
allocateMemoryIO(context.runtime, total), total, typeSize, flags);
}
- private IRubyObject init(ThreadContext context, IRubyObject rbTypeSize, int count, int flags) {
+ private IRubyObject init(ThreadContext context, IRubyObject rbTypeSize, int count, int flags, Block block) {
this.typeSize = calculateTypeSize(context, rbTypeSize);
this.size = this.typeSize * count;
this.inout = flags;
setMemoryIO(allocateMemoryIO(context.runtime, (int) this.size));
+ if (block.isGiven()) {
+ block.yield(context, this);
+ }
+
return this;
}
@JRubyMethod(name = "initialize", visibility = PRIVATE)
- public IRubyObject initialize(ThreadContext context, IRubyObject sizeArg) {
+ public IRubyObject initialize(ThreadContext context, IRubyObject sizeArg, Block block) {
return sizeArg instanceof RubyFixnum
? init(context, RubyFixnum.one(context.runtime),
- RubyFixnum.fix2int(sizeArg), IN | OUT)
- : init(context, sizeArg, 1, IN | OUT);
+ RubyFixnum.fix2int(sizeArg), (IN | OUT), block)
+ : init(context, sizeArg, 1, (IN | OUT), block);
}
@JRubyMethod(name = "initialize", visibility = PRIVATE)
- public IRubyObject initialize(ThreadContext context, IRubyObject sizeArg, IRubyObject arg2) {
- return init(context, sizeArg, getCount(arg2), IN | OUT);
+ public IRubyObject initialize(ThreadContext context, IRubyObject sizeArg, IRubyObject arg2, Block block) {
+ return init(context, sizeArg, getCount(arg2), (IN | OUT), block);
}
@JRubyMethod(name = "initialize", visibility = PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject sizeArg,
- IRubyObject countArg, IRubyObject clearArg) {
- return init(context, sizeArg, RubyFixnum.fix2int(countArg), IN | OUT);
+ IRubyObject countArg, IRubyObject clearArg, Block block) {
+ return init(context, sizeArg, RubyFixnum.fix2int(countArg), (IN | OUT), block);
}
/**
View
171 spec/ffi/buffer_spec.rb
@@ -1,23 +1,22 @@
-require 'ffi'
-MemoryPointer = FFI::MemoryPointer
-Buffer = FFI::Buffer
-Platform = FFI::Platform
-LongSize = FFI::Platform::LONG_SIZE / 8
+#
+# This file is part of ruby-ffi.
+# For licensing, see LICENSE.SPECS
+#
describe "Buffer#total" do
[1,2,3].each do |i|
- { :char => 1, :uchar => 1, :short => 2, :ushort => 2, :int => 4, :uint => 4, \
- :long => LongSize, :ulong => LongSize, :long_long => 8, :ulong_long => 8, \
- :float => 4, :double => 8
+ { :char => 1, :uchar => 1, :short => 2, :ushort => 2, :int => 4,
+ :uint => 4, :long => FFI::Type::LONG.size, :ulong => FFI::Type::ULONG.size,
+ :long_long => 8, :ulong_long => 8, :float => 4, :double => 8
}.each_pair do |t, s|
it "Buffer.alloc_in(#{t}, #{i}).total == #{i * s}" do
- expect(Buffer.alloc_in(t, i).total).to eq(i * s)
+ FFI::Buffer.alloc_in(t, i).total.should == i * s
end
it "Buffer.alloc_out(#{t}, #{i}).total == #{i * s}" do
- expect(Buffer.alloc_out(t, i).total).to eq(i * s)
+ FFI::Buffer.alloc_out(t, i).total.should == i * s
end
it "Buffer.alloc_inout(#{t}, #{i}).total == #{i * s}" do
- expect(Buffer.alloc_inout(t, i).total).to eq(i * s)
+ FFI::Buffer.alloc_inout(t, i).total.should == i * s
end
end
end
@@ -28,7 +27,7 @@
(0..127).each do |i|
(0..bufsize-1).each do |offset|
it "put_char(#{offset}, #{i}).get_char(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_char(offset, i).get_char(offset)).to eq(i)
+ FFI::Buffer.alloc_in(bufsize).put_char(offset, i).get_char(offset).should == i
end
end
end
@@ -38,7 +37,7 @@
(0..255).each do |i|
(0..bufsize-1).each do |offset|
it "Buffer.put_uchar(#{offset}, #{i}).get_uchar(#{offset}) == #{i}" do
- Buffer.alloc_in(bufsize).put_uchar(offset, i).get_uchar(offset).should == i
+ FFI::Buffer.alloc_in(bufsize).put_uchar(offset, i).get_uchar(offset).should == i
end
end
end
@@ -48,7 +47,7 @@
[0, 1, 128, 32767].each do |i|
(0..bufsize-2).each do |offset|
it "put_short(#{offset}, #{i}).get_short(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_short(offset, i).get_short(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_short(offset, i).get_short(offset).should == i
end
end
end
@@ -58,7 +57,7 @@
[ 0, 1, 128, 32767, 65535, 0xfee1, 0xdead, 0xbeef, 0xcafe ].each do |i|
(0..bufsize-2).each do |offset|
it "put_ushort(#{offset}, #{i}).get_ushort(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_ushort(offset, i).get_ushort(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_ushort(offset, i).get_ushort(offset).should == i
end
end
end
@@ -68,7 +67,7 @@
[0, 1, 128, 32767, 0x7ffffff ].each do |i|
(0..bufsize-4).each do |offset|
it "put_int(#{offset}, #{i}).get_int(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_int(offset, i).get_int(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_int(offset, i).get_int(offset).should == i
end
end
end
@@ -78,7 +77,7 @@
[ 0, 1, 128, 32767, 65535, 0xfee1dead, 0xcafebabe, 0xffffffff ].each do |i|
(0..bufsize-4).each do |offset|
it "put_uint(#{offset}, #{i}).get_uint(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_uint(offset, i).get_uint(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_uint(offset, i).get_uint(offset).should == i
end
end
end
@@ -86,9 +85,9 @@
describe "Buffer#put_long" do
bufsize = 16
[0, 1, 128, 32767, 0x7ffffff ].each do |i|
- (0..bufsize-LongSize).each do |offset|
+ (0..bufsize-FFI::Type::LONG.size).each do |offset|
it "put_long(#{offset}, #{i}).get_long(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_long(offset, i).get_long(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_long(offset, i).get_long(offset).should == i
end
end
end
@@ -96,9 +95,9 @@
describe "Buffer#put_ulong" do
bufsize = 16
[ 0, 1, 128, 32767, 65535, 0xfee1dead, 0xcafebabe, 0xffffffff ].each do |i|
- (0..bufsize-LongSize).each do |offset|
+ (0..bufsize-FFI::Type::LONG.size).each do |offset|
it "put_ulong(#{offset}, #{i}).get_ulong(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_ulong(offset, i).get_ulong(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_ulong(offset, i).get_ulong(offset).should == i
end
end
end
@@ -108,7 +107,7 @@
[0, 1, 128, 32767, 0x7ffffffffffffff ].each do |i|
(0..bufsize-8).each do |offset|
it "put_long_long(#{offset}, #{i}).get_long_long(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_long_long(offset, i).get_long_long(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_long_long(offset, i).get_long_long(offset).should == i
end
end
end
@@ -118,19 +117,133 @@
[ 0, 1, 128, 32767, 65535, 0xdeadcafebabe, 0x7fffffffffffffff ].each do |i|
(0..bufsize-8).each do |offset|
it "put_ulong_long(#{offset}, #{i}).get_ulong_long(#{offset}) == #{i}" do
- expect(Buffer.alloc_in(bufsize).put_ulong_long(offset, i).get_ulong_long(offset)).to eq i
+ FFI::Buffer.alloc_in(bufsize).put_ulong_long(offset, i).get_ulong_long(offset).should == i
end
end
end
end
+describe "Reading/Writing binary strings" do
+ it "Buffer#put_bytes" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.put_bytes(0, str);
+ s2 = buf.get_bytes(0, 11);
+ s2.should == str
+ end
+ it "Buffer#put_bytes with index and length" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.put_bytes(0, str, 5, 6);
+ s2 = buf.get_bytes(0, 6);
+ s2.should == str[5..-1]
+ end
+ it "Buffer#put_bytes with only index" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.put_bytes(0, str, 5);
+ s2 = buf.get_bytes(0, 6);
+ s2.should == str[5..-1]
+ end
+ it "Buffer#put_bytes with index > str.length" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ lambda { buf.put_bytes(0, str, 12); }.should raise_error
+ end
+ it "Buffer#put_bytes with length > str.length" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ lambda { buf.put_bytes(0, str, 0, 12); }.should raise_error
+ end
+ it "Buffer#put_bytes with negative index" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ lambda { buf.put_bytes(0, str, -1, 12); }.should raise_error
+ end
+
+ it "Buffer#write_bytes" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.write_bytes(str)
+ s2 = buf.get_bytes(0, 11)
+ s2.should == str
+ end
+ it "Buffer#write_bytes with index and length" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.write_bytes(str, 5, 6)
+ s2 = buf.get_bytes(0, 6)
+ s2.should == str[5..-1]
+ end
+ it "Buffer#write_bytes with only index" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.write_bytes(str, 5)
+ s2 = buf.get_bytes(0, 6)
+ s2.should == str[5..-1]
+ end
+ it "Buffer#write_bytes with index > str.length" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ lambda { buf.write_bytes(str, 12) }.should raise_error
+ end
+ it "Buffer#put_bytes with length > str.length" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ lambda { buf.put_bytes(0, str, 0, 12) }.should raise_error
+ end
+ it "Buffer#write_bytes with negative index" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ lambda { buf.write_bytes(str, -1, 12) }.should raise_error
+ end
+end
+describe "Reading/Writing ascii strings" do
+ it "Buffer#put_string with string containing zero byte" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.put_string(0, str);
+ s2 = buf.get_bytes(0, 11);
+ s2.should == str
+ end
+ it "Buffer#get_string with string containing zero byte" do
+ str = "hello\0world"
+ buf = FFI::Buffer.new 1024
+ buf.put_bytes(0, str);
+ s2 = buf.get_string(0, 11);
+ s2.should == "hello"
+ end
+ it "Buffer#put_string without length should NUL terminate" do
+ str = "hello"
+ buf = FFI::Buffer.new 1024
+ buf.put_string(0, str);
+ s2 = buf.get_bytes(0, 6);
+ s2.should == "hello\0"
+ end
+end
describe "Buffer#put_pointer" do
it "put_pointer(0, p).get_pointer(0) == p" do
- p = MemoryPointer.new :ulong_long
+ p = FFI::MemoryPointer.new :ulong_long
p.put_uint(0, 0xdeadbeef)
- buf = Buffer.alloc_inout 8
+ buf = FFI::Buffer.alloc_inout 8
p2 = buf.put_pointer(0, p).get_pointer(0)
- expect(p2).to_not be_nil
- expect(p2).to eq p
- expect(p2.get_uint(0)).to eq 0xdeadbeef
+ p2.should_not be_nil
+ p2.should == p
+ p2.get_uint(0).should == 0xdeadbeef
+ end
+end
+describe "Buffer#size" do
+ it "should return size" do
+ buf = FFI::Buffer.new 14
+ buf.size.should == 14
+ end
+end
+
+describe "Buffer#initialize" do
+ it "with block should execute block" do
+ block_executed = false
+ FFI::Buffer.new(:pointer) do |ptr|
+ block_executed = true
+ end
+ block_executed.should be_true
end
-end
+end
Something went wrong with that request. Please try again.