fold position module and object position

simpler that way, aslo code is moving to listners
This commit is contained in:
Torsten Ruger
2018-06-02 21:59:41 +03:00
parent 24f6e30b54
commit c2d450f779
17 changed files with 381 additions and 441 deletions

View File

@ -1,11 +1,9 @@
require_relative "../helper"
module Risc
module Position
class Dummy
def padded_length
4
end
class Dummy
def padded_length
4
end
end
end

View File

@ -1,35 +1,33 @@
require_relative "helper"
module Risc
module Position
# tests that require a boot and test propagation
class TestcodeListener < MiniTest::Test
def setup
Risc.machine.boot
@binary = Parfait::BinaryCode.new(1)
@method = Parfait.object_space.types.values.first.methods
@label = Risc.label("hi","ho")
end
# tests that require a boot and test propagation
class TestcodeListener < MiniTest::Test
def setup
Risc.machine.boot
@binary = Parfait::BinaryCode.new(1)
@method = Parfait.object_space.types.values.first.methods
@label = Risc.label("hi","ho")
end
def test_has_init
pos = CodeListener.init(@binary)
assert_equal pos, Position.get(@binary)
end
def test_init_returns_position
def test_has_init
pos = CodeListener.init(@binary)
assert_equal pos, Position.get(@binary)
end
def test_init_returns_position
assert_equal Position::ObjectPosition , CodeListener.init(@binary).class
end
def test_init_listner
@binary.extend_one
CodeListener.init(@binary)
pos = Position.get(@binary)
assert !pos.event_table[:position_changed].empty?
end
def test_not_init_listner
CodeListener.init(@binary)
pos = Position.get(@binary)
assert pos.event_table[:position_changed].empty?
end
assert_equal Position , CodeListener.init(@binary).class
end
def test_init_listner
@binary.extend_one
CodeListener.init(@binary)
pos = Position.get(@binary)
assert !pos.event_table[:position_changed].empty?
end
def test_not_init_listner
CodeListener.init(@binary)
pos = Position.get(@binary)
assert pos.event_table[:position_changed].empty?
end
end
end

View File

@ -11,7 +11,7 @@ module Risc
end
def test_set_bin
pos = Position.set( @binary , 0 , @method)
assert_equal Position::CodePosition , pos.class
assert_equal CodePosition , pos.class
end
def test_type
pos = Position.set( @binary , 0 , @method)

View File

@ -1,63 +1,61 @@
require_relative "helper"
module Risc
module Position
# tests that require a boot and test propagation
class TestInstructionListener < MiniTest::Test
def setup
Risc.machine.boot
@binary = Parfait::BinaryCode.new(1)
#Position.set(@binary , 0 , Parfait.object_space.get_main)
@label = Risc.label("hi","ho")
end
def test_init
assert InstructionListener.init(@label , @binary)
end
def pest_set_instr
pos = Position.set( @label , 8 , @binary)
assert_equal InstructionPosition , pos.class
end
def pest_label_set_int
Position.set( @label , 8 , @binary)
assert_equal 8 , @label.address.value
end
def pest_label_reset_int
Position.set( @label , 8 , @binary)
Position.set( @label , 18 , @binary)
assert_equal 18 , @label.address.value
end
def pest_ins_propagates
@label.set_next Arm::ArmMachine.b( @label)
Position.set( @label , 8 , @binary)
assert_equal 8 , Position.get(@label.next).at
end
def pest_ins_propagates_again
second = Arm::ArmMachine.b( @label)
@label.set_next(second)
Position.set( @label , 8 , @binary)
Position.set(second , 12 , @binary)
Position.set( @label , 8 , @binary)
assert_equal 8 , Position.get(@label.next).at
end
def pest_label_at
branch = Branch.new("b" , @label)
Position.set(@label , 8 , @binary)
Position.set(branch , 8 , @binary)
at_4 = Position.at(8)
assert_equal InstructionPosition , at_4.class
assert_equal Branch , at_4.instruction.class
end
def pest_label_at_reverse
branch = Branch.new("b" , @label)
Position.set(branch , 8 , @binary)
Position.set(@label , 8 , @binary)
at_4 = Position.at(8)
assert_equal InstructionPosition , at_4.class
assert_equal Branch , at_4.instruction.class
end
def pest_reset_false_type
assert_raises {Position.set(@label , 0 , @binary)}
end
# tests that require a boot and test propagation
class TestInstructionListener < MiniTest::Test
def setup
Risc.machine.boot
@binary = Parfait::BinaryCode.new(1)
#Position.set(@binary , 0 , Parfait.object_space.get_main)
@label = Risc.label("hi","ho")
end
def test_init
assert InstructionListener.init(@label , @binary)
end
def pest_set_instr
pos = Position.set( @label , 8 , @binary)
assert_equal InstructionPosition , pos.class
end
def pest_label_set_int
Position.set( @label , 8 , @binary)
assert_equal 8 , @label.address.value
end
def pest_label_reset_int
Position.set( @label , 8 , @binary)
Position.set( @label , 18 , @binary)
assert_equal 18 , @label.address.value
end
def pest_ins_propagates
@label.set_next Arm::ArmMachine.b( @label)
Position.set( @label , 8 , @binary)
assert_equal 8 , Position.get(@label.next).at
end
def pest_ins_propagates_again
second = Arm::ArmMachine.b( @label)
@label.set_next(second)
Position.set( @label , 8 , @binary)
Position.set(second , 12 , @binary)
Position.set( @label , 8 , @binary)
assert_equal 8 , Position.get(@label.next).at
end
def pest_label_at
branch = Branch.new("b" , @label)
Position.set(@label , 8 , @binary)
Position.set(branch , 8 , @binary)
at_4 = Position.at(8)
assert_equal InstructionPosition , at_4.class
assert_equal Branch , at_4.instruction.class
end
def pest_label_at_reverse
branch = Branch.new("b" , @label)
Position.set(branch , 8 , @binary)
Position.set(@label , 8 , @binary)
at_4 = Position.at(8)
assert_equal InstructionPosition , at_4.class
assert_equal Branch , at_4.instruction.class
end
def pest_reset_false_type
assert_raises {Position.set(@label , 0 , @binary)}
end
end
end

View File

@ -1,29 +1,27 @@
require_relative "helper"
module Risc
module Position
class TestObjectListener < MiniTest::Test
class TestPositionListener < MiniTest::Test
def setup
@object = Dummy.new
@dependent = Dummy.new
@pos = ObjectPosition.new(@object,0)
ObjectPosition.new(@dependent,0)
@listener = ObjectListener.new(@dependent)
end
def test_register
assert @pos.register_event(:position_changed , @listener)
end
def test_no_fire
@pos.register_event(:position_changed , self)
Position.set_to(@pos,0)
assert_equal 0 , Position.get(@object).at
end
def test_reset
@pos.register_event(:position_changed , @listener)
Position.set_to(@pos,4)
assert_equal 0 , Position.at(4).at
end
def setup
@object = Dummy.new
@dependent = Dummy.new
@pos = Position.new(@object,0)
Position.new(@dependent,0)
@listener = PositionListener.new(@dependent)
end
def test_register
assert @pos.register_event(:position_changed , @listener)
end
def test_no_fire
@pos.register_event(:position_changed , self)
Position.set_to(@pos,0)
assert_equal 0 , Position.get(@object).at
end
def test_reset
@pos.register_event(:position_changed , @listener)
Position.set_to(@pos,4)
assert_equal 0 , Position.at(4).at
end
end
end

View File

@ -1,89 +0,0 @@
require_relative "helper"
module Risc
module Position
# tests that do no require a boot and only test basic positioning
class TestObjectPosition < MiniTest::Test
def test_init
assert ObjectPosition.init(self , -1)
end
def test_next_slot
mov = Arm::ArmMachine.mov(:r1 , :r1)
position = ObjectPosition.new(mov , 0)
assert_equal 4, position.next_slot
end
def test_has_get_code
assert_nil ObjectPosition.init(self , -1).get_code
end
def pest_creation_ok
assert ObjectPosition.new(self,0)
end
def pest_creation_fail
assert_raises {Position.new("0")}
end
def pest_add
res = ObjectPosition.new(self,0) + 5
assert_equal 5 , res
end
def pest_sub
res = ObjectPosition.new(self,0) - 1
assert_equal -1 , res
end
def pest_sub_pos
res = ObjectPosition.new(self,0) - ObjectPosition.new(self,0)
assert_equal 0 , res
end
def pest_set
pos = Position.set(self , 5)
assert_equal 5 , pos.at
end
def tet_tos
assert_equal "0x10" , Position.set(self).to_s
end
def pest_reset_ok
pos = Position.set(self , 5)
pos = Position.set(self , 10)
assert_equal 10 , pos.at
end
def pest_reset_fail
Position.set(self , 5)
assert_raises{Position.set(self , 10000)}
end
def pest_raises_set_nil
assert_raises { Position.set(self,nil)}
end
def pest_at
pos = Position.set(self , 5)
pos = Position.at(5)
assert_equal 5 , pos.at
end
end
class TestPositionEvents < MiniTest::Test
def setup
@position = ObjectPosition.new(self)
end
def pest_has_register
assert @position.register_event(:position_changed , self)
end
def pest_can_unregister
assert @position.register_event(:position_changed ,self)
assert @position.unregister_event(:position_changed ,self)
end
def pest_fires
@position.register_event(:position_changed ,self)
@position.trigger(:position_changed , @position)
assert_equal @position , @trigger
end
def pest_no_fire_after_unregister
assert @position.register_event(:position_changed ,self)
assert @position.unregister_event(:position_changed ,self)
@position.trigger(:position_changed , @position)
assert_nil @trigger
end
def position_changed(pos)
@trigger = pos
end
end
end
end

View File

@ -1,5 +1,91 @@
require_relative "helper"
module Risc
# tests that do no require a boot and only test basic positioning
class TestPosition < MiniTest::Test
def test_new
assert Position.new(self , -1)
end
def test_next_slot
mov = Arm::ArmMachine.mov(:r1 , :r1)
position = Position.new(mov , 0)
assert_equal 4, position.next_slot
end
def test_has_get_code
assert_nil Position.new(self , -1).get_code
end
def pest_creation_ok
assert Position.new(self,0)
end
def pest_creation_fail
assert_raises {Position.new("0")}
end
def pest_add
res = Position.new(self,0) + 5
assert_equal 5 , res
end
def pest_sub
res = Position.new(self,0) - 1
assert_equal -1 , res
end
def pest_sub_pos
res = Position.new(self,0) - Position.new(self,0)
assert_equal 0 , res
end
def pest_set
pos = Position.set(self , 5)
assert_equal 5 , pos.at
end
def tet_tos
assert_equal "0x10" , Position.set(self).to_s
end
def pest_reset_ok
pos = Position.set(self , 5)
pos = Position.set(self , 10)
assert_equal 10 , pos.at
end
def pest_reset_fail
Position.set(self , 5)
assert_raises{Position.set(self , 10000)}
end
def pest_raises_set_nil
assert_raises { Position.set(self,nil)}
end
def pest_at
pos = Position.set(self , 5)
pos = Position.at(5)
assert_equal 5 , pos.at
end
end
class TestPositionEvents < MiniTest::Test
def setup
@position = Position.new(self)
end
def pest_has_register
assert @position.register_event(:position_changed , self)
end
def pest_can_unregister
assert @position.register_event(:position_changed ,self)
assert @position.unregister_event(:position_changed ,self)
end
def pest_fires
@position.register_event(:position_changed ,self)
@position.trigger(:position_changed , @position)
assert_equal @position , @trigger
end
def pest_no_fire_after_unregister
assert @position.register_event(:position_changed ,self)
assert @position.unregister_event(:position_changed ,self)
@position.trigger(:position_changed , @position)
assert_nil @trigger
end
def position_changed(pos)
@trigger = pos
end
end
end
module Risc
class TestMachinePositions < MiniTest::Test
def setup