Fix a bug with table resizing. Includes junit test case.

This commit is contained in:
Ian Farmer
2008-01-23 01:21:54 +00:00
parent f55a393fb8
commit 4fa417bdd9
2 changed files with 24 additions and 1 deletions

View File

@@ -499,7 +499,7 @@ public class LTable extends LValue {
int slot = findSlot( i+1 );
if ( m_hashKeys[ slot ] != null ) {
newVector[ i ] = m_hashValues[ slot ];
m_hashKeys[ i ] = null;
m_hashKeys[ slot ] = null;
--m_hashEntries;
} else {
// Make sure all array-part values are initialized to nil
@@ -508,6 +508,10 @@ public class LTable extends LValue {
newVector[ i ] = LNil.NIL;
}
}
if ( m_hashEntries == 0 ) {
m_hashKeys = null;
m_hashValues = null;
}
} else {
for ( int i = oldCapacity; i < newCapacity; ++i ) {
newVector[ i ] = LNil.NIL;

View File

@@ -30,6 +30,25 @@ public class LTableTest extends TestCase {
}
}
public void testResize() {
LTable t = new LTable();
// NOTE: This order of insertion is important.
t.put(3, LInteger.valueOf(3));
t.put(1, LInteger.valueOf(1));
t.put(5, LInteger.valueOf(5));
t.put(4, LInteger.valueOf(4));
t.put(6, LInteger.valueOf(6));
t.put(2, LInteger.valueOf(2));
for ( int i = 1; i < 6; ++i ) {
assertEquals(LInteger.valueOf(i), t.get(i));
}
assertEquals( 0, t.getHashCapacity() );
assertTrue( t.getArrayCapacity() >= 6 );
}
public void testOutOfOrderIntegerKeyInsertion() {
LTable t = new LTable();