summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLua Team <team@lua.org>1996-05-14 12:00:00 +0000
committerrepogen <>1996-05-14 12:00:00 +0000
commit721542976ebc89f2f8d17d19be7e4426570b69be (patch)
tree0c79a45c63aa89d6e4b8ac80931e46d74a72f8cb /test
parent71754d2f6423fb9b6e87658e58bafc5470d53f65 (diff)
downloadlua-github-2.4.tar.gz
Lua 2.42.4
Diffstat (limited to 'test')
-rw-r--r--test/bisect.lua26
-rw-r--r--test/cf.lua27
-rw-r--r--test/dump.lua2
-rw-r--r--test/examples/ps/ps.lua10
-rw-r--r--test/hello.lua3
-rw-r--r--test/long.lua22
-rw-r--r--test/sort.lua72
7 files changed, 95 insertions, 67 deletions
diff --git a/test/bisect.lua b/test/bisect.lua
new file mode 100644
index 00000000..cfb1bbfd
--- /dev/null
+++ b/test/bisect.lua
@@ -0,0 +1,26 @@
+$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)
+ local c=(a+b)/2
+ if abs(a-b)<delta then return c end
+ n=n+1
+ local fc=f(c)
+ if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
+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
+ n=0
+ local z=bisect(f,a,b,f(a),f(b))
+ print(format("after %d steps, root is %.10g",n,z))
+end
+
+-- our function
+function f(x)
+ return x*x*x-x-1
+end
+
+solve(f,1,2)
diff --git a/test/cf.lua b/test/cf.lua
new file mode 100644
index 00000000..038b5e75
--- /dev/null
+++ b/test/cf.lua
@@ -0,0 +1,27 @@
+-- temperature conversion table
+
+-- celsius to farenheit
+
+c0=-20
+while c0<50 do
+
+ c1=c0+10
+ write("C ")
+ c=c0
+ while c<c1 do
+ write(format("%3.0f ",c))
+ c=c+1
+ end
+ write("\n")
+
+ c=c0
+ write("F ")
+ while c<c1 do
+ f=(9/5)*c+32
+ write(format("%3.0f ",f))
+ c=c+1
+ end
+ write("\n\n")
+
+ c0=c1
+end
diff --git a/test/dump.lua b/test/dump.lua
index fa0aa4fb..ad8d44a6 100644
--- a/test/dump.lua
+++ b/test/dump.lua
@@ -1,4 +1,4 @@
-
+-- dump global environment
function savevar (n,v)
if v == nil then return end;
diff --git a/test/examples/ps/ps.lua b/test/examples/ps/ps.lua
index 62b4e173..0d749194 100644
--- a/test/examples/ps/ps.lua
+++ b/test/examples/ps/ps.lua
@@ -1,7 +1,7 @@
-- ps.lua
-- lua interface to postscript
-- Luiz Henrique de Figueiredo (lhf@csg.uwaterloo.ca)
--- 12 Nov 95
+-- 14 May 96
PS={}
@@ -12,15 +12,11 @@ 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("%%Creator: ps.lua from Lua 2.4")
+ P("%%CreationDate: "..date())
P("%%Pages: (atend)")
P("%%BoundingBox: (atend)")
P("%%EndComments")
diff --git a/test/hello.lua b/test/hello.lua
new file mode 100644
index 00000000..639144b6
--- /dev/null
+++ b/test/hello.lua
@@ -0,0 +1,3 @@
+-- the first program in every language
+
+print("hello world, from Lua!")
diff --git a/test/long.lua b/test/long.lua
deleted file mode 100644
index 5492fd50..00000000
--- a/test/long.lua
+++ /dev/null
@@ -1,22 +0,0 @@
-function sort(a,n) -- selection sort
- local i=1
- while i<=n do
- local m, j = i, i+1
- while j<=n do
- if a[j]<a[m] then m=j end
- j=j+1
- end
- a[i],a[m]=a[m],a[i] -- swap a[i] and a[m]
- i=i+1
- end
-end
-
-
-v = { }
-
-i=1
-while i <= 5000 do v[i] = 5000-i i=i+1 end
-
-sort(v,5000)
-
-print("v512 = ".. v[512])
diff --git a/test/sort.lua b/test/sort.lua
index 14d4cf22..d665e96b 100644
--- a/test/sort.lua
+++ b/test/sort.lua
@@ -1,28 +1,25 @@
$debug
-function quicksort(r,s)
- if s<=r then return end -- caso basico da recursao
- local v, i, j = x[r], r, s+1
- i=i+1; while x[i]<v do i=i+1 end
- j=j-1; while x[j]>v do j=j-1 end
- x[i],x[j]=x[j],x[i]
- while j>i do -- separacao
- i=i+1; while x[i]<v do i=i+1 end
- j=j-1; while x[j]>v do j=j-1 end
- x[i],x[j]=x[j],x[i]
- end
- x[i],x[j]=x[j],x[i] -- undo last swap
- x[j],x[r]=x[r],x[j]
- quicksort(r,j-1) -- recursao
- quicksort(j+1,s)
+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)
end
-function sort(a,n) -- selection sort
+function selectionsort(a,n)
local i=1
while i<=n do
local m, j = i, i+1
while j<=n do
- if a[j]<a[m] then m=j end
+ if a[j]>a[m] then m=j end -- reverse sort
j=j+1
end
a[i],a[m]=a[m],a[i] -- swap a[i] and a[m]
@@ -30,25 +27,26 @@ function sort(a,n) -- selection sort
end
end
-function main()
- x={}
- n=-1
- n=n+1; x[n]="a"
- n=n+1; x[n]="waldemar"
- n=n+1; x[n]="luiz"
- n=n+1; x[n]="lula"
- n=n+1; x[n]="peter"
- n=n+1; x[n]="raquel"
- n=n+1; x[n]="camilo"
- n=n+1; x[n]="andre"
- n=n+1; x[n]="marcelo"
- n=n+1; x[n]="sedrez"
- n=n+1; x[n]="z"
- print(x[0]..","..x[1]..","..x[2]..","..x[3]..","..x[4]..","..x[5]..","..x[6]..","..x[7]..","..x[8]..","..x[9]..","..x[10])
- quicksort(1,n-1)
- print(x[0]..","..x[1]..","..x[2]..","..x[3]..","..x[4]..","..x[5]..","..x[6]..","..x[7]..","..x[8]..","..x[9]..","..x[10])
- sort (x, n-1)
- print(x[0]..","..x[1]..","..x[2]..","..x[3]..","..x[4]..","..x[5]..","..x[6]..","..x[7]..","..x[8]..","..x[9]..","..x[10])
+function show(m,x)
+ write(m.."\n\t")
+ local i=0
+ while x[i] do
+ write(x[i])
+ i=i+1
+ if x[i] then write(",") end
+ end
+ write("\n")
end
-main()
+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)
+
+quicksort(x,1,n-1)
+show("after quicksort",x)
+
+selectionsort(x, n-1)
+show("after reverse selection sort",x)