stop including padding

use it as a helper module
This commit is contained in:
Torsten Ruger 2016-12-31 20:08:33 +02:00
parent dccd097fef
commit 86dafccb04
7 changed files with 11 additions and 21 deletions

View File

@ -9,7 +9,6 @@ module Register
# functions on the objects, but now it has gone to a visitor pattern. # functions on the objects, but now it has gone to a visitor pattern.
class Assembler class Assembler
include Padding
include Logging include Logging
log_level :info log_level :info
@ -261,7 +260,7 @@ module Register
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary # pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
def pad_after length def pad_after length
before = stream_position before = stream_position
pad = padding_for(length) - 4 # four is for the MARKER we write pad = Padding.padding_for(length) - 4 # four is for the MARKER we write
pad.times do pad.times do
@stream.write_unsigned_int_8(0) @stream.write_unsigned_int_8(0)
end end

View File

@ -5,17 +5,17 @@ module Padding
# objects only come in lengths of multiple of 8 words / 32 bytes # objects only come in lengths of multiple of 8 words / 32 bytes
# and there is a "hidden" 1 word that is used for debug/check memory corruption # and there is a "hidden" 1 word that is used for debug/check memory corruption
def padded len def self.padded( len )
a = 32 * (1 + (len + 3)/32 ) a = 32 * (1 + (len + 3)/32 )
#puts "#{a} for #{len}" #puts "#{a} for #{len}"
a a
end end
def padded_words words def self.padded_words( words )
padded(words*4) # 4 == word length, a constant waiting for a home padded(words*4) # 4 == word length, a constant waiting for a home
end end
def padding_for length def self.padding_for( length )
pad = padded(length) - length # for header, type pad = padded(length) - length # for header, type
pad pad
end end

View File

@ -160,7 +160,7 @@ module Parfait
# but additionally, the amount of data comes on top. # but additionally, the amount of data comes on top.
# unfortuntely we can't just use super because of the Padding # unfortuntely we can't just use super because of the Padding
def padded_length def padded_length
padded_words( get_type().instance_length + get_length() ) Padding.padded_words( get_type().instance_length + get_length() )
end end
def each def each

View File

@ -30,7 +30,6 @@ module Parfait
object object
end end
include Padding
include Positioned include Positioned
# 1 -based index # 1 -based index
@ -109,7 +108,7 @@ module Parfait
end end
def padded_length def padded_length
padded_words( @type.instance_length ) Padding.padded_words( @type.instance_length )
end end
# parfait versions are deliberately called different, so we "relay" # parfait versions are deliberately called different, so we "relay"

View File

@ -1,7 +1,6 @@
class Symbol class Symbol
include Positioned include Positioned
include Padding
def has_type? def has_type?
true true
@ -12,7 +11,7 @@ class Symbol
l l
end end
def padded_length def padded_length
padded to_s.length + 4 Padding.padded( to_s.length + 4)
end end
end end

View File

@ -196,7 +196,7 @@ module Parfait
end end
def padded_length def padded_length
padded( 4 * get_type().instance_length + @char_length ) Padding.padded( 4 * get_type().instance_length + @char_length )
end end
private private

View File

@ -1,27 +1,20 @@
require_relative "../helper" require_relative "../helper"
class Padded
include Padding
end
class TestPadding < MiniTest::Test class TestPadding < MiniTest::Test
def setup
@pad = Padded.new
end
def test_small def test_small
[6,27,28].each do |p| [6,27,28].each do |p|
assert_equal 32 , @pad.padded(p) , "Expecting 32 for #{p}" assert_equal 32 , Padding.padded(p) , "Expecting 32 for #{p}"
end end
end end
def test_medium def test_medium
[29,33,40,57,60].each do |p| [29,33,40,57,60].each do |p|
assert_equal 64 , @pad.padded(p) , "Expecting 64 for #{p}" assert_equal 64 , Padding.padded(p) , "Expecting 64 for #{p}"
end end
end end
def test_large def test_large
[61,65,88].each do |p| [61,65,88].each do |p|
assert_equal 96 , @pad.padded(p) , "Expecting 96 for #{p}" assert_equal 96 , Padding.padded(p) , "Expecting 96 for #{p}"
end end
end end
end end