Permalink
Browse files

Refactor tests

  • Loading branch information...
1 parent 21d480b commit c3e41eb996e7ec519401b9a4b5ac8a2ed422c129 @judofyr committed Apr 7, 2012
Showing with 153 additions and 132 deletions.
  1. +8 −0 Rakefile
  2. +38 −0 test/helper.rb
  3. +105 −0 test/test_assembler.rb
  4. +2 −132 test/test_emulator.rb
View
@@ -0,0 +1,8 @@
+require 'rake/testtask'
+
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.libs << 'test'
+end
+
View
@@ -0,0 +1,38 @@
+$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
+
+require 'minitest/autorun'
+require 'rcpu'
+
+module RCPU
+ class TestCase < MiniTest::Unit::TestCase
+ def rcpu(run = true, &blk)
+ lib = Library.new
+ lib.instance_eval(&blk)
+ linker = Linker.new
+ linker.compile(lib)
+ @emu = Emulator.new(linker.finalize)
+ @emu.memory.add_extensions(linker.extensions)
+ @emu.run if run
+ end
+
+ def memory
+ @emu.memory
+ end
+
+ def register(name)
+ @emu.registers[name]
+ end
+
+ def block(&blk)
+ rcpu do
+ block :main do
+ instance_eval(&blk)
+
+ label :crash
+ SET pc, :crash
+ end
+ end
+ end
+ end
+end
+
View
@@ -0,0 +1,105 @@
+require 'helper'
+
+module RCPU
+ class TestAssembler < TestCase
+ def test_bytedata
+ block do
+ SET a, [:hello]
+ SET [:hello], 123
+ SET b, [:hello]
+ SET pc, :crash
+
+ data :hello, [12]
+ end
+
+ assert_equal 12, register(:A)
+ assert_equal 123, register(:B)
+ end
+
+ def test_zerodata
+ block do
+ SET i, 1
+
+ SET a, 1
+ SET a, [:hello]
+
+ SET b, 1
+ SET b, [i+:hello]
+ SET pc, :crash
+
+ data :hello, 2
+ end
+
+ assert_equal 0, register(:A)
+ assert_equal 0, register(:B)
+ end
+
+ def test_stringdata
+ block do
+ SET i, 1
+ SET a, [:hello]
+ SET b, [i+:hello]
+ SET pc, :crash
+
+ data :hello, "ab"
+ end
+
+ assert_equal "a".ord, register(:A)
+ assert_equal "b".ord, register(:B)
+ end
+
+ def test_unknowndata
+ assert_raises(AssemblerError, "unkown data type") do
+ block do
+ data :hello, :nope
+ end
+ end
+ end
+
+ def test_missing_label
+ assert_raises(AssemblerError, "no label: fail") do
+ block do
+ SET pc, :fail
+ end
+ end
+ end
+
+ def test_multiple_blocks
+ rcpu do
+ block :main do
+ JSR :_another
+ label :crash
+ SET pc, :crash
+ end
+
+ block :another do
+ SET a, 1
+ SET pc, pop
+ end
+ end
+
+ assert_equal 1, register(:A)
+ end
+
+ def test_missing_block
+ assert_raises(AssemblerError, "no external label: another") do
+ block do
+ JSR :_another
+ end
+ end
+ end
+
+ def test_data_label
+ block do
+ SUB pc, 1
+ data :sp, [:stack]
+ data :stack, 512
+ data :sp2, [:stack]
+ end
+
+ assert_equal 0x2, memory[0x1]
+ assert_equal 0x2, memory[0x1+512+0x1]
+ end
+
+ end
+end
View
@@ -1,39 +1,7 @@
-$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
-
-require 'minitest/autorun'
-require 'rcpu'
+require 'helper'
module RCPU
- class TestEmulator < MiniTest::Unit::TestCase
- def rcpu(run = true, &blk)
- lib = Library.new
- lib.instance_eval(&blk)
- linker = Linker.new
- linker.compile(lib)
- @emu = Emulator.new(linker.finalize)
- @emu.memory.add_extensions(linker.extensions)
- @emu.run if run
- end
-
- def memory
- @emu.memory
- end
-
- def register(name)
- @emu.registers[name]
- end
-
- def block(&blk)
- rcpu do
- block :main do
- instance_eval(&blk)
-
- label :crash
- SET pc, :crash
- end
- end
- end
-
+ class TestEmulator < TestCase
def test_parse
ins = []
rcpu(false) do
@@ -271,92 +239,6 @@ def test_stack
assert_equal 0, register(:X)
end
- def test_bytedata
- block do
- SET a, [:hello]
- SET [:hello], 123
- SET b, [:hello]
- SET pc, :crash
-
- data :hello, [12]
- end
-
- assert_equal 12, register(:A)
- assert_equal 123, register(:B)
- end
-
- def test_zerodata
- block do
- SET i, 1
-
- SET a, 1
- SET a, [:hello]
-
- SET b, 1
- SET b, [i+:hello]
- SET pc, :crash
-
- data :hello, 2
- end
-
- assert_equal 0, register(:A)
- assert_equal 0, register(:B)
- end
-
- def test_stringdata
- block do
- SET i, 1
- SET a, [:hello]
- SET b, [i+:hello]
- SET pc, :crash
-
- data :hello, "ab"
- end
-
- assert_equal "a".ord, register(:A)
- assert_equal "b".ord, register(:B)
- end
-
- def test_unknowndata
- assert_raises(AssemblerError, "unkown data type") do
- block do
- data :hello, :nope
- end
- end
- end
-
- def test_missing_label
- assert_raises(AssemblerError, "no label: fail") do
- block do
- SET pc, :fail
- end
- end
- end
-
- def test_multiple_blocks
- rcpu do
- block :main do
- JSR :_another
- label :crash
- SET pc, :crash
- end
-
- block :another do
- SET a, 1
- SET pc, pop
- end
- end
-
- assert_equal 1, register(:A)
- end
-
- def test_missing_block
- assert_raises(AssemblerError, "no external label: another") do
- block do
- JSR :_another
- end
- end
- end
class VoidExtension
def initialize(array, start)
@@ -423,18 +305,6 @@ def test_format
end
end
- def test_data_label
- block do
- SUB pc, 1
- data :sp, [:stack]
- data :stack, 512
- data :sp2, [:stack]
- end
-
- assert_equal 0x2, memory[0x1]
- assert_equal 0x2, memory[0x1+512+0x1]
- end
-
def test_parse_number
rcpu do
library :parse

0 comments on commit c3e41eb

Please sign in to comment.