From fe962cde158562e0d41b61ea48267cf0413f190c Mon Sep 17 00:00:00 2001 From: drbrain Date: Thu, 31 May 2012 22:07:09 +0000 Subject: [PATCH] * ext/dl/lib/dl/struct.rb (DL::CStructEntity#set_ctypes): Refactored #set_ctypes using newer ruby features to simplify its implementation. * test/dl/test_c_struct_entry.rb (class DL): Test to verify refactoring. Reviewed by Aaron Patterson. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35857 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ ext/dl/lib/dl/struct.rb | 31 ++++++++++++------------------- test/dl/test_c_struct_entry.rb | 12 ++++++++++++ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 528b38b78a..f6dba16801 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri Jun 1 06:57:10 2012 Eric Hodel + + * ext/dl/lib/dl/struct.rb (DL::CStructEntity#set_ctypes): Refactored + #set_ctypes using newer ruby features to simplify its implementation. + * test/dl/test_c_struct_entry.rb (class DL): Test to verify + refactoring. + Fri Jun 1 06:40:25 2012 Eric Hodel * object.c (Init_Object): Restored Kernel documentation based on diff --git a/ext/dl/lib/dl/struct.rb b/ext/dl/lib/dl/struct.rb index e6f7ba710b..37cb3b5120 100644 --- a/ext/dl/lib/dl/struct.rb +++ b/ext/dl/lib/dl/struct.rb @@ -127,27 +127,20 @@ module DL @ctypes = types @offset = [] offset = 0 - max_align = 0 - types.each_with_index{|t,i| + + max_align = types.map { |type, count = 1| orig_offset = offset - if( t.is_a?(Array) ) - align = ALIGN_MAP[t[0]] - else - align = ALIGN_MAP[t] - end + align = ALIGN_MAP[type] offset = PackInfo.align(orig_offset, align) - @offset[i] = offset - if( t.is_a?(Array) ) - offset += (SIZE_MAP[t[0]] * t[1]) - else - offset += SIZE_MAP[t] - end - if (max_align < align) - max_align = align - end - } - offset = PackInfo.align(offset, max_align) - @size = offset + + @offset << offset + + offset += (SIZE_MAP[type] * count) + + align + }.max + + @size = PackInfo.align(offset, max_align) end # Fetch struct member +name+ diff --git a/test/dl/test_c_struct_entry.rb b/test/dl/test_c_struct_entry.rb index b83cde85e0..aa49a99572 100644 --- a/test/dl/test_c_struct_entry.rb +++ b/test/dl/test_c_struct_entry.rb @@ -37,5 +37,17 @@ class DL::TestCStructEntity < DL::TestBase assert_equal expected, size end + + def test_set_ctypes + union = DL::CStructEntity.malloc [DL::TYPE_INT, DL::TYPE_LONG] + union.assign_names %w[int long] + + # this test is roundabout because the stored ctypes are not accessible + union['long'] = 1 + union['int'] = 2 + + assert_equal 1, union['long'] + assert_equal 2, union['int'] + end end