some binary code tests

This commit is contained in:
Torsten Ruger 2018-03-26 14:04:13 +03:00
parent 633e99466d
commit 60617ca632
3 changed files with 90 additions and 34 deletions

View File

@ -1,18 +1,18 @@
# A typed method object is a description of the method, it's name etc
#
# But the code that the method represents, the binary, is held as an array
# in one of these.
#
module Parfait module Parfait
# A typed method object is a description of the method, it's name etc
#
# But the code that the method represents, the binary, is held as an array
# in these. As Objects are fixed size (this one 16 words), we use linked list
# and as the last code of each link is a jump to the next link.
#
class BinaryCode < Data16 class BinaryCode < Data16
attr_accessor :next attr_reader :next
def initialize(total_size) def initialize(total_size)
super() super()
if total_size > self.data_length if total_size > self.data_length
@next = BinaryCode.new(total_size - data_length) @next = BinaryCode.new(total_size - data_length - 1) #one for the jump
end end
#puts "Init with #{total_size} for #{object_id}" #puts "Init with #{total_size} for #{object_id}"
end end
@ -22,18 +22,18 @@ module Parfait
def data_length def data_length
14 14
end end
def char_length def byte_length
4*data_length 4*data_length
end end
def set_char(c , index) def set_char(index , char)
if index >= char_length if index >= byte_length
puts "Pass it on #{index} for #{object_id}" puts "Pass it on #{index} for #{object_id}"
return @next.set_char( c , index - char_length) return @next.set_char( char , index - byte_length)
end end
word_index = (index - 1) / 4 + 2 word_index = (index - 1) / 4 + 2
old = get_internal_word( word_index ) old = get_internal_word( word_index )
old = old && c << ((index-1)%4) old = old && char << ((index-1)%4)
set_internal_word(word_index , c) set_internal_word(word_index , char)
end end
def total_byte_length(start = 0 ) def total_byte_length(start = 0 )
start += 4*14 start += 4*14

View File

@ -0,0 +1,51 @@
require_relative "../helper"
module Parfait
class TestBinaryCode < MiniTest::Test
def setup
Risc.machine.boot
@code = BinaryCode.new(10)
end
def test_class
assert_equal :BinaryCode, @code.get_type.object_class.name
end
def test_var_names
assert_equal List , @code.get_instance_variables.class
end
def test_var_names_length
assert_equal 2 , @code.get_instance_variables.get_length
end
def test_var_next
assert_equal :next , @code.get_instance_variables[2]
end
def test_next_nil
assert_nil @code.next
end
def test_data_length
assert_equal 14 , @code.data_length
end
def test_byte_length
assert_equal 14*4 , @code.byte_length
end
def test_next_not_nil
@code = BinaryCode.new(16)
assert @code.next
assert_nil @code.next.next
end
def test_set_char1
assert @code.set_char(1 , 1)
end
def test_set_char55
assert @code.set_char(55 , 1)
end
def test_set_char56_raises
assert_raises {@code.set_char(56 , 1)}
end
def test_set_char56_double
@code = BinaryCode.new(16)
assert @code.set_char(56 , 1)
end
end
end

View File

@ -1,27 +1,32 @@
require_relative "../helper" require_relative "../helper"
class TestNamedLists < MiniTest::Test module Parfait
class TestNamedLists < MiniTest::Test
def setup def setup
Risc.machine.boot Risc.machine.boot
@space = Parfait.object_space @space = Parfait.object_space
@named_list = @space.first_message.frame @named_list = @space.first_message.frame
@type = @named_list.get_type @type = @named_list.get_type
end end
def test_named_list_get_type def test_named_list_get_type
assert_equal Parfait::Type , @type.class assert_equal Type , @type.class
assert @type.names assert @type.names
assert @named_list.get_instance_variables assert @named_list.get_instance_variables
end end
def test_new def test_new
list = Parfait::NamedList.new list = NamedList.new
assert list.get_type assert list.get_type
end end
def test_var_names
def test_var_names list = NamedList.new
list = Parfait::NamedList.new assert_equal List , list.get_instance_variables.class
assert list.get_instance_variables end
def test_var_names_length
list = NamedList.new
assert_equal 1 , list.get_instance_variables.get_length
end
end end
end end