Andy73

Test.lua

Feb 12th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. local RESULT={}
  2.  
  3. local Version={
  4.     Matrix="0.1.7",
  5.     Library="0.2.1",
  6. }
  7.  
  8. local function s() --TODO SOLVE THIS!!!!!!!!!!!!!!!!!!!!!!!
  9.     local t,e=tostring({}),nil
  10.     os.queueEvent(t)
  11.     while t~=e do
  12.         e=coroutine.yield()
  13.     end
  14. end
  15.  
  16. local TaskList={}
  17.  
  18. local Test={}
  19. --[[Test.SetResultTable=function(t)
  20.     if type(t)~="table" then error("(1)Expected table, got "..type(t)..".",2) end
  21.     RESULT=t
  22.     return true
  23. end]]
  24.  
  25. Test.New=function(name,g,max,maxt,yield,fn,...) --test name,group,maximum calls of the benchmarked function,maximum time for benchmark,whether to yield or not,benchmarked function/coroutine,benchmarked function arguments
  26.     if type(name)~="string" then error("(1)Expected string as 'name'.",2) end
  27.     if type(max)~="number" or max<1 then error("(3)Expected number greater than 1.",2) end
  28.     g=g or "Graphics"
  29.     maxt=maxt/100--maxt is in seconds, convert to 1/100 of its value for easy comparison with os.time() (better than converting os.time() on every single comparison)
  30.     if yield==nil then yield=true end
  31.    
  32.     local n={} --new (instance)
  33.     local st,t,r=0,0,false --start time,time,running
  34.     local calls=0 --actual b. function calls successfully performed
  35.     --TODO/WARN loops completed or function calls? (diff. position)
  36.    
  37.     local args={...}
  38.     if type(fn)~="function" and type(fn)~="thread" then
  39.         error("(5)Expected function or thread, got "..type(fn)..".",2)
  40.     end
  41.    
  42.     local fix=false
  43.    
  44.     local function Start()
  45.         if fix then st=os.time() end
  46.         for i=1,max do
  47.             if type(fn)=="thread" then
  48.                 if coroutine.status(fn)=="dead" then error("Coroutine died during test execution!",2) end
  49.                 coroutine.resume(fn,unpack(args))
  50.             else
  51.                 fn(unpack(args)) --TODO different arguments to Run(...) pass here (/comment related) WARN was Start(...) before
  52.             end
  53.             if yield then s() end --TODO/WARN: Also via argument? WARN coroutine!!!!!!
  54.             calls=i
  55.             if os.time()-st>maxt then break end --TODO/WARN > or >=?
  56.         end
  57.     end
  58.  
  59.     local function Stop()
  60.         if r then
  61.             t=os.time()-st
  62.             r=false
  63.         end
  64.     end
  65.  
  66.     n.Run=function(bDoReturnCoroutine)
  67.         term.setTextColor(colors.blue)
  68.         print(name)
  69.         if TaskList[name] then
  70.             if r then error("Test already running",2)end --WARN 'not' deleted (what it was doing there?)
  71.             r=true                                       --WARN now it isnt possible to encouter an already running test
  72.             st=os.time()
  73.             if bDoReturnCoroutine then
  74.                 fix=true
  75.                 return coroutine.create(Start)
  76.             end
  77.             Start()                                 --TODO/DONE? rename the functions, "Start" actually runs the test, Stop is kinda useless...
  78.         end
  79.         return n.End()
  80.     end
  81.    
  82.     n.Running=function()
  83.         return r
  84.     end
  85.  
  86.     n.OnGetResults=0 --WARN highly EXPERIMENTAL
  87.  
  88.     n.GetResults=function()
  89.         if r then error("Cannot fetch results of an ongoing test",2) end
  90.         local out={["g"]=g,["calls"]=calls,["time"]=t,["mark"]=Test.GetMark(t,calls)}
  91.         if type(n.OnGetResults)=="function" then -----<<<--WARN untested feature
  92.             local values={pcall(n.OnGetResults(t,g))}
  93.             if values[1]==true then
  94.                 table.remove(values,1)
  95.                 out["custom"]=values
  96.             end
  97.         end
  98.         return out
  99.     end
  100.  
  101.     n.GetName=function()return name end
  102.  
  103.     n.Rename=function(nm)
  104.         if type(nm)~="string" then error("Expected string as 'name'",2)end
  105.         name=nm return true
  106.     end
  107.  
  108.     n.End=function()
  109.         Stop()
  110.         if type(RESULT)~="table" then error("(!)The result table pointer was not set, unable to log test results.",2)end --WARN useless now?
  111.         local old=RESULT[name]
  112.         RESULT[name]=n.GetResults()
  113.         return old,RESULT
  114.     end
  115.  
  116.     return n
  117.  
  118. end
  119.  
  120. Test.GetResults=function()
  121.     return RESULT
  122. end
  123. Test.Version=Version
  124.  
  125. Test.GetMark=function(t,c) --time,calls
  126.     return c/t  --the bigger the better
  127. end
  128.  
  129. Test.Convert=function(str)
  130.    
  131. end
  132.  
  133. Test.SetTaskList=function(tsk)
  134.     if type(tsk)~="table" then error("(1)Expected table as TaskList, got "..type(tsk),2) end
  135.     TaskList=tsk
  136. end
  137.  
  138. _G.Test=Test
  139.  
  140. --[[
  141.  
  142. TODO
  143. -point system, a way to evaluate
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. ]]
Advertisement
Add Comment
Please, Sign In to add comment