Skip to content

Commit

Permalink
Add FFI specs for writing to array and char array fields
Browse files Browse the repository at this point in the history
* Note that the last 2 specs used to fail before the previous commit.
  • Loading branch information
eregon committed Mar 28, 2020
1 parent c0a446a commit 50eb96c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions spec/ffi/struct_spec.rb
Expand Up @@ -786,6 +786,62 @@ class StructWithArray < FFI::Struct
@s = LibTest::StructWithArray.new(LibTest.struct_make_struct_with_array(0, 1, 2, 3, 4))
expect(@s[:a].to_ptr).to eq(LibTest::struct_field_array(@s.to_ptr))
end

it 'raises when trying to set an array field' do
@s = LibTest::StructWithArray.new(LibTest.struct_make_struct_with_array(0, 1, 2, 3, 4))
expect { @s[:a] = [7, 8, 9, 10, 11] }.to raise_error(NotImplementedError, 'cannot set array field')
end
end

describe FFI::Struct, ' with a char array field' do
module LibTest
class StructWithCharArray < FFI::Struct
layout :before, :int32, :str, [:char, 4], :after, :int32
end
end

before do
@s = LibTest::StructWithCharArray.new
@s.pointer.write_bytes([0, 1, 2, 3, 4, 0].pack("lC4l"))
end

it 'should read values from memory' do
expect(@s[:before]).to eq(0)
expect(@s[:str].to_a).to eq([1, 2, 3, 4])
expect(@s[:str].to_s).to eq("\x01\x02\x03\x04".b)
expect(@s[:after]).to eq(0)
end

it 'should return the number of elements in the array field' do
expect(@s[:str].size).to eq(4)
end

it 'should allow iteration through the array elements' do
@s[:str].each_with_index { |elem, i| expect(elem).to eq(i+1) }
end

it 'should return the pointer to the array' do
expect(@s[:str].to_ptr).to eq(@s.to_ptr + 4)
end

it 'allows writing a shorter String to the char array' do
@s[:str] = "abc"
expect(@s[:before]).to eq(0)
expect(@s[:str].to_s).to eq("abc")
expect(@s[:after]).to eq(0)
end

it 'allows writing a String of the same size to the char array' do
@s[:after] = -1
@s[:str] = "abcd"
expect(@s[:before]).to eq(0)
expect(@s[:str].to_s).to eq("abcd")
expect(@s[:after]).to eq(-1) # The above should not write to the next field
end

it 'raises when writing a longer String to the char array' do
expect { @s[:str] = "abcdefg" }.to raise_error(IndexError)
end
end

describe 'BuggedStruct' do
Expand Down

0 comments on commit 50eb96c

Please sign in to comment.