From 4a79d20a4029f39261f18caefa5d5dd5f720e44f Mon Sep 17 00:00:00 2001 From: Torsten Ruger <torsten@villataika.fi> Date: Thu, 15 Mar 2018 20:51:46 +0530 Subject: [PATCH] remove duplicate tests --- test/mom/helper.rb | 81 ++++++++++- test/mom/test_assignment.rb | 131 ++++++++++++------ test/mom/test_dynamic_call_statement.rb | 29 ---- test/mom/test_if_simple_statement.rb | 29 ---- test/mom/test_if_statement.rb | 30 ---- test/mom/test_instruction.rb | 10 -- test/mom/test_simple_call_statement.rb | 23 --- test/mom/test_while_statement.rb | 30 ---- test/mom/to_risc/helper.rb | 80 ----------- test/mom/to_risc/test_assignment.rb | 107 -------------- .../to_mom/send/test_send_cached_simple.rb | 6 +- .../vool/to_mom/send/test_send_simple_args.rb | 3 + test/vool/to_mom/test_if_simple.rb | 3 + test/vool/to_mom/test_while_simple.rb | 3 + 14 files changed, 185 insertions(+), 380 deletions(-) delete mode 100644 test/mom/test_dynamic_call_statement.rb delete mode 100644 test/mom/test_if_simple_statement.rb delete mode 100644 test/mom/test_if_statement.rb delete mode 100644 test/mom/test_instruction.rb delete mode 100644 test/mom/test_simple_call_statement.rb delete mode 100644 test/mom/test_while_statement.rb delete mode 100644 test/mom/to_risc/helper.rb delete mode 100644 test/mom/to_risc/test_assignment.rb diff --git a/test/mom/helper.rb b/test/mom/helper.rb index 26258082..a4091ad3 100644 --- a/test/mom/helper.rb +++ b/test/mom/helper.rb @@ -1 +1,80 @@ -require_relative "../helper" +require_relative '../helper' + +module Risc + module SpaceHack + # test hack to in place change object type + def add_space_field(name,type) + class_type = Parfait.object_space.get_class_by_name(:Space).instance_type + class_type.send(:private_add_instance_variable, name , type) + end + end + module ExpressionHelper + include SpaceHack + + def check + Risc.machine.boot unless Risc.machine.booted + compiler = Vm::MethodCompiler.new Parfait.object_space.get_main + code = Vm.ast_to_code @input + assert code.to_s , @input + produced = compiler.process( code ) + assert @output , "No output given" + assert_equal produced.class , @output , "Wrong class" + produced + end + + end + + module Statements + include AST::Sexp + include CleanCompile + include SpaceHack + + def setup + Risc.machine.boot # force boot to reset main + end + + def preamble + [Label, LoadConstant, SlotToReg, RegToSlot ] + end + def postamble + [ Label, FunctionReturn] + end + def check_nil + assert @expect , "No output given" + Vool::VoolCompiler.ruby_to_vool "class Space; def main(arg);#{@input};end;end" + produced = Parfait.object_space.get_main.instructions + compare_instructions produced , @expect + end + def check_return + was = check_nil + raise was if was + Parfait.object_space.get_main.instructions + end + + def compare_instructions( instruction , expect ) + index = 0 + all = instruction.to_arr + full_expect = preamble + expect + postamble + #full_expect = expect + begin + should = full_expect[index] + return "No instruction at #{index}\n#{should(all)}" unless should + return "Expected at #{index+1}\n#{should(all)}" unless instruction.class == should + puts instruction.to_s + index += 1 + instruction = instruction.next + end while( instruction ) + nil + end + def should( all ) + preamble.each {all.shift} + postamble.each {all.pop} + str = all.to_s.gsub("Risc::","") + ret = "" + str.split(",").each_slice(6).each do |line| + ret += " " + line.join(",") + " ,\n" + end + ret + end + end +end diff --git a/test/mom/test_assignment.rb b/test/mom/test_assignment.rb index a0ca4387..c1f1140a 100644 --- a/test/mom/test_assignment.rb +++ b/test/mom/test_assignment.rb @@ -1,55 +1,106 @@ -require_relative "helper" +require_relative 'helper' -module Mom - class TestConstToIvarAssignemnt < MiniTest::Test - include MomCompile +module Risc + class TestAssignStatement < MiniTest::Test + include Statements - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "@a = 5") + def pest_assign_local_assign + Parfait.object_space.get_main.add_local(:r , :Integer) + @input = "r = 5" + @expect = [LoadConstant, RegToSlot] + assert_nil msg = check_nil , msg end - def test_if_compiles - assert_equal SlotConstant , @stats.class , @stats - end - def test_length - assert_equal 1 , @stats.length - end - def test_assigns_class - assert_equal Mom::IntegerConstant , @stats.right.class , @stats.inspect - end - def test_assigns_value - assert_equal 5 , @stats.right.value , @stats.inspect + def pest_assign_op + Parfait.object_space.get_main.add_local(:r , :Integer) + @input = "r = 10.mod4" + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg end - end - class TestConstToLocalAssignemnt < MiniTest::Test - include MomCompile - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "a = 5") + def pest_assign_ivar_notpresent + @input = "@r = 5" + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg end - def test_if_compiles - assert_equal SlotConstant , @stats.class , @stats + + def pest_assign_ivar + add_space_field(:r , :Integer) + + @input =s(:statements, s(:i_assignment, s(:ivar, :r), s(:int, 5))) + + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , + RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg end - def test_length - assert_equal 1 , @stats.length + + def pest_assign_call + Parfait.object_space.get_main.add_local(:r , :Object) + @input = s(:statements, s(:l_assignment, s(:local, :r), s(:call, :main, s(:arguments)))) + @expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot , + LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer , + FunctionCall, Label, RiscTransfer, SlotToReg, SlotToReg, SlotToReg , + RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg end - end - class TestIvarToLocalAssignemnt < MiniTest::Test - include MomCompile - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "a = @a") + + def pest_named_list_get + Parfait.object_space.get_main.add_local(:r , :Integer) + @input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)), s(:return, s(:local, :r))) + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg , + RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + was = check_return + get = was.next(5) + assert_equal SlotToReg , get.class + assert_equal 1 + 1, get.index , "Get to named_list index must be offset, not #{get.index}" end - def test_if_compiles - assert_equal SlotMove , @stats.class , @stats + + def pest_assign_local_int + Parfait.object_space.get_main.add_local(:r , :Integer) + @input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)) ) + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , + RegToSlot, Label, FunctionReturn] + was = check_return + set = was.next(3) + assert_equal RegToSlot , set.class + assert_equal 1 + 1, set.index , "Set to named_list index must be offset, not #{set.index}" end - def test_length - assert_equal 1 , @stats.length + + def pest_misassign_local + Parfait.object_space.get_main.add_local(:r , :Integer) + @input = s(:statements, s(:l_assignment, s(:local, :r), s(:string, "5")) ) + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + assert_raises {check } end - def test_assigns_class - assert_equal Mom::SlotDefinition , @stats.right.class , @stats.inspect + + def pest_assign_arg + Parfait.object_space.get_main.add_argument(:blar , :Integer) + @input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:int, 5))) + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , + RegToSlot, Label, FunctionReturn] + was = check_return + set = was.next(3) + assert_equal RegToSlot , set.class + assert_equal 1 + 1, set.index , "Set to args index must be offset, not #{set.index}" + end + + def pest_misassign_arg + Parfait.object_space.get_main.add_argument(:blar , :Integer) + @input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:string, "5"))) + @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + assert_raises {check } + end + + def pest_arg_get + # have to define bar externally, just because redefining main. Otherwise that would be automatic + Parfait.object_space.get_main.add_argument(:balr , :Integer) + @input = s(:statements, s(:return, s(:arg, :balr))) + @expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg , + RegToSlot, Label, FunctionReturn] + was = check_return + get = was.next(2) + assert_equal SlotToReg , get.class + assert_equal 1 + 1, get.index , "Get to args index must be offset, not #{get.index}" end end end diff --git a/test/mom/test_dynamic_call_statement.rb b/test/mom/test_dynamic_call_statement.rb deleted file mode 100644 index 7d9736f1..00000000 --- a/test/mom/test_dynamic_call_statement.rb +++ /dev/null @@ -1,29 +0,0 @@ -require_relative "helper" - -module Mom - class TestDynamicCall < MiniTest::Test - include MomCompile - - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "a = 5; a.mod4") - @first = @stats.next - end - - def test_if_compiles - assert_equal @stats.last.class , DynamicCall , @stats - end - def test_check - assert_equal NotSameCheck , @first.class - end - def test_condition_is_slot - assert_equal SlotDefinition , @first.left.class , @first - end - def test_length - assert_equal 12 , @stats.length - end - def test_array - check_array [SlotConstant,NotSameCheck,Label,SlotMove,MessageSetup,ArgumentTransfer,SimpleCall,SlotMove,Label,MessageSetup,ArgumentTransfer,DynamicCall] , @stats - end - end -end diff --git a/test/mom/test_if_simple_statement.rb b/test/mom/test_if_simple_statement.rb deleted file mode 100644 index 7eae1144..00000000 --- a/test/mom/test_if_simple_statement.rb +++ /dev/null @@ -1,29 +0,0 @@ -require_relative "helper" - -module Mom - class TestSimpleIfMom < MiniTest::Test - include MomCompile - - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "if(@a == 5) ; 5.mod4 ; end") - @first = @stats.next - end - - def test_if_compiles - assert IfStatement != @stats.class , @stats - end - def test_check - assert_equal TruthCheck , @first.class - end - def test_condition_is_slot - assert_equal SlotDefinition , @first.condition.class , @first - end - def test_length - assert_equal 7 , @stats.length - end - def test_array - check_array [SlotMove,TruthCheck,Label,MessageSetup,ArgumentTransfer,SimpleCall,Label], @stats - end - end -end diff --git a/test/mom/test_if_statement.rb b/test/mom/test_if_statement.rb deleted file mode 100644 index 4afc0efe..00000000 --- a/test/mom/test_if_statement.rb +++ /dev/null @@ -1,30 +0,0 @@ - -require_relative "helper" - -module Mom - class TestConditionIfMom < MiniTest::Test - include MomCompile - - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end") - @first = @stats.next - end - - def test_if_compiles - assert IfStatement != @stats.class , @stats - end - def test_check - assert_equal TruthCheck , @first.class - end - def test_condition_is_slot - assert_equal SlotDefinition , @first.condition.class , @first - end - def test_length - assert_equal 11 , @stats.length - end - def test_array - check_array [SlotMove,TruthCheck,Label,MessageSetup,ArgumentTransfer,SimpleCall,Label,MessageSetup,ArgumentTransfer,SimpleCall,Label] , @stats - end - end -end diff --git a/test/mom/test_instruction.rb b/test/mom/test_instruction.rb deleted file mode 100644 index a0d7eb2f..00000000 --- a/test/mom/test_instruction.rb +++ /dev/null @@ -1,10 +0,0 @@ -require_relative "helper" - -module Mom - class TestInstruction < MiniTest::Test - - def test_class_exists - assert Instruction.new - end - end -end diff --git a/test/mom/test_simple_call_statement.rb b/test/mom/test_simple_call_statement.rb deleted file mode 100644 index a6e6ce29..00000000 --- a/test/mom/test_simple_call_statement.rb +++ /dev/null @@ -1,23 +0,0 @@ -require_relative "helper" - -module Mom - class TestSimpleCall < MiniTest::Test - include MomCompile - - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "5.mod4") - @first = @stats.next - end - - def test_if_compiles - assert_equal @stats.last.class , SimpleCall , @stats - end - def test_method_name - assert_equal @stats.last.method.name , :mod4 , @stats.last.to_rxf - end - def test_array - check_array [MessageSetup,ArgumentTransfer,SimpleCall] , @stats - end - end -end diff --git a/test/mom/test_while_statement.rb b/test/mom/test_while_statement.rb deleted file mode 100644 index 2a363dc9..00000000 --- a/test/mom/test_while_statement.rb +++ /dev/null @@ -1,30 +0,0 @@ - -require_relative "helper" - -module Mom - class TestWhileConditionMom < MiniTest::Test - include MomCompile - - def setup - Risc.machine.boot - @stats = compile_first_method_flat( "while(@a == 5) ; 5.mod4 ; end") - @first = @stats.next.next - end - - def test_if_compiles - assert IfStatement != @stats.class , @stats - end - def test_check - assert_equal TruthCheck , @first.class - end - def test_condition_is_slot - assert_equal SlotDefinition , @first.condition.class , @first - end - def test_length - assert_equal 4 , @stats.length - end - def test_array - check_array [Label,SlotMove,TruthCheck,Label] , @stats - end - end -end diff --git a/test/mom/to_risc/helper.rb b/test/mom/to_risc/helper.rb deleted file mode 100644 index a4091ad3..00000000 --- a/test/mom/to_risc/helper.rb +++ /dev/null @@ -1,80 +0,0 @@ -require_relative '../helper' - -module Risc - module SpaceHack - # test hack to in place change object type - def add_space_field(name,type) - class_type = Parfait.object_space.get_class_by_name(:Space).instance_type - class_type.send(:private_add_instance_variable, name , type) - end - end - module ExpressionHelper - include SpaceHack - - def check - Risc.machine.boot unless Risc.machine.booted - compiler = Vm::MethodCompiler.new Parfait.object_space.get_main - code = Vm.ast_to_code @input - assert code.to_s , @input - produced = compiler.process( code ) - assert @output , "No output given" - assert_equal produced.class , @output , "Wrong class" - produced - end - - end - - module Statements - include AST::Sexp - include CleanCompile - include SpaceHack - - def setup - Risc.machine.boot # force boot to reset main - end - - def preamble - [Label, LoadConstant, SlotToReg, RegToSlot ] - end - def postamble - [ Label, FunctionReturn] - end - def check_nil - assert @expect , "No output given" - Vool::VoolCompiler.ruby_to_vool "class Space; def main(arg);#{@input};end;end" - produced = Parfait.object_space.get_main.instructions - compare_instructions produced , @expect - end - def check_return - was = check_nil - raise was if was - Parfait.object_space.get_main.instructions - end - - def compare_instructions( instruction , expect ) - index = 0 - all = instruction.to_arr - full_expect = preamble + expect + postamble - #full_expect = expect - begin - should = full_expect[index] - return "No instruction at #{index}\n#{should(all)}" unless should - return "Expected at #{index+1}\n#{should(all)}" unless instruction.class == should - puts instruction.to_s - index += 1 - instruction = instruction.next - end while( instruction ) - nil - end - def should( all ) - preamble.each {all.shift} - postamble.each {all.pop} - str = all.to_s.gsub("Risc::","") - ret = "" - str.split(",").each_slice(6).each do |line| - ret += " " + line.join(",") + " ,\n" - end - ret - end - end -end diff --git a/test/mom/to_risc/test_assignment.rb b/test/mom/to_risc/test_assignment.rb deleted file mode 100644 index 53019243..00000000 --- a/test/mom/to_risc/test_assignment.rb +++ /dev/null @@ -1,107 +0,0 @@ - -require_relative 'helper' - -module Risc - class TestAssignStatement < MiniTest::Test - include Statements - - def pest_assign_local_assign - Parfait.object_space.get_main.add_local(:r , :Integer) - @input = "r = 5" - @expect = [LoadConstant, RegToSlot] - assert_nil msg = check_nil , msg - end - - def pest_assign_op - Parfait.object_space.get_main.add_local(:r , :Integer) - @input = "r = 10.mod4" - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - assert_nil msg = check_nil , msg - end - - def pest_assign_ivar_notpresent - @input = "@r = 5" - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - assert_nil msg = check_nil , msg - end - - def pest_assign_ivar - add_space_field(:r , :Integer) - - @input =s(:statements, s(:i_assignment, s(:ivar, :r), s(:int, 5))) - - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , - RegToSlot, Label, FunctionReturn] - assert_nil msg = check_nil , msg - end - - def pest_assign_call - Parfait.object_space.get_main.add_local(:r , :Object) - @input = s(:statements, s(:l_assignment, s(:local, :r), s(:call, :main, s(:arguments)))) - @expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot , - LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer , - FunctionCall, Label, RiscTransfer, SlotToReg, SlotToReg, SlotToReg , - RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - assert_nil msg = check_nil , msg - end - - def pest_named_list_get - Parfait.object_space.get_main.add_local(:r , :Integer) - @input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)), s(:return, s(:local, :r))) - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg , - RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - was = check_return - get = was.next(5) - assert_equal SlotToReg , get.class - assert_equal 1 + 1, get.index , "Get to named_list index must be offset, not #{get.index}" - end - - def pest_assign_local_int - Parfait.object_space.get_main.add_local(:r , :Integer) - @input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)) ) - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , - RegToSlot, Label, FunctionReturn] - was = check_return - set = was.next(3) - assert_equal RegToSlot , set.class - assert_equal 1 + 1, set.index , "Set to named_list index must be offset, not #{set.index}" - end - - def pest_misassign_local - Parfait.object_space.get_main.add_local(:r , :Integer) - @input = s(:statements, s(:l_assignment, s(:local, :r), s(:string, "5")) ) - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - assert_raises {check } - end - - def pest_assign_arg - Parfait.object_space.get_main.add_argument(:blar , :Integer) - @input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:int, 5))) - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg , - RegToSlot, Label, FunctionReturn] - was = check_return - set = was.next(3) - assert_equal RegToSlot , set.class - assert_equal 1 + 1, set.index , "Set to args index must be offset, not #{set.index}" - end - - def pest_misassign_arg - Parfait.object_space.get_main.add_argument(:blar , :Integer) - @input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:string, "5"))) - @expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] - assert_raises {check } - end - - def pest_arg_get - # have to define bar externally, just because redefining main. Otherwise that would be automatic - Parfait.object_space.get_main.add_argument(:balr , :Integer) - @input = s(:statements, s(:return, s(:arg, :balr))) - @expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg , - RegToSlot, Label, FunctionReturn] - was = check_return - get = was.next(2) - assert_equal SlotToReg , get.class - assert_equal 1 + 1, get.index , "Get to args index must be offset, not #{get.index}" - end - end -end diff --git a/test/vool/to_mom/send/test_send_cached_simple.rb b/test/vool/to_mom/send/test_send_cached_simple.rb index bc68cc3a..dee89e5f 100644 --- a/test/vool/to_mom/send/test_send_cached_simple.rb +++ b/test/vool/to_mom/send/test_send_cached_simple.rb @@ -6,7 +6,7 @@ module Vool def setup Risc.machine.boot - @stats = compile_first_method( "arg.mod4").first + @stats = compile_first_method_flat( "a = 5; a.mod4") @first, @second , @third ,@fourth= @stats[0],@stats[1],@stats[2],@stats[3] end @@ -50,5 +50,9 @@ module Vool assert_equal Mom::DynamicCall , @fourth.class , @fourth.to_rxf end + def test_array + check_array [SlotConstant,NotSameCheck,Label,SlotMove,MessageSetup,ArgumentTransfer,SimpleCall,SlotMove,Label,MessageSetup,ArgumentTransfer,DynamicCall] , @stats + end + end end diff --git a/test/vool/to_mom/send/test_send_simple_args.rb b/test/vool/to_mom/send/test_send_simple_args.rb index a8963d7d..22dbf14a 100644 --- a/test/vool/to_mom/send/test_send_simple_args.rb +++ b/test/vool/to_mom/send/test_send_simple_args.rb @@ -22,5 +22,8 @@ module Vool assert_equal Mom::IntegerConstant, @stats[1].arguments[1].right.class assert_equal 2, @stats[1].arguments[1].right.value end + def test_array + check_array [MessageSetup,ArgumentTransfer,SimpleCall] , @stats + end end end diff --git a/test/vool/to_mom/test_if_simple.rb b/test/vool/to_mom/test_if_simple.rb index 3f78635e..803569d2 100644 --- a/test/vool/to_mom/test_if_simple.rb +++ b/test/vool/to_mom/test_if_simple.rb @@ -26,5 +26,8 @@ module Vool def test_nothing_hoisted assert_nil @first.hoisted , @stats end + def test_array + check_array [SlotMove,TruthCheck,Label,MessageSetup,ArgumentTransfer,SimpleCall,Label], @stats + end end end diff --git a/test/vool/to_mom/test_while_simple.rb b/test/vool/to_mom/test_while_simple.rb index 7c23b0bb..978cf48a 100644 --- a/test/vool/to_mom/test_while_simple.rb +++ b/test/vool/to_mom/test_while_simple.rb @@ -26,5 +26,8 @@ module Vool def test_nothing_hoisted assert_nil @first.hoisted , @stats end + def test_array + check_array [Label,SlotMove,TruthCheck,Label] , @stats + end end end