2018-05-17 19:14:59 +02:00
|
|
|
require_relative "helper"
|
|
|
|
|
|
|
|
module Risc
|
|
|
|
class TestInterpreterBasics < MiniTest::Test
|
2019-09-13 13:07:12 +02:00
|
|
|
include ScopeHelper
|
2018-07-02 16:29:26 +02:00
|
|
|
def setup
|
2019-09-13 13:07:12 +02:00
|
|
|
compiler = compiler_with_main()
|
|
|
|
@linker = compiler.to_target( :interpreter)
|
2018-07-02 16:29:26 +02:00
|
|
|
end
|
2018-05-17 19:14:59 +02:00
|
|
|
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_class
|
2018-07-02 16:29:26 +02:00
|
|
|
assert_equal Risc::Interpreter , Interpreter.new(@linker).class
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-07-03 09:12:40 +02:00
|
|
|
def test_starts
|
|
|
|
interpreter = Interpreter.new(@linker)
|
|
|
|
@linker.position_all
|
|
|
|
assert_equal 0 , interpreter.start_program
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_starts_stopped
|
2018-07-02 16:29:26 +02:00
|
|
|
assert_equal :stopped , Interpreter.new(@linker).state
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_has_regs
|
2020-03-14 13:05:23 +01:00
|
|
|
# assert_equal 16 , Interpreter.new(@linker).registers.length
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_has_r0
|
2020-03-14 13:05:23 +01:00
|
|
|
# assert_equal :r0 , Interpreter.new(@linker).registers.keys.first
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
class TestInterpreterStarts < MiniTest::Test
|
2018-07-02 16:29:26 +02:00
|
|
|
include Ticker
|
2018-05-17 19:14:59 +02:00
|
|
|
def setup
|
2018-07-02 16:29:26 +02:00
|
|
|
@string_input = as_main("return 5")
|
|
|
|
super
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_started
|
2018-05-17 19:14:59 +02:00
|
|
|
assert_equal :running , @interpreter.state
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_pos
|
2018-05-23 17:05:22 +02:00
|
|
|
assert_equal 1 , @interpreter.clock
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
class TestInterpreterTicks < MiniTest::Test
|
2018-07-02 16:29:26 +02:00
|
|
|
include Ticker
|
2018-05-17 19:14:59 +02:00
|
|
|
def setup
|
2018-07-02 16:29:26 +02:00
|
|
|
@string_input = as_main("return 5")
|
|
|
|
super
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2020-03-22 10:29:56 +01:00
|
|
|
def test_mapping
|
|
|
|
assert_equal :r0 , @interpreter.std_reg(:syscall_1)
|
|
|
|
assert_equal :r13 , @interpreter.std_reg(:message)
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_tick1
|
2018-05-23 17:05:22 +02:00
|
|
|
assert_equal 2 , @interpreter.tick
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_clock1
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
assert_equal 2 , @interpreter.clock
|
|
|
|
end
|
2019-09-15 14:13:11 +02:00
|
|
|
def test_pc
|
2019-08-24 08:46:33 +02:00
|
|
|
@interpreter.tick
|
2020-03-22 14:56:57 +01:00
|
|
|
assert_equal t = 36072 , @interpreter.pc
|
2019-08-24 08:46:33 +02:00
|
|
|
@interpreter.tick
|
2019-09-15 14:13:11 +02:00
|
|
|
assert_equal t + 4 , @interpreter.pc
|
2018-05-23 17:05:22 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_tick2
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
assert_equal 3 , @interpreter.tick
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_clock2
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
@interpreter.tick
|
|
|
|
assert_equal 3 , @interpreter.clock
|
|
|
|
end
|
2020-03-13 15:25:35 +01:00
|
|
|
def test_tick_16_jump
|
|
|
|
#16.times { @interpreter.tick ;puts @interpreter.instruction.class}
|
|
|
|
ticks(16)
|
2018-05-25 18:16:13 +02:00
|
|
|
assert_equal Branch , @interpreter.instruction.class
|
2020-03-13 15:25:35 +01:00
|
|
|
assert_equal "return_label" , @interpreter.instruction.label.name
|
2018-05-25 18:16:13 +02:00
|
|
|
end
|
2020-03-13 15:25:35 +01:00
|
|
|
def test_tick_26_exit
|
|
|
|
# 26.times { @interpreter.tick ;puts @interpreter.instruction.class}
|
2020-03-17 10:18:51 +01:00
|
|
|
ticks(27)
|
2020-03-13 15:25:35 +01:00
|
|
|
assert_equal Syscall , @interpreter.instruction.class
|
|
|
|
assert_equal :exit , @interpreter.instruction.name
|
|
|
|
end
|
|
|
|
def test_tick_13_bin
|
|
|
|
ticks(13)
|
|
|
|
bin_pos = binary_position
|
|
|
|
@interpreter.tick #jump into next binary
|
|
|
|
assert bin_pos.at != binary_position.at , "#{bin_pos} == #{binary_position}"
|
2018-05-25 18:16:13 +02:00
|
|
|
end
|
|
|
|
def binary_position
|
2018-06-10 08:15:57 +02:00
|
|
|
pos = Position.get(@interpreter.instruction)
|
|
|
|
list = pos.event_table[:position_changed].first
|
|
|
|
assert_equal InstructionListener, list.class
|
|
|
|
Position.get(list.binary)
|
2018-05-25 18:16:13 +02:00
|
|
|
end
|
2018-06-10 08:15:57 +02:00
|
|
|
def test_tick_15 #more than a binary code worth
|
2020-03-13 15:25:35 +01:00
|
|
|
15.times {@interpreter.tick}
|
2018-05-23 20:35:22 +02:00
|
|
|
end
|
2020-03-13 15:25:35 +01:00
|
|
|
class TestInterpreterDies #< MiniTest::Test
|
2019-09-09 10:47:37 +02:00
|
|
|
include Ticker
|
|
|
|
def setup
|
|
|
|
@string_input = as_main("random.call")
|
|
|
|
super
|
|
|
|
end
|
|
|
|
def test_dies
|
|
|
|
# no method does syscall :dies that raises
|
|
|
|
assert_raises {44.times {@interpreter.tick}}
|
|
|
|
end
|
|
|
|
end
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
|
|
|
end
|