diff options
author | Lua Team <team@lua.org> | 1997-07-01 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 1997-07-01 12:00:00 +0000 |
commit | 4f8c5d0f284e1f4da717aea5008915f185cd2e05 (patch) | |
tree | 5671acf8a2cacf0c0524ce96d22959590a3aa5af /test | |
parent | 47a298a24ad3a8202440051de5938618502302a0 (diff) | |
download | lua-github-4f8c5d0f284e1f4da717aea5008915f185cd2e05.tar.gz |
Lua 3.03.0
Diffstat (limited to 'test')
-rw-r--r-- | test/README | 7 | ||||
-rw-r--r-- | test/array.lua | 6 | ||||
-rw-r--r-- | test/bisect.lua | 4 | ||||
-rw-r--r-- | test/dump.lua | 26 | ||||
-rw-r--r-- | test/examples/complex/complex.lua | 82 | ||||
-rw-r--r-- | test/examples/complex/mandel.lua | 25 | ||||
-rw-r--r-- | test/examples/www/README | 10 | ||||
-rw-r--r-- | test/examples/www/db.lua | 46 | ||||
-rw-r--r-- | test/examples/www/staff.lua | 37 | ||||
-rw-r--r-- | test/examples/www/template.html | 29 | ||||
-rw-r--r-- | test/hello.lua | 2 | ||||
-rw-r--r-- | test/loop.lua | 6 | ||||
l--------- | test/lua | 1 | ||||
l--------- | test/luac | 1 | ||||
-rw-r--r-- | test/sort.lua | 2 | ||||
-rw-r--r-- | test/split.lua | 18 | ||||
-rw-r--r-- | test/type.lua | 37 |
17 files changed, 149 insertions, 190 deletions
diff --git a/test/README b/test/README index 625644d0..6f410ee4 100644 --- a/test/README +++ b/test/README @@ -1,7 +1,6 @@ These are simple tests for Lua. Some of them contain useful code. They are meant to be run to make sure Lua is ok and also to be read, to see how Lua programs can look like. -In the directory examples/, there is more useful code, such as a library for -complex arithmetic (a good example of fallbacks for overloading and redefinition -of primitive functions), and a library for PostScript output. Both libraries -include simple examples. + +In the directory examples/, there is more useful code: a library for PostScript +output and a database for a fake web site. diff --git a/test/array.lua b/test/array.lua index 7caca888..48c28576 100644 --- a/test/array.lua +++ b/test/array.lua @@ -2,14 +2,14 @@ $debug a = {} -i=0 +local i=0 while i<10 do a[i] = i*i i=i+1 end -r,v = next(a,nil) +local r,v = next(a,nil) while r ~= nil do - print ("array["..r.."] = "..v) + write ("array[",r,"] = ",v,"\n") r,v = next(a,r) end diff --git a/test/bisect.lua b/test/bisect.lua index cfb1bbfd..8b720654 100644 --- a/test/bisect.lua +++ b/test/bisect.lua @@ -2,7 +2,7 @@ $debug -- bisection method for solving non-linear equations function bisect(f,a,b,fa,fb) -print(n.." a="..a.." fa="..fa.." b="..b.." fb="..fb) + write(n," a=",a," fa=",fa," b=",b," fb=",fb,"\n") local c=(a+b)/2 if abs(a-b)<delta then return c end n=n+1 @@ -15,7 +15,7 @@ function solve(f,a,b) delta=1e-6 -- tolerance n=0 local z=bisect(f,a,b,f(a),f(b)) - print(format("after %d steps, root is %.10g",n,z)) + write(format("after %d steps, root is %.10g\n",n,z)) end -- our function diff --git a/test/dump.lua b/test/dump.lua index ad8d44a6..bcf51410 100644 --- a/test/dump.lua +++ b/test/dump.lua @@ -1,17 +1,19 @@ -- dump global environment function savevar (n,v) - if v == nil then return end; - if type(v) == "number" then print(n.."="..v) return end - if type(v) == "string" then print(n.."='"..v.."'") return end - if type(v) == "table" then + 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 - print(n .. "=" .. v.__visited__); + write(v.__visited__) else - print(n.."={}") - v.__visited__ = n; - local r,f; - r,f = next(v,nil); + 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 @@ -23,13 +25,15 @@ function savevar (n,v) r,f = next(v,r) end 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); + savevar(n,v) n,v = nextvar(n) end end diff --git a/test/examples/complex/complex.lua b/test/examples/complex/complex.lua deleted file mode 100644 index a213a746..00000000 --- a/test/examples/complex/complex.lua +++ /dev/null @@ -1,82 +0,0 @@ --- complex.lua --- complex arithmetic package for lua --- Luiz Henrique de Figueiredo (lhf@csg.uwaterloo.ca) --- 24 Oct 95 -$debug - -Complex={type="package"} - -function complex(x,y) - return { re=x, im=y, type="complex" } -end - -function Complex.conj(x,y) - return complex(x.re,-x.im) -end - -function Complex.norm2(x) - local n=Complex.mul(x,Complex.conj(x)) - return n.re -end - -function Complex.abs(x) - return sqrt(Complex.norm2(x)) -end - -function Complex.add(x,y) - return complex(x.re+y.re,x.im+y.im) -end - -function Complex.sub(x,y) - return complex(x.re-y.re,x.im-y.im) -end - -function Complex.mul(x,y) - return complex(x.re*y.re-x.im*y.im,x.re*y.im+x.im*y.re) -end - -function Complex.div(x,y) - local z=x*Complex.conj(y) - local t=Complex.norm2(y) - z.re=z.re/t - z.im=z.im/t - return z -end - -function Complex.fallback(x,y,op) - if type(x)=="number" then x=complex(x,0) end - if type(y)=="number" then y=complex(y,0) end - if type(x)=="complex" and type(y)=="complex" then - return Complex[op](x,y) - else - return Complex.oldfallback(x,y) - end -end - -function Complex.newtype(x) - local t=Complex.oldtype(x) - if t=="table" and x.type then return x.type else return t end -end - -function Complex.print(x) - if type(x)~="complex" then - Complex.oldprint(x) - else - local s - if x.im>=0 then s="+" else s="" end - Complex.oldprint(x.re..s..x.im.."I") - end -end - -function Complex.newabs(x) - if type(x)~="complex" then - return Complex.oldabs(x) - else - return Complex.abs(x) - end -end - -Complex.oldfallback=setfallback("arith",Complex.fallback) -Complex.oldtype=type type=Complex.newtype -Complex.oldprint=print print=Complex.print -Complex.oldabs=abs abs=Complex.abs diff --git a/test/examples/complex/mandel.lua b/test/examples/complex/mandel.lua deleted file mode 100644 index 5e3d3eb6..00000000 --- a/test/examples/complex/mandel.lua +++ /dev/null @@ -1,25 +0,0 @@ -dofile("complex.lua") - -xmin=-2 xmax=2 ymin=-2 ymax=2 -d=.125 - -function level(x,y) - local c=complex(x,y) - local l=0 - local z=c - repeat - z=z*z+c - l=l+1 - until abs(z)>2 or l>255 - return l-1 -end - -x=xmin -while x<xmax do - y=ymin - while y<ymax do - print(level(x,y)) - y=y+d - end - x=x+d -end diff --git a/test/examples/www/README b/test/examples/www/README new file mode 100644 index 00000000..0c249a43 --- /dev/null +++ b/test/examples/www/README @@ -0,0 +1,10 @@ +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.) + +To run, type lua db.lua. + +This example is meant to show the power of gsub and Lua declarative constructs, +which have been combined here into a "mail-merge" application. + diff --git a/test/examples/www/db.lua b/test/examples/www/db.lua new file mode 100644 index 00000000..837afd29 --- /dev/null +++ b/test/examples/www/db.lua @@ -0,0 +1,46 @@ +dofile("staff.lua") + +global{ + ROOT='http://www.tecgraf.puc-rio.br', + EMAIL="|LOGIN|@tecgraf.puc-rio.br", + WWW="|ROOT|/~|LOGIN|", + webmast='|ROOT|/webmaster.html', + TECGRAF='<A HREF="http://www.tecgraf.puc-rio.br">TeCGraf</A>', + CS='<A HREF="http://www.inf.puc-rio.br">Computer Science Department</A>', + PUCRIO='<A HREF="http://www.puc-rio.br">PUC-Rio</A>', +} + +staff{ +DI="inf.puc-rio.br", + LOGIN="roberto", + NAME="Roberto Ierusalimschy", + TITLE="D.Sc., |PUCRIO|, 1990", + POSITION="Associate Professor, |CS|, |PUCRIO|", + AREAS="Programming Languages, Object Oriented Programming", + EMAIL="|LOGIN|@|DI|", + WWW="http://www.|DI|/~|LOGIN|", +} + +staff{ +PCG="http://www.graphics.cornell.edu", + LOGIN="celes", + NAME="Waldemar Celes", + TITLE="D.Sc., |PUCRIO|, 1995", + POSITION="Postdoctoral Associate at ".. +'<A HREF="|PCG|">Program of Computer Graphics</A>, '.. +'<A HREF="http://www.cornell.edu">Cornell University</A>', + WWW="|PCG|/~celes/", + AREAS="Image segmentation and 3D reconstruction; " +.."Physical simulation and educational software;" +.."Geometric modeling and topological data structures;" +.."Extension languages and customizable applications" +} + +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|', + AREAS="Geometric modeling; Software tools", + WWW="http://www2.lncc.br/~lhf/", +} diff --git a/test/examples/www/staff.lua b/test/examples/www/staff.lua new file mode 100644 index 00000000..7fa5b810 --- /dev/null +++ b/test/examples/www/staff.lua @@ -0,0 +1,37 @@ +$debug + +readfrom("template.html") +TEMPLATE=read(".*") +readfrom() + +PAT="|(%a%a*)|" + +GLOBAL={ + DATE=date("%d/%m/%Y %T"), +} + +function get(i) + if LOCAL[i] then return LOCAL[i] + elseif GLOBAL[i] then return GLOBAL[i] + else return "<BLINK>?"..i.."?</BLINK>" end +end + +function global(t) + local i,v=next(t,nil) + while i do + GLOBAL[i]=v + i,v=next(t,i) + end +end + +function staff(t) + LOCAL=t + if t.AREAS then t.AREAS=gsub(t.AREAS,"[;,] *","\n<LI>") end + local p,n=TEMPLATE + if t.WWW=="" then p=gsub(p,'<A HREF="|WWW|">|NAME|</A>',"|NAME|") end + repeat p,n=gsub(p,PAT,get) until n==0 + write(t.LOGIN,"\n") + writeto(t.LOGIN..".html") + write(p) + writeto() +end diff --git a/test/examples/www/template.html b/test/examples/www/template.html new file mode 100644 index 00000000..c8494b76 --- /dev/null +++ b/test/examples/www/template.html @@ -0,0 +1,29 @@ +<HTML> +<HEAD> +<TITLE> +Imaginary web site:Staff:|LOGIN| +</TITLE> +</HEAD> + +<BODY> +<H1><A HREF="|WWW|">|NAME|</A></H1> +<A HREF="mailto:|EMAIL|">|EMAIL|</A> +<P> + +|POSITION|<BR> +|TITLE|<P> + +<H2>Research areas</H2> +<UL> +<LI>|AREAS| +</UL> +<P> + +<HR> +Last update in |DATE| by +<A HREF="|webmast|">websmaster</A>. +<P> + +</BODY> + +</HTML> diff --git a/test/hello.lua b/test/hello.lua index 639144b6..ea818b9a 100644 --- a/test/hello.lua +++ b/test/hello.lua @@ -1,3 +1,3 @@ -- the first program in every language -print("hello world, from Lua!") +write("hello world, from Lua!\n") diff --git a/test/loop.lua b/test/loop.lua deleted file mode 100644 index b464d09e..00000000 --- a/test/loop.lua +++ /dev/null @@ -1,6 +0,0 @@ -$debug -i=0 -repeat - print(i) - i=i+1 -until i>10 diff --git a/test/lua b/test/lua new file mode 120000 index 00000000..3b0b1a28 --- /dev/null +++ b/test/lua @@ -0,0 +1 @@ +../bin/lua
\ No newline at end of file diff --git a/test/luac b/test/luac new file mode 120000 index 00000000..694bc0dc --- /dev/null +++ b/test/luac @@ -0,0 +1 @@ +../bin/luac
\ No newline at end of file diff --git a/test/sort.lua b/test/sort.lua index d665e96b..a5eb0956 100644 --- a/test/sort.lua +++ b/test/sort.lua @@ -28,7 +28,7 @@ function selectionsort(a,n) end function show(m,x) - write(m.."\n\t") + write(m,"\n\t") local i=0 while x[i] do write(x[i]) diff --git a/test/split.lua b/test/split.lua deleted file mode 100644 index 0f64c508..00000000 --- a/test/split.lua +++ /dev/null @@ -1,18 +0,0 @@ -function split (s) - local n = 1 - local f = strfind(s,"/") - while f do - n = n+f - f = strfind(strsub(s,n),"/") - end - return strsub(s,1,n-1), strsub(s,n) -end - - -function test(s) - local path, filename = split(s) - print(s .. "=[".. path.."]+["..filename.."]") -end - -test("a:/lua/obj/lua.c") -test("lua.lib") diff --git a/test/type.lua b/test/type.lua deleted file mode 100644 index 5bfc33e4..00000000 --- a/test/type.lua +++ /dev/null @@ -1,37 +0,0 @@ -$debug - -function check (object, class) - local v = next(object,nil); - while v ~= nil do - if class[v] == nil then - print("unknown field: " .. v) - elseif type(object[v]) ~= class[v].type then - print("wrong type for field " .. v) - end - v = next(object,v); - end - v = next(class,nil); - while v ~= nil do - if object[v] == nil then - if class[v].default ~= nil then - object[v] = class[v].default - else - print("field "..v.." not initialized") - end - end - v = next(class,v); - end -end - -typeblock = {x = {default = 0, type = "number"}, - y = {default = 0, type = "number"}, - name = {type = "string"} - } - -function block(t) check(t,typeblock) end - -block{ x = 7, name = "3"} -block{ x = "7", name = "3"} -block{ x = 7, name = 3} -block{ x = 7} -block{ x = 7, name = "3", bogus=3.14} |