rubyx/test/risc/position/test_position.rb

156 lines
3.9 KiB
Ruby
Raw Normal View History

2018-06-01 18:13:53 +02:00
require_relative "helper"
module Risc
# tests that do no require a boot and only test basic positioning
class TestPosition < MiniTest::Test
2018-06-02 22:48:12 +02:00
def setup
@pos = Position.new(self , -1)
end
def test_new
2018-06-02 22:48:12 +02:00
assert @pos
end
def test_invalid
assert !@pos.valid?
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
2018-06-02 22:48:12 +02:00
assert_nil @pos.get_code
end
2018-06-02 22:02:59 +02:00
def test_has_listeners_helper
2018-06-02 22:48:12 +02:00
assert_equal Array , @pos.position_listeners.class
end
2018-06-02 22:02:59 +02:00
def test_listeners_empty
2018-06-02 22:48:12 +02:00
assert @pos.position_listeners.empty?
2018-06-02 22:02:59 +02:00
end
def test_has_listener_helper
2018-06-02 22:48:12 +02:00
@pos.position_listener( self )
assert_equal 1 , @pos.position_listeners.length
end
2018-06-02 22:48:12 +02:00
def test_set
assert_equal 0 , @pos.set(0)
end
2018-06-02 22:48:12 +02:00
end
class TestPositionMath < MiniTest::Test
def setup
@pos = Position.new(self , 5)
end
2018-06-02 22:48:12 +02:00
def test_add
res = @pos + 5
assert_equal 10 , res
end
2018-06-02 22:48:12 +02:00
def test_sub
res = @pos - 3
assert_equal 2 , res
end
2018-06-02 22:48:12 +02:00
def test_sub_pos
res = @pos - Position.new(@pos,4)
assert_equal 1 , res
end
2018-06-05 23:53:41 +02:00
def test_lg
assert @pos > Position.new(@pos,4)
end
2018-06-02 22:48:12 +02:00
def test_tos
assert_equal "0x5" , @pos.to_s
end
2018-06-02 22:48:12 +02:00
def test_reset_ok
pos = @pos.set(10)
assert_equal 10 , pos
end
2018-06-02 22:48:12 +02:00
def test_at
pos = Position.at(5)
assert_equal 5 , pos.at
end
end
class TestPositionEvents < MiniTest::Test
def setup
2018-06-02 22:48:12 +02:00
Position.clear_positions
@instruction = DummyInstruction.new
2018-06-02 22:48:12 +02:00
@position = Position.new(@instruction , 0)
@listener = PositionListener.new( @instruction )
end
2018-06-02 22:48:12 +02:00
def test_has_register
assert @position.position_listener( @instruction )
end
2018-06-02 22:48:12 +02:00
def test_can_unregister
2018-06-02 22:02:59 +02:00
listener = PositionListener.new(self)
assert @position.position_listener(listener)
assert @position.unregister_event(:position_changed ,listener)
end
def test_fires
2018-06-02 22:48:12 +02:00
@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.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
@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