Tue, 26 Oct 2010 10:43:56 +0100
Add tests/ to the repo
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/fail/demo_account.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,38 @@ +-- account.lua +-- from PiL 1, Chapter 16 + +Account = {balance = 0} + +function Account:new (o, name) + o = o or {name=name} + setmetatable(o, self) + self.__index = self + return o +end + +function Account:deposit (v) + self.balance = self.balance + v +end + +function Account:withdraw (v) + if v > self.balance then error("insufficient funds on account "..self.name) end + self.balance = self.balance - v +end + +function Account:show (title) + print(title or "", self.name, self.balance) +end + +a = Account:new(nil,"demo") +a:show("after creation") +a:deposit(1000.00) +a:show("after deposit") +a:withdraw(100.00) +a:show("after withdraw") + +-- this would raise an error +--[[ +b = Account:new(nil,"DEMO") +b:withdraw(100.00) +--]] +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/fail/demo_bisect.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,29 @@ +-- bisect.lua +-- bisection method for solving non-linear equations + +delta=1e-6 -- tolerance + +function bisect(f,a,b,fa,fb) + local c=(a+b)/2 + io.write(n," c=",c," a=",a," b=",b,"\n") + if c==a or c==b or math.abs(a-b)<delta then return c,b-a 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]. needs f(a)*f(b)<0 +function solve(f,a,b) + n=0 + local z,e=bisect(f,a,b,f(a),f(b)) + io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z))) +end + +-- our function +function f(x) + return x*x*x-x-1 +end + +-- find zero in [1,2] +solve(f,1,2) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/fail/demo_globals.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,24 @@ +-- globals.lua +-- show all global variables + +local seen={} + +function dump(t,i) + seen[t]=true + local s={} + local n=0 + for k in pairs(t) do + n=n+1 s[n]=k + end + table.sort(s) + for k,v in ipairs(s) do + print(i,v) + v=t[v] + if type(v)=="table" and not seen[v] then + dump(v,i.."\t") + end + end +end + +dump(_G,"") +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/fail/demo_hello.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,5 @@ +-- hello.lua +-- the first program in every language + +io.write("Hello world, from ",_VERSION,"!\n") +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/fail/demo_sieve.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,31 @@ +-- sieve.lua +-- the sieve of Eratosthenes programmed with coroutines +-- typical usage: lua -e N=1000 sieve.lua | column + +-- generate all the numbers from 2 to n +function gen (n) + return coroutine.wrap(function () + for i=2,n do coroutine.yield(i) end + end) +end + +-- filter the numbers generated by `g', removing multiples of `p' +function filter (p, g) + return coroutine.wrap(function () + while 1 do + local n = g() + if n == nil then return end + if math.mod(n, p) ~= 0 then coroutine.yield(n) end + end + end) +end + +N=N or 1000 -- from command line +x = gen(N) -- generate primes up to N +while 1 do + local n = x() -- pick a number until done + if n == nil then break end + print(n) -- must be a prime number + x = filter(n, x) -- now remove its multiples +end +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pass/hello.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,15 @@ +local a, b = "One", "Deux"; + +function pp() + local _b = b; + a = "Une" + local c = "Trois!"; + local function b() + return a, _b, c; + end + return b; +end + +print(pp()()) + +print(a)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pass/loop.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,1 @@ +for i=1,100000 do print(i) end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pass/nilglobal.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,1 @@ +print(hello) \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pass/t.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,4 @@ +local a = { two = "two" }; +a.one = "one" +local b = "one"; +print(a[b], a.two)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pass/test_test.lua Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,2 @@ +local a, b = true, false; a = a and b; +print(a);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/run.sh Tue Oct 26 10:43:56 2010 +0100 @@ -0,0 +1,28 @@ +#!/bin/bash + +failed=""; +function failtest { + echo "FAILED: $1"; + failed="$failed $1"; + failed_count=$(($failed_count+1)) +} +test_count=0 +failed_count=0 + +for script in tests/pass/*.lua; do + luac "$script" && node lvm.js >/dev/null || failtest "$script"; + test_count=$(($test_count+1)) +done + +for script in tests/fail/*.lua; do + luac "$script" && node lvm.js >/dev/null && failtest "$script"; + test_count=$(($test_count+1)) +done + +echo $(($test_count-$failed_count))"/$test_count TESTS PASSED"; +if ! [ "$failed" == "" ]; then + echo "$failed_count TESTS FAILED:"; + echo "$failed"; + exit 1; +fi +exit 0;