Fix a bug with table resizing. Includes junit test case.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user