Remove obsolete v 1.0 source files (still available in 1.0 branch).

This commit is contained in:
James Roseborough
2009-10-27 06:20:40 +00:00
parent 3863ff8e46
commit d1debdf2ec
123 changed files with 3 additions and 21813 deletions

View File

@@ -1,154 +0,0 @@
package org.luaj.jit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.luaj.compiler.LuaC;
import org.luaj.lib.BaseLib;
import org.luaj.platform.J2sePlatform;
import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype;
import org.luaj.vm.LuaState;
import org.luaj.vm.Platform;
/**
* Simple test cases for lua jit basic functional test
*/
public class LuaJitBasicTest extends TestCase {
static {
Platform.setInstance(new J2sePlatform());
LuaC.install();
}
public void testPrintHelloWorld() throws IOException {
stringTest( "print( 'hello, world' )" );
}
public void testArgPassing() throws IOException {
stringTest(
"local function f(a,b)\n" +
" print('a',a,'b',b)\n" +
" return 'c','d'\n" +
"end\n" +
"print( 'f1', f(123) )\n" +
"print( 'f2', f(456,789) )\n" +
"print( 'f3', f(12,34,56) )\n" );
}
public void testForInDoEnd() throws IOException {
stringTest(
"local t = {abc=123,def=456}\n" +
"for k,v in pairs(t) do\n" +
" print( 'k,v', k, v )\n" +
"end");
}
public void testForIEqualsDoEnd() throws IOException {
stringTest(
"print 'starting'\n" +
"for i=1,5,2 do\n" +
" print( 'i', i )\n" +
"end");
}
public void testRepeatUntil() throws IOException {
stringTest(
"local i=7\n" +
"repeat\n"+
" print(i)\n"+
"until i\n");
}
public void testWhileDoEnd() throws IOException {
stringTest(
"local i=4\n" +
"while i>0 do\n"+
" print( i )\n"+
" i = i-1\n"+
"end\n");
}
public void testForIEqualsDoBreakEnd() throws IOException {
stringTest(
"print 'starting'\n" +
"for i=1,5,2 do\n" +
" print( 'i', i )\n" +
" break\n" +
"end");
}
public void testRepeatUntilBreak() throws IOException {
stringTest(
"local i=7\n" +
"repeat\n"+
" print(i)\n"+
" break\n"+
"until i\n");
}
public void testWhileDoBreak() throws IOException {
stringTest(
"local i=4\n" +
"while i>0 do\n"+
" print( i )\n"+
" i = i-1\n"+
" break\n"+
"end\n");
}
public void testIfThenEnd() throws IOException {
stringTest(
"if a then\n" +
" print(1)\n" +
"end\n" +
"print(2)\n" );
}
public void testIfThenElseEnd() throws IOException {
stringTest(
"if a then\n" +
" print(1)\n" +
"else\n" +
" print(2)\n" +
"end\n" +
"print(3)\n" );
}
public void testIfThenElseifElseEnd() throws IOException {
stringTest(
"if a then\n" +
" print(1)\n" +
"elseif b then \n" +
" print(2)\n" +
"else\n" +
" print(3)\n" +
"end\n" +
"print(4)\n" );
}
private void stringTest(String program) throws IOException {
InputStream is = new ByteArrayInputStream(program.getBytes());
LPrototype p = LuaC.compile(is, "program");
String expected = run( p );
LPrototype q = LuaJit.jitCompile( p );
assertTrue(p!=q);
String actual = run( q );
assertEquals( expected, actual );
}
private static String run(LPrototype p) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BaseLib.redirectOutput(outputStream);
LuaState vm = Platform.newLuaState();
LClosure c = p.newClosure(vm._G);
vm.pushlvalue(c);
vm.call(0, 0);
return outputStream.toString();
}
}

View File

@@ -1,92 +0,0 @@
package org.luaj.jit;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.TestCase;
import org.luaj.compiler.LuaC;
import org.luaj.lib.BaseLib;
import org.luaj.platform.J2sePlatform;
import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype;
import org.luaj.vm.LuaState;
import org.luaj.vm.Platform;
public class LuaJitPerfTest extends TestCase {
static {
Platform.setInstance(new J2sePlatform());
LuaC.install();
}
//
// public void testFannkuch() throws IOException {
// timedFileTest( "fannkuch.lua" );
// }
//
// public void testMandelbrot() throws IOException {
// timedFileTest( "mandelbrot.lua" );
// }
public void testNbody() throws IOException {
timedFileTest( "nbody.lua" );
}
//
// public void testForLoop() throws IOException {
// timedTest( "for loop",
// "local sum=0\n" +
// "for i=1,10000,1 do\n" +
// " sum = sum + i\n" +
// "end");
// }
//
private void timedFileTest(String filename) throws IOException {
File file = new File("src/test/perf/"+filename);
int len = (int) file.length();
byte[] b = new byte[len];
DataInputStream dis = new DataInputStream( new FileInputStream( file ) );
dis.readFully(b);
dis.close();
timedTest( filename, new String(b) );
}
private void timedTest(String testName, String script) throws IOException {
System.out.println("---- "+testName+" ----");
InputStream is = new ByteArrayInputStream(script.getBytes());
LPrototype p = LuaC.compile(is, "script");
int plain = timeTrial( "plain", p );
LPrototype q = LuaJit.jitCompile( p );
assertTrue(p!=q);
int jit = timeTrial( "jit", q );
System.out.println("plain="+plain+" jit="+jit+" ratio="+(double)jit/(double)plain);
assertTrue( "jit faster than plain", jit > plain );
}
private static int timeTrial(String type, LPrototype p) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BaseLib.redirectOutput(outputStream);
LuaState vm = Platform.newLuaState();
LClosure c = p.newClosure(vm._G);
int globalCount = 0;
for ( int i=0; i<5; i++ ) {
int localCount = 0;
long t1 = System.currentTimeMillis() + 1000;
while ( t1 > System.currentTimeMillis() ) {
vm.pushlvalue(c);
vm.call(0, 0);
localCount++;
}
System.out.println(type+": "+(localCount));
System.out.flush();
globalCount += localCount;
}
return globalCount;
}
}

View File

@@ -1,21 +0,0 @@
package org.luaj.jit;
import java.io.IOException;
import org.luaj.jit.LuaJit;
import org.luaj.vm.LPrototype;
import org.luaj.vm.CompatibiltyTest;
import org.luaj.vm.LuaState;
/**
* Suite of standard tests, but using the LuaJit compiler
* for all loaded prototypes.
*/
public class LuaJitTest extends CompatibiltyTest {
protected LPrototype loadScript( LuaState state, String name ) throws IOException {
LPrototype p = super.loadScript(state, name);
return LuaJit.jitCompile(p);
}
}

View File

@@ -1,15 +0,0 @@
-- Clear out builtin math package
math = nil
local function autoload(table, key)
local pkg = require(key)
table[key] = pkg
return pkg
end
setmetatable(_G, { __index = autoload } )
-- local result = math.sqrt(9.0)
-- print("x=", result)
print("square root of 9.0 is ", math.sqrt(9.0))
print("square root of 4.0 is ", math.sqrt(4.0))

View File

@@ -1,280 +0,0 @@
-- unit tests for functions in BaseLib.java
package.path = "?.lua;src/test/res/?.lua"
require 'ids'
-- wrap pcall to return one result
-- error message are tested elsewhere
local pc = pcall
local pcall = function(...)
local s,e = pc(...)
if s then return e end
return false, type(e)
end
-- print
print()
print(11)
print("abc",123,nil,"pqr")
-- assert
print( 'assert(true)', assert(true) )
print( 'pcall(assert,true)', pcall(assert,true) )
print( 'pcall(assert,false)', pcall(assert,false) )
print( 'pcall(assert,nil)', pcall(assert,nil) )
print( 'pcall(assert,true,"msg")', pcall(assert,true,"msg") )
print( 'pcall(assert,false,"msg")', pcall(assert,false,"msg") )
print( 'pcall(assert,nil,"msg")', pcall(assert,nil,"msg") )
print( 'pcall(assert,false,"msg","msg2")', pcall(assert,false,"msg","msg2") )
-- collectgarbage (not supported)
print( 'collectgarbage("count")', type(collectgarbage("count")))
print( 'collectgarbage("collect")', type(collectgarbage("collect")))
print( 'collectgarbage("count")', type(collectgarbage("count")))
-- dofile (not supported)
-- ipairs
print( 'pcall(ipairs)', pcall(ipairs) )
print( 'pcall(ipairs,nil)', pcall(ipairs,nil) )
print( 'pcall(ipairs,"a")', pcall(ipairs,"a") )
print( 'pcall(ipairs,1)', pcall(ipairs,1) )
for k,v in ipairs({}) do print('ipairs1',k,v)end
for k,v in ipairs({'one','two'}) do print('ipairs2',k,v)end
for k,v in ipairs({aa='aaa',bb='bbb'}) do print('ipairs3',k,v)end
for k,v in ipairs({aa='aaa',bb='bbb','one','two'}) do print('ipairs4',k,v)end
for k,v in ipairs({[30]='30',[20]='20'}) do print('ipairs5',k,v)end
-- load
-- loadfile
-- loadstring
local lst = "print(3+4); return 8"
local chunk, err = loadstring( lst )
print( 'loadstring("'..lst..'")', id(chunk), err )
print( 'loadstring("'..lst..'")()', chunk() )
-- pairs
print( 'pcall(pairs)', pcall(pairs) )
print( 'pcall(pairs,nil)', pcall(pairs,nil) )
print( 'pcall(pairs,"a")', pcall(pairs,"a") )
print( 'pcall(pairs,1)', pcall(pairs,1) )
for k,v in pairs({}) do print('pairs1',k,v)end
for k,v in pairs({'one','two'}) do print('pairs2',k,v)end
for k,v in pairs({aa='aaa',bb='bbb'}) do print('pairs3',k,v)end
for k,v in pairs({aa='aaa',bb='bbb','one','two'}) do print('pairs4',k,v)end
for k,v in pairs({[20]='30',[30]='20'}) do print('pairs5',k,v)end
-- _G
print( '_G["abc"] (before)', _G["abc"] )
abc='def'
print( '_G["abc"] (after)', _G["abc"] )
-- type
print( 'type(nil)', type(nil) )
print( 'type("a")', type("a") )
print( 'type(1)', type(1) )
print( 'type(1.5)', type(1.5) )
print( 'type(function() end)', type(function() end) )
print( 'type({})', type({}) )
print( 'type(true)', type(true) )
print( 'type(false)', type(false) )
print( 'pcall(type,type)', pcall(type,type) )
print( 'pcall(type)', pcall(type) )
print( '(function() return pcall(type) end)()', (function() return pcall(type) end)() )
local function la() return pcall(type) end
print( 'la()', la() )
function ga() return pcall(type) end
print( 'ga()', ga() )
-- getfenv, setfenv: tested in setfenv.lua
-- getmetatable, setmetatable
ta = { aa1="aaa1", aa2="aaa2" }
tb = { bb1="bbb1", bb2="bbb2" }
print( 'getmetatable(ta)', getmetatable(ta) )
print( 'getmetatable(tb)', getmetatable(tb) )
print( 'setmetatable(ta),{cc1="ccc1"}', type( setmetatable(ta,{cc1="ccc1"}) ) )
print( 'setmetatable(tb),{dd1="ddd1"}', type( setmetatable(tb,{dd1="ddd1"}) ) )
print( 'getmetatable(ta)["cc1"]', getmetatable(ta)["cc1"] )
print( 'getmetatable(tb)["dd1"]', getmetatable(tb)["dd1"] )
print( 'getmetatable(1)', getmetatable(1) )
print( 'pcall(setmetatable,1)', pcall(setmetatable,1) )
print( 'pcall(setmetatable,nil)', pcall(setmetatable,nil) )
print( 'pcall(setmetatable,"ABC")', pcall(setmetatable,"ABC") )
print( 'pcall(setmetatable,function() end)', pcall(setmetatable,function() end) )
-- rawget,rawset
local mt = {aa="aaa", bb="bbb"}
mt.__index = mt
mt.__newindex = mt
local s = {cc="ccc", dd="ddd", }
local t = {cc="ccc", dd="ddd"}
setmetatable(t,mt)
print( 'pcall(rawget)', pcall(rawget))
print( 'pcall(rawget,"a")', pcall(rawget,"a"))
print( 'pcall(rawget,s)', pcall(rawget,s))
print( 'pcall(rawget,t)', pcall(rawget,t))
function printtables()
function printtable(name,t)
print( ' '..name, t["aa"], t["bb"], t["cc"], t["dd"], t["ee"], t["ff"], t["gg"] )
print( ' '..name,
rawget(t,"aa"),
rawget(t,"bb"),
rawget(t,"cc"),
rawget(t,"dd"),
rawget(t,"ee"),
rawget(t,"ff"),
rawget(t,"gg") )
end
printtable( 's', s )
printtable( 't', t )
printtable( 'mt', mt )
end
printtables()
print( 'pcall(rawset,s,"aa","www")', id(rawset(s,"aa","www")))
printtables()
print( 'pcall(rawset,s,"cc","xxx")', id(rawset(s,"cc","xxx")))
printtables()
print( 'pcall(rawset,t,"bb","yyy")', id(rawset(t,"aa","yyy")))
printtables()
print( 'pcall(rawset,t,"dd","zzz")', id(rawset(s,"dd","zzz")))
printtables()
printtables()
print( 's["ee"]="ppp"' ); s["ee"]="ppp"
printtables()
print( 's["cc"]="qqq"' ); s["cc"]="qqq"
printtables()
print( 's["bb"]="rrr"' ); t["bb"]="rrr"
printtables()
print( 's["dd"]="sss"' ); t["dd"]="sss"
printtables()
print( 's["gg"]="ttt"' ); mt["gg"]="ttt"
printtables()
-- select
print( 'pcall(select)', pcall(select) )
print( 'select(1,11,22,33,44,55)', select(1,11,22,33,44,55) )
print( 'select(2,11,22,33,44,55)', select(2,11,22,33,44,55) )
print( 'select(3,11,22,33,44,55)', select(3,11,22,33,44,55) )
print( 'select(4,11,22,33,44,55)', select(4,11,22,33,44,55) )
print( 'pcall(select,5,11,22,33,44,55)', pcall(select,5,11,22,33,44,55) )
print( 'pcall(select,6,11,22,33,44,55)', pcall(select,6,11,22,33,44,55) )
print( 'pcall(select,7,11,22,33,44,55)', pcall(select,7,11,22,33,44,55) )
print( 'pcall(select,0,11,22,33,44,55)', pcall(select,0,11,22,33,44,55) )
print( 'pcall(select,-1,11,22,33,44,55)', pcall(select,-1,11,22,33,44,55) )
print( 'pcall(select,-2,11,22,33,44,55)', pcall(select,-2,11,22,33,44,55) )
print( 'pcall(select,-4,11,22,33,44,55)', pcall(select,-4,11,22,33,44,55) )
print( 'pcall(select,-5,11,22,33,44,55)', pcall(select,-5,11,22,33,44,55) )
print( 'pcall(select,-6,11,22,33,44,55)', pcall(select,-6,11,22,33,44,55) )
print( 'pcall(select,1)', pcall(select,1) )
print( 'pcall(select,select)', pcall(select,select) )
print( 'pcall(select,{})', pcall(select,{}) )
print( 'pcall(select,"2",11,22,33)', pcall(select,"2",11,22,33) )
print( 'pcall(select,"abc",11,22,33)', pcall(select,"abc",11,22,33) )
-- tonumber
print( 'pcall(tonumber)', pcall(tostring) )
print( 'pcall(tonumber,nil)', pcall(tonumber,nil) )
print( 'pcall(tonumber,"abc")', pcall(tonumber,"abc") )
print( 'pcall(tonumber,"123")', pcall(tonumber,"123") )
print( 'pcall(tonumber,"123",10)', pcall(tonumber,"123", 10) )
print( 'pcall(tonumber,"123",8)', pcall(tonumber,"123", 8) )
print( 'pcall(tonumber,"123",6)', pcall(tonumber,"123", 6) )
print( 'pcall(tonumber,"10101",4)', pcall(tonumber,"10101", 4) )
print( 'pcall(tonumber,"10101",3)', pcall(tonumber,"10101", 3) )
print( 'pcall(tonumber,"10101",2)', pcall(tonumber,"10101", 2) )
print( 'pcall(tonumber,"1a1",16)', pcall(tonumber,"1a1", 16) )
print( 'pcall(tonumber,"1a1",32)', pcall(tonumber,"1a1", 32) )
print( 'pcall(tonumber,"1a1",54)', pcall(tonumber,"1a1", 54) )
print( 'pcall(tonumber,"1a1",1)', pcall(tonumber,"1a1", 1) )
print( 'pcall(tonumber,"1a1",0)', pcall(tonumber,"1a1", 0) )
print( 'pcall(tonumber,"1a1",-1)', pcall(tonumber,"1a1", -1) )
print( 'pcall(tonumber,"1a1","32")', pcall(tonumber,"1a1", "32") )
print( 'pcall(tonumber,"123","456")', pcall(tonumber,"123","456") )
print( 'pcall(tonumber,"1a1",10)', pcall(tonumber,"1a1", 10) )
print( 'pcall(tonumber,"151",4)', pcall(tonumber,"151", 4) )
print( 'pcall(tonumber,"151",3)', pcall(tonumber,"151", 3) )
print( 'pcall(tonumber,"151",2)', pcall(tonumber,"151", 2) )
print( 'pcall(tonumber,"123",8,8)', pcall(tonumber,"123", 8, 8) )
print( 'pcall(tonumber,123)', pcall(tonumber,123) )
print( 'pcall(tonumber,true)', pcall(tonumber,true) )
print( 'pcall(tonumber,false)', pcall(tonumber,false) )
print( 'pcall(tonumber,tonumber)', pcall(tonumber,tonumber) )
print( 'pcall(tonumber,function() end)', pcall(tonumber,function() end) )
print( 'pcall(tonumber,{"one","two",a="aa",b="bb"})', pcall(tonumber,{"one","two",a="aa",b="bb"}) )
print( 'pcall(tonumber,"123.456")', pcall(tonumber,"123.456") )
print( 'pcall(tonumber," 123.456")', pcall(tonumber," 123.456") )
print( 'pcall(tonumber," 234qwer")', pcall(tonumber," 234qwer") )
print( 'pcall(tonumber,"0x20")', pcall(tonumber,"0x20") )
print( 'pcall(tonumber," 0x20")', pcall(tonumber," 0x20") )
print( 'pcall(tonumber,"0x20 ")', pcall(tonumber,"0x20 ") )
print( 'pcall(tonumber," 0x20 ")', pcall(tonumber," 0x20 ") )
print( 'pcall(tonumber,"0X20")', pcall(tonumber,"0X20") )
print( 'pcall(tonumber," 0X20")', pcall(tonumber," 0X20") )
print( 'pcall(tonumber,"0X20 ")', pcall(tonumber,"0X20 ") )
print( 'pcall(tonumber," 0X20 ")', pcall(tonumber," 0X20 ") )
print( 'pcall(tonumber,"0x20",10)', pcall(tonumber,"0x20",10) )
print( 'pcall(tonumber,"0x20",16)', pcall(tonumber,"0x20",16) )
print( 'pcall(tonumber,"0x20",8)', pcall(tonumber,"0x20",8) )
-- tostring
print( 'pcall(tostring)', pcall(tostring) )
print( 'pcall(tostring,nil)', pcall(tostring,nil) )
print( 'pcall(tostring,"abc")', pcall(tostring,"abc") )
print( 'pcall(tostring,"abc","def")', pcall(tostring,"abc","def") )
print( 'pcall(tostring,123)', pcall(tostring,123) )
print( 'pcall(tostring,true)', pcall(tostring,true) )
print( 'pcall(tostring,false)', pcall(tostring,false) )
print( 'tostring(tostring)', type(tostring(tostring)) )
print( 'tostring(function() end)', type(tostring(function() end)) )
print( 'tostring({"one","two",a="aa",b="bb"})', type(tostring({"one","two",a="aa",b="bb"})) )
-- unpack
print( 'pcall(unpack)', pcall(unpack) );
print( 'pcall(unpack,nil)', pcall(unpack,nil) );
print( 'pcall(unpack,"abc")', pcall(unpack,"abc") );
print( 'pcall(unpack,1)', pcall(unpack,1) );
print( 'unpack({"aa"})', unpack({"aa"}) );
print( 'unpack({"aa","bb"})', unpack({"aa","bb"}) );
print( 'unpack({"aa","bb","cc"})', unpack({"aa","bb","cc"}) );
local t = {"aa","bb","cc","dd","ee","ff"}
print( 'pcall(unpack,t)', pcall(unpack,t) );
print( 'pcall(unpack,t,2)', pcall(unpack,t,2) );
print( 'pcall(unpack,t,2,5)', pcall(unpack,t,2,5) );
print( 'pcall(unpack,t,2,6)', pcall(unpack,t,2,6) );
print( 'pcall(unpack,t,2,7)', pcall(unpack,t,2,7) );
print( 'pcall(unpack,t,1)', pcall(unpack,t,1) );
print( 'pcall(unpack,t,1,5)', pcall(unpack,t,1,5) );
print( 'pcall(unpack,t,1,6)', pcall(unpack,t,1,6) );
print( 'pcall(unpack,t,1,7)', pcall(unpack,t,1,7) );
print( 'pcall(unpack,t,0)', pcall(unpack,t,0) );
print( 'pcall(unpack,t,0,5)', pcall(unpack,t,0,5) );
print( 'pcall(unpack,t,0,6)', pcall(unpack,t,0,6) );
print( 'pcall(unpack,t,0,7)', pcall(unpack,t,0,7) );
print( 'pcall(unpack,t,-1)', pcall(unpack,t,-1) );
print( 'pcall(unpack,t,-1,5)', pcall(unpack,t,-1,5) );
print( 'pcall(unpack,t,-1,6)', pcall(unpack,t,-1,6) );
print( 'pcall(unpack,t,-1,7)', pcall(unpack,t,-1,7) );
print( 'pcall(unpack,t,2,4)', pcall(unpack,t,2,4) );
print( 'pcall(unpack,t,2,5)', pcall(unpack,t,2,5) );
print( 'pcall(unpack,t,2,6)', pcall(unpack,t,2,6) );
print( 'pcall(unpack,t,2,7)', pcall(unpack,t,2,7) );
print( 'pcall(unpack,t,2,8)', pcall(unpack,t,2,8) );
print( 'pcall(unpack,t,2,2)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,1)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,0)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,-1)', pcall(unpack,t,2,-1) );
t[0] = 'zz'
t[-1] = 'yy'
t[-2] = 'xx'
print( 'pcall(unpack,t,0)', pcall(unpack,t,0) );
print( 'pcall(unpack,t,2,0)', pcall(unpack,t,2,0) );
print( 'pcall(unpack,t,2,-1)', pcall(unpack,t,2,-1) );
print( 'pcall(unpack,t,"3")', pcall(unpack,t,"3") );
print( 'pcall(unpack,t,"a")', pcall(unpack,t,"a") );
print( 'pcall(unpack,t,function() end)', pcall(unpack,t,function() end) );
-- _VERSION
print( '_VERSION', type(_VERSION) )

View File

@@ -1,31 +0,0 @@
t = true
f = false
n = nil
s = "Hello"
z = 0
one = 1
print(t)
print(f)
print(not t)
print(not f)
print(not n)
print(not z)
print(not s)
print(not(not(t)))
print(not(not(z)))
print(not(not(n)))
print(t and f)
print(t or f)
print(f and t)
print(f or t)
print(f or one)
print(f or z)
print(f or n)
print(t and one)
print(t and z)
print(t and n)

View File

@@ -1,28 +0,0 @@
local function f(x)
-- tailcall to a builtin
return math.sin(x)
end
local function factorial(i)
local function helper(product, n)
if n <= 0 then
return product
else
-- tail call to a nested Lua function
return helper(n * product, n - 1)
end
end
return helper(1, i)
end
local result1 = factorial(5)
print(result1)
print(factorial(5))
local function truncate(x)
local s = tostring(x)
return (#s<6 and s) or string.sub(s,1,6)..'...'
end
local result2 = f(math.pi)
print(truncate(result2))
print(truncate(f(math.pi)))

View File

@@ -1,5 +0,0 @@
print("5.0"+3.1)
print(4+"7.5")
print(3.14*" 2.75")
print("-33"/"11")
print(-"-5")

View File

@@ -1,12 +0,0 @@
if 2 < 5 then
print("OK!")
else
print("Something is not right")
end
print(2 < 5)
print(2 <= 5)
print(2 == 5)
print(2 ~= 5)
print(2 > 5)
print(2 >= 5)

View File

@@ -1,8 +0,0 @@
#!/bin/bash
TESTS=`echo *.lua`
for x in $TESTS
do
echo compiling $x
luac -l -o ${x}c ${x}
lua ${x}c
done

View File

@@ -1,97 +0,0 @@
function printrunning()
if coroutine.running() == nil then
print("running is nil");
else
print("running is not nil")
end
end
function foo (a)
print("foo", a)
return coroutine.yield(2*a)
end
co = coroutine.create(function (a,b)
print("co-body", a, b)
local r = foo(a+1)
print("co-body", r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
printrunning()
print("co.status.inside",coroutine.status(co));
local co2 = coroutine.create(function()
print("co.status.inside2",coroutine.status(co));
end)
print("co.status.inside",coroutine.status(co));
coroutine.resume(co2);
return b, "end"
end)
function exercise()
printrunning()
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, 1, 10))
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, "r"))
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, "x", "y"))
print("co.status",coroutine.status(co));
print("main", coroutine.resume(co, "x", "y"))
print("co.status",coroutine.status(co));
end
exercise();
co = coroutine.create(function (a,b)
print("co-body", a, b)
-- TODO: make java and C behave the same for yielding in pcalls
-- local status,r = pcall( foo, a+1 )
foo(a+1)
print("co-body", status,r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
return b, "end"
end)
exercise();
-- wrap test
local g = coroutine.wrap(function (a,b)
print("co-body", a, b)
local r = foo(a+1)
print("co-body", r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
return b, "end"
end )
print("g", g(1, 10))
print("g", g("r"))
print("g", g("x", "y"))
local s,e = pcall( g, "x", "y" )
print("g", string.match(e,'cannot resume dead coroutine') or 'badmessage: '..tostring(e))
-- varargs passing
local echo = function(msg,...)
print( msg, ...)
return ...
end
local echocr = function(...)
echo('(echocr) first args', unpack(arg,1,arg.n))
local a = arg
while true do
a = { echo( '(echoch) yield returns', coroutine.yield( unpack(a) ) ) }
end
end
local c = coroutine.create( echocr )
local step = function(...)
echo( '(main) resume returns',
coroutine.resume(c, echo('(main) sending args', ...)) )
end
step(111,222,333)
step()
step(111)
step(111,222,333)

View File

@@ -1,227 +0,0 @@
local print,tostring,_G,pcall,ipairs,isnumber = print,tostring,_G,pcall,ipairs,isnumber
local e,f,g,h,s
print( 'has debug', debug~=nil )
if not debug then error( 'no debug' ) end
print( '----- debug.getfenv, debug.setfenv' )
f = function(a)
return 'f:'..tostring(a)..'|'..tostring(b)
end
s,e,g = pcall( debug.getfenv, f )
print( s, type(e), type(g), (e==G), pcall( f, 'abc' ) )
s,e,g = pcall( debug.setfenv, f, {b='def'} )
print( s, type(e), type(g), (e==G), pcall( f, 'abc' ) )
s,e,g = pcall( debug.getfenv, f )
print( s, type(e), type(g), (e==G), pcall( f, 'abc' ) )
print( '----- debug.getlocal, debug.setlocal' )
h = function(v,i,n)
s = 'h-'..v..'-'..i
local x1,y1 = debug.getlocal(v,i)
local x2,y2 = debug.setlocal(v,i,n)
local x3,y3 = debug.getlocal(v,i)
return s..' -> '..v..'-'..i..' '..
'get='..tostring(x1)..','..tostring(y1)..' '..
'set='..tostring(x2)..','..tostring(y2)..' '..
'get='..tostring(x3)..','..tostring(y3)..' '
end
g = function(...)
local p,q,r=7,8,9
local t = h(...)
local b = table.concat({...},',')
return t..'\tg locals='..p..','..q..','..r..' tbl={'..b..'}'
end
f = function(a,b,c)
local d,e,f = 4,5,6
local t = g(a,b,c)
return t..'\tf locals='..','..a..','..b..','..c..','..d..','..e..','..f
end
do lvl=1,1
for lcl=3,7 do
print( pcall( f, lvl, lcl, '#' ) )
end
end
do lvl=2,3
for lcl=0,7 do
print( pcall( f, lvl, lcl, '#' ) )
end
end
print( '----- debug.getupvalue, debug.setupvalue' )
local m,n,o = 101,102,103
f = function(p,q,r)
local p,q,r = 104,105,106
local g = function(s,t,u)
local v,w,x = 107,108,109
return function()
return m,n,o,p,q,r,v,w,x
end
end
return g
end
g = f()
h = g()
local callh = function()
local t = {}
for i,v in ipairs( { pcall(h) } ) do
t[i] = tostring(v)
end
return table.concat(t,',')
end
print( 'h', h() )
local funs = { f, g, h }
local names = { 'f', 'g', 'h' }
for i=1,3 do
local fun,name = funs[i],names[i]
for index=0,10 do
local s1,x1,y1 = pcall( debug.getupvalue, fun, index )
local s2,x2,y2 = pcall( debug.setupvalue, fun, index, 666000+i*111000+index )
local s3,x3,y3 = pcall( debug.getupvalue, fun, index )
print( name..' -> '..i..'-'..index..' '..
'get='..tostring(s1)..','..tostring(x1)..','..tostring(y1)..' '..
'set='..tostring(s2)..','..tostring(x2)..','..tostring(y2)..' '..
'get='..tostring(s3)..','..tostring(x3)..','..tostring(y3)..' '..
'tbl='..callh() )
end
end
print( '----- debug.setmetatable, debug.getmetatable' )
local a = {a='bbb'}
local b = {}
local mt = {__index={b='ccc'}}
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s1,x1,y1 = pcall( debug.getmetatable, a )
local s2,x2,y2 = pcall( debug.setmetatable, a, mt )
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s3,x3,y3 = pcall( debug.getmetatable, a )
local s4,x4,y4 = pcall( debug.getmetatable, b )
local s5,x5,y5 = pcall( debug.setmetatable, a, nil )
print( 'a.a='..tostring(a.a)..' a.b='..tostring(a.b)..' b.a='..tostring(b.a)..' b.b='..tostring(b.b))
local s6,x6,y6 = pcall( debug.getmetatable, a )
if not s1 then print( 's1 error', x1 ) end
if not s2 then print( 's2 error', x2 ) end
if not s3 then print( 's3 error', x3 ) end
if not s4 then print( 's4 error', x4 ) end
if not s5 then print( 's5 error', x5 ) end
if not s6 then print( 's6 error', x6 ) end
print( 'get='..tostring(s1)..','..tostring(x1==nil)..','..tostring(y1) )
print( 'set='..tostring(s2)..','..tostring(x2==a)..','..tostring(y2) )
print( 'get='..tostring(s3)..','..tostring(x3==mt)..','..tostring(y3) )
print( 'get='..tostring(s4)..','..tostring(x4==nil)..','..tostring(y4) )
print( 'set='..tostring(s5)..','..tostring(x5==a)..','..tostring(y5) )
print( 'get='..tostring(s6)..','..tostring(x6==nil)..','..tostring(y6) )
print( pcall( debug.getmetatable, 1 ) )
-- print( pcall( debug.setmetatable, 1, {} ) )
-- print( pcall( debug.setmetatable, 1, nil ) )
print( '----- debug.getinfo' )
local printfield = function(tbl, field)
local x = tbl[field]
if x == nil then return end
local typ = type(x)
if typ=='table' then
x = '{'..table.concat(x,',')..'}'
elseif typ=='function' then
x = typ
end
print( ' '..field..': '..tostring(x) )
end
local fields = { 'source', 'short_src', 'what',
'currentline', 'linedefined', 'lastlinedefined',
'nups', 'func', 'activelines' }
local printinfo = function(...)
for i,a in ipairs(arg) do
if type(a) == 'table' then
for j,field in ipairs(fields) do
printfield( a, field)
end
else
print( tostring(a) )
end
end
end
function test()
local x = 5
function f()
x = x + 1
return x
end
function g()
x = x - 1
print( '---' )
printinfo( 'debug.getinfo(1)', debug.getinfo(1) )
printinfo( 'debug.getinfo(1,"")', debug.getinfo(1, "") )
printinfo( 'debug.getinfo(1,"l")', debug.getinfo(1, "l") )
printinfo( 'debug.getinfo(1,"fL")', debug.getinfo(1, "fL") )
printinfo( 'debug.getinfo(2)', debug.getinfo(2) )
printinfo( 'debug.getinfo(2,"l")', debug.getinfo(2, "l") )
printinfo( 'debug.getinfo(2,"fL")', debug.getinfo(2, "fL") )
printinfo( 'debug.getinfo(10,"")', pcall( debug.getinfo, 10, "" ) )
printinfo( 'debug.getinfo(-10,"")', pcall( debug.getinfo, -10, "" ) )
--[[
for i=1,3 do
printinfo( 'debug.traceback("msg")', debug.traceback('msg') )
printinfo( 'debug.traceback("another",'..i..')', debug.traceback('another',i) )
end
--]]
print( '---' )
return x
end
print(f())
print(g())
return f, g
end
local options = "nSlufL"
local e,f,g = pcall( test )
print( 'e,f,g', e, type(f), type(g) )
printinfo( 'debug.getinfo(f)', pcall(debug.getinfo, f) )
printinfo( 'debug.getinfo(f,"'..options..'")', pcall(debug.getinfo, f, options) )
for j=1,6 do
local opts = options:sub(j,j)
printinfo( 'debug.getinfo(f,"'..opts..'")', pcall(debug.getinfo, f, opts) )
end
printinfo( 'debug.getinfo(g)', pcall(debug.getinfo, g) )
printinfo( 'debug.getinfo(test)', pcall(debug.getinfo, test) )
print( '----- debug.sethook, debug.gethook' )
f = function(x)
g = function(y)
return math.min(x,h)
end
local a = g(x)
return a + a
end
local hook = function(...)
print( ' ... in hook', ... )
end
local tryfunc = function(hook,mask,func,arg)
local x,f,h,m
pcall( function()
debug.sethook(hook,mask)
x = func(arg)
f,h,m = debug.gethook()
end )
debug.sethook()
return x,f,h,m
end
local tryhooks = function(mask)
local s1,a1,b1,c1,d1 = pcall( tryfunc, hook, mask, f, 333 )
print( 'hook = '..mask..' -> '..
'result='..tostring(s1)..','..tostring(a1)..','..
type(b1)..','..type(c1)..','..
tostring(b1==f)..','..tostring(c1==hook)..','..
tostring(d1)..' ' )
end
--[[
tryhooks("c")
tryhooks("r")
tryhooks("l")
tryhooks("crl")
--]]

View File

@@ -1,121 +0,0 @@
-- object ids
package.path = "?.lua;src/test/res/?.lua"
require 'ids'
-- test of common types of errors
local function c(f,...) return f(...) end
local function b(...) return c(...) end
local function a(...) return (pcall(b,...)) end
s = 'some string'
local t = {}
-- error message tests
print( 'a(error)', a(error) )
print( 'a(error,"msg")', a(error,"msg") )
print( 'a(error,"msg",0)', a(error,"msg",0) )
print( 'a(error,"msg",1)', a(error,"msg",1) )
print( 'a(error,"msg",2)', a(error,"msg",2) )
print( 'a(error,"msg",3)', a(error,"msg",3) )
print( 'a(error,"msg",4)', a(error,"msg",4) )
print( 'a(error,"msg",5)', a(error,"msg",5) )
print( 'a(error,"msg",6)', a(error,"msg",6) )
-- call errors
print( 'a(nil())', a(function() return n() end) )
print( 'a(t()) ', a(function() return t() end) )
print( 'a(s()) ', a(function() return s() end) )
print( 'a(true())', a(function() local b = true; return b() end) )
-- arithmetic errors
print( 'a(nil+1)', a(function() return nil+1 end) )
print( 'a(a+1) ', a(function() return a+1 end) )
print( 'a(s+1) ', a(function() return s+1 end) )
print( 'a(true+1)', a(function() local b = true; return b+1 end) )
-- table errors
print( 'a(nil.x)', a(function() return n.x end) )
print( 'a(a.x) ', a(function() return a.x end) )
print( 'a(s.x) ', a(function() return s.x end) )
print( 'a(true.x)', a(function() local b = true; return b.x end) )
print( 'a(nil.x=5)', a(function() n.x=5 end) )
print( 'a(a.x=5) ', a(function() a.x=5 end) )
print( 'a(s.x=5) ', a(function() s.x=5 end) )
print( 'a(true.x=5)', a(function() local b = true; b.x=5 end) )
-- len operator
print( 'a(#nil) ', a(function() return #n end) )
print( 'a(#t) ', a(function() return #t end) )
print( 'a(#s) ', a(function() return #s end) )
print( 'a(#a) ', a(function() return #a end) )
print( 'a(#true)', a(function() local b = true; return #b end) )
-- comparison errors
print( 'a(nil>1)', a(function() return nil>1 end) )
print( 'a(a>1) ', a(function() return a>1 end) )
print( 'a(s>1) ', a(function() return s>1 end) )
print( 'a(true>1)', a(function() local b = true; return b>1 end) )
-- unary minus errors
print( 'a(-nil)', a(function() return -n end) )
print( 'a(-a) ', a(function() return -a end) )
print( 'a(-s) ', a(function() return -s end) )
print( 'a(-true)', a(function() local b = true; return -b end) )
-- string concatenation
local function concatsuite(comparefunc)
print( '"a".."b"', comparefunc("a","b") )
print( '"a"..nil', comparefunc("a",nil) )
print( 'nil.."b"', comparefunc(nil,"b") )
print( '"a"..{}', comparefunc("a",{}) )
print( '{}.."b"', comparefunc({},"b") )
print( '"a"..2', comparefunc("a",2) )
print( '2.."b"', comparefunc(2,"b") )
print( '"a"..print', comparefunc("a",print) )
print( 'print.."b"', comparefunc(print,"b") )
print( '"a"..true', comparefunc("a",true) )
print( 'true.."b"', comparefunc(true,"b") )
print( 'nil..true', comparefunc(nil,true) )
print( '"a"..3.5', comparefunc("a",3.5) )
print( '3.5.."b"', comparefunc(3.5,"b") )
end
local function strconcat(a,b)
return (pcall( function() return a..b end) )
end
local function tblconcat(a,b)
local t={a,b}
return (pcall( function() return table.concat(t,'-') end ))
end
-- concatsuite(strconcat)
concatsuite(tblconcat)
-- pairs
print( 'a(pairs(nil))', a(function() return id(pairs(nil,{})) end) )
print( 'a(pairs(a)) ', a(function() return id(pairs(a,{})) end) )
print( 'a(pairs(s)) ', a(function() return id(pairs(s,{})) end) )
print( 'a(pairs(t)) ', a(function() return id(pairs(t,{})) end) )
print( 'a(pairs(true))', a(function() local b = true; return id(pairs(b,{})) end) )
-- setmetatable
function sm(...)
return id(setmetatable(...))
end
print( 'a(setmetatable(nil))', a(function() return sm(nil,{}) end) )
print( 'a(setmetatable(a)) ', a(function() return sm(a,{}) end) )
print( 'a(setmetatable(s)) ', a(function() return sm(s,{}) end) )
print( 'a(setmetatable(true))', a(function() local b = true; return sm(b,{}) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(setmetatable(t*)) ', a(function() return sm(t,{__metatable={}}) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
t = {}
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(setmetatable(t*)) ', a(function() return sm(t,{__metatable='some string'}) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t,{}) end) )
print( 'a(setmetatable(t,nil)) ', a(function() return sm(t,nil) end) )
print( 'a(setmetatable(t)) ', a(function() return sm(t) end) )
print( 'a(setmetatable({},"abc")) ', a(function() return sm({},'abc') end) )
-- bad args to error!
print( 'error("msg","arg")', a(function() error('some message', 'some bad arg') end) )
-- loadfile, dofile on missing files
print( 'loadfile("bogus.txt")', a(function() return loadfile("bogus.txt") end) )
print( 'dofile("bogus.txt")', a(function() return dofile("bogus.txt") end) )

View File

@@ -1,43 +0,0 @@
-- test tables with more than 50 elements
local t = { 1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
}
print ("#t=",#t,'t[1,50,51,59]', t[1], t[50], t[51], t[59])
print (table.concat(t,','))
local t2= { 0,3,4,7,9,8,12,15,23,5,
10,13,14,17,19,18,112,115,123,15,
20,33,24,27,29,28,212,215,223,25,
40,43,44,47,49,48,412,415,423,45,
50,53,54,57,59,58,512,515,523,55,
60,63,64,67,69,68,612,615,623,65,
70,73,74,77,79,78,72,715,723,75,
}
print ("#t2=",#t2,'t[1,50,51,59]', t[1], t[50], t[51], t[59])
print (table.concat(t2,','))
local t = {
[2000]='a', [2001]='b', [2002]='c', [2003]='d', [2004]='e', [2005]='f', [2006]='g', [2007]='h', [2008]='i', [2009]='j',
[3000]='a', [3001]='b', [3002]='c', [3003]='d', [3004]='e', [3005]='f', [3006]='g', [3007]='h', [3008]='i', [3009]='j',
[4000]='a', [4001]='b', [4002]='c', [4003]='d', [4004]='e', [4005]='f', [4006]='g', [4007]='h', [4008]='i', [4009]='j',
[5000]='a', [5001]='b', [5002]='c', [5003]='d', [5004]='e', [5005]='f', [5006]='g', [5007]='h', [5008]='i', [5009]='j',
[6000]='a', [6001]='b', [6002]='c', [6003]='d', [6004]='e', [6005]='f', [6006]='g', [6007]='h', [6008]='i', [6009]='j',
[7000]='a', [7001]='b', [7002]='c', [7003]='d', [7004]='e', [7005]='f', [7006]='g', [7007]='h', [7008]='i', [7009]='j',
[8000]='a', [8001]='b', [8002]='c', [8003]='d', [8004]='e', [8005]='f', [8006]='g', [8007]='h', [8008]='i', [8009]='j',
}
for i=2000,8000,1000 do
for j=0,9,1 do
print( 't['..tostring(i+j)..']', t[i+j] )
end
end

View File

@@ -1,36 +0,0 @@
-- utility to give tables and functions uniform ids for testing
ids = {}
-- return unique id for the value, independent of object id
function id(obj)
if not obj or type(obj) == 'number' or type(obj) == 'string' or type(obj) == 'boolean' then
return obj
end
local v = ids[obj]
if v then
return v
end
table.insert(ids,obj)
ids[obj] = type(obj)..'.'..tostring(#ids)
return ids[obj]
end
-- return key-value pairs sorted by keys
function spairs(unsorted)
local keys = {}
for k,v in pairs(unsorted) do
table.insert(keys,k)
end
table.sort(keys)
local lookup = {}
for i,k in ipairs(keys) do
lookup[k] = i;
end
local iterator = function(tbl,key)
local i = lookup[key]
local j,k = next(keys,i)
if not j then return nil end
return k,unsorted[k]
end
return iterator, table, nil
end

View File

@@ -1,115 +0,0 @@
-- simple io-library tests
print( io ~= nil )
print( io.open ~= nil )
print( io.stdin ~= nil )
print( io.stdout ~= nil )
print( io.stderr ~= nil )
print( 'write', io.write() )
print( 'write', io.write("This") )
print( 'write', io.write(" is a pen.\n") )
print( 'flush', io.flush() )
local f = io.open("abc.txt","w")
print( 'f', type(f) )
print( io.type(f) )
print( 'write', f:write("abcdef 12345 \t\r\n\t 678910 more\naaaaaa\rbbb\nthe rest") )
print( 'type(f)', io.type(f) )
print( 'close', f:close() )
print( 'type(f)', io.type(f) )
print( 'type("f")', io.type("f") )
local g = io.open("abc.txt","r")
local t = { g:read(3, 3, "*n", "*n", "*l", "*l", "*a") }
for i,v in ipairs(t) do
print( string.format("%q",tostring(v)), type(v))
end
local h,s = io.open("abc.txt", "a")
print( 'h', io.type(h), s )
print( 'write', h:write('\nmore text\neven more text\n') )
print( 'close', h:close() )
local j = io.open( "abc.txt", "r" )
print( 'j', io.type(j) )
print( 'seek', j:seek("set", 3) )
print( 'read', j:read(4), j:read(3) )
print( 'seek', j:seek("set", 2) )
print( 'read', j:read(4), j:read(3) )
print( 'seek', j:seek("cur", -8 ) )
print( 'read', j:read(4), j:read(3) )
print( 'seek(cur,0)', j:seek("cur",0) )
print( 'seek(cur,20)', j:seek("cur",20) )
print( 'seek(end,-5)', j:seek("end", -5) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
for l in io.lines("abc.txt") do
print( string.format('%q',l) )
end
io.input("abc.txt")
for l in io.lines() do
print( string.format('%q',l) )
end
io.input(io.open("abc.txt","r"))
for l in io.lines() do
print( string.format('%q',l) )
end
io.input("abc.txt")
io.input(io.input())
for l in io.lines() do
print( string.format('%q',l) )
end
local count = 0
io.tmpfile = function()
count = count + 1
return io.open("tmp"..count..".out","w")
end
local a = io.tmpfile()
local b = io.tmpfile()
print( io.type(a) )
print( io.type(b) )
print( "a:write", a:write('aaaaaaa') )
print( "b:write", b:write('bbbbbbb') )
print( "a:setvbuf", a:setvbuf("no") )
print( "a:setvbuf", a:setvbuf("full",1024) )
print( "a:setvbuf", a:setvbuf("line") )
print( "a:write", a:write('ccccc') )
print( "b:write", b:write('ddddd') )
print( "a:flush", a:flush() )
print( "b:flush", b:flush() )
--[[
print( "a:read", a:read(7) )
print( "b:read", b:read(7) )
print( "a:seek", a:seek("cur",-4) )
print( "b:seek", b:seek("cur",-4) )
print( "a:write", a:read(7) )
print( "b:write", b:read(7) )
--]]
local pcall = function(...) return ( pcall(...) )end
print( 'a:close', pcall( a.close, a ) )
print( 'a:write', pcall( a.write, a, 'eee') )
print( 'a:flush', pcall( a.flush, a) )
print( 'a:read', pcall( a.read, a, 5) )
print( 'a:lines', pcall( a.lines, a) )
print( 'a:seek', pcall( a.seek, a, "cur", -2) )
print( 'a:setvbuf', pcall( a.setvbuf, a, "no") )
print( 'a:close', pcall( a.close, a ) )
print( 'io.type(a)', pcall( io.type, a ) )
print( 'io.close()', pcall( io.close ) )
print( 'io.close(io.output())', pcall( io.close, io.output() ) )
io.output('abc.txt')
print( 'io.close()', pcall( io.close ) )
print( 'io.write', pcall( io.write, 'eee') )
print( 'io.flush', pcall( io.flush) )
print( 'io.close', pcall( io.close ) )
io.input('abc.txt'):close()
print( 'io.read', pcall( io.read, 5) )
print( 'io.lines', pcall( io.lines) )

View File

@@ -1,22 +0,0 @@
-- This script tests the "generic" for loop with a script iterator.
local function stuff()
local function i(o)
if o.counter > 3 then
return nil
else
local v = o.counter
o.counter = v + 1
return v
end
end
return i, { counter=1 }
end
local function testfor()
for x in stuff() do
print(x)
end
end
testfor()

View File

@@ -1,31 +0,0 @@
-- test program with more than 50 non-sequential integer elements
local t0000='a'; local t0001='b'; local t0002='c'; local t0003='d'; local t0004='e'; local t0005='f'; local t0006='g'; local t0007='h'; local t0008='i'; local t0009='j';
local t1000='a'; local t1001='b'; local t1002='c'; local t1003='d'; local t1004='e'; local t1005='f'; local t1006='g'; local t1007='h'; local t1008='i'; local t1009='j';
local t2000='a'; local t2001='b'; local t2002='c'; local t2003='d'; local t2004='e'; local t2005='f'; local t2006='g'; local t2007='h'; local t2008='i'; local t2009='j';
local t3000='a'; local t3001='b'; local t3002='c'; local t3003='d'; local t3004='e'; local t3005='f'; local t3006='g'; local t3007='h'; local t3008='i'; local t3009='j';
local t4000='a'; local t4001='b'; local t4002='c'; local t4003='d'; local t4004='e'; local t4005='f'; local t4006='g'; local t4007='h'; local t4008='i'; local t4009='j';
local t5000='a'; local t5001='b'; local t5002='c'; local t5003='d'; local t5004='e'; local t5005='f'; local t5006='g'; local t5007='h'; local t5008='i'; local t5009='j';
local t6000='a'; local t6001='b'; local t6002='c'; local t6003='d'; local t6004='e'; local t6005='f'; local t6006='g'; local t6007='h'; local t6008='i'; local t6009='j';
local t7000='a'; local t7001='b'; local t7002='c'; local t7003='d'; local t7004='e'; local t7005='f'; local t7006='g'; local t7007='h'; local t7008='i'; local t7009='j';
local t8000='a'; local t8001='b'; local t8002='c'; local t8003='d'; local t8004='e'; local t8005='f'; local t8006='g'; local t8007='h'; local t8008='i'; local t8009='j';
local t9000='a'; local t9001='b'; local t9002='c'; local t9003='d'; local t9004='e'; local t9005='f'; local t9006='g'; local t9007='h'; local t9008='i'; local t9009='j';
local t10000='a'; local t10001='b'; local t10002='c'; local t10003='d'; local t10004='e'; local t10005='f'; local t10006='g'; local t10007='h'; local t10008='i'; local t10009='j';
local t11000='a'; local t11001='b'; local t11002='c'; local t11003='d'; local t11004='e'; local t11005='f'; local t11006='g'; local t11007='h'; local t11008='i'; local t11009='j';
local t12000='a'; local t12001='b'; local t12002='c'; local t12003='d'; local t12004='e'; local t12005='f'; local t12006='g'; local t12007='h'; local t12008='i'; local t12009='j';
local t13000='a'; local t13001='b'; local t13002='c'; local t13003='d'; local t13004='e'; local t13005='f'; local t13006='g'; local t13007='h'; local t13008='i'; local t13009='j';
-- print the variables
print(t0000,'a'); print(t0001,'b'); print(t0002,'c'); print(t0003,'d'); print(t0004,'e'); print(t0005,'f'); print(t0006,'g'); print(t0007,'h'); print(t0008,'i'); print(t0009,'j');
print(t1000,'a'); print(t1001,'b'); print(t1002,'c'); print(t1003,'d'); print(t1004,'e'); print(t1005,'f'); print(t1006,'g'); print(t1007,'h'); print(t1008,'i'); print(t1009,'j');
print(t2000,'a'); print(t2001,'b'); print(t2002,'c'); print(t2003,'d'); print(t2004,'e'); print(t2005,'f'); print(t2006,'g'); print(t2007,'h'); print(t2008,'i'); print(t2009,'j');
print(t3000,'a'); print(t3001,'b'); print(t3002,'c'); print(t3003,'d'); print(t3004,'e'); print(t3005,'f'); print(t3006,'g'); print(t3007,'h'); print(t3008,'i'); print(t3009,'j');
print(t4000,'a'); print(t4001,'b'); print(t4002,'c'); print(t4003,'d'); print(t4004,'e'); print(t4005,'f'); print(t4006,'g'); print(t4007,'h'); print(t4008,'i'); print(t4009,'j');
print(t5000,'a'); print(t5001,'b'); print(t5002,'c'); print(t5003,'d'); print(t5004,'e'); print(t5005,'f'); print(t5006,'g'); print(t5007,'h'); print(t5008,'i'); print(t5009,'j');
print(t6000,'a'); print(t6001,'b'); print(t6002,'c'); print(t6003,'d'); print(t6004,'e'); print(t6005,'f'); print(t6006,'g'); print(t6007,'h'); print(t6008,'i'); print(t6009,'j');
print(t7000,'a'); print(t7001,'b'); print(t7002,'c'); print(t7003,'d'); print(t7004,'e'); print(t7005,'f'); print(t7006,'g'); print(t7007,'h'); print(t7008,'i'); print(t7009,'j');
print(t8000,'a'); print(t8001,'b'); print(t8002,'c'); print(t8003,'d'); print(t8004,'e'); print(t8005,'f'); print(t8006,'g'); print(t8007,'h'); print(t8008,'i'); print(t8009,'j');
print(t9000,'a'); print(t9001,'b'); print(t9002,'c'); print(t9003,'d'); print(t9004,'e'); print(t9005,'f'); print(t9006,'g'); print(t9007,'h'); print(t9008,'i'); print(t9009,'j');
print(t10000,'a'); print(t10001,'b'); print(t10002,'c'); print(t10003,'d'); print(t10004,'e'); print(t10005,'f'); print(t10006,'g'); print(t10007,'h'); print(t10008,'i'); print(t10009,'j');
print(t11000,'a'); print(t11001,'b'); print(t11002,'c'); print(t11003,'d'); print(t11004,'e'); print(t11005,'f'); print(t11006,'g'); print(t11007,'h'); print(t11008,'i'); print(t11009,'j');
print(t12000,'a'); print(t12001,'b'); print(t12002,'c'); print(t12003,'d'); print(t12004,'e'); print(t12005,'f'); print(t12006,'g'); print(t12007,'h'); print(t12008,'i'); print(t12009,'j');
print(t13000,'a'); print(t13001,'b'); print(t13002,'c'); print(t13003,'d'); print(t13004,'e'); print(t13005,'f'); print(t13006,'g'); print(t13007,'h'); print(t13008,'i'); print(t13009,'j');

View File

@@ -1,7 +0,0 @@
local mathClass = luajava.bindClass("java.lang.Math")
local function sqrt(x)
return mathClass:sqrt(x)
end
return { sqrt = sqrt; pi = mathClass.PI }

View File

@@ -1,87 +0,0 @@
print( math.sin( 0.0 ) )
print( math.cos( math.pi ) )
print( math.sqrt( 9.0 ) )
print( math.modf( 5.25 ) )
local aliases = {
['0']='<zero>',
['-0']='<zero>',
['nan']='<nan>',
['inf']='<pos-inf>',
['-inf']='<neg-inf>',
['1.#INF']='<pos-inf>',
['-1.#INF']='<neg-inf>',
['1.#IND']='<nan>',
['-1.#IND']='<nan>',
}
local function normalized(x)
local s = tostring(x)
return aliases[s] or s
end
-- binary ops
function binops( a, b )
local sa = tostring(a)
local sb = tostring(b)
print( sa..'+'..sb..'='..normalized(a+b) )
print( sa..'-'..sb..'='..normalized(a-b) )
print( sa..'*'..sb..'='..normalized(a*b) )
print( sa..'^'..sb..'='..normalized(a^b) )
print( sa..'/'..sb..'='..normalized(a/b) )
print( sa..'%'..sb..'='..normalized(a%b) )
return '--'
end
print( pcall( binops, 2, 0 ) )
print( pcall( binops, 2.5, 0 ) )
print( pcall( binops, -2.5, 0 ) )
print( pcall( binops, 2, 1 ) )
print( pcall( binops, 5, 2 ) )
print( pcall( binops, -5, 2 ) )
print( pcall( binops, 16, -2 ) )
print( pcall( binops, -16, -2 ) )
print( pcall( binops, 256, 0.5 ) )
print( pcall( binops, 256, 0.25 ) )
print( pcall( binops, 256, 0.625 ) )
print( pcall( binops, 256, -0.5 ) )
print( pcall( binops, 256, -0.25 ) )
print( pcall( binops, 256, -0.625 ) )
print( pcall( binops, -256, 0.5 ) )
print( pcall( binops, 0.5, 0 ) )
print( pcall( binops, 0.5, 1 ) )
print( pcall( binops, 0.5, 2 ) )
print( pcall( binops, 0.5, -1 ) )
print( pcall( binops, 0.5, -2 ) )
print( pcall( binops, 2.25, 0 ) )
print( pcall( binops, 2.25, 2 ) )
print( pcall( binops, 2.25, 0.5 ) )
print( pcall( binops, 2.25, 2.5 ) )
print( pcall( binops, -2, 0 ) )
-- random tests
print("Random tests")
local function testrandom(string,lo,hi)
local c,e = loadstring('return '..string)
for i=1,5 do
local s,e = pcall(c)
print( string, s and type(e) or e, (e>=lo) and (e<=hi) )
end
end
testrandom('math.random()',0,1)
testrandom('math.random(5,10)',5,10)
testrandom('math.random(30)',0,30)
testrandom('math.random(-4,-2)',-4,-2)
local t = {}
print( math.randomseed(20) )
for i=1,20 do
t[i] = math.random()
end
print( '-- comparing new numbers')
for i=1,20 do
print( t[i] == math.random(), t[i] == t[0] )
end
print( '-- resetting seed')
print( math.randomseed(20) )
for i=1,20 do
print( t[i] == math.random() )
end

View File

@@ -1,48 +0,0 @@
package.path = "?.lua;src/test/res/?.lua"
require 'ids'
-- The purpose of this test case is to demonstrate that
-- basic metatable operations on non-table types work.
-- i.e. that s.sub(s,...) could be used in place of string.sub(s,...)
local s = "hello"
print(s:sub(2,4))
local t = {}
function op(name,...)
local a,b = pcall( setmetatable, t, ... )
print( name, id(t), id(getmetatable(t)), id(a), a and id(b) or type(b) )
end
op('set{} ',{})
op('set-nil',nil)
op('set{} ',{})
op('set')
op('set{} ',{})
op('set{} ',{})
op('set{}{}',{},{})
op('set-nil',nil)
op('set{__}',{__metatable={}})
op('set{} ',{})
op('set-nil',nil)
t = {}
op('set{} ',{})
op('set-nil',nil)
op('set{__}',{__metatable='abc'})
op('set{} ',{})
op('set-nil',nil)
local i = 1234
local t = setmetatable( {}, {
__mode="v",
__index=function(t,k)
local v = i
i = i + 1
rawset(t,k,v)
return v
end,
} )
local l = { 'a', 'b', 'a', 'b', 'c', 'a', 'b', 'c', 'd' }
for i,key in ipairs(l) do
print( 't.'..key, t[key] )
end

View File

@@ -1,90 +0,0 @@
-- unit tests for module() function
local ids = {}
local function id(obj)
if not obj or type(obj) == 'number' or type(obj) == 'string' then
return obj
end
local v = ids[obj]
if v then
return v
end
table.insert(ids,obj)
ids[obj] = type(obj)..'.'..tostring(#ids)
return ids[obj]
end
-- module tests
local pr = print
local pkg = package
local g = _G
local md = module
local rq = require
local sa = package.seeall
local gfe = getfenv
local gmt = getmetatable
local function envs()
return id(gfe(0)), id(gfe(1)), id(gfe(2)),
id(gmt(gfe(0))), id(gmt(gfe(1))), id(gmt(gfe(2)))
end
local function trymodule(name)
pr( '_G['..name..']', id(g[name]) )
pr( 'pkg.loaded['..name..']', id(pkg.loaded[name]) )
pr( 'envs', envs() )
md(name)
pr( 'envs', envs() )
pr( 'status,result', status, result )
pr( '_G['..name..']', id(g[name]) )
local t = pkg.loaded[name]
pr( 't=pkg.loaded['..name..']', id(t) )
pr( 't._M, t._NAME, t._PACKAGE', id(t._M), id(t._NAME), id(t._PACKAGE) )
pr( 'rq('..name..')', id( rq(name) ) )
pr( 'print', id(print) )
pr( 'seeall(t)', sa(t) )
pr( 'print, _G['..name..']', id(print), id(g[name]) )
end
trymodule('abc.def.ghi')
trymodule('abc.def.ghi')
trymodule('abc.def')
trymodule('abc.def.lmn')
trymodule('abc.def')
trymodule('abc')
package.loaded['opq.rst'] = {}
trymodule('opq.rst')
trymodule('opq.rst')
uvw = { xyz="x1y1z1" }
--print( "uvw", id(uvw) )
--print( "uvw.xyz", id(uvw.xyz) )
--print( "uvw.abc", id(uvw.abc) )
print( pcall( trymodule, 'uvw.xyz' ) )
print( pcall( trymodule, 'uvw' ) )
print( pcall( trymodule, 'uvw.abc' ) )
print( "uvw", id(uvw) )
print( "uvw.xyz", id(uvw.xyz) )
print( "uvw.abc", id(uvw.abc) )
function f()
module( 'fff', package.seeall )
a = 'aaa'
print( a )
end
f()
f()
print( a )
print( getfenv(f)['a'] )
print( a )
-- do metatables work with package.loaded and require?
print( 'setting metatable for package.loaded' )
print( 'package.loaded.mypreload', package.loaded.mypreload )
print( 'setmetatable')
pcall( setmetatable, package.loaded, { __index={mypreload=12345}})
print( 'package.loaded.mypreload', package.loaded.mypreload )
print( "require, 'mypreload'", pcall( require, 'mypreload' ) )
print( 'package.loaded.mypreload', package.loaded.mypreload )
print( 'resetting metatable for package.loaded' )
print( 'setmetatable')
pcall( setmetatable, package.loaded, nil )
print( "require, 'mypreload'", (pcall( require, 'mypreload' )) )
print( 'package.loaded.mypreload', package.loaded.mypreload )

View File

@@ -1,37 +0,0 @@
-- call with arg 'true' to turn on error messages
local messages = select(1,...)
function mpcall(...)
if messages then return pcall(...)
else return (pcall(...)) end
end
-- unit tests for the next() function
function checkvalues(tag, tbl)
local values = {}
local index,value = next(tbl,nil)
while index do
table.insert( values, tostring(index).."="..tostring(value) )
index,value = next(tbl,index)
end
table.sort( values )
print( tag, "values: {"..table.concat(values,",").."}" )
end
function donexts(tag,tbl,count)
checkvalues(tag,tbl)
print( tag, '--- -1', 'pcall( next, tbl,-1 )', mpcall( next, tbl,-1 ) )
print( tag, '--- 0', 'pcall( next, tbl,0 )', mpcall( next, tbl,0 ) )
print( tag, '---"a"', 'pcall( next, tbl,"a" )', mpcall( next, tbl,"a" ) )
print( tag, '--- 10', 'pcall( next, tbl, 10 )', mpcall( next, tbl, 10 ) )
end
donexts( 'next1', {}, 2 )
donexts( 'next2', {'one', 'two', 'three' }, 5 )
donexts( 'next3', { aa='aaa', bb='bbb', cc='ccc', [20]='20', [30]='30'}, 7 )
donexts( 'next4', {'one', 'two', 'three', aa='aaa', bb='bbb', cc='ccc', [20]='20', [30]='30'}, 9 )
donexts( 'next5', {'one', 'two', 'three', [-1]='minus-one', [0]='zero' }, 7 )
print( 'pcall(next)', mpcall(next) )
print( 'pcall(next,nil)', mpcall(next,nil) )
print( 'pcall(next,"a")', mpcall(next,"a") )
print( 'pcall(next,1)', mpcall(next,1) )

View File

@@ -1,36 +0,0 @@
-- simple os-library tests
-- these can't really be compared to C meaningfully,
-- because they are so highly os-dependent.
local lib = "org.luaj.lib.j2se.J2seOsLib"
-- local lib = "org.luaj.lib.OsLib"
print( 'require "'..lib..'"', pcall( require, lib ) )
print( 'os', os ~= nil )
print( 'os.clock()', pcall( os.clock ) )
print( 'os.date()', pcall( os.date ) )
print( 'os.difftime(123000, 21250)', pcall( os.difftime, 123000, 21250 ) )
print( 'os.execute("hostname")', pcall( os.execute, 'hostname' ) )
print( 'os.execute("")', pcall( os.execute, '' ) )
print( 'os.getenv()', pcall( os.getenv ) )
print( 'os.getenv("bogus.key")', pcall( os.getenv, 'bogus.key' ) )
print( 'os.getenv("java.runtime.version")', pcall( os.getenv, 'java.runtime.version' ) )
local s,p = pcall( os.tmpname )
local s,q = pcall( os.tmpname )
print( 'os.tmpname()', s, p )
print( 'os.tmpname()', s, q )
print( 'os.remove(p)', pcall( os.remove, p ) )
print( 'os.rename(p,q)', pcall( os.rename, p, q ) )
local s,f = pcall( io.open, p,"w" )
print( 'io.open', s, f )
print( 'write', pcall( f.write, f, "abcdef 12345" ) )
print( 'close', pcall( f.close, f ) )
print( 'os.rename(p,q)', pcall( os.rename, p, q ) )
print( 'os.remove(q)', pcall( os.remove, q ) )
print( 'os.remove(q)', pcall( os.remove, q ) )
print( 'os.setlocale()', pcall( os.setlocale ) )
print( 'os.setlocale("jp")', pcall( os.setlocale, "jp" ) )
print( 'os.setlocale("us","monetary")', pcall( os.setlocale, "us", "monetary" ) )
print( 'os.setlocale(nil,"all")', pcall( os.setlocale, nil, "all" ) )
print( 'os.setlocale("c")', pcall( os.setlocale, "c" ) )
print( 'os.exit(123)' )
-- print( pcall( os.exit, -123 ) )
print( 'failed to exit' )

View File

@@ -1,75 +0,0 @@
-- sample lua function that returns values in reverse order
local function lc(a,b,c)
return c,b,a
end
-- sample lua function that throws a lua error
local function le(a,b,c)
error( 'sample error', 0 )
end
-- function that does a plain call to the underlying function
local function cp(f,a,b,c)
global = f(a,b,c)
return global
end
-- function that does a tail call to the underlying function
local function ct(f,a,b,c)
return f(a,b,c)
end
-- wrap pcall to be more useful in testing
local pc = pcall
local pcall = function(...)
local s,c = pc(...)
return s, type(c)
end
-- lua calls
print( 'lc(22,33,44)', lc(22,33,44) )
print( 'pcall(lc,22,33,44)', pcall(lc,22,33,44) )
print( 'pcall(le,22,33,44)', pcall(le,22,33,44) )
print( 'cp(lc,22,33,44)', cp(lc,22,33,44) )
print( 'pcall(cp,lc,22,33,44)', pcall(cp,lc,22,33,44) )
print( 'pcall(cp,le,22,33,44)', pcall(cp,le,22,33,44) )
print( 'ct(lc,22,33,44)', ct(lc,22,33,44) )
print( 'pcall(ct,lc,22,33,44)', pcall(ct,lc,22,33,44) )
print( 'pcall(ct,le,22,33,44)', pcall(ct,le,22,33,44) )
print( "assert(true,'a','b','c')", assert( true, 'a', 'b', 'c' ) )
print( "pcall(assert,true,'a','b','c')", pcall(assert, true, 'a', 'b', 'c' ) )
print( "pcall(assert,false,'a','b','c')", pcall(assert, false, 'a', 'b', 'c' ) )
-- more error, pcall tests
print( 'pcall(error)', pcall(error) )
print( 'pcall(error,"msg")', pcall(error,"msg") )
print( 'pcall(error,"msg",1)', pcall(error,"msg",1) )
print( 'pcall(error,"msg",2)', pcall(error,"msg",2) )
local function le(level)
error("msg",level)
end
function ge(level)
error("msg",level)
end
for i = 0,4 do
print( 'pcall(le,i)', i, pcall(le,i) )
print( 'pcall(ge,i)', i, pcall(ge,i) )
end
-- xpcall tests
local function goodfunction() return 'from good function' end
local function badfunction() error( 'from bad function') end
local function goodhandler(msg) return '-->'..msg..'<--' end
local function badhandler(msg) error( 'from bad handler' ) end
print( 'xpcall(error,goodhandler)', ( xpcall(error,goodhandler) ) )
print( 'xpcall(goodfunction,goodhandler)', ( xpcall(goodfunction,goodhandler) ) )
print( 'xpcall(badfunction,goodhandler)', ( xpcall(badfunction,goodhandler) ) )
print( 'xpcall(goodfunction,badhandler)', ( xpcall(goodfunction,badhandler) ) )
print( 'xpcall(badfunction,badhandler)', ( xpcall(badfunction,badhandler) ) )
print( 'xpcall(goodfunction,nil)', ( xpcall(goodfunction,nil) ) )
print( 'xpcall(badfunction,nil)', ( xpcall(badfunction,nil) ) )
print( 'xpcall(goodfunction)', ( pcall( function() return xpcall(goodfunction) end ) ) )
print( 'xpcall(badfunction)', ( pcall( function() return xpcall(badfunction) end ) ) )

View File

@@ -1,26 +0,0 @@
-- print uses tostring under-the-hood!
local function f()
print()
print('abc')
print(123)
print(true)
print('abc',123,true)
end
local function g()
local fenv = {tostring=function(x)
return '*'..type(x)..'*'
end}
package.seeall(fenv)
f()
print('setfenv', pcall(setfenv, 0, fenv), {}, f )
f()
end
local s,c = pcall( coroutine.create, g )
print('create', s, s and type(c) or c)
print('resume', pcall( coroutine.resume, c ) )
f()

View File

@@ -1,16 +0,0 @@
-- helper file for require() tests
require 'ids'
print( 'in subsample (before module statement)' )
print( 'env', id(getfenv()), '_G', id(_G), '_NAME', _NAME, '_M', id(_M), '_PACKAGE', _PACKAGE )
module( 'req.subsample', package.seeall )
print( 'in subsample (after module statement)' )
print( 'env', id(getfenv()), '_G', id(_G), '_NAME', _NAME, '_M', id(_M), '_PACKAGE', _PACKAGE )
function h()
print 'in subsample.h'
print( 'env', id(getfenv()), '_G', id(_G), '_NAME', _NAME, '_M', id(_M), '_PACKAGE', _PACKAGE )
end
print 'loading subsample.lua'
h()
return 'return value from subsample', 'second return value from subsample'

View File

@@ -1,101 +0,0 @@
-- unit tests for require() function
package.path = "?.lua;src/test/res/?.lua"
require 'ids'
-- tests on require
package.path='?.lua;src/test/res/?.lua'
function f( name )
print( module( 'testmod', package.seeall ) )
print( 'before', id(sample), id(bogus), id(_G[name]) );
local status,result = pcall( require, name )
if status then
print( 'pcall(require,"'..name..'")', status, id(result) )
print( 'after', id(sample), id(bogus), id(_G[name]) );
else
print( 'pcall(require,"'..name..'")', status )
end
end
f('sample')
print( 'main', id(sample), id(bogus), id(custom), id(req) );
f('sample')
print( 'main', id(sample), id(bogus), id(custom), id(req) );
f('bogus')
print( 'main', id(sample), id(bogus), id(custom), id(req) );
f( 'req.subsample' )
print( 'main', id(sample), id(bogus), id(custom), id(req) );
-- custom loader chain
local pl = package.loaders
for i=1,3 do
print( i,id(package.loaders[i]) )
end
function loader1( ... )
print ('in loader1', ...)
return "loader1 didn't find anything"
end
function loader2( name, ... )
print ('in loader2', ...)
if name ~= 'custom' then
message = "name is not 'custom'"
print ('loader2 is returning', message )
return message
end
table = {}
result = function()
print( 'in loader function, returning', id(table) )
return table
end
print ('loader2 is returning', id(result) )
return result
end
function loader3( ... )
print ('in loader3', ...)
return nil
end
package.loaders = { loader1, loader2, loader3 }
f( 'bogus' )
print( 'main', id(sample), id(bogus), id(custom), id(src) );
f( 'custom' )
print( 'main', id(sample), id(bogus), id(custom), id(src) );
-- good, and bad lua samples
function g(name)
print( name, pcall(f,name) )
end
package.loaders = { function(...)
print( 'in success loader', ... )
return function(...)
print( 'in success chunk', ... )
end
end }
pcall( g, 'require-sample-succeed' )
package.loaders = { function(...)
print( 'in loader-error loader', ... )
error( 'sample error thrown by loader-error loader')
return function(...)
print( 'in loader-error chunk', ... )
end
end }
pcall( g, 'require-sample-loader-error' )
package.loaders = { function(...)
print( 'in chunk-error loader', ... )
return function(...)
error( 'sample error thrown by chunk-error function')
print( 'in chunk-error chunk', ... )
end
end }
pcall( g, 'require-sample-chunk-error' )
-- good, and bad java samples
--[[ (needs to be moved to different test suite)
package.loaders = pl
function g(name)
print( name, pcall(f,name) )
print( 'main', id(org) );
end
pcall( g, 'org.luaj.vm.require.RequireSampleClassCastExcep')
pcall( g, 'org.luaj.vm.require.RequireSampleLoadLuaError')
pcall( g, 'org.luaj.vm.require.RequireSampleLoadRuntimeExcep')
pcall( g, 'org.luaj.vm.require.RequireSampleSuccess')
pcall( g, 'org.luaj.vm.require.RequireSampleSuccess')
--]]

View File

@@ -1,7 +0,0 @@
-- helper file for require() tests
module( 'sample', package.seeall )
function h()
print 'in sample.h'
end
print 'loading sample.lua'

View File

@@ -1,31 +0,0 @@
-- Parts of this test are commented out because it looks like
-- there is a problem with our argument passing, particularly in the
-- presence of the VARARG instruction.
--[[ local function f(...)
print("arg count:", select('#', ...))
end
local function g(...)
local a, b, c = select(2, ...)
print(a, b, c)
end
]]--
print((select(1, "a", "b", "c")))
print( select(1, "a", "b", "c"))
print((select(2, "a", "b", "c")))
print( select(2, "a", "b", "c"))
print((select(3, "a", "b", "c")))
print( select(3, "a", "b", "c"))
print((select(4, "a", "b", "c")))
print( select(4, "a", "b", "c"))
print( select("#") )
print( select("#", "a") )
print( select("#", "a", "b") )
-- f("hello", "world")
-- g(1, 2, 3, 4, 5, 6, 7)

View File

@@ -1,61 +0,0 @@
-- object ids
package.path = "?.lua;src/test/res/?.lua"
require 'ids'
local id = id
local setfenv = setfenv
local print = print
local pcall = pcall
local create = coroutine.create
local resume = coroutine.resume
local seeall = package.seeall
-- unit tests for getfenv, setfenv
local function f3(level,value)
if value then
local t = {abc=value}
seeall(t)
setfenv( level, t )
end
return abc
end
local function f2(...)
local r = f3(...)
return abc,r
end
local function f1(...)
local r,s = f2(...)
print( ' ....... f1 returning ',abc,r,s )
return abc,r,s
end
local function survey(msg)
print('-------',msg)
print( ' _G,f1,f2,f3', id(_G),id(f1),id(f2),id(f3) )
print( ' envs._G,f1,f2,f3', id(getfenv()),id(getfenv(f1)),id(getfenv(f2)),id(getfenv(f3)) )
print( ' vals._G,f1,f2,f3', abc,getfenv(f1).abc, getfenv(f2).abc, getfenv(f3).abc )
print( ' pcall(f1)', pcall(f1) )
end
survey( 'before')
setfenv(f1,{abc='ghi'})
setfenv(f2,{abc='jkl'})
setfenv(f3,{abc='mno'})
survey( 'after')
print( 'abc,f1(1,"pqr")', abc,f1(1,"pqr") )
print( 'abc,f1(2,"stu")', abc,f1(2,"stu") )
print( 'abc,f1(3,"vwx")', abc,f1(3,"vwx") )
survey( 'via f1()' )
local c = create( function()
survey( 'coroutine-thread-before')
print( 'f1(3,"abc")', f1(3,"567") )
survey( 'coroutine-thread-1')
print( 'f1(2,"abc")', f1(2,"456") )
survey( 'coroutine-thread-2')
print( 'f1(1,"abc")', f1(1,"345") )
survey( 'coroutine-thread-3')
print( 'f1(0,"abc")', f1(0,"234") )
survey( 'coroutine-thread-after')
end )
print( 'resume', resume(c) )
survey( 'post-resume')

View File

@@ -1,27 +0,0 @@
-- This file attemps to test that the setlist instruction works
local list = { 1, 2, 3 }
-- for now, can't just do:
-- for x, y in pairs( list ) do
-- since our tables don't iterate over keys in the same order
-- as regular Lua.
print( #list )
for i = 1, 3 do
print("list[", i, "]=", list[i])
end
local function printList( l )
for i = 1, #l do
print(i, "->", l[i] )
end
end
printList( { "a", "b", "c" } )
local function foo()
return "d", "e", "f", "g"
end
printList( { foo() } )

View File

@@ -1,14 +0,0 @@
-- The purpose of this test case is to check
-- that certain simple metatable operations work properly
t = { b='bbb' }
t.__index = t
u = {}
setmetatable( u, t )
u.c = 'ccc'
print( 't.a', t.a )
print( 'u.a', u.a )
print( 't.b', t.b )
print( 'u.b', u.b )
print( 't.c', t.c )
print( 'u.c', u.c )

View File

@@ -1,27 +0,0 @@
-- concat
print( '-- sort tests' )
local function tryall(cmp)
local function try(t)
print( table.concat(t,'-') )
if pcall( table.sort, t, cmp ) then
print( table.concat(t,'-') )
else
print( 'sort failed' )
end
end
try{ 2, 4, 6, 8, 1, 3, 5, 7 }
try{ 333, 222, 111 }
try{ "www", "xxx", "yyy", "aaa", "bbb", "ccc" }
try{ 21, 23, "25", 27, 22, "24", 26, 28 }
end
local function comparator(a,b)
return tonumber(a)<tonumber(b)
end
tryall()
tryall(comparator)

View File

@@ -1,46 +0,0 @@
-- This test case computes and outputs the sums of the first n
-- integers for n from 1 to 100 in the most ridiculous way possible.
-- The point is to exercise code related to varargs & argument passing
-- in the hopes of detecting bug(s) in stack resizing code.
local function upto(n)
local t = {}
for i = 1, n do
t[i] = i
end
return unpack(t)
end
local function map(f, ...)
local args = { ... }
local result = {}
local i = 1
while args[i] do
result[i] = f(args[i])
i = i + 1
end
return unpack(result)
end
local function join(sep, ...)
return table.concat({ ... }, sep)
end
local i = 1
local j = 1
while i < 200 do
local params = join(", ", map(function(x) return("x"..x) end, upto(i)))
local body = join(" + ", map(function(x) return("x"..x) end, upto(i)))
local func = "function f(" .. params .. ")\n return(" .. body .. ")\nend"
local args = join(", ", map(function(x) return tostring(x) end, upto(i)))
local chunk = func .. "\nreturn f( " .. args .. " )"
local co = coroutine.create(assert(loadstring(chunk, "test #"..i)))
print(coroutine.resume(co))
i = i + j
j = j + 1
end

Binary file not shown.

View File

@@ -1,169 +0,0 @@
print( string.find("1234567890", ".", 0, true) )
print( string.find( 'alo alx 123 b\0o b\0o', '(..*) %1' ) )
print( string.find( 'aloALO', '%l*' ) )
print( string.find( ' \n isto <20> assim', '%S%S*' ) )
print( string.find( "", "" ) )
print( string.find( "ababaabbaba", "abb" ) )
print( string.find( "ababaabbaba", "abb", 7 ) )
print( string.match( "aabaa", "a*" ) )
print( string.match( "aabaa", "a*", 3 ) )
print( string.match( "aabaa", "a*b" ) )
print( string.match( "aabaa", "a*b", 3 ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)b" ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b" ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 3 ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 8 ) )
print( string.match( "abbaaababaabaaabaa", "b(a*)()b", 12 ) )
print( string.byte("hi", -3) )
print( string.gsub("ABC", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("@123", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123@def", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123@qrs@def@tuv", "@(%x+)", function(s) return "|abcd" end) )
print( string.gsub("ABC@123@qrs@def@tuv", "@(%x+)", function(s) return "@ab" end) )
print( tostring(1234567890123) )
print( tostring(1234567890124) )
print( tostring(1234567890125) )
function f1(s, p)
print(p)
p = string.gsub(p, "%%([0-9])", function (s) return "%" .. (s+1) end)
print(p)
p = string.gsub(p, "^(^?)", "%1()", 1)
print(p)
p = string.gsub(p, "($?)$", "()%1", 1)
print(p)
local t = {string.match(s, p)}
return string.sub(s, t[1], t[#t] - 1)
end
print( f1('alo alx 123 b\0o b\0o', '(..*) %1') )
local function badpat()
print( string.gsub( "alo", "(.)", "%2" ) )
end
print( pcall( badpat ) )
for k, v in string.gmatch("w=200&h=150", "(%w+)=(%w+)") do
print(k, v)
end
-- string.sub
function t(str)
local i = { 0, 1, 2, 8, -1 }
for ki,vi in ipairs(i) do
local s,v = pcall( string.sub, str, vi )
print( 'string.sub("'..str..'",'..tostring(vi)..')='..tostring(s)..',"'..tostring(v)..'"' )
local j = { 0, 1, 2, 4, 8, -1 }
for kj,vj in ipairs(j) do
local s,v = pcall( string.sub, str, vi, vj )
print( 'string.sub("'..str..'",'..tostring(vi)..','..tostring(vj)..')='..tostring(s)..',"'..tostring(v)..'"' )
end
end
end
t( 'abcdefghijklmn' )
t( 'abcdefg' )
t( 'abcd' )
t( 'abc' )
t( 'ab' )
t( 'a' )
t( '' )
print(string.len("Hello, world"))
print(#"Hello, world")
print(string.len("\0\0\0"))
print(#"\0\0\0")
print(string.len("\0\1\2\3"))
print(#"\0\1\2\3")
local s = "My JaCk-O-lAnTeRn CaSe TeXt"
print(s, string.len(s), #s)
-- string.format
print(string.format("(%.0d) (%.0d) (%.0d)", 0, -5, 9))
print(string.format("(%.1d) (%.1d) (%.1d)", 0, -5, 9))
print(string.format("(%.2d) (%.2d) (%.2d)", 0, -5, 9))
print(string.format("(%+.0d) (%+.0d) (%+.0d)", 0, -5, 9))
print(string.format("(%+.1d) (%+.1d) (%+.1d)", 0, -5, 9))
print(string.format("(%+.2d) (%+.2d) (%+.2d)", 0, -5, 9))
print(string.format("(%+3d) (% 3d) (%+ 3d)", 55, 55, 55))
print(string.format("(%-1d) (%-1d) (%-1d)", 1, 12, -12))
print(string.format("(%-2d) (%-2d) (%-2d)", 1, 12, -12))
print(string.format("(%-3d) (%-3d) (%-3d)", 1, 12, -12))
print(string.format("(%8x) (%8d) (%8o)", 255, 255, 255))
print(string.format("(%08x) (%08d) (%08o)", 255, 255, 255))
print(string.format("simple%ssimple", " simple "))
specials = "\"specials\": %% \000 \r \n"
print(string.format("%s\n%q\n", specials, specials))
print(string.format("%%"))
print(string.format("this is a %s long string", string.rep("really, ", 30)))
local function pc(...)
local s,e = pcall(...)
return s and e or 'false-'..type(e)
end
local function strtests(name,func,...)
print(name, 'good', pc( func, ... ) )
print(name, 'empty', pc( func ) )
print(name, 'table', pc( func, {} ) )
print(name, 'nil', pc( func, nil ) )
end
strtests('lower', string.lower, s )
strtests('upper', string.upper, s )
strtests('reverse', string.reverse, s )
strtests('char', string.char, 92, 60, 61, 93 )
strtests('dump', string.dump, function() print('hello, world') end )
-- floating point formats (not supported yet)
--[[
local prefixes = {'','+','-'}
local lengths = {'7','2','0','1',''}
local letters = {'f','e','g'}
local fmt, spec, desc
for i,letter in ipairs(letters) do
for k,before in ipairs(lengths) do
for j,prefix in ipairs(prefixes) do
spec = '(%'..prefix..before..letter..')'
fmt = spec..'\t'..spec..'\t'..spec..'\t'..spec..'\t'..spec..'\t'..spec
print(spec, string.format(fmt, 12.34, -12.34, 1/11, -1/11, 300/11, -300/11) )
for l,after in ipairs(lengths) do
spec = '(%'..prefix..before..'.'..after..letter..')'
fmt = spec..' '..spec..' '..spec..' '..spec..' '..spec..' '..spec
print(spec, string.format(fmt, 12.34, -12.34, 1/11, -1/11, 300/11, -300/11) )
end
end
end
end
--]]
local function fmterr(...)
local r, s = pcall(...)
if r then
return s
else
s = string.gsub(s, "stdin:%d+:%s*", "")
return s
end
end
print(fmterr(string.find, "ab%c)0(", "%"))
print(fmterr(string.find, "ab%c)0(", "("))
print(pcall(string.find, "ab%c)0(", ")"))

View File

@@ -1,29 +0,0 @@
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
pane = luajava.newInstance( "javax.swing.JPanel" );
borderFactory = luajava.bindClass( "javax.swing.BorderFactory" )
border = borderFactory:createEmptyBorder( 30, 30, 10, 30 )
pane:setBorder( border )
label = luajava.newInstance( "javax.swing.JLabel", "This is a Label" );
layout = luajava.newInstance( "java.awt.GridLayout", 2, 2 );
pane:setLayout( layout )
pane:add( label )
pane:setBounds( 20, 30, 10, 30 )
borderLayout = luajava.bindClass( "java.awt.BorderLayout" )
frame:getContentPane():add(pane, borderLayout.CENTER )
jframe = luajava.bindClass( "javax.swing.JFrame" )
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
frame:pack()
frame:setVisible(true)
local listener = luajava.createProxy("java.awt.event.MouseListener",
{
mouseClicked = function(me)
print("clicked!", me)
end
})
frame:addMouseListener(listener)

View File

@@ -1,224 +0,0 @@
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
table.insert(t,'six');
table.insert(t,1,'seven');
table.insert(t,4,'eight');
table.insert(t,7,'nine');
table.insert(t,10,'ten'); print( #t )
-- concat
print( '-- concat tests' )
function tryconcat(t)
print( table.concat(t) )
print( table.concat(t,'--') )
print( table.concat(t,',',2) )
print( table.concat(t,',',2,2) )
print( table.concat(t,',',5,2) )
end
tryconcat( { "one", "two", "three", a='aaa', b='bbb', c='ccc' } )
tryconcat( { "one", "two", "three", "four", "five" } )
function tryconcat(t)
print( table.concat(t) )
print( table.concat(t,'--') )
print( table.concat(t,',',2) )
end
tryconcat( { a='aaa', b='bbb', c='ccc', d='ddd', e='eee' } )
tryconcat( { [501]="one", [502]="two", [503]="three", [504]="four", [505]="five" } )
tryconcat( {} )
-- print the elements of a table in a platform-independent way
function eles(t,f)
f = f or pairs
all = {}
for k,v in f(t) do
table.insert( all, "["..tostring(k).."]="..tostring(v) )
end
table.sort( all )
return "{"..table.concat(all,',').."}"
end
-- insert, maxn
print( '-- insert, len tests' )
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
print( eles(t), #t )
table.insert(t,'six'); print( eles(t), #t )
table.insert(t,1,'seven'); print( eles(t), #t )
table.insert(t,4,'eight'); print( eles(t), #t )
table.insert(t,7,'nine'); print( eles(t), #t )
table.insert(t,10,'ten'); print( eles(t), #t )
print( '#{}', #{} )
print( '#{"a"}', #{"a"} )
print( '#{"a","b"}', #{"a","b"} )
print( '#{"a",nil}', #{"a",nil} )
print( '#{nil,"b"}', #{nil,"b"} )
print( '#{nil,nil}', #{nil,nil} )
print( '#{"a","b","c"}', #{"a","b","c"} )
print( '#{nil,"b","c"}', #{nil,"b","c"} )
print( '#{"a",nil,"c"}', #{"a",nil,"c"} )
print( '#{"a","b",nil}', #{"a","b",nil} )
print( '#{nil,"b",nil}', #{nil,"b",nil} )
print( '#{nil,nil,"c"}', #{nil,nil,"c"} )
print( '#{"a",nil,nil}', #{"a",nil,nil} )
print( '#{nil,nil,nil}', #{nil,nil,nil} )
-- remove
print( '-- remove tests' )
t = { "one", "two", "three", "four", "five", "six", "seven", [10]="ten", a='aaa', b='bbb', c='ccc' }
print( eles(t), #t )
print( 'table.remove(t)', table.remove(t) ); print( eles(t), #t )
print( 'table.remove(t,1)', table.remove(t,1) ); print( eles(t), #t )
print( 'table.remove(t,3)', table.remove(t,3) ); print( eles(t), #t )
print( 'table.remove(t,5)', table.remove(t,5) ); print( eles(t), #t )
print( 'table.remove(t,10)', table.remove(t,10) ); print( eles(t), #t )
print( 'table.remove(t,-1)', table.remove(t,-1) ); print( eles(t), #t )
print( 'table.remove(t,-1)', table.remove(t,-1) ) ; print( eles(t), #t )
-- sort
print( '-- sort tests' )
function sorttest(t,f)
t = (t)
print( table.concat(t,'-') )
if f then
table.sort(t,f)
else
table.sort(t)
end
print( table.concat(t,'-') )
end
sorttest{ "one", "two", "three" }
sorttest{ "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }
sorttest( { "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }, function(a,b) return b<a end)
-- getn
t0 = {}
t1 = { 'one', 'two', 'three' }
t2 = { a='aa', b='bb', c='cc' }
t3 = { 'one', 'two', 'three', a='aa', b='bb', c='cc' }
print( 'getn('..eles(t0)..')', pcall( table.getn, t0 ) )
print( 'getn('..eles(t1)..')', pcall( table.getn, t1 ) )
print( 'getn('..eles(t2)..')', pcall( table.getn, t2 ) )
print( 'getn('..eles(t3)..')', pcall( table.getn, t3 ) )
-- foreach
function test( f, t, result, name )
status, value = pcall( f, t, function(...)
print(' -- ',...)
print(' next',next(t,(...)))
return result
end )
print( name, 's,v', status, value )
end
function testall( f, t, name )
test( f, t, nil, name..'nil' )
test( f, t, false, name..'fls' )
test( f, t, 100, name..'100' )
end
testall( table.foreach, t0, 'table.foreach('..eles(t0)..')' )
testall( table.foreach, t1, 'table.foreach('..eles(t1)..')' )
testall( table.foreach, t2, 'table.foreach('..eles(t2)..')' )
testall( table.foreach, t3, 'table.foreach('..eles(t3)..')' )
testall( table.foreachi, t0, 'table.foreachi('..eles(t0)..')' )
testall( table.foreachi, t1, 'table.foreachi('..eles(t1)..')' )
testall( table.foreachi, t2, 'table.foreachi('..eles(t2)..')' )
testall( table.foreachi, t3, 'table.foreachi('..eles(t3)..')' )
-- pairs, ipairs
function testpairs(f, t, name)
print( name )
for a,b in f(t) do
print( ' ', a, b )
end
end
function testbothpairs(t)
testpairs( pairs, t, 'pairs( '..eles(t)..' )' )
testpairs( ipairs, t, 'ipairs( '..eles(t)..' )' )
end
for i,t in ipairs({t0,t1,t2,t3}) do
testbothpairs(t)
end
t = { 'one', 'two', 'three', 'four', 'five' }
testbothpairs(t)
t[6] = 'six'
testbothpairs(t)
t[4] = nil
testbothpairs(t)
-- tests of setlist table constructors
local function a(...) return ... end
print('-',unpack({a()}))
print('a',unpack({a('a')}))
print('.',unpack({a(nil)}))
print('ab',unpack({a('a', 'b')}))
print('.b',unpack({a(nil, 'a')}))
print('a.',unpack({a('a', nil)}))
print('abc',unpack({a('a', 'b', 'c')}))
print('.ab',unpack({a(nil, 'a', 'b')}))
print('a.b',unpack({a('a', nil, 'b')}))
print('ab.',unpack({a('a', 'b', nil)}))
print('..b',unpack({a(nil, nil, 'b')}))
print('a..',unpack({a('a', nil, nil)}))
print('.b.',unpack({a(nil, 'b', nil)}))
print('...',unpack({a(nil, nil, nil)}))
-- misc tests
print( # { 'abc', 'def', 'ghi', nil } ) -- should be 3 !
print( # { 'abc', 'def', 'ghi', false } ) -- should be 4 !
print( # { 'abc', 'def', 'ghi', 0 } ) -- should be 4 !
print( nil and 'T' or 'F' ) -- should be 'F'
print( false and 'T' or 'F' ) -- should be 'F'
print( 0 and 'T' or 'F' ) -- should be 'T'
-- basic table operation tests
local dummyfunc = function(t,...)
print( 'metatable call args', type(t), ...)
return 'dummy'
end
local makeloud = function(t)
return setmetatable(t,{
__index=function(t,k)
print( '__index', type(t), k )
return rawset(t,k)
end,
__newindex=function(t,k,v)
print( '__newindex', type(t), k, v )
rawset(t,k,v)
end})
end
local tests = {
{'basic table', {}},
{'function metatable on __index', setmetatable({},{__index=dummyfunc})},
{'function metatable on __newindex', setmetatable({},{__newindex=dummyfunc})},
{'plain metatable on __index', setmetatable({},makeloud({}))},
{'plain metatable on __newindex', setmetatable({},makeloud({}))},
}
for i,test in ipairs(tests) do
local testname = test[1]
local testtable = test[2]
print( '------ basic table tests on '..testname..' '..type(testtable) )
print( 't[1]=2', pcall( function() testtable[1]=2 end ) )
print( 't[1]', pcall( function() return testtable[1] end ) )
print( 't[1]=nil', pcall( function() testtable[1]=nil end ) )
print( 't[1]', pcall( function() return testtable[1] end ) )
print( 't["a"]="b"', pcall( function() testtable["a"]="b" end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't.a="c"', pcall( function() testtable.a="c" end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't.a=nil', pcall( function() testtable.a=nil end ) )
print( 't["a"],t.a', pcall( function() return testtable["a"],testtable.a end ) )
print( 't[nil]="d"', pcall( function() testtable[nil]="d" end ) )
print( 't[nil]', pcall( function() return testtable[nil] end ) )
print( 't[nil]=nil', pcall( function() testtable[nil]=nil end ) )
print( 't[nil]', pcall( function() return testtable[nil] end ) )
end
-- tables with doubles
local t = { [1]='a', [2]='b', [3.0]='c', [7]='d', [9]='e', [20]='f', [30.0]='g', [12.5]='h' }
print(#t)
print(t[1], t[2], t[3], t[7], t[9], t[20], t[30])
print(t[1.0], t[2.0], t[3.0], t[7.0], t[9.0], t[20.0], t[30.0], t[12.5])
local i,j,k,l,m,n,o = math.ceil(0.7),math.floor(2.1),math.abs(-3.0),math.sqrt(49.0),math.ceil(8.6),math.floor(20.5),math.max(1.2,30.0)
print(i, j, k, l, m, n, o)
print(t[i], t[j], t[k], t[l], t[m], t[n], t[o])
local half,two = .5,2
print(t[1.5-half], t[1.5+half], t[6/2], t[3.5*2], t[8.5+half], t[20.5-half], t[60*half], t[13-half])
print(#t)

View File

@@ -1,62 +0,0 @@
function a()
return pcall( function() end )
end
function b()
return pcall( function() print 'b' end )
end
function c()
return pcall( function() return 'c' end )
end
print( pcall( a ) )
print( pcall( b ) )
print( pcall( c ) )
local function sum(...)
local s = 0
for i,v in ipairs({...}) do
s = s + v
end
return s
end
local function f1(n,a,b,c)
local a = a or 0
local b = b or 0
local c = c or 0
if n <= 0 then
return a,a+b,a+b+c
end
return f1(n-1,a,a+b,a+b+c)
end
local function f2(n,...)
if n <= 0 then
return sum(...)
end
return f2(n-1,n,...)
end
local function f3(n,...)
if n <= 0 then
return sum(...)
end
return pcall(f3,n-1,n,...)
end
local function all(f)
for n=0,3 do
t = {}
for m=1,5 do
print( pcall( f, n, unpack(t)) )
t[m] = m
end
end
end
all(f1)
all(f2)
all(f3)

View File

@@ -1,86 +0,0 @@
for cycle = 1,10 do
a = 123 + 456
print( a )
i = 777
while i<780 do
print(i)
i = i+1
end
a,b = 0,1
while true do -- infinite loop
print( b )
a,b = b,a+b
if a>10 then break end -- exit the loop if the condition is true
end
for count = 336,330,-2 do print(count) end -- numerical iteration
function sum(a,b,c,d) -- "sum" method
local d = d or 0
return a+b+c+d -- return sum
end
print( sum( 1, 2, 3, 4 ) )
print( sum( 5, 6, 7 ) )
print( sum( 9, 10, 11, 12, 13, 14 ) )
print( sum( sum(1,2,3,4), sum(5,6,7), sum(9,10,11,12,13,14), 15 ) )
function f0() print( "f0:" ) end
function f1(a) print( "f1:", a ) end
function f2(a,b) print( "f2:", a, b ) end
function f3(a,b,c) print( "f3:", a, b, c ) end
function f4(a,b,c,d) print( "f4:", a, b, c, d ) end
f0() f0( "a1/1" ) f0( "a1/2", "a2/2" ) f0( "a1/3", "a2/3", "a3/3" ) f0( "a1/4", "a2/4", "a3/4", "a4/4" )
f1() f1( "a1/1" ) f1( "a1/2", "a2/2" ) f1( "a1/3", "a2/3", "a3/3" ) f1( "a1/4", "a2/4", "a3/4", "a4/4" )
f2() f2( "a1/1" ) f2( "a1/2", "a2/2" ) f2( "a1/3", "a2/3", "a3/3" ) f2( "a1/4", "a2/4", "a3/4", "a4/4" )
f3() f3( "a1/1" ) f3( "a1/2", "a2/2" ) f3( "a1/3", "a2/3", "a3/3" ) f3( "a1/4", "a2/4", "a3/4", "a4/4" )
f4() f4( "a1/1" ) f4( "a1/2", "a2/2" ) f4( "a1/3", "a2/3", "a3/3" ) f4( "a1/4", "a2/4", "a3/4", "a4/4" )
function g0(a,b,c,d) return end
function g1(a,b,c,d) return d end
function g2(a,b,c,d) return c, d end
function g3(a,b,c,d) return b, c, d end
function g4(a,b,c,d) return a, b, c, d end
z = g0("c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4")
print( "z0:", z )
z = g2("c2.1/4", "c2.2/4", "c2.3/4", "c2.4/4")
print( "z2:", z )
z = g4("c4.1/4", "c4.2/4", "c4.3/4", "c4.4/4")
print( "z4:", z )
a,b,c,d = g0( "c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4" )
print( "g0:", a, b, c, d, "(eol)" )
a,b,c,d = g2( "b2.1/4", "b2.2/4", "b2.3/4", "b2.4/4" )
print( "g2:", a, b, c, d, "(eol)" )
a,b,c,d = g4( "b4.1/4", "b4.2/4", "b4.3/4", "b4.4/4" )
print( "g4:", a, b, c, d, "(eol)" )
function func(a,b,c)
return a, b, c
end
print( func(11, 12, 13) )
print( func(23, 22, 21) )
print( func(func(32,33,34), func(45,46,47), func(58,59,50)) )
--[[
function p(a,...)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
p()
p("q")
p("q","r")
p("q","r","s")
--]]
end

View File

@@ -1,80 +0,0 @@
function sum(a,b,c,d) -- "sum" method
local d = d or 0
return a+b+c+d -- return sum
end
print( sum( 1, 2, 3, 4 ) )
print( sum( 5, 6, 7 ) )
print( sum( 9, 10, 11, 12, 13, 14 ) )
print( sum( sum(1,2,3,4), sum(5,6,7), sum(9,10,11,12,13,14), 15 ) )
function myfunc(x)
return x*x;
end
print( myfunc(0.25) )
do
local oldMyfunc = myfunc
local k = 55
myfunc = function (x)
local a = k + oldMyfunc(x)
k = k + 5
return a
end
end
print( myfunc(0.1) )
print( myfunc(0.1) )
i = 1
table = { "west", "south", "east", "north" }
function next()
if ( i >= 4 ) then
i = 0
end
i = i + 1
return table[i]
end
print( next() )
print( next() )
print( next() )
print( next() )
print( next() )
function room1 ()
local move = next()
print( "room1 moving", move )
if move == "south" then return room3()
elseif move == "east" then return room2()
else print("invalid move")
return room1() -- stay in the same room
end
end
function room2 ()
local move = next()
print( "room2 moving", move )
if move == "south" then return room4()
elseif move == "west" then return room1()
else print("invalid move")
return room2()
end
end
function room3 ()
local move = next()
print( "room3 moving", move )
if move == "north" then return room1()
elseif move == "east" then return room4()
else print("invalid move")
return room3()
end
end
function room4 ()
print("congratulations!")
end
room1()

View File

@@ -1,62 +0,0 @@
function f0() print( "f0:" ) end
function f1(a) print( "f1:", a ) end
function f2(a,b) print( "f2:", a, b ) end
function f3(a,b,c) print( "f3:", a, b, c ) end
function f4(a,b,c,d) print( "f4:", a, b, c, d ) end
f0() f0( "a1/1" ) f0( "a1/2", "a2/2" ) f0( "a1/3", "a2/3", "a3/3" ) f0( "a1/4", "a2/4", "a3/4", "a4/4" )
f1() f1( "a1/1" ) f1( "a1/2", "a2/2" ) f1( "a1/3", "a2/3", "a3/3" ) f1( "a1/4", "a2/4", "a3/4", "a4/4" )
f2() f2( "a1/1" ) f2( "a1/2", "a2/2" ) f2( "a1/3", "a2/3", "a3/3" ) f2( "a1/4", "a2/4", "a3/4", "a4/4" )
f3() f3( "a1/1" ) f3( "a1/2", "a2/2" ) f3( "a1/3", "a2/3", "a3/3" ) f3( "a1/4", "a2/4", "a3/4", "a4/4" )
f4() f4( "a1/1" ) f4( "a1/2", "a2/2" ) f4( "a1/3", "a2/3", "a3/3" ) f4( "a1/4", "a2/4", "a3/4", "a4/4" )
function g0(a,b,c,d) return end
function g1(a,b,c,d) return d end
function g2(a,b,c,d) return c, d end
function g3(a,b,c,d) return b, c, d end
function g4(a,b,c,d) return a, b, c, d end
z = g0("c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4")
print( "z0:", z )
z = g2("c2.1/4", "c2.2/4", "c2.3/4", "c2.4/4")
print( "z2:", z )
z = g4("c4.1/4", "c4.2/4", "c4.3/4", "c4.4/4")
print( "z4:", z )
a,b,c,d = g0( "c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4" )
print( "g0:", a, b, c, d, "(eol)" )
a,b,c,d = g2( "b2.1/4", "b2.2/4", "b2.3/4", "b2.4/4" )
print( "g2:", a, b, c, d, "(eol)" )
a,b,c,d = g4( "b4.1/4", "b4.2/4", "b4.3/4", "b4.4/4" )
print( "g4:", a, b, c, d, "(eol)" )
function func(a,b,c)
return a, b, c
end
print( func(11, 12, 13) )
print( func(23, 22, 21) )
print( func(func(32,33,34), func(45,46,47), func(58,59,50)) )
function p(a,...)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
p()
p("q")
p("q","r")
p("q","r","s")
function first(...)
return 'abc'
end
function second( a, b, c )
return first( a, b, c )
end
print( 'second', second() )

View File

@@ -1,8 +0,0 @@
function Point(x, y) -- "Point" object constructor
return { x = x, y = y } -- Creates and returns a new object (table)
end
array = { Point(10, 20), Point(30, 40), Point(50, 60) } -- Creates array of points
print(array[2].y) -- Prints 40

View File

@@ -1,20 +0,0 @@
i = 777
while i<780 do
print(i)
i = i+1
end
a,b = 0,1
while true do -- infinite loop
print( b )
a,b = b,a+b
if a>10 then break end -- exit the loop if the condition is true
end
for count = 336,330,-2 do print(count) end -- numerical iteration
for key,value in pairs({a=10, 3.14159265358, c="banana" })
do
print(key, value)
end

View File

@@ -1,99 +0,0 @@
package.path = "?.lua;src/test/res/?.lua"
require 'ids'
t = { 11, 22, 33, you='one', me='two' }
print( "---------" )
for a,b in pairs(t) do
print( id(a), id(b) )
end
print( "----" )
print( "t[2]", t[2] )
print( "t.me", t.me )
print( "t.fred", t.fred )
print( "t['me']", t["me"] )
print( "t['fred']", t["fred"] )
print( "me", me )
print( "fred", fred )
print( "t[me]", t[me] )
print( "t[fred]", t[fred] )
function out(t,...)
print(type(t),...)
end
-- basic metatable setting
t = { 11, 22, 33, you='one', me='two' }
mt = { __index = out, __newindex = out }
setmetatable(t,mt)
a = t[11]
b = t.one
c = t[1]
d = t.you
t[4] = 'rat'
t[1] = 'pipe'
print( a, b, c, d )
print( "---------" )
for a,b in pairs(t) do
print( id(a), id(b) )
end
print( "----" )
-- delegate to actual metatable
s = { }
mt = { __newindex = s, __index = _G }
setmetatable(t, mt)
print( t.you )
x = 'wow'
print( t.x )
me = 'here'
print( t.me )
t[5] = 99
print( "---------" )
for a,b in pairs(s) do
print( id(a), id(b) )
end
print( "----" )
Vector = {}
Vector_mt = { __index = Vector }
function Vector:new(x,y)
return setmetatable( {x=x, y=y}, Vector_mt)
end
function Vector:mag()
return math.sqrt(self:dot(self))
end
function Vector:dot(v)
return self.x * v.x + self.y * v.y
end
v1 = Vector:new(3,4)
print( "---------" )
for a,b in pairs(v1) do
print( id(a), id(b) )
end
print( "----" )
v2 = Vector:new(2,1)
print( v2:dot(v1) )
print( id(Vector) )
print( "=======" )
for a,b in spairs(Vector) do
print( id(a), id(b) )
end
print( "----" )
print( id(v1), id(v2) )
print( id(Vector_mt), id(getmetatable(v1)), id(getmetatable(v2)) )
print( "---------" )
for a,b in pairs(Vector_mt) do
print( id(a), id(b) )
end
print( "----" )

View File

@@ -1,7 +0,0 @@
java.lang.Object@xxxxxx
org.luaj.sample.SampleClass@xxxxxx
Hello
Hello
true
World
Square root of 9 is 3

View File

@@ -1,21 +0,0 @@
local function fixhash(msg)
return (string.gsub(msg, "@(%x+)", function(s) return "@"..(string.rep("x", 6)) end))
end
obj = luajava.newInstance("java.lang.Object")
print( fixhash( tostring(obj) ) )
sample = luajava.newInstance("org.luaj.sample.SampleClass")
print( fixhash( tostring(sample) ) )
sample.s = "Hello"
print( sample.s )
print( sample:getS() )
sample:setObj(obj)
print( obj == sample:getObj() )
sample:setS( "World" )
print( sample.s )
math = luajava.bindClass("java.lang.Math")
print("Square root of 9 is", math:sqrt(9.0))

View File

@@ -1,12 +0,0 @@
t = {}
t[1] = "foo"
t[2] = "bah"
print(t[1])
print(t[2])
print(t[3])
t[1] = nil
t[2] = nil
print(t[1])
print(t[2])
print(t[3])

View File

@@ -1,15 +0,0 @@
local x
local function g()
local a, b, c
return a, b, c
end
local function f()
print("hello")
end
g()
x = ...
f()

View File

@@ -1,5 +0,0 @@
local f = luajava.bindClass("java.lang.Float")
print(f:toHexString(0.5))
print(f:valueOf(0.5))
print(f:valueOf("0.5"))

View File

@@ -1,8 +0,0 @@
print(type(5))
print(type(3.14))
print(type("hello"))
print(type(function() return 1 end))
print(type({}))
print(type(nil))
print(type(2 < 5))
print(type(pairs({})))

View File

@@ -1,25 +0,0 @@
function test()
local x = 5
function f()
x = x + 1
return x
end
function g()
x = x - 1
return x
end
print(f())
print(g())
return f, g
end
f1, g1 = test()
print("f1()=", f1())
print("g1()=", g1())
f2, g2 = test()
print("f2()=", f2())
print("g2()=", g2())
print("g1()=", g1())
print("f1()=", f1())

View File

@@ -1,42 +0,0 @@
-- The point of this test is that when an upvalue is created, it may
-- need to be inserted in the middle of the list, rather than always
-- appended at the end. Otherwise, it may not be found when it is
-- needed by another closure.
local function test()
local x = 3
local y = 5
local z = 7
local function f()
print("y=", y)
end
local function g()
print("z=", z)
end
local function h()
print("x=", x)
end
local function setter(x1, y1, z1)
x = x1
y = y1
z = z1
end
return f, g, h, setter
end
local f, g, h, setter = test()
h()
f()
g()
setter("x", "y", "z")
h()
f()
g()

View File

@@ -1,14 +0,0 @@
local f
do
local x = 10
function g()
print(x, f())
end
end
function f()
return 20
end
g()

View File

@@ -1,51 +0,0 @@
function p(a,...)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
function q(a,...)
print("a,arg[1],arg[2],arg[3]",a,arg.n,arg[1],arg[2],arg[3])
end
function r(a,...)
print("a,arg",a,arg)
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
function s(a)
local arg = { '1', '2', '3' }
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
print("a",a)
end
function t(a,...)
local arg = { '1', '2', '3' }
print("a,arg[1],arg[2],arg[3]",a,arg[1],arg[2],arg[3])
print("a",a)
print("...",...)
print("...,a",...,a)
print("a,...",a,...)
end
function u(arg)
print( 'arg', arg )
end
arg = { "global-1", "global-2", "global-3" }
function tryall(f,name)
print( '---- function '..name..'()' )
print( '--'..name..'():' )
print( ' ->', pcall( f ) )
print( '--'..name..'("q"):' )
print( ' ->', pcall( f, "q" ) )
print( '--'..name..'("q","r"):' )
print( ' ->', pcall( f, "q", "r" ) )
print( '--'..name..'("q","r","s"):' )
print( ' ->', pcall( f, "q", "r", "s" ) )
end
tryall(p,'p')
tryall(q,'q')
tryall(r,'r')
tryall(s,'s')
tryall(t,'t')
tryall(u,'u')

View File

@@ -1,150 +0,0 @@
-- normalized printing
function eles(t,f)
f = f or pairs
all = {}
for k,v in f(t) do
if type(v) == 'table' then
v = '{'..tostring(v.v)..'}'
end
table.insert( all, "["..tostring(k).."]="..tostring(v) )
end
table.sort( all )
return "{"..table.concat(all,',').."}"
end
function newtable(t)
return setmetatable(t,{__mode="v"})
end
function new(a)
return {v='_'..tostring(a).."_"}
end
-- basic weak-reference table test
local strong = { new('one'), new('two'), a=new('aaa'), c=new('ccc') }
local weak = newtable{ strong[1], new('three'), strong[2], new('four'),
a=new('aaa'), b=new('bbb'), c=new('ccc'), d=new('ddd') }
print( 'before, weak:', eles(weak) )
print( 'before, strong:', eles(strong) )
print( 'gc', pcall( collectgarbage, "collect" ) )
print( 'after, weak:', eles(weak) )
print( 'after, strong:', eles(strong) )
print( 'gc', pcall( collectgarbage, "collect" ) )
print( 'after, weak:', eles(weak) )
print( 'after, strong:', eles(strong) )
print( '-- concat tests' )
function tryconcat(t)
print( table.concat(t) )
print( table.concat(t,'--') )
print( table.concat(t,',',2) )
print( table.concat(t,',',2,2) )
print( table.concat(t,',',5,2) )
end
tryconcat( newtable{ "one", "two", "three", a='aaa', b='bbb', c='ccc' } )
tryconcat( newtable{ "one", "two", "three", "four", "five" } )
function tryconcat(t)
print( table.concat(t) )
print( table.concat(t,'--') )
print( table.concat(t,',',2) )
end
tryconcat( newtable{ a='aaa', b='bbb', c='ccc', d='ddd', e='eee' } )
tryconcat( newtable{ [501]="one", [502]="two", [503]="three", [504]="four", [505]="five" } )
tryconcat( newtable{} )
-- insert, maxn
print( '-- insert, maxn tests' )
local t = newtable{ "one", "two", "three", a='aaa', b='bbb', c='ccc' }
print( eles(t) )
table.insert(t,'six'); print( eles(t) )
table.insert(t,1,'seven'); print( eles(t) )
table.insert(t,4,'eight'); print( eles(t) )
table.insert(t,7,'nine'); print( eles(t) )
table.insert(t,10,'ten'); print( eles(t) )
-- remove
print( '-- remove tests' )
t = newtable{ "one", "two", "three", "four", "five", "six", "seven", [10]="ten", a='aaa', b='bbb', c='ccc' }
print( eles(t) )
print( 'table.remove(t)', table.remove(t) ); print( eles(t) )
print( 'table.remove(t,1)', table.remove(t,1) ); print( eles(t) )
print( 'table.remove(t,3)', table.remove(t,3) ); print( eles(t) )
print( 'table.remove(t,5)', table.remove(t,5) ); print( eles(t) )
print( 'table.remove(t,10)', table.remove(t,10) ); print( eles(t) )
print( 'table.remove(t,-1)', table.remove(t,-1) ); print( eles(t) )
print( 'table.remove(t,-1)', table.remove(t,-1) ) ; print( eles(t) )
-- sort
print( '-- sort tests' )
function sorttest(t,f)
t = (t)
print( table.concat(t,'-') )
if f then
table.sort(t,f)
else
table.sort(t)
end
print( table.concat(t,'-') )
end
--[[
sorttest( newtable{ "one", "two", "three" } )
sorttest( newtable{ "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" } )
sorttest( newtable{ "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }, function(a,b) return b<a end)
--]]
-- getn
t0 = newtable{}
t1 = newtable{ 'one', 'two', 'three' }
t2 = newtable{ a='aa', b='bb', c='cc' }
t3 = newtable{ 'one', 'two', 'three', a='aa', b='bb', c='cc' }
print( 'getn('..eles(t0)..')', pcall( table.getn, t0 ) )
print( 'getn('..eles(t1)..')', pcall( table.getn, t1 ) )
print( 'getn('..eles(t2)..')', pcall( table.getn, t2 ) )
print( 'getn('..eles(t3)..')', pcall( table.getn, t3 ) )
-- foreach
function test( f, t, result, name )
status, value = pcall( f, t, function(...)
print(' -- ',...)
print(' next',next(t,(...)))
return result
end )
print( name, 's,v', status, value )
end
function testall( f, t, name )
test( f, t, nil, name..'nil' )
test( f, t, false, name..'fls' )
test( f, t, 100, name..'100' )
end
testall( table.foreach, t0, 'table.foreach('..eles(t0)..')' )
testall( table.foreach, t1, 'table.foreach('..eles(t1)..')' )
testall( table.foreach, t2, 'table.foreach('..eles(t2)..')' )
testall( table.foreach, t3, 'table.foreach('..eles(t3)..')' )
testall( table.foreachi, t0, 'table.foreachi('..eles(t0)..')' )
testall( table.foreachi, t1, 'table.foreachi('..eles(t1)..')' )
testall( table.foreachi, t2, 'table.foreachi('..eles(t2)..')' )
testall( table.foreachi, t3, 'table.foreachi('..eles(t3)..')' )
-- pairs, ipairs
function testpairs(f, t, name)
print( name )
for a,b in f(t) do
print( ' ', a, b )
end
end
function testbothpairs(t)
testpairs( pairs, t, 'pairs( '..eles(t)..' )' )
testpairs( ipairs, t, 'ipairs( '..eles(t)..' )' )
end
for i,t in ipairs({t0,t1,t2,t3}) do
testbothpairs(t)
end
t = newtable{ 'one', 'two', 'three', 'four', 'five' }
testbothpairs(t)
t[6] = 'six'
testbothpairs(t)
t[4] = nil
testbothpairs(t)