summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLua Team <team@lua.org>1998-07-11 12:00:00 +0000
committerrepogen <>1998-07-11 12:00:00 +0000
commit377347776f1f3d820f92151f70bec667f96d5e6b (patch)
treecdb3ba26158df33547dfe765547177afcee119d1 /test
parent4f8c5d0f284e1f4da717aea5008915f185cd2e05 (diff)
downloadlua-github-3.1.tar.gz
Lua 3.13.1
Diffstat (limited to 'test')
-rw-r--r--test/array.lua2
-rw-r--r--test/bisect.lua6
-rw-r--r--test/dump.lua41
-rw-r--r--test/examples/www/README6
-rw-r--r--test/examples/www/db.lua2
-rw-r--r--test/factorial.lua37
-rw-r--r--test/fib.lua11
-rw-r--r--test/globals.lua20
-rw-r--r--test/save.lua44
-rw-r--r--test/sort.lua69
-rw-r--r--test/trace.lua33
11 files changed, 189 insertions, 82 deletions
diff --git a/test/array.lua b/test/array.lua
index 48c28576..728a0b27 100644
--- a/test/array.lua
+++ b/test/array.lua
@@ -1,5 +1,3 @@
-$debug
-
a = {}
local i=0
diff --git a/test/bisect.lua b/test/bisect.lua
index 8b720654..4de9e99b 100644
--- a/test/bisect.lua
+++ b/test/bisect.lua
@@ -1,9 +1,9 @@
-$debug
-- bisection method for solving non-linear equations
function bisect(f,a,b,fa,fb)
write(n," a=",a," fa=",fa," b=",b," fb=",fb,"\n")
local c=(a+b)/2
+ if c==a or c==b then return c end
if abs(a-b)<delta then return c end
n=n+1
local fc=f(c)
@@ -12,10 +12,10 @@ end
-- find root of f in the inverval [a,b]. bisection needs that f(a)*f(b)<0
function solve(f,a,b)
- delta=1e-6 -- tolerance
+ delta=1e-9 -- tolerance
n=0
local z=bisect(f,a,b,f(a),f(b))
- write(format("after %d steps, root is %.10g\n",n,z))
+ write(format("after %d steps, root is %.10g, f=%g\n",n,z,f(z)))
end
-- our function
diff --git a/test/dump.lua b/test/dump.lua
deleted file mode 100644
index bcf51410..00000000
--- a/test/dump.lua
+++ /dev/null
@@ -1,41 +0,0 @@
--- dump global environment
-
-function savevar (n,v)
- if v == nil then return end
- if type(v)=="userdata" or type(v)=="function" then return end
- -- if type(v)=="userdata" or type(v)=="function" then write("\t-- ") end
- write(n,"=")
- if type(v) == "string" then write(format("%q",v))
- elseif type(v) == "table" then
- if v.__visited__ ~= nil then
- write(v.__visited__)
- else
- write("{}\n")
- v.__visited__ = n
- local r,f
- r,f = next(v,nil)
- while r ~= nil do
- if r ~= "__visited__" then
- if type(r) == 'string' then
- savevar(n.."."..r,f)
- else
- savevar(n.."["..r.."]",f)
- end
- end
- r,f = next(v,r)
- end
- end
- else write(tostring(v)) end
- write("\n")
-end
-
-function save ()
- print("\n-- global environment")
- local n,v = nextvar(nil)
- while n ~= nil do
- savevar(n,v)
- n,v = nextvar(n)
- end
-end
-
-save()
diff --git a/test/examples/www/README b/test/examples/www/README
index 0c249a43..b0e1e8bd 100644
--- a/test/examples/www/README
+++ b/test/examples/www/README
@@ -1,7 +1,7 @@
This directory contains a database for a fake web site.
-Standard web page for the persons listed in db.lua are
-creared automatically from template.html with staff.lua.
-(See http://www.cos.ufrj.br for a real web site was created in this way.)
+Standard web page for the persons listed in db.lua are created
+automatically from template.html with staff.lua.
+(See http://www.cos.ufrj.br for a real web site created in this way.)
To run, type lua db.lua.
diff --git a/test/examples/www/db.lua b/test/examples/www/db.lua
index 837afd29..53d78024 100644
--- a/test/examples/www/db.lua
+++ b/test/examples/www/db.lua
@@ -40,7 +40,7 @@ staff{
LOGIN="lhf",
NAME="Luiz Henrique de Figueiredo",
TITLE='D.Sc., <A HREF="http://www.impa.br">IMPA</A>, 1992',
- POSITION='Visiting Research fellow at <A HREF="http://www.lncc.br">LNCC</A>; Consultant at |TECGRAF|',
+ POSITION='Associate Researcher at <A HREF="http://www.lncc.br">LNCC</A>; Consultant at |TECGRAF|',
AREAS="Geometric modeling; Software tools",
WWW="http://www2.lncc.br/~lhf/",
}
diff --git a/test/factorial.lua b/test/factorial.lua
new file mode 100644
index 00000000..d9cc375c
--- /dev/null
+++ b/test/factorial.lua
@@ -0,0 +1,37 @@
+-- function closures are powerful
+
+-- traditional fixed-point operator from functional programming
+
+Y = function (g)
+ local a = function (f) return f(f) end
+ return a(function (f)
+ return %g(function (x)
+ local c=%f(%f)
+ return c(x)
+ end)
+ end)
+end
+
+
+-- factorial without recursion
+
+F = function (f)
+ return function (n)
+ if n == 0 then return 1
+ else return n*%f(n-1) end
+ end
+ end
+
+factorial = Y(F) -- factorial is the fixed point of F
+
+-- now test it
+
+function test(x)
+ write(x,"! = ",factorial(x),"\n")
+end
+
+test(3)
+test(4)
+test(5)
+test(6)
+test(7)
diff --git a/test/fib.lua b/test/fib.lua
new file mode 100644
index 00000000..881cbdc3
--- /dev/null
+++ b/test/fib.lua
@@ -0,0 +1,11 @@
+-- very inefficient fibonacci function
+
+function fib(n)
+ if n<2 then
+ return n
+ else
+ return fib(n-1)+fib(n-2)
+ end
+end
+
+print(fib(20))
diff --git a/test/globals.lua b/test/globals.lua
new file mode 100644
index 00000000..03b977c3
--- /dev/null
+++ b/test/globals.lua
@@ -0,0 +1,20 @@
+-- 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
+
+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
+ local g=gsub(s,P,"")
+ write(g,"\t",l,"\n")
+ elseif strfind(s,"%sSETGLOBAL") then
+ local g=gsub(s,P,"")
+ write(g,"*\t",l,"\n")
+ end
+end
diff --git a/test/save.lua b/test/save.lua
index f16bdf20..ce2e5b34 100644
--- a/test/save.lua
+++ b/test/save.lua
@@ -1,4 +1,44 @@
-dofile("dump.lua")
+-- dump global environment
+
+function savevar (n,v)
+ if v == nil then return end
+ if type(v)=="userdata" or type(v)=="function" then return end
+ -- if type(v)=="userdata" or type(v)=="function" then write("\t-- ") end
+ write(n,"=")
+ if type(v) == "string" then write(format("%q",v))
+ elseif type(v) == "table" then
+ if v.__visited__ ~= nil then
+ write(v.__visited__)
+ else
+ write("{}\n")
+ v.__visited__ = n
+ local r,f
+ r,f = next(v,nil)
+ while r ~= nil do
+ if r ~= "__visited__" then
+ if type(r) == 'string' then
+ savevar(n.."."..r,f)
+ else
+ savevar(n.."["..r.."]",f)
+ end
+ end
+ r,f = next(v,r)
+ end
+ end
+ else write(tostring(v)) end
+ write("\n")
+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
+end
+
+-- ow some examples
a = 3
x = {a = 4, b = "name", l={4,5,67}}
@@ -6,6 +46,4 @@ x = {a = 4, b = "name", l={4,5,67}}
b = {t=5}
x.next = b
-
save()
-
diff --git a/test/sort.lua b/test/sort.lua
index a5eb0956..41473f4a 100644
--- a/test/sort.lua
+++ b/test/sort.lua
@@ -1,35 +1,42 @@
-$debug
-
-function quicksort(a,r,s)
- if s<=r then return end
- local v, i, j = a[r], r, s+1
- repeat
- i=i+1; while a[i]<v do i=i+1 end
- j=j-1; while a[j]>v do j=j-1 end
- a[i],a[j]=a[j],a[i]
- until j<=i
- a[i],a[j]=a[j],a[i] -- undo last swap
- a[j],a[r]=a[r],a[j]
- quicksort(a,r,j-1)
- quicksort(a,j+1,s)
+-- extracted from Programming Pearls, page 110
+function qsort(x,l,u,f)
+ if l<u then
+ local m=random(u-(l-1))+l-1 -- choose a random pivot in range l..u
+ x[l],x[m]=x[m],x[l] -- swap pivot to first position
+ local t=x[l] -- pivot value
+ m=l
+ local i=l+1
+ while i<=u do
+ -- invariant: x[l+1..m] < t <= x[m+1..i-1]
+ if f(x[i],t) then
+ m=m+1
+ x[m],x[i]=x[i],x[m] -- swap x[i] and x[m]
+ end
+ i=i+1
+ end
+ x[l],x[m]=x[m],x[l] -- swap pivot to a valid place
+ -- x[l+1..m-1] < x[m] <= x[m+1..u]
+ qsort(x,l,m-1,f)
+ qsort(x,m+1,u,f)
+ end
end
-function selectionsort(a,n)
+function selectionsort(x,n,f)
local i=1
while i<=n do
- local m, j = i, i+1
+ local m,j=i,i+1
while j<=n do
- if a[j]>a[m] then m=j end -- reverse sort
+ if f(x[j],x[m]) then m=j end
j=j+1
end
- a[i],a[m]=a[m],a[i] -- swap a[i] and a[m]
+ x[i],x[m]=x[m],x[i] -- swap x[i] and x[m]
i=i+1
end
end
function show(m,x)
write(m,"\n\t")
- local i=0
+ local i=1
while x[i] do
write(x[i])
i=i+1
@@ -38,15 +45,19 @@ function show(m,x)
write("\n")
end
-x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}
-
-n=1 while x[n] do n=n+1 end -- count elements
-x[0]="A" x[n]="Z" -- quicksort need sentinels
-
-show("original",x)
+function testsorts(x)
+ local n=1
+ while x[n] do n=n+1 end; n=n-1 -- count elements
+ show("original",x)
+ qsort(x,1,n,function (x,y) return x<y end)
+ show("after quicksort",x)
+ selectionsort(x,n,function (x,y) return x>y end)
+ show("after reverse selection sort",x)
+ qsort(x,1,n,function (x,y) return x<y end)
+ show("after quicksort again",x)
+end
-quicksort(x,1,n-1)
-show("after quicksort",x)
+-- array t be sorted
+x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}
-selectionsort(x, n-1)
-show("after reverse selection sort",x)
+testsorts(x)
diff --git a/test/trace.lua b/test/trace.lua
new file mode 100644
index 00000000..5f32a8f9
--- /dev/null
+++ b/test/trace.lua
@@ -0,0 +1,33 @@
+-- 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