2010-04-03 15:14:30 +00:00
|
|
|
package org.luaj.vm2.compiler;
|
2008-08-21 05:34:43 +00:00
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2008-08-21 21:57:52 +00:00
|
|
|
import java.io.File;
|
2008-08-21 14:52:14 +00:00
|
|
|
import java.io.FileOutputStream;
|
2008-08-21 05:34:43 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
2013-09-18 05:32:30 +00:00
|
|
|
import java.io.Reader;
|
|
|
|
|
import java.io.StringReader;
|
2008-08-21 05:34:43 +00:00
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
2013-09-18 05:32:30 +00:00
|
|
|
import org.luaj.vm2.Globals;
|
2010-04-03 15:14:30 +00:00
|
|
|
import org.luaj.vm2.LoadState;
|
|
|
|
|
import org.luaj.vm2.LuaClosure;
|
2010-07-25 22:31:43 +00:00
|
|
|
import org.luaj.vm2.LuaFunction;
|
2010-04-03 15:14:30 +00:00
|
|
|
import org.luaj.vm2.LuaValue;
|
|
|
|
|
import org.luaj.vm2.Prototype;
|
2010-07-30 18:09:31 +00:00
|
|
|
import org.luaj.vm2.lib.jse.JsePlatform;
|
2010-04-03 15:14:30 +00:00
|
|
|
|
2008-08-21 05:34:43 +00:00
|
|
|
|
|
|
|
|
public class DumpLoadEndianIntTest extends TestCase {
|
2008-08-21 21:57:52 +00:00
|
|
|
private static final String SAVECHUNKS = "SAVECHUNKS";
|
2008-08-21 05:34:43 +00:00
|
|
|
|
2008-08-21 21:57:52 +00:00
|
|
|
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)";
|
2008-08-21 05:34:43 +00:00
|
|
|
private static final String withdoubles = "1234-#!-23.75";
|
|
|
|
|
private static final String withints = "1234-#!-23";
|
2010-04-03 15:14:30 +00:00
|
|
|
|
2013-12-29 22:49:23 +00:00
|
|
|
private Globals globals;
|
2010-04-03 15:14:30 +00:00
|
|
|
|
2008-08-21 05:34:43 +00:00
|
|
|
protected void setUp() throws Exception {
|
|
|
|
|
super.setUp();
|
2013-12-29 22:49:23 +00:00
|
|
|
globals = JsePlatform.standardGlobals();
|
2008-08-21 21:57:52 +00:00
|
|
|
DumpState.ALLOW_INTEGER_CASTING = false;
|
2008-08-21 05:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-21 21:57:52 +00:00
|
|
|
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 );
|
2008-08-21 05:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testLittleDoubleCompile() {
|
2008-08-21 21:57:52 +00:00
|
|
|
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 );
|
2008-08-21 05:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testBigIntCompile() {
|
2008-08-21 21:57:52 +00:00
|
|
|
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 );
|
2008-08-21 05:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void testLittleIntCompile() {
|
2008-08-21 21:57:52 +00:00
|
|
|
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 );
|
2008-08-21 05:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-21 21:57:52 +00:00
|
|
|
public void doTest( boolean littleEndian, int numberFormat, boolean stripDebug,
|
|
|
|
|
String script, String expectedPriorDump, String expectedPostDump, boolean shouldPass ) {
|
2008-08-21 05:34:43 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// compile into prototype
|
2013-09-18 05:32:30 +00:00
|
|
|
Reader reader = new StringReader(script);
|
2013-12-29 22:49:23 +00:00
|
|
|
Prototype p = globals.compilePrototype(reader, "script");
|
2008-08-21 05:34:43 +00:00
|
|
|
|
|
|
|
|
// double check script result before dumping
|
2013-12-29 22:49:23 +00:00
|
|
|
LuaFunction f = new LuaClosure(p, globals);
|
2010-04-03 15:14:30 +00:00
|
|
|
LuaValue r = f.call();
|
2010-04-25 15:50:40 +00:00
|
|
|
String actual = r.tojstring();
|
2008-08-21 21:57:52 +00:00
|
|
|
assertEquals( expectedPriorDump, actual );
|
2008-08-21 05:34:43 +00:00
|
|
|
|
|
|
|
|
// dump into bytes
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
2008-08-21 21:57:52 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2008-08-21 05:34:43 +00:00
|
|
|
byte[] dumped = baos.toByteArray();
|
2008-08-21 14:52:14 +00:00
|
|
|
|
2008-08-21 05:34:43 +00:00
|
|
|
// load again using compiler
|
2013-09-18 05:32:30 +00:00
|
|
|
InputStream is = new ByteArrayInputStream(dumped);
|
2013-12-29 22:49:23 +00:00
|
|
|
f = globals.load(is, "dumped", "b", globals).checkfunction();
|
2010-04-03 15:14:30 +00:00
|
|
|
r = f.call();
|
2010-04-25 15:50:40 +00:00
|
|
|
actual = r.tojstring();
|
2008-08-21 21:57:52 +00:00
|
|
|
assertEquals( expectedPostDump, actual );
|
2008-08-21 05:34:43 +00:00
|
|
|
|
2008-08-21 14:52:14 +00:00
|
|
|
// write test chunk
|
2008-08-21 21:57:52 +00:00
|
|
|
if ( System.getProperty(SAVECHUNKS) != null && script.equals(mixedscript) ) {
|
|
|
|
|
new File("build").mkdirs();
|
|
|
|
|
String filename = "build/test-"
|
2008-08-21 14:52:14 +00:00
|
|
|
+(littleEndian? "little-": "big-")
|
2008-08-21 21:57:52 +00:00
|
|
|
+(numberFormat==DumpState.NUMBER_FORMAT_FLOATS_OR_DOUBLES? "double-":
|
|
|
|
|
numberFormat==DumpState.NUMBER_FORMAT_INTS_ONLY? "int-":
|
|
|
|
|
numberFormat==DumpState.NUMBER_FORMAT_NUM_PATCH_INT32? "numpatch4-": "???-")
|
2008-08-21 14:52:14 +00:00
|
|
|
+(stripDebug? "nodebug-": "debug-")
|
|
|
|
|
+"bin.lua";
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(filename);
|
|
|
|
|
fos.write( dumped );
|
|
|
|
|
fos.close();
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-21 05:34:43 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
fail(e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|