diff --git a/lib/risc/position.rb b/lib/risc/position.rb index 71d42785..408b1b01 100644 --- a/lib/risc/position.rb +++ b/lib/risc/position.rb @@ -21,6 +21,15 @@ module Risc @positions end + def self.at( int ) + self.positions.each do |object , position| + next unless position.at == int + return position unless position.is_a?(InstructionPosition) + return position unless position.instruction.is_a?(Label) + end + nil + end + def self.set?(object) self.positions.has_key?(object) end diff --git a/test/risc/interpreter/test_events.rb b/test/risc/interpreter/test_events.rb index 93f6421f..52152a4a 100644 --- a/test/risc/interpreter/test_events.rb +++ b/test/risc/interpreter/test_events.rb @@ -21,7 +21,7 @@ module Risc def length 88 end - def est_state_change + def test_state_change @interpreter.register_event :state_changed , self ticks length assert @state_events[:state_changed] @@ -30,7 +30,7 @@ module Risc @interpreter.unregister_event :state_changed , self end - def est_instruction_events + def test_instruction_events @interpreter.register_event :instruction_changed , self ticks length assert_equal length , @instruction_events.length diff --git a/test/risc/position/test_instruction_position.rb b/test/risc/position/test_instruction_position.rb index 6e218255..6055ed24 100644 --- a/test/risc/position/test_instruction_position.rb +++ b/test/risc/position/test_instruction_position.rb @@ -27,6 +27,14 @@ module Risc Position.set( @label , 0 , @binary) assert_equal 0 , Position.get(@label.next).at end + def test_label_at + branch = Branch.new("b" , @label) + Position.set(@label , 4 , @binary) + Position.set(branch , 4 , @binary) + at_4 = Position.at(4) + assert_equal InstructionPosition , at_4.class + assert_equal Branch , at_4.instruction.class + end end end end diff --git a/test/risc/position/test_object_position.rb b/test/risc/position/test_object_position.rb index b49d2179..57a4b977 100644 --- a/test/risc/position/test_object_position.rb +++ b/test/risc/position/test_object_position.rb @@ -42,6 +42,11 @@ module Risc def test_raises_set_nil assert_raises { Position.set(self,nil)} end + def test_at + pos = Position.set(self , 5) + pos = Position.at(5) + assert_equal 5 , pos.at + end end end end diff --git a/test/risc/test_machine.rb b/test/risc/test_machine.rb index 4e4d7976..6f5d951e 100644 --- a/test/risc/test_machine.rb +++ b/test/risc/test_machine.rb @@ -1,4 +1,4 @@ -require_relative "../helper" +require_relative "helper" module Risc class TestMachineObjects < MiniTest::Test @@ -18,18 +18,6 @@ module Risc assert @machine.add_constant( Parfait::Integer.new(5) ) end end - class TestMachinePositions < MiniTest::Test - def setup - @machine = Risc.machine.boot - @machine.translate(:arm) - @machine.position_all - end - def test_has_positions - @machine.objects.each do |id,obj| - assert Position.get(obj) - end - end - end class TestMachineInit < MiniTest::Test def setup @machine = Risc.machine.boot diff --git a/test/risc/test_position.rb b/test/risc/test_position.rb new file mode 100644 index 00000000..933fada4 --- /dev/null +++ b/test/risc/test_position.rb @@ -0,0 +1,53 @@ +require_relative "helper" + +module Risc + class TestMachinePositions < MiniTest::Test + def setup + @machine = Risc.machine.boot + end + def test_cpu_init + @machine.translate(:interpreter) + @machine.position_all + assert Position.get @machine.cpu_init + end + def test_cpu_label + @machine.translate(:interpreter) + @machine.position_all + assert Position.get( @machine.cpu_init.label ) + end + def test_cpu_first_arm + @machine.translate(:arm) + @machine.position_all + assert Position.get( @machine.cpu_init.first ) + end + def test_has_arm_pos + has_positions(:arm) + end + def test_has_int_pos + has_positions(:interpreter) + end + def has_positions(platform) + @machine.translate(:arm) + @machine.position_all + @machine.objects.each do |id,obj| + assert Position.get(obj) + end + end + def test_has_arm_meth + meth_positions(:arm) + end + def test_has_int_meth + meth_positions(:interpreter) + end + def meth_positions(platform) + @machine.translate(:arm) + @machine.position_all + Parfait.object_space.each_type do |type| + type.each_method do |method| + assert Position.get(method.binary) + assert Position.get(method.cpu_instructions) + end + end + end + end +end