summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWayne Meissner <wmeissner@gmail.com>2012-11-11 13:48:38 +1000
committerWayne Meissner <wmeissner@gmail.com>2012-11-11 13:48:38 +1000
commite189bdc82dcd53d279bc8271b1f805302691ad08 (patch)
treeedb3a38a4509363425bfc369de66379154de1173
parent439b18603518b549f5a247a0c760fd6915c78ac4 (diff)
downloadffi-e189bdc82dcd53d279bc8271b1f805302691ad08.tar.gz
Add spec for zero-length arrays in structs
-rw-r--r--spec/ffi/struct_spec.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/ffi/struct_spec.rb b/spec/ffi/struct_spec.rb
index f98f80d..e83d1c0 100644
--- a/spec/ffi/struct_spec.rb
+++ b/spec/ffi/struct_spec.rb
@@ -717,3 +717,43 @@ describe "Struct allocation" do
end
end
+
+describe "variable-length arrays" do
+ it "zero length array should be accepted as last field" do
+ lambda {
+ Class.new(FFI::Struct) do
+ layout :count, :int, :data, [ :char, 0 ]
+ end
+ }.should_not raise_error
+ end
+
+ it "zero length array before last element should raise error" do
+ lambda {
+ Class.new(FFI::Struct) do
+ layout :data, [ :char, 0 ], :count, :int
+ end
+ }.should raise_error
+ end
+
+ it "can access elements of array" do
+ struct_class = Class.new(FFI::Struct) do
+ layout :count, :int, :data, [ :long, 0 ]
+ end
+ s = struct_class.new(FFI::MemoryPointer.new(1024))
+ s[:data][0] = 0xdeadbeef
+ s[:data][1] = 0xfee1dead
+ s[:data][0].should eq 0xdeadbeef
+ s[:data][1].should eq 0xfee1dead
+ end
+
+ it "non-variable length array is bounds checked" do
+ struct_class = Class.new(FFI::Struct) do
+ layout :count, :int, :data, [ :long, 1 ]
+ end
+ s = struct_class.new(FFI::MemoryPointer.new(1024))
+ s[:data][0] = 0xdeadbeef
+ lambda { s[:data][1] = 0xfee1dead }.should raise_error
+ s[:data][0].should eq 0xdeadbeef
+ lambda { s[:data][1].should eq 0xfee1dead }.should raise_error
+ end
+end