ruby-changes:23798
From: drbrain <ko1@a...>
Date: Thu, 31 May 2012 08:41:18 +0900 (JST)
Subject: [ruby-changes:23798] drbrain:r35849 (trunk): * ext/dl/lib/dl/struct.rb (DL::CUnionEntity#set_ctypes): Refactored
drbrain 2012-05-31 08:41:07 +0900 (Thu, 31 May 2012) New Revision: 35849 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35849 Log: * ext/dl/lib/dl/struct.rb (DL::CUnionEntity#set_ctypes): Refactored #set_types to reuse DL::CUnionEntity::size * test/dl/test_c_union_entity.rb: Added test Reviewed by Aaron Paterson Modified files: trunk/ChangeLog trunk/ext/dl/lib/dl/struct.rb trunk/test/dl/test_c_union_entity.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35848) +++ ChangeLog (revision 35849) @@ -1,3 +1,9 @@ +Thu May 31 08:40:34 2012 Eric Hodel <drbrain@s...> + + * ext/dl/lib/dl/struct.rb (DL::CUnionEntity#set_ctypes): Refactored + #set_types to reuse DL::CUnionEntity::size + * test/dl/test_c_union_entity.rb: Added test + Thu May 31 08:20:14 2012 Eric Hodel <drbrain@s...> * ext/dl/lib/dl/struct.rb (DL::CUnionEntity::size): Fixed ::size to Index: ext/dl/lib/dl/struct.rb =================================================================== --- ext/dl/lib/dl/struct.rb (revision 35848) +++ ext/dl/lib/dl/struct.rb (revision 35849) @@ -242,19 +242,8 @@ # Given +types+, calculate the necessary offset and for each union member def set_ctypes(types) @ctypes = types - @offset = [] - @size = 0 - types.each_with_index{|t,i| - @offset[i] = 0 - if( t.is_a?(Array) ) - size = SIZE_MAP[t[0]] * t[1] - else - size = SIZE_MAP[t] - end - if( size > @size ) - @size = size - end - } + @offset = Array.new(types.length, 0) + @size = self.class.size types end end end Index: test/dl/test_c_union_entity.rb =================================================================== --- test/dl/test_c_union_entity.rb (revision 35848) +++ test/dl/test_c_union_entity.rb (revision 35849) @@ -14,5 +14,17 @@ assert_equal DL::SIZEOF_CHAR * 20, size end + + def test_set_ctypes + union = DL::CUnionEntity.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 + assert_equal 1, union['long'] + + union['int'] = 1 + assert_equal 1, union['int'] + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/