2018-06-24 16:10:39 +02:00
|
|
|
require_relative 'helper'
|
|
|
|
|
2020-02-08 11:44:35 +01:00
|
|
|
# set environment TEST_ARM to true to have these tests run
|
|
|
|
# They need qemu and arm cross linker to be installed
|
|
|
|
# Also they are too slow to include in normal runs, so only by request and on travis
|
|
|
|
|
|
|
|
# set TEST_ARM to DEBUG to get extra output
|
|
|
|
|
2018-06-24 16:10:39 +02:00
|
|
|
module Mains
|
|
|
|
class TestArm < MiniTest::Test
|
2020-03-22 17:18:07 +01:00
|
|
|
@qemu = "qemu-arm"
|
|
|
|
@linker = "arm-linux-gnueabi-ld"
|
2019-08-18 14:30:52 +02:00
|
|
|
def self.Linker
|
2020-03-22 17:18:07 +01:00
|
|
|
@linker
|
2019-08-18 14:30:52 +02:00
|
|
|
end
|
|
|
|
def self.Qemu
|
2020-03-22 17:18:07 +01:00
|
|
|
@qemu
|
2019-08-18 14:30:52 +02:00
|
|
|
end
|
2018-06-24 16:44:02 +02:00
|
|
|
|
2018-06-24 16:10:39 +02:00
|
|
|
# runnable_methods is called by minitest to determine which tests to run
|
|
|
|
def self.runnable_methods
|
2018-06-24 16:44:02 +02:00
|
|
|
all = Dir["test/mains/source/*_*.rb"]
|
2018-06-24 16:10:39 +02:00
|
|
|
tests =[]
|
2019-08-18 11:39:23 +02:00
|
|
|
return tests unless has_qemu
|
2018-06-24 16:10:39 +02:00
|
|
|
all.each do |file_name|
|
|
|
|
fullname = file_name.split("/").last.split(".").first
|
2019-09-24 21:05:38 +02:00
|
|
|
order , name , stdout , exit_code = fullname.split("_")
|
|
|
|
method_name = "test_#{order}_#{name}"
|
2018-06-24 16:10:39 +02:00
|
|
|
input = File.read(file_name)
|
2018-06-24 16:44:02 +02:00
|
|
|
tests << method_name
|
2018-06-24 16:10:39 +02:00
|
|
|
self.send(:define_method, method_name ) do
|
2019-09-24 21:05:38 +02:00
|
|
|
out , code = run_code(input , "#{order}_#{name}")
|
2018-06-24 16:10:39 +02:00
|
|
|
assert_equal stdout , out , "Wrong stdout #{name}"
|
|
|
|
assert_equal exit_code , code.to_s , "Wrong exit code #{name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tests
|
|
|
|
end
|
|
|
|
|
2019-08-18 11:39:23 +02:00
|
|
|
def self.has_qemu
|
2019-08-18 14:22:09 +02:00
|
|
|
if `uname -a`.include?("torsten")
|
2020-03-22 17:18:07 +01:00
|
|
|
@linker = "arm-linux-gnu-ld" #on fedora
|
2019-08-18 14:22:09 +02:00
|
|
|
end
|
2020-03-22 17:18:07 +01:00
|
|
|
return false #Disabling for a moment unless ENV["TEST_ARM"]
|
2019-08-18 14:22:09 +02:00
|
|
|
begin
|
2020-03-22 17:18:07 +01:00
|
|
|
`#{@qemu} -version`
|
|
|
|
`#{@linker} -v`
|
2019-08-18 14:22:09 +02:00
|
|
|
rescue => e
|
|
|
|
puts e
|
|
|
|
return false
|
|
|
|
end
|
2019-08-18 11:39:23 +02:00
|
|
|
true
|
2018-06-24 16:10:39 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 11:39:23 +02:00
|
|
|
def run_code(input , name )
|
2020-02-08 11:44:35 +01:00
|
|
|
puts "Compiling #{name}.o" if ENV["TEST_ARM"] == "DEBUG"
|
2019-08-18 11:39:23 +02:00
|
|
|
|
2019-09-24 21:05:38 +02:00
|
|
|
linker = ::RubyX::RubyXCompiler.new({}).ruby_to_binary( input , :arm )
|
2019-08-18 11:39:23 +02:00
|
|
|
writer = Elf::ObjectWriter.new(linker)
|
|
|
|
|
|
|
|
writer.save "mains.o"
|
|
|
|
|
2020-02-08 11:44:35 +01:00
|
|
|
puts "Linking #{name}" if ENV["TEST_ARM"] == "DEBUG"
|
2019-08-18 11:39:23 +02:00
|
|
|
|
2019-08-18 14:30:52 +02:00
|
|
|
`#{TestArm.Linker} -N mains.o`
|
2019-08-18 11:39:23 +02:00
|
|
|
assert_equal 0 , $?.exitstatus , "Linking #{name} failed #{$?}"
|
|
|
|
|
2020-02-08 11:44:35 +01:00
|
|
|
puts "Running #{name}" if ENV["TEST_ARM"] == "DEBUG"
|
2019-08-18 14:30:52 +02:00
|
|
|
stdout = `#{TestArm.Qemu} ./a.out`
|
2019-08-18 11:39:23 +02:00
|
|
|
exit_code = $?.exitstatus
|
2020-02-08 11:44:35 +01:00
|
|
|
puts "Result #{stdout} #{exit_code}" if ENV["TEST_ARM"] == "DEBUG"
|
2018-06-24 16:44:02 +02:00
|
|
|
return stdout , exit_code
|
2018-06-24 16:10:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|