From 3244c7d633a9993eb8ca503be4e2191882517881 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 5 May 2018 20:11:08 +0300 Subject: [PATCH] add position tests and refactor padding out --- lib/elf/object_writer.rb | 6 ++-- lib/risc/position.rb | 24 +++++++++++++--- test/risc/test_padding.rb | 53 ++++++++++++++++++++++++++++-------- test/risc/test_position.rb | 37 +++++++++++++++++++++++++ test/risc/test_positioned.rb | 42 ---------------------------- 5 files changed, 101 insertions(+), 61 deletions(-) create mode 100644 test/risc/test_position.rb delete mode 100644 test/risc/test_positioned.rb diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index a782f5f7..f497fc4d 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -37,12 +37,12 @@ module Elf if( slot.respond_to? :sof_reference_name ) label = "#{slot.sof_reference_name}" else - label = "#{slot.class.name}::#{Position.position(slot).to_s(16)}" + label = "#{slot.class.name}::#{Risc::Position.position(slot).to_s(16)}" end label += "=#{slot}" if slot.is_a?(Symbol) or slot.is_a?(String) - add_symbol label , Position.position(slot) + add_symbol label , Risc::Position.position(slot) if slot.is_a?(Parfait::TypedMethod) - add_symbol slot.name.to_s , Position.position(slot.binary) + add_symbol slot.name.to_s , Risc::Position.position(slot.binary) end end end diff --git a/lib/risc/position.rb b/lib/risc/position.rb index 83ff108b..37110863 100644 --- a/lib/risc/position.rb +++ b/lib/risc/position.rb @@ -17,6 +17,23 @@ module Risc class Position @positions = {} + attr_reader :at + + def initialize( at ) + @at = at + raise "not int #{self}-#{at}" unless @at.is_a?(Integer) + end + + def +(offset) + @at + offset + end + def -(offset) + @at - offset + end + def to_s + @at.to_s(16) + end + def self.positions @positions end @@ -33,15 +50,14 @@ module Risc end def self.set_position( object , pos ) - raise "Position must be number not :#{pos}:" unless pos.is_a?(Numeric) # resetting of position used to be error, but since relink and dynamic instruction size it is ok. # in measures (of 32) #puts "Setting #{pos} for #{self.class}" old = Position.positions[object] - if old != nil and ((old - pos).abs > 10000) - raise "position set again #{pos}!=#{old} for #{object}" + if old != nil and ((old - pos).abs > 1000) + raise "position set too far off #{pos}!=#{old} for #{object}" end - self.positions[object] = pos + self.positions[object] = Position.new( pos ) end end end diff --git a/test/risc/test_padding.rb b/test/risc/test_padding.rb index ce0996b7..b8e1df66 100644 --- a/test/risc/test_padding.rb +++ b/test/risc/test_padding.rb @@ -1,20 +1,49 @@ require_relative "../helper" -class TestPadding < MiniTest::Test +module Risc + class TestPadding < MiniTest::Test - def test_small - [6,27,28].each do |p| - assert_equal 32 , Padding.padded(p) , "Expecting 32 for #{p}" + def setup + Risc.machine.boot unless Risc.machine.booted end - end - def test_medium - [29,33,40,57,60].each do |p| - assert_equal 64 , Padding.padded(p) , "Expecting 64 for #{p}" + + def test_small + [6,27,28].each do |p| + assert_equal 32 , Padding.padded(p) , "Expecting 32 for #{p}" + end end - end - def test_large - [61,65,88].each do |p| - assert_equal 96 , Padding.padded(p) , "Expecting 96 for #{p}" + def test_medium + [29,33,40,57,60].each do |p| + assert_equal 64 , Padding.padded(p) , "Expecting 64 for #{p}" + end end + + def test_large + [61,65,88].each do |p| + assert_equal 96 , Padding.padded(p) , "Expecting 96 for #{p}" + end + end + def test_list1 + list = Parfait.new_list([1]) + assert_equal 32 , list.padded_length + end + def test_list5 + list = Parfait.new_list([1,2,3,4,5]) + assert_equal 32 , list.padded_length + end + def test_type + type = Parfait::Type.for_hash Parfait.object_space.get_class_by_name(:Object) , {} + type.set_type( type ) + assert_equal 32 , type.padded_length + end + def test_word + word = Parfait::Word.new(12) + assert_equal 32 , word.padded_length + end + def test_pos_arm + mov = Arm::ArmMachine.mov :r1, 128 + mov.set_position(0,0) + end + end end diff --git a/test/risc/test_position.rb b/test/risc/test_position.rb new file mode 100644 index 00000000..9cba3cfe --- /dev/null +++ b/test/risc/test_position.rb @@ -0,0 +1,37 @@ +require_relative "../helper" + +module Risc + class TestPosition < MiniTest::Test + + def test_creation_ok + assert Position.new(0) + end + def test_creation_fail + assert_raises {Position.new("0")} + end + def test_add + res = Position.new(0) + 5 + assert_equal 5 , res + end + def test_sub + res = Position.new(5) - 1 + assert_equal 4 , res + end + def test_set + pos = Position.set_position(self , 5) + assert_equal 5 , pos.at + end + def test_reset_ok + pos = Position.set_position(self , 5) + pos = Position.set_position(self , 10) + assert_equal 10 , pos.at + end + def test_reset_fail + Position.set_position(self , 5) + assert_raises{Position.set_position(self , 10000)} + end + def test_raises_set_nil + assert_raises { Position.set_position(self,nil)} + end + end +end diff --git a/test/risc/test_positioned.rb b/test/risc/test_positioned.rb deleted file mode 100644 index e45414e4..00000000 --- a/test/risc/test_positioned.rb +++ /dev/null @@ -1,42 +0,0 @@ -require_relative "../helper" - -class TestPosition < MiniTest::Test - def setup - Risc.machine.boot unless Risc.machine.booted - end - def test_list1 - list = Parfait.new_list([1]) - assert_equal 32 , list.padded_length - end - def test_list5 - list = Parfait.new_list([1,2,3,4,5]) - assert_equal 32 , list.padded_length - end - def test_type - type = Parfait::Type.for_hash Parfait.object_space.get_class_by_name(:Object) , {} - type.set_type( type ) - assert_equal 32 , type.padded_length - end - def test_word - word = Parfait::Word.new(12) - assert_equal 32 , word.padded_length - end - def test_raises_no_init - assert_raises { Position.position(self)} - end - def test_raises_set_nil - assert_raises { Position.set_position(self,nil)} - end - def test_raises_reset_far - assert_raises do - test = TestPosition.new - test.set_position 0 - test.set_position 12000 - end - end - - def test_pos_arm - mov = Arm::ArmMachine.mov :r1, 128 - mov.set_position(0,0) - end -end