summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLua Team <team@lua.org>1995-11-28 12:00:00 +0000
committerrepogen <>1995-11-28 12:00:00 +0000
commit71754d2f6423fb9b6e87658e58bafc5470d53f65 (patch)
treec704e97b80e52a52d3152738941bb4c8ca676b97 /test
parenta8b6ba0954edb9e0e669e1f451b9a8f145ce5166 (diff)
downloadlua-github-2.2.tar.gz
Lua 2.22.2
Diffstat (limited to 'test')
-rw-r--r--test/README7
-rw-r--r--test/examples/complex/complex.lua82
-rw-r--r--test/examples/complex/mandel.lua25
-rw-r--r--test/examples/ps/hilbert.lua44
-rw-r--r--test/examples/ps/ps.lua185
-rw-r--r--test/save.lua37
-rw-r--r--test/type.lua10
7 files changed, 349 insertions, 41 deletions
diff --git a/test/README b/test/README
new file mode 100644
index 00000000..625644d0
--- /dev/null
+++ b/test/README
@@ -0,0 +1,7 @@
+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.
diff --git a/test/examples/complex/complex.lua b/test/examples/complex/complex.lua
new file mode 100644
index 00000000..a213a746
--- /dev/null
+++ b/test/examples/complex/complex.lua
@@ -0,0 +1,82 @@
+-- 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
new file mode 100644
index 00000000..5e3d3eb6
--- /dev/null
+++ b/test/examples/complex/mandel.lua
@@ -0,0 +1,25 @@
+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/ps/hilbert.lua b/test/examples/ps/hilbert.lua
new file mode 100644
index 00000000..b1042b57
--- /dev/null
+++ b/test/examples/ps/hilbert.lua
@@ -0,0 +1,44 @@
+-- hilbert.c
+-- Hilbert curve
+-- Luiz Henrique de Figueiredo (lhf@csg.uwaterloo.ca)
+-- 10 Nov 95
+
+$debug
+
+dofile("ps.lua")
+
+function p()
+ PS.lineto(x,y)
+end
+
+function a(n)
+ if (n==0) then return else n=n-1 end
+ d(n); y=y+h; p(); a(n); x=x+h; p(); a(n); y=y-h; p(); b(n);
+end
+
+function b(n)
+ if (n==0) then return else n=n-1 end
+ c(n); x=x-h; p(); b(n); y=y-h; p(); b(n); x=x+h; p(); a(n);
+end
+
+function c(n)
+ if (n==0) then return else n=n-1 end
+ b(n); y=y-h; p(); c(n); x=x-h; p(); c(n); y=y+h; p(); d(n);
+end
+
+function d(n)
+ if (n==0) then return else n=n-1 end
+ a(n); x=x+h; p(); d(n); y=y+h; p(); d(n); x=x-h; p(); c(n);
+end
+
+function hilbert(n)
+ PS.open("hilbert curve")
+ h=2^(9-n)
+ x=0
+ y=0
+ PS.moveto(x,y)
+ a(n)
+ PS.close()
+end
+
+hilbert(5)
diff --git a/test/examples/ps/ps.lua b/test/examples/ps/ps.lua
new file mode 100644
index 00000000..62b4e173
--- /dev/null
+++ b/test/examples/ps/ps.lua
@@ -0,0 +1,185 @@
+-- ps.lua
+-- lua interface to postscript
+-- Luiz Henrique de Figueiredo (lhf@csg.uwaterloo.ca)
+-- 12 Nov 95
+
+PS={}
+
+function P(x)
+ write(x.."\n")
+end
+
+-------------------------------------------------------------------- control --
+
+function PS.open(title)
+ local d,m,y=date()
+ local H,M,S=time()
+ if d<10 then d="0"..d end
+ if m<10 then m="0"..m end
+ if title==nil then title="(no title)" end
+ P("%!PS-Adobe-2.0 EPSF-1.2")
+ P("%%Title: "..title)
+ P("%%Creator: ps.lua from Lua 2.2")
+ P("%%CreationDate: "..y..m..d.." "..H..":"..M..":"..S)
+ P("%%Pages: (atend)")
+ P("%%BoundingBox: (atend)")
+ P("%%EndComments")
+ P("%%BeginProcSet: ps.lua")
+ P("/s { stroke } bind def")
+ P("/f { fill } bind def")
+ P("/m { moveto } bind def")
+ P("/l { lineto } bind def")
+ P("/L { moveto lineto stroke } bind def")
+ P("/t { show } bind def")
+ P("/o { 0 360 arc stroke } bind def")
+ P("/O { 0 360 arc fill } bind def")
+ P("/p { 3 0 360 arc fil } bind def")
+ P("/F { findfont exch scalefont setfont } bind def")
+ P("/LS { 0 setdash } bind def")
+ P("/LW { setlinewidth } bind def")
+ P("%%EndProcSet: ps.lua")
+ P("%%EndProlog")
+ P("%%BeginSetup")
+ P("0 setlinewidth")
+ P("1 setlinejoin")
+ P("1 setlinecap")
+ P("10 /Times-Roman F")
+ P("%%EndSetup\n")
+ P("%%Page: 1 1")
+-- cxmin=dv.xmin; cxmax=dv.xmax; cymin=dv.ymin; cymax=dv.ymax
+ xmin=1000; xmax=-1000; ymin=1000; ymax=-1000
+ page=1
+end
+
+function PS.close()
+ P("stroke")
+ P("showpage")
+ P("%%Trailer")
+ P("%%Pages: "..page)
+ P("%%BoundingBox: "..xmin.." "..ymin.." "..xmax.." "..ymax)
+ P("%%EOF")
+end
+
+function PS.clear()
+ if (empty) then return end
+ page=page+1
+ P("showpage")
+ P("%%Page: "..page.." "..page)
+ empty=1
+end
+
+function PS.comment(s)
+ P("% "..s)
+end
+
+--------------------------------------------------------------- direct color --
+
+function PS.rgbcolor(r,g,b)
+ P(r.." "..g.." "..b.." setrgbcolor")
+end
+
+function PS.gray(g)
+ P(g.." setgray")
+end
+
+---------------------------------------------------------------- named color --
+
+function PS.color(c)
+ P("C"..c)
+end
+
+function PS.defrgbcolor(c,r,g,b)
+ P("/C"..c.." { "..r.." "..g.." "..b.." setrgbcolor } def")
+end
+
+function PS.defgraycolor(c,g)
+ P("/C"..c.." { "..g.." setgray } def")
+end
+
+----------------------------------------------------------------------- line --
+
+function PS.line(x1,y1,x2,y2)
+ P(x2.." "..y2.." "..x1.." "..y1.." L")
+ PS.update(x1,y1)
+ PS.update(x2,y2)
+end
+
+function PS.moveto(x,y)
+ P(x.." "..y.." m")
+ PS.update(x,y)
+end
+
+function PS.lineto(x,y)
+ P(x.." "..y.." l")
+ PS.update(x,y)
+end
+
+function PS.linewidth(w)
+ P(w.." LW")
+end
+
+function PS.linestyle(s)
+ P("["..s.."] LS")
+end
+
+----------------------------------------------------------------------- text --
+
+function PS.font(name,size)
+ if (size==nil) then size=10 end
+ P(size.." /"..name.." F")
+end
+
+function PS.text(x,y,s)
+ P(x.." "..y.."m ("..s..") t")
+ PS.update(x,y)
+end
+
+--------------------------------------------------------------------- circle --
+
+function PS.circle(x,y,r)
+ P(x.." "..y.." "..r.." o")
+ PS.update(x-r,y-r)
+ PS.update(x+r,y+r)
+end
+
+function PS.disk(x,y,r)
+ P(x.." "..y.." "..r.." O")
+ PS.update(x-r,y-r)
+ PS.update(x+r,y+r)
+end
+
+function PS.dot(x,y)
+ P(x.." "..y.." p")
+ PS.update(x-r,y-r)
+ PS.update(x+r,y+r)
+end
+
+------------------------------------------------------------------------ box --
+
+function PS.rectangle(xmin,xmax,ymin,ymax)
+ P(xmin.." "..ymin.." m "..
+ xmax.." "..ymin.." l "..
+ xmax.." "..ymax.." l "..
+ xmin.." "..ymax.." l s")
+ PS.update(xmin,ymin)
+ PS.update(xmax,ymax)
+end
+
+function PS.box(xmin,xmax,ymin,ymax)
+ P(xmin.." "..ymin.." m "..
+ xmax.." "..ymin.." l "..
+ xmax.." "..ymax.." l "..
+ xmin.." "..ymax.." l f")
+ PS.update(xmin,ymin)
+ PS.update(xmax,ymax)
+end
+
+-------------------------------------------------------- update bounding box --
+
+function PS.update(x,y)
+-- if (x>=cxmin and x<=cxmax and y>=cxmin and y<=cxmax) then
+ if (x<xmin) then xmin=x elseif (x>xmax) then xmax=x end
+ if (y<ymin) then ymin=y elseif (y>ymax) then ymax=y end
+ empty=0
+-- end
+end
diff --git a/test/save.lua b/test/save.lua
index 8c64c568..f16bdf20 100644
--- a/test/save.lua
+++ b/test/save.lua
@@ -1,39 +1,4 @@
-
-
-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.__visited__ ~= nil then
- print(n .. "=" .. v.__visited__);
- else
- print(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
- end
-end
-
-function save ()
-local n,v
- n,v = nextvar(nil)
- while n ~= nil do
- savevar(n,v);
- n,v = nextvar(n)
- end
-end
+dofile("dump.lua")
a = 3
x = {a = 4, b = "name", l={4,5,67}}
diff --git a/test/type.lua b/test/type.lua
index 3079391d..5bfc33e4 100644
--- a/test/type.lua
+++ b/test/type.lua
@@ -30,8 +30,8 @@ typeblock = {x = {default = 0, type = "number"},
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}
+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}