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()
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
def empty?
@ -46,24 +52,14 @@ module Parfait
# private method
def key_index(key)
len = @keys.get_length()
index = 1
found = nil
while(index <= len)
if( @keys.get(index) == key)
found = index
break
end
index += 1
end
found
@keys.index_of(key)
end
# set key with value, returns value
def set(key , value)
index = key_index(key)
if( index )
@keys.set(index , value)
@values.set(index , value)
else
@keys.push(key)
@values.push(value)

View File

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

View File

@ -60,11 +60,17 @@ class TestDictionary < MiniTest::Test
assert_equal v , shouldda[k]
end
end
def test_values
assert @lookup.values
@lookup[2] = 2
assert @lookup.values.get_length == 1
end
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