From 40cf3e0cca0a347bf8b6b44832fdcc358e068630 Mon Sep 17 00:00:00 2001 From: drbrain Date: Thu, 31 May 2012 00:27:34 +0000 Subject: [PATCH] * ext/dl/lib/dl/struct.rb (DL::CStructEntity::size): Refactored ::size to remove unused variables and simplify using newer ruby features. * test/dl/test_c_struct_entry.rb: Test to validate refactoring Reviewed by Aaron Patterson git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35850 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 +++++ ext/dl/lib/dl/struct.rb | 32 +++++++++-------------- test/dl/test_c_struct_entry.rb | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 test/dl/test_c_struct_entry.rb diff --git a/ChangeLog b/ChangeLog index 8ff6bf47d4..ec5fe8161d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Thu May 31 09:27:06 2012 Eric Hodel + + * ext/dl/lib/dl/struct.rb (DL::CStructEntity::size): Refactored ::size + to remove unused variables and simplify using newer ruby features. + * test/dl/test_c_struct_entry.rb: Test to validate refactoring + Thu May 31 08:40:34 2012 Eric Hodel * ext/dl/lib/dl/struct.rb (DL::CUnionEntity#set_ctypes): Refactored diff --git a/ext/dl/lib/dl/struct.rb b/ext/dl/lib/dl/struct.rb index 628adb21c2..e6f7ba710b 100644 --- a/ext/dl/lib/dl/struct.rb +++ b/ext/dl/lib/dl/struct.rb @@ -93,26 +93,18 @@ module DL # => 24 def CStructEntity.size(types) offset = 0 - max_align = 0 - types.each_with_index{|t,i| - orig_offset = offset - if( t.is_a?(Array) ) - align = PackInfo::ALIGN_MAP[t[0]] - offset = PackInfo.align(orig_offset, align) - size = offset - orig_offset - offset += (PackInfo::SIZE_MAP[t[0]] * t[1]) - else - align = PackInfo::ALIGN_MAP[t] - offset = PackInfo.align(orig_offset, align) - size = offset - orig_offset - offset += PackInfo::SIZE_MAP[t] - end - if (max_align < align) - max_align = align - end - } - offset = PackInfo.align(offset, max_align) - offset + + max_align = types.map { |type, count = 1| + last_offset = offset + + align = PackInfo::ALIGN_MAP[type] + offset = PackInfo.align(last_offset, align) + + (PackInfo::SIZE_MAP[type] * count) + + align + }.max + + PackInfo.align(offset, max_align) end # Wraps the C pointer +addr+ as a C struct with the given +types+. The C diff --git a/test/dl/test_c_struct_entry.rb b/test/dl/test_c_struct_entry.rb new file mode 100644 index 0000000000..3df6c368b8 --- /dev/null +++ b/test/dl/test_c_struct_entry.rb @@ -0,0 +1,46 @@ +require_relative 'test_base' + +require 'dl/cparser' + +class DL::TestCStructEntity < DL::TestBase + def test_class_size + types = [DL::TYPE_DOUBLE, DL::TYPE_CHAR] + + size = DL::CStructEntity.size types + + alignments = types.map { |type| DL::PackInfo::ALIGN_MAP[type] } + + expected = DL::PackInfo.align 0, alignments[0] + expected += DL::PackInfo::SIZE_MAP[DL::TYPE_DOUBLE] + + expected = DL::PackInfo.align expected, alignments[1] + expected += DL::PackInfo::SIZE_MAP[DL::TYPE_CHAR] + + expected = DL::PackInfo.align expected, alignments.max + + assert_equal expected, size + end + + def test_class_size_with_count + size = DL::CStructEntity.size([[DL::TYPE_DOUBLE, 2], [DL::TYPE_CHAR, 20]]) + + assert_equal DL::TYPE_DOUBLE * 2 + DL::SIZEOF_CHAR * 20, size + end + def test_class_size_with_count + size = DL::CStructEntity.size([[DL::TYPE_DOUBLE, 2], [DL::TYPE_CHAR, 20]]) + + types = [DL::TYPE_DOUBLE, DL::TYPE_CHAR] + alignments = types.map { |type| DL::PackInfo::ALIGN_MAP[type] } + + expected = DL::PackInfo.align 0, alignments[0] + expected += DL::PackInfo::SIZE_MAP[DL::TYPE_DOUBLE] * 2 + + expected = DL::PackInfo.align expected, alignments[1] + expected += DL::PackInfo::SIZE_MAP[DL::TYPE_CHAR] * 20 + + expected = DL::PackInfo.align expected, alignments.max + + assert_equal expected, size + end +end +