From 8fa501fb91362e940b739a0336780f284370c94d Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 17 May 2015 14:38:25 +0300 Subject: [PATCH] fix object compiletime and write some tests for it --- lib/parfait.rb | 29 ++++++++++++++++---- test/parfait/test_object.rb | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 test/parfait/test_object.rb diff --git a/lib/parfait.rb b/lib/parfait.rb index af91ada7..16144039 100644 --- a/lib/parfait.rb +++ b/lib/parfait.rb @@ -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 diff --git a/test/parfait/test_object.rb b/test/parfait/test_object.rb new file mode 100644 index 00000000..52e32272 --- /dev/null +++ b/test/parfait/test_object.rb @@ -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