fix object compiletime

and write some tests for it
This commit is contained in:
Torsten Ruger 2015-05-17 14:38:25 +03:00
parent fe2be323d8
commit 8fa501fb91
2 changed files with 78 additions and 5 deletions

View File

@ -18,11 +18,14 @@ require "parfait/space"
module FakeMem
def initialize
@memory = []
@memory = [0]
end
end
module Parfait
# Objects memory functions. Object memory is 1 based
# but we implement it with ruby array (0 based) and use 0 as type-word
# These are the same functions that Builtin implements at run-time
class Object
include FakeMem
def self.new_object *args
@ -31,17 +34,33 @@ module Parfait
object = self.new(*args)
object
end
def internal_object_length
@memory.length
# these internal functions are _really_ internal
# they respresent the smallest code needed to build larger functionality
# but should _never_ be used outside parfait. in fact that should be impossible
def internal_object_get_typeword
@memory[0]
end
def internal_object_set_typeword w
@memory[0] = w
end
def internal_object_length
@memory.length - 1 # take of type-word
end
# 1 -based index
def internal_object_get(index)
@memory[index]
end
# 1 -based index
def internal_object_set(index , value)
@memory[index] = value
end
def internal_object_grow(index)
@memory[index] = nil
def internal_object_grow(length)
old_length = internal_object_length()
while( old_length < length )
internal_object_set( old_length + 1, nil)
end
end
end
class Parfait::Class

View File

@ -0,0 +1,54 @@
require_relative "../helper"
class TestObject < MiniTest::Test
def setup
@object = ::Parfait::Object.new_object
end
def test_list_create
assert_equal 0 , @object.internal_object_length
end
def test_empty_list_doesnt_return
assert_equal nil , @object.internal_object_get(3)
end
def test_one_set1
assert_equal 1 , @object.internal_object_set(1,1)
end
def test_set1_len
@object.internal_object_set(1,1)
assert_equal 1 , @object.internal_object_length
end
def test_one_set2
assert_equal :some , @object.internal_object_set(2,:some)
end
def test_set2_len
@object.internal_object_set(2,:some)
assert_equal 2 , @object.internal_object_length
end
def test_two_sets
assert_equal 1 , @object.internal_object_set(1,1)
assert_equal :some , @object.internal_object_set(1,:some)
end
def test_one_get1
test_one_set1
assert_equal 1 , @object.internal_object_get(1)
end
def test_one_get2
test_one_set2
assert_equal :some , @object.internal_object_get(2)
end
def test_many_get
shouldda = { 1 => :one , 2 => :two , 3 => :three}
shouldda.each do |k,v|
@object.internal_object_set(k,v)
end
shouldda.each do |k,v|
assert_equal v , @object.internal_object_get(k)
end
end
end