fixed silly dictionary bug

also finally duplicating keys and values and not handing them out
This commit is contained in:
Torsten Ruger 2016-12-30 20:46:18 +02:00
parent a7935db107
commit ef66a87527
3 changed files with 27 additions and 16 deletions

View File

@ -15,7 +15,13 @@ module Parfait
@values = List.new() @values = List.new()
end end
attr_reader :values , :keys #FIXME these should be dupped, not handed out def keys
@keys.dup
end
def values
@values.dup
end
# are there any key/value items in the list # are there any key/value items in the list
def empty? def empty?
@ -46,24 +52,14 @@ module Parfait
# private method # private method
def key_index(key) def key_index(key)
len = @keys.get_length() @keys.index_of(key)
index = 1
found = nil
while(index <= len)
if( @keys.get(index) == key)
found = index
break
end
index += 1
end
found
end end
# set key with value, returns value # set key with value, returns value
def set(key , value) def set(key , value)
index = key_index(key) index = key_index(key)
if( index ) if( index )
@keys.set(index , value) @values.set(index , value)
else else
@keys.push(key) @keys.push(key)
@values.push(value) @values.push(value)

View File

@ -33,6 +33,15 @@ module Parfait
def to_sof_node(writer , level , ref ) def to_sof_node(writer , level , ref )
Sof.array_to_sof_node(self , writer , level , ref ) Sof.array_to_sof_node(self , writer , level , ref )
end end
def dup
list = List.new
each do |item|
list.push(item)
end
list
end
def to_a def to_a
array = [] array = []
index = 1 index = 1

View File

@ -60,11 +60,17 @@ class TestDictionary < MiniTest::Test
assert_equal v , shouldda[k] assert_equal v , shouldda[k]
end end
end end
def test_values def test_values
assert @lookup.values @lookup[2] = 2
assert @lookup.values.get_length == 1
end end
def test_keys def test_keys
assert @lookup.keys @lookup[2] = 2
assert @lookup.keys.get_length == 1
end
def test_override_exising
@lookup[2] = 2
@lookup[2] = :two
assert @lookup[2] == :two
end end
end end