copy and other improvements for word
more tests reveal a bug (fixed) and more docs
This commit is contained in:
parent
06cfba7c58
commit
d71547ea01
@ -15,6 +15,7 @@ module Parfait
|
|||||||
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
|
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
|
||||||
class Word < Object
|
class Word < Object
|
||||||
# initialize with length. For now we try to keep all non-parfait (including String) out
|
# initialize with length. For now we try to keep all non-parfait (including String) out
|
||||||
|
# String will contain spaces for non-zero length
|
||||||
# Virtual provides methods to create Parfait objects from ruby
|
# Virtual provides methods to create Parfait objects from ruby
|
||||||
def initialize len
|
def initialize len
|
||||||
super()
|
super()
|
||||||
@ -23,38 +24,62 @@ module Parfait
|
|||||||
set_length( len , 32 ) unless len == 0
|
set_length( len , 32 ) unless len == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# return a copy of self
|
||||||
|
def copy
|
||||||
|
cop = Word.new( self.length )
|
||||||
|
index = 1
|
||||||
|
while( index <= self.length )
|
||||||
|
cop.set_char(index , self.get_char(index))
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
cop
|
||||||
|
end
|
||||||
|
|
||||||
|
# return the number of characters
|
||||||
def length()
|
def length()
|
||||||
obj_len = internal_object_length - 1
|
obj_len = internal_object_length - 1
|
||||||
return obj_len
|
return obj_len
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# true if no characters
|
||||||
def empty?
|
def empty?
|
||||||
return self.length == 0
|
return self.length == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# pad the string with the given character to the given length
|
||||||
|
#
|
||||||
def set_length(len , fill_char)
|
def set_length(len , fill_char)
|
||||||
return if len <= 0
|
return if len <= 0
|
||||||
counter = self.length()
|
counter = self.length()
|
||||||
return if counter >= len
|
return if counter >= len
|
||||||
internal_object_grow( len + 1)
|
internal_object_grow( len + 1)
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
while( counter < len)
|
while( counter <= len)
|
||||||
set_char( counter , fill_char)
|
set_char( counter , fill_char)
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# set the character at the given index to the given character
|
||||||
|
# character must be an integer, as is the index
|
||||||
|
# the index starts at one, but may be negative to count from the end
|
||||||
|
# indexes out of range will raise an error
|
||||||
def set_char at , char
|
def set_char at , char
|
||||||
raise "char not fixnum #{char}" unless char.kind_of? Fixnum
|
raise "char not fixnum #{char}" unless char.kind_of? Fixnum
|
||||||
index = range_correct_index(at)
|
index = range_correct_index(at)
|
||||||
internal_object_set( index + 1 , char )
|
internal_object_set( index + 1 , char )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# get the character at the given index
|
||||||
|
# the index starts at one, but may be negative to count from the end
|
||||||
|
# indexes out of range will raise an error
|
||||||
|
#the return "character" is an integer
|
||||||
def get_char at
|
def get_char at
|
||||||
index = range_correct_index(at)
|
index = range_correct_index(at)
|
||||||
return internal_object_get(index + 1)
|
return internal_object_get(index + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# private method to calculate negative indexes into positives
|
||||||
def range_correct_index at
|
def range_correct_index at
|
||||||
index = at
|
index = at
|
||||||
index = self.length + at if at < 0
|
index = self.length + at if at < 0
|
||||||
@ -63,36 +88,31 @@ module Parfait
|
|||||||
return index
|
return index
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# compare the word to another
|
||||||
|
# currently checks for same class, though really identity of the characters
|
||||||
|
# in right order would suffice
|
||||||
def == other
|
def == other
|
||||||
return false if other.class != self.class
|
return false if other.class != self.class
|
||||||
return false if other.length != self.length
|
return false if other.length != self.length
|
||||||
len = self.length
|
len = self.length
|
||||||
while(len >= 0)
|
while(len > 0)
|
||||||
return false if self.get_char(len) != other.get_char(len)
|
return false if self.get_char(len) != other.get_char(len)
|
||||||
len = len - 1
|
len = len - 1
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# this is a sof check if there are instance variables or "structure"
|
||||||
def is_value?
|
def is_value?
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# as we answered is_value? with true, sof will create a basic node with this string
|
||||||
def to_sof
|
def to_sof
|
||||||
"Parfait::Word('#{to_s}')"
|
"Parfait::Word('#{to_s}')"
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
#below here is OLD, DUBIOUS and needs to be checked TODO
|
||||||
# this should call parfait get_class, alas that is not implemented yet
|
|
||||||
return false if other.class != self.class
|
|
||||||
return false if other.length != self.length
|
|
||||||
index = self.length
|
|
||||||
while(index > 0)
|
|
||||||
return false if other.get_char(index) != self.get_char(index)
|
|
||||||
index = index - 1
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
def result= value
|
def result= value
|
||||||
raise "called"
|
raise "called"
|
||||||
class_for(MoveInstruction).new(value , self , :opcode => :mov)
|
class_for(MoveInstruction).new(value , self , :opcode => :mov)
|
||||||
|
@ -30,6 +30,20 @@ class TestWord < MiniTest::Test
|
|||||||
def test_len
|
def test_len
|
||||||
assert_equal 5 , @word.length
|
assert_equal 5 , @word.length
|
||||||
end
|
end
|
||||||
|
def test_first_char
|
||||||
|
assert_equal 32 , @word.get_char(1)
|
||||||
|
end
|
||||||
|
def test_equals_copy
|
||||||
|
assert_equal @word.copy , @word
|
||||||
|
end
|
||||||
|
def test_equals_copy2
|
||||||
|
@word.set_char(1 , 2)
|
||||||
|
@word.set_char(5 , 6)
|
||||||
|
assert_equal @word.copy , @word
|
||||||
|
end
|
||||||
|
def test_equals_same
|
||||||
|
assert_equal ::Parfait::Word.new_object(5) , @word
|
||||||
|
end
|
||||||
def test_index_check_get
|
def test_index_check_get
|
||||||
assert_raises RuntimeError do
|
assert_raises RuntimeError do
|
||||||
@word.get_char(-6)
|
@word.get_char(-6)
|
||||||
@ -53,38 +67,28 @@ class TestWord < MiniTest::Test
|
|||||||
def test_one_char
|
def test_one_char
|
||||||
assert_equal 32 , @word.set_char(1 , 32)
|
assert_equal 32 , @word.set_char(1 , 32)
|
||||||
end
|
end
|
||||||
def test_one_char_doesnt_cuase_problems
|
def test_one_char_doesnt_cause_problems
|
||||||
@word.set_char(1 , 32)
|
@word.set_char(1 , 32)
|
||||||
assert_equal 32 , @word.get_char(1)
|
assert_equal 32 , @word.get_char(1)
|
||||||
end
|
end
|
||||||
def pest_empty_word_doesnt_return
|
def test_one_set1
|
||||||
assert_equal nil , @word.get(3)
|
assert_equal 44 , @word.set_char(1, 44 )
|
||||||
end
|
end
|
||||||
def pest_one_set0
|
def test_two_sets
|
||||||
assert_equal 1 , @word.set(0,1)
|
assert_equal 1 , @word.set_char(1,1)
|
||||||
|
assert_equal 44 , @word.set_char(1,44)
|
||||||
end
|
end
|
||||||
def pest_one_set1
|
def test_one_get1
|
||||||
assert_equal :some , @word.set(1,:some)
|
test_one_set1
|
||||||
|
assert_equal 44 , @word.get_char(1)
|
||||||
end
|
end
|
||||||
def pest_two_sets
|
def test_many_get
|
||||||
assert_equal 1 , @word.set(0,1)
|
shouldda = { 1 => 11 , 2 => 22 , 3 => 33}
|
||||||
assert_equal :some , @word.set(1,:some)
|
|
||||||
end
|
|
||||||
def pest_one_get1
|
|
||||||
pest_one_set0
|
|
||||||
assert_equal 1 , @word.get(0)
|
|
||||||
end
|
|
||||||
def pest_one_get2
|
|
||||||
pest_one_set1
|
|
||||||
assert_equal :some , @word.get(1)
|
|
||||||
end
|
|
||||||
def pest_many_get
|
|
||||||
shouldda = { 1 => :one , 2 => :two , 3 => :three}
|
|
||||||
shouldda.each do |k,v|
|
shouldda.each do |k,v|
|
||||||
@word.set(k,v)
|
@word.set_char(k,v)
|
||||||
end
|
end
|
||||||
shouldda.each do |k,v|
|
shouldda.each do |k,v|
|
||||||
assert_equal v , @word.get(k)
|
assert_equal v , @word.get_char(k)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user