ruby-changes:23799
From: drbrain <ko1@a...>
Date: Thu, 31 May 2012 09:27:48 +0900 (JST)
Subject: [ruby-changes:23799] drbrain:r35850 (trunk): * ext/dl/lib/dl/struct.rb (DL::CStructEntity::size): Refactored ::size
drbrain 2012-05-31 09:27:34 +0900 (Thu, 31 May 2012) New Revision: 35850 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35850 Log: * 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 Added files: trunk/test/dl/test_c_struct_entry.rb Modified files: trunk/ChangeLog trunk/ext/dl/lib/dl/struct.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35849) +++ ChangeLog (revision 35850) @@ -1,3 +1,9 @@ +Thu May 31 09:27:06 2012 Eric Hodel <drbrain@s...> + + * 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 <drbrain@s...> * ext/dl/lib/dl/struct.rb (DL::CUnionEntity#set_ctypes): Refactored Index: ext/dl/lib/dl/struct.rb =================================================================== --- ext/dl/lib/dl/struct.rb (revision 35849) +++ ext/dl/lib/dl/struct.rb (revision 35850) @@ -93,26 +93,18 @@ # => 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 Index: test/dl/test_c_struct_entry.rb =================================================================== --- test/dl/test_c_struct_entry.rb (revision 0) +++ test/dl/test_c_struct_entry.rb (revision 35850) @@ -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 + Property changes on: test/dl/test_c_struct_entry.rb ___________________________________________________________________ Added: svn:eol-style + LF -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/