From 77cc13943cd9ec908d75133d7ac0b89c9ab57deb Mon Sep 17 00:00:00 2001 From: sinisterchipmunk Date: Wed, 22 Jan 2020 20:37:23 -0500 Subject: [ruby/fiddle] Fix assignment to array within struct (#26) * Allow access to a struct's underlying memory with `struct[offset, length]`. https://github.com/ruby/fiddle/commit/24083690a6 --- test/fiddle/test_import.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/fiddle') diff --git a/test/fiddle/test_import.rb b/test/fiddle/test_import.rb index 778327b4b2..867f6960ee 100644 --- a/test/fiddle/test_import.rb +++ b/test/fiddle/test_import.rb @@ -129,6 +129,20 @@ module Fiddle assert_equal([0,1,2], ary.value) end + def test_struct_array_assignment() + instance = Fiddle::Importer.struct(["unsigned int stages[3]"]).malloc + instance.stages[0] = 1024 + instance.stages[1] = 10 + instance.stages[2] = 100 + assert_equal 1024, instance.stages[0] + assert_equal 10, instance.stages[1] + assert_equal 100, instance.stages[2] + assert_equal [1024, 10, 100].pack(Fiddle::PackInfo::PACK_MAP[-Fiddle::TYPE_INT] * 3), + instance.to_ptr[0, 3 * Fiddle::SIZEOF_INT] + assert_raise(IndexError) { instance.stages[-1] = 5 } + assert_raise(IndexError) { instance.stages[3] = 5 } + end + def test_struct() s = LIBC::MyStruct.malloc() s.num = [0,1,2,3,4] -- cgit v1.2.3