package org.luaj.jit;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.TestCase;

import org.luaj.compiler.LuaC;
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 testForLoop() throws IOException {
    	stringTest( "print 'starting'\n" +
				"for i=1,3 do\n" +
				"	print( 'i', i )\n" +
				"end");
    }
        
	private void stringTest(String program) throws IOException {		
		InputStream is = new ByteArrayInputStream(program.getBytes());
		LPrototype p = LuaC.compile(is, "program");
		run( p );
		LPrototype q = LuaJit.jitCompile( p );
		assertTrue(p!=q);
		run( q );
	}
	
	private static void run(LPrototype p) {
		LuaState vm = Platform.newLuaState();
		LClosure c = p.newClosure(vm._G);
		vm.pushlvalue(c);
		vm.call(0, 0);
	}
    
}

package org.luaj.jit;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.TestCase;

import org.luaj.compiler.LuaC;
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 testForInDoEnd() throws IOException {    	
    }
        
    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" +
    		"repeat\n"+
	        "	print(i)\n"+
	        "until i\n");
    }
        
    public void testWhileDoEnd() throws IOException {    	
    	stringTest( 
    		"local i=4" +
    		"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" +
			"repeat\n"+
	        "	print(i)\n"+
			"	break\n"+
	        "until i\n");
    }
        
    public void testWhileDoBreak() throws IOException {    	
    	stringTest( 
    		"local i=4" +
    		"while i>0 do\n"+
	        "	print( i )\n"+
			"	break\n"+
	        "	i = i-1\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");
		run( p );
		LPrototype q = LuaJit.jitCompile( p );
		assertTrue(p!=q);
		run( q );
	}
	
	private static void run(LPrototype p) {
		LuaState vm = Platform.newLuaState();
		LClosure c = p.newClosure(vm._G);
		vm.pushlvalue(c);
		vm.call(0, 0);
	}
    
}

Add basic jit tests.