#!/usr/local/bin/lua -- load up the object machinery! dofile("obj_machinery.lua"); -- ########################################################### Object = { __CLASS__ = "Object", new = function (self) return { _parents = { self } , } end } AABase = { __CLASS__ = "AABase", aa_value = "aa" } ABase = { __CLASS__ = "ABase", _parents = { AABase }, a_value = "a" } BBase = { __CLASS__ = "BBase", b_value = "b" } TestObj = { _parents = { Object, ABase, BBase }, my_data = 1, instance_data_access_test = function (self,count) local i = 0 local a self.my_data_here = 1 while i < count do a = self.my_data_here i = i + 1 end end, cached_parent_data_access_test = function (self,count) local i = 0 local a self.my_data_here = 1 while i < count do a = self.b_value i = i + 1 end end, missing_instance_data_access_test = function (self,count) local i = 0 local a while i < count do a = self.my_data2 i = i + 1 end end, }; function regression_test(an_obj) print("REGRESSION test starting...") if an_obj.a_value ~= "a" then error("a_value didn't read correctly") end if an_obj.b_value ~= "b" then error("b_value didn't read correctly") end if an_obj.aa_value ~= "aa" then error("aa_value didn't read correctly") end an_obj.a_value = "c" if an_obj.a_value ~= "c" then error("a_value couldn't be written") end -- this is very weird, but it's one of -- the side effects of Lua's "nil" meaning -- absent style. an_obj.a_value = nil if an_obj.a_value ~= "a" then error("inherited a_value didn't reappear") end print("REGRESSION PASSED!") end -- ########################################################### function timemeth(label, obj, count) -- printTables(obj) start_sec,start_usec = utime() obj[label](obj,count) end_sec, end_usec = utime() -- printTables(obj) diff_sec = end_sec - start_sec diff_usec = end_usec - start_usec diff = diff_sec + diff_usec print(label, count, "elapsed: ", diff) end -- Main an_obj = TestObj:new() regression_test(an_obj) timemeth("instance_data_access_test", an_obj, 500000) timemeth("cached_parent_data_access_test", an_obj, 500000) timemeth("missing_instance_data_access_test", an_obj, 500000)