Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer#write_array_of_type did not work #637

Merged
merged 2 commits into from Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/ffi/pointer.rb
Expand Up @@ -124,10 +124,9 @@ def read_array_of_type(type, reader, length)
# ptr.write_array_of_type(TYPE_UINT8, :put_uint8, [1, 2, 3 ,4])
def write_array_of_type(type, writer, ary)
size = FFI.type_size(type)
tmp = self
ary.each_with_index {|i, j|
tmp.send(writer, i)
tmp += size unless j == ary.length-1 # avoid OOB
ary.each_with_index { |val, i|
break unless i < self.size
self.send(writer, i * size, val)
}
self
end
Expand Down
21 changes: 20 additions & 1 deletion spec/ffi/pointer_spec.rb
Expand Up @@ -94,7 +94,26 @@ def to_ptr
expect(array[j].address).to eq(address)
end
end


it "#write_array_of_type for uint8" do
values = [10, 227, 32]
memory = FFI::MemoryPointer.new FFI::TYPE_UINT8, values.size
memory.write_array_of_type(FFI::TYPE_UINT8, :put_uint8, values)
array = memory.read_array_of_type(FFI::TYPE_UINT8, :read_uint8, values.size)
values.each_with_index do |val, j|
expect(array[j]).to eq(val)
end
end

it "#write_array_of_type for uint32" do
values = [10, 227, 32]
memory = FFI::MemoryPointer.new FFI::TYPE_UINT32, values.size
memory.write_array_of_type(FFI::TYPE_UINT32, :put_uint32, values)
array = memory.read_array_of_type(FFI::TYPE_UINT32, :read_uint32, values.size)
values.each_with_index do |val, j|
expect(array[j]).to eq(val)
end
end
end

describe 'NULL' do
Expand Down