$LOAD_PATH.unshift(File.dirname(__FILE__)) require 'test_helper' =begin rdoc This file has tests of the code written for the CoreWar project: an assembler for "Redcode" programs, and a "MARS" virtual machine emulator. The should probably be a few more tests of individual components, but the tests here just make sure the whole system operates as expected. For example, the instruction tests make arrays of strings containing source instructions, pass them to the assembler, and execute the resulting instructions. The tests don't look too closely at the result, e.g. to make sure all the symbols are in the symbol table, or each instruction has the right components -- the test just makes sure the instructions do what they are expected to do. Tests in this file: * an assembler test looks for syntax errors on ill-formed statements * another assembler test looks at the code for a small, valid program * an address mode test makes sure MOV instructions copy a piece of data to the expected location using each of the four addressing modes * individual instruction tests for each machine instruction NOTE: to test that instructions fetched from memory have the expected value the assertions compare the result of instr.to_s to a string. Things will break horribly if the to_s method changes the format.... NOTE: Be careful when making test programs -- there needs to be a label or a space before each opcode, e.g. [" DAT #0, <1", " DAT #0, #1"] =end include MARSLab class TestMARS < Test::Unit::TestCase def test_00_banner print "\nMARSLab" end # Assembling these instructions should lead to one error per line, and # no code generated. def test_01_syntax_errors source = [ "@label mov #0, #1 ; label needs to be alphanumeric", "l@bel mov #0, #1 ; label needs to be alphanumeric", "mov #0, #1 ; unlabeled line must start with space", "label @bogus #0, #1 ; opcode with non-alphanumeric", "label b@gus #0, #1 ; opcode with non-alphanumeric", "label muv #0, #1 ; unknown opcode", " mov #b@gus, #1 ; messed up operand", " mov #0 #1 ; missing comma", " EQU 3 ; missing label on pseudo-op", "x EQU 3x ; arg not an integer", " END foo ; undefined label", ] name, code, symbols, errors = MARS.assemble(source) assert_equal source.length, errors.length # every line generates an error assert_equal 0, code.length # should be no instructions generated end # A program from the ICWS archives -- has a wide variety of instructions, each # addressing mode, and a pseudo-op. Should produce no errors, and one machine # instruction for each operation (inclduing DAT). def test_02_assembly source = [ ";redcode", ";name Mice", ";author Chip Wendell", ";strategy 1st place in ICWST'86", ";strategy replicator", ";assert 1", "PTR DAT #0", "START MOV #12 ,PTR", "LOOP MOV @PTR ,