2018-06-01 18:13:53 +02:00
|
|
|
require_relative "helper"
|
2018-05-17 19:13:33 +02:00
|
|
|
|
2018-06-02 20:59:41 +02:00
|
|
|
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
|
2018-06-02 20:59:41 +02:00
|
|
|
def test_new
|
2018-06-02 22:48:12 +02:00
|
|
|
assert @pos
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-06 00:16:00 +02:00
|
|
|
def test_invalid
|
|
|
|
assert !@pos.valid?
|
|
|
|
end
|
2018-06-02 20:59:41 +02:00
|
|
|
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
|
2018-06-02 20:59:41 +02:00
|
|
|
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
|
2018-06-02 20:59:41 +02:00
|
|
|
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
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_set
|
|
|
|
assert_equal 0 , @pos.set(0)
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
end
|
|
|
|
class TestPositionMath < MiniTest::Test
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@pos = Position.new(self , 5)
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_add
|
|
|
|
res = @pos + 5
|
|
|
|
assert_equal 10 , res
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_sub
|
|
|
|
res = @pos - 3
|
|
|
|
assert_equal 2 , res
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_sub_pos
|
|
|
|
res = @pos - Position.new(@pos,4)
|
|
|
|
assert_equal 1 , res
|
2018-06-02 20:59:41 +02:00
|
|
|
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
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_reset_ok
|
|
|
|
pos = @pos.set(10)
|
|
|
|
assert_equal 10 , pos
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_at
|
2018-06-02 20:59:41 +02:00
|
|
|
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
|
2018-06-05 17:11:25 +02:00
|
|
|
@instruction = DummyInstruction.new
|
2018-06-02 22:48:12 +02:00
|
|
|
@position = Position.new(@instruction , 0)
|
|
|
|
@listener = PositionListener.new( @instruction )
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-02 22:48:12 +02:00
|
|
|
def test_has_register
|
|
|
|
assert @position.position_listener( @instruction )
|
2018-06-02 20:59:41 +02:00
|
|
|
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)
|
2018-06-02 20:59:41 +02:00
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_fires
|
2018-06-02 22:48:12 +02:00
|
|
|
@object = @instruction
|
2018-06-05 18:05:12 +02:00
|
|
|
@position.register_event(:position_changed , self)
|
2018-06-02 20:59:41 +02:00
|
|
|
@position.trigger(:position_changed , @position)
|
|
|
|
assert_equal @position , @trigger
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
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)
|
2018-06-02 20:59:41 +02:00
|
|
|
@position.trigger(:position_changed , @position)
|
|
|
|
assert_nil @trigger
|
|
|
|
end
|
|
|
|
def position_changed(pos)
|
|
|
|
@trigger = pos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-17 19:13:33 +02:00
|
|
|
module Risc
|
|
|
|
class TestMachinePositions < MiniTest::Test
|
|
|
|
def setup
|
|
|
|
@machine = Risc.machine.boot
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_cpu_init
|
2018-05-17 19:13:33 +02:00
|
|
|
@machine.translate(:interpreter)
|
|
|
|
@machine.position_all
|
|
|
|
assert Position.get @machine.cpu_init
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_cpu_label
|
2018-05-17 19:13:33 +02:00
|
|
|
@machine.translate(:interpreter)
|
|
|
|
@machine.position_all
|
|
|
|
assert Position.get( @machine.cpu_init.label )
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_cpu_first_arm
|
2018-05-17 19:13:33 +02:00
|
|
|
@machine.translate(:arm)
|
|
|
|
@machine.position_all
|
|
|
|
assert Position.get( @machine.cpu_init.first )
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_has_arm_pos
|
2018-05-17 19:13:33 +02:00
|
|
|
has_positions(:arm)
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_has_int_pos
|
2018-05-17 19:13:33 +02:00
|
|
|
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
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_has_arm_meth
|
2018-05-17 19:13:33 +02:00
|
|
|
meth_positions(:arm)
|
|
|
|
end
|
2018-06-05 18:05:12 +02:00
|
|
|
def test_has_int_meth
|
2018-05-17 19:13:33 +02:00
|
|
|
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
|