449594 Handle ArrayTrie overflow with false return

This commit is contained in:
Greg Wilkins 2014-11-05 10:32:10 +11:00
parent 04fc803b0e
commit 63d6754ea1
2 changed files with 27 additions and 2 deletions

View File

@ -99,6 +99,7 @@ public class ArrayTrie<V> extends AbstractTrie<V>
this(128);
}
@SuppressWarnings("unchecked")
public ArrayTrie(int capacityInNodes)
{
super(true);
@ -151,8 +152,14 @@ public class ArrayTrie<V> extends AbstractTrie<V>
}
}
}
if (t>=_key.length)
{
_rows=(char)_key.length;
return false;
}
_key[t]=v==null?null:s;
V old=(V)_value[t];
_value[t] = v;
return true;
}
@ -440,6 +447,6 @@ public class ArrayTrie<V> extends AbstractTrie<V>
@Override
public boolean isFull()
{
return _rows+1==_key.length;
return _rows+1>=_key.length;
}
}

View File

@ -65,6 +65,24 @@ public class TrieTest
trie.put("",9);
}
@Test
public void testOverflow() throws Exception
{
int i=0;
while (true)
{
if (++i>10000)
break; // must not be fixed size
if (!trie.put("prefix" + i, i))
{
Assert.assertTrue(trie.isFull());
break;
}
}
Assert.assertTrue(!trie.isFull() || !trie.put("overflow", 0));
}
@Test
public void testKeySet() throws Exception
{