add a each_pair to indexed and use in types

This commit is contained in:
Torsten Ruger 2016-12-07 23:35:51 +02:00
parent 266a04040b
commit 2741f35380
5 changed files with 47 additions and 8 deletions

View File

@ -103,8 +103,6 @@ module Parfait
end end
def each def each
# not sure how to do this with define_method, because of the double block issue.
# probably some clever way around that, but not important
index = 1 index = 1
while index <= self.get_length while index <= self.get_length
item = get(index) item = get(index)
@ -114,6 +112,17 @@ module Parfait
self self
end end
def each_pair
index = 1
while index <= self.get_length
key = get( index )
value = get(index + 1)
yield key , value
index = index + 2
end
self
end
def set_length len def set_length len
was = self.get_length was = self.get_length
return if was == len return if was == len

View File

@ -46,6 +46,7 @@ module Parfait
def get_internal_word(index) def get_internal_word(index)
@memory[index] @memory[index]
end end
# 1 -based index # 1 -based index
def set_internal_word(index , value) def set_internal_word(index , value)
raise "failed init for #{self.class}" unless @memory raise "failed init for #{self.class}" unless @memory
@ -132,6 +133,7 @@ module Parfait
def instance_variables def instance_variables
get_instance_variables.to_a.collect{ |n| "@#{n}".to_sym } get_instance_variables.to_a.collect{ |n| "@#{n}".to_sym }
end end
# name comes in as a ruby @var name # name comes in as a ruby @var name
def instance_variable_get name def instance_variable_get name
var = get_instance_variable name.to_s[1 .. -1].to_sym var = get_instance_variable name.to_s[1 .. -1].to_sym

View File

@ -66,10 +66,8 @@ module Parfait
def instance_names def instance_names
names = List.new names = List.new
name = true each_pair do |name , type|
each do |item| names.push(name)
names.push(item) if name
name = ! name
end end
names names
end end
@ -110,5 +108,8 @@ module Parfait
nil # stop resolve recursing up metaclasses nil # stop resolve recursing up metaclasses
end end
def hash
h = name.hash
end
end end
end end

View File

@ -17,7 +17,7 @@ class TestAttributes < MiniTest::Test
end end
def test_message_name_nil def test_message_name_nil
last = @type.instance_names.last last = @type.instance_names.last
assert_equal :indexed_length , last assert_equal :indexed_length , last , @type.instance_names.inspect
assert_nil @mess.name assert_nil @mess.name
end end
def test_message_next_set def test_message_next_set

View File

@ -95,15 +95,42 @@ class TestList < MiniTest::Test
test_one_set2 test_one_set2
assert_equal :some , @list.get(2) assert_equal :some , @list.get(2)
end end
def test_many_get def set_shouldda
shouldda = { 1 => :one , 2 => :two , 3 => :three} shouldda = { 1 => :one , 2 => :two , 3 => :three}
shouldda.each do |k,v| shouldda.each do |k,v|
@list.set(k,v) @list.set(k,v)
end end
shouldda
end
def test_many_get
shouldda = set_shouldda
shouldda.each do |k,v| shouldda.each do |k,v|
assert_equal v , @list.get(k) assert_equal v , @list.get(k)
end end
end end
def test_each
shouldda_values = set_shouldda.values
@list.each do |val|
shouldda_values.delete val
end
assert_equal 0 , shouldda_values.length
end
def test_each_pair_length
shouldda_values = set_shouldda.values
@list.each_pair do |key,val|
shouldda_values.delete key
shouldda_values.delete val
end
assert_equal 0 , shouldda_values.length
end
def test_each_pair_count
set_shouldda.values
counter = 0
@list.each_pair do |key,val|
counter += 1
end
assert_equal 2 , counter
end
def test_delete_at def test_delete_at
test_many_get test_many_get
assert @list.delete_at 2 assert @list.delete_at 2