diff options
author | Lua Team <team@lua.org> | 2000-11-06 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 2000-11-06 12:00:00 +0000 |
commit | 8cb71cb5548e3138e5d4e4744f52c79d9fafb116 (patch) | |
tree | 25859eb162c67eafc46866e0ec3a9a7ebf93157a /test | |
parent | b7610da5fed99f59ac73ae452da8839a0f2c1bda (diff) | |
download | lua-github-4.0.tar.gz |
Lua 4.04.0
Diffstat (limited to 'test')
-rw-r--r-- | test/array.lua | 13 | ||||
-rw-r--r-- | test/bisect.lua | 3 | ||||
-rw-r--r-- | test/cf-for.lua | 19 | ||||
-rw-r--r-- | test/examples/ps/hilbert.lua | 2 | ||||
-rw-r--r-- | test/examples/www/staff.lua | 8 | ||||
-rw-r--r-- | test/fib.lua | 30 | ||||
-rw-r--r-- | test/globals.lua | 13 | ||||
-rw-r--r-- | test/life.lua | 121 | ||||
-rw-r--r-- | test/lisp.lua | 22 | ||||
-rw-r--r-- | test/old.lua | 42 | ||||
-rw-r--r-- | test/qp.lua | 6 | ||||
-rw-r--r-- | test/save.lua | 13 | ||||
-rw-r--r-- | test/sort.lua | 5 | ||||
-rw-r--r-- | test/table.lua | 7 | ||||
-rw-r--r-- | test/trace-calls.lua | 23 | ||||
-rw-r--r-- | test/trace-globals.lua | 75 | ||||
-rw-r--r-- | test/trace.lua | 33 | ||||
-rw-r--r-- | test/undefined.lua | 20 | ||||
-rw-r--r-- | test/webform.lua | 8 |
19 files changed, 390 insertions, 73 deletions
diff --git a/test/array.lua b/test/array.lua deleted file mode 100644 index 728a0b27..00000000 --- a/test/array.lua +++ /dev/null @@ -1,13 +0,0 @@ -a = {} - -local i=0 -while i<10 do - a[i] = i*i - i=i+1 -end - -local r,v = next(a,nil) -while r ~= nil do - write ("array[",r,"] = ",v,"\n") - r,v = next(a,r) -end diff --git a/test/bisect.lua b/test/bisect.lua index 4de9e99b..9e1d8f70 100644 --- a/test/bisect.lua +++ b/test/bisect.lua @@ -18,9 +18,12 @@ function solve(f,a,b) write(format("after %d steps, root is %.10g, f=%g\n",n,z,f(z))) end +-- an example + -- our function function f(x) return x*x*x-x-1 end +-- find zero in [1,2] solve(f,1,2) diff --git a/test/cf-for.lua b/test/cf-for.lua new file mode 100644 index 00000000..1bd0ab27 --- /dev/null +++ b/test/cf-for.lua @@ -0,0 +1,19 @@ +-- temperature conversion table +-- now using the new "for" statement + +-- celsius to farenheit + +for c0=-20,50-1,10 do + write("C ") + for c=c0,c0+10-1 do + write(format("%3.0f ",c)) + end + write("\n") + + write("F ") + for c=c0,c0+10-1 do + f=(9/5)*c+32 + write(format("%3.0f ",f)) + end + write("\n\n") +end diff --git a/test/examples/ps/hilbert.lua b/test/examples/ps/hilbert.lua index b1042b57..dbcec963 100644 --- a/test/examples/ps/hilbert.lua +++ b/test/examples/ps/hilbert.lua @@ -3,8 +3,6 @@ -- Luiz Henrique de Figueiredo (lhf@csg.uwaterloo.ca) -- 10 Nov 95 -$debug - dofile("ps.lua") function p() diff --git a/test/examples/www/staff.lua b/test/examples/www/staff.lua index 7fa5b810..f36df4ad 100644 --- a/test/examples/www/staff.lua +++ b/test/examples/www/staff.lua @@ -1,7 +1,5 @@ -$debug - readfrom("template.html") -TEMPLATE=read(".*") +TEMPLATE=read("*a") readfrom() PAT="|(%a%a*)|" @@ -17,10 +15,8 @@ function get(i) end function global(t) - local i,v=next(t,nil) - while i do + for i,v in t do GLOBAL[i]=v - i,v=next(t,i) end end diff --git a/test/fib.lua b/test/fib.lua index 881cbdc3..d946946a 100644 --- a/test/fib.lua +++ b/test/fib.lua @@ -1,6 +1,7 @@ -- very inefficient fibonacci function function fib(n) + N=N+1 if n<2 then return n else @@ -8,4 +9,31 @@ function fib(n) end end -print(fib(20)) +-- a much faster cached version + +function cache(f) + local c={} + return function (x) + local y=%c[x] + if not y then + y=%f(x) + %c[x]=y + end + return y + end +end + +function test(s) + N=0 + local c=clock() + local v=fib(n) + local t=clock()-c + print(s,n,v,t,N) +end + +n=n or 24 -- for other values, do lua -e n=XX fib.lua +n=tonumber(n) +print("","n","value","time","evals") +test("plain") +fib=cache(fib) +test("cached") diff --git a/test/globals.lua b/test/globals.lua index 03b977c3..baa75049 100644 --- a/test/globals.lua +++ b/test/globals.lua @@ -1,20 +1,19 @@ --- globals.lua -- reads the output of luac -d -l -p and reports global variable usage --- typical usage: luac -p -l -d file.lua | lua globals.lua | sort +-- lines where a global is written to are marked with "*" +-- typical usage: luac -p -l file.lua | lua globals.lua | sort local P="^.*; " -- pattern to extract comments -local l="" -- last line seen while 1 do local s=read() if s==nil then return end - if strfind(s,"%sSETLINE") then - l=gsub(s,P,"") - elseif strfind(s,"%sGETGLOBAL") then + if strfind(s,"%sGETGLOBAL") then local g=gsub(s,P,"") + local _,_,l=strfind(s,"(%d+)") write(g,"\t",l,"\n") elseif strfind(s,"%sSETGLOBAL") then local g=gsub(s,P,"") - write(g,"*\t",l,"\n") + local _,_,l=strfind(s,"(%d+)") + write(g,"\t",l,"*\n") end end diff --git a/test/life.lua b/test/life.lua new file mode 100644 index 00000000..7c817a04 --- /dev/null +++ b/test/life.lua @@ -0,0 +1,121 @@ +-- life.lua +-- original by Dave Bollinger <DBollinger@compuserve.com> posted to lua-l +-- modified to use ANSI terminal escape sequences + +ALIVE="O" +DEAD="-" + +function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary + local i=10000 + while i>0 do i=i-1 end + -- local i=clock()+1 while(clock()<i) do end +end + +function ARRAY2D(w,h) + local t = {} + t.w=w + t.h=h + while h>0 do + t[h] = {} + local x=w + while x>0 do + t[h][x]=0 + x=x-1 + end + h=h-1 + end + return t +end + +_CELLS = {} + +-- give birth to a "shape" within the cell array +function _CELLS:spawn(shape,left,top) + local y=0 + while y<shape.h do + local x=0 + while x<shape.w do + self[top+y][left+x] = shape[y*shape.w+x+1] + x=x+1 + end + y=y+1 + end +end + +-- run the CA and produce the next generation +function _CELLS:evolve(next) + local ym1,y,yp1,yi=self.h-1,self.h,1,self.h + while yi > 0 do + local xm1,x,xp1,xi=self.w-1,self.w,1,self.w + while xi > 0 do + local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] + + self[y][xm1] + self[y][xp1] + + self[yp1][xm1] + self[yp1][x] + self[yp1][xp1] + next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0 + xm1,x,xp1,xi = x,xp1,xp1+1,xi-1 + end + ym1,y,yp1,yi = y,yp1,yp1+1,yi-1 + end +end + +-- output the array to screen +function _CELLS:draw() + local out="" -- accumulate to reduce flicker + local y=1 + while y <= self.h do + local x=1 + while x <= self.w do + out=out..(((self[y][x]>0) and ALIVE) or DEAD) + x=x+1 + end + out=out.."\n" + y=y+1 + end + write(out) +end + +-- constructor +function CELLS(w,h) + local c = ARRAY2D(w,h) + c.spawn = _CELLS.spawn + c.evolve = _CELLS.evolve + c.draw = _CELLS.draw + return c +end + +-- +-- shapes suitable for use with spawn() above +-- +HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 } +GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 } +EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 } +FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 } +BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 } + +-- the main routine +function LIFE(w,h) + -- create two arrays + local thisgen = CELLS(w,h) + local nextgen = CELLS(w,h) + + -- create some life + -- about 1000 generations of fun, then a glider steady-state + thisgen:spawn(GLIDER,5,4) + thisgen:spawn(EXPLODE,25,10) + thisgen:spawn(FISH,4,12) + + -- run until break + local gen=1 + write("\027[2J") -- ANSI clear screen + while 1 do + thisgen:evolve(nextgen) + thisgen,nextgen = nextgen,thisgen + write("\027[H") -- ANSI home cursor + thisgen:draw() + write("Generation: "..gen.."\n") + gen=gen+1 + --delay() -- no delay + end +end + +LIFE(40,20) diff --git a/test/lisp.lua b/test/lisp.lua new file mode 100644 index 00000000..e6bcd084 --- /dev/null +++ b/test/lisp.lua @@ -0,0 +1,22 @@ +-- a simple LISP evaluator + +function eval(x) + if type(x)=="table" then + return eval(x[1])(eval(x[2]),eval(x[3])) + else + return x + end +end + +function add(x,y) return x+y end +function sub(x,y) return x-y end +function mul(x,y) return x*y end +function div(x,y) return x/y end +function pow(x,y) return x^y end + +-- an example + +function E(x) print(eval(x)) end + +E{add,1,{mul,2,3}} +E{sin,60} diff --git a/test/old.lua b/test/old.lua new file mode 100644 index 00000000..ebb4e70b --- /dev/null +++ b/test/old.lua @@ -0,0 +1,42 @@ +-- implementation of old functions + +function foreach(t,f) + for i,v in t do + local r=f(i,v) + if r then return r end + end +end + +function foreachi(t,f) + for i=1,getn(t) do + local r=f(i,t[i]) + if r then return r end + end +end + +function foreachvar(f) + return foreach(globals(),f) +end + +function nextvar(n) + return next(globals(),n) +end + +function rawgetglobal(n) + return rawget(globals(),n) +end + +function rawsetglobal(n,v) + return rawset(globals(),n,v) +end + +rawsettable=rawset +rawgettable=rawget + +function getglobal(n) + return globals()[n] +end + +function setglobal(n,v) + globals()[n]=v +end diff --git a/test/qp.lua b/test/qp.lua new file mode 100644 index 00000000..eb6b3205 --- /dev/null +++ b/test/qp.lua @@ -0,0 +1,6 @@ +-- decode quoted-printable text + +T=read"*a" +T=gsub(T,"=\n","") +T=gsub(T,"=(%x%x)",function (x) return strchar(tonumber(x,16)) end) +write(T) diff --git a/test/save.lua b/test/save.lua index ce2e5b34..855e4c19 100644 --- a/test/save.lua +++ b/test/save.lua @@ -12,9 +12,7 @@ function savevar (n,v) else write("{}\n") v.__visited__ = n - local r,f - r,f = next(v,nil) - while r ~= nil do + for r,f in v do if r ~= "__visited__" then if type(r) == 'string' then savevar(n.."."..r,f) @@ -22,7 +20,6 @@ function savevar (n,v) savevar(n.."["..r.."]",f) end end - r,f = next(v,r) end end else write(tostring(v)) end @@ -31,14 +28,10 @@ end function save () write("\n-- global environment\n") - local n,v = nextvar(nil) - while n ~= nil do - savevar(n,v) - n,v = nextvar(n) - end + foreach(globals(),savevar) end --- ow some examples +-- an example a = 3 x = {a = 4, b = "name", l={4,5,67}} diff --git a/test/sort.lua b/test/sort.lua index 41473f4a..1d50ad47 100644 --- a/test/sort.lua +++ b/test/sort.lua @@ -1,3 +1,6 @@ +-- two implementations of a sort function +-- this is an example only. Lua has now a built-in function "sort" + -- extracted from Programming Pearls, page 110 function qsort(x,l,u,f) if l<u then @@ -57,7 +60,7 @@ function testsorts(x) show("after quicksort again",x) end --- array t be sorted +-- array to be sorted x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"} testsorts(x) diff --git a/test/table.lua b/test/table.lua new file mode 100644 index 00000000..2c2dd90a --- /dev/null +++ b/test/table.lua @@ -0,0 +1,7 @@ +local A +while 1 do + local a,b=read("*w","*w") + if a==nil then break end + if a~=A then A=a write("\n",a,":\n") end + write(b," ") +end diff --git a/test/trace-calls.lua b/test/trace-calls.lua new file mode 100644 index 00000000..a073cc20 --- /dev/null +++ b/test/trace-calls.lua @@ -0,0 +1,23 @@ +-- trace calls +-- example: lua trace-calls.lua hello.lua bisect.lua + +function callhook(func) + local t=getinfo(2) + write(">>> ") +--foreach(t,print) + if t.what=="main" then + if func=="call" then + write("begin ",t.source) + else + write("end ",t.source) + end + elseif t.what=="Lua" then + write(func," ",t.name," <",t.linedefined,":",t.source,">") + else + write(func," ",t.name," [",t.what,"] ") + end + if t.currentline>=0 then write(":",t.currentline) end + write("\n") +end + +setcallhook(callhook) diff --git a/test/trace-globals.lua b/test/trace-globals.lua new file mode 100644 index 00000000..566e1106 --- /dev/null +++ b/test/trace-globals.lua @@ -0,0 +1,75 @@ +-- shows how to trace assigments to global variables + +-- a tostring that quotes strings. note the use of the original tostring. +local tostring=function(a) + if tag(a)==tag("") then + return format("%q",a) + else + return %tostring(a) + end +end + +local T=newtag() + +local Tlog=function (name,old,new) + local t=getinfo(3,"Sl") + local line=t.currentline + write(t.source) + if line>=0 then write(":",line) end + write(" -- ",name," is now ",%tostring(new)," (was ",%tostring(old),")","\n") +end + +local Tgetnew=function (name) + local t=settag({},%T) + rawset(globals(),name,t) + return nil +end + +local Tsetnew=function (name,old,new) + %Tlog(name,old,new) + local t=settag({value=new},%T) + rawset(globals(),name,t) + return t +end + +local Tsetglobal=function (name,old,new) + %Tlog(name,old.value,new) + old.value=new +end + +local Tgetglobal=function (x,value) + return value.value +end + +settagmethod(T,"getglobal",Tgetglobal) +settagmethod(T,"setglobal",Tsetglobal) + +-- to trace only selected variables, use the following function +-- and comment the next two calls to settagmethod + +function trace(name) + local t=settag({value=rawget(globals(),name)},%T) + rawset(globals(),name,t) +end + +settagmethod(tag(nil),"getglobal",Tgetnew) +settagmethod(tag(nil),"setglobal",Tsetnew) + +-- an example + +trace"a" +print(a) +a=1 +b=2 +c=3 +a=10 +b=20 +c=30 +trace"b" +b="lua" +c={} +a=print +c=nil +c=100 + +print(a,b,c,d) diff --git a/test/trace.lua b/test/trace.lua deleted file mode 100644 index 5f32a8f9..00000000 --- a/test/trace.lua +++ /dev/null @@ -1,33 +0,0 @@ --- shows how to trace assigments to global variables - -T=newtag() -- tag for tracing - -function Ttrace(name) -- trace a global variable - local t={} - settag(t,T) - rawsetglobal(name,t) -end - -function Tsetglobal(name,old,new) - write("tracing: ",name," now is ",new,"\n") - old.value=new -end - -function Tgetglobal(x,value) -- get the actual value - return value.value -end - -settagmethod(T,"getglobal",Tgetglobal) -settagmethod(T,"setglobal",Tsetglobal) - --- now show it working - -Ttrace("a") -Ttrace("c") - -a=1 -b=2 -c=3 -a=10 -b=20 -c=30 diff --git a/test/undefined.lua b/test/undefined.lua new file mode 100644 index 00000000..bbecffe3 --- /dev/null +++ b/test/undefined.lua @@ -0,0 +1,20 @@ +-- catch "undefined" global variables. see FAQ. + +do + local f=function(name) + local v=rawget(globals(),name) + if v then + return v + else + error("undefined global variable `"..name.."'") + end + end + + settagmethod(tag(nil),"getglobal",f) +end + +-- an example + +a=1 +c=3 +print(a,b,c) -- 'b' is undefined diff --git a/test/webform.lua b/test/webform.lua new file mode 100644 index 00000000..3e4c3ce4 --- /dev/null +++ b/test/webform.lua @@ -0,0 +1,8 @@ +-- convert POST data to Lua table + +T=read"*a" -- for GET, use T=getenv"QUERY_STRING" +T=gsub(T,"=","=[[") +T=gsub(T,"&","]],\n") +T=gsub(T,"+"," ") +T=gsub(T,"%%(%x%x)",function (x) return strchar(tonumber(x,16)) end) +write("form{\n",T,"]]}\n") |