From 7fa8397b56bf6c583a10dc6b063419984b98e1b3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 6 Jun 2018 10:19:18 +0300 Subject: [PATCH] insertion pushes, split position tests --- lib/risc/position/instruction_listener.rb | 16 +-- lib/risc/position/position.rb | 9 +- .../position/test_instruction_listener1.rb | 2 +- test/risc/position/test_position.rb | 129 +----------------- test/risc/position/test_position1.rb | 46 +++++++ test/risc/position/test_position2.rb | 36 +++++ test/risc/position/test_position3.rb | 53 +++++++ 7 files changed, 155 insertions(+), 136 deletions(-) create mode 100644 test/risc/position/test_position1.rb create mode 100644 test/risc/position/test_position2.rb create mode 100644 test/risc/position/test_position3.rb diff --git a/lib/risc/position/instruction_listener.rb b/lib/risc/position/instruction_listener.rb index 318905cb..5287823f 100644 --- a/lib/risc/position/instruction_listener.rb +++ b/lib/risc/position/instruction_listener.rb @@ -45,23 +45,23 @@ module Risc # before the insert, ie: # position : the arg is the first instruction in the chain where insert happened # position.object.next : is the newly inserted instruction we need to setup - # @instruction : previously dependent on position, now on .next's position + # @instruction : previously dependent on position, now on inserted's position # # So we need to : - # - move the listener of @instruction to listen to the inserted instruction + # - move the listener self to listen to the inserted instruction # - add a listener for the new instruction (to listen to the arg) # - set the new position (moving the chain along) def position_inserted(position) inserted = position.object.next raise "uups" unless inserted ## TODO: remove - position.remove_position_listener(InstructionListener) - new_pos = Position.get(inserted) - new_pos.position_listener(old_listener) + position.remove_position_listener(self) + new_pos = Position.new(inserted , -1) + new_pos.position_listener(self) - my_pos = Position.get(@instruction) - listen = InstructionListener.new(position.object , @binary) - my_pos.position_listener(listen) + listen = InstructionListener.new(inserted , @binary) + position.position_listener(listen) + position.trigger_changed end # check that the binary we use is the one where the current position falls diff --git a/lib/risc/position/position.rb b/lib/risc/position/position.rb index 40381f2e..09cab3ac 100644 --- a/lib/risc/position/position.rb +++ b/lib/risc/position/position.rb @@ -69,10 +69,17 @@ module Risc return int if int == self.at Position.set_to(self , int) @at = int - trigger(:position_changed , self ) + trigger_changed int end + # helper to fire the event that the position changed + # Note: set checks if the position actually has changed, before fireing + # but during insert it is helpful to trigger just to set the next + def trigger_changed + trigger(:position_changed , self ) + end + def trigger_inserted event_table[:position_changed].each { |handler| handler.position_inserted( self) } end diff --git a/test/risc/position/test_instruction_listener1.rb b/test/risc/position/test_instruction_listener1.rb index c19aa226..cce7a910 100644 --- a/test/risc/position/test_instruction_listener1.rb +++ b/test/risc/position/test_instruction_listener1.rb @@ -24,7 +24,7 @@ module Risc @instruction.insert DummyInstruction.new assert Position.get(@instruction.next) end - def pest_insert_pushes + def test_insert_pushes @instruction.insert DummyInstruction.new assert_equal 76 , Position.get(@instruction.last).at end diff --git a/test/risc/position/test_position.rb b/test/risc/position/test_position.rb index b110303a..aa480616 100644 --- a/test/risc/position/test_position.rb +++ b/test/risc/position/test_position.rb @@ -27,6 +27,9 @@ module Risc def test_has_trigger_inserted assert_equal [] , @pos.trigger_inserted end + def test_has_trigger_changed + assert_equal [] , @pos.trigger_changed + end def test_listeners_empty assert @pos.position_listeners.empty? end @@ -38,130 +41,4 @@ module Risc assert_equal 0 , @pos.set(0) end end - class TestPositionMath < MiniTest::Test - - def setup - @pos = Position.new(self , 5) - end - def test_add - res = @pos + 5 - assert_equal 10 , res - end - def test_sub - res = @pos - 3 - assert_equal 2 , res - end - def test_sub_pos - res = @pos - Position.new(@pos,4) - assert_equal 1 , res - end - def test_lg - assert @pos > Position.new(@pos,4) - end - def test_tos - assert_equal "0x5" , @pos.to_s - end - def test_reset_ok - pos = @pos.set(10) - assert_equal 10 , pos - end - def test_at - pos = Position.at(5) - assert_equal 5 , pos.at - end - end - class TestPositionEvents < MiniTest::Test - def setup - Position.clear_positions - @instruction = DummyInstruction.new - @position = Position.new(@instruction , 0) - @listener = PositionListener.new( @instruction ) - end - def test_has_register - assert @position.position_listener( @instruction ) - end - def test_can_unregister - listener = PositionListener.new(self) - assert @position.position_listener(listener) - assert @position.remove_position_listener(listener) - end - def test_fires - @object = @instruction - @position.register_event(:position_changed , self) - @position.trigger(:position_changed , @position) - assert_equal @position , @trigger - end - def test_no_fire_after_unregister - @object = @instruction - Position.new(self, 10) - assert @position.register_event(:position_changed , self) - assert @position.remove_position_listener(self) - @position.trigger(:position_changed , @position) - assert_nil @trigger - end - def test_can_trigger_inserted - @object = @instruction - @position.register_event(:position_changed , self) - @position.trigger_inserted - assert_equal @position , @trigger - end - def position_changed(pos) - @trigger = pos - end - def position_inserted(pos) - @trigger = pos - end - end -end - -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 diff --git a/test/risc/position/test_position1.rb b/test/risc/position/test_position1.rb new file mode 100644 index 00000000..d2e5e77e --- /dev/null +++ b/test/risc/position/test_position1.rb @@ -0,0 +1,46 @@ +require_relative "helper" + +module Risc + class TestPositionEvents < MiniTest::Test + def setup + Position.clear_positions + @instruction = DummyInstruction.new + @position = Position.new(@instruction , 0) + @listener = PositionListener.new( @instruction ) + end + def test_has_register + assert @position.position_listener( @instruction ) + end + def test_can_unregister + listener = PositionListener.new(self) + assert @position.position_listener(listener) + assert @position.remove_position_listener(listener) + end + def test_fires + @object = @instruction + @position.register_event(:position_changed , self)#can't use helper + @position.trigger_changed + assert_equal @position , @trigger + end + def test_no_fire_after_unregister + @object = @instruction + Position.new(self, 10) + assert @position.register_event(:position_changed , self)#can't use helper + assert @position.remove_position_listener(self) + @position.trigger(:position_changed , @position) + assert_nil @trigger + end + def test_can_trigger_inserted + @object = @instruction + @position.register_event(:position_changed , self) #can't use helper + @position.trigger_inserted + assert_equal @position , @trigger + end + def position_changed(pos) + @trigger = pos + end + def position_inserted(pos) + @trigger = pos + end + end +end diff --git a/test/risc/position/test_position2.rb b/test/risc/position/test_position2.rb new file mode 100644 index 00000000..414083fa --- /dev/null +++ b/test/risc/position/test_position2.rb @@ -0,0 +1,36 @@ +require_relative "helper" + +module Risc + class TestPositionMath < MiniTest::Test + + def setup + @pos = Position.new(self , 5) + end + def test_add + res = @pos + 5 + assert_equal 10 , res + end + def test_sub + res = @pos - 3 + assert_equal 2 , res + end + def test_sub_pos + res = @pos - Position.new(@pos,4) + assert_equal 1 , res + end + def test_lg + assert @pos > Position.new(@pos,4) + end + def test_tos + assert_equal "0x5" , @pos.to_s + end + def test_reset_ok + pos = @pos.set(10) + assert_equal 10 , pos + end + def test_at + pos = Position.at(5) + assert_equal 5 , pos.at + end + end +end diff --git a/test/risc/position/test_position3.rb b/test/risc/position/test_position3.rb new file mode 100644 index 00000000..933fada4 --- /dev/null +++ b/test/risc/position/test_position3.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