Add test for luajava, compiler, and compatibility witn luavm 1.0
This commit is contained in:
@@ -30,7 +30,7 @@ public class Print extends Lua {
|
||||
private static final String STRING_FOR_NULL = "null";
|
||||
public static PrintStream ps = System.out;
|
||||
|
||||
private static final String[] luaP_opnames = {
|
||||
public static final String[] OPNAMES = {
|
||||
"MOVE",
|
||||
"LOADK",
|
||||
"LOADBOOL",
|
||||
@@ -159,7 +159,7 @@ public class Print extends Lua {
|
||||
ps.print("[" + line + "] ");
|
||||
else
|
||||
ps.print("[-] ");
|
||||
ps.print(luaP_opnames[o] + " ");
|
||||
ps.print(OPNAMES[o] + " ");
|
||||
switch (getOpMode(o)) {
|
||||
case iABC:
|
||||
ps.print( a );
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,20 +0,0 @@
|
||||
#!/bin/bash
|
||||
LUA_HOME=/cygdrive/c/programs/lua5.1
|
||||
#DIRS="lua5.1-tests regressions"
|
||||
DIRS="regressions"
|
||||
for d in $DIRS; do
|
||||
|
||||
# clean out the old
|
||||
rm -f $d/*.luac
|
||||
|
||||
# compile the tests
|
||||
TESTS=`echo $d/*.lua`
|
||||
for x in $TESTS; do
|
||||
echo compiling $x
|
||||
luac -o ${x}c ${x}
|
||||
done
|
||||
|
||||
# rebuild the directory
|
||||
rm -f ${d}.zip
|
||||
jar -cvf ${d}.zip ${d}
|
||||
done
|
||||
@@ -1,12 +0,0 @@
|
||||
#!/bin/bash
|
||||
LUA_HOME=/cygdrive/c/programs/lua5.1
|
||||
#DIRS="lua5.1-tests regressions"
|
||||
DIRS="regressions"
|
||||
for d in $DIRS; do
|
||||
|
||||
# unpack files into the directory
|
||||
rm -rf $d
|
||||
mkdir -p $d
|
||||
jar -xvf $d.zip
|
||||
|
||||
done
|
||||
@@ -1,97 +0,0 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.URL;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.vm.LPrototype;
|
||||
import org.luaj.vm.LoadState;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
import org.luaj.vm.Print;
|
||||
|
||||
abstract public class AbstractUnitTests extends TestCase {
|
||||
|
||||
private final String zipfile;
|
||||
private final String dir;
|
||||
|
||||
public AbstractUnitTests(String zipfile, String dir) {
|
||||
this.zipfile = zipfile;
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance(new TestPlatform());
|
||||
}
|
||||
|
||||
protected void doTest(String file) {
|
||||
try {
|
||||
// load source from jar
|
||||
String path = "jar:file:" + zipfile + "!/" + dir + "/" + file;
|
||||
byte[] lua = bytesFromJar(path);
|
||||
|
||||
// compile in memory
|
||||
InputStream is = new ByteArrayInputStream(lua);
|
||||
LPrototype p = LuaC.compile(is, dir + "/" + file);
|
||||
String actual = protoToString(p);
|
||||
|
||||
// load expected value from jar
|
||||
byte[] luac = bytesFromJar(path + "c");
|
||||
LPrototype e = loadFromBytes(luac, file);
|
||||
String expected = protoToString(e);
|
||||
|
||||
// compare results
|
||||
assertEquals(expected, actual);
|
||||
|
||||
// dump into memory
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DumpState.dump(p, baos, false);
|
||||
byte[] dumped = baos.toByteArray();
|
||||
|
||||
// re-undump
|
||||
LPrototype p2 = loadFromBytes(dumped, file);
|
||||
String actual2 = protoToString(p2);
|
||||
|
||||
// compare again
|
||||
assertEquals(actual, actual2);
|
||||
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
protected byte[] bytesFromJar(String path) throws IOException {
|
||||
URL url = new URL(path);
|
||||
InputStream is = url.openStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[2048];
|
||||
int n;
|
||||
while ((n = is.read(buffer)) >= 0)
|
||||
baos.write(buffer, 0, n);
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
protected LPrototype loadFromBytes(byte[] bytes, String script)
|
||||
throws IOException {
|
||||
LuaState state = Platform.newLuaState();
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
return LoadState.undump(state, is, script);
|
||||
}
|
||||
|
||||
protected String protoToString(LPrototype p) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream ps = new PrintStream(baos);
|
||||
Print.ps = ps;
|
||||
new Print().printFunction(p, true);
|
||||
return baos.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
|
||||
|
||||
public class CompilerUnitTests extends AbstractUnitTests {
|
||||
|
||||
public CompilerUnitTests() {
|
||||
super("src/test/compile/lua5.1-tests.zip", "lua5.1-tests");
|
||||
}
|
||||
|
||||
public void testAll() { doTest("all.lua"); }
|
||||
public void testApi() { doTest("api.lua"); }
|
||||
public void testAttrib() { doTest("attrib.lua"); }
|
||||
public void testBig() { doTest("big.lua"); }
|
||||
public void testCalls() { doTest("calls.lua"); }
|
||||
public void testChecktable() { doTest("checktable.lua"); }
|
||||
public void testClosure() { doTest("closure.lua"); }
|
||||
public void testCode() { doTest("code.lua"); }
|
||||
public void testConstruct() { doTest("constructs.lua"); }
|
||||
public void testDb() { doTest("db.lua"); }
|
||||
public void testErrors() { doTest("errors.lua"); }
|
||||
public void testEvents() { doTest("events.lua"); }
|
||||
public void testFiles() { doTest("files.lua"); }
|
||||
public void testGc() { doTest("gc.lua"); }
|
||||
public void testLiterals() { doTest("literals.lua"); }
|
||||
public void testLocals() { doTest("locals.lua"); }
|
||||
public void testMain() { doTest("main.lua"); }
|
||||
public void testMath() { doTest("math.lua"); }
|
||||
public void testNextvar() { doTest("nextvar.lua"); }
|
||||
public void testPm() { doTest("pm.lua"); }
|
||||
public void testSort() { doTest("sort.lua"); }
|
||||
public void testStrings() { doTest("strings.lua"); }
|
||||
public void testVararg() { doTest("vararg.lua"); }
|
||||
public void testVerybig() { doTest("verybig.lua"); }
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.platform.J2sePlatform;
|
||||
import org.luaj.vm.LFunction;
|
||||
import org.luaj.vm.LPrototype;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
public class DumpLoadEndianIntTest extends TestCase {
|
||||
private static final String SAVECHUNKS = "SAVECHUNKS";
|
||||
|
||||
private static final boolean SHOULDPASS = true;
|
||||
private static final boolean SHOULDFAIL = false;
|
||||
private static final String mixedscript = "return tostring(1234)..'-#!-'..tostring(23.75)";
|
||||
private static final String intscript = "return tostring(1234)..'-#!-'..tostring(23)";
|
||||
private static final String withdoubles = "1234-#!-23.75";
|
||||
private static final String withints = "1234-#!-23";
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance(new J2sePlatform());
|
||||
DumpState.ALLOW_INTEGER_CASTING = false;
|
||||
}
|
||||
|
||||
public void testBigDoubleCompile() {
|
||||
doTest( false, DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES, false, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
doTest( false, DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES, true, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
}
|
||||
|
||||
public void testLittleDoubleCompile() {
|
||||
doTest( true, DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES, false, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
doTest( true, DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES, true, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
}
|
||||
|
||||
public void testBigIntCompile() {
|
||||
DumpState.ALLOW_INTEGER_CASTING = true;
|
||||
doTest( false, DumpState.NUMBER_FORMAT_INTS_ONLY, false, mixedscript, withdoubles, withints, SHOULDPASS );
|
||||
doTest( false, DumpState.NUMBER_FORMAT_INTS_ONLY, true, mixedscript, withdoubles, withints, SHOULDPASS );
|
||||
DumpState.ALLOW_INTEGER_CASTING = false;
|
||||
doTest( false, DumpState.NUMBER_FORMAT_INTS_ONLY, false, mixedscript, withdoubles, withints, SHOULDFAIL );
|
||||
doTest( false, DumpState.NUMBER_FORMAT_INTS_ONLY, true, mixedscript, withdoubles, withints, SHOULDFAIL );
|
||||
doTest( false, DumpState.NUMBER_FORMAT_INTS_ONLY, false, intscript, withints, withints, SHOULDPASS );
|
||||
doTest( false, DumpState.NUMBER_FORMAT_INTS_ONLY, true, intscript, withints, withints, SHOULDPASS );
|
||||
}
|
||||
|
||||
public void testLittleIntCompile() {
|
||||
DumpState.ALLOW_INTEGER_CASTING = true;
|
||||
doTest( true, DumpState.NUMBER_FORMAT_INTS_ONLY, false, mixedscript, withdoubles, withints, SHOULDPASS );
|
||||
doTest( true, DumpState.NUMBER_FORMAT_INTS_ONLY, true, mixedscript, withdoubles, withints, SHOULDPASS );
|
||||
DumpState.ALLOW_INTEGER_CASTING = false;
|
||||
doTest( true, DumpState.NUMBER_FORMAT_INTS_ONLY, false, mixedscript, withdoubles, withints, SHOULDFAIL );
|
||||
doTest( true, DumpState.NUMBER_FORMAT_INTS_ONLY, true, mixedscript, withdoubles, withints, SHOULDFAIL );
|
||||
doTest( true, DumpState.NUMBER_FORMAT_INTS_ONLY, false, intscript, withints, withints, SHOULDPASS );
|
||||
doTest( true, DumpState.NUMBER_FORMAT_INTS_ONLY, true, intscript, withints, withints, SHOULDPASS );
|
||||
}
|
||||
|
||||
public void testBigNumpatchCompile() {
|
||||
doTest( false, DumpState.NUMBER_FORMAT_NUM_PATCH_INT32, false, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
doTest( false, DumpState.NUMBER_FORMAT_NUM_PATCH_INT32, true, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
}
|
||||
|
||||
public void testLittleNumpatchCompile() {
|
||||
doTest( true, DumpState.NUMBER_FORMAT_NUM_PATCH_INT32, false, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
doTest( true, DumpState.NUMBER_FORMAT_NUM_PATCH_INT32, true, mixedscript, withdoubles, withdoubles, SHOULDPASS );
|
||||
}
|
||||
|
||||
public void doTest( boolean littleEndian, int numberFormat, boolean stripDebug,
|
||||
String script, String expectedPriorDump, String expectedPostDump, boolean shouldPass ) {
|
||||
try {
|
||||
LuaState vm = Platform.newLuaState();
|
||||
|
||||
// compile into prototype
|
||||
InputStream is = new ByteArrayInputStream(script.getBytes());
|
||||
LPrototype p = LuaC.compile(is, "script");
|
||||
|
||||
// double check script result before dumping
|
||||
LFunction f = p.newClosure(vm._G);
|
||||
vm.pushfunction(f);
|
||||
vm.call(0,1);
|
||||
String actual = vm.poplvalue().toJavaString();
|
||||
assertEquals( expectedPriorDump, actual );
|
||||
|
||||
// dump into bytes
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
DumpState.dump(p, baos, stripDebug, numberFormat, littleEndian);
|
||||
if ( ! shouldPass )
|
||||
fail( "dump should not have succeeded" );
|
||||
} catch ( Exception e ) {
|
||||
if ( shouldPass )
|
||||
fail( "dump threw "+e );
|
||||
else
|
||||
return;
|
||||
}
|
||||
byte[] dumped = baos.toByteArray();
|
||||
|
||||
// load again using compiler
|
||||
is = new ByteArrayInputStream(dumped);
|
||||
vm.load(is, "dumped");
|
||||
vm.call(0,1);
|
||||
actual = vm.poplvalue().toJavaString();
|
||||
assertEquals( expectedPostDump, actual );
|
||||
|
||||
// write test chunk
|
||||
if ( System.getProperty(SAVECHUNKS) != null && script.equals(mixedscript) ) {
|
||||
new File("build").mkdirs();
|
||||
String filename = "build/test-"
|
||||
+(littleEndian? "little-": "big-")
|
||||
+(numberFormat==DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES? "double-":
|
||||
numberFormat==DumpState.NUMBER_FORMAT_INTS_ONLY? "int-":
|
||||
numberFormat==DumpState.NUMBER_FORMAT_NUM_PATCH_INT32? "numpatch4-": "???-")
|
||||
+(stripDebug? "nodebug-": "debug-")
|
||||
+"bin.lua";
|
||||
FileOutputStream fos = new FileOutputStream(filename);
|
||||
fos.write( dumped );
|
||||
fos.close();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
/**
|
||||
* Framework to add regression tests as problem areas are found.
|
||||
*
|
||||
* To add a new regression test:
|
||||
* 1) run "unpack.sh" in the project root
|
||||
* 2) add a new "lua" file in the "regressions" subdirectory
|
||||
* 3) run "repack.sh" in the project root
|
||||
* 4) add a line to the source file naming the new test
|
||||
*
|
||||
* After adding a test, check in the zip file
|
||||
* rather than the individual regression test files.
|
||||
*
|
||||
* @author jrosebor
|
||||
*/
|
||||
public class RegressionTests extends AbstractUnitTests {
|
||||
|
||||
public RegressionTests() {
|
||||
super( "src/test/compile/regressions.zip",
|
||||
"regressions" );
|
||||
}
|
||||
|
||||
public void testModulo() { doTest("modulo.lua"); }
|
||||
public void testConstruct() { doTest("construct.lua"); }
|
||||
public void testBigAttrs() { doTest("bigattr.lua"); }
|
||||
public void testControlChars() { doTest("controlchars.lua"); }
|
||||
public void testComparators() { doTest("comparators.lua"); }
|
||||
public void testMathRandomseed() { doTest("mathrandomseed.lua"); }
|
||||
public void testVarargs() { doTest("varargs.lua"); }
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.TestPlatform;
|
||||
import org.luaj.lib.BaseLib;
|
||||
import org.luaj.vm.LClosure;
|
||||
import org.luaj.vm.LDouble;
|
||||
import org.luaj.vm.LInteger;
|
||||
import org.luaj.vm.LPrototype;
|
||||
import org.luaj.vm.LValue;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
import org.luaj.vm.Print;
|
||||
|
||||
public class SimpleTests extends TestCase {
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance(new TestPlatform());
|
||||
}
|
||||
|
||||
private void doTest( String script ) {
|
||||
try {
|
||||
InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
|
||||
LPrototype p = LuaC.compile( is, "script" );
|
||||
assertNotNull( p );
|
||||
Print.printCode( p );
|
||||
|
||||
// try running the code!
|
||||
LuaState state = Platform.newLuaState();
|
||||
BaseLib.install( state._G );
|
||||
LClosure c = p.newClosure( state._G );
|
||||
state.doCall( c, new LValue[0] );
|
||||
} catch ( Exception e ) {
|
||||
fail("i/o exception: "+e );
|
||||
}
|
||||
}
|
||||
|
||||
public void testTrivial() {
|
||||
String s = "print( 2 )\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
public void testAlmostTrivial() {
|
||||
String s = "print( 2 )\n" +
|
||||
"print( 3 )\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
public void testSimple() {
|
||||
String s = "print( 'hello, world' )\n"+
|
||||
"for i = 2,4 do\n" +
|
||||
" print( 'i', i )\n" +
|
||||
"end\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
public void testBreak() {
|
||||
String s = "a=1\n"+
|
||||
"while true do\n"+
|
||||
" if a>10 then\n"+
|
||||
" break\n"+
|
||||
" end\n"+
|
||||
" a=a+1\n"+
|
||||
" print( a )\n"+
|
||||
"end\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
public void testShebang() {
|
||||
String s = "#!../lua\n"+
|
||||
"print( 2 )\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
public void testInlineTable() {
|
||||
String s = "A = {g=10}\n"+
|
||||
"print( A )\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
public void testEqualsAnd() {
|
||||
String s = "print( 1 == b and b )\n";
|
||||
doTest( s );
|
||||
}
|
||||
|
||||
private static final int [] samehash = { 0, 1, -1, 2, -2, 4, 8, 16, 32, Integer.MAX_VALUE, Integer.MIN_VALUE };
|
||||
private static final double [] diffhash = { .5, 1, 1.5, 1, .5, 1.5, 1.25, 2.5 };
|
||||
|
||||
public void testDoubleHashCode() {
|
||||
for ( int i=0; i<samehash.length; i++ ) {
|
||||
LInteger j = LInteger.valueOf(samehash[i]);
|
||||
LDouble d = new LDouble(samehash[i]);
|
||||
int hj = j.hashCode();
|
||||
int hd = d.hashCode();
|
||||
assertEquals(hj, hd);
|
||||
}
|
||||
for ( int i=0; i<diffhash.length; i+=2 ) {
|
||||
LDouble c = new LDouble(diffhash[i+0]);
|
||||
LDouble d = new LDouble(diffhash[i+1]);
|
||||
int hc = c.hashCode();
|
||||
int hd = d.hashCode();
|
||||
assertTrue("hash codes are same: "+hc,hc!=hd);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
package org.luaj.vm;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Compatibility tests for the Luaj VM
|
||||
*
|
||||
* Results are compared for exact match with
|
||||
* the installed C-based lua environment.
|
||||
*/
|
||||
public class CompatibiltyTest extends ScriptDrivenTest {
|
||||
|
||||
private static final String dir = "src/test/res";
|
||||
|
||||
public CompatibiltyTest() {
|
||||
super(dir);
|
||||
}
|
||||
|
||||
public void testTest1() throws IOException, InterruptedException {
|
||||
runTest("test1");
|
||||
}
|
||||
|
||||
public void testTest2() throws IOException, InterruptedException {
|
||||
runTest("test2");
|
||||
}
|
||||
|
||||
public void testTest3() throws IOException, InterruptedException {
|
||||
runTest("test3");
|
||||
}
|
||||
|
||||
public void testTest4() throws IOException, InterruptedException {
|
||||
runTest("test4");
|
||||
}
|
||||
|
||||
public void testTest5() throws IOException, InterruptedException {
|
||||
runTest("test5");
|
||||
}
|
||||
|
||||
public void testTest6() throws IOException, InterruptedException {
|
||||
runTest("test6");
|
||||
}
|
||||
|
||||
public void testTest7() throws IOException, InterruptedException {
|
||||
runTest("test7");
|
||||
}
|
||||
|
||||
public void testTest8() throws IOException, InterruptedException {
|
||||
runTest("test8");
|
||||
}
|
||||
|
||||
public void testTest9() throws IOException, InterruptedException {
|
||||
runTest("test9");
|
||||
}
|
||||
|
||||
public void testAutoload() throws IOException, InterruptedException {
|
||||
runTest("autoload");
|
||||
}
|
||||
|
||||
public void testBaseLib() throws IOException, InterruptedException {
|
||||
runTest("baselib");
|
||||
}
|
||||
|
||||
public void testBoolean() throws IOException, InterruptedException {
|
||||
runTest("boolean");
|
||||
}
|
||||
|
||||
public void testCalls() throws IOException, InterruptedException {
|
||||
runTest("calls");
|
||||
}
|
||||
|
||||
public void testCoercions() throws IOException, InterruptedException {
|
||||
runTest("coercions");
|
||||
}
|
||||
|
||||
public void testCoroutines() throws IOException, InterruptedException {
|
||||
runTest("coroutines");
|
||||
}
|
||||
|
||||
public void testCompare() throws IOException, InterruptedException {
|
||||
runTest("compare");
|
||||
}
|
||||
|
||||
public void testDebugLib() throws IOException, InterruptedException {
|
||||
runTest("debuglib");
|
||||
}
|
||||
|
||||
public void testErrors() throws IOException, InterruptedException {
|
||||
runTest("errors");
|
||||
}
|
||||
|
||||
public void testHugeTable() throws IOException, InterruptedException {
|
||||
runTest("hugetable");
|
||||
}
|
||||
|
||||
public void testIoLib() throws IOException, InterruptedException {
|
||||
runTest("iolib");
|
||||
}
|
||||
|
||||
public void testLoops() throws IOException, InterruptedException {
|
||||
runTest("loops");
|
||||
}
|
||||
|
||||
public void testManyLocals() throws IOException, InterruptedException {
|
||||
runTest("manylocals");
|
||||
}
|
||||
|
||||
public void testMathLib() throws IOException, InterruptedException {
|
||||
runTest("mathlib");
|
||||
}
|
||||
|
||||
public void testMetatables() throws IOException, InterruptedException {
|
||||
runTest("metatables");
|
||||
}
|
||||
|
||||
public void testModule() throws IOException, InterruptedException {
|
||||
runTest("module");
|
||||
}
|
||||
|
||||
public void testNext() throws IOException, InterruptedException {
|
||||
runTest("next");
|
||||
}
|
||||
|
||||
public void testOsLib() throws IOException, InterruptedException {
|
||||
runTest("oslib");
|
||||
}
|
||||
|
||||
public void testPcalls() throws IOException, InterruptedException {
|
||||
runTest("pcalls");
|
||||
}
|
||||
|
||||
public void testPrint() throws IOException, InterruptedException {
|
||||
runTest("print");
|
||||
}
|
||||
|
||||
public void testRequire() throws IOException, InterruptedException {
|
||||
runTest("require");
|
||||
}
|
||||
|
||||
public void testSelect() throws IOException, InterruptedException {
|
||||
runTest("select");
|
||||
}
|
||||
|
||||
public void testSetfenv() throws IOException, InterruptedException {
|
||||
runTest("setfenv");
|
||||
}
|
||||
|
||||
public void testSetlist() throws IOException, InterruptedException {
|
||||
runTest("setlist");
|
||||
}
|
||||
|
||||
public void testSimpleMetatables() throws IOException, InterruptedException {
|
||||
runTest("simplemetatables");
|
||||
}
|
||||
|
||||
public void testStack() throws IOException, InterruptedException {
|
||||
runTest("stack");
|
||||
}
|
||||
|
||||
public void testStrLib() throws IOException, InterruptedException {
|
||||
runTest("strlib");
|
||||
}
|
||||
|
||||
public void testSort() throws IOException, InterruptedException {
|
||||
runTest("sort");
|
||||
}
|
||||
|
||||
public void testTable() throws IOException, InterruptedException {
|
||||
runTest("table");
|
||||
}
|
||||
|
||||
public void testTailcall() throws IOException, InterruptedException {
|
||||
runTest("tailcall");
|
||||
}
|
||||
|
||||
public void testType() throws IOException, InterruptedException {
|
||||
runTest("type");
|
||||
}
|
||||
|
||||
public void testUpvalues() throws IOException, InterruptedException {
|
||||
runTest("upvalues");
|
||||
}
|
||||
|
||||
public void testUpvalues2() throws IOException, InterruptedException {
|
||||
runTest("upvalues2");
|
||||
}
|
||||
|
||||
public void testUpvalues3() throws IOException, InterruptedException {
|
||||
runTest("upvalues3");
|
||||
}
|
||||
|
||||
public void testVarargs() throws IOException, InterruptedException {
|
||||
runTest("varargs");
|
||||
}
|
||||
|
||||
public void testWeakTable() throws IOException, InterruptedException {
|
||||
runTest("weaktable");
|
||||
}
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
package org.luaj.vm;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.luaj.lib.j2se.CoerceJavaToLua;
|
||||
import org.luaj.lib.j2se.CoerceLuaToJava;
|
||||
import org.luaj.platform.J2sePlatform;
|
||||
|
||||
public class LuaJavaCoercionTest extends TestCase {
|
||||
|
||||
private LuaState vm;
|
||||
private static LInteger ZERO = LInteger.valueOf(0);
|
||||
private static LInteger ONE = LInteger.valueOf(1);
|
||||
private static LInteger TWO = LInteger.valueOf(2);
|
||||
private static LInteger THREE = LInteger.valueOf(3);
|
||||
private static LString LENGTH = LString.valueOf("length");
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Platform.setInstance( new J2sePlatform() );
|
||||
org.luaj.compiler.LuaC.install();
|
||||
vm = Platform.newLuaState();
|
||||
}
|
||||
|
||||
public void testJavaIntToLuaInt() {
|
||||
Integer i = Integer.valueOf(777);
|
||||
LValue v = CoerceJavaToLua.coerce(i);
|
||||
assertEquals( LInteger.class, v.getClass() );
|
||||
assertEquals( 777, v.toJavaInt() );
|
||||
}
|
||||
|
||||
public void testLuaIntToJavaInt() {
|
||||
LInteger i = LInteger.valueOf(777);
|
||||
Object o = CoerceLuaToJava.coerceArg(i, int.class);
|
||||
assertEquals( Integer.class, o.getClass() );
|
||||
assertEquals( 777, ((Number)o).intValue() );
|
||||
o = CoerceLuaToJava.coerceArg(i, Integer.class);
|
||||
assertEquals( Integer.class, o.getClass() );
|
||||
assertEquals( new Integer(777), o );
|
||||
}
|
||||
|
||||
public void testJavaStringToLuaString() {
|
||||
String s = new String("777");
|
||||
LValue v = CoerceJavaToLua.coerce(s);
|
||||
assertEquals( LString.class, v.getClass() );
|
||||
assertEquals( "777", v.toJavaString() );
|
||||
}
|
||||
|
||||
public void testLuaStringToJavaString() {
|
||||
LString s = new LString("777");
|
||||
Object o = CoerceLuaToJava.coerceArg(s, String.class);
|
||||
assertEquals( String.class, o.getClass() );
|
||||
assertEquals( "777", o );
|
||||
}
|
||||
|
||||
public void testJavaIntArrayToLuaTable() {
|
||||
int[] i = { 222, 333 };
|
||||
LValue v = CoerceJavaToLua.coerce(i);
|
||||
assertEquals( LUserData.class, v.getClass() );
|
||||
assertNotNull( v.luaGetMetatable() );
|
||||
assertEquals( LInteger.valueOf(222), v.luaGetTable(vm, ONE) );
|
||||
assertEquals( LInteger.valueOf(333), v.luaGetTable(vm, TWO) );
|
||||
assertEquals( TWO, v.luaGetTable(vm, LENGTH));
|
||||
assertEquals( LNil.NIL, v.luaGetTable(vm, THREE) );
|
||||
assertEquals( LNil.NIL, v.luaGetTable(vm, ZERO) );
|
||||
v.luaSetTable(vm, ONE, LInteger.valueOf(444));
|
||||
v.luaSetTable(vm, TWO, LInteger.valueOf(555));
|
||||
assertEquals( 444, i[0] );
|
||||
assertEquals( 555, i[1] );
|
||||
assertEquals( LInteger.valueOf(444), v.luaGetTable(vm, ONE) );
|
||||
assertEquals( LInteger.valueOf(555), v.luaGetTable(vm, TWO) );
|
||||
try {
|
||||
v.luaSetTable(vm, ZERO, LInteger.valueOf(777));
|
||||
fail( "array bound exception not thrown" );
|
||||
} catch ( LuaErrorException lee ) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
v.luaSetTable(vm, THREE, LInteger.valueOf(777));
|
||||
fail( "array bound exception not thrown" );
|
||||
} catch ( LuaErrorException lee ) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testLuaTableToJavaIntArray() {
|
||||
LTable t = new LTable();
|
||||
t.put(1, LInteger.valueOf(222) );
|
||||
t.put(2, LInteger.valueOf(333) );
|
||||
int[] i = null;
|
||||
Object o = CoerceLuaToJava.coerceArg(t, int[].class);
|
||||
assertEquals( int[].class, o.getClass() );
|
||||
i = (int[]) o;
|
||||
assertEquals( 2, i.length );
|
||||
assertEquals( 222, i[0] );
|
||||
assertEquals( 333, i[1] );
|
||||
}
|
||||
|
||||
|
||||
public void testArrayParamScoring() {
|
||||
int a = 5;
|
||||
int[] b = { 44, 66 };
|
||||
int[][] c = { { 11, 22 }, { 33, 44 } };
|
||||
LValue la = LInteger.valueOf(a);
|
||||
LTable tb = new LTable();
|
||||
LTable tc = new LTable();
|
||||
LValue va = CoerceJavaToLua.coerce(a);
|
||||
LValue vb = CoerceJavaToLua.coerce(b);
|
||||
LValue vc = CoerceJavaToLua.coerce(c);
|
||||
tc.put( ONE, new LTable() );
|
||||
|
||||
int saa = CoerceLuaToJava.scoreParamTypes( new LValue[] { la }, new Class[] { int.class } );
|
||||
int sab = CoerceLuaToJava.scoreParamTypes( new LValue[] { la }, new Class[] { int[].class } );
|
||||
int sac = CoerceLuaToJava.scoreParamTypes( new LValue[] { la }, new Class[] { int[][].class } );
|
||||
assertTrue( saa < sab );
|
||||
assertTrue( saa < sac );
|
||||
int sba = CoerceLuaToJava.scoreParamTypes( new LValue[] { tb }, new Class[] { int.class } );
|
||||
int sbb = CoerceLuaToJava.scoreParamTypes( new LValue[] { tb }, new Class[] { int[].class } );
|
||||
int sbc = CoerceLuaToJava.scoreParamTypes( new LValue[] { tb }, new Class[] { int[][].class } );
|
||||
assertTrue( sbb < sba );
|
||||
assertTrue( sbb < sbc );
|
||||
int sca = CoerceLuaToJava.scoreParamTypes( new LValue[] { tc }, new Class[] { int.class } );
|
||||
int scb = CoerceLuaToJava.scoreParamTypes( new LValue[] { tc }, new Class[] { int[].class } );
|
||||
int scc = CoerceLuaToJava.scoreParamTypes( new LValue[] { tc }, new Class[] { int[][].class } );
|
||||
assertTrue( scc < sca );
|
||||
assertTrue( scc < scb );
|
||||
|
||||
int vaa = CoerceLuaToJava.scoreParamTypes( new LValue[] { va }, new Class[] { int.class } );
|
||||
int vab = CoerceLuaToJava.scoreParamTypes( new LValue[] { va }, new Class[] { int[].class } );
|
||||
int vac = CoerceLuaToJava.scoreParamTypes( new LValue[] { va }, new Class[] { int[][].class } );
|
||||
assertTrue( vaa < vab );
|
||||
assertTrue( vaa < vac );
|
||||
int vba = CoerceLuaToJava.scoreParamTypes( new LValue[] { vb }, new Class[] { int.class } );
|
||||
int vbb = CoerceLuaToJava.scoreParamTypes( new LValue[] { vb }, new Class[] { int[].class } );
|
||||
int vbc = CoerceLuaToJava.scoreParamTypes( new LValue[] { vb }, new Class[] { int[][].class } );
|
||||
assertTrue( vbb < vba );
|
||||
assertTrue( vbb < vbc );
|
||||
int vca = CoerceLuaToJava.scoreParamTypes( new LValue[] { vc }, new Class[] { int.class } );
|
||||
int vcb = CoerceLuaToJava.scoreParamTypes( new LValue[] { vc }, new Class[] { int[].class } );
|
||||
int vcc = CoerceLuaToJava.scoreParamTypes( new LValue[] { vc }, new Class[] { int[][].class } );
|
||||
assertTrue( vcc < vca );
|
||||
assertTrue( vcc < vcb );
|
||||
}
|
||||
|
||||
public static class SampleClass {
|
||||
public String sample() { return "void-args"; }
|
||||
public String sample(int a) { return "int-args "+a; }
|
||||
public String sample(int[] a) { return "int-array-args "+a[0]+","+a[1]; }
|
||||
public String sample(int[][] a) { return "int-array-array-args "+a[0][0]+","+a[0][1]+","+a[1][0]+","+a[1][1]; }
|
||||
}
|
||||
|
||||
private static final LString SAMPLE = LString.valueOf("sample");
|
||||
|
||||
public void testIntArrayParameterMatching() {
|
||||
LValue v = CoerceJavaToLua.coerce(new SampleClass());
|
||||
|
||||
// get sample field, call with no arguments
|
||||
LValue method = v.luaGetTable(vm, SAMPLE);
|
||||
vm.pushlvalue(method);
|
||||
vm.pushlvalue(v);
|
||||
vm.call(1,1);
|
||||
assertEquals( "void-args", vm.tostring(-1) );
|
||||
|
||||
// get sample field, call with no arguments
|
||||
vm.pushlvalue(method);
|
||||
vm.pushlvalue(v);
|
||||
vm.pushlvalue( CoerceJavaToLua.coerce(new Integer(123)));
|
||||
vm.call(2,1);
|
||||
assertEquals( "int-args 123", vm.tostring(-1) );
|
||||
|
||||
// get sample field, call with no arguments
|
||||
vm.pushlvalue(method);
|
||||
vm.pushlvalue(v);
|
||||
vm.pushlvalue( CoerceJavaToLua.coerce(new int[]{345,678}) );
|
||||
vm.call(2,1);
|
||||
assertEquals( "int-array-args 345,678", vm.tostring(-1) );
|
||||
|
||||
// get sample field, call with no arguments
|
||||
vm.pushlvalue(method);
|
||||
vm.pushlvalue(v);
|
||||
vm.pushlvalue( CoerceJavaToLua.coerce(new int[][]{{22,33},{44,55}}) );
|
||||
vm.call(2,1);
|
||||
assertEquals( "int-array-array-args 22,33,44,55", vm.tostring(-1) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user