Skip to content

Commit

Permalink
Merge pull request #637 from ytaka/ytaka
Browse files Browse the repository at this point in the history
Pointer#write_array_of_type did not work
  • Loading branch information
larskanis committed Jan 6, 2019
2 parents 09ddbbd + ec46ced commit fa65b25
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit fa65b25

Please sign in to comment.