Implement math.pow() for all platforms, add unit tests for basic math operations.
This commit is contained in:
@@ -12,6 +12,7 @@ public class AllTests {
|
||||
TestSuite vm = new TestSuite("VM");
|
||||
vm.addTestSuite(org.luaj.vm.LoadStateTest.class);
|
||||
vm.addTestSuite(org.luaj.vm.LStringTest.class);
|
||||
vm.addTestSuite(org.luaj.vm.MathLibTest.class);
|
||||
vm.addTestSuite(org.luaj.vm.LTableTest.class);
|
||||
vm.addTestSuite(org.luaj.vm.LuaJTest.class);
|
||||
suite.addTest(vm);
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
package org.luaj.compiler;
|
||||
|
||||
import org.luaj.debug.j2se.J2sePlatform;
|
||||
import org.luaj.vm.LDouble;
|
||||
import org.luaj.vm.LNumber;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
|
||||
public class CompilerUnitTests extends AbstractUnitTests {
|
||||
|
||||
static {
|
||||
// override platform to test with standard debug features.
|
||||
Platform.setInstance( new J2sePlatform() {
|
||||
public LNumber mathPow(double lhs, double rhs) {
|
||||
double d = Math.pow(lhs, rhs);
|
||||
return LDouble.valueOf(d);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompilerUnitTests() {
|
||||
super( "src/test/compile/lua5.1-tests.zip",
|
||||
"lua5.1-tests" );
|
||||
|
||||
29
src/test/java/org/luaj/vm/MathLibTest.java
Normal file
29
src/test/java/org/luaj/vm/MathLibTest.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package org.luaj.vm;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class MathLibTest extends TestCase {
|
||||
|
||||
public void testMathDPow() {
|
||||
assertEquals( 1, LDouble.dpow(2, 0), 0 );
|
||||
assertEquals( 2, LDouble.dpow(2, 1), 0 );
|
||||
assertEquals( 8, LDouble.dpow(2, 3), 0 );
|
||||
assertEquals( -8, LDouble.dpow(-2, 3), 0 );
|
||||
assertEquals( 1/8., LDouble.dpow(2, -3), 0 );
|
||||
assertEquals( -1/8., LDouble.dpow(-2, -3), 0 );
|
||||
assertEquals( 16, LDouble.dpow(256, .5), 0 );
|
||||
assertEquals( 4, LDouble.dpow(256, .25), 0 );
|
||||
assertEquals( 64, LDouble.dpow(256, .75), 0 );
|
||||
assertEquals( 1./16, LDouble.dpow(256, - .5), 0 );
|
||||
assertEquals( 1./ 4, LDouble.dpow(256, -.25), 0 );
|
||||
assertEquals( 1./64, LDouble.dpow(256, -.75), 0 );
|
||||
assertEquals( Double.NaN, LDouble.dpow(-256, .5), 0 );
|
||||
assertEquals( 1, LDouble.dpow(.5, 0), 0 );
|
||||
assertEquals( .5, LDouble.dpow(.5, 1), 0 );
|
||||
assertEquals(.125, LDouble.dpow(.5, 3), 0 );
|
||||
assertEquals( 2, LDouble.dpow(.5, -1), 0 );
|
||||
assertEquals( 8, LDouble.dpow(.5, -3), 0 );
|
||||
assertEquals(1, LDouble.dpow(0.0625, 0), 0 );
|
||||
assertEquals(0.00048828125, LDouble.dpow(0.0625, 2.75), 0 );
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,48 @@ print( math.sin( 0.0 ) )
|
||||
print( math.cos( math.pi ) )
|
||||
print( math.sqrt( 9.0 ) )
|
||||
print( math.modf( 5.25 ) )
|
||||
|
||||
|
||||
-- binary ops
|
||||
function binops( a, b )
|
||||
local sa = tostring(a)
|
||||
local sb = tostring(b)
|
||||
print( sa..'+'..sb..'='..tostring(a+b) )
|
||||
print( sa..'-'..sb..'='..tostring(a-b) )
|
||||
print( sa..'*'..sb..'='..tostring(a*b) )
|
||||
print( sa..'^'..sb..'='..tostring(a^b) )
|
||||
print( sa..'/'..sb..'=',pcall( function() return a / b end ) )
|
||||
print( sa..'%'..sb..'=',pcall( function() return a % b end ) )
|
||||
return '--'
|
||||
end
|
||||
print( pcall( binops, 2, 0 ) )
|
||||
print( pcall( binops, -2, 0 ) )
|
||||
print( pcall( binops, 2.5, 0 ) )
|
||||
print( pcall( binops, -2.5, 0 ) )
|
||||
print( pcall( binops, 2, 1 ) )
|
||||
print( pcall( binops, 5, 2 ) )
|
||||
print( pcall( binops, -5, 2 ) )
|
||||
print( pcall( binops, 16, -2 ) )
|
||||
print( pcall( binops, -16, -2 ) )
|
||||
print( pcall( binops, 256, .5 ) )
|
||||
print( pcall( binops, 256, .25 ) )
|
||||
print( pcall( binops, 256, .625 ) )
|
||||
print( pcall( binops, 256, -.5 ) )
|
||||
print( pcall( binops, 256, -.25 ) )
|
||||
print( pcall( binops, 256, -.625 ) )
|
||||
print( pcall( binops, -256, .5 ) )
|
||||
print( pcall( binops, .5, 0 ) )
|
||||
print( pcall( binops, .5, 1 ) )
|
||||
print( pcall( binops, .5, 2 ) )
|
||||
print( pcall( binops, .5, -1 ) )
|
||||
print( pcall( binops, .5, -2 ) )
|
||||
print( pcall( binops, 2.25, 0 ) )
|
||||
print( pcall( binops, 2.25, 2 ) )
|
||||
print( pcall( binops, 2.25, .5 ) )
|
||||
print( pcall( binops, 2.25, 2.5 ) )
|
||||
|
||||
|
||||
-- random tests
|
||||
print( math.random(5,10) )
|
||||
print( math.random(5,10) )
|
||||
print( math.random(5,10) )
|
||||
@@ -10,7 +52,7 @@ print( math.random() )
|
||||
print( math.random() )
|
||||
print( math.random() )
|
||||
print( math.random() )
|
||||
print( math.randomseed(20), math.random() )
|
||||
print( math.randomseed(20), math.random(), math.random(), math.random() )
|
||||
print( math.randomseed(20), math.random() )
|
||||
print( math.randomseed(20), math.random() )
|
||||
print( math.randomseed(30), math.random() )
|
||||
|
||||
Reference in New Issue
Block a user