fix object compiletime
and write some tests for it
This commit is contained in:
parent
fe2be323d8
commit
8fa501fb91
@ -18,11 +18,14 @@ require "parfait/space"
|
|||||||
|
|
||||||
module FakeMem
|
module FakeMem
|
||||||
def initialize
|
def initialize
|
||||||
@memory = []
|
@memory = [0]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Parfait
|
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
|
class Object
|
||||||
include FakeMem
|
include FakeMem
|
||||||
def self.new_object *args
|
def self.new_object *args
|
||||||
@ -31,17 +34,33 @@ module Parfait
|
|||||||
object = self.new(*args)
|
object = self.new(*args)
|
||||||
object
|
object
|
||||||
end
|
end
|
||||||
def internal_object_length
|
# these internal functions are _really_ internal
|
||||||
@memory.length
|
# 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
|
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)
|
def internal_object_get(index)
|
||||||
@memory[index]
|
@memory[index]
|
||||||
end
|
end
|
||||||
|
# 1 -based index
|
||||||
def internal_object_set(index , value)
|
def internal_object_set(index , value)
|
||||||
@memory[index] = value
|
@memory[index] = value
|
||||||
end
|
end
|
||||||
def internal_object_grow(index)
|
def internal_object_grow(length)
|
||||||
@memory[index] = nil
|
old_length = internal_object_length()
|
||||||
|
while( old_length < length )
|
||||||
|
internal_object_set( old_length + 1, nil)
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class Parfait::Class
|
class Parfait::Class
|
||||||
|
54
test/parfait/test_object.rb
Normal file
54
test/parfait/test_object.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user