From a9d5e144caec00dc71d2d8baeab70b91973b0e33 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 26 Mar 2018 18:14:52 +0300 Subject: [PATCH] get/set word for binary code --- lib/parfait/binary_code.rb | 19 ++++++++++++++++++- test/parfait/test_binary_code.rb | 13 +++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/parfait/binary_code.rb b/lib/parfait/binary_code.rb index 14522ed2..06f0ee48 100644 --- a/lib/parfait/binary_code.rb +++ b/lib/parfait/binary_code.rb @@ -14,7 +14,8 @@ module Parfait if total_size > self.data_length @next = BinaryCode.new(total_size - data_length) #one for the jump end - puts "Init with #{total_size} for #{object_id}" + #puts "Init with #{total_size} for #{object_id}" + (1..(data_length+1)).each{ |index| set_word(index , 0) } end def to_s "BinaryCode #{}" @@ -25,6 +26,22 @@ module Parfait def byte_length 4*data_length end + def set_word(index , word) + raise "invalid index #{index}" if index < 1 + if index > data_length + 1 + raise "invalid index #{index}" unless @next + @next.set_word( index - data_length , word) + end + set_internal_word(index + 2 , word) + end + def get_word(index) + raise "invalid index #{index}" if index < 1 + if index > data_length + 1 + raise "invalid index #{index}" unless @next + return @next.get_word( index - data_length) + end + get_internal_word(index + 2) + end def set_char(index , char) if index >= byte_length puts "Pass it on #{index} for #{object_id}" diff --git a/test/parfait/test_binary_code.rb b/test/parfait/test_binary_code.rb index 0d9b6e5a..1f45b535 100644 --- a/test/parfait/test_binary_code.rb +++ b/test/parfait/test_binary_code.rb @@ -51,5 +51,18 @@ module Parfait @code = BinaryCode.new(16) assert @code.set_char(56 , 1) end + def test_nilled + assert_equal 0 , @code.get_word(1) + assert_equal 0 , @code.get_word(14) + end + def test_get_set_self + @code.set_word(10,1) + assert_equal 1 , @code.get_word(10) + end + def test_get_set_next + @code = BinaryCode.new(20) + @code.set_word(20,1) + assert_equal 1 , @code.get_word(20) + end end end