Add unit tests used in vm1

This commit is contained in:
James Roseborough
2010-05-09 18:10:55 +00:00
parent 7c72a9feaf
commit 40b7cb19c9
36 changed files with 487 additions and 1444 deletions

View File

@@ -44,6 +44,7 @@ public class AllTests {
vm.addTestSuite(UnaryBinaryOperatorsTest.class);
vm.addTestSuite(MetatableTest.class);
vm.addTestSuite(LuaOperationsTest.class);
vm.addTestSuite(StringTest.class);
suite.addTest(vm);
// table tests
@@ -67,11 +68,14 @@ public class AllTests {
// library tests
TestSuite lib = new TestSuite("Library Tests");
lib.addTestSuite(LuaJavaCoercionTest.class);
lib.addTestSuite(RequireClassTest.class);
suite.addTest(lib);
// compatiblity tests
suite.addTest(CompatibiltyTest.suite());
suite.addTestSuite(Luajvm1CompatibilityTest.class);
TestSuite compat = (TestSuite) CompatibiltyTest.suite();
suite.addTest( compat );
compat.addTestSuite(ErrorsTest.class);
compat.addTestSuite(Luajvm1CompatibilityTest.class);
// luajc regression tests
TestSuite luajc = new TestSuite("Java Compiler Tests");

View File

@@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2009 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;
import org.luaj.vm2.compiler.LuaC;
/**
* Test argument type check errors
*
* Results are compared for exact match with
* the installed C-based lua environment.
*/
public class ErrorsTest extends ScriptDrivenTest {
private static final String dir = "test/lua/errors";
public ErrorsTest() {
super(ScriptDrivenTest.PlatformType.JSE, dir);
}
protected void setUp() throws Exception {
super.setUp();
LuaC.install();
}
public void testBaseLibArgs() { runTest("baselibargs"); }
public void testCoroutineLibArgs() { runTest("coroutinelibargs"); }
public void testIoLibArgs() { runTest("iolibargs"); }
public void testMathLibArgs() { runTest("mathlibargs"); }
public void testModuleLibArgs() { runTest("modulelibargs"); }
public void testOperators() { runTest("operators"); }
public void testStringLibArgs() { runTest("stringlibargs"); }
public void testTableLibArgs() { runTest("tablelibargs"); }
}

View File

@@ -0,0 +1,251 @@
package org.luaj.vm2;
import junit.framework.TestCase;
import org.luaj.vm2.lib.JsePlatform;
import org.luaj.vm2.lib.MathLib;
import org.luaj.vm2.lib.jse.JseMathLib;
public class MathLibTest extends TestCase {
private LuaValue j2se;
private LuaValue j2me;
private boolean supportedOnJ2me;
public MathLibTest() {
LuaValue g = JsePlatform.standardGlobals();
j2se = g.get("math");
g.load( new MathLib() );
j2me = g.get("math");
}
protected void setUp() throws Exception {
supportedOnJ2me = true;
}
public void testMathDPow() {
assertEquals( 1, j2mepow(2, 0), 0 );
assertEquals( 2, j2mepow(2, 1), 0 );
assertEquals( 8, j2mepow(2, 3), 0 );
assertEquals( -8, j2mepow(-2, 3), 0 );
assertEquals( 1/8., j2mepow(2, -3), 0 );
assertEquals( -1/8., j2mepow(-2, -3), 0 );
assertEquals( 16, j2mepow(256, .5), 0 );
assertEquals( 4, j2mepow(256, .25), 0 );
assertEquals( 64, j2mepow(256, .75), 0 );
assertEquals( 1./16, j2mepow(256, - .5), 0 );
assertEquals( 1./ 4, j2mepow(256, -.25), 0 );
assertEquals( 1./64, j2mepow(256, -.75), 0 );
assertEquals( Double.NaN, j2mepow(-256, .5), 0 );
assertEquals( 1, j2mepow(.5, 0), 0 );
assertEquals( .5, j2mepow(.5, 1), 0 );
assertEquals(.125, j2mepow(.5, 3), 0 );
assertEquals( 2, j2mepow(.5, -1), 0 );
assertEquals( 8, j2mepow(.5, -3), 0 );
assertEquals(1, j2mepow(0.0625, 0), 0 );
assertEquals(0.00048828125, j2mepow(0.0625, 2.75), 0 );
}
private double j2mepow(double x, double y) {
return j2me.get("pow").call(LuaValue.valueOf(x),LuaValue.valueOf(y)).todouble();
}
public void testAbs() {
tryMathOp( "abs", 23.45 );
tryMathOp( "abs", -23.45 );
}
public void testCos() {
tryTrigOps( "cos" );
}
public void testCosh() {
supportedOnJ2me = false;
tryTrigOps( "cosh" );
}
public void testDeg() {
tryTrigOps( "deg" );
}
public void testExp() {
//supportedOnJ2me = false;
tryMathOp( "exp", 0 );
tryMathOp( "exp", 0.1 );
tryMathOp( "exp", .9 );
tryMathOp( "exp", 1. );
tryMathOp( "exp", 9 );
tryMathOp( "exp", -.1 );
tryMathOp( "exp", -.9 );
tryMathOp( "exp", -1. );
tryMathOp( "exp", -9 );
}
public void testLog() {
supportedOnJ2me = false;
tryMathOp( "log", 0.1 );
tryMathOp( "log", .9 );
tryMathOp( "log", 1. );
tryMathOp( "log", 9 );
tryMathOp( "log", -.1 );
tryMathOp( "log", -.9 );
tryMathOp( "log", -1. );
tryMathOp( "log", -9 );
}
public void testLog10() {
supportedOnJ2me = false;
tryMathOp( "log10", 0.1 );
tryMathOp( "log10", .9 );
tryMathOp( "log10", 1. );
tryMathOp( "log10", 9 );
tryMathOp( "log10", 10 );
tryMathOp( "log10", 100 );
tryMathOp( "log10", -.1 );
tryMathOp( "log10", -.9 );
tryMathOp( "log10", -1. );
tryMathOp( "log10", -9 );
tryMathOp( "log10", -10 );
tryMathOp( "log10", -100 );
}
public void testRad() {
tryMathOp( "rad", 0 );
tryMathOp( "rad", 0.1 );
tryMathOp( "rad", .9 );
tryMathOp( "rad", 1. );
tryMathOp( "rad", 9 );
tryMathOp( "rad", 10 );
tryMathOp( "rad", 100 );
tryMathOp( "rad", -.1 );
tryMathOp( "rad", -.9 );
tryMathOp( "rad", -1. );
tryMathOp( "rad", -9 );
tryMathOp( "rad", -10 );
tryMathOp( "rad", -100 );
}
public void testSin() {
tryTrigOps( "sin" );
}
public void testSinh() {
supportedOnJ2me = false;
tryTrigOps( "sinh" );
}
public void testSqrt() {
tryMathOp( "sqrt", 0 );
tryMathOp( "sqrt", 0.1 );
tryMathOp( "sqrt", .9 );
tryMathOp( "sqrt", 1. );
tryMathOp( "sqrt", 9 );
tryMathOp( "sqrt", 10 );
tryMathOp( "sqrt", 100 );
}
public void testTan() {
tryTrigOps( "tan" );
}
public void testTanh() {
supportedOnJ2me = false;
tryTrigOps( "tanh" );
}
public void testAtan2() {
supportedOnJ2me = false;
tryDoubleOps( "atan2", false );
}
public void testFmod() {
tryDoubleOps( "fmod", false );
}
public void testPow() {
tryDoubleOps( "pow", true );
}
private void tryDoubleOps( String op, boolean positiveOnly ) {
// y>0, x>0
tryMathOp( op, 0.1, 4.0 );
tryMathOp( op, .9, 4.0 );
tryMathOp( op, 1., 4.0 );
tryMathOp( op, 9, 4.0 );
tryMathOp( op, 10, 4.0 );
tryMathOp( op, 100, 4.0 );
// y>0, x<0
tryMathOp( op, 0.1, -4.0 );
tryMathOp( op, .9, -4.0 );
tryMathOp( op, 1., -4.0 );
tryMathOp( op, 9, -4.0 );
tryMathOp( op, 10, -4.0 );
tryMathOp( op, 100, -4.0 );
if ( ! positiveOnly ) {
// y<0, x>0
tryMathOp( op, -0.1, 4.0 );
tryMathOp( op, -.9, 4.0 );
tryMathOp( op, -1., 4.0 );
tryMathOp( op, -9, 4.0 );
tryMathOp( op, -10, 4.0 );
tryMathOp( op, -100, 4.0 );
// y<0, x<0
tryMathOp( op, -0.1, -4.0 );
tryMathOp( op, -.9, -4.0 );
tryMathOp( op, -1., -4.0 );
tryMathOp( op, -9, -4.0 );
tryMathOp( op, -10, -4.0 );
tryMathOp( op, -100, -4.0 );
}
// degenerate cases
tryMathOp( op, 0, 1 );
tryMathOp( op, 1, 0 );
tryMathOp( op, -1, 0 );
tryMathOp( op, 0, -1 );
tryMathOp( op, 0, 0 );
}
private void tryTrigOps(String op) {
tryMathOp( op, 0 );
tryMathOp( op, Math.PI/8 );
tryMathOp( op, Math.PI*7/8 );
tryMathOp( op, Math.PI*8/8 );
tryMathOp( op, Math.PI*9/8 );
tryMathOp( op, -Math.PI/8 );
tryMathOp( op, -Math.PI*7/8 );
tryMathOp( op, -Math.PI*8/8 );
tryMathOp( op, -Math.PI*9/8 );
}
private void tryMathOp(String op, double x) {
try {
double expected = j2se.get(op).call( LuaValue.valueOf(x)).todouble();
double actual = j2me.get(op).call( LuaValue.valueOf(x)).todouble();
if ( supportedOnJ2me )
assertEquals( expected, actual, 1.e-4 );
else
fail("j2me should throw exception for math."+op+" but returned "+actual);
} catch ( LuaError lee ) {
if ( supportedOnJ2me )
throw lee;
}
}
private void tryMathOp(String op, double a, double b) {
try {
double expected = j2se.get(op).call( LuaValue.valueOf(a), LuaValue.valueOf(b)).todouble();
double actual = j2me.get(op).call( LuaValue.valueOf(a), LuaValue.valueOf(b)).todouble();
if ( supportedOnJ2me )
assertEquals( expected, actual, 1.e-5 );
else
fail("j2me should throw exception for math."+op+" but returned "+actual);
} catch ( LuaError lee ) {
if ( supportedOnJ2me )
throw lee;
}
}
}

View File

@@ -0,0 +1,84 @@
package org.luaj.vm2;
import junit.framework.TestCase;
import org.luaj.vm2.lib.JsePlatform;
import org.luaj.vm2.require.RequireSampleClassCastExcep;
import org.luaj.vm2.require.RequireSampleLoadLuaError;
import org.luaj.vm2.require.RequireSampleLoadRuntimeExcep;
public class RequireClassTest extends TestCase {
private LuaTable globals;
private LuaValue require;
public void setUp() {
globals = JsePlatform.standardGlobals();
require = globals.get("require");
}
public void testRequireClassSuccess() {
LuaValue result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") );
assertEquals( "require-sample-success", result.tojstring() );
result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") );
assertEquals( "require-sample-success", result.tojstring() );
}
public void testRequireClassLoadLuaError() {
try {
LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadLuaError.class.getName()) );
fail( "incorrectly loaded class that threw lua error");
} catch ( LuaError le ) {
assertEquals(
"sample-load-lua-error",
le.getMessage() );
}
try {
LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadLuaError.class.getName()) );
fail( "incorrectly loaded class that threw lua error");
} catch ( LuaError le ) {
assertEquals(
"loop or previous error loading module '"+RequireSampleLoadLuaError.class.getName()+"'",
le.getMessage() );
}
}
public void testRequireClassLoadRuntimeException() {
try {
LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadRuntimeExcep.class.getName()) );
fail( "incorrectly loaded class that threw runtime exception");
} catch ( RuntimeException le ) {
assertEquals(
"sample-load-runtime-exception",
le.getMessage() );
}
try {
LuaValue result = require.call( LuaValue.valueOf(RequireSampleLoadRuntimeExcep.class.getName()) );
fail( "incorrectly loaded class that threw runtime exception");
} catch ( LuaError le ) {
assertEquals(
"loop or previous error loading module '"+RequireSampleLoadRuntimeExcep.class.getName()+"'",
le.getMessage() );
}
}
public void testRequireClassClassCastException() {
try {
LuaValue result = require.call( LuaValue.valueOf(RequireSampleClassCastExcep.class.getName()) );
fail( "incorrectly loaded class that threw class cast exception");
} catch ( LuaError le ) {
String msg = le.getMessage();
if ( msg.indexOf("not found") < 0 )
fail( "expected 'not found' message but got "+msg );
}
try {
LuaValue result = require.call( LuaValue.valueOf(RequireSampleClassCastExcep.class.getName()) );
fail( "incorrectly loaded class that threw class cast exception");
} catch ( LuaError le ) {
String msg = le.getMessage();
if ( msg.indexOf("not found") < 0 )
fail( "expected 'not found' message but got "+msg );
}
}
}

View File

@@ -0,0 +1,107 @@
package org.luaj.vm2;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import junit.framework.TestCase;
import org.luaj.vm2.lib.JsePlatform;
public class StringTest extends TestCase {
protected void setUp() throws Exception {
JsePlatform.standardGlobals();
}
public void testToInputStream() throws IOException {
LuaString str = LuaString.valueOf("Hello");
InputStream is = str.toInputStream();
assertEquals( 'H', is.read() );
assertEquals( 'e', is.read() );
assertEquals( 2, is.skip( 2 ) );
assertEquals( 'o', is.read() );
assertEquals( -1, is.read() );
assertTrue( is.markSupported() );
is.reset();
assertEquals( 'H', is.read() );
is.mark( 4 );
assertEquals( 'e', is.read() );
is.reset();
assertEquals( 'e', is.read() );
LuaString substr = str.substring( 1, 4 );
assertEquals( 3, substr.length() );
is.close();
is = substr.toInputStream();
assertEquals( 'e', is.read() );
assertEquals( 'l', is.read() );
assertEquals( 'l', is.read() );
assertEquals( -1, is.read() );
is = substr.toInputStream();
is.reset();
assertEquals( 'e', is.read() );
}
private static final String userFriendly( String s ) {
StringBuffer sb = new StringBuffer();
for ( int i=0, n=s.length(); i<n; i++ ) {
int c = s.charAt(i);
if ( c < ' ' || c >= 0x80 ) {
sb.append( "\\u"+Integer.toHexString(0x10000+c).substring(1) );
} else {
sb.append( (char) c );
}
}
return sb.toString();
}
public void testUtf8() {
for ( int i=4; i<0xffff; i+=4 ) {
char[] c = { (char) (i+0), (char) (i+1), (char) (i+2), (char) (i+3) };
String before = new String(c)+" "+i+"-"+(i+4);
LuaString ls = LuaString.valueOf(before);
String after = ls.tojstring();
assertEquals( userFriendly( before ), userFriendly( after ) );
}
char[] c = { (char) (1), (char) (2), (char) (3) };
String before = new String(c)+" 1-3";
LuaString ls = LuaString.valueOf(before);
String after = ls.tojstring();
assertEquals( userFriendly( before ), userFriendly( after ) );
}
public void testSpotCheckUtf8() throws UnsupportedEncodingException {
byte[] bytes = {(byte)194,(byte)160,(byte)194,(byte)161,(byte)194,(byte)162,(byte)194,(byte)163,(byte)194,(byte)164};
String expected = new String(bytes, "UTF8");
String actual = LuaString.valueOf(bytes).tojstring();
char[] d = actual.toCharArray();
assertEquals(160, d[0]);
assertEquals(161, d[1]);
assertEquals(162, d[2]);
assertEquals(163, d[3]);
assertEquals(164, d[4]);
}
public void testNullTerminated() {
char[] c = { 'a', 'b', 'c', '\0', 'd', 'e', 'f' };
String before = new String(c);
LuaString ls = LuaString.valueOf(before);
String after = ls.tojstring();
assertEquals( userFriendly( "abc" ), userFriendly( after ) );
}
}

View File

@@ -0,0 +1,17 @@
package org.luaj.vm2.require;
import org.luaj.vm2.LuaValue;
/**
* This should fail while trying to load via "require() because it is not a LibFunction"
*
*/
public class RequireSampleClassCastExcep {
public RequireSampleClassCastExcep() {
}
public LuaValue call() {
return LuaValue.valueOf("require-sample-class-cast-excep");
}
}

View File

@@ -0,0 +1,20 @@
package org.luaj.vm2.require;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ZeroArgFunction;
/**
* This should fail while trying to load via
* "require()" because it throws a LuaError
*
*/
public class RequireSampleLoadLuaError extends ZeroArgFunction {
public RequireSampleLoadLuaError() {
}
public LuaValue call() {
error("sample-load-lua-error");
return LuaValue.valueOf("require-sample-load-lua-error");
}
}

View File

@@ -0,0 +1,18 @@
package org.luaj.vm2.require;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ZeroArgFunction;
/**
* This should fail while trying to load via "require()" because it throws a RuntimeException
*
*/
public class RequireSampleLoadRuntimeExcep extends ZeroArgFunction {
public RequireSampleLoadRuntimeExcep() {
}
public LuaValue call() {
throw new RuntimeException("sample-load-runtime-exception");
}
}

View File

@@ -0,0 +1,17 @@
package org.luaj.vm2.require;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ZeroArgFunction;
/**
* This should succeed as a library that can be loaded dynamically via "require()"
*/
public class RequireSampleSuccess extends ZeroArgFunction {
public RequireSampleSuccess() {
}
public LuaValue call() {
return LuaValue.valueOf("require-sample-success");
}
}