Add test for luajava, compiler, and compatibility witn luavm 1.0

This commit is contained in:
James Roseborough
2010-04-03 15:14:30 +00:00
parent a548ef27fb
commit 0d18988a96
17 changed files with 237 additions and 479 deletions

View File

@@ -14,5 +14,6 @@
<classpathentry kind="var" path="WTK_HOME/lib/cldcapi11.jar"/> <classpathentry kind="var" path="WTK_HOME/lib/cldcapi11.jar"/>
<classpathentry kind="var" path="WTK_HOME/lib/midpapi20.jar"/> <classpathentry kind="var" path="WTK_HOME/lib/midpapi20.jar"/>
<classpathentry kind="lib" path="lib/bcel-5.2.jar"/> <classpathentry kind="lib" path="lib/bcel-5.2.jar"/>
<classpathentry kind="lib" path="lib/luaj-j2se-1.0.3.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

Binary file not shown.

View File

@@ -30,7 +30,7 @@ public class Print extends Lua {
private static final String STRING_FOR_NULL = "null"; private static final String STRING_FOR_NULL = "null";
public static PrintStream ps = System.out; public static PrintStream ps = System.out;
private static final String[] luaP_opnames = { public static final String[] OPNAMES = {
"MOVE", "MOVE",
"LOADK", "LOADK",
"LOADBOOL", "LOADBOOL",
@@ -159,7 +159,7 @@ public class Print extends Lua {
ps.print("[" + line + "] "); ps.print("[" + line + "] ");
else else
ps.print("[-] "); ps.print("[-] ");
ps.print(luaP_opnames[o] + " "); ps.print(OPNAMES[o] + " ");
switch (getOpMode(o)) { switch (getOpMode(o)) {
case iABC: case iABC:
ps.print( a ); ps.print( a );

View File

@@ -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

View File

@@ -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

View File

@@ -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");
}
}

View File

@@ -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) );
}
}

View File

@@ -27,6 +27,7 @@ import junit.framework.TestSuite;
import org.luaj.vm2.WeakTableTest.WeakKeyTableTest; import org.luaj.vm2.WeakTableTest.WeakKeyTableTest;
import org.luaj.vm2.WeakTableTest.WeakKeyValueTableTest; import org.luaj.vm2.WeakTableTest.WeakKeyValueTableTest;
import org.luaj.vm2.lib.jse.LuaJavaCoercionTest; import org.luaj.vm2.lib.jse.LuaJavaCoercionTest;
import org.luaj.vm2.vm1.Luajvm1CompatibilityTest;
public class AllTests { public class AllTests {
@@ -58,6 +59,7 @@ public class AllTests {
// compatiblity tests // compatiblity tests
suite.addTest(CompatibiltyTest.suite()); suite.addTest(CompatibiltyTest.suite());
suite.addTestSuite(Luajvm1CompatibilityTest.class);
// luajc regression tests // luajc regression tests
TestSuite luajc = new TestSuite("Luajc Tests"); TestSuite luajc = new TestSuite("Luajc Tests");

View File

@@ -1,4 +1,4 @@
package org.luaj.compiler; package org.luaj.vm2.compiler;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -9,42 +9,44 @@ import java.net.URL;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.luaj.TestPlatform; import org.luaj.vm2.LoadState;
import org.luaj.vm.LPrototype; import org.luaj.vm2.LuaTable;
import org.luaj.vm.LoadState; import org.luaj.vm2.Print;
import org.luaj.vm.LuaState; import org.luaj.vm2.Prototype;
import org.luaj.vm.Platform; import org.luaj.vm2.lib.JsePlatform;
import org.luaj.vm.Print;
abstract public class AbstractUnitTests extends TestCase { abstract public class AbstractUnitTests extends TestCase {
private final String zipfile;
private final String dir; private final String dir;
private final String jar;
private LuaTable _G;
public AbstractUnitTests(String zipfile, String dir) { public AbstractUnitTests(String zipfile, String dir) {
this.zipfile = zipfile; URL zip = getClass().getResource(zipfile);
this.jar = "jar:" + zip.toExternalForm()+ "!/";
this.dir = dir; this.dir = dir;
} }
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
Platform.setInstance(new TestPlatform()); _G = JsePlatform.standardGlobals();
LuaC.install();
} }
protected void doTest(String file) { protected void doTest(String file) {
try { try {
// load source from jar // load source from jar
String path = "jar:file:" + zipfile + "!/" + dir + "/" + file; String path = jar + dir + "/" + file;
byte[] lua = bytesFromJar(path); byte[] lua = bytesFromJar(path);
// compile in memory // compile in memory
InputStream is = new ByteArrayInputStream(lua); InputStream is = new ByteArrayInputStream(lua);
LPrototype p = LuaC.compile(is, dir + "/" + file); Prototype p = LuaC.compile(is, dir + "/" + file);
String actual = protoToString(p); String actual = protoToString(p);
// load expected value from jar // load expected value from jar
byte[] luac = bytesFromJar(path + "c"); byte[] luac = bytesFromJar(path + "c");
LPrototype e = loadFromBytes(luac, file); Prototype e = loadFromBytes(luac, file);
String expected = protoToString(e); String expected = protoToString(e);
// compare results // compare results
@@ -56,7 +58,7 @@ abstract public class AbstractUnitTests extends TestCase {
byte[] dumped = baos.toByteArray(); byte[] dumped = baos.toByteArray();
// re-undump // re-undump
LPrototype p2 = loadFromBytes(dumped, file); Prototype p2 = loadFromBytes(dumped, file);
String actual2 = protoToString(p2); String actual2 = protoToString(p2);
// compare again // compare again
@@ -79,14 +81,13 @@ abstract public class AbstractUnitTests extends TestCase {
return baos.toByteArray(); return baos.toByteArray();
} }
protected LPrototype loadFromBytes(byte[] bytes, String script) protected Prototype loadFromBytes(byte[] bytes, String script)
throws IOException { throws IOException {
LuaState state = Platform.newLuaState();
InputStream is = new ByteArrayInputStream(bytes); InputStream is = new ByteArrayInputStream(bytes);
return LoadState.undump(state, is, script); return LoadState.compile(is, script);
} }
protected String protoToString(LPrototype p) { protected String protoToString(Prototype p) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos); PrintStream ps = new PrintStream(baos);
Print.ps = ps; Print.ps = ps;

View File

@@ -1,11 +1,11 @@
package org.luaj.compiler; package org.luaj.vm2.compiler;
public class CompilerUnitTests extends AbstractUnitTests { public class CompilerUnitTests extends AbstractUnitTests {
public CompilerUnitTests() { public CompilerUnitTests() {
super("src/test/compile/lua5.1-tests.zip", "lua5.1-tests"); super("lua5.1-tests.zip", "lua5.1-tests");
} }
public void testAll() { doTest("all.lua"); } public void testAll() { doTest("all.lua"); }

View File

@@ -1,4 +1,4 @@
package org.luaj.compiler; package org.luaj.vm2.compiler;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -9,11 +9,13 @@ import java.io.InputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.luaj.platform.J2sePlatform; import org.luaj.vm2.LoadState;
import org.luaj.vm.LFunction; import org.luaj.vm2.LuaClosure;
import org.luaj.vm.LPrototype; import org.luaj.vm2.LuaTable;
import org.luaj.vm.LuaState; import org.luaj.vm2.LuaValue;
import org.luaj.vm.Platform; import org.luaj.vm2.Prototype;
import org.luaj.vm2.lib.JsePlatform;
public class DumpLoadEndianIntTest extends TestCase { public class DumpLoadEndianIntTest extends TestCase {
private static final String SAVECHUNKS = "SAVECHUNKS"; private static final String SAVECHUNKS = "SAVECHUNKS";
@@ -25,9 +27,12 @@ public class DumpLoadEndianIntTest extends TestCase {
private static final String withdoubles = "1234-#!-23.75"; private static final String withdoubles = "1234-#!-23.75";
private static final String withints = "1234-#!-23"; private static final String withints = "1234-#!-23";
private LuaTable _G;
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
Platform.setInstance(new J2sePlatform()); _G = JsePlatform.standardGlobals();
LuaC.install();
DumpState.ALLOW_INTEGER_CASTING = false; DumpState.ALLOW_INTEGER_CASTING = false;
} }
@@ -76,17 +81,15 @@ public class DumpLoadEndianIntTest extends TestCase {
public void doTest( boolean littleEndian, int numberFormat, boolean stripDebug, public void doTest( boolean littleEndian, int numberFormat, boolean stripDebug,
String script, String expectedPriorDump, String expectedPostDump, boolean shouldPass ) { String script, String expectedPriorDump, String expectedPostDump, boolean shouldPass ) {
try { try {
LuaState vm = Platform.newLuaState();
// compile into prototype // compile into prototype
InputStream is = new ByteArrayInputStream(script.getBytes()); InputStream is = new ByteArrayInputStream(script.getBytes());
LPrototype p = LuaC.compile(is, "script"); Prototype p = LuaC.compile(is, "script");
// double check script result before dumping // double check script result before dumping
LFunction f = p.newClosure(vm._G); LuaClosure f = new LuaClosure(p, _G);
vm.pushfunction(f); LuaValue r = f.call();
vm.call(0,1); String actual = r.toString();
String actual = vm.poplvalue().toJavaString();
assertEquals( expectedPriorDump, actual ); assertEquals( expectedPriorDump, actual );
// dump into bytes // dump into bytes
@@ -105,9 +108,10 @@ public class DumpLoadEndianIntTest extends TestCase {
// load again using compiler // load again using compiler
is = new ByteArrayInputStream(dumped); is = new ByteArrayInputStream(dumped);
vm.load(is, "dumped"); p = LoadState.compile(is, "dumped");
vm.call(0,1); f = new LuaClosure(p, _G);
actual = vm.poplvalue().toJavaString(); r = f.call();
actual = r.toString();
assertEquals( expectedPostDump, actual ); assertEquals( expectedPostDump, actual );
// write test chunk // write test chunk

View File

@@ -1,4 +1,4 @@
package org.luaj.compiler; package org.luaj.vm2.compiler;
/** /**
* Framework to add regression tests as problem areas are found. * Framework to add regression tests as problem areas are found.
@@ -17,7 +17,7 @@ package org.luaj.compiler;
public class RegressionTests extends AbstractUnitTests { public class RegressionTests extends AbstractUnitTests {
public RegressionTests() { public RegressionTests() {
super( "src/test/compile/regressions.zip", super( "regressions.zip",
"regressions" ); "regressions" );
} }

View File

@@ -1,40 +1,40 @@
package org.luaj.compiler; package org.luaj.vm2.compiler;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.luaj.TestPlatform; import org.luaj.vm2.LuaClosure;
import org.luaj.lib.BaseLib; import org.luaj.vm2.LuaDouble;
import org.luaj.vm.LClosure; import org.luaj.vm2.LuaInteger;
import org.luaj.vm.LDouble; import org.luaj.vm2.LuaTable;
import org.luaj.vm.LInteger; import org.luaj.vm2.LuaValue;
import org.luaj.vm.LPrototype; import org.luaj.vm2.Print;
import org.luaj.vm.LValue; import org.luaj.vm2.Prototype;
import org.luaj.vm.LuaState; import org.luaj.vm2.lib.BaseLib;
import org.luaj.vm.Platform; import org.luaj.vm2.lib.JsePlatform;
import org.luaj.vm.Print;
public class SimpleTests extends TestCase { public class SimpleTests extends TestCase {
private LuaTable _G;
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
Platform.setInstance(new TestPlatform()); _G = JsePlatform.standardGlobals();
LuaC.install();
} }
private void doTest( String script ) { private void doTest( String script ) {
try { try {
InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") ); InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
LPrototype p = LuaC.compile( is, "script" ); Prototype p = LuaC.compile( is, "script" );
assertNotNull( p ); assertNotNull( p );
Print.printCode( p ); Print.printCode( p );
// try running the code! // try running the code!
LuaState state = Platform.newLuaState(); LuaClosure c = new LuaClosure( p, _G );
BaseLib.install( state._G ); c.call();
LClosure c = p.newClosure( state._G );
state.doCall( c, new LValue[0] );
} catch ( Exception e ) { } catch ( Exception e ) {
fail("i/o exception: "+e ); fail("i/o exception: "+e );
} }
@@ -93,15 +93,15 @@ public class SimpleTests extends TestCase {
public void testDoubleHashCode() { public void testDoubleHashCode() {
for ( int i=0; i<samehash.length; i++ ) { for ( int i=0; i<samehash.length; i++ ) {
LInteger j = LInteger.valueOf(samehash[i]); LuaValue j = LuaInteger.valueOf(samehash[i]);
LDouble d = new LDouble(samehash[i]); LuaValue d = LuaDouble.valueOf(samehash[i]);
int hj = j.hashCode(); int hj = j.hashCode();
int hd = d.hashCode(); int hd = d.hashCode();
assertEquals(hj, hd); assertEquals(hj, hd);
} }
for ( int i=0; i<diffhash.length; i+=2 ) { for ( int i=0; i<diffhash.length; i+=2 ) {
LDouble c = new LDouble(diffhash[i+0]); LuaValue c = LuaValue.valueOf(diffhash[i+0]);
LDouble d = new LDouble(diffhash[i+1]); LuaValue d = LuaValue.valueOf(diffhash[i+1]);
int hc = c.hashCode(); int hc = c.hashCode();
int hd = d.hashCode(); int hd = d.hashCode();
assertTrue("hash codes are same: "+hc,hc!=hd); assertTrue("hash codes are same: "+hc,hc!=hd);

View File

@@ -0,0 +1,167 @@
/*******************************************************************************
* Copyright (c) 2010 Luaj.org. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.vm2.vm1;
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.vm2.LuaValue;
/**
* Test for compatiblity between luaj 1.0 and 2.0 vms
*
*/
public class Luajvm1CompatibilityTest extends TestCase {
private static final String zipfile = "luajvm1-tests.zip";
private static String jarpath;
private void runTest(String test) {
try {
URL zip = getClass().getResource(zipfile);
jarpath = "jar:"+zip.toExternalForm()+"!/";
String luaj10 = luaj10Run(test);
String luaj20 = luaj20Run(test);
assertEquals( luaj10, luaj20 );
} catch ( IOException ioe ) {
fail( ioe.toString() );
}
}
private static InputStream open(String file) {
try {
String path = jarpath+file;
URL url = new URL(path);
return url.openStream();
} catch ( Exception e ) {
return null;
}
}
private String luaj10Run(String test) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
org.luaj.vm.Platform.setInstance(new org.luaj.platform.J2sePlatform() {
public InputStream openFile(String fileName) {
return open( fileName );
}
});
org.luaj.vm.LuaState vm = org.luaj.vm.Platform.newLuaState();
org.luaj.compiler.LuaC.install();
org.luaj.lib.DebugLib.install( vm );
org.luaj.lib.BaseLib.redirectOutput(outputStream);
vm.getglobal("require");
vm.pushstring(test);
vm.call(1,0);
return outputStream.toString();
} finally {
org.luaj.lib.BaseLib.restoreStandardOutput();
outputStream.close();
}
}
private String luaj20Run(String test) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream( outputStream );
try {
org.luaj.vm2.LuaTable _G = org.luaj.vm2.lib.JsePlatform.standardGlobals();
_G.get("package").get("loaders").checktable().insert(1, new org.luaj.vm2.lib.OneArgFunction(_G) {
public LuaValue call(LuaValue arg) {
String name = arg.toString();
String file = name + ".lua";
InputStream is = open( file );
if ( is == null )
return LuaValue.valueOf("not found: "+file);
try {
return org.luaj.vm2.LoadState.load(is, name, env);
} catch (IOException e) {
return LuaValue.valueOf(e.toString());
} finally {
try { is.close(); } catch ( IOException ioe ) {}
}
}
});
org.luaj.vm2.compiler.LuaC.install();
org.luaj.vm2.lib.BaseLib.instance.STDOUT = printStream;
_G.get("require").call(LuaValue.valueOf(test));
printStream.flush();
return outputStream.toString();
} finally {
printStream.close();
// script.close();
}
}
public Luajvm1CompatibilityTest() {
}
public void testTest1() { runTest("test1"); }
public void testTest2() { runTest("test2"); }
public void testTest3() { runTest("test3"); }
public void testTest4() { runTest("test4"); }
public void testTest5() { runTest("test5"); }
public void testTest6() { runTest("test6"); }
public void testTest7() { runTest("test7"); }
public void testTest8() { runTest("test8"); }
public void testTest9() { runTest("test9"); }
public void testAutoload() { runTest("autoload"); }
public void testBaseLib() { runTest("baselib"); }
public void testBoolean() { runTest("boolean"); }
public void testCalls() { runTest("calls"); }
public void testCoercions() { runTest("coercions"); }
public void testCoroutines() { runTest("coroutines"); }
public void testCompare() { runTest("compare"); }
public void testDebugLib() { runTest("debuglib"); }
public void testErrors() { runTest("errors"); }
public void testHugeTable() { runTest("hugetable"); }
public void testIoLib() { runTest("iolib"); }
public void testLoops() { runTest("loops"); }
public void testManyLocals() { runTest("manylocals"); }
public void testMathLib() { runTest("mathlib"); }
public void testMetatables() { runTest("metatables"); }
public void testModule() { runTest("module"); }
public void testNext() { runTest("next"); }
public void testOsLib() { runTest("oslib"); }
public void testPcalls() { runTest("pcalls"); }
public void testPrint() { runTest("print"); }
public void testRequire() { runTest("require"); }
public void testSelect() { runTest("select"); }
public void testSetfenv() { runTest("setfenv"); }
public void testSetlist() { runTest("setlist"); }
public void testSimpleMetatables() { runTest("simplemetatables"); }
public void testStack() { runTest("stack"); }
public void testStrLib() { runTest("strlib"); }
public void testSort() { runTest("sort"); }
public void testTable() { runTest("table"); }
public void testTailcall() { runTest("tailcall"); }
public void testType() { runTest("type"); }
public void testUpvalues() { runTest("upvalues"); }
public void testUpvalues2() { runTest("upvalues2"); }
public void testUpvalues3() { runTest("upvalues3"); }
public void testVarargs() { runTest("varargs"); }
public void testWeakTable() { runTest("weaktable"); }
}

Binary file not shown.