From ddf9acc506cd22304143538c1e7f15ed83526948 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Fri, 11 Apr 2008 00:08:19 +0000 Subject: [PATCH] improve table tests. --- src/test/java/org/luaj/vm/LTableTest.java | 170 +++++++++++++++++++--- 1 file changed, 150 insertions(+), 20 deletions(-) diff --git a/src/test/java/org/luaj/vm/LTableTest.java b/src/test/java/org/luaj/vm/LTableTest.java index e75cd65d..d58ab144 100644 --- a/src/test/java/org/luaj/vm/LTableTest.java +++ b/src/test/java/org/luaj/vm/LTableTest.java @@ -1,11 +1,6 @@ package org.luaj.vm; -import org.luaj.vm.LDouble; -import org.luaj.vm.LInteger; -import org.luaj.vm.LNil; -import org.luaj.vm.LString; -import org.luaj.vm.LTable; -import org.luaj.vm.LValue; +import java.util.Vector; import junit.framework.TestCase; @@ -17,6 +12,11 @@ public class LTableTest extends TestCase { for ( int i = 1; i <= 32; ++i ) { t.put( i, new LString( "Test Value! "+i ) ); } + + // Ensure all keys are still there. + for ( int i = 1; i <= 32; ++i ) { + assertEquals( "Test Value! " + i, t.get( i ).toJavaString() ); + } // Ensure capacities make sense assertEquals( 0, t.getHashCapacity() ); @@ -24,10 +24,6 @@ public class LTableTest extends TestCase { assertTrue( t.getArrayCapacity() >= 32 ); assertTrue( t.getArrayCapacity() <= 64 ); - // Ensure all keys are still there. - for ( int i = 1; i <= 32; ++i ) { - assertEquals( "Test Value! " + i, t.get( i ).luaAsString().toJavaString() ); - } } public void testResize() { @@ -45,8 +41,8 @@ public class LTableTest extends TestCase { assertEquals(LInteger.valueOf(i), t.get(i)); } - assertEquals( 0, t.getHashCapacity() ); - assertTrue( t.getArrayCapacity() >= 6 ); + assertTrue( t.getArrayCapacity() >= 0 && t.getArrayCapacity() <= 2 ); + assertTrue( t.getHashCapacity() >= 4 ); } public void testOutOfOrderIntegerKeyInsertion() { @@ -55,17 +51,19 @@ public class LTableTest extends TestCase { for ( int i = 32; i > 0; --i ) { t.put( i, new LString( "Test Value! "+i ) ); } - - // Ensure capacities make sense - assertEquals( 0, t.getHashCapacity() ); - - assertTrue( t.getArrayCapacity() >= 32 ); - assertTrue( t.getArrayCapacity() <= 64 ); - + // Ensure all keys are still there. for ( int i = 1; i <= 32; ++i ) { - assertEquals( "Test Value! " + i, t.get( i ).luaAsString() ); + assertEquals( "Test Value! "+i, t.get( i ).toJavaString() ); } + + // Ensure capacities make sense + assertTrue( t.getArrayCapacity() >= 0 ); + assertTrue( t.getArrayCapacity() <= 6 ); + + assertTrue( t.getHashCapacity() >= 16 ); + assertTrue( t.getHashCapacity() <= 64 ); + } public void testStringAndIntegerKeys() { @@ -177,4 +175,136 @@ public class LTableTest extends TestCase { assertEquals( 0, t.size() ); } + public void testInOrderLuaLength() { + LTable t = new LTable(); + + for ( int i = 1; i <= 32; ++i ) { + t.put( i, new LString( "Test Value! "+i ) ); + assertEquals( i, t.luaLength() ); + assertEquals( i, t.luaMaxN().toJavaInt() ); + } + } + + public void testOutOfOrderLuaLength() { + LTable t = new LTable(); + + for ( int j=8; j<32; j+=8 ) { + for ( int i = j; i > 0; --i ) { + t.put( i, new LString( "Test Value! "+i ) ); + } + assertEquals( j, t.luaLength() ); + assertEquals( j, t.luaMaxN().toJavaInt() ); + } + } + + public void testStringKeysLuaLength() { + LTable t = new LTable(); + + for ( int i = 1; i <= 32; ++i ) { + t.put( "str-"+i, new LString( "String Key Test Value! "+i ) ); + assertEquals( 0, t.luaLength() ); + assertEquals( 0, t.luaMaxN().toJavaInt() ); + } + } + + public void testMixedKeysLuaLength() { + LTable t = new LTable(); + + for ( int i = 1; i <= 32; ++i ) { + t.put( "str-"+i, new LString( "String Key Test Value! "+i ) ); + t.put( i, new LString( "Int Key Test Value! "+i ) ); + assertEquals( i, t.luaLength() ); + assertEquals( i, t.luaMaxN().toJavaInt() ); + } + } + + private static final void compareLists(LTable t,Vector v) { + int n = v.size(); + assertEquals(v.size(),t.luaLength()); + for ( int j=0; j